File: options.c

package info (click to toggle)
gworldclock 1.4.4-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 980 kB
  • ctags: 158
  • sloc: sh: 3,855; ansic: 2,228; makefile: 68; sed: 16
file content (531 lines) | stat: -rw-r--r-- 14,536 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/* Processes user options from the command line or from the option file
~/.gworldclock.

.gworldclock is an XML file, with default structure:

<?xml version="1.0"?>
<gworldclock>
  <timeDisplayFormat>%c</timeDisplayFormat>
  <rendezvous>
     <doneLabel>Return</doneLabel>
  </rendezvous>
</gworldclock>

<timeDisplayFormat> contains the date format string used to display
data&time.

<doneLabel> inside <rendezvous> provides a means to specify an
alternate label for the "Done" button (the button which closes the
Rendezvous interface and returns to normal clock operation).

In case you were wondering whether name/value pairs would have been simpler 
than XML for storing user options, my simple response is that I wanted to learn
how to use XML from within C.

*/



#include <stdlib.h>
#include <time.h>

#include <unistd.h> /* getopt */
#include <gtk/gtk.h>
#include <libxml/parser.h>
#include <libintl.h>

#include "timer.h"

#define _(A) gettext(A)

#define LOCALE_DEFAULT_DISPLAY "%c"
#define SHORT_DISPLAY "%x %l:%M%P"
#define SHORT_DISPLAY_24HOUR "%x %R"

gchar *defaultOptionFile=".gworldclock";
gchar *defaultConfigFilename=".tzlist";
gchar *defaultTimeDisplayFormat = LOCALE_DEFAULT_DISPLAY;
extern gchar *defaultRendezvousUIFormat;

gboolean changedPreferences = FALSE;
GtkEntry *rollYourOwnDisplayFormat=NULL;

GString *oldTimeDisplayFormat;

#define XMLVERSION  "1.0"
#define ROOT_NODE   "gworldclock"
#define TIME_DISPLAY_FORMAT_NODE "timeDisplayFormat"
#define RENDEZVOUS_NODE "rendezvous"
#define RENDEZVOUS_DONELABEL_NODE "doneLabel"

GString *getTimeDisplayFormat( xmlDocPtr optionXML )
{
   GString *format=NULL;
   xmlNodePtr cur;
   xmlChar *formatText;

   cur = xmlDocGetRootElement(optionXML);
   if (cur != NULL) 
   {
      if (xmlStrcmp(cur->name, ROOT_NODE)) {
	 fprintf(stderr,"Options file incorrectly formatted\n");
	 return NULL;
      }
      
      cur = cur->xmlChildrenNode;
      while ( (cur != NULL) && (format==NULL) ) 
      {
	 if ((!xmlStrcmp(cur->name, TIME_DISPLAY_FORMAT_NODE))) 
	 {
	    formatText = xmlNodeListGetString(optionXML, 
					      cur->xmlChildrenNode, 1);
	    format = g_string_new(formatText);
	    xmlFree(formatText);
	    return format;
	 }
	 cur = cur->next;
      }

   }   

   return format;
}

GString *getRendezvousDoneLabel( xmlDocPtr optionXML )
{
   GString *format=NULL;
   xmlNodePtr cur;
   xmlChar *formatText;

   cur = xmlDocGetRootElement(optionXML);
   if (cur != NULL) 
   {
      if (xmlStrcmp(cur->name, ROOT_NODE)) {
	 fprintf(stderr,"Options file incorrectly formatted\n");
	 return NULL;
      }
      
      cur = cur->xmlChildrenNode;
      while ( (cur != NULL) && (format==NULL) ) 
      {
	 if ((!xmlStrcmp(cur->name, RENDEZVOUS_NODE))) 
	 {

	     cur = cur->xmlChildrenNode;
	     while ( (cur != NULL) && (format==NULL) ) 
	     {
		 if ((!xmlStrcmp(cur->name, RENDEZVOUS_DONELABEL_NODE))) 
		 {
		     formatText = xmlNodeListGetString(optionXML, 
						       cur->xmlChildrenNode, 1);
		     format = g_string_new(formatText);
		     xmlFree(formatText);
		     return format;
		 }
		 cur = cur->next;
	     }
	 }
	 cur = cur->next;
      }

   }   

   return format;
}

