Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / programs / dtstyle / SaveRestore.c
1 /*$XConsortium: SaveRestore.c /main/5 1996/03/25 00:53:19 pascale $ */
2 /************************************<+>*************************************
3  ****************************************************************************
4  **
5  **   File:        SaveRestore.c
6  **
7  **   Project:     DT 3.0
8  **
9  **   Description: Controls the Dtstyle Save/Restore functionality
10  **
11  **
12  ****************************************************************************
13  ************************************<+>*************************************/
14 /*
15  * (c) Copyright 1996 Digital Equipment Corporation.
16  * (c) Copyright 1990, 1996 Hewlett-Packard Company.
17  * (c) Copyright 1996 International Business Machines Corp.
18  * (c) Copyright 1996 Sun Microsystems, Inc.
19  * (c) Copyright 1996 Novell, Inc. 
20  * (c) Copyright 1996 FUJITSU LIMITED.
21  * (c) Copyright 1996 Hitachi.
22  */
23
24 /*+++++++++++++++++++++++++++++++++++++++*/
25 /* include files                         */
26 /*+++++++++++++++++++++++++++++++++++++++*/
27
28 #include <stdio.h>
29
30 #include <X11/Xlib.h>
31
32 #include <Xm/Xm.h>
33 #include <Xm/XmP.h>
34 #include <Xm/Form.h>
35 #include <Xm/LabelG.h>
36 #include <Xm/PushBG.h>
37 #include <Xm/Scale.h>
38
39 #include <Dt/DialogBox.h>
40
41 #include <Dt/HourGlass.h>
42 #include <Dt/UserMsg.h>
43
44 #include "Main.h"
45
46 /*+++++++++++++++++++++++++++++++++++++++*/
47 /* include extern functions              */
48 /*+++++++++++++++++++++++++++++++++++++++*/
49 #include "SaveRestore.h"
50
51 /*+++++++++++++++++++++++++++++++++++++++*/
52 /* Local #defines                        */
53 /*+++++++++++++++++++++++++++++++++++++++*/
54 #define MSG1  ((char *)GETMESSAGE(10, 1, "Check file permissions.")) 
55 #define MSG2  ((char *)GETMESSAGE(10, 3, "%s is the file that would have been used to save your session\n"))
56
57
58 /************************************************************************
59  *
60  *  saveSessionCB
61  *      Creates a file as a resource data base, and writes out all
62  *  info needed to save our current state.  This info will be used
63  *  later by restoreSession to start up the help system in the exact
64  *  state in which we saved the session.
65  *
66  ************************************************************************/
67 void 
68 saveSessionCB(
69         Widget w,
70         XtPointer client_data,
71         XtPointer call_data )
72 {
73   char *longpath, *name;
74   int fd, n; 
75   char *xa_CommandStr[3];
76   unsigned char *data = NULL;
77   char *tmpStr;
78
79 #ifdef _SUN_OS
80   char *noCommands = NULL;
81 #endif /*  _SUN_OS */
82
83   /* get the root window property of SaveMode */
84   GetSessionSaveMode(&data );
85   
86   /* if the property is "home" don't save dtstyle */
87   if(strcmp((char *)data, "home") == 0)
88   {
89 #ifdef _SUN_OS   /*  Sun doesn't like "NULL" when expecting "char **" */
90      XSetCommand(style.display, XtWindow(w), &noCommands, 0);
91 #else
92      XSetCommand(style.display, XtWindow(w), NULL, 0);
93 #endif /* _SUN_OS */
94      return;
95   }
96
97   /* Xt may not pass a widget as advertised */
98   if(!XtIsShell(w))
99     DtSessionSavePath(XtParent(w), &longpath, &name);
100   else
101     DtSessionSavePath(w, &longpath, &name);
102
103   /*  Create the session file  */
104   if ((fd = creat (longpath, S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP)) == -1)
105     {
106       tmpStr = (char *)XtMalloc(strlen(MSG2) + strlen(longpath)+ 1);
107       sprintf(tmpStr, MSG2, longpath);
108       _DtSimpleErrnoError(progName,DtError, MSG1, tmpStr, NULL);   
109       XtFree(tmpStr);
110       XtFree ((char *)longpath);
111       XtFree ((char *)name);
112       return;
113     }
114
115   /*write out each needed resource*/
116   saveMain(fd);
117   saveColor(fd);
118   saveColorEdit(fd);
119   saveFonts(fd);
120   saveBackdrop(fd);
121   saveKeybd(fd);
122   saveMouse(fd);
123   saveAudio(fd);
124   saveScreen(fd);
125   saveDtwm(fd);
126   saveStartup(fd);
127   saveI18n(fd);
128   
129   close (fd);
130
131 /*If this is a session manager requested saveYrSlf, need to put dialogs in*/
132 /*recoverable state, and make them consistent with server (etc) state.*/
133 /*Have to do it before exit so SM gets corrected server state.*/
134   callCancels();
135
136   n = 0;
137   xa_CommandStr[n] = style.execName; n++;
138   xa_CommandStr[n] =  "-session"; n++;
139   xa_CommandStr[n] = name; n++;
140
141   XSetCommand(style.display, XtWindow(w), xa_CommandStr, n);
142   XtFree ((char *)longpath);
143   XtFree ((char *)name);
144
145 /*  Don't exit yet, SM needs time to get the new commandStr.*/
146 }
147
148
149 /************************************************************************
150  *
151  *  RestoreSession
152  *      Open the file as a resource data base, and use the data to
153  *      set the dialog position and (optionally) size.
154  *
155  ************************************************************************/
156
157 Boolean 
158 restoreSession(
159         Widget shell,
160         char *name )
161 {
162   XrmDatabase db;
163   char *tmpStr;
164   char *longpath;
165   Boolean status;
166
167   status = DtSessionRestorePath(shell, &longpath, name);
168   if (!status)
169       return(1);
170   
171   /*  Open the file as a resource database */
172   if ((db = XrmGetFileDatabase (longpath)) == NULL) {
173     tmpStr = (char *)XtMalloc(strlen(MSG2) + strlen(longpath)+ 1);
174     sprintf(tmpStr, MSG2, longpath);
175     _DtSimpleErrnoError(progName, DtError, MSG1, tmpStr, NULL);   
176     XtFree(tmpStr);
177     XtFree(longpath);
178     return(1);
179   }
180
181   restoreMain(shell, db);   /* Must happen first */
182
183   /* Now, Restore the dialogs */
184   restoreColor(shell, db);
185   restoreColorEdit(shell, db);
186   restoreFonts(shell, db);
187   restoreBackdrop(shell, db);
188   restoreKeybd(shell, db);
189   restoreMouse(shell, db);
190   restoreAudio(shell, db);
191   restoreScreen(shell, db);
192   restoreDtwm(shell, db);
193   restoreStartup(shell, db);
194   restoreI18n(shell, db);
195
196   XrmDestroyDatabase(db);
197
198   XtFree(longpath);
199   return(0);
200 }