-fpermissive to allow GCC to compile old C++
[oweals/cde.git] / cde / programs / dtmail / MotifApp / Application.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 /* $TOG: Application.C /main/15 1998/10/01 12:10:26 mgreess $ */
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 //////////////////////////////////////////////////////////////////////////////
43 //         This example code is from the book:
44 //
45 //           Object-Oriented Programming with C++ and OSF/Motif
46 //         by
47 //           Douglas Young
48 //           Prentice Hall, 1992
49 //           ISBN 0-13-630252-1 
50 //
51 //         Copyright 1991 by Prentice Hall
52 //         All Rights Reserved
53 //
54 //  Permission to use, copy, modify, and distribute this software for 
55 //  any purpose except publication and without fee is hereby granted, provided 
56 //  that the above copyright notice appear in all copies of the software.
57 ///////////////////////////////////////////////////////////////////////////////
58 //////////////////////////////////////////////////////////////////////////////
59
60
61 ////////////////////////////////////////////////////////////
62 // Application.C: 
63 ////////////////////////////////////////////////////////////
64 #include "Application.h"
65 #include "MainWindow.h"
66 #include <X11/StringDefs.h>
67 #include <X11/Shell.h>
68 #include <assert.h>
69 #include <stdlib.h>
70 #include <sys/types.h>
71 #include <time.h>
72 #include <unistd.h>
73 #include <nl_types.h>
74 #include <string.h>
75
76 #include "EUSDebug.hh"
77
78
79 XtResource
80 Application::_appResources[] = {
81   {
82     "workspaceList", "WorkspaceList", XtRString, sizeof(XtRString),
83     XtOffset(Application *, _appWorkspaceList), XtRString, (XtPointer) NULL
84   }
85 };
86
87 // XPG3 compatible. NL_CAT_LOCALE is set to 1 (non-zero) in XPG4. Use NL_CAT_LOCALE
88 // for all catopen() calls. This is also defined in include/DtMail/Common.h for dtmail
89 // and libDtMail catopen calls, if later on we have a common include file for
90 // dtmail, libDtMail and MotifApp, we can move this define over there.
91
92 #if defined(sun) && (_XOPEN_VERSION == 3)
93 #undef NL_CAT_LOCALE
94 #define NL_CAT_LOCALE 0
95
96 // If NL_CAT_LOCALE is not defined in other platforms, set it to 0
97 #elif !defined(NL_CAT_LOCALE)   
98 #define NL_CAT_LOCALE 0
99 #endif
100
101 #ifdef hpV4
102 /*
103  * Wrapper around catgets -- this makes sure the message string is saved
104  * in a safe location; so repeated calls to catgets() do not overwrite
105  * the catgets() internal buffer.  This has been a problem on HP systems.
106  */
107 char *catgets_cache2(nl_catd catd, int set, int num, char *dflt)
108 {
109
110 #define MSGS_PER_SET_MAX        12
111 #define NUM_SETS_MAX            2
112
113   /* array to hold messages from catalog */
114   static  char *MsgCat[NUM_SETS_MAX][MSGS_PER_SET_MAX];
115   
116   /* convert to a zero based index */
117   int setIdx = set - 1;
118   int numIdx = num - 1;
119   
120   if ( ! MsgCat[setIdx][numIdx] ) {
121     MsgCat[setIdx][numIdx] = strdup( catgets(catd, set, num, dflt));
122   }
123   
124   return MsgCat[setIdx][numIdx];
125 }
126
127 #endif
128
129 Application *theApplication = NULL;
130
131 nl_catd catd = (nl_catd) -1;    // catgets file descriptor
132
133 extern String ApplicationFallbacks[];
134
135 Application::Application ( char *appClassName ) : 
136                     UIComponent ( appClassName )
137 {
138     // Set the global Application pointer
139     DebugPrintf(2, "Application::Application(%p \"%s\")\n", appClassName, appClassName);
140     
141     theApplication = this;  
142     
143     // Initialize data members
144     
145     _display    = NULL;
146     _appContext = NULL;
147     _bMenuButton = 0;
148     _windows    = NULL;
149     _numWindows = 0;
150     _shutdownEnabled = 1;
151     _applicationClass = strdup ( appClassName );
152 }
153
154 void Application::initialize ( int *argcp, char **argv )
155 {
156     DebugPrintf(2, "Application::initialize(%p %d, %p)\n", argcp, *argcp, argv);
157
158     DebugPrintf(3, "Application::initialize - Initializing privileges.\n");
159
160     // The Solaris sendmail operates differently than the HP/IBM sendmail. 
161     // sendmail on Solaris runs as 'root' and so has access permissions 
162     // to any file on the system. sendmail on HP/IBM runs as set-group-id 
163     // 'mail' and so requires that all mailboxes that it may deliver e-mail 
164     // to be writable either by being group mail group writable, or by being 
165     // writable by the world. On those platforms, then, dtmail is required 
166     // to always run with set-group-id mail otherwise, when mailboxes are 
167     // saved, they will loose their group ownership and sendmail will no 
168     // onger be able to deliver to those mailboxes.
169
170     // we have to be set-gid to group "mail" when opening and storing
171     // folders.  But we don't want to do everything as group mail.
172     // here we record our original gid, and set the effective gid
173     // back the the real gid.  We'll set it back when we're dealing
174     // with folders...
175     //
176     _originalEgid = getegid();  // remember effective group ID
177     _originalRgid = getgid();   // remember real group ID
178     disableGroupPrivileges();   // disable group privileges from here on
179
180     DebugPrintf(3, "Application::initialize - Initializing Xt.\n");
181
182     _w = XtOpenApplication (
183                         &_appContext, 
184                         _applicationClass, 
185                         (XrmOptionDescList) NULL, 0, 
186                         argcp, argv, ApplicationFallbacks,
187                         sessionShellWidgetClass, (ArgList) NULL, 0 );
188     
189     // Extract and save a pointer to the X display structure
190     DebugPrintf(3, "Application::initialize - Extracting display.\n");
191     _display = XtDisplay ( _w );
192
193     // Set virtual BMenu mouse binding
194     int numButtons = XGetPointerMapping(_display, (unsigned char *)NULL, 0);
195     _bMenuButton = (numButtons < 3) ? Button2 : Button3;
196     
197     // The Application class is less likely to need to handle
198     // "surprise" widget destruction than other classes, but
199     // we might as well install a callback to be safe and consistent
200     DebugPrintf(3, "Application::initialize - Installing destroy handler.\n");
201     installDestroyHandler();
202     
203     // Center the shell, and make sure it isn't visible
204     DebugPrintf(3, "Application::initialize - Setting window size.\n");
205     XtVaSetValues ( _w,
206                    XmNmappedWhenManaged, FALSE,
207                    XmNx, DisplayWidth ( _display, 0 ) / 2,
208                    XmNy, DisplayHeight ( _display, 0 ) / 2,
209                    XmNwidth,  1,
210                    XmNheight, 1,
211                    NULL );
212     
213     // The instance name of this object was set in the UIComponent 
214     // constructor, before the name of the program was available
215     // Free the old name and reset it to argv[0]
216     DebugPrintf(3, "Application::initialize - Deleting name %p\n", _name);
217     free(_name);
218     _name = strdup ( argv[0] );
219
220     // Force the shell window to exist so dialogs popped up from
221     // this shell behave correctly
222     DebugPrintf(3, "Application::initialize - Realizing shell window.\n");
223     XtRealizeWidget ( _w );
224     
225     getResources(_appResources, XtNumber(_appResources));
226
227     // Initialize and manage any windows registered
228     // with this application.
229     
230     for ( int i = 0; i < _numWindows; i++ )
231     {
232         DebugPrintf(3, "Application::initialize - Initializing windows[%d]\n", i);
233         _windows[i]->initialize();
234         DebugPrintf(3, "Application::initialize - Managing windows[%d]\n", i);
235         _windows[i]->manage();
236     }
237 }
238
239 // Calling _exit() now to work around a problem with threads
240 // deadlocking if exit() is called.
241 // Need to fix the threads deadlocking bug and then replace
242 // _exit() with exit().
243
244 Application::~Application()
245 {
246     //
247     // Allocated using strdup, so free using free.
248     //
249     free((void*) _applicationClass);
250     delete []_windows;
251
252 #ifdef CDExc21492
253   #if defined(__hpux) || defined(USL) || defined(__uxp__)
254     this->BasicComponent::~BasicComponent();
255   #elif  __osf__
256     BasicComponent * The_End = this;
257     The_End->BasicComponent::~BasicComponent();
258   #else
259     BasicComponent::~BasicComponent();
260   #endif
261 #endif
262
263     catclose(catd);
264
265     // In an MT environment, calling exit() causes threads to
266     // hang and a deadlock results.
267     // Call _exit() instead
268
269     _exit(0);
270 }
271
272 // ApplicationExtractEventTime - extract the time the
273 // current event happened if it is one we are interested
274 // in - this is used to delay actions that can lock the application
275 // while the user is being interactive with the application
276 //
277
278 inline void Application::extractAndRememberEventTime(XEvent *event)
279 {
280   switch (((XAnyEvent *)event)->type)
281     {
282     case KeyPress:      // press any key on the keyboard
283     case ButtonPress:   // press any botton on the screen
284     case MotionNotify:  // motion events
285       _lastInteractiveEventTime = time((time_t *)0);
286       break;
287     }
288 }
289
290 void Application::handleEvents()
291 {
292     // Just loop forever
293 #if 0    
294     XtAppMainLoop ( _appContext );
295 #else
296     XEvent      event;
297
298     _lastInteractiveEventTime = time((time_t *)0);
299     
300     for (;;) {
301       XtAppNextEvent( _appContext, &event );
302       extractAndRememberEventTime( &event );
303       XtDispatchEvent( &event );
304     }
305 #endif
306 }
307
308 void Application::registerWindow ( MainWindow *window )
309 {
310     int i;
311     MainWindow **newList;
312     
313     // Allocate a new list large enough to hold the new
314     // object, and copy the contents of the current list 
315     // to the new list
316     
317     newList = new MainWindow*[_numWindows + 1];
318     
319     for ( i = 0; i < _numWindows; i++ )
320         newList[i] = _windows[i];
321     
322     // Install the new list and add the window to the list
323     
324     if (_numWindows > 0) delete []_windows;
325     _windows =  newList;
326     _windows[_numWindows] = window;
327     
328     _numWindows++;
329 }
330
331 void Application::unregisterWindow ( MainWindow *window )
332 {
333     int i, index;
334     
335     // If this is the last window bye bye.
336
337     if (isEnabledShutdown() && _numWindows == 1)
338     {
339         _numWindows--;
340
341         // Call derived class's shutdown method.
342         shutdown();
343         return;
344     }
345
346     // Copy all objects, except the one to be removed, to a new list
347
348     MainWindow **newList = new MainWindow*[_numWindows - 1];
349     
350     for (i=0, index=0; i<_numWindows; i++)
351       if (_windows[i] != window)
352         newList[index++] = _windows[i];
353     
354     delete []_windows;
355     _windows = newList;
356     _numWindows--;
357 }
358
359 void Application::manage()
360 {
361     // Manage all application windows. This will pop up
362     // iconified windows as well.
363     
364     for ( int i = 0; i < _numWindows; i++ )
365         _windows[i]->manage();
366 }
367
368 void Application::unmanage()
369 {
370     // Unmanage all application windows
371     
372     for ( int i = 0; i < _numWindows; i++ )
373         _windows[i]->unmanage();
374 }
375
376 void Application::iconify()
377 {
378     // Iconify all top-level windows.
379     
380     for ( int i = 0; i < _numWindows; i++ )
381         _windows[i]->iconify();
382 }
383
384
385 void
386 Application::open_catalog()
387 {
388     // open message catalog file
389     catd = catopen("MotifApp", NL_CAT_LOCALE);
390 }
391
392 void
393 Application::setAppWorkspaceList(char *workspaceList)
394 {
395     // open message catalog file
396     if (NULL != _appWorkspaceList)
397       free(_appWorkspaceList);
398
399     _appWorkspaceList = strdup(workspaceList);
400 }