dtcm: Resolve CID 87408
[oweals/cde.git] / cde / programs / dtcm / dtcm / 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 /*******************************************************************************
24 **
25 **  session.c
26 **
27 **  $TOG: session.c /main/6 1997/06/18 17:28:49 samborn $
28 **
29 **  RESTRICTED CONFIDENTIAL INFORMATION:
30 **
31 **  The information in this document is subject to special
32 **  restrictions in a confidential disclosure agreement between
33 **  HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
34 **  document outside HP, IBM, Sun, USL, SCO, or Univel without
35 **  Sun's specific written approval.  This document 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 *******************************************************************************/
42
43 /*
44  *  (c) Copyright 1993, 1994 Hewlett-Packard Company
45  *  (c) Copyright 1993, 1994 International Business Machines Corp.
46  *  (c) Copyright 1993, 1994 Novell, Inc.
47  *  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
48  */
49
50 #include <stdio.h>
51 #include <fcntl.h>
52 #include <unistd.h>
53 #include <stdlib.h>
54 #include <sys/stat.h>
55 #include <Xm/Xm.h>
56 #include <Xm/AtomMgr.h>
57 #include <Xm/XmP.h>
58 #include <Dt/Session.h>
59 #include "calendar.h"
60 #include "session.h"
61
62 extern Calendar *calendar;
63
64 /*  Structure used on a save session to see if a dt is iconic  */
65 typedef struct {
66         int     state;
67         Window  icon;
68 } WM_STATE;
69
70 /***************************************************************************
71  *                                                                         *
72  * Routine:   CMSaveSessionCB                                              *
73  *                                                                         *
74  * Purpose:   save state information for session management                *
75  **************************************************************************/
76 void
77 CMSaveSessionCB (
78         Widget           w,
79         XtPointer        clientData,
80         XtPointer        callbackArg)
81 {
82         char            *path, 
83                         *command,
84                         *name;
85         FILE            *fp;
86         int              n, 
87                          actualFormat,
88                          command_len,
89                          save_session = True;
90         Position         x, y;
91         Dimension        width, height;
92         WM_STATE        *wmState;
93         Atom wmStateAtom, actualType;
94         unsigned long    nitems, 
95                          leftover;
96         Atom             command_atom;
97
98         if (!DtSessionSavePath(calendar->frame, &path, &name))
99                 save_session = False;
100
101         if (save_session) {
102
103                 /*  Create the session file  */
104                 if (!(fp = fopen(path, "w+"))) {
105                         printf("fopen of %s failed. %p \n", path, fp);
106                         XtFree ((char *)path);
107                         XtFree ((char *)name);
108                         return;
109                 }
110
111                 chmod(path, S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP);
112
113                 /* The initial set up is done on the file.  From here on 
114                    out the application should write out a set of resource 
115                    definitions that will later be read in.  These resource 
116                    definitions will be used to restore the syate of the tool 
117                    when the session is restarted. */
118
119                 /*  Getting the WM_STATE property to see if iconified or not */
120                 wmStateAtom = XmInternAtom(XtDisplay(calendar->frame), 
121                                                         "WM_STATE", False);
122
123                 XGetWindowProperty(XtDisplay(calendar->frame), 
124                         XtWindow(calendar->frame), 
125                         wmStateAtom, 0L, (long)BUFSIZ, False, 
126                         wmStateAtom, &actualType, &actualFormat, 
127                         &nitems, &leftover, (unsigned char **) &wmState);
128
129                 if (wmState->state == IconicState)
130                         fprintf(fp, "*iconic: True\n");
131                 else
132                         fprintf(fp, "*iconic: False\n");
133
134                 /*** Get and write out the geometry info for our Window ***/
135                 x = XtX(calendar->frame);
136                 y = XtY(calendar->frame);
137                 width = XtWidth(calendar->frame);
138                 height = XtHeight(calendar->frame);
139
140                 fprintf(fp, "*x: %d\n", x);
141                 fprintf(fp, "*y: %d\n", y);
142                 fprintf(fp, "*width: %d\n", width);
143                 fprintf(fp, "*height: %d\n", height);
144
145                 if (calendar->view->glance == dayGlance)
146                         fprintf (fp, "*defaultView: day\n");
147                 else if (calendar->view->glance == weekGlance)
148                         fprintf (fp, "*defaultView: week\n");
149                 else if (calendar->view->glance == monthGlance)
150                         fprintf (fp, "*defaultView: month\n");
151                 else
152                         fprintf (fp, "*defaultView: year\n");
153
154                 /* This is the end of client resource writing.  At this point 
155                    the client will reset the WM_COMMAND property on it's top 
156                    level window to include the original starting parameters as 
157                    well as the -session flag and the file to restore with. */
158         }
159
160         command_atom = XA_WM_COMMAND;
161                 /* Generate the reinvoking command and add it as the 
162                    property value */
163
164         if (save_session)
165                 command_len = calendar->view->wm_cmdstrlen + 
166                                         strlen("-session") + strlen(name) + 2;
167         else
168                 command_len = calendar->view->wm_cmdstrlen + 2; 
169                                         
170         command = XtMalloc (sizeof (char) * command_len);
171
172         memcpy(command, calendar->view->wm_cmdstr, 
173                                                 calendar->view->wm_cmdstrlen);
174         command_len = calendar->view->wm_cmdstrlen;
175
176         if (save_session) {
177                 memcpy(command + command_len, "-session", 8);
178                 command_len += 8;
179                 command[command_len] = '\0';
180                 command_len++;
181                 memcpy(command + command_len, name, strlen(name));
182                 command_len += strlen(name);
183         }
184         
185         XChangeProperty(XtDisplay(calendar->frame), XtWindow(calendar->frame), 
186                 command_atom, XA_STRING, 8, PropModeReplace, 
187                 (unsigned char *)command,
188                 command_len);
189
190         free(command);
191
192         /* Note the bogus use of XtFree here.  DtSessionSavePath requires that
193            the returned strings be freed this way.  Yick. */
194
195         if (save_session) {
196                 XtFree((char *)path);
197                 XtFree((char *)name);
198                 fclose(fp);
199         }
200 }
201
202 /***************************************************************************
203  *                                                                         *
204  * Routine:   GetSessionInfo                                               *
205  *                                                                         *
206  * Purpose:   get dticon session information                              *
207  **************************************************************************/
208
209 void
210 GetSessionInfo(
211         Calendar        *c)
212 {
213     XrmDatabase          db;
214     char                *path;
215     XrmName              xrm_name[5];
216     XrmRepresentation    rep_type;
217     XrmValue             value;
218
219     if (c->app_data->session_file == NULL)
220         return;
221
222     /***  Open the resource database file ***/
223
224     if (DtSessionRestorePath(c->frame, &path, c->app_data->session_file) 
225                                                                     == False) {
226         /* XXX: Should generate error about not being able to restore session.*/
227         return;
228     }
229
230     if ((c->view->sessiondb = XrmGetFileDatabase (path)) == NULL) {
231         if (path) XtFree(path);
232         return;
233     }
234
235     if (path) XtFree(path);
236
237     xrm_name[1] = '\0';
238  
239     /* get default view */
240     xrm_name[0] = XrmStringToQuark ("defaultView");
241     if (XrmQGetResource(c->view->sessiondb, xrm_name, xrm_name, 
242                         &rep_type, &value)) {
243         if (value.addr)
244                 c->app_data->default_view = strdup(value.addr);
245     }
246 }