nsgmls: resolve coverity warnings related to uninitialed members in C++ classes
[oweals/cde.git] / cde / programs / dtpad / session.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: session.c /main/3 1995/11/01 10:39:26 rswiston $ */
24 /**********************************<+>*************************************
25 ***************************************************************************
26 **
27 **  File:        session.c
28 **
29 **  Project:     DT dtpad, a memo maker type editor based on the motif
30 **               text widget.
31 **
32 **  Description:  Provides the functionality for saving and restoring the
33 **                user's session.
34 **                      
35 **
36 *******************************************************************
37 **  (c) Copyright Hewlett-Packard Company, 1990, 1991, 1992, 1993.
38 **  All rights are
39 **  reserved.  Copying or other reproduction of this program
40 **  except for archival purposes is prohibited without prior
41 **  written consent of Hewlett-Packard Company.
42 ********************************************************************
43 **
44 ********************************************************************
45 **  (c) Copyright 1993, 1994 Hewlett-Packard Company
46 **  (c) Copyright 1993, 1994 International Business Machines Corp.
47 **  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
48 **  (c) Copyright 1993, 1994 Novell, Inc.
49 ********************************************************************
50 **
51 **
52 **************************************************************************
53 **********************************<+>*************************************/
54 #include "dtpad.h"
55 #include <Dt/Wsm.h> 
56 /*
57  * This for the sleazy hack to get the window manager frame width/height
58  */
59 #include <Xm/VendorSEP.h>
60
61 /* Copied from BaseClassI.h */
62 extern XmWidgetExtData _XmGetWidgetExtData( 
63                         Widget widget,
64 #if NeedWidePrototypes
65                         unsigned int extType) ;
66 #else
67                         unsigned char extType) ;
68 #endif /* NeedWidePrototypes */
69
70 #define MSG1  ((char *)GETMESSAGE(10, 1, "Check the file permissions."))
71 #define MSG2  ((char *)GETMESSAGE(10, 2, "%s is the file that would have been used to save your session."))
72
73 extern Widget topLevelWithWmCommand;    /* declared in main.c */
74 extern Editor *pPadList;
75 extern int numActivePads;
76
77 /*  Structure used on a save session to see if a dt is iconic  */
78
79 static Atom wm_state_atom;
80 typedef struct
81 {
82    int state;
83    Window icon;
84 } WM_STATE;
85
86
87 /************************************************************************
88  *
89  * SaveMain - saves the parameters associated with a Pad's mainWindow
90  *      (called from SaveSessionCB() below).
91  *
92  ***********************************************************************/
93 void 
94 SaveMain(
95         Editor *pPad,
96         int padNum,
97         int fd)
98 {
99     char bufr[1024];
100     Position x,y;
101     Dimension width, height;
102     Atom *pWsPresence;
103     unsigned long numInfo;
104     Atom actual_type;
105     int  actual_format;
106     unsigned long nitems;
107     unsigned long leftover;
108     WM_STATE * wm_state;
109
110     if(pPad->mainWindow != (Widget)NULL) 
111     {
112         XmVendorShellExtObject vendorExt;
113         XmWidgetExtData        extData;
114
115         if(XtIsRealized(pPad->mainWindow))
116             sprintf(bufr,"*mainWindow%d.ismapped: True\n", padNum);
117
118         /* Get and write out the geometry info for our Window */
119         x = XtX(XtParent(pPad->mainWindow));
120         y = XtY(XtParent(pPad->mainWindow));
121
122         /*
123          * Modify x & y to take into account window mgr frames
124          * This is pretty bogus, but I don't know a better way to do it.
125          */
126         extData = _XmGetWidgetExtData(pPad->app_shell, XmSHELL_EXTENSION);
127         vendorExt = (XmVendorShellExtObject)extData->widget;
128         x -= vendorExt->vendor.xOffset;
129         y -= vendorExt->vendor.yOffset;
130
131         width = XtWidth(XtParent(pPad->mainWindow));
132         height = XtHeight(XtParent(pPad->mainWindow));
133
134         sprintf(bufr, "%s*mainWindow%d.x: %d\n", bufr, padNum, x);
135         sprintf(bufr, "%s*mainWindow%d.y: %d\n", bufr, padNum, y);
136         sprintf(bufr, "%s*mainWindow%d.width: %d\n", bufr, padNum, width);
137         sprintf(bufr, "%s*mainWindow%d.height: %d\n", bufr, padNum, height);
138
139         wm_state_atom = XmInternAtom (XtDisplay(pPad->app_shell), "WM_STATE", 
140                                       False);
141         /*  Getting the WM_STATE property to see if iconified or not */
142         XGetWindowProperty (XtDisplay(pPad->app_shell), 
143                             XtWindow (pPad->app_shell),
144                             wm_state_atom, 0L, (long) BUFSIZ, False,
145                             wm_state_atom, &actual_type, &actual_format,
146                             &nitems, &leftover, (unsigned char **) &wm_state);
147
148         /* Write out if iconified our not */
149         sprintf(bufr, "%s*mainWindow%d.iconify: ", bufr, padNum);
150
151         if (wm_state->state == IconicState)
152           sprintf (bufr, "%sTrue\n", bufr);
153         else
154           sprintf (bufr, "%sFalse\n", bufr);
155
156         if(DtWsmGetWorkspacesOccupied(XtDisplay(pPad->app_shell), 
157                                   XtWindow(pPad->app_shell), &pWsPresence,
158                                   &numInfo) == Success)
159         {
160             int i;
161             sprintf(bufr, "%s*mainWindow%d.workspaceList: ", bufr, padNum);
162             for(i = 0; i < numInfo; i++)
163             {
164                 char *name =  XGetAtomName(XtDisplay(pPad->app_shell),
165                                            pWsPresence[i]);
166                 sprintf(bufr, "%s %s", bufr, name);
167                 XtFree(name);
168             }
169             sprintf(bufr, "%s\n", bufr);
170             XtFree((char *)pWsPresence);
171         }
172
173         write (fd, bufr, strlen(bufr));
174     }
175     if(pPad->fileStuff.fileName != (char *)NULL)
176     {
177         sprintf(bufr, "*mainWindow%d.fileName: %s\n", padNum, 
178                 pPad->fileStuff.fileName);
179         write (fd, bufr, strlen(bufr));
180     }
181 }
182
183
184 /************************************************************************
185  *
186  *  SaveSessionCB - saves the editor state (just filename) - does not save
187  *      the file at this time.
188  *
189  *      This routines is setup as the "WM_SAVE_YOURSELF" WMProtocolCallback
190  *      on the top level widget (created via XtInitialize).
191  *
192  ************************************************************************/
193 /* ARGSUSED */
194 void 
195 SaveSessionCB(
196         Widget w,                       /* widget id */
197         caddr_t client_data,            /* data from application  */
198         caddr_t call_data )             /* data from widget class */
199 {
200     char *longpath, *fileName;
201     int fd, numPadsToSave;
202     char *xa_CommandStr[10];
203     char *tmpStr, bufr[1024];
204     Editor *pPad;
205     int i;
206
207     /* Xt may not pass a widget as advertised (??? is this needed? - hp) */
208     if(!XtIsShell(w))
209         w = XtParent(w);
210
211     for(pPad = pPadList, numPadsToSave = 0; pPad != (Editor *)NULL; 
212         pPad = pPad->pNextPad)
213     {
214         if(pPad->inUse == True)
215             numPadsToSave++;
216     }
217     if(numPadsToSave < 1)
218     {
219         xa_CommandStr[0] = (char *)NULL;
220         XSetCommand(XtDisplay(w), XtWindow(w), xa_CommandStr, 1);
221         return;
222     }
223
224     DtSessionSavePath(w, &longpath, &fileName);
225
226     /*  Create the session file  */
227     if ((fd = creat (longpath, S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP)) == -1)
228     {
229         tmpStr = (char *)malloc(strlen(MSG2) + strlen(longpath)+ 1);
230         sprintf(tmpStr, MSG2, longpath);
231         _DtSimpleErrnoError(pPad->progname, DtError, MSG1, tmpStr, NULL);
232         free(tmpStr);
233         XtFree ((char *)longpath);
234         return;
235     }
236
237     sprintf(bufr, "*pads.numActivePads: %d\n", numPadsToSave);
238     write (fd, bufr, strlen(bufr));
239
240     for(pPad = pPadList, i = 0; pPad != (Editor *)NULL; 
241         pPad = pPad->pNextPad, i++)
242     {
243         if(pPad->inUse == True)
244             SaveMain(pPad, i, fd);
245     }
246
247     close(fd);
248
249     i = 0;
250     xa_CommandStr[i] = pPadList->progname; i++;
251     xa_CommandStr[i] =  "-session"; i++;
252     xa_CommandStr[i] =  fileName; i++;
253
254     XSetCommand(XtDisplay(topLevelWithWmCommand), 
255                 XtWindow(topLevelWithWmCommand), xa_CommandStr, i);
256
257     XtFree ((char *)fileName);
258 }
259
260
261 /***********************************************************************
262  *
263  * closeCB - set up as the "WM_DELETE_WINDOW" WMProtocolCallback on the
264  *      application shell for each Editor instance - and called when
265  *      a delete window command is received from the Window Manager.
266  *      Waits for CloseWindow to become false before it continues.
267  *
268  ***********************************************************************/
269  
270 /* ARGSUSED */
271 void
272 closeCB(
273         Widget w,
274         caddr_t client_data,
275         caddr_t call_data )
276 {
277     Editor *pPad = (Editor *)client_data;
278
279     /* call the callback for Exit within the File Menu pulldown */
280     XtCallCallbacks(pPad->ExitWid, XmNactivateCallback, (XtPointer)pPad);
281 }
282
283
284 /***********************************************************************
285  *
286  * restoreSession - gets the valid x and y location of where to put the
287  *      Text Edit on the root window.  Sets the global varible
288  *      dtpad.saveRestore to True so the rest of the program knows that
289  *      a session is being restored.
290  *
291  ***********************************************************************/
292 void
293 restoreSession(
294         Editor *pPad)
295 {
296     XrmDatabase db;
297     char *tmpStr;
298     XrmName xrm_name[5];
299     XrmRepresentation rep_type;
300     XrmValue value;
301     char *fileName = pPad->xrdb.session;
302     char *path;
303     int numPadsToRestore, i;
304     Boolean foundPad;
305
306     if(DtSessionRestorePath(topLevelWithWmCommand, &path, fileName) == False)
307         path = fileName;
308
309     /*  Open the file as a resource database */
310     if ((db = XrmGetFileDatabase (path)) == NULL) 
311     {
312       tmpStr = (char *)XtMalloc(strlen(MSG2) + strlen(path)+ 1);
313       sprintf(tmpStr, MSG2, path);
314       _DtSimpleErrnoError(pPad->progname, DtError, MSG1, tmpStr, NULL);
315       XtFree(tmpStr);
316       return;
317     }
318
319     xrm_name[0] = XrmStringToQuark ("pads");
320     xrm_name[1] = XrmStringToQuark ("numActivePads");
321     xrm_name[2] = 0;
322     XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
323     numPadsToRestore = atoi((char *)value.addr);
324
325     if(numPadsToRestore == 0)
326     {
327         /*
328          * Either it's an old (i.e. 2.01) session file,
329          * or it's bogus.  Either way, we'll create one
330          * window, taking whatever mainWindow: resources
331          * we can find.
332          */
333         xrm_name[0] = XrmStringToQuark ("mainWindow");
334         xrm_name[2] = 0;
335
336         /* get x position */
337         xrm_name[1] = XrmStringToQuark ("x");
338         XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
339         pPad->x = atoi((char *)value.addr);
340
341         /* get y position */
342         xrm_name[1] = XrmStringToQuark ("y");
343         XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
344         pPad->y = atoi((char *)value.addr);
345  
346         pPad->saveRestore = True;
347
348         return;
349     }      
350
351     RestorePad(pPad, 0, db);
352
353     for(i = 1; i < numPadsToRestore; i++)
354     {
355         foundPad = FindOrCreatePad(&pPad);
356         RestorePad(pPad, i, db);
357
358         if(foundPad == False)
359             RealizeNewPad(pPad);
360         else
361             ManageOldPad(pPad, False);
362     }
363 }
364
365
366 /************************************************************************
367  *
368  * RestoreMain - 
369  *
370  ***********************************************************************/
371 static void
372 RestoreMain(
373         Editor *pPad,
374         int padNum,
375         XrmDatabase db)
376 {
377     char * iconify = NULL;
378     char buf[1024];
379     XrmName xrm_name[5];
380     XrmRepresentation rep_type;
381     XrmValue value;
382
383     sprintf(buf, "mainWindow%d", padNum);
384     xrm_name[0] = XrmStringToQuark(buf);
385     xrm_name[2] = 0;
386
387     /* get x position */
388     xrm_name[1] = XrmStringToQuark ("x");
389     XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
390     pPad->x = atoi((char *)value.addr);
391
392     /* get y position */
393     xrm_name [1] = XrmStringToQuark ("y");
394     XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
395     pPad->y = atoi((char *)value.addr);
396
397     /* get width */
398     xrm_name [1] = XrmStringToQuark ("width");
399     XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
400     pPad->width = atoi((char *)value.addr);
401
402     /* get height */
403     xrm_name [1] = XrmStringToQuark ("height");
404     XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
405     pPad->height = atoi((char *)value.addr);
406  
407    /*  Get and set whether the view is iconic  */
408    xrm_name [1] = XrmStringToQuark ("iconify");
409    XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
410    /*  If there is an iconify resource and its value is True,  */
411    /*  then mark the window as iconified.                      */
412    if ((iconify = (char *) value.addr) != NULL &&
413                                     strcmp (iconify, "True") == 0)
414       pPad->iconic = True;
415    else
416       pPad->iconic = False;
417
418     /* get the file name */
419     xrm_name [1] = XrmStringToQuark ("fileName");
420     XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
421     pPad->fileStuff.fileName = strdup((char *) value.addr);
422
423     /* get workspace list */
424     xrm_name[1] = XrmStringToQuark("workspaceList");
425     XrmQGetResource(db, xrm_name, xrm_name, &rep_type, &value);
426     pPad->xrdb.workspaceList = strdup((char *) value.addr);
427
428 }
429
430
431 /************************************************************************
432  *
433  * RestorePad - 
434  *
435  ***********************************************************************/
436 void
437 RestorePad(
438         Editor *pPad,
439         int padNum,
440         XrmDatabase db)
441 {
442     RestoreMain(pPad, padNum, db);
443     pPad->saveRestore = True;
444 }