Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / programs / dtmail / dtmail / AlternatesListUiItem.C
1 /* $XConsortium: AlternatesListUiItem.C /main/3 1995/11/06 16:03:45 rswiston $ */
2 /*
3  *+SNOTICE
4  *
5  *      RESTRICTED CONFIDENTIAL INFORMATION:
6  *      
7  *      The information in this document is subject to special
8  *      restrictions in a confidential disclosure agreement bertween
9  *      HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
10  *      document outside HP, IBM, Sun, USL, SCO, or Univel wihtout
11  *      Sun's specific written approval.  This documment and all copies
12  *      and derivative works thereof must be returned or destroyed at
13  *      Sun's request.
14  *
15  *      Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
16  *
17  *+ENOTICE
18  */
19 #include "RoamApp.h"
20 #include <Xm/List.h>
21 #include <DtMail/options_util.h>
22 #include "options_ui.h"
23 #include <DtMail/PropUi.hh>
24 #include <DtMail/ListUiItem.hh>
25 #include <DtMail/AlternatesListUiItem.hh>
26
27 extern Boolean props_changed;
28 void handleAlternateSelection(Widget w, XtPointer clientdata, XtPointer calldata);
29
30 // AlternatesListUiItem::AlternatesListUiItem
31 // AlternatesListUiItem ctor
32 ////////////////////////////////////////////////////////////////////
33
34 AlternatesListUiItem::AlternatesListUiItem(Widget w, 
35                        int source, 
36                        char *search_key,
37                        Widget text_entry_widget):ListUiItem(w, source, search_key, NULL)
38 {
39   source = source; search_key = search_key;
40
41   entry_field_widget = text_entry_widget;
42
43   list_items = NULL;
44   deleted_items = NULL;
45
46   XtVaSetValues(w,
47         XmNuserData, this,
48         XmNautomaticSelection, True,
49         XmNselectionPolicy, XmBROWSE_SELECT,
50         NULL);
51
52     XtAddCallback(w,
53                 XmNbrowseSelectionCallback, 
54                 (XtCallbackProc)handleAlternateSelection,
55                   (XtPointer)this);
56 }
57
58 //---------------------------------------------------------------
59 void handleAlternateSelection(Widget w, XtPointer clientdata, XtPointer calldata)
60 {
61   AlternatesListUiItem *item;
62   XmListCallbackStruct *list_info = (XmListCallbackStruct *)calldata;
63   char *selection_string = NULL;
64   DtVirtArray<PropStringPair *> *list_items;
65   clientdata = clientdata;
66
67   XtVaGetValues(w, 
68         XmNuserData, &item,
69         NULL);  
70
71   list_items = item->getItemList();
72
73   if(list_items != NULL)
74     {
75       // motif index is 1 based
76       //virtarry is 0 based
77       PropStringPair *pair = (*list_items)[list_info->item_position - 1];
78
79       if(pair != NULL)
80         {
81           XtVaSetValues(item->getEntryFieldWidget(),
82                         XmNvalue,pair->label,
83                         NULL);
84
85         }       
86
87     }
88 }
89
90
91 // AlternatesListUiItem::writeFromUiToSource()
92 // Takes values in the UI and puts them into the source DB
93 ///////////////////////////////////////////////////////////////////
94 void AlternatesListUiItem::writeFromUiToSource()
95 {
96   Widget w = this->getWidget();
97   DtMailEnv error;
98   DtMail::Session * d_session = theRoamApp.session()->session();
99   DtMail::MailRc * mail_rc = d_session->mailRc(error);
100   int i, num_items;
101
102   if(deleted_items != NULL)
103     {
104       num_items = deleted_items->length();
105
106       for(i = 0; i < num_items; i++)
107         {
108           mail_rc->removeAlternate(error,(*deleted_items)[i]);
109         }
110     }
111
112   if(list_items != NULL)
113     {
114       num_items = list_items->length();
115       for(i = 0; i < num_items; i++)
116         {
117           mail_rc->setAlternate(error,(*list_items)[i]->label);
118         }
119     }
120   
121   if(deleted_items != NULL)
122     {
123       delete deleted_items;
124       deleted_items = NULL;
125     }
126 }
127
128 // AlternatesListUiItem::writeFromSourceToUi()
129 // Takes values in the UI and puts them into the source DB
130 ///////////////////////////////////////////////////////////////////
131 void AlternatesListUiItem::writeFromSourceToUi()
132 {
133   DtMailEnv error;
134   DtMail::Session * d_session = theRoamApp.session()->session();
135   DtMail::MailRc * mail_rc = d_session->mailRc(error);
136   Widget w = this->getWidget();
137   const char *list_str = NULL;
138   DtVirtArray<char *> *char_list = NULL;
139   PropStringPair *new_pair;
140   char *token, *buf = NULL;
141
142   XmListDeleteAllItems(w);
143
144   if(deleted_items != NULL)
145     {
146       delete deleted_items;
147       deleted_items = NULL;
148     }
149
150   if(list_items != NULL)
151   {
152     delete list_items;
153     list_items = NULL;
154   }
155
156   if ((list_str = mail_rc->getAlternates(error)) == NULL) {
157         list_str = strdup("");
158   }
159   
160   if ((buf = (char *) malloc(strlen(list_str) + 1)) == NULL)
161     return;
162   strcpy(buf, (char *)list_str);
163   
164   if((token = (char *) strtok(buf, " "))) 
165     {
166       list_items = new DtVirtArray<PropStringPair *>(10);
167       char_list = new DtVirtArray<char *>(10);
168
169       new_pair = new PropStringPair;
170       new_pair->label = strdup(token);
171       new_pair->value = NULL;
172       list_items->append(new_pair);
173
174       char_list->append(strdup(token));
175  
176       while(token = (char *)strtok(NULL, " "))
177         {
178           new_pair = new PropStringPair;
179           new_pair->label = strdup(token);
180           new_pair->value = NULL;
181           list_items->append(new_pair);
182
183           char_list->append(strdup(token)); 
184         }
185     }
186
187   options_list_init(w, char_list);
188
189   delete char_list;
190
191   if(list_str != NULL)
192     free((void *)list_str);
193
194   if(buf != NULL)
195     free((void *)buf);
196
197 }
198
199 ///////////////////////////////////////////////////////////
200 void AlternatesListUiItem::handleAddButtonPress()
201 {
202   Widget entry_field = this->getEntryFieldWidget();
203   char *test_str = NULL;
204   PropStringPair *new_pair = NULL;
205
206   XtVaGetValues(entry_field,
207                 XmNvalue, &test_str,
208                 NULL);
209
210   if(test_str != NULL)
211     if(strlen(test_str) > 1)
212       {
213         new_pair = new PropStringPair;
214         int *pos_list, num_pos;
215
216         new_pair->label = strdup(test_str);
217         new_pair->value = NULL;
218       
219         if(XmListGetSelectedPos(this->getWidget(),
220                                 &pos_list,
221                                 &num_pos))
222           {
223             if(list_items == NULL)
224               list_items = new DtVirtArray<PropStringPair *>(10);
225
226             list_items->insert(new_pair,pos_list[0] - 1); 
227             XmListAddItem(this->getWidget(),
228                           XmStringCreateLocalized(new_pair->label),
229                           pos_list[0]);
230             
231             XmListSelectPos(this->getWidget(),
232                             pos_list[0],
233                             TRUE);
234
235             XmListSetPos(this->getWidget(),
236                          pos_list[0]);
237           }     
238         else
239           {
240             if(list_items == NULL)
241               list_items = new DtVirtArray<PropStringPair *>(10);
242
243             list_items->insert(new_pair,0); 
244
245             XmListAddItem(this->getWidget(),
246                           XmStringCreateLocalized(new_pair->label),
247                           1);
248             XmListSelectPos(this->getWidget(),
249                             1,
250                             TRUE);
251             XmListSetPos(this->getWidget(),
252                          1);
253           }
254         props_changed = TRUE;
255       }
256 }
257 ///////////////////////////////////////////////////////////
258 void AlternatesListUiItem::handleChangeButtonPress()
259 {
260   Widget entry_field = this->getEntryFieldWidget();
261   char *test_str = NULL;
262   PropStringPair *new_pair = NULL;
263   XmString replace_string;
264   int *pos_list, num_pos;
265
266   // if nothing selected nothing to change...
267   if(XmListGetSelectedPos(this->getWidget(),
268                           &pos_list,
269                           &num_pos))
270     {
271       XtVaGetValues(entry_field,
272                     XmNvalue, &test_str,
273                     NULL);
274
275       if(test_str != NULL)
276         if(strlen(test_str) > 1)
277           {
278
279             new_pair = (*list_items)[pos_list[0] - 1];
280
281             if(deleted_items == NULL)
282               {
283                 deleted_items = new DtVirtArray<char *>(10);
284               } 
285
286             deleted_items->append(strdup((*list_items)[pos_list[0] -1]->label));
287
288             free(new_pair->label);
289             new_pair->label = strdup(test_str);
290
291             replace_string = XmStringCreateLocalized(new_pair->label);
292
293             XmListReplaceItemsPos(this->getWidget(),
294                                   &replace_string,
295                                   1,
296                                   pos_list[0]);
297
298             XmListSelectPos(this->getWidget(),
299                             pos_list[0],
300                             TRUE);
301           }
302         props_changed = TRUE;
303     }
304 }
305 ///////////////////////////////////////////////////////////
306 void AlternatesListUiItem::handleDeleteButtonPress()
307 {
308   Widget list_widget = this->getWidget();
309   int *p_list, p_count;
310
311   // get the selected position
312   if(XmListGetSelectedPos(list_widget,
313                           &p_list,
314                           &p_count))
315     {
316       
317       if(deleted_items == NULL)
318         {
319           deleted_items = new DtVirtArray<char *>(10);
320         }       
321
322       deleted_items->append(strdup((*list_items)[p_list[0] -1]->label));
323
324       // delete the item from our list 
325       this->list_items->remove(p_list[0] - 1); // remove only first
326
327       // delete the item from the widget
328       XmListDeletePos(list_widget, p_list[0]);
329
330       XtVaSetValues(this->getEntryFieldWidget(),
331                         XmNvalue,"",
332                         NULL);
333       XmListSelectPos(list_widget,
334                       p_list[0],
335                       TRUE);
336       
337       props_changed = TRUE;
338     }
339
340
341
342 }
343
344
345
346