dtcm: Resolve CID 87562
[oweals/cde.git] / cde / programs / dtcm / dtcm / timezone.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: timezone.c /main/10 1999/07/01 16:36:43 mgreess $ */
24 /*
25  *  (c) Copyright 1993, 1994 Hewlett-Packard Company
26  *  (c) Copyright 1993, 1994 International Business Machines Corp.
27  *  (c) Copyright 1993, 1994 Novell, Inc.
28  *  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
29  */
30
31 #include <EUSCompat.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <csa.h>
35 #include <Xm/Xm.h>
36 #include <Xm/Form.h>
37 #include <Xm/LabelG.h>
38 #include <Xm/Label.h>
39 #include <Xm/List.h>
40 #include <Xm/DialogS.h>
41 #include <Xm/PushB.h>
42 #include <Xm/RowColumn.h>
43 #include <Xm/Scale.h>
44 #include <Xm/SeparatoG.h>
45 #include <Xm/Text.h>
46 #include <Xm/ToggleBG.h>
47 #include <Xm/MwmUtil.h>
48 #include <Xm/ComboBox.h>
49 #include <Dt/HourGlass.h>
50 #include "calendar.h"
51 #include "util.h"
52 #include "timeops.h"
53 #include "timezone.h"
54 #include "format.h"
55 #include "datefield.h"
56 #include "props.h"
57 #include "editor.h"
58 #include "todo.h"
59 #include "group_editor.h"
60 #include "select.h"
61 #include "help.h"
62
63 extern caddr_t
64 make_timezone(c)
65 Calendar *c;
66 {
67         Timezone *t;
68         Widget text, separator, button_form;
69         XmString xmstr;
70         int ac;
71         Arg args[10];
72         char buf[BUFSIZ], tmp_buf[BUFSIZ];
73         char *title;
74         void tz_customtime_cb(), tz_mytime_cb(), 
75                         tz_apply_cb(), tz_cancel_cb(), tz_ok_cb();
76
77         if (c->timezone == NULL) {
78                 c->timezone = (caddr_t)ckalloc(sizeof(Timezone));
79                 t = (Timezone*)c->timezone;
80         }
81         else
82                 t = (Timezone*)c->timezone;
83
84         title = XtNewString(catgets(c->DT_catd, 1, 649, "Calendar : Time Zone"));
85         t->frame = XtVaCreatePopupShell("frame",
86                 xmDialogShellWidgetClass, c->frame,
87                 XmNtitle,               title,
88                 XmNallowShellResize,    False,
89                 XmNmwmFunctions,        MWM_FUNC_CLOSE | MWM_FUNC_MOVE,
90                 XmNmappedWhenManaged,   False,
91                 NULL);
92         XtFree(title);
93
94         t->form = XtVaCreateWidget("timezone_form",
95                 xmFormWidgetClass,      t->frame,
96                 XmNautoUnmanage,        False,
97                 XmNhorizontalSpacing,   5,
98                 XmNverticalSpacing,     5,
99                 XmNmarginWidth,         0,
100                 XmNmarginHeight,        0,
101                 XmNfractionBase,        4,
102                 NULL);
103
104         t->timezone_rc_mgr = XtVaCreateWidget("search_rc_mgr",
105                 xmRowColumnWidgetClass, t->form,
106                 XmNpacking,             XmPACK_COLUMN,
107                 XmNorientation,         XmVERTICAL,
108                 XmNradioBehavior,       True,
109                 XmNisHomogeneous,       True,
110                 XmNentryClass,          xmToggleButtonGadgetClass,
111                 XmNleftAttachment,      XmATTACH_FORM,
112                 XmNleftOffset,          5,
113                 XmNtopAttachment,       XmATTACH_FORM,
114                 XmNtopOffset,           5,
115                 NULL);
116  
117         xmstr = XmStringCreateLocalized(catgets(c->DT_catd, 1, 650, "My Time"));
118         t->mytime = XtVaCreateWidget("myTime",
119                 xmToggleButtonGadgetClass, t->timezone_rc_mgr,
120                 XmNlabelString, xmstr,
121                 XmNset, True,
122                 XmNuserData, t,
123                 NULL);
124         XmStringFree(xmstr);
125         XtAddCallback(t->mytime, XmNvalueChangedCallback, tz_mytime_cb, NULL);
126  
127         xmstr = XmStringCreateLocalized(catgets(c->DT_catd, 1, 652, "Custom Time"));
128         t->customtime = XtVaCreateWidget("customTime",
129                 xmToggleButtonGadgetClass, t->timezone_rc_mgr,
130                 XmNlabelString, xmstr,
131                 XmNuserData, t,
132                 NULL);
133         XmStringFree(xmstr);
134         XtAddCallback(t->customtime, XmNvalueChangedCallback, tz_customtime_cb, NULL);
135
136         xmstr = XmStringCreateLocalized(catgets(c->DT_catd, 1, 653, "GMT"));
137         t->gmtlabel = XtVaCreateWidget("gmtLabel", xmLabelWidgetClass, t->form,
138                 XmNlabelString, xmstr,
139                 XmNleftAttachment, XmATTACH_WIDGET,
140                 XmNleftWidget, t->customtime,
141                 XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
142                 XmNbottomWidget, t->timezone_rc_mgr,
143                 XmNbottomOffset, 7,
144                 NULL);
145         XmStringFree(xmstr);
146         XtSetSensitive(t->gmtlabel, False);
147
148         ac = 0;  
149         XtSetArg(args[ac], XmNcomboBoxType, XmDROP_DOWN_COMBO_BOX); ++ac;
150         XtSetArg(args[ac], XmNleftAttachment, XmATTACH_WIDGET); ++ac;
151         XtSetArg(args[ac], XmNleftWidget, t->gmtlabel); ++ac;
152         XtSetArg(args[ac], XmNleftOffset, 8); ++ac;
153         XtSetArg(args[ac], XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET); ++ac;
154         XtSetArg(args[ac], XmNbottomOffset, 5); ++ac;
155         XtSetArg(args[ac], XmNbottomWidget, t->timezone_rc_mgr); ++ac;
156         XtSetArg(args[ac], XmNpositionMode, XmONE_BASED); ++ac;
157         t->gmtcombo = XmCreateComboBox(t->form, "gmtcombo", args, ac);
158
159         XtVaSetValues(t->gmtcombo, XmNwidth, 75, NULL);
160         XtSetSensitive(t->gmtcombo, False);
161
162         for(ac = -12; ac <= 12; ac += 1) {
163                 if (ac >= 0)
164                         sprintf(buf, "+%d", ac);  
165                 else
166                         sprintf(buf, "%d", ac);  
167                 xmstr = XmStringCreateLocalized(buf);
168                 XmComboBoxAddItem(t->gmtcombo, xmstr, 0, False);
169                 XmStringFree(xmstr);
170         }
171         /* Remind: remove this kludge after timezone attribute is available */
172         /* Remind: don't do this because of daylight savings time */
173         /*
174         XtVaGetValues(t->gmtcombo, XmNtextField, &text, NULL);
175         XmTextSetString(text, "-6");
176         */
177
178         xmstr = XmStringCreateLocalized(catgets(c->DT_catd, 1, 654, "Hours"));
179         t->hourlabel = XtVaCreateWidget("gmtHours", xmLabelWidgetClass, t->form,
180                 XmNlabelString, xmstr,
181                 XmNleftAttachment, XmATTACH_WIDGET,
182                 XmNleftWidget, t->gmtcombo,
183                 XmNrightAttachment, XmATTACH_FORM,
184                 XmNrightOffset, 10,
185                 XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
186                 XmNbottomWidget, t->timezone_rc_mgr,
187                 XmNbottomOffset, 7,
188                 NULL);
189         XmStringFree(xmstr);
190         XtSetSensitive(t->hourlabel, False);
191
192         separator = XtVaCreateWidget("separator",
193                 xmSeparatorGadgetClass,
194                 t->form,
195                 XmNleftAttachment,      XmATTACH_FORM,
196                 XmNrightAttachment,     XmATTACH_FORM,
197                 XmNtopAttachment,       XmATTACH_WIDGET,
198                 XmNtopWidget,           t->timezone_rc_mgr,
199                 XmNtopOffset,           1,
200                 NULL);
201
202         button_form = XtVaCreateWidget("button_form",
203                 xmFormWidgetClass,      t->form,
204                 XmNautoUnmanage,        False,
205                 XmNleftAttachment,      XmATTACH_FORM,
206                 XmNleftOffset,          5,
207                 XmNrightAttachment,     XmATTACH_FORM,
208                 XmNrightOffset,         5,
209                 XmNtopAttachment,       XmATTACH_WIDGET,
210                 XmNtopWidget,           separator,
211                 XmNhorizontalSpacing,   3,
212                 XmNfractionBase,        4,
213                 NULL);
214
215         xmstr = XmStringCreateLocalized(catgets(c->DT_catd, 1, 655, "OK"));
216        t->okbutton = XtVaCreateWidget("OK", 
217                 xmPushButtonWidgetClass, button_form,
218                 XmNlabelString,         xmstr,
219                 XmNtopAttachment,       XmATTACH_WIDGET,
220                 XmNtopWidget,           separator,
221                 XmNtopOffset,           8,
222                 XmNleftAttachment,      XmATTACH_POSITION,
223                 XmNleftPosition,        0,
224                 XmNrightAttachment,     XmATTACH_POSITION,
225                 XmNrightPosition,       1,
226                 XmNnavigationType,      XmTAB_GROUP,
227                 XmNuserData,            c,
228                 NULL);
229         XmStringFree(xmstr);
230         XtAddCallback(t->okbutton, XmNactivateCallback, tz_ok_cb, NULL);
231
232         xmstr = XmStringCreateLocalized(catgets(c->DT_catd, 1, 460, "Apply"));
233        t->applybutton = XtVaCreateWidget("Apply", 
234                 xmPushButtonWidgetClass, button_form,
235                 XmNlabelString,         xmstr,
236                 XmNtopAttachment,       XmATTACH_WIDGET,
237                 XmNtopWidget,           separator,
238                 XmNtopOffset,           8,
239                 XmNleftAttachment,      XmATTACH_POSITION,
240                 XmNleftPosition,        1,
241                 XmNrightAttachment,     XmATTACH_POSITION,
242                 XmNrightPosition,       2,
243                 XmNnavigationType,      XmTAB_GROUP,
244                 XmNuserData,            c,
245                 NULL);
246         XmStringFree(xmstr);
247         XtAddCallback(t->applybutton, XmNactivateCallback, tz_apply_cb, NULL);
248
249         xmstr = XmStringCreateLocalized(catgets(c->DT_catd, 1, 923, "Cancel"));
250         t->cancelbutton = XtVaCreateWidget("Cancel", 
251                 xmPushButtonWidgetClass, button_form,
252                 XmNlabelString,         xmstr,
253                 XmNtopAttachment,       XmATTACH_WIDGET,
254                 XmNtopWidget,           separator,
255                 XmNtopOffset,           8,
256                 XmNleftAttachment,      XmATTACH_POSITION,
257                 XmNleftPosition,        2,
258                 XmNrightAttachment,     XmATTACH_POSITION,
259                 XmNrightPosition,       3,
260                 XmNnavigationType,      XmTAB_GROUP,
261                 XmNuserData,            c,
262                 NULL);
263         XmStringFree(xmstr);
264         XtAddCallback(t->cancelbutton, XmNactivateCallback, tz_cancel_cb, NULL);
265
266         xmstr = XmStringCreateLocalized(catgets(c->DT_catd, 1, 77, "Help"));
267         t->helpbutton = XtVaCreateWidget("Help", 
268                 xmPushButtonWidgetClass, button_form,
269                 XmNlabelString,         xmstr,
270                 XmNtopAttachment,       XmATTACH_WIDGET,
271                 XmNtopWidget,           separator,
272                 XmNtopOffset,           8,
273                 XmNleftAttachment,      XmATTACH_POSITION,
274                 XmNleftPosition,        3,
275                 XmNrightAttachment,     XmATTACH_POSITION,
276                 XmNrightPosition,       4,
277                 XmNnavigationType,      XmTAB_GROUP,
278                 NULL);
279         XmStringFree(xmstr);
280         XtAddCallback(t->helpbutton, XmNactivateCallback,
281                                 (XtCallbackProc)help_cb, TIMEZONE_HELP_BUTTON);
282
283
284         t->timezone_message = XtVaCreateWidget("message",
285                 xmLabelGadgetClass, t->form,
286                 XmNalignment, XmALIGNMENT_BEGINNING,
287                 XmNtopAttachment, XmATTACH_WIDGET,
288                 XmNtopWidget, t->helpbutton,
289                 XmNtopOffset, 8,
290                 XmNleftAttachment, XmATTACH_FORM,
291                 XmNrightAttachment, XmATTACH_FORM,
292                 XmNbottomAttachment, XmATTACH_FORM,
293                 NULL);
294
295         /* set timezone */
296         /* Remind - get this from the structure after it's available */
297         cm_strcpy(t->gmttimezone, "");
298         cm_strcpy(t->mytimezone, "US/Pacific");
299         cm_strcpy(tmp_buf, (char*)getenv("TZ"));
300         if (tmp_buf)
301                 cm_strcpy(t->mytimezone, tmp_buf);
302
303         sprintf(buf, "%s %s", catgets(c->DT_catd, 1, 659, "Time Zone:"),
304                 t->mytimezone);
305         set_message(t->timezone_message, buf);
306         set_message(c->message_text, buf);
307
308         XtVaSetValues(t->form, 
309                         XmNdefaultButton,       t->applybutton,
310                         XmNcancelButton,        t->cancelbutton,
311                         NULL);
312         XtVaSetValues(button_form, 
313                         XmNdefaultButton,       t->applybutton,
314                         XmNcancelButton,        t->cancelbutton,
315                         NULL);
316         ManageChildren(t->timezone_rc_mgr);
317         ManageChildren(button_form);
318         ManageChildren(t->form);
319         XtManageChild(t->form);
320         XtVaSetValues(t->frame, XmNmappedWhenManaged, True, NULL);
321         XtRealizeWidget(t->frame);
322
323         XtPopup(t->frame, XtGrabNone);
324
325         return(caddr_t)t;
326 }
327
328 extern void
329 refresh_timezone(t)
330 Timezone *t;
331 {
332     XmToggleButtonSetState(t->mytime, t->timezone_type == mytime, FALSE);
333     XmToggleButtonSetState(t->customtime, t->timezone_type == customtime,FALSE);
334     XtSetSensitive(t->gmtlabel, t->timezone_type == customtime);
335     XtSetSensitive(t->gmtcombo, t->timezone_type == customtime);
336     XtSetSensitive(t->hourlabel, t->timezone_type == customtime);
337
338     if (t->timezone_type == mytime)
339     {
340         Widget text;
341         XtVaGetValues(t->gmtcombo, XmNtextField, &text, NULL);
342         XmTextSetString(text, "");
343     }
344 }
345
346 void
347 tz_mytime_cb(Widget widget, XtPointer client_data, XtPointer call_data)
348 {
349         Timezone *t;
350         XmToggleButtonCallbackStruct *state =
351                 (XmToggleButtonCallbackStruct *) call_data;
352
353         XtVaGetValues(widget, XmNuserData, &t, NULL);
354
355         if (state->set)
356                 t->edit_timezone_type = mytime;
357         XtSetSensitive(t->gmtlabel, !state->set);
358         XtSetSensitive(t->gmtcombo, !state->set);
359         XtSetSensitive(t->hourlabel, !state->set);
360 }
361
362 void
363 tz_customtime_cb(Widget widget, XtPointer client_data, XtPointer call_data)
364 {
365         Timezone *t;
366         XmToggleButtonCallbackStruct *state =
367                 (XmToggleButtonCallbackStruct *) call_data;
368
369         XtVaGetValues(widget, XmNuserData, &t, NULL);
370
371         if (state->set)
372                 t->edit_timezone_type = customtime;
373         XtSetSensitive(t->gmtlabel, state->set);
374         XtSetSensitive(t->gmtcombo, state->set);
375         XtSetSensitive(t->hourlabel, state->set);
376 }
377
378 void
379 tz_set_timezone(Calendar *c, Timezone *t)
380 {
381     char *tmp_buf, buf[BUFSIZ], gmt[BUFSIZ];
382     Widget text;
383     Dtcm_calendar *cal_handle;
384     int time_diff;
385
386     t->timezone_type = t->edit_timezone_type;
387     if (t->timezone_type == mytime)
388     {
389         set_timezone(t->mytimezone);
390         sprintf(buf, "%s %s", catgets(c->DT_catd, 1, 659, "Time Zone:"),
391                 t->mytimezone);
392
393         /* get utc time */
394         /* Remind: this won't work because of daylight savings time */
395         /* Remind: add this after we can get more info from backend */
396         /*
397           cal_handle = allocate_cal_struct(
398                                         appt_read, 
399                                         c->general->version,
400                                         DtCM_ATTR_UTC_OFFSET_I, 
401                                         NULL);
402           if (query_cal_struct(c->my_cal_handle, cal_handle) == CSA_SUCCESS)
403             time_diff =
404               seconds_to_hours(cal_handle->attrs->value->item.number_value);
405           if (time_diff >= 0) sprintf(gmt, "GMT+%d", time_diff);
406           else sprintf(gmt, "GMT%d", time_diff);
407           set_timezone(gmt);
408         */
409
410     }
411     else if (t->timezone_type == customtime)
412     {
413         XtVaGetValues(t->gmtcombo, XmNtextField, &text, NULL);
414         tmp_buf = XmTextGetString(text);
415         cm_strcpy(t->gmttimezone, tmp_buf);
416         sprintf(gmt, "GMT%s", tmp_buf);
417         set_timezone(gmt);
418         sprintf(buf, "%s %s", catgets(c->DT_catd, 1, 659, "Time Zone:"), gmt);
419     }
420     set_message(t->timezone_message, buf);
421     paint_canvas(c, NULL, RENDER_CLEAR_FIRST);
422     set_message(c->message_text, buf);
423     if (editor_showing((Editor*)c->editor))
424       add_all_appt((Editor*)c->editor);
425     if (geditor_showing((GEditor*)c->geditor))
426       add_all_gappt((GEditor*)c->geditor);
427     if (todo_showing((ToDo*)c->todo))
428       add_all_todo((ToDo*)c->todo);
429 }
430
431 void
432 tz_apply_cb(Widget widget, XtPointer client_data, XtPointer call_data)
433 {
434         Calendar *c;
435         Timezone *t;
436
437         XtVaGetValues(widget, XmNuserData, &c, NULL);
438         t = (Timezone*)c->timezone;
439         tz_set_timezone(c, t);
440 }
441
442  
443 void
444 tz_ok_cb(Widget widget, XtPointer client_data, XtPointer call_data)
445 {
446         Calendar *c;
447         Timezone *t;
448
449         XtVaGetValues(widget, XmNuserData, &c, NULL);
450         t = (Timezone*)c->timezone;
451         tz_set_timezone(c, t);
452
453         XtPopdown(t->frame);
454 }
455
456 void
457 tz_cancel_cb(Widget widget, XtPointer client_data, XtPointer call_data)
458 {
459         Calendar *c;
460         Timezone *t;
461
462         XtVaGetValues(widget, XmNuserData, &c, NULL);
463         t = (Timezone*)c->timezone;
464
465         XtPopdown(t->frame);
466 }