dthelp: Change to ANSI function definitions
[oweals/cde.git] / cde / programs / dtmail / dtmail / IgnoreListUiItem.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: IgnoreListUiItem.C /main/4 1997/09/03 17:38:42 mgreess $ */
24 /*
25  *+SNOTICE
26  *
27  *      $TOG: IgnoreListUiItem.C /main/4 1997/09/03 17:38:42 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 #include "RoamApp.h"
44 #include <Xm/List.h>
45 #include <Xm/TextF.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/IgnoreListUiItem.hh>
51
52 void handleIgnoreSelection(Widget w, XtPointer clientdata, XtPointer calldata);
53 extern Boolean props_changed;
54
55 // IgnoreListUiItem::IgnoreListUiItem
56 // IgnoreListUiItem ctor
57 ////////////////////////////////////////////////////////////////////
58
59 IgnoreListUiItem::IgnoreListUiItem(Widget w, 
60                        int source, 
61                        char *search_key,
62                        Widget text_entry_widget):ListUiItem(w, source, search_key, NULL)
63 {
64   source = source; search_key = search_key;
65
66   entry_field_widget = text_entry_widget;
67
68   list_items = NULL;
69   deleted_items = NULL;
70
71   XtVaSetValues(w,
72         XmNuserData, this,
73         XmNautomaticSelection, True,
74         XmNselectionPolicy, XmBROWSE_SELECT,
75         NULL);
76
77     XtAddCallback(w,
78                 XmNbrowseSelectionCallback, 
79                 (XtCallbackProc)handleIgnoreSelection,
80                   (XtPointer)this);
81
82
83 }
84 //---------------------------------------------------------------
85 void handleIgnoreSelection(Widget w, XtPointer, XtPointer calldata)
86 {
87   IgnoreListUiItem *item;
88   XmListCallbackStruct *list_info = (XmListCallbackStruct *)calldata;
89   char *selection_string = NULL;
90   DtVirtArray<PropStringPair *> *list_items;
91
92   XtVaGetValues(w, 
93         XmNuserData, &item,
94         NULL);  
95
96   list_items = item->getItemList();
97
98   if(list_items != NULL)
99     {
100       // motif index is 1 based
101       //virtarry is 0 based
102       PropStringPair *pair = (*list_items)[list_info->item_position - 1];
103
104       if(pair != NULL)
105         {
106           XtVaSetValues(item->getEntryFieldWidget(),
107                         XmNvalue,pair->label,
108                         NULL);
109
110         }       
111
112     }
113 }
114
115 // IgnoreListUiItem::writeFromUiToSource()
116 // Takes values in the UI and puts them into the source DB
117 ///////////////////////////////////////////////////////////////////
118 void IgnoreListUiItem::writeFromUiToSource()
119 {
120   Widget w = this->getWidget();
121   DtMailEnv error;
122   DtMail::Session * d_session = theRoamApp.session()->session();
123   DtMail::MailRc * mail_rc = d_session->mailRc(error);
124   int i, num_items;
125
126   if(deleted_items != NULL)
127     {
128       num_items = deleted_items->length();
129
130       for(i = 0; i < num_items; i++)
131         {
132           mail_rc->removeIgnore(error,(*deleted_items)[i]);
133         }
134     }
135
136   if(list_items != NULL)
137     {
138       num_items = list_items->length();
139       for(i = 0; i < num_items; i++)
140         {
141           mail_rc->addIgnore(error,(*list_items)[i]->label);
142         }
143     }
144   
145   if(deleted_items != NULL)
146     {
147       delete deleted_items;
148       deleted_items = NULL;
149     }
150 }
151
152 // IgnoreListUiItem::writeFromSourceToUi()
153 // Takes values in the UI and puts them into the source DB
154 ///////////////////////////////////////////////////////////////////
155 void IgnoreListUiItem::writeFromSourceToUi()
156 {
157   DtMailEnv error;
158   DtMail::Session * d_session = theRoamApp.session()->session();
159   DtMail::MailRc * mail_rc = d_session->mailRc(error);
160   DtVirtArray<char *> *char_list = NULL;
161   int list_len, i;
162   PropStringPair *new_pair;
163
164   Widget w = this->getWidget();
165
166   XmListDeleteAllItems(w);
167
168   if(deleted_items != NULL)
169     {
170       delete deleted_items;
171       deleted_items = NULL;
172     }
173
174   if(list_items != NULL)
175   {
176     delete list_items;
177     list_items=NULL;
178   }
179
180   // ignore list code here...
181   char_list = mail_rc->getIgnoreList();
182   
183   if(char_list != NULL)
184     {
185       list_len = char_list->length();
186       list_items = new DtVirtArray<PropStringPair *>(10);
187
188       for(i = 0; i < list_len; i++)
189         {
190           new_pair = new PropStringPair;
191           new_pair->label = strdup((*char_list)[i]);
192           new_pair->value = NULL;
193
194           list_items->append(new_pair);
195         }
196     }
197   
198   options_list_init(w, char_list);
199 }
200 ///////////////////////////////////////////////////////////
201 void IgnoreListUiItem::handleAddButtonPress()
202 {
203   Widget entry_field = this->getEntryFieldWidget();
204   char *test_str = NULL;
205   PropStringPair *new_pair = NULL;
206
207   XtVaGetValues(entry_field,
208                 XmNvalue, &test_str,
209                 NULL);
210
211   if(test_str != NULL)
212     if(  (strlen(test_str) > 1) &&
213          (!XmListItemExists(this->getWidget(),
214                             XmStringCreateLocalized(test_str)))  )
215       {
216         new_pair = new PropStringPair;
217         int *pos_list, num_pos;
218
219         new_pair->label = strdup(test_str);
220         new_pair->value = NULL;
221       
222         if(XmListGetSelectedPos(this->getWidget(),
223                                 &pos_list,
224                                 &num_pos))
225           {
226             if(list_items == NULL)
227               list_items = new DtVirtArray<PropStringPair *>(10);
228
229             list_items->insert(new_pair,pos_list[0] - 1); 
230             XmListAddItem(this->getWidget(),
231                           XmStringCreateLocalized(new_pair->label),
232                           pos_list[0]);
233             
234             XmListSelectPos(this->getWidget(),
235                             pos_list[0],
236                             TRUE);
237
238             XmListSetPos(this->getWidget(),
239                          pos_list[0]);
240           }     
241         else
242           {
243             if(list_items == NULL)
244               list_items = new DtVirtArray<PropStringPair *>(10);
245
246             list_items->insert(new_pair,0); 
247             XmListAddItem(this->getWidget(),
248                           XmStringCreateLocalized(new_pair->label),
249                           1);
250             XmListSelectPos(this->getWidget(),
251                             1,
252                             TRUE);
253
254             XmListSetPos(this->getWidget(),
255                          1);
256           }
257         props_changed = TRUE;
258       }
259 }
260 ///////////////////////////////////////////////////////////
261 void IgnoreListUiItem::handleChangeButtonPress()
262 {
263   Widget entry_field = this->getEntryFieldWidget();
264   char *test_str = NULL;
265   PropStringPair *new_pair = NULL;
266   XmString replace_string;
267   int *pos_list, num_pos;
268
269   // if nothing selected nothing to change...
270   if(XmListGetSelectedPos(this->getWidget(),
271                           &pos_list,
272                           &num_pos))
273     {
274       XtVaGetValues(entry_field,
275                     XmNvalue, &test_str,
276                     NULL);
277
278       if(test_str != NULL)
279         if(strlen(test_str) > 1)
280           {
281
282             new_pair = (*list_items)[pos_list[0] - 1];
283
284             if(deleted_items == NULL)
285               {
286                 deleted_items = new DtVirtArray<char *>(10);
287               }         
288
289             deleted_items->append(strdup((*list_items)[pos_list[0] -1]->label));
290
291             free(new_pair->label);
292             new_pair->label = strdup(test_str);
293       
294             list_items->insert(new_pair,pos_list[0] - 1); 
295             replace_string = XmStringCreateLocalized(new_pair->label);
296
297             XmListReplaceItemsPos(this->getWidget(),
298                                   &replace_string,
299                                   1,
300                                   pos_list[0]);
301
302             XmListSelectPos(this->getWidget(),
303                             pos_list[0],
304                             TRUE);
305           }
306         props_changed = TRUE;
307     }
308 }
309 ///////////////////////////////////////////////////////////
310 void IgnoreListUiItem::handleDeleteButtonPress()
311 {
312   Widget list_widget = this->getWidget();
313   int *p_list, p_count;
314
315   // get the selected position
316   if(XmListGetSelectedPos(list_widget,
317                           &p_list,
318                           &p_count))
319     {
320       
321       if(deleted_items == NULL)
322         {
323           deleted_items = new DtVirtArray<char *>(10);
324         }       
325
326       deleted_items->append(strdup((*list_items)[p_list[0] -1]->label));
327
328       // delete the item from our list 
329       this->list_items->remove(p_list[0] - 1); // remove only first
330
331       // delete the item from the widget
332       XmListDeletePos(list_widget, p_list[0]);
333
334       XtVaSetValues(this->getEntryFieldWidget(),
335                         XmNvalue,"",
336                         NULL);
337       XmListSelectPos(list_widget,
338                       p_list[0],
339                       TRUE);
340       props_changed = TRUE;
341       
342     }
343
344
345
346 }
347 void IgnoreListUiItem::AddDefaults()
348 {
349   DtMailEnv error;
350   DtMail::Session * d_session = theRoamApp.session()->session();
351   DtMail::MailRc * mail_rc = d_session->mailRc(error);
352   DtVirtArray<char *> *char_list = NULL;
353   int list_len;
354
355   // ignore list code here...
356   char_list = mail_rc->getIgnoreList();
357
358   list_len = char_list->length();
359   if(list_len == 0)
360   {
361       XmTextFieldSetString(entry_field_widget,"Received");
362       handleAddButtonPress();
363       XmTextFieldSetString(entry_field_widget,"");
364       mail_rc->addIgnore(error,"Received");
365       mail_rc->update(error);
366   }
367 }
368