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