-fpermissive to allow GCC to compile old C++
[oweals/cde.git] / cde / programs / dtmail / dtmail / AliasListUiItem.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $TOG: AliasListUiItem.C /main/6 1997/09/03 17:33:31 mgreess $ */
24 /*
25  *+SNOTICE
26  *
27  *      $TOG: AliasListUiItem.C /main/6 1997/09/03 17:33:31 mgreess $
28  *
29  *      RESTRICTED CONFIDENTIAL INFORMATION:
30  *      
31  *      The information in this document is subject to special
32  *      restrictions in a confidential disclosure agreement bertween
33  *      HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
34  *      document outside HP, IBM, Sun, USL, SCO, or Univel wihtout
35  *      Sun's specific written approval.  This documment and all copies
36  *      and derivative works thereof must be returned or destroyed at
37  *      Sun's request.
38  *
39  *      Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
40  *
41  *+ENOTICE
42  */
43
44 #include "RoamApp.h"
45 #include <Xm/List.h>
46 #include <DtMail/options_util.h>
47 #include "options_ui.h"
48 #include <DtMail/PropUi.hh>
49 #include <DtMail/ListUiItem.hh>
50 #include <DtMail/AliasListUiItem.hh>
51
52 extern Boolean props_changed;
53 void alias_stuffing_func(char * key, void * data, void * client_data);
54 void handleAliasSelection(Widget w, XtPointer clientdata, XtPointer calldata);
55
56 // AliasListUiItem::AliasListUiItem
57 // AliasListUiItem ctor
58 ////////////////////////////////////////////////////////////////////
59
60 AliasListUiItem::AliasListUiItem(Widget w, 
61                        int source, 
62                        char *search_key,
63                        Widget key_entry_widget,
64                        Widget value_entry_widget):ListUiItem(w, source, search_key, NULL)
65 {
66   source = source; search_key = search_key;
67
68   key_widget = key_entry_widget;
69   value_widget = value_entry_widget;
70
71   list_items = NULL;
72   deleted_items = NULL;
73
74   XtVaSetValues(w,
75         XmNuserData, this,
76         XmNautomaticSelection, True,
77         XmNselectionPolicy, XmBROWSE_SELECT,
78         NULL);
79
80     XtAddCallback(w,
81                   XmNbrowseSelectionCallback, 
82                   (XtCallbackProc)handleAliasSelection,
83                   (XtPointer)this);
84
85 }
86
87 //-----------------======================-----------------
88 void handleAliasSelection(Widget w, XtPointer, XtPointer calldata)
89 {
90   AliasListUiItem *item;
91   XmListCallbackStruct *list_info = (XmListCallbackStruct *)calldata;
92   char *selection_string = NULL;
93   DtVirtArray<PropStringPair *> *list_items;
94
95   XtVaGetValues(w, 
96         XmNuserData, &item,
97         NULL);  
98
99   list_items = item->getItemList();
100
101   if(list_items != NULL)
102     {                                         // motif index is 1 based
103                                               //virtarry is 0 based
104       PropStringPair *pair = (*list_items)[list_info->item_position - 1];
105
106       if(pair != NULL)
107         {
108           XtVaSetValues(item->getKeyWidget(),
109                         XmNvalue,pair->label,
110                         NULL);
111           
112           XtVaSetValues(item->getValueWidget(),
113                         XmNvalue,pair->value,
114                         NULL);
115         }
116     }   
117 }
118
119 // AliasListUiItem::writeFromUiToSource()
120 // Takes values in the UI and puts them into the source DB
121 ///////////////////////////////////////////////////////////////////
122 void AliasListUiItem::writeFromUiToSource()
123 {
124   Widget w = this->getWidget();
125   DtMailEnv error;
126   DtMail::Session * d_session = theRoamApp.session()->session();
127   DtMail::MailRc * mail_rc = d_session->mailRc(error);
128   int i, num_items;
129
130   if(deleted_items != NULL)
131     {
132       num_items = deleted_items->length();
133
134       for(i = 0; i < num_items; i++)
135         {
136           mail_rc->removeAlias(error,(*deleted_items)[i]);
137         }
138     }
139
140   if(list_items != NULL)
141     {
142       num_items = list_items->length();
143       for(i = 0; i < num_items; i++)
144         {
145
146           mail_rc->setAlias(error,(*list_items)[i]->label,
147                             (*list_items)[i]->value);
148         }
149     }
150   
151   
152   if(deleted_items != NULL)
153     {
154       delete deleted_items;
155       deleted_items = NULL;
156     }
157 }
158
159 // AliasListUiItem::writeFromSourceToUi()
160 // Takes values in the UI and puts them into the source DB
161 ///////////////////////////////////////////////////////////////////
162 static int cmp_prop_string_pair(const void *v1, const void *v2)
163 {
164     PropStringPair      *p1 = *((PropStringPair **) v1);
165     PropStringPair      *p2 = *((PropStringPair **) v2);
166     int                 ret;
167
168     ret =  strcmp((const char *) p1->label, (const char *) p2->label);
169     return ret;
170
171     
172 void AliasListUiItem::writeFromSourceToUi()
173 {
174   DtMailEnv error;
175   DtMail::Session * d_session = theRoamApp.session()->session();
176   DtMail::MailRc * mail_rc = d_session->mailRc(error);
177   Widget w = this->getWidget();
178   const char *list_str = NULL;
179   DtVirtArray<char *> list_str_list(10);
180   char *buf = NULL;
181   int list_len, i;
182   const char *value = NULL;
183   PropStringPair **prop_pairs = NULL;
184
185   XmListDeleteAllItems(w);
186
187   if (deleted_items != NULL)
188     {
189       delete deleted_items;
190       deleted_items = NULL;
191     }
192
193   if (list_items != NULL)
194     delete list_items;
195
196   list_items = new DtVirtArray<PropStringPair *>(10);
197   mail_rc->getAliasList(alias_stuffing_func, list_items);
198
199   list_len = list_items->length();
200   if (list_len)
201   {
202       prop_pairs =
203         (PropStringPair**) XtMalloc(list_len * sizeof(PropStringPair*));
204
205       for (i = 0; i < list_len; i++)
206       {
207           prop_pairs[i] = (*list_items)[0];
208           list_items->remove(0);
209       }
210
211       qsort(
212         prop_pairs,
213         list_len,
214         sizeof(PropStringPair*),
215         cmp_prop_string_pair);
216
217       for (i = 0; i < list_len; i++)
218         list_items->append(prop_pairs[i]);
219
220       if (NULL != prop_pairs)
221         XtFree((char*) prop_pairs);
222   }
223
224   for (i = 0; i < list_len; i++)
225     list_str_list.append(
226         formatPropPair( (*list_items)[i]->label, (*list_items)[i]->value ));
227
228   options_list_init(w, &list_str_list);
229 }
230 //
231 // callback for creating alias list...
232 void alias_stuffing_func(char * key, void * data, void * client_data)
233 {
234   DtVirtArray<PropStringPair *> *alias_list = (DtVirtArray<PropStringPair *> *)client_data;
235   PropStringPair *new_pair;
236   
237   new_pair = new PropStringPair;
238
239   new_pair->label = strdup(key);
240   new_pair->value = strdup((char *)data);
241   
242   alias_list->append(new_pair);
243
244 }
245
246
247
248 ///////////////////////////////////////////////////////////
249 void AliasListUiItem::handleAddButtonPress()
250 {
251   char *key_str = NULL;
252   char *value_str = NULL;
253   PropStringPair *new_pair = NULL;
254
255   XtVaGetValues(key_widget,
256                 XmNvalue, &key_str,
257                 NULL);
258
259   XtVaGetValues(value_widget,
260                 XmNvalue, &value_str,
261                 NULL);
262
263   if(key_str != NULL)
264     if(strlen(key_str) > 0)
265       {
266         new_pair = new PropStringPair;
267         int *pos_list, num_pos;
268
269         new_pair->label = strdup(key_str);
270         
271         if(value_str != NULL)
272           new_pair->value = strdup(value_str);
273         else
274           new_pair->value = NULL;
275         
276         if(XmListGetSelectedPos(this->getWidget(),
277                                 &pos_list,
278                                 &num_pos))
279           {
280             if(list_items == NULL)
281               list_items = new DtVirtArray<PropStringPair *>(10);
282
283             list_items->insert(new_pair,pos_list[0] - 1); 
284         
285             XmListAddItem(this->getWidget(),
286                           XmStringCreateLocalized(
287                                        formatPropPair(
288                                                       new_pair->label,
289                                                       new_pair->value)),
290                           pos_list[0]);
291             
292             XmListSelectPos(this->getWidget(),
293                             pos_list[0],
294                             TRUE);
295
296             XmListSetPos(this->getWidget(),
297                          pos_list[0]); 
298           }     
299         else
300           {
301             if(list_items == NULL)
302               list_items = new DtVirtArray<PropStringPair *>(10);
303
304             list_items->insert(new_pair,0); 
305             XmListAddItem(this->getWidget(),
306                           XmStringCreateLocalized(
307                                        formatPropPair(
308                                                       new_pair->label,
309                                                       new_pair->value)),
310                           1);
311             XmListSelectPos(this->getWidget(),
312                             1,
313                             TRUE);
314
315             XmListSetPos(this->getWidget(),
316                          1);     
317           }
318           props_changed = TRUE;
319       }
320 }
321 ///////////////////////////////////////////////////////////
322 void AliasListUiItem::handleChangeButtonPress()
323 {
324   char *key_str = NULL;
325   char *value_str = NULL;  
326   PropStringPair *new_pair = NULL;
327   XmString replace_string;
328   int *pos_list, num_pos;
329
330   // if nothing selected nothing to change...
331   if(XmListGetSelectedPos(this->getWidget(),
332                           &pos_list,
333                           &num_pos))
334     {
335       XtVaGetValues(key_widget,
336                     XmNvalue, &key_str,
337                     NULL);
338
339       XtVaGetValues(value_widget,
340                     XmNvalue, &value_str,
341                     NULL);
342
343       if(key_str != NULL)
344         if(strlen(key_str) > 0)
345           {
346
347             new_pair = (*list_items)[pos_list[0] - 1];
348
349             if(deleted_items == NULL)
350               {
351                 deleted_items = new DtVirtArray<char *>(10);
352               } 
353
354             deleted_items->append(strdup((*list_items)[pos_list[0] -1]->label));
355
356             free(new_pair->label);
357             new_pair->label = strdup(key_str);
358
359             if(new_pair->value != NULL)
360               {
361                 free(new_pair->value);
362                 if(value_str != NULL)
363                   new_pair->value = strdup(value_str);
364                 else
365                   new_pair->value = NULL;
366               }
367
368             replace_string = XmStringCreateLocalized(
369                                      formatPropPair(new_pair->label,
370                                                     new_pair->value));
371
372             XmListReplaceItemsPos(this->getWidget(),
373                                   &replace_string,
374                                   1,
375                                   pos_list[0]);
376
377             XmListSelectPos(this->getWidget(),
378                             pos_list[0],
379                             TRUE);
380           }
381         props_changed = TRUE;
382     }
383 }
384 ///////////////////////////////////////////////////////////
385 void AliasListUiItem::handleDeleteButtonPress()
386 {
387   Widget list_widget = this->getWidget();
388   int *p_list, p_count;
389
390   // get the selected position
391   if(XmListGetSelectedPos(list_widget,
392                           &p_list,
393                           &p_count))
394     {
395       
396       if(deleted_items == NULL)
397         {
398           deleted_items = new DtVirtArray<char *>(10);
399         }       
400
401       deleted_items->append(strdup((*list_items)[p_list[0] -1]->label));
402
403       // delete the item from our list 
404       this->list_items->remove(p_list[0] - 1); // remove only first
405
406       // delete the item from the widget
407       XmListDeletePos(list_widget, p_list[0]);
408
409       XtVaSetValues(this->getKeyWidget(),
410                         XmNvalue,"",
411                         NULL);
412
413       XtVaSetValues(this->getValueWidget(),
414                         XmNvalue,"",
415                         NULL);
416
417       XmListSelectPos(list_widget,
418                       p_list[0],
419                       TRUE);
420
421       props_changed = TRUE;
422       
423     }
424
425 }
426
427