Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtmail / dtmail / CustomListUiItem.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: CustomListUiItem.C /main/6 1997/09/03 17:34:40 mgreess $ */
24 /*
25  *+SNOTICE
26  *
27  *      $TOG: CustomListUiItem.C /main/6 1997/09/03 17:34:40 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/CustomListUiItem.hh>
51
52 extern void handleCustSelection(Widget, XtPointer, XtPointer );
53 extern Boolean props_changed;
54
55 // CustomListUiItem::CustomListUiItem
56 // CustomListUiItem ctor
57 ////////////////////////////////////////////////////////////////////
58
59 CustomListUiItem::CustomListUiItem(Widget w, 
60                        int source, 
61                        char *search_key,
62                        Widget key_entry_widget,
63                        Widget value_entry_widget):ListUiItem(w, source, search_key, NULL)
64 {
65   source = source; search_key = search_key;
66
67   key_widget = key_entry_widget;
68   value_widget = value_entry_widget;
69
70   list_items = NULL;
71   deleted_items = NULL;
72
73   XtVaSetValues(w,
74         XmNuserData, this,
75         XmNautomaticSelection, True,
76         XmNselectionPolicy, XmBROWSE_SELECT,
77         NULL);
78
79     XtAddCallback(w,
80                   XmNbrowseSelectionCallback, 
81                   (XtCallbackProc)handleCustSelection,
82                   (XtPointer)this);
83
84
85
86 }
87
88 #ifdef DEAD_WOOD
89 //-----------------======================-----------------
90 void handleDoubleSelection(Widget w, XtPointer clientdata, XtPointer calldata)
91 {
92   CustomListUiItem *item;
93   XmListCallbackStruct *list_info = (XmListCallbackStruct *)calldata;
94   char *selection_string = NULL;
95   DtVirtArray<PropStringPair *> *list_items;
96
97
98   XtVaGetValues(w, 
99         XmNuserData, &item,
100         NULL);  
101
102   list_items = item->getItemList();
103
104   if(list_items != NULL)
105     {                                         // motif index is 1 based
106                                               //virtarry is 0 based
107       PropStringPair *pair = (*list_items)[list_info->item_position - 1];
108
109       if(pair != NULL)
110         {
111           XtVaSetValues(item->getKeyWidget(),
112                         XmNvalue,pair->label,
113                         NULL);
114           
115           XtVaSetValues(item->getValueWidget(),
116                         XmNvalue,pair->value,
117                         NULL);
118         }
119     }   
120 }
121 #endif /* DEAD_WOOD */
122
123 //-----------------======================-----------------
124 void handleCustSelection(Widget w, XtPointer, XtPointer calldata)
125 {
126   CustomListUiItem *item;
127   XmListCallbackStruct *list_info = (XmListCallbackStruct *)calldata;
128   char *selection_string = NULL;
129   DtVirtArray<PropStringPair *> *list_items;
130
131
132   XtVaGetValues(w, 
133         XmNuserData, &item,
134         NULL);  
135
136   list_items = item->getItemList();
137
138   if(list_items != NULL)
139     {                                         // motif index is 1 based
140                                               //virtarry is 0 based
141       PropStringPair *pair = (*list_items)[list_info->item_position - 1];
142
143       if(pair != NULL)
144         {
145           XtVaSetValues(item->getKeyWidget(),
146                         XmNvalue,pair->label,
147                         NULL);
148           
149           XtVaSetValues(item->getValueWidget(),
150                         XmNvalue,pair->value,
151                         NULL);
152         }
153     }   
154 }
155 // CustomListUiItem::writeFromUiToSource()
156 // Takes values in the UI and puts them into the source DB
157 ///////////////////////////////////////////////////////////////////
158 void CustomListUiItem::writeFromUiToSource()
159 {
160   Widget w = this->getWidget();
161   DtMailEnv error;
162   DtMail::Session * d_session = theRoamApp.session()->session();
163   DtMail::MailRc * mail_rc = d_session->mailRc(error);
164   int i, num_items;
165   char *cf_str = NULL;
166  
167   if(list_items != NULL) {
168       if(list_items->length() > 0)
169         {
170           num_items = list_items->length();
171           // calc mailrc strlen...
172           int total_len = 1; // add space for the terminator...
173           for(i = 0; i < num_items; i++) {
174               int num_blanks=0;
175               for (char *ptr = (*list_items)[i]->value; 
176                         ptr && *ptr; ptr++)
177                         if (*ptr == ' ') num_blanks++;
178               // strlen(label) strlen(value) + space + colon +
179               // potential \'s that precede the blank character
180               if((*list_items)[i]->label != NULL)
181                 total_len = total_len + strlen(":") + 
182                         (strlen("\\") * num_blanks) + 
183                         strlen((*list_items)[i]->label) + 2;
184  
185               if((*list_items)[i]->value != NULL)
186                 total_len = total_len + strlen((*list_items)[i]->value);
187           }
188           cf_str = (char *)malloc(total_len);
189           cf_str[0] = '\0';
190
191           for(i = 0; i < num_items; i++) {
192               if((*list_items)[i]->label != NULL) 
193                    strcat(cf_str, (*list_items)[i]->label);
194               strcat(cf_str, ":");
195               if((*list_items)[i]->value != NULL) {
196                 if (strchr((char*)(*list_items)[i]->value, ' ')) {
197                         char *token, *tmpbuf;
198                         tmpbuf = strdup((*list_items)[i]->value);
199                         if (token = (char *)strtok(tmpbuf, " ")) {
200                                 strcat(cf_str, token);
201                                 while(token = (char *)strtok(NULL, " ")) {
202                                         strcat(cf_str, "\\ ");
203                                         strcat(cf_str, token);
204                                 }
205                         }
206                         free(tmpbuf);
207                 }
208                 else
209                         strcat(cf_str, (*list_items)[i]->value);
210               }
211               strcat(cf_str, " ");
212           }
213           mail_rc->setValue(error, "additionalfields", cf_str);
214         }
215       else
216           mail_rc->removeValue(error, "additionalfields");
217     }
218   else
219        mail_rc->removeValue(error, "additionalfields");
220
221 }
222
223 // CustomListUiItem::writeFromSourceToUi()
224 // Takes values in the UI and puts them into the source DB
225 ///////////////////////////////////////////////////////////////////
226 void CustomListUiItem::writeFromSourceToUi()
227 {
228   DtMailEnv error;
229   DtMail::Session * d_session = theRoamApp.session()->session();
230   DtMail::MailRc * mail_rc = d_session->mailRc(error);
231   Widget w = this->getWidget();
232   const char *list_str = NULL;
233   DtVirtArray<char *> list_str_list(10);
234   char *buf = NULL;
235   int list_len, i;
236   const char *value = NULL;
237
238   XmListDeleteAllItems(w);
239
240   mail_rc->getValue(error, "additionalfields", &value);
241
242   if(list_items != NULL)
243     delete list_items;
244
245   list_items = new DtVirtArray<PropStringPair *>(10);
246
247   parsePropString(value, *list_items);
248
249   list_len = list_items->length();
250   for(i = 0; i < list_len; i++)
251     list_str_list.append(formatPropPair((*list_items)[i]->label, 
252                                         (*list_items)[i]->value));
253
254   options_list_init(w, &list_str_list);
255
256   if (value != NULL)
257     free((void *)value);
258 }
259
260 ///////////////////////////////////////////////////////////
261 void CustomListUiItem::handleAddButtonPress()
262 {
263   char *key_str = NULL;
264   char *value_str = NULL;
265   PropStringPair *new_pair = NULL;
266
267   XtVaGetValues(key_widget,
268                 XmNvalue, &key_str,
269                 NULL);
270
271   XtVaGetValues(value_widget,
272                 XmNvalue, &value_str,
273                 NULL);
274
275   if(key_str != NULL)
276     if(strlen(key_str) > 0)
277       {
278         new_pair = new PropStringPair;
279         int *pos_list, num_pos;
280
281         new_pair->label = strdup(key_str);
282         
283         if(value_str != NULL)
284           new_pair->value = strdup(value_str);
285         else
286           new_pair->value = NULL;
287         
288         if(XmListGetSelectedPos(this->getWidget(),
289                                 &pos_list,
290                                 &num_pos))
291           {
292             if(list_items == NULL)
293               list_items = new DtVirtArray<PropStringPair *>(10);
294
295             list_items->insert(new_pair,pos_list[0] - 1); 
296         
297             XmListAddItem(this->getWidget(),
298                           XmStringCreateLocalized(
299                                        formatPropPair(
300                                                       new_pair->label,
301                                                       new_pair->value)),
302                           pos_list[0]);
303             
304             XmListSelectPos(this->getWidget(),
305                             pos_list[0],
306                             TRUE);
307
308             XmListSetPos(this->getWidget(),
309                          pos_list[0]); 
310           }     
311         else
312           {
313             if(list_items == NULL)
314               list_items = new DtVirtArray<PropStringPair *>(10);
315
316             list_items->insert(new_pair,0); 
317             XmListAddItem(this->getWidget(),
318                           XmStringCreateLocalized(
319                                        formatPropPair(
320                                                       new_pair->label,
321                                                       new_pair->value)),
322                           1);
323             XmListSelectPos(this->getWidget(),
324                             1,
325                             TRUE);
326
327             XmListSetPos(this->getWidget(),
328                          1);     
329           }
330         props_changed = TRUE;
331       }
332 }
333 ///////////////////////////////////////////////////////////
334 void CustomListUiItem::handleChangeButtonPress()
335 {
336   char *key_str = NULL;
337   char *value_str = NULL;  
338   PropStringPair *new_pair = NULL;
339   XmString replace_string;
340   int *pos_list, num_pos;
341
342   // if nothing selected nothing to change...
343   if(XmListGetSelectedPos(this->getWidget(),
344                           &pos_list,
345                           &num_pos))
346     {
347       XtVaGetValues(key_widget,
348                     XmNvalue, &key_str,
349                     NULL);
350
351       XtVaGetValues(value_widget,
352                     XmNvalue, &value_str,
353                     NULL);
354
355       if(key_str != NULL)
356         if(strlen(key_str) > 0)
357           {
358
359             new_pair = (*list_items)[pos_list[0] - 1];
360
361             free(new_pair->label);
362             new_pair->label = strdup(key_str);
363   
364             if(new_pair->value != NULL)
365               {
366                 free(new_pair->value);
367                 if(value_str != NULL)
368                   new_pair->value = strdup(value_str);
369                 else
370                   new_pair->value = NULL;
371               }
372
373             replace_string = XmStringCreateLocalized(
374                                      formatPropPair(new_pair->label,
375                                                     new_pair->value));
376
377             XmListReplaceItemsPos(this->getWidget(),
378                                   &replace_string,
379                                   1,
380                                   pos_list[0]);
381
382             XmListSelectPos(this->getWidget(),
383                             pos_list[0],
384                             TRUE);
385           }
386       props_changed = TRUE;
387     }
388 }
389 ///////////////////////////////////////////////////////////
390 void CustomListUiItem::handleDeleteButtonPress()
391 {
392   Widget list_widget = this->getWidget();
393   int *p_list, p_count;
394
395   // get the selected position
396   if(XmListGetSelectedPos(list_widget,
397                           &p_list,
398                           &p_count))
399     {
400       
401       // delete the item from our list 
402       this->list_items->remove(p_list[0] - 1); // remove only first
403
404       // delete the item from the widget
405       XmListDeletePos(list_widget, p_list[0]);
406
407       XtVaSetValues(this->getKeyWidget(),
408                         XmNvalue,"",
409                         NULL);
410
411       XtVaSetValues(this->getValueWidget(),
412                         XmNvalue,"",
413                         NULL);
414
415       XmListSelectPos(list_widget,
416                       p_list[0],
417                       TRUE);
418       
419       props_changed = TRUE;
420     }
421
422 }
423
424
425
426