2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
23 /* $TOG: AliasListUiItem.C /main/6 1997/09/03 17:33:31 mgreess $ */
27 * $TOG: AliasListUiItem.C /main/6 1997/09/03 17:33:31 mgreess $
29 * RESTRICTED CONFIDENTIAL INFORMATION:
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
39 * Copyright 1993 Sun Microsystems, Inc. All rights reserved.
46 #include <DtMail/options_util.h>
47 #include "options_ui.h"
48 #include <DtMail/PropUi.hh>
49 #include <DtMail/ListUiItem.hh>
50 #include <DtMail/AliasListUiItem.hh>
52 extern Boolean props_changed;
53 void alias_stuffing_func(char * key, void * data, void * client_data);
54 void handleAliasSelection(Widget w, XtPointer clientdata, XtPointer calldata);
56 // AliasListUiItem::AliasListUiItem
57 // AliasListUiItem ctor
58 ////////////////////////////////////////////////////////////////////
60 AliasListUiItem::AliasListUiItem(Widget w,
63 Widget key_entry_widget,
64 Widget value_entry_widget):ListUiItem(w, source, search_key, NULL)
66 source = source; search_key = search_key;
68 key_widget = key_entry_widget;
69 value_widget = value_entry_widget;
76 XmNautomaticSelection, True,
77 XmNselectionPolicy, XmBROWSE_SELECT,
81 XmNbrowseSelectionCallback,
82 (XtCallbackProc)handleAliasSelection,
87 //-----------------======================-----------------
88 void handleAliasSelection(Widget w, XtPointer, XtPointer calldata)
90 AliasListUiItem *item;
91 XmListCallbackStruct *list_info = (XmListCallbackStruct *)calldata;
92 char *selection_string = NULL;
93 DtVirtArray<PropStringPair *> *list_items;
99 list_items = item->getItemList();
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];
108 XtVaSetValues(item->getKeyWidget(),
109 XmNvalue,pair->label,
112 XtVaSetValues(item->getValueWidget(),
113 XmNvalue,pair->value,
119 // AliasListUiItem::writeFromUiToSource()
120 // Takes values in the UI and puts them into the source DB
121 ///////////////////////////////////////////////////////////////////
122 void AliasListUiItem::writeFromUiToSource()
124 Widget w = this->getWidget();
126 DtMail::Session * d_session = theRoamApp.session()->session();
127 DtMail::MailRc * mail_rc = d_session->mailRc(error);
130 if(deleted_items != NULL)
132 num_items = deleted_items->length();
134 for(i = 0; i < num_items; i++)
136 mail_rc->removeAlias(error,(*deleted_items)[i]);
140 if(list_items != NULL)
142 num_items = list_items->length();
143 for(i = 0; i < num_items; i++)
146 mail_rc->setAlias(error,(*list_items)[i]->label,
147 (*list_items)[i]->value);
152 if(deleted_items != NULL)
154 delete deleted_items;
155 deleted_items = NULL;
159 // AliasListUiItem::writeFromSourceToUi()
160 // Takes values in the UI and puts them into the source DB
161 ///////////////////////////////////////////////////////////////////
162 static int cmp_prop_string_pair(const void *v1, const void *v2)
164 PropStringPair *p1 = *((PropStringPair **) v1);
165 PropStringPair *p2 = *((PropStringPair **) v2);
168 ret = strcmp((const char *) p1->label, (const char *) p2->label);
172 void AliasListUiItem::writeFromSourceToUi()
175 DtMail::Session * d_session = theRoamApp.session()->session();
176 DtMail::MailRc * mail_rc = d_session->mailRc(error);
177 Widget w = this->getWidget();
178 const char *list_str = NULL;
179 DtVirtArray<char *> list_str_list(10);
182 const char *value = NULL;
183 PropStringPair **prop_pairs = NULL;
185 XmListDeleteAllItems(w);
187 if (deleted_items != NULL)
189 delete deleted_items;
190 deleted_items = NULL;
193 if (list_items != NULL)
196 list_items = new DtVirtArray<PropStringPair *>(10);
197 mail_rc->getAliasList(alias_stuffing_func, list_items);
199 list_len = list_items->length();
203 (PropStringPair**) XtMalloc(list_len * sizeof(PropStringPair*));
205 for (i = 0; i < list_len; i++)
207 prop_pairs[i] = (*list_items)[0];
208 list_items->remove(0);
214 sizeof(PropStringPair*),
215 cmp_prop_string_pair);
217 for (i = 0; i < list_len; i++)
218 list_items->append(prop_pairs[i]);
220 if (NULL != prop_pairs)
221 XtFree((char*) prop_pairs);
224 for (i = 0; i < list_len; i++)
225 list_str_list.append(
226 formatPropPair( (*list_items)[i]->label, (*list_items)[i]->value ));
228 options_list_init(w, &list_str_list);
231 // callback for creating alias list...
232 void alias_stuffing_func(char * key, void * data, void * client_data)
234 DtVirtArray<PropStringPair *> *alias_list = (DtVirtArray<PropStringPair *> *)client_data;
235 PropStringPair *new_pair;
237 new_pair = new PropStringPair;
239 new_pair->label = strdup(key);
240 new_pair->value = strdup((char *)data);
242 alias_list->append(new_pair);
248 ///////////////////////////////////////////////////////////
249 void AliasListUiItem::handleAddButtonPress()
251 char *key_str = NULL;
252 char *value_str = NULL;
253 PropStringPair *new_pair = NULL;
255 XtVaGetValues(key_widget,
259 XtVaGetValues(value_widget,
260 XmNvalue, &value_str,
264 if(strlen(key_str) > 0)
266 new_pair = new PropStringPair;
267 int *pos_list, num_pos;
269 new_pair->label = strdup(key_str);
271 if(value_str != NULL)
272 new_pair->value = strdup(value_str);
274 new_pair->value = NULL;
276 if(XmListGetSelectedPos(this->getWidget(),
280 if(list_items == NULL)
281 list_items = new DtVirtArray<PropStringPair *>(10);
283 list_items->insert(new_pair,pos_list[0] - 1);
285 XmListAddItem(this->getWidget(),
286 XmStringCreateLocalized(
292 XmListSelectPos(this->getWidget(),
296 XmListSetPos(this->getWidget(),
301 if(list_items == NULL)
302 list_items = new DtVirtArray<PropStringPair *>(10);
304 list_items->insert(new_pair,0);
305 XmListAddItem(this->getWidget(),
306 XmStringCreateLocalized(
311 XmListSelectPos(this->getWidget(),
315 XmListSetPos(this->getWidget(),
318 props_changed = TRUE;
321 ///////////////////////////////////////////////////////////
322 void AliasListUiItem::handleChangeButtonPress()
324 char *key_str = NULL;
325 char *value_str = NULL;
326 PropStringPair *new_pair = NULL;
327 XmString replace_string;
328 int *pos_list, num_pos;
330 // if nothing selected nothing to change...
331 if(XmListGetSelectedPos(this->getWidget(),
335 XtVaGetValues(key_widget,
339 XtVaGetValues(value_widget,
340 XmNvalue, &value_str,
344 if(strlen(key_str) > 0)
347 new_pair = (*list_items)[pos_list[0] - 1];
349 if(deleted_items == NULL)
351 deleted_items = new DtVirtArray<char *>(10);
354 deleted_items->append(strdup((*list_items)[pos_list[0] -1]->label));
356 free(new_pair->label);
357 new_pair->label = strdup(key_str);
359 if(new_pair->value != NULL)
361 free(new_pair->value);
362 if(value_str != NULL)
363 new_pair->value = strdup(value_str);
365 new_pair->value = NULL;
368 replace_string = XmStringCreateLocalized(
369 formatPropPair(new_pair->label,
372 XmListReplaceItemsPos(this->getWidget(),
377 XmListSelectPos(this->getWidget(),
381 props_changed = TRUE;
384 ///////////////////////////////////////////////////////////
385 void AliasListUiItem::handleDeleteButtonPress()
387 Widget list_widget = this->getWidget();
388 int *p_list, p_count;
390 // get the selected position
391 if(XmListGetSelectedPos(list_widget,
396 if(deleted_items == NULL)
398 deleted_items = new DtVirtArray<char *>(10);
401 deleted_items->append(strdup((*list_items)[p_list[0] -1]->label));
403 // delete the item from our list
404 this->list_items->remove(p_list[0] - 1); // remove only first
406 // delete the item from the widget
407 XmListDeletePos(list_widget, p_list[0]);
409 XtVaSetValues(this->getKeyWidget(),
413 XtVaSetValues(this->getValueWidget(),
417 XmListSelectPos(list_widget,
421 props_changed = TRUE;