Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtmail / dtmail / RoamInterruptibleCmd.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 /* $XConsortium: RoamInterruptibleCmd.C /main/3 1995/11/06 16:12:29 rswiston $ */
24 /*
25  *+SNOTICE
26  *
27  *      RESTRICTED CONFIDENTIAL INFORMATION:
28  *      
29  *      The information in this document is subject to special
30  *      restrictions in a confidential disclosure agreement bertween
31  *      HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
32  *      document outside HP, IBM, Sun, USL, SCO, or Univel wihtout
33  *      Sun's specific written approval.  This documment and all copies
34  *      and derivative works thereof must be returned or destroyed at
35  *      Sun's request.
36  *
37  *      Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
38  *
39  *+ENOTICE
40  */
41 ///////////////////////////////////////////////////////////////
42 // RoamInterruptibleCmd.C: Abstract class that supports lengthy,
43 //                     user-interruptible activities
44 //////////////////////////////////////////////////////////////
45 #include "RoamInterruptibleCmd.hh"
46 #include "DtMailWDM.hh"
47 #include "Application.h"
48 #include <Xm/Xm.h>
49 #include <Xm/MessageB.h>
50 #include <assert.h>
51 extern forceUpdate( Widget );
52
53
54 RoamInterruptibleCmd::RoamInterruptibleCmd ( char *name, 
55                                              char *label, 
56                                              int active ) :
57                         NoUndoCmd ( name, label, active )
58 {
59     _wpId        = NULL;   // There is no work procedure yet
60     _callback    = NULL;           // Callbacks are specified in execute()
61     _clientData  = NULL;
62     _done        = FALSE; 
63     _interrupted = FALSE;
64 }
65
66 RoamInterruptibleCmd::~RoamInterruptibleCmd()
67 {
68     // Clean up by removing all callbacks
69     
70     if ( _wpId)    
71         XtRemoveWorkProc ( _wpId );
72 }
73
74 void 
75 RoamInterruptibleCmd::execute ( 
76     RoamTaskDoneCallback callback, 
77     void *clientData
78 )
79 {
80     _callback   = callback;
81     _clientData = clientData;
82     execute();
83 }
84
85 void 
86 RoamInterruptibleCmd::execute()
87 {
88     char *name_str;
89     
90     name_str = (char *) name();
91
92     _done  = FALSE;  // Initialize flag
93
94     // Let the derived class post the dialog.
95     // Updates happen when derived classes call update() or 
96     // updateMessage().
97
98     post_dialog();
99     
100     // Call the Cmd execute function to handle the Undo, and other
101     // general mechanisms supported by Cmd. 
102     // execute() calls doit() of derived class.
103     
104     Cmd::execute();  
105
106     // If the task was completed in a single call,
107     // don't bother to set up a work procedure. Just
108     // give derived classes a chance to cleanup and
109     // call the application's callback function
110
111     // If it was interrupted, the interruptCallback would have been
112     // called already and the dialog would have been unposted...
113
114     // We need to focus on only two cases here:  what if the task was
115     // was completed in one call without interruptions (unpost dialog 
116     // and call callback indicating task completed), and what if the 
117     // task was not completed in one call (install a workProc...)
118     // 
119
120     // if it done but not interrupted, it was genuinely done.
121     if (_done && !_interrupted)
122       {
123           unpost_dialog();  // derived classes implement this
124           cleanup();
125           
126           if ( _callback )  // the FALSE is to say it was not interrupted.
127               ( *_callback )( this, FALSE, _clientData );
128       }
129
130     // If the task is not done and it was not interrupted and there is
131     // a callback to install, install a work procedure to continue the 
132     // task as soon as possible.  Call the callback via the work proc
133     // after completion.
134     // 
135     
136     else if ((!_done && !_interrupted && _callback)) 
137       {
138           
139           _wpId = XtAppAddWorkProc ( theApplication->appContext(), 
140                                   &RoamInterruptibleCmd::workProcCallback,
141                                   (XtPointer) this );
142       }
143 }
144
145 Boolean 
146 RoamInterruptibleCmd::workProcCallback ( 
147     XtPointer clientData 
148 )
149 {
150     RoamInterruptibleCmd *obj = (RoamInterruptibleCmd *) clientData;
151     
152     // The work procedure just returns the value returned by the
153     // workProc member function.
154     
155     return ( obj->workProc() );
156 }
157
158 Boolean 
159 RoamInterruptibleCmd::workProc()
160 {
161     // Call derived class's check_if_done() and see if they think the
162     // work is already done.
163
164      check_if_done();
165
166      if (_interrupted) {
167
168          unpost_dialog();
169          cleanup();
170
171         // the TRUE is to say task was interrupted
172
173         if ( _callback )  
174             ( *_callback )( this, TRUE, _clientData );
175      }   
176     
177     // If the task has been completed, hide the dialog,
178     // give the derived class a chance to clean up, and notify
179     // the application that instantiated this object.
180     
181     else if (_done) {
182         unpost_dialog();
183         cleanup();
184         
185         // the FALSE is to say task completed without interruptions
186
187         if ( _callback )  
188             ( *_callback )( this, FALSE, _clientData );
189     }
190     
191     return _done;
192 }
193
194 void 
195 RoamInterruptibleCmd::cleanup()
196 {
197     // Empty
198 }
199
200 // The task has been interrupted.  The user clicked on interrupt...
201
202 void 
203 RoamInterruptibleCmd::interruptCallback ( 
204     void * clientData 
205 )
206 {
207     RoamInterruptibleCmd *obj = ( RoamInterruptibleCmd * ) clientData;
208     
209     // Just set the _interrupt flag to TRUE. The workProc() 
210     // function will notice the next time it is called
211     
212     obj->interrupt();
213 }
214
215 void 
216 RoamInterruptibleCmd::interrupt()
217 {
218     _interrupted = TRUE;
219
220     if (_wpId) {
221         // Remove the work procedure
222
223         XtRemoveWorkProc ( _wpId );
224     }
225     
226     // Remove the working dialog and give derived 
227     // classes a chance to clean up 
228     
229     unpost_dialog();
230     cleanup();
231     
232     // Notify the application that the task was interrupted
233     
234     if ( _callback )
235         ( *_callback )( this, TRUE, _clientData);
236 }
237
238 void 
239 RoamInterruptibleCmd::updateMessage ( 
240     char * msg 
241 )
242 {
243     theDtMailWDM->updateDialog ( msg );
244     forceUpdate(theDtMailWDM->baseWidget());
245 }
246
247 Boolean
248 RoamInterruptibleCmd::interrupted()
249 {
250     return _interrupted;
251 }
252
253 void
254 RoamInterruptibleCmd::update()
255 {
256     forceUpdate(theDtMailWDM->baseWidget());
257 }
258
259
260