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