1 /* $TOG: TemplateListUiItem.C /main/6 1997/09/03 17:40:26 mgreess $ */
5 * $TOG: TemplateListUiItem.C /main/6 1997/09/03 17:40:26 mgreess $
7 * RESTRICTED CONFIDENTIAL INFORMATION:
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
17 * Copyright 1993 Sun Microsystems, Inc. All rights reserved.
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>
30 extern Boolean props_changed;
31 void handleTmplSelection(Widget w, XtPointer clientdata, XtPointer calldata);
32 // TemplateListUiItem::TemplateListUiItem
33 // TemplateListUiItem ctor
34 ////////////////////////////////////////////////////////////////////
36 TemplateListUiItem::TemplateListUiItem(Widget w,
39 Widget key_entry_widget,
40 Widget value_entry_widget):ListUiItem(w, source, search_key, NULL)
42 source = source; search_key = search_key;
44 key_widget = key_entry_widget;
45 value_widget = value_entry_widget;
52 XmNautomaticSelection, True,
53 XmNselectionPolicy, XmBROWSE_SELECT,
57 XmNbrowseSelectionCallback,
58 (XtCallbackProc)handleTmplSelection,
64 //-----------------======================-----------------
65 void handleTmplSelection(Widget w, XtPointer, XtPointer calldata)
67 TemplateListUiItem *item;
68 XmListCallbackStruct *list_info = (XmListCallbackStruct *)calldata;
69 char *selection_string = NULL;
70 DtVirtArray<PropStringPair *> *list_items;
77 list_items = item->getItemList();
79 if(list_items != NULL)
80 { // motif index is 1 based
82 PropStringPair *pair = (*list_items)[list_info->item_position - 1];
86 XtVaSetValues(item->getKeyWidget(),
90 XtVaSetValues(item->getValueWidget(),
97 // TemplateListUiItem::writeFromUiToSource()
98 // Takes values in the UI and puts them into the source DB
99 ///////////////////////////////////////////////////////////////////
100 void TemplateListUiItem::writeFromUiToSource()
102 Widget w = this->getWidget();
104 DtMail::Session * d_session = theRoamApp.session()->session();
105 DtMail::MailRc * mail_rc = d_session->mailRc(error);
110 if(list_items != NULL) {
111 if(list_items->length() > 0)
113 num_items = list_items->length();
115 // calc mailrc strlen...
116 total_len = 1; // add space for the terminator...
117 for(i = 0; i < num_items; i++) {
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;
128 if((*list_items)[i]->value != NULL)
129 total_len = total_len + strlen((*list_items)[i]->value);
132 cf_str = (char *)malloc(total_len);
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);
150 strcat(cf_str, (*list_items)[i]->label);
153 if((*list_items)[i]->value != NULL)
154 strcat(cf_str, (*list_items)[i]->value);
158 mail_rc->setValue(error, "templates", cf_str);
161 mail_rc->removeValue(error, "templates");
164 mail_rc->removeValue(error, "templates");
167 // TemplateListUiItem::writeFromSourceToUi()
168 // Takes values in the UI and puts them into the source DB
169 ///////////////////////////////////////////////////////////////////
170 void TemplateListUiItem::writeFromSourceToUi()
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);
180 const char *value = NULL;
182 XmListDeleteAllItems(w);
184 mail_rc->getValue(error, "templates", &value);
186 if(list_items != NULL)
189 char * expanded_list = d_session->expandPath(error, value);
191 list_items = new DtVirtArray<PropStringPair *>(10);
193 parsePropString(expanded_list, *list_items);
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));
201 options_list_init(w, &list_str_list);
207 ///////////////////////////////////////////////////////////
208 void TemplateListUiItem::handleAddButtonPress()
210 char *key_str = NULL;
211 char *value_str = NULL;
212 PropStringPair *new_pair = NULL;
214 XtVaGetValues(key_widget,
218 XtVaGetValues(value_widget,
219 XmNvalue, &value_str,
223 if(strlen(key_str) > 0)
225 new_pair = new PropStringPair;
226 int *pos_list, num_pos;
228 new_pair->label = strdup(key_str);
230 if(value_str != NULL)
231 new_pair->value = strdup(value_str);
233 new_pair->value = NULL;
235 if(XmListGetSelectedPos(this->getWidget(),
239 if(list_items == NULL)
240 list_items = new DtVirtArray<PropStringPair *>(10);
242 list_items->insert(new_pair,pos_list[0] - 1);
244 XmListAddItem(this->getWidget(),
245 XmStringCreateLocalized(
251 XmListSelectPos(this->getWidget(),
255 XmListSetPos(this->getWidget(),
260 if(list_items == NULL)
261 list_items = new DtVirtArray<PropStringPair *>(10);
263 list_items->insert(new_pair,0);
264 XmListAddItem(this->getWidget(),
265 XmStringCreateLocalized(
270 XmListSelectPos(this->getWidget(),
274 XmListSetPos(this->getWidget(),
277 props_changed = TRUE;
280 ///////////////////////////////////////////////////////////
281 void TemplateListUiItem::handleChangeButtonPress()
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;
289 // if nothing selected nothing to change...
290 if(XmListGetSelectedPos(this->getWidget(),
294 XtVaGetValues(key_widget,
298 XtVaGetValues(value_widget,
299 XmNvalue, &value_str,
303 if(strlen(key_str) > 0)
306 new_pair = (*list_items)[pos_list[0] - 1];
308 free(new_pair->label);
309 new_pair->label = strdup(key_str);
311 if(new_pair->value != NULL)
313 free(new_pair->value);
314 if(value_str != NULL)
315 new_pair->value = strdup(value_str);
317 new_pair->value = NULL;
320 replace_string = XmStringCreateLocalized(
321 formatPropPair(new_pair->label,
324 XmListReplaceItemsPos(this->getWidget(),
329 XmListSelectPos(this->getWidget(),
333 props_changed = TRUE;
336 ///////////////////////////////////////////////////////////
337 void TemplateListUiItem::handleDeleteButtonPress()
339 Widget list_widget = this->getWidget();
340 int *p_list, p_count;
342 // get the selected position
343 if(XmListGetSelectedPos(list_widget,
348 // delete the item from our list
349 this->list_items->remove(p_list[0] - 1); // remove only first
351 // delete the item from the widget
352 XmListDeletePos(list_widget, p_list[0]);
354 XtVaSetValues(this->getKeyWidget(),
358 XtVaSetValues(this->getValueWidget(),
362 XmListSelectPos(list_widget,
366 props_changed = TRUE;