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