Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtmail / dtmail / Undelete.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 librararies 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 /*
24  *+SNOTICE
25  *
26  *      $TOG: Undelete.C /main/9 1998/09/24 17:33:53 mgreess $
27  *
28  *      RESTRICTED CONFIDENTIAL INFORMATION:
29  *
30  *      The information in this document is subject to special
31  *      restrictions in a confidential disclosure agreement between
32  *      HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
33  *      document outside HP, IBM, Sun, USL, SCO, or Univel without
34  *      Sun's specific written approval.  This document and all copies
35  *      and derivative works thereof must be returned or destroyed at
36  *      Sun's request.
37  *
38  *      Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
39  *
40  *+ENOTICE
41  */
42  
43 #include <Xm/Form.h>
44 #include <Xm/LabelG.h>
45 #include <Xm/RowColumn.h>
46 #include "Undelete.hh"
47 #include "MsgScrollingList.hh"
48 #include "RoamMenuWindow.h"
49 #ifdef DEAD_WOOD
50 #include "QueryDialogManager.hh"
51 #endif /* DEAD_WOOD */
52 #include "DialogShell.h"
53 #include "Application.h"
54 #include "ButtonInterface.h"
55 #include "RoamCmds.h"
56 #include "RoamApp.h"
57 #include "CmdList.h"
58 #include "MsgHndArray.hh"
59 #include "Help.hh"
60 #include "MemUtils.hh"
61 #include "MailMsg.h"
62 #include "DtMailGenDialog.hh"
63
64
65 UndelMsgScrollingList::UndelMsgScrollingList (RoamMenuWindow *rmw, Widget w,
66                                                 char *string)
67     : MsgScrollingList(rmw, w, string) 
68 {
69 }
70
71 UndelMsgScrollingList::~UndelMsgScrollingList()
72 {
73 }
74
75 void
76 UndelMsgScrollingList::extended_selection(DtMailEnv &, int)
77 {
78 }
79
80
81 void
82 UndelMsgScrollingList::deleteSelected(Boolean)
83 {
84     // SR - Added stuff below.  Made code more efficient also.
85     FORCE_SEGV_DECL(MsgStruct, a_del_msg_struct);
86     FORCE_SEGV_DECL(MsgStruct, tmpMS);
87     int  position_in_list, i;
88     FORCE_SEGV_DECL(int, position_list);
89     int position_count;
90     Boolean any_selected;
91     int num_msgs;
92  
93  
94
95     XtVaGetValues( _w,
96                    XmNitemCount, &num_msgs,
97                    NULL );
98  
99     any_selected = XmListGetSelectedPos(_w,
100                                         &position_list,
101                                         &position_count);
102  
103     if (!any_selected) return;
104  
105     for (i=0; i < position_count; i++ ) {
106         position_in_list = *(position_list + i);
107  
108         a_del_msg_struct = get_message_struct(position_in_list);
109         _msgs->mark_for_delete(position_in_list - 1);
110            
111     }
112  
113     _msgs->compact(0);
114  
115  
116     // Delete the items from the scrolling list
117      XmListDeletePositions(_w,
118                           position_list,
119                           position_count);
120  
121     // XnListGetSelectedPos allocated memory for the array and
122     // wants us to free it.  See documentation.
123  
124  
125     num_msgs = _msgs->length();
126     XtFree((char*) position_list);
127 }
128  
129 void
130 UndelMsgScrollingList::insertMsg(DtMailMessageHandle tmpMH)
131 {
132     MsgScrollingList::insertMsg(tmpMH);
133 }
134
135 void
136 UndelMsgScrollingList::insertMsg(
137     DtMailEnv &mail_error, 
138     MsgStruct *tmpMS
139 )
140 {
141     XmString item;
142
143     _msgs->append(tmpMS);
144
145     DtMailHeaderLine info;
146
147     DtMail::MailBox * mbox = this->parent()->mailbox();
148
149     mbox->getMessageSummary(mail_error,
150                             tmpMS->message_handle, 
151                             _header_info, 
152                             info);
153     DtMail::Message * msg = mbox->getMessage(mail_error, tmpMS->message_handle);
154
155
156     if (mail_error.isSet()) {
157         return;
158     }
159
160     item = formatHeader(info,
161                         tmpMS->sessionNumber,
162                         show_with_attachments(msg),
163                         msg->flagIsSet(mail_error, DtMailMessageNew));
164     XmListAddItem( _w , item, 0 );
165     XmStringFree( item );
166  
167     scroll_to_bottom();
168 }
169
170 void
171 UndelMsgScrollingList::loadMsgs(
172     DtMailEnv &mail_error, 
173     MsgHndArray *deleted_msgs,
174     int count
175 )
176 {
177     MsgStruct *tmpMS;
178     XmString *msg_hdrs;
179     int i;
180     DtMailHeaderLine info;
181     DtMail::Message * msg;
182
183
184     msg_hdrs = (XmString *)malloc(sizeof(XmString) * count);
185     memset(msg_hdrs, 0, sizeof(XmString) * count);    
186
187     DtMail::MailBox * mbox = this->parent()->mailbox();
188
189     for (i = 0; i < count; i ++) {
190         tmpMS = deleted_msgs->at(i);
191         _msgs->append(tmpMS);
192         
193         mbox->getMessageSummary(mail_error,
194                             tmpMS->message_handle, 
195                             _header_info, 
196                             info);
197
198         msg =  mbox->getMessage(mail_error, tmpMS->message_handle);
199
200         if (mail_error.isSet()) {
201             return;
202         }
203
204         msg_hdrs[i] = formatHeader(info,
205                                 tmpMS->sessionNumber,
206                                 show_with_attachments(msg),
207                                 msg->flagIsSet(mail_error, DtMailMessageNew));
208     }
209
210     XmListAddItems(_w, msg_hdrs, count, 0);
211
212     // Free the strings we added to the header array.
213     for (i = 0; i < count; i++) {
214         XmStringFree(msg_hdrs[i]);
215     }
216
217     free (msg_hdrs);
218
219     scroll_to_bottom();
220 }
221
222 void
223 UndelFromListDialog::loadMsgs(
224     DtMailEnv &mail_error, 
225     MsgHndArray *deleted_msgs,
226     int count
227 )
228 {
229     _list->loadMsgs(mail_error, deleted_msgs, count);
230 }
231
232 UndelFromListDialog::UndelFromListDialog ( 
233     char *name, 
234     RoamMenuWindow *parent
235 ) : DialogShell ( name, parent )
236 {
237     assert (theApplication != NULL);
238     _list = NULL;
239 }
240
241 UndelFromListDialog::~UndelFromListDialog()
242 {
243 }
244
245 void
246 UndelFromListDialog::popped_down()
247 {
248     XtUnmapWidget(this->baseWidget());
249 }
250
251 void
252 UndelFromListDialog::popped_up()
253 {
254     if (_workArea)
255         XtMapWidget(this->baseWidget());
256 }
257
258 void
259 UndelFromListDialog::initialize()
260 {
261     DialogShell::initialize();
262 }
263
264 void
265 UndelFromListDialog::quit()
266 {
267     // Pop down the dialog
268     this->popped_down();
269 }
270
271 Widget
272 UndelFromListDialog::createWorkArea ( Widget parent )
273 {
274
275     XmString labelStr;
276     Widget l1, l2, l3, l4;
277     Widget form1;
278     XtWidgetGeometry size;
279
280     form1 = XmCreateForm(parent, "Work_Area",  NULL, 0);
281
282     // add help callback
283     printHelpId("form1", form1);
284
285     rowOfLabels = XtCreateManagedWidget ("rowOfLabels",
286                         xmFormWidgetClass, 
287                         form1,
288                         NULL, 0);
289
290
291     printHelpId("rowOfLabels", rowOfLabels);
292     // add help callback
293
294     size.request_mode = CWHeight;
295     XtQueryGeometry(rowOfLabels, NULL, &size);
296     XtVaSetValues(rowOfLabels,
297                 XmNpaneMaximum, size.height,
298                 XmNpaneMinimum, size.height,
299                 XmNrightAttachment, XmATTACH_FORM,
300                 XmNleftAttachment, XmATTACH_FORM,
301                 XmNtopAttachment, XmATTACH_FORM,
302                 XmNtopOffset, 5,
303                 XmNorientation, XmHORIZONTAL,
304                 XmNspacing, 120,
305                 XmNmarginWidth, 20,
306                 XmNentryAlignment, XmALIGNMENT_CENTER,
307                 NULL);
308
309     labelStr = XmStringCreateLocalized(GETMSG(DT_catd, 1, 14,
310            "Sender"));
311
312     l1 = XtVaCreateManagedWidget("Label1", xmLabelGadgetClass,
313                 rowOfLabels,
314                 XmNlabelString, labelStr,
315                 NULL);
316
317     XmStringFree(labelStr);
318     printHelpId("l1", l1);
319     /* add help callback */
320     // XtAddCallback(l1, XmNhelpCallback, HelpCB, helpId);
321
322
323     labelStr = XmStringCreateLocalized(GETMSG(DT_catd, 1, 15,
324            "Subject"));
325     l2 = XtVaCreateManagedWidget("Label2",
326                                 xmLabelGadgetClass, rowOfLabels,
327                                 XmNlabelString, labelStr,
328                                 NULL);
329
330
331
332     XmStringFree(labelStr);
333     printHelpId("l2", l2);
334     /* add help callback */
335     // XtAddCallback(l2, XmNhelpCallback, HelpCB, helpId);
336
337     labelStr = XmStringCreateLocalized(GETMSG(DT_catd, 1, 16,
338            "Date and Time"));
339     l3 = XtVaCreateManagedWidget("Label3",
340                         xmLabelGadgetClass, rowOfLabels,
341                         XmNlabelString, labelStr,
342                         NULL);
343
344     XmStringFree(labelStr);
345     printHelpId("l3", l3);
346     /* add help callback */
347     // XtAddCallback(l3, XmNhelpCallback, HelpCB, helpId);
348
349     labelStr = XmStringCreateLocalized(GETMSG(DT_catd, 1, 17,
350            "Size"));
351     l4 = XtVaCreateManagedWidget("Label4",
352                 xmLabelGadgetClass, rowOfLabels,
353                 XmNlabelString, labelStr,
354                 NULL);
355
356     XmStringFree(labelStr);
357     printHelpId("l4", l4);
358     /* add help callback */
359
360     _list = new UndelMsgScrollingList( this->parent(), form1, "Message_List");
361
362     DtMailEnv error;
363     DtMail::Session * d_session = theRoamApp.session()->session();
364     DtMail::MailRc * mailrc = d_session->mailRc(error);
365     const char * value;
366     int msgnums = False;
367
368     mailrc->getValue(error, "showmsgnum", &value);
369     if (error.isNotSet()) {
370         msgnums = True;
371     }
372     if (NULL != value)
373       free((void*) value);
374
375     // Adjust labels so the align on the columns
376     _list->layoutLabels(l1, l2, l3, l4);
377
378
379     rowOfButtons = XtCreateManagedWidget("RowColumn",
380                                         xmFormWidgetClass,
381                                         form1,
382                                         NULL, 0);
383     // add help callback
384     printHelpId ("rowOfButtons", rowOfButtons);
385
386     XtVaSetValues(XtParent(_list->get_scrolling_list()),
387                 XmNrightAttachment, XmATTACH_FORM,
388                 XmNleftAttachment, XmATTACH_FORM,
389                 XmNtopAttachment, XmATTACH_WIDGET,
390                 XmNtopWidget, rowOfLabels,
391                 XmNtopOffset, 3,
392                 XmNbottomAttachment, XmATTACH_WIDGET,
393                 XmNbottomWidget, rowOfButtons,
394                 XmNbottomOffset, 7,
395                 NULL);
396
397     XtVaSetValues(rowOfButtons,
398                 XmNorientation, XmHORIZONTAL,
399                 XmNpaneMaximum, size.height,
400                 XmNpaneMinimum, size.height,
401                 XmNrightAttachment, XmATTACH_FORM,
402                 XmNleftAttachment, XmATTACH_FORM,
403                 XmNbottomAttachment, XmATTACH_FORM,
404                 XmNfractionBase, 100,
405                 XmNresizable, FALSE,
406                 NULL);
407     
408     // Create undelete and close buttons.
409     this->addToRowOfButtons();
410
411
412
413     // Label should go here that says in how many days
414     // deleted messages will be destroyed, and for status.
415     rowOfMessageStatus = XtCreateManagedWidget("Message_Status",
416                                 xmFormWidgetClass,
417                                 form1, NULL, 0);
418     XtVaSetValues(rowOfMessageStatus,
419                 XmNrightAttachment, XmATTACH_FORM,
420                 XmNleftAttachment, XmATTACH_FORM,
421                 XmNtopAttachment, XmATTACH_FORM,
422                 XmNtopOffset, 5,
423                 NULL);
424
425
426     XtManageChild(_list->baseWidget());
427     XtManageChild(form1);
428
429     // return the widget
430     return(form1);
431 }
432     
433 void
434 UndelFromListDialog::insertMsg(DtMailEnv &mail_error, MsgStruct *tmpMS)
435 {
436     _list->insertMsg(mail_error, tmpMS);
437 }
438
439 void
440 UndelFromListDialog::undelLast()
441 {
442     int len;
443
444     len = _list->get_messages()->length();
445     _list->get_messages()->mark_for_delete(len - 1);
446     _list->get_messages()->compact(0);
447     XmListDeletePos(_list->get_scrolling_list(), len);
448
449 }
450
451
452 void
453 UndelFromListDialog::undelSelected()
454 {
455     FORCE_SEGV_DECL(int, position_list);
456     FORCE_SEGV_DECL(MsgHndArray, tmpMHlist);
457     int position_count;
458     Boolean any_selected = FALSE;
459
460     any_selected = XmListGetSelectedPos(_list->get_scrolling_list(),
461                                         &position_list,
462                                         &position_count);
463
464     // Don't do anything if there aren't any messages selected.
465
466     if ((!any_selected) || (position_count <= 0)) return;
467
468
469         // Put the selected messages from the Deleted Messages List
470         // into the RoamMenuWindow Message Scrolling List.
471
472
473         // This is kind of tricky, because we have to get the list of
474         // selected messages before we delete the selected messages,
475         // but we have to delete the selected messages before we
476         // pass them to undelete_messages().  The reason for this
477         // is that deleteSelected() changes the is_deleted attribute
478         // of the message handle to TRUE, which will undo undelete_messages()
479         // setting is_deleted to FALSE!  Did you follow all that?
480
481     tmpMHlist = _list->selected();
482
483         // Remove the selected messages from the Deleted Messages Dialog.
484     _list->deleteSelected();
485
486         // Add the messages to the RoamMenuWindow scrolling list.
487     MsgScrollingList *msl = parent()->list();
488     msl->undelete_messages(tmpMHlist);
489
490     XtFree((char*) position_list);
491
492 }
493
494 void
495 UndelFromListDialog::addToRowOfButtons()
496 {
497     FORCE_SEGV_DECL(CmdInterface, ci);
498     Widget w, prev_widget;
499
500     _undelete_button = new DoUndeleteCmd(
501                                 "Undelete", 
502                                 GETMSG(DT_catd, 1, 115, "Undelete"), 
503                                 TRUE, 
504                                 this);
505     ci = new ButtonInterface (rowOfButtons, _undelete_button);
506
507     w = ci->baseWidget();
508     XtVaSetValues(w,
509         XmNleftAttachment, XmATTACH_POSITION,
510         XmNleftPosition, 35,          // centralized tUndelete and Close buttons
511         XmNbottomAttachment, XmATTACH_FORM,
512         NULL );
513     ci->manage();
514     prev_widget = w;
515
516     _close_button = new CloseUndelCmd(
517                                 "Close",
518                                 GETMSG(DT_catd, 1, 116, "Close"), 
519                                 TRUE, 
520                                 this);
521     ci = new ButtonInterface (rowOfButtons, _close_button);
522     w = ci->baseWidget();
523     XtVaSetValues(w,
524         XmNleftAttachment, XmATTACH_WIDGET,
525         XmNleftWidget, prev_widget,
526         XmNleftOffset, 30,
527         XmNbottomAttachment, XmATTACH_FORM,
528         NULL );
529     ci->manage();
530
531 }
532
533 void
534 UndelFromListDialog::expunge(void)
535 {
536     for (int pos = 0; pos < _list->get_num_messages(); pos++) {
537         _list->get_messages()->mark_for_delete(pos);
538     }
539
540     _list->get_messages()->compact(0);
541
542     XmListDeleteAllItems(_list->get_scrolling_list());
543 }
544