dtcm: Resolve CID 87713
[oweals/cde.git] / cde / programs / dtdbcache / Main.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: Main.c /main/5 1999/09/20 14:08:17 mgreess $ */
24 /*
25 cc -g -o Main Main.c -I/usr/dt/include -I/usr/openwin/include -R/export2/build/SunOS/lib1/DtSvc:/usr/dt/lib:/usr/openwin/lib -L/export2/build/SunOS/lib1/DtSvc -L/usr/dt/lib -L/usr/openwin/lib -lDtSvc -lXt
26  */
27 #include <locale.h>
28 #include <Tt/tt_c.h>
29 #include <X11/Intrinsic.h>
30 #include <Xm/MessageB.h>
31 #include <Xm/Form.h>
32 #include <Xm/Text.h>
33 #include <Dt/Dts.h>
34 #include <Dt/UserMsg.h>
35 #include <Dt/EnvControlP.h>
36
37 static void DbCacheCreateError(Widget toplevel);
38 static void DbCacheCreateErrorCallback(Widget, XtPointer, XtPointer);
39 static void DieFromToolTalkError(char*, Tt_status);
40
41 #define ApplicationClass "dtdtscache"
42 #define RebuildMsg       "DtDtsCache"
43
44 int             build_db = 1;
45
46 int
47 main(int argc, char **argv)
48 {
49         XtAppContext    appContext;
50         Widget          toplevel, form;
51         char           *tmp;
52         int             c;
53         int             more = 1;
54         Tt_message      msg;
55         Tt_status       status;
56         int             count = 0;
57         int             slp = 0;
58         int             init_flag;
59         int             i;
60         char            *locale = setlocale(LC_ALL, "");
61
62
63         if(!locale)
64         {
65                 perror("setlocale");
66         }
67         _DtEnvControl(DT_ENV_SET);
68
69         if(strcmp(argv[1], "-init") == 0)
70         {
71                 init_flag = 1;
72         }
73         else
74         {
75                 init_flag = 0;
76         }
77
78         if(!init_flag)
79         {
80                 status = tt_ptr_error(tt_open());
81                 DieFromToolTalkError(
82                         "Could not connect to ToolTalk:\n%s\nExiting",
83                         status);
84         }
85         if(!init_flag)
86         {
87                 status = tt_ptype_declare("SDTDTSCACHE");
88                 DieFromToolTalkError(
89                         "Could not declare SDTDTSCACHE ptype:\n%s\nExiting",
90                         status);
91         }
92
93         toplevel = XtAppInitialize(&appContext, ApplicationClass, NULL, 0,
94                                    &argc, argv, NULL, NULL, 0);
95
96         if (DtAppInitialize(appContext, XtDisplay(toplevel), toplevel, argv[0],
97                             ApplicationClass) == False)
98         {
99                 _DtSimpleError(ApplicationClass,
100                                 DtFatalError,
101                                 NULL ,
102                                 "%s: Couldn't initialize Dt\n",
103                                 argv[0]);
104         }
105
106         while(!init_flag && more)
107         {
108                 msg = tt_message_receive();
109                 if(msg != 0)
110                 {
111                         char    *op = tt_message_op(msg);
112
113                         if(strcmp(op, RebuildMsg) != 0)
114                         {
115                                 tt_message_reject(msg);
116                         }
117                         else
118                         {
119                                 Tt_message m;
120
121                                 if (!_DtDtsMMInit(1))
122                                 {
123                                     tt_message_reply(msg);
124                                     DbCacheCreateError(toplevel);
125                                     /* NOTREACHED */
126                                 }
127
128                                 count = 0;
129                                 slp = 0;
130                                 tt_message_reply(msg);
131 /*
132                                 m = tt_message_create();
133                                 tt_message_scope_set(m, TT_SESSION);
134                                 tt_message_class_set(m, TT_NOTICE);
135                                 tt_message_op_set(m, "DtTypes_Reloaded");
136                                 tt_message_address_set(m, TT_PROCEDURE);
137                                 tt_message_send(m);
138 */
139                         }
140                 }
141                 else
142                 {
143                         count++;
144                 }
145                 if(count >= 10)
146                 {
147                         if(slp)
148                         {
149                                 more = 0;
150                                 continue;
151                         }
152                         sleep(30);
153                         slp = 1;
154                 }
155         }
156
157         if(init_flag)
158         {
159             if (!_DtDtsMMInit(1))
160             {
161                 /*
162                  * Log an error message.  Cannot post a dialog because
163                  * there is a good chance system is starting up.
164                  */
165                 _DtSimpleError(ApplicationClass, DtFatalError, NULL,
166                   "Couldn't create the Desktop Action/DataTypes Database.\n\
167 Check disk space and/or permissions.\n");
168             }
169         }
170         exit(0);
171 }
172
173 static void
174 DbCacheCreateErrorCallback(Widget w, XtPointer userData, XtPointer callData)
175 {
176     XtUnmanageChild(w);
177     XFlush(XtDisplay(w));
178     XmUpdateDisplay(w);
179     _DtSimpleError(ApplicationClass, DtFatalError, NULL,
180                    "Couldn't recreate the Desktop Action/DataTypes Database.\n\
181 Check disk space and/or permissions.\n");
182 }
183
184 /*
185  * This function posts an error dialog informing the user that creation
186  * of the Actions/DataTypes Database failed.
187  * Wait for the user to acknowledge the error, log a message, and then exit.
188  */
189 static void 
190 DbCacheCreateError(Widget toplevel)
191 {
192     Widget err;
193     int n;
194     Arg args[10];
195     XmString okLabel;
196     XmString message;
197     char * messagein;
198     char * title;
199     Dimension dialogWd, dialogHt;
200
201     title = "dtdbcache failure";
202     messagein = "Couldn't recreate the Desktop Actions/DataTypes Database.\n\
203 Check disk space and/or permissions.";
204     message = XmStringCreateLocalized(messagein);
205     okLabel = XmStringCreateLocalized("OK");
206
207     /* Post an error dialog */
208     n = 0;
209     XtSetArg(args[n], XmNtitle, title); n++;
210     XtSetArg(args[n], XmNmessageString, message); n++;
211     XtSetArg(args[n], XmNokLabelString, okLabel); n++;
212     XtSetArg(args[n], XmNdefaultPosition, False);  n++;
213     XtSetArg(args[n], XmNmappedWhenManaged, False);  n++;
214     err = XmCreateErrorDialog(toplevel, "err", args, n);
215
216     XmStringFree(okLabel);
217     XmStringFree(message);
218
219     XtUnmanageChild(XmMessageBoxGetChild(err, XmDIALOG_CANCEL_BUTTON));
220     XtUnmanageChild(XmMessageBoxGetChild(err, XmDIALOG_HELP_BUTTON));
221
222     /* Center the dialog */
223     XtRealizeWidget(err);
224     XtVaGetValues(err, XmNwidth, &dialogWd, XmNheight, &dialogHt, NULL);
225     XtSetArg(args[0], XmNx,
226              (WidthOfScreen(XtScreen(err)) - dialogWd) / 2);
227     XtSetArg(args[1], XmNy,
228              (HeightOfScreen(XtScreen(err)) - dialogHt) / 2);
229     XtSetValues (err, args, 2);
230
231     XtManageChild(err);
232     XtSetMappedWhenManaged(err, True);
233     XtAddCallback(err, XmNokCallback, DbCacheCreateErrorCallback,
234                   (XtPointer)NULL);
235
236     XtAppMainLoop(XtWidgetToApplicationContext(err));
237 }
238
239 static void
240 DieFromToolTalkError(char *errfmt, Tt_status status)
241 {
242     char        *statmsg;
243
244     if (! tt_is_err(status)) return;
245
246     statmsg = tt_status_message(status);
247     _DtSimpleError(ApplicationClass, DtFatalError, NULL, errfmt, statmsg);
248     exit(status);
249 }