Even more spelling fixed
[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 libraries 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       XtFree((char*) prop_pairs);
221   }
222
223   for (i = 0; i < list_len; i++)
224     list_str_list.append(
225         formatPropPair( (*list_items)[i]->label, (*list_items)[i]->value ));
226
227   options_list_init(w, &list_str_list);
228 }
229 //
230 // callback for creating alias list...
231 void alias_stuffing_func(char * key, void * data, void * client_data)
232 {
233   DtVirtArray<PropStringPair *> *alias_list = (DtVirtArray<PropStringPair *> *)client_data;
234   PropStringPair *new_pair;
235   
236   new_pair = new PropStringPair;
237
238   new_pair->label = strdup(key);
239   new_pair->value = strdup((char *)data);
240   
241   alias_list->append(new_pair);
242
243 }
244
245
246
247 ///////////////////////////////////////////////////////////
248 void AliasListUiItem::handleAddButtonPress()
249 {
250   char *key_str = NULL;
251   char *value_str = NULL;
252   PropStringPair *new_pair = NULL;
253
254   XtVaGetValues(key_widget,
255                 XmNvalue, &key_str,
256                 NULL);
257
258   XtVaGetValues(value_widget,
259                 XmNvalue, &value_str,
260                 NULL);
261
262   if(key_str != NULL)
263     if(strlen(key_str) > 0)
264       {
265         new_pair = new PropStringPair;
266         int *pos_list, num_pos;
267
268         new_pair->label = strdup(key_str);
269         
270         if(value_str != NULL)
271           new_pair->value = strdup(value_str);
272         else
273           new_pair->value = NULL;
274         
275         if(XmListGetSelectedPos(this->getWidget(),
276                                 &pos_list,
277                                 &num_pos))
278           {
279             if(list_items == NULL)
280               list_items = new DtVirtArray<PropStringPair *>(10);
281
282             list_items->insert(new_pair,pos_list[0] - 1); 
283         
284             XmListAddItem(this->getWidget(),
285                           XmStringCreateLocalized(
286                                        formatPropPair(
287                                                       new_pair->label,
288                                                       new_pair->value)),
289                           pos_list[0]);
290             
291             XmListSelectPos(this->getWidget(),
292                             pos_list[0],
293                             TRUE);
294
295             XmListSetPos(this->getWidget(),
296                          pos_list[0]); 
297           }     
298         else
299           {
300             if(list_items == NULL)
301               list_items = new DtVirtArray<PropStringPair *>(10);
302
303             list_items->insert(new_pair,0); 
304             XmListAddItem(this->getWidget(),
305                           XmStringCreateLocalized(
306                                        formatPropPair(
307                                                       new_pair->label,
308                                                       new_pair->value)),
309                           1);
310             XmListSelectPos(this->getWidget(),
311                             1,
312                             TRUE);
313
314             XmListSetPos(this->getWidget(),
315                          1);     
316           }
317           props_changed = TRUE;
318       }
319 }
320 ///////////////////////////////////////////////////////////
321 void AliasListUiItem::handleChangeButtonPress()
322 {
323   char *key_str = NULL;
324   char *value_str = NULL;  
325   PropStringPair *new_pair = NULL;
326   XmString replace_string;
327   int *pos_list, num_pos;
328
329   // if nothing selected nothing to change...
330   if(XmListGetSelectedPos(this->getWidget(),
331                           &pos_list,
332                           &num_pos))
333     {
334       XtVaGetValues(key_widget,
335                     XmNvalue, &key_str,
336                     NULL);
337
338       XtVaGetValues(value_widget,
339                     XmNvalue, &value_str,
340                     NULL);
341
342       if(key_str != NULL)
343         if(strlen(key_str) > 0)
344           {
345
346             new_pair = (*list_items)[pos_list[0] - 1];
347
348             if(deleted_items == NULL)
349               {
350                 deleted_items = new DtVirtArray<char *>(10);
351               } 
352
353             deleted_items->append(strdup((*list_items)[pos_list[0] -1]->label));
354
355             free(new_pair->label);
356             new_pair->label = strdup(key_str);
357
358             if(new_pair->value != NULL)
359               {
360                 free(new_pair->value);
361                 if(value_str != NULL)
362                   new_pair->value = strdup(value_str);
363                 else
364                   new_pair->value = NULL;
365               }
366
367             replace_string = XmStringCreateLocalized(
368                                      formatPropPair(new_pair->label,
369                                                     new_pair->value));
370
371             XmListReplaceItemsPos(this->getWidget(),
372                                   &replace_string,
373                                   1,
374                                   pos_list[0]);
375
376             XmListSelectPos(this->getWidget(),
377                             pos_list[0],
378                             TRUE);
379           }
380         props_changed = TRUE;
381     }
382 }
383 ///////////////////////////////////////////////////////////
384 void AliasListUiItem::handleDeleteButtonPress()
385 {
386   Widget list_widget = this->getWidget();
387   int *p_list, p_count;
388
389   // get the selected position
390   if(XmListGetSelectedPos(list_widget,
391                           &p_list,
392                           &p_count))
393     {
394       
395       if(deleted_items == NULL)
396         {
397           deleted_items = new DtVirtArray<char *>(10);
398         }       
399
400       deleted_items->append(strdup((*list_items)[p_list[0] -1]->label));
401
402       // delete the item from our list 
403       this->list_items->remove(p_list[0] - 1); // remove only first
404
405       // delete the item from the widget
406       XmListDeletePos(list_widget, p_list[0]);
407
408       XtVaSetValues(this->getKeyWidget(),
409                         XmNvalue,"",
410                         NULL);
411
412       XtVaSetValues(this->getValueWidget(),
413                         XmNvalue,"",
414                         NULL);
415
416       XmListSelectPos(list_widget,
417                       p_list[0],
418                       TRUE);
419
420       props_changed = TRUE;
421       
422     }
423
424 }
425
426