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