1 /*----------------------------------------------------------------------------
2 --
3 --  Module:           xtmOpenView
4 --
5 --  Project:          XDiary
6 --  System:           xtm - X Desktop Calendar
7 --    Subsystem:      <>
8 --    Function block: <>
9 --
10 --  Description:
11 --    Open a single day list view.
12 --
13 --  Filename:         xtmOpenView.c
14 --
15 --  Authors:          Roger Larsson, Ulrika Bornetun
16 --  Creation date:    1991-06-15
17 --
18 --
19 --  (C) Copyright Ulrika Bornetun, Roger Larsson (1995)
20 --      All rights reserved
21 --
22 --  Permission to use, copy, modify, and distribute this software and its
23 --  documentation for any purpose and without fee is hereby granted,
24 --  provided that the above copyright notice appear in all copies. Ulrika
25 --  Bornetun and Roger Larsson make no representations about the usability
26 --  of this software for any purpose. It is provided "as is" without express
27 --  or implied warranty.
28 ----------------------------------------------------------------------------*/
29 
30 /* SCCS module identifier. */
31 static char SCCSID[] = "@(#) Module: xtmOpenView.c, Version: 1.1, Date: 95/02/18 15:52:36";
32 
33 
34 /*----------------------------------------------------------------------------
35 --  Include files
36 ----------------------------------------------------------------------------*/
37 
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <ctype.h>
42 
43 #include <X11/Intrinsic.h>
44 
45 #include <Xm/Xm.h>
46 #include <Xm/Form.h>
47 #include <Xm/List.h>
48 #include <Xm/RowColumn.h>
49 #include <Xm/Text.h>
50 
51 #include "System.h"
52 #include "LstLinked.h"
53 #include "Message.h"
54 #include "TimDate.h"
55 
56 #include "msgXdiary.h"
57 #include "xtmGlobal.h"
58 #include "xtmCalDb.h"
59 #include "xtmDbTools.h"
60 #include "xtmHelp.h"
61 #include "xtmLocate.h"
62 #include "xtmPickDiary.h"
63 #include "xtmTools.h"
64 #include "xitError.h"
65 #include "xitTools.h"
66 #include "xtmOpenView.h"
67 
68 
69 /*----------------------------------------------------------------------------
70 --  Macro definitions
71 ----------------------------------------------------------------------------*/
72 
73 /* How many calendars can we have? */
74 #define MAX_CALENDARS     200
75 
76 
77 /* Local widgets in the database select window. */
78 #define calActionRc       dataLocalW[  0 ]
79 #define calLocLa          dataLocalW[  1 ]
80 #define calLocTx          dataLocalW[  2 ]
81 #define calNameLa         dataLocalW[  3 ]
82 #define calNameTx         dataLocalW[  4 ]
83 #define dbLocLa           dataLocalW[  5 ]
84 #define dbLocLi           dataLocalW[  6 ]
85 
86 
87 /*----------------------------------------------------------------------------
88 --  Type declarations
89 ----------------------------------------------------------------------------*/
90 
91 /* Our own user data. */
92 typedef struct {
93 
94   /* The window is application modal? */
95   Boolean  appl_modal;
96 
97   /* The selected calendar. */
98   char  selected_cal[ XTM_GL_MAX_CAL_NAME + 1 ];
99 
100   /* The open view window. */
101   Widget  openW;
102 
103   /* Application wide data. */
104   XTM_GL_BASE_DATA_REF  appl_data_ref;
105 
106   /* Pick calendars. */
107   XTM_PD_HANDLE  pick_handle;
108 
109   /* Callback to inform our creator of specific actions. */
110   void              *user_data;
111   XTM_OV_ACTION_CB  actionCB;
112 
113 } OPEN_REC, *OPEN_REC_REF;
114 
115 
116 /*----------------------------------------------------------------------------
117 --  Global definitions
118 ----------------------------------------------------------------------------*/
119 
120 /* Name of module. */
121 static char  *module_name = "xtmOpenView";
122 
123 /* IDs for the help windows. */
124 static char  *open_window_id = "OpenView";
125 
126 
127 /*----------------------------------------------------------------------------
128 --  Function prototypes
129 ----------------------------------------------------------------------------*/
130 
131 static void
132   applyCB( Widget        widget,
133            OPEN_REC_REF  open_ref,
134            XtPointer     call_data );
135 
136 static void
137   calSelectedCB( Widget                widget,
138                  OPEN_REC_REF          open_ref,
139                  XmListCallbackStruct  *call_data );
140 
141 static void
142   cancelCB( Widget        widget,
143             OPEN_REC_REF  open_ref,
144             XtPointer     call_data );
145 
146 Widget
147   createOpenViewWindow( OPEN_REC_REF  open_ref,
148                         Widget        parent );
149 
150 static void
151   destroyCB( Widget        widget,
152              OPEN_REC_REF  open_ref,
153              XtPointer     call_data );
154 
155 static Boolean
156   fetchSelectedCal( OPEN_REC_REF  open_ref );
157 
158 static void
159   guessLocationCB( Widget        widget,
160                    OPEN_REC_REF  open_ref,
161                    XtPointer     call_data );
162 
163 static void
164   helpCB( Widget        widget,
165           OPEN_REC_REF  open_ref,
166           XtPointer     call_data );
167 
168 static void
169   okCB( Widget        widget,
170         OPEN_REC_REF  open_ref,
171         XtPointer     call_data );
172 
173 static void
174   pickCalCB( Widget        widget,
175              OPEN_REC_REF  open_ref,
176              XtPointer     call_data );
177 
178 static void
179   pickCalApplyCB( XTM_PD_REASON    reason,
180                   XTM_CD_CAL_INFO  *db_ref,
181                   void             *client_data );
182 
183 void
184   setCalendarList( OPEN_REC_REF  open_ref );
185 
186 
187 
188 /*----------------------------------------------------------------------------
189 --  Functions
190 ----------------------------------------------------------------------------*/
191 
192 XTM_OV_HANDLE
xtmOvInitialize(XTM_GL_BASE_DATA_REF appl_data_ref,Widget parent,Boolean appl_modal,XTM_OV_ACTION_CB actionCB,void * user_data)193   xtmOvInitialize( XTM_GL_BASE_DATA_REF  appl_data_ref,
194                    Widget                parent,
195                    Boolean               appl_modal,
196                    XTM_OV_ACTION_CB      actionCB,
197                    void                  *user_data )
198 {
199 
200   /* Variables. */
201   OPEN_REC_REF  open_ref;
202 
203 
204   /* Code. */
205 
206   /* Create and initialize our private data. */
207   open_ref = SysNew( OPEN_REC );
208   if( open_ref == NULL )
209     return( NULL );
210 
211   open_ref -> appl_data_ref     = appl_data_ref;
212   open_ref -> appl_modal        = appl_modal;
213   open_ref -> pick_handle       = NULL;
214   open_ref -> actionCB          = actionCB;
215   open_ref -> user_data         = user_data;
216   open_ref -> selected_cal[ 0 ] = '\0';
217 
218 
219   /* Create the open view window. */
220   open_ref -> openW = createOpenViewWindow( open_ref, parent );
221 
222   if( open_ref -> openW == NULL ) {
223     SysFree( open_ref );
224 
225     return( NULL );
226   }
227 
228 
229   return( (XTM_OV_HANDLE) open_ref );
230 
231 } /* xtmOvInitialize */
232 
233 
234 /*----------------------------------------------------------------------*/
235 
236 void
xtmOvDestroy(XTM_OV_HANDLE open_handle)237   xtmOvDestroy( XTM_OV_HANDLE  open_handle )
238 {
239 
240   /* Variables. */
241   OPEN_REC_REF  open_ref;
242 
243 
244   /* Code. */
245 
246   if( open_handle == NULL )
247     return;
248 
249   /* Our private data. */
250   open_ref = (OPEN_REC_REF) open_handle;
251 
252 
253   /* Destroy the window. */
254   XtDestroyWidget( open_ref -> openW );
255 
256 
257   return;
258 
259 } /* xtmOvDestroy */
260 
261 
262 /*----------------------------------------------------------------------*/
263 
264 void
xtmOvOpenView(XTM_OV_HANDLE open_handle)265   xtmOvOpenView( XTM_OV_HANDLE  open_handle )
266 {
267 
268   /* Variables. */
269   OPEN_REC_REF  open_ref;
270 
271 
272   /* Code. */
273 
274   if( open_handle == NULL )
275     return;
276 
277   /* Our private data. */
278   open_ref = (OPEN_REC_REF) open_handle;
279 
280 
281   /* Set the list of calendars we can use. */
282   setCalendarList( open_ref );
283 
284 
285   /* Make sure the window is visible. */
286   XtManageChild( open_ref -> openW );
287 
288 
289   return;
290 
291 } /* xtmOvOpenView */
292 
293 
294 /*----------------------------------------------------------------------*/
295 
296 Widget
createOpenViewWindow(OPEN_REC_REF open_ref,Widget parent)297   createOpenViewWindow( OPEN_REC_REF  open_ref,
298                         Widget        parent )
299 {
300 
301   /* Variables. */
302   int       index;
303   Arg       args[ 10 ];
304   Cardinal  n;
305   Widget    calActionBu[ 2 ];
306   Widget    openFd;
307   Widget    workFo;
308   Widget    dataLocalW[ 7 ];
309 
310   static XIT_PUSH_STRUCT cal_action_def[] = {
311     { "PickPb",  "", "", True, NULL },
312     { "GuessPb", "", "", True, NULL },
313   };
314 
315   static XIT_TEXT_STRUCT text_buffer[] = {
316     { "CalNameTx", NULL, 1, True },
317     { "CalLocTx",  NULL, 1, True },
318   };
319 
320   static XIT_ACTION_AREA_ITEM  action_buttons[] = {
321     { "", okCB,     NULL },
322     { "", applyCB,  NULL },
323     { "", cancelCB, NULL },
324     { "", helpCB,   NULL },
325   };
326 
327 
328   /* Code. */
329 
330   /* Set message strings. */
331   action_buttons[ 0 ].label = msgGetText( MXDI_OK_BUTTON );
332   action_buttons[ 0 ].data  = open_ref;
333   action_buttons[ 1 ].label = msgGetText( MXDI_APPLY_BUTTON );
334   action_buttons[ 1 ].data  = open_ref;
335   action_buttons[ 2 ].label = msgGetText( MXDI_CANCEL_BUTTON );
336   action_buttons[ 2 ].data  = open_ref;
337   action_buttons[ 3 ].label = msgGetText( MXDI_HELP_BUTTON );
338   action_buttons[ 3 ].data  = open_ref;
339 
340   cal_action_def[ 0 ].title = msgGetText( MXDI_PICK_CALENDAR );
341   cal_action_def[ 1 ].title = msgGetText( MXDI_GUESS_LOCATION_BUTTON );
342 
343 
344   /* Create a form dialog with buttons. */
345   openFd = xitCreateFormDialog( parent, "OpenFd",
346                                 1, 0,
347                                 action_buttons,
348                                 XtNumber( action_buttons ) );
349 
350   XtAddCallback( openFd, XmNdestroyCallback,
351                  (XtCallbackProc) destroyCB, (XtPointer) open_ref );
352 
353   n = 0;
354   XtSetArg( args[ n ], XmNtitle, " " ); n++;
355   XtSetValues( XtParent( openFd ), args, n );
356 
357   if( open_ref -> appl_modal ) {
358     n = 0;
359     XtSetArg( args[ n ], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL ); n++;
360     XtSetValues( openFd, args, n );
361   }
362 
363 
364   /* Container for the contents of the window. */
365   workFo = XtNameToWidget( openFd, "OpenFdFo" );
366 
367 
368   /* What should the user do? */
369   dbLocLa = xitCreateLabel( workFo, "DbLocLa",
370                             msgGetText( MXDI_SELECT_CALENDAR ), -1 );
371 
372 
373   /* List with calendars. */
374   n = 0;
375   XtSetArg( args[ n ], XmNlistSizePolicy,         XmCONSTANT ); n++;
376   XtSetArg( args[ n ], XmNscrollBarDisplayPolicy, XmSTATIC ); n++;
377   XtSetArg( args[ n ], XmNselectionPolicy,        XmSINGLE_SELECT ); n++;
378   XtSetArg( args[ n ], XmNlistMarginHeight,       5 ); n++;
379   XtSetArg( args[ n ], XmNlistMarginWidth,        5 ); n++;
380   dbLocLi = XmCreateScrolledList( workFo, "DbLocLi", args, n );
381 
382   XtAddCallback( dbLocLi, XmNsingleSelectionCallback,
383                  (XtCallbackProc) calSelectedCB, (XtPointer) open_ref );
384   XtAddCallback( dbLocLi, XmNdefaultActionCallback,
385                  (XtCallbackProc) calSelectedCB, (XtPointer) open_ref );
386 
387 
388   /* Name of calendar. */
389   calNameLa = xitCreateLabel( workFo, "CalNameLa",
390                               msgGetText( MXDI_DB_NAME_LABEL ), -1 );
391 
392   calNameTx = xitCreateTextCols( workFo,
393                                  &text_buffer[ 0 ], XTM_GL_MAX_CAL_NAME );
394 
395 
396   /* Location for the calendar. */
397   calLocLa = xitCreateLabel( workFo, "CalLocLa",
398                              msgGetText( MXDI_DB_DIR_LABEL ), -1 );
399 
400   calLocTx = xitCreateTextCols( workFo, &text_buffer[ 1 ], 35 );
401 
402   n = 0;
403   XtSetArg( args[ n ], XmNmaxLength, XTM_GL_MAX_CAL_NAME ); n++;
404   XtSetValues( calNameTx, args, n );
405 
406   n = 0;
407   XtSetArg( args[ n ], XmNmaxLength, XTM_GL_MAX_LOCATION + 1 ); n++;
408   XtSetValues( calLocTx, args, n );
409 
410 
411   /* Action (select). */
412   n = 0;
413   XtSetArg( args[ n ], XmNorientation,  XmHORIZONTAL ); n++;
414   XtSetArg( args[ n ], XmNspacing,      10 ); n++;
415   XtSetArg( args[ n ], XmNmarginHeight, 10 ); n++;
416   calActionRc = XmCreateRowColumn( workFo, "CalActionRc", args, n );
417 
418   for( index = 0; index < XtNumber( cal_action_def ); index++ )
419     calActionBu[ index ] = xitCreatePushButton( calActionRc,
420                                                 &cal_action_def[ index ] );
421 
422   XtAddCallback( calActionBu[ 0 ], XmNactivateCallback,
423                  (XtCallbackProc) pickCalCB, (XtPointer) open_ref );
424   XtAddCallback( calActionBu[ 1 ], XmNactivateCallback,
425                  (XtCallbackProc) guessLocationCB, (XtPointer)  open_ref );
426 
427 
428   /* Put the elements together. */
429   xitAttachWidget( dbLocLa,
430                    XmATTACH_FORM, NULL, XmATTACH_FORM, NULL,
431                    XmATTACH_NONE, NULL, XmATTACH_NONE, NULL );
432   xitAttachWidget( XtParent( dbLocLi ),
433                    XmATTACH_WIDGET, dbLocLa, XmATTACH_FORM, NULL,
434                    XmATTACH_NONE,   NULL,    XmATTACH_NONE, NULL );
435   xitAttachWidget( calNameLa,
436                    XmATTACH_WIDGET, XtParent( dbLocLi ),
437                    XmATTACH_FORM, NULL,
438                    XmATTACH_NONE, NULL, XmATTACH_NONE, NULL );
439   xitAttachWidget( calNameTx,
440                    XmATTACH_WIDGET, calNameLa, XmATTACH_FORM, NULL,
441                    XmATTACH_NONE,   NULL,      XmATTACH_NONE, NULL );
442   xitAttachWidget( calLocLa,
443                    XmATTACH_WIDGET, calNameTx, XmATTACH_FORM, NULL,
444                    XmATTACH_NONE,   NULL,      XmATTACH_NONE, NULL );
445   xitAttachWidget( calLocTx,
446                    XmATTACH_WIDGET, calLocLa, XmATTACH_FORM, NULL,
447                    XmATTACH_NONE,   NULL,     XmATTACH_NONE, NULL );
448   xitAttachWidget( calActionRc,
449                    XmATTACH_WIDGET, calLocTx, XmATTACH_FORM, NULL,
450                    XmATTACH_NONE,   NULL,     XmATTACH_NONE, NULL );
451 
452 
453   /* Make sure there is enough space between the children. */
454   n = 0;
455   XtSetArg( args[ n ], XmNtopOffset,    5 ); n++;
456   XtSetArg( args[ n ], XmNbottomOffset, 5 ); n++;
457   XtSetArg( args[ n ], XmNleftOffset,   5 ); n++;
458   XtSetArg( args[ n ], XmNrightOffset,  5 ); n++;
459   XtSetValues( dbLocLa,             args, n );
460   XtSetValues( XtParent( dbLocLi ), args, n );
461   XtSetValues( calNameLa,           args, n );
462   XtSetValues( calNameTx,           args, n );
463   XtSetValues( calLocLa,            args, n );
464   XtSetValues( calLocTx,            args, n );
465   XtSetValues( calActionRc,         args, n );
466 
467 
468   /* Manage the widgets. */
469   xitManageChildren( calActionBu, XtNumber( calActionBu ) );
470   xitManageChildren( dataLocalW,  XtNumber( dataLocalW ) );
471 
472   /* Set the size of the window. */
473   xitSetSizeFormDialog( openFd, True );
474 
475 
476   /* Make sure our children don't spoil our size. */
477   n = 0;
478   XtSetArg( args[ n ], XmNallowShellResize, False ); n++;
479   XtSetValues( XtParent( openFd ), args, n );
480 
481 
482   /* Make the final attachments. */
483   xitAttachWidget( calActionRc,
484                    XmATTACH_NONE, NULL, XmATTACH_FORM, NULL,
485                    XmATTACH_NONE, NULL, XmATTACH_FORM, NULL );
486   xitAttachWidget( calLocTx,
487                    XmATTACH_NONE, NULL, XmATTACH_FORM, NULL,
488                    XmATTACH_NONE, NULL, XmATTACH_WIDGET, calActionRc );
489   xitAttachWidget( calLocLa,
490                    XmATTACH_NONE, NULL, XmATTACH_FORM,   NULL,
491                    XmATTACH_NONE, NULL, XmATTACH_WIDGET, calLocTx );
492   xitAttachWidget( calNameTx,
493                    XmATTACH_NONE, NULL, XmATTACH_FORM,   NULL,
494                    XmATTACH_NONE, NULL, XmATTACH_WIDGET, calLocLa );
495   xitAttachWidget( calNameLa,
496                    XmATTACH_NONE, NULL, XmATTACH_FORM,   NULL,
497                    XmATTACH_NONE, NULL, XmATTACH_WIDGET, calNameTx );
498   xitAttachWidget( XtParent( dbLocLi ),
499                    XmATTACH_WIDGET, dbLocLa, XmATTACH_FORM,   NULL,
500                    XmATTACH_FORM,   NULL,    XmATTACH_WIDGET, calNameLa );
501 
502 
503   return( openFd );
504 
505 } /* createOpenViewWindow */
506 
507 
508 /*----------------------------------------------------------------------*/
509 
510 static Boolean
fetchSelectedCal(OPEN_REC_REF open_ref)511   fetchSelectedCal( OPEN_REC_REF  open_ref )
512 {
513 
514   /* Variables. */
515   Boolean                 ok;
516   int                     items;
517   UINT32                  operations;
518   char                    cal_name[ XTM_GL_MAX_CAL_NAME + 1 ];
519   char                    location[ XTM_GL_MAX_LOCATION + 1 ];
520   char                    *char_ref;
521   Widget                  mainW;
522   Widget                  tempW;
523   XTM_GL_CUSTOM_DATA_REF  custom_data_ref;
524   XTM_CD_CAL_INFO         db_info;
525 
526 
527   /* Code. */
528 
529   custom_data_ref = open_ref -> appl_data_ref -> custom_data;
530   mainW           = XtNameToWidget( open_ref -> openW, "OpenFdFo" );
531 
532   open_ref -> selected_cal[ 0 ] = '\0';
533 
534 
535   /* Fetch the calendar name. */
536   tempW    = XtNameToWidget( mainW, "CalNameTx" );
537   char_ref = xitStringGetText( tempW );
538 
539   items = sscanf( char_ref, "%s", cal_name );
540   SysFree( char_ref );
541 
542   if( items != 1 ) {
543     xitErMessage( open_ref -> openW, XIT_ER_INFO,
544                   module_name, "fetchSelectedCal",
545                   msgGetText( MXDI_MISSING_NAME_OR_LOC ) );
546 
547     return( False );
548   }
549 
550   /* Fetch the calendar location. */
551   tempW = XtNameToWidget( mainW, "CalLocTx" );
552   char_ref = xitStringGetText( tempW );
553 
554   location[ 0 ] = '\0';
555   items = sscanf( char_ref, "%s", location );
556   SysFree( char_ref );
557 
558 
559 
560   /* Fetch information about the calendar. */
561   ok = xtmCdFetchNamedDb( custom_data_ref -> cal_db_handle, cal_name,
562                           &db_info, NULL );
563   if( ok ) {
564     strcpy( open_ref -> selected_cal, db_info.short_name );
565 
566     return( True );
567   }
568 
569 
570   /* We did not find the calendar in our calendar DB. Any location? */
571   if( strlen( location ) == 0 ) {
572     xitErMessage( open_ref -> openW, XIT_ER_INFO,
573                   module_name, "fetchSelectedCal",
574                   msgGetText( MXDI_NO_CAL_NO_LOC ) );
575 
576     return( False );
577   }
578 
579 
580   /* We need at least read access. */
581   xtmDbCheckDbOperations( location, True, &operations );
582 
583   if( flagIsClear( operations, XTM_DB_FLAG_MODE_READ ) ) {
584     xitErMessage( open_ref -> openW, XIT_ER_ERROR,
585                   module_name, "fetchSelectedCal",
586                   msgGetText( MXDI_CUSTOM_DIR_NOT_EXIST ) );
587 
588     return( False );
589   }
590 
591 
592   /* Add the calendar to the DB and the list. */
593   xtmCdAddDatabase( custom_data_ref -> cal_db_handle,
594                     cal_name, location, 0 );
595 
596   setCalendarList( open_ref );
597   strcpy( open_ref -> selected_cal, cal_name );
598 
599 
600   return( True );
601 
602 } /* fetchSelectedCal */
603 
604 
605 /*----------------------------------------------------------------------*/
606 
607 void
setCalendarList(OPEN_REC_REF open_ref)608   setCalendarList( OPEN_REC_REF  open_ref )
609 {
610 
611   /* Variables. */
612   int                     index;
613   int                     index1;
614   UINT32                  operations;
615   char                    buffer[ 100 ];
616   char                    flags_buffer[ 20 ];
617   char                    *char_ref;
618   char                    *db_names;
619   Arg                     args[ 10 ];
620   Cardinal                n;
621   Widget                  mainW;
622   Widget                  tempW;
623   XmString                list_items[ MAX_CALENDARS ];
624   XTM_GL_CUSTOM_DATA_REF  custom_data_ref;
625 
626 
627   /* Code. */
628 
629   custom_data_ref = open_ref -> appl_data_ref -> custom_data;
630   mainW           = XtNameToWidget( open_ref -> openW, "OpenFdFo" );
631 
632 
633   /* Set values for the database locations. */
634   (void) xtmCdFetchDbNames( custom_data_ref -> cal_db_handle, &db_names );
635   char_ref = db_names;
636   index    = 0;
637 
638   do {
639 
640     int              char_read;
641     char             db_name[ XTM_GL_MAX_CAL_NAME + 1 ];
642     XTM_CD_CAL_INFO  db_info;
643 
644     while( isspace( *char_ref ) )
645       char_ref++;
646 
647     if( *char_ref == '\0' )
648       break;
649 
650     char_read = strlen( char_ref );
651     sscanf( char_ref, "%s%n", db_name, &char_read );
652     char_ref = char_ref + char_read;
653 
654 
655     /* Fetch information about the database. */
656     (void) xtmCdFetchNamedDb( custom_data_ref -> cal_db_handle, db_name,
657                               &db_info, NULL );
658 
659     /* Database operations possible. */
660     xtmDbCheckDbOperations( db_info.directory, True, &operations );
661 
662 
663     /* Only calendars we can read are shown. */
664     if( flagIsClear( operations, XTM_DB_FLAG_MODE_READ ) )
665       continue;
666 
667 
668     /* Fetch the flags values. */
669     flags_buffer[ 0 ] = '\0';
670 
671     if( flagIsSet( operations, XTM_DB_FLAG_MODE_READ ) )
672       strcat( flags_buffer, "r" );
673     else
674       strcat( flags_buffer, "-" );
675 
676     if( flagIsSet( operations, XTM_DB_FLAG_MODE_WRITE ) )
677       strcat( flags_buffer, "w" );
678     else
679       strcat( flags_buffer, "-" );
680 
681     strcat( flags_buffer, " " );
682 
683 
684     sprintf( buffer, "%-15.15s %-5.5s %s",
685 	     db_info.short_name, flags_buffer,
686 	     db_info.directory );
687 
688     list_items[ index ] = XmStringCreate( buffer, CS );
689     index++;
690 
691     if( index >= MAX_CALENDARS )
692       break;
693 
694   } while( True );
695 
696   if( db_names != NULL )
697     SysFree( db_names );
698 
699 
700   /* The list is always sorted. */
701   xitSortStringList( list_items, index );
702 
703 
704   tempW = XtNameToWidget( mainW, "DbLocLiSW.DbLocLi" );
705 
706   /* Assign the database locations to the list. */
707   n = 0;
708   XtSetArg( args[ n ], XmNitems, list_items ); n++;
709   XtSetArg( args[ n ], XmNitemCount, index ); n++;
710   XtSetArg( args[ n ], XmNselectedItemCount, 0 ); n++;
711   XtSetValues( tempW, args, n );
712 
713   /* Free allocated memory. */
714   for( index1 = 0; index1 < index; index1++ )
715     XmStringFree( list_items[ index1 ] );
716 
717 
718   return;
719 
720 } /* setCalendarList */
721 
722 
723 /*----------------------------------------------------------------------*/
724 
725 static void
applyCB(Widget widget,OPEN_REC_REF open_ref,XtPointer call_data)726   applyCB( Widget        widget,
727            OPEN_REC_REF  open_ref,
728            XtPointer     call_data )
729 {
730 
731   /* Variables. */
732   Boolean                 ok;
733   XTM_GL_CUSTOM_DATA_REF  custom_data_ref;
734   XTM_CD_CAL_INFO         db_info;
735 
736 
737   /* Code. */
738 
739   custom_data_ref = open_ref -> appl_data_ref -> custom_data;
740 
741   /* Fetch the selected database. */
742   ok = fetchSelectedCal( open_ref );
743   if( ! ok )
744     return;
745 
746 
747   /* Fetch the selected calendar. */
748   ok = xtmCdFetchNamedDb( custom_data_ref -> cal_db_handle,
749                           open_ref -> selected_cal,
750                           &db_info, NULL );
751   if( ! ok )
752     return;
753 
754 
755   /* Call callback function? */
756   if( open_ref -> actionCB != NULL )
757     (* open_ref -> actionCB )( XTM_OV_REASON_APPLY, &db_info,
758                                open_ref -> user_data );
759 
760 
761   return;
762 
763 } /* applyCB */
764 
765 
766 /*----------------------------------------------------------------------*/
767 
768 static void
calSelectedCB(Widget widget,OPEN_REC_REF open_ref,XmListCallbackStruct * call_data)769   calSelectedCB( Widget                widget,
770                  OPEN_REC_REF          open_ref,
771                  XmListCallbackStruct  *call_data )
772 {
773 
774   /* Variables. */
775   Boolean                 ok;
776   char                    db_name[ XTM_GL_MAX_CAL_NAME + 1 ];
777   char                    *char_ref;
778   Widget                  mainW;
779   Widget                  tempW;
780   XTM_GL_CUSTOM_DATA_REF  custom_data_ref;
781   XTM_CD_CAL_INFO         db_info;
782 
783 
784   /* Code. */
785 
786   custom_data_ref = open_ref -> appl_data_ref -> custom_data;
787   mainW           = XtNameToWidget( open_ref -> openW, "OpenFdFo" );
788 
789   open_ref -> selected_cal[ 0 ] = '\0';
790 
791 
792   /* Fetch the selected calendar line. */
793   char_ref = xitStringGetString( call_data -> item, CS );
794 
795   sscanf( char_ref, "%s", db_name );
796   SysFree( char_ref );
797 
798 
799   /* Fetch the selected calendar. */
800   ok = xtmCdFetchNamedDb( custom_data_ref -> cal_db_handle,
801                           db_name,
802                           &db_info, NULL );
803   if( ! ok )
804     return;
805 
806   strcpy( open_ref -> selected_cal, db_info.short_name );
807 
808 
809   /* Selected name and location. */
810   tempW = XtNameToWidget( mainW, "CalNameTx" );
811   XmTextSetString( tempW, db_info.short_name );
812 
813   tempW = XtNameToWidget( mainW, "CalLocTx" );
814   XmTextSetString( tempW, db_info.directory );
815 
816 
817   /* Default action is to open the day view. */
818   if( call_data -> reason == XmCR_DEFAULT_ACTION )
819     okCB( widget, open_ref, NULL );
820 
821 
822   return;
823 
824 } /* calSelectedCB */
825 
826 
827 /*----------------------------------------------------------------------*/
828 
829 static void
cancelCB(Widget widget,OPEN_REC_REF open_ref,XtPointer call_data)830   cancelCB( Widget        widget,
831             OPEN_REC_REF  open_ref,
832             XtPointer     call_data )
833 {
834 
835   /* Code. */
836 
837   XtUnmanageChild( open_ref -> openW );
838 
839 
840   return;
841 
842 } /* cancelCB */
843 
844 
845 /*----------------------------------------------------------------------*/
846 
847 static void
destroyCB(Widget widget,OPEN_REC_REF open_ref,XtPointer call_data)848   destroyCB( Widget        widget,
849              OPEN_REC_REF  open_ref,
850              XtPointer     call_data )
851 {
852 
853   /* Code. */
854 
855   /* Call callback function? */
856   if( open_ref -> actionCB != NULL )
857     (* open_ref -> actionCB )( XTM_OV_REASON_DESTROY, NULL,
858                                open_ref -> user_data );
859 
860   /* Release the user data. */
861   if( open_ref -> pick_handle != NULL )
862     xtmPdDestroy( open_ref -> pick_handle );
863 
864   SysFree( open_ref );
865 
866 
867   return;
868 
869 } /* destroyCB */
870 
871 
872 /*----------------------------------------------------------------------*/
873 
874 static void
guessLocationCB(Widget widget,OPEN_REC_REF open_ref,XtPointer call_data)875   guessLocationCB( Widget        widget,
876                    OPEN_REC_REF  open_ref,
877                    XtPointer     call_data )
878 {
879 
880   /* Variables. */
881   Boolean                 ok;
882   int                     items;
883   char                    cal_name[ XTM_GL_MAX_CAL_NAME + 1 ];
884   char                    location[ XTM_GL_MAX_LOCATION + 1 ];
885   char                    *char_ref;
886   Widget                  mainW;
887   Widget                  tempW;
888   XTM_GL_CUSTOM_DATA_REF  custom_data_ref;
889   XTM_CD_CAL_INFO         db_info;
890 
891 
892   /* Code. */
893 
894   custom_data_ref = open_ref -> appl_data_ref -> custom_data;
895   mainW           = XtNameToWidget( open_ref -> openW, "OpenFdFo" );
896 
897 
898   /* Fetch the calendar name. */
899   tempW    = XtNameToWidget( mainW, "CalNameTx" );
900   char_ref = xitStringGetText( tempW );
901 
902   items = sscanf( char_ref, "%s", cal_name );
903   SysFree( char_ref );
904 
905   if( items != 1 ) {
906     xitErMessage( open_ref -> openW, XIT_ER_INFO,
907                   module_name, "guessLocationCB",
908                   msgGetText( MXDI_MISSING_CALENDAR_NAME ) );
909 
910     return;
911   }
912 
913 
914   /* Do we know of the calendar already? */
915   ok = xtmCdFetchNamedDb( custom_data_ref -> cal_db_handle, cal_name,
916                           &db_info, NULL );
917 
918   if( ok && flagIsSet( db_info.operations, XTM_DB_FLAG_MODE_READ ) ) {
919     tempW = XtNameToWidget( mainW, "CalLocTx" );
920     XmTextSetString( tempW, db_info.directory );
921 
922     return;
923   }
924 
925 
926   /* Try to locate the calendar. */
927   ok = xtmLcLocateCalendar( open_ref -> openW, cal_name,
928                             custom_data_ref -> find_location_script,
929                             location, sizeof( location ) );
930 
931   if( ! ok || strlen( location ) == 0 ) {
932     xitErMessage( open_ref -> openW, XIT_ER_INFO,
933                   module_name, "guessLocationCB",
934                   msgGetText( MXDI_CANNOT_LOCATE_CALENDAR ) );
935 
936     return;
937   }
938 
939   tempW = XtNameToWidget( mainW, "CalLocTx" );
940   XmTextSetString( tempW, location );
941 
942 
943   return;
944 
945 } /* guessLocationCB */
946 
947 
948 /*----------------------------------------------------------------------*/
949 
950 static void
helpCB(Widget widget,OPEN_REC_REF open_ref,XtPointer call_data)951   helpCB( Widget        widget,
952           OPEN_REC_REF  open_ref,
953           XtPointer     call_data )
954 {
955 
956   /* Code. */
957 
958   xtmHlDisplayHelp( open_ref -> appl_data_ref -> info_handle,
959                     XTM_HL_WINDOW_INDEX,
960                     open_window_id, "" );
961 
962   return;
963 
964 } /* helpCB */
965 
966 
967 /*----------------------------------------------------------------------*/
968 
969 static void
okCB(Widget widget,OPEN_REC_REF open_ref,XtPointer call_data)970   okCB( Widget        widget,
971         OPEN_REC_REF  open_ref,
972         XtPointer     call_data )
973 {
974 
975   /* Variables. */
976   Boolean                 ok;
977   XTM_GL_CUSTOM_DATA_REF  custom_data_ref;
978   XTM_CD_CAL_INFO         db_info;
979 
980 
981   /* Code. */
982 
983   custom_data_ref = open_ref -> appl_data_ref -> custom_data;
984 
985   /* Fetch the selected database. */
986   ok = fetchSelectedCal( open_ref );
987   if( ! ok )
988     return;
989 
990 
991   /* Fetch the selected calendar. */
992   ok = xtmCdFetchNamedDb( custom_data_ref -> cal_db_handle,
993                           open_ref -> selected_cal,
994                           &db_info, NULL );
995   if( ! ok )
996     return;
997 
998 
999   /* Call callback function? */
1000   if( open_ref -> actionCB != NULL )
1001     (* open_ref -> actionCB )( XTM_OV_REASON_APPLY, &db_info,
1002                                open_ref -> user_data );
1003 
1004   XtUnmanageChild( open_ref -> openW );
1005 
1006 
1007   return;
1008 
1009 } /* okCB */
1010 
1011 
1012 /*----------------------------------------------------------------------*/
1013 
1014 static void
pickCalCB(Widget widget,OPEN_REC_REF open_ref,XtPointer call_data)1015   pickCalCB( Widget        widget,
1016              OPEN_REC_REF  open_ref,
1017              XtPointer     call_data )
1018 {
1019 
1020   /* Variables. */
1021   XTM_GL_CUSTOM_DATA_REF  custom_data_ref;
1022 
1023 
1024   /* Code. */
1025 
1026   custom_data_ref = open_ref -> appl_data_ref -> custom_data;
1027 
1028   /* Initialize? */
1029   if( open_ref -> pick_handle == NULL )
1030     open_ref -> pick_handle = xtmPdInitialize(
1031                                 open_ref -> openW,
1032                                 True,
1033                                 open_ref -> appl_data_ref -> info_handle,
1034                                 pickCalApplyCB,
1035                                 (void *) open_ref );
1036 
1037   /* Diaplay the window. */
1038   xtmPdDisplayPickWindow( open_ref -> pick_handle,
1039                           custom_data_ref -> db_selection_dir,
1040                           "*_cal" );
1041 
1042   return;
1043 
1044 } /* pickCalCB */
1045 
1046 
1047 /*----------------------------------------------------------------------*/
1048 
1049 static void
pickCalApplyCB(XTM_PD_REASON reason,XTM_CD_CAL_INFO * db_ref,void * client_data)1050   pickCalApplyCB( XTM_PD_REASON    reason,
1051                   XTM_CD_CAL_INFO  *db_ref,
1052                   void             *client_data )
1053 {
1054 
1055   /* Variables. */
1056   Widget        mainW;
1057   Widget        tempW;
1058   OPEN_REC_REF  open_ref;
1059 
1060 
1061   /* Code. */
1062 
1063   if( reason != XTM_PD_REASON_OK )
1064     return;
1065 
1066   open_ref = (OPEN_REC_REF) client_data;
1067   mainW    = XtNameToWidget( open_ref -> openW, "OpenFdFo" );
1068 
1069   tempW = XtNameToWidget( mainW, "CalNameTx" );
1070   XmTextSetString( tempW, db_ref -> short_name );
1071 
1072   tempW = XtNameToWidget( mainW, "CalLocTx" );
1073   XmTextSetString( tempW, db_ref -> directory );
1074 
1075 
1076   return;
1077 
1078 } /* pickCalApplyCB */
1079