void GetOptions( int argc, char **argv )
{
  extern GString *configfile, *displayFormat, *optionFile;
  extern GString *rendezvousDoneLabel;
  int c;
  xmlDocPtr optionXML;
  GString *temp;


  /* set defaults */
  configfile = g_string_new(g_strdup((gchar *) getenv("HOME")));  
  g_string_append(configfile,"/");
  g_string_append(configfile,defaultConfigFilename);

  displayFormat = g_string_new( g_strdup( defaultTimeDisplayFormat ) );

 /* read configuration options from file */
  optionFile = g_string_new(g_strdup((gchar *) getenv("HOME")));  
  g_string_append(optionFile,"/");
  g_string_append(optionFile,defaultOptionFile);
  optionXML = xmlParseFile( optionFile->str );
  if ( optionXML!=NULL )
  {
     temp = getTimeDisplayFormat(optionXML);
     if ( temp != NULL ) 
     {
	g_string_free( displayFormat, TRUE );
	displayFormat = temp;
     }

     temp = getRendezvousDoneLabel(optionXML);
     if ( temp != NULL ) 
     {
	 if ( rendezvousDoneLabel != NULL )
	     g_string_free( rendezvousDoneLabel, TRUE );
	rendezvousDoneLabel = temp;
     }

  }

  if ( optionXML!=NULL )
     xmlFreeDoc(optionXML);


  /* set command line options */
  while ((c = getopt (argc, argv, "f:")) != -1)
     switch (c) 
     {
	case 'f':
	   configfile = g_string_new(g_strdup((gchar *)optarg));  
	   break;
	   
	default:
	break;
     }

}

/* save options to options file (default ~/.gworldclock) 
   - creates the file if it does not already exist
*/
gint SaveOptions()
{
   extern GString *optionFile, *displayFormat;
   xmlDocPtr optionXML;
   xmlNodePtr root, cur, foundNode=NULL;
   xmlChar *formatText;
   gint save = FALSE;

   optionXML = xmlParseFile( optionFile->str );
   if ( optionXML==NULL ) /* create new document */
   {
      optionXML = xmlNewDoc(XMLVERSION);
      root = xmlNewDocRawNode( optionXML, NULL, ROOT_NODE, NULL ); 
      xmlDocSetRootElement( optionXML, root );
   }
   else
   {
      root = xmlDocGetRootElement(optionXML);
   }
   
   cur = root->xmlChildrenNode;
   while ( (cur != NULL) && (foundNode==NULL) ) 
   {
      if ((!xmlStrcmp(cur->name, TIME_DISPLAY_FORMAT_NODE))) 
      {
	 foundNode=cur;
      }
      cur = cur->next;
   }

   if (foundNode==NULL)
   {
      save = TRUE;
      foundNode = xmlNewTextChild( root, NULL, 
				   TIME_DISPLAY_FORMAT_NODE,
				   displayFormat->str );
   }
   else
   {
      formatText = xmlNodeListGetString(optionXML, 
					foundNode->xmlChildrenNode, 1);
      if(strcasecmp(formatText,displayFormat->str))
      {
	 save = TRUE;
	 xmlNodeSetContent( foundNode, displayFormat->str );
      }
      xmlFree(formatText);
   }

   if (save)
   {
      xmlSaveFormatFile( optionFile->str, optionXML, 1 );
   }

   xmlFreeDoc(optionXML);
   
   return 1;
}

void StoreOldPreferences()
{
   extern GString *displayFormat;
   if ( oldTimeDisplayFormat == NULL )
   {
      oldTimeDisplayFormat = g_string_new( displayFormat->str);
   }
   else
   {
      oldTimeDisplayFormat = g_string_assign( oldTimeDisplayFormat, 
					      displayFormat->str);
   }
}


void updateDisplayFormat( GString *newDisplayFormat, gpointer clocklist )
{
   extern GString *displayFormat;
   
   displayFormat = g_string_assign( displayFormat, 
				    newDisplayFormat->str);

   /* restart timer, in case displayFormat was changed to require or
      not require seconds */
   reset_timer(clocklist);

   // update display now, in case timer is set to 60 sec, or else
   // new display will not be seen for another minute
   SetTime(clocklist); 

   /* update column size to adapt to new date format */
   resizeWindow( gtk_widget_get_toplevel( GTK_WIDGET(clocklist) ), clocklist);
   

   changedPreferences = TRUE;

}

void RestoreOldPreferences( gpointer clocklist )
{
   extern GString *displayFormat;

   if ( ! g_string_equal( displayFormat, oldTimeDisplayFormat ) )
   {
       updateDisplayFormat( oldTimeDisplayFormat, clocklist );
   }
}

/* Updates the user's preferences. */ 
void ProcessPreferences( GObject *owner, gpointer clocklist )
{
   extern GString *displayFormat;

   GString *newDisplayFormat;
   GSList *radioGroup;
   GtkEntry *entry;

   radioGroup =  gtk_radio_button_get_group( 
      g_object_get_data(owner, "radioDisplayFormat") );

   while( radioGroup != NULL )
   {
      if ( gtk_toggle_button_get_active(
	      GTK_TOGGLE_BUTTON(radioGroup->data) ) )
      {
	 newDisplayFormat = g_string_new( 
	    g_object_get_data( G_OBJECT(radioGroup->data),
			       "displayFormat" ) );
	 break;
      }
      else
	 radioGroup = radioGroup->next;
   }

   if (!strcmp(newDisplayFormat->str, "rollYourOwn") )
   {
      entry = g_object_get_data(owner, "entryRollYourOwnDisplayFormat");
      newDisplayFormat = g_string_assign( newDisplayFormat, 
				        gtk_entry_get_text( entry ) );
   }

   if ( ! g_string_equal( displayFormat, newDisplayFormat ) )
   {
       updateDisplayFormat( newDisplayFormat, clocklist );
   }
   g_string_free( newDisplayFormat, TRUE );
}

void SavePreferences()
{
   extern GString *displayFormat;

   if (changedPreferences)
   {
      SaveOptions();
      changedPreferences = FALSE;
   }
}

void ProcessPreferenceResponse( GtkDialog *dialog,
			 gint response,
			 gpointer clocklist)
{
   switch (response)
   {
      case GTK_RESPONSE_OK:
	 /* process preferences and close */
         ProcessPreferences(G_OBJECT(dialog), clocklist);
	 SavePreferences();
	 gtk_widget_destroy(GTK_WIDGET(dialog));
         break;
      case GTK_RESPONSE_APPLY:
	 /* process preferences, keep dialog box open */
         ProcessPreferences(G_OBJECT(dialog), clocklist);
         break;
      case GTK_RESPONSE_CLOSE:
      case GTK_RESPONSE_DELETE_EVENT:
	 SavePreferences();
	 gtk_widget_destroy(GTK_WIDGET(dialog));
         break;
      case GTK_RESPONSE_CANCEL:
	 /* restore original preferences and close */
	 RestoreOldPreferences(clocklist);
	 SavePreferences();
	 gtk_widget_destroy (GTK_WIDGET(dialog));
         break;
      default:
         /* unknown response, do nothing */
         break;
   }
}

/* Lays out the various display options, 
   as radio buttons inside the container.
   A reference to the Display Format radio buttons is stored in owner with
   key "radioDisplayFormat".
   The user's format Entry Box is stored under "entryRollYourOwnDisplayFormat".
   The format string for each radio button is stored with it under key
   "displayFormat".
*/
void SetDisplayAlternatives( GtkContainer *container, GObject *owner )
{
   extern GString *displayFormat;

   GtkWidget *radio, *entry;
   gboolean foundFormat = FALSE;
   GString *text;
   time_t currenttime;
   struct tm *tm;
   char rawTimeDisplay[TIME_DISPLAY_SIZE];
   gchar *timeDisplay;

   time(&currenttime);
   tm = (struct tm *) localtime( (time_t *) &currenttime );

   strftime (rawTimeDisplay, TIME_DISPLAY_SIZE, 
	     LOCALE_DEFAULT_DISPLAY, 
	     tm      );
   timeDisplay = g_locale_to_utf8 ( rawTimeDisplay,
				    -1, NULL, NULL, NULL );   
   text = g_string_new( _("Locale default") );
   text = g_string_append_c( text, ' ' );
   text = g_string_append( text, _("e.g.") );
   text = g_string_append_c( text, ' ' );
   text = g_string_append( text, timeDisplay );
   radio = gtk_radio_button_new_with_label( NULL, text->str );
   g_object_set_data( G_OBJECT(radio), "displayFormat", 
		      LOCALE_DEFAULT_DISPLAY );
   gtk_container_add ( container, radio);
   if( ! strcmp(displayFormat->str,  LOCALE_DEFAULT_DISPLAY) )
   {
      gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(radio), TRUE );
      foundFormat = TRUE;
   }
   g_free( timeDisplay );
   g_string_free(text, TRUE);
   g_object_set_data( owner, "radioDisplayFormat", (gpointer) radio );

   strftime (rawTimeDisplay, TIME_DISPLAY_SIZE, 
	     SHORT_DISPLAY, 
	     tm      );
   timeDisplay = g_locale_to_utf8 ( rawTimeDisplay,
				    -1, NULL, NULL, NULL );   
   text = g_string_new( _("Short Form") );
   text = g_string_append_c( text, ' ' );
   text = g_string_append( text, _("e.g.") );
   text = g_string_append_c( text, ' ' );
   text = g_string_append( text, timeDisplay );
   radio = gtk_radio_button_new_with_label_from_widget( GTK_RADIO_BUTTON(radio), text->str );
   g_object_set_data( G_OBJECT(radio), "displayFormat", 
		      SHORT_DISPLAY );
   gtk_container_add ( container, radio);
   if( ! strcmp(displayFormat->str,  SHORT_DISPLAY) )
   {
      gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(radio), TRUE );
      foundFormat = TRUE;
   }
   g_free( timeDisplay );
   g_string_free(text, TRUE);

   strftime (rawTimeDisplay, TIME_DISPLAY_SIZE, 
	     SHORT_DISPLAY_24HOUR, 
	     tm      );
   timeDisplay = g_locale_to_utf8 ( rawTimeDisplay,
				    -1, NULL, NULL, NULL );   
   text = g_string_new( _("Short Form (24 Hour)") );
   text = g_string_append_c( text, ' ' );
   text = g_string_append( text, _("e.g.") );
   text = g_string_append_c( text, ' ' );
   text = g_string_append( text, timeDisplay );
   radio = gtk_radio_button_new_with_label_from_widget( GTK_RADIO_BUTTON(radio), text->str );
   g_object_set_data( G_OBJECT(radio), "displayFormat", 
		      SHORT_DISPLAY_24HOUR );
   gtk_container_add ( container, radio);
   if( ! strcmp(displayFormat->str,  SHORT_DISPLAY_24HOUR) )
   {
      gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(radio), TRUE );
      foundFormat = TRUE;
   }
   g_free( timeDisplay );
   g_string_free(text, TRUE);


   text = g_string_new( _("Roll Your Own") );
   radio = gtk_radio_button_new_with_label_from_widget( GTK_RADIO_BUTTON(radio), text->str );
   g_object_set_data( G_OBJECT(radio), "displayFormat", 
		      "rollYourOwn" );
   gtk_container_add ( container, radio);
   g_string_free(text, TRUE);

   entry = gtk_entry_new();
   gtk_entry_set_width_chars( GTK_ENTRY(entry), 15 ); /* ignored!  why?! ;( */
   gtk_entry_set_text( GTK_ENTRY(entry), displayFormat->str );
   gtk_container_add ( container, entry);
   rollYourOwnDisplayFormat = GTK_ENTRY( entry );   
   gtk_widget_show (entry);
   g_object_set_data( owner, "entryRollYourOwnDisplayFormat", 
		      (gpointer) rollYourOwnDisplayFormat);
   if( ! foundFormat )
      gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(radio), TRUE );
}


/* dialog box allows user to set preferences
   e.g. preferred display format for time and date */
void ChangePreferences(GtkWidget *w, gpointer clocklist)
{
   GtkWidget *dialog, *frame, *box;

   StoreOldPreferences();
   dialog = gtk_dialog_new_with_buttons (_("Preferences"),
                                         NULL,
                                         GTK_DIALOG_DESTROY_WITH_PARENT,
                                         GTK_STOCK_OK,
					 GTK_RESPONSE_OK,
                                         GTK_STOCK_APPLY,
					 GTK_RESPONSE_APPLY,
					 GTK_STOCK_CLOSE,
					 GTK_RESPONSE_CLOSE,
					 GTK_STOCK_REVERT_TO_SAVED,
					 GTK_RESPONSE_CANCEL,
                                         NULL);
   g_signal_connect (G_OBJECT (dialog),
		     "response",
		     G_CALLBACK (ProcessPreferenceResponse),
		     (gpointer)clocklist);
   
   frame = gtk_frame_new( _("Time Display Format") );
   box = gtk_vbox_new( FALSE, 0 );
   SetDisplayAlternatives( GTK_CONTAINER( box ), G_OBJECT(dialog) );
   gtk_container_add (GTK_CONTAINER(frame), box);
   gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox),
                      frame);

   gtk_widget_show_all (dialog);
}