~ubuntu-core-dev/update-notifier/ubuntu

« back to all changes in this revision

Viewing changes to src/hooks.c

  • Committer: mvo
  • Date: 2005-06-28 17:46:20 UTC
  • Revision ID: gustavo@niemeyer.net-20050628174620-34809fc806c728cb
* src/hooks.c:
  - better debugging support
  - fixed various small bugs in the code

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
/* relative to the home dir */
14
14
#define HOOKS_SEEN ".update-notifier/hooks_seen"
15
15
 
 
16
 
 
17
#define HOOK_DEBUG 0
 
18
 
 
19
// Debuging 
 
20
static void debug_log_handler (const gchar   *log_domain,
 
21
                               GLogLevelFlags log_level,
 
22
                               const gchar   *message,
 
23
                               gpointer       user_data)
 
24
{
 
25
#if HOOK_DEBUG 
 
26
    g_log_default_handler (log_domain, log_level, message, user_data);
 
27
#endif
 
28
}
 
29
 
 
30
 
16
31
void hooks_trayicon_update_tooltip (TrayApplet *un, int num_hooks)
17
32
{
18
 
   //g_print("update_tooltip: %x \n", un);
 
33
   g_debug("update_tooltip: %x ", un);
19
34
   gchar *updates;
20
35
   gchar *explanation;
21
36
 
31
46
}
32
47
 
33
48
 
34
 
 
 
49
// compare a HookFile with a filename and find the HookFile that matches
35
50
gint compare_hook_func(gconstpointer a, gconstpointer b)
36
51
{
37
 
   //g_print("compare: %s %s\n",(char*)(((HookFileSeen*)a)->filename),(char*)b);
 
52
   //g_debug("compare: %s %s\n",(char*)(((HookFileSeen*)a)->filename),(char*)b);
38
53
   return strcmp(((HookFile*)a)->filename, b);
39
54
}
40
55
 
50
65
   return buf.st_mtime;
51
66
}
52
67
 
53
 
 
 
68
/* mark a given hook file as seen 
 
69
  (actually implemented to write out all the information we have)
 
70
*/
54
71
gboolean hook_file_mark_as_seen(HookDialog *hook_dialog, 
55
72
                                HookFile *hf)
56
73
{
57
 
   g_print("mark_hook_file_as_seen: %s\n", hf->filename);
 
74
   g_debug("mark_hook_file_as_seen: %s", hf->filename);
58
75
 
59
76
   // mark as seen 
60
77
   hf->seen = TRUE;
61
78
 
62
79
   // write out the list of known files
63
 
   gchar *filename = g_strdup_printf("%s/"HOOKS_SEEN,g_get_home_dir());
 
80
   gchar *filename = g_strdup_printf("%s/%s",g_get_home_dir(),HOOKS_SEEN);
64
81
   FILE *f = fopen(filename, "w");
65
 
   if(f==NULL)
 
82
   if(f==NULL) {
 
83
      g_warning("Something went wrong writing the users hook file");
66
84
      return FALSE;
67
 
 
 
85
   }
68
86
   GList *elm = g_list_first(hook_dialog->hook_files);
69
87
   for(;elm != NULL; elm = g_list_next(elm)) {
70
88
      HookFile *e = (HookFile*)elm->data;
71
 
      if(e->seen == TRUE)
 
89
      if(e->seen == TRUE) {
72
90
         fprintf(f,"%s %i %x\n", e->filename, e->mtime, (int)(e->cmd_run));
 
91
         g_debug("writing: %s %i %x", e->filename, e->mtime, (int)(e->cmd_run));
 
92
      }
73
93
   }
74
94
 
75
95
   fclose(f);
78
98
   return TRUE;
79
99
}
80
100
 
 
101
/* mark a given HookFile as run */
81
102
gboolean mark_hook_file_as_run(HookDialog *hook_dialog, HookFile *hf)
82
103
{
83
 
   // IMPLEMENT ME
84
 
   //g_print("mark_hook_file_as_run: %s\n",filename);
 
104
   // FIXME: TEST THIS CODE
 
105
   //g_debug("mark_hook_file_as_run: %s\n",filename);
85
106
   hf->cmd_run = TRUE;
86
107
 
87
108
   return TRUE;
120
141
   /* try $field-$languagecode_$countrycode first */
121
142
   s = g_strdup_printf("%s-%s", field, get_lang_code(FALSE));
122
143
   entry = rfc822_header_lookup(rfc822, s);
123
 
   //g_print("Looking for: %s ; found: %s\n",s,entry);
 
144
   //g_debug("Looking for: %s ; found: %s\n",s,entry);
124
145
   g_free(s);
125
146
   if(entry != NULL)
126
147
      return entry;
127
148
 
128
149
   /* try $field-$languagecode next */
129
150
   s = g_strdup_printf("%s-%s", field, get_lang_code(TRUE));
130
 
   //g_print("Looking for: %s ; found: %s\n",s,entry);
 
151
   //g_debug("Looking for: %s ; found: %s\n",s,entry);
131
152
   entry = rfc822_header_lookup(rfc822, s);
132
153
   g_free(s);
133
154
   if(entry != NULL)
142
163
 */
143
164
gboolean show_next_hook(TrayApplet *ta, GList *hooks)
144
165
{
145
 
   g_print("show_next_hook()\n");
 
166
   //g_debug("show_next_hook()\n");
146
167
 
147
168
   HookDialog *hook_dialog = (HookDialog *)ta->user_data;
148
169
   GladeXML *xml = hook_dialog->glade_xml;
164
185
      hf = (HookFile*)elm->data;
165
186
      if(hf->seen == FALSE) {
166
187
         hook_file = hf->filename;
167
 
         g_print("next_hook is: %s\n",hook_file);
 
188
         //g_debug("next_hook is: %s\n",hook_file);
168
189
         break;
169
190
      }
170
191
   }
217
238
 
218
239
void cb_button_run(GtkWidget *self, void *data)
219
240
{
220
 
   //g_print("cb_button_run()\n");
 
241
   //g_debug("cb_button_run()\n");
221
242
   gchar *argv[4];
222
243
 
223
244
   TrayApplet *ta = (TrayApplet *)data;
250
271
 
251
272
void cb_button_next(GtkWidget *self, void *data)
252
273
{
253
 
   g_print("cb_button_next()\n");
 
274
   g_debug("cb_button_next()");
254
275
   TrayApplet *ta = (TrayApplet *)data;
255
276
   HookDialog *hook_dialog = (HookDialog *)ta->user_data;
256
277
 
274
295
 
275
296
void show_hooks(TrayApplet *ta)
276
297
{
277
 
   //g_print("show_hooks()\n");
 
298
   g_debug("show_hooks()");
278
299
   HookDialog* hook_dialog = (HookDialog *)ta->user_data;
279
300
 
280
301
   GladeXML *xml = hook_dialog->glade_xml;
312
333
                   TrayApplet *ta)
313
334
{
314
335
        if (event->button == 1 && event->type == GDK_BUTTON_RELEASE) {
315
 
           //g_print("left click on hook applet\n");
 
336
           //g_debug("left click on hook applet\n");
316
337
           show_hooks(ta);
317
338
        }
318
339
        return TRUE;
321
342
 
322
343
gboolean check_update_hooks(TrayApplet *ta)
323
344
{
324
 
   g_print("\ncheck_update_hooks()\n");
 
345
   g_debug("check_update_hooks()");
325
346
 
326
347
   HookDialog *hook_dialog = (HookDialog*)ta->user_data;
327
348
 
340
361
                                      compare_hook_func);
341
362
      // not seen before, add to the list
342
363
      if(elm == NULL) {
343
 
         g_print("never seen before: %s\n",hook_file);
 
364
         g_debug("never seen before: %s",hook_file);
344
365
         new_count++;
345
366
         HookFile *t = g_new0(HookFile, 1);
346
367
         t->filename = strdup(hook_file);
353
374
      
354
375
      // this is the hook file information we have
355
376
      HookFile *hf = (HookFile*)elm->data;
 
377
 
 
378
      // we have not seen it yet
 
379
      if(hf->seen == FALSE) {
 
380
         new_count++;
 
381
         continue;
 
382
      }
 
383
 
356
384
      // file has changed since we last seen it
357
385
      time_t new_mtime = hook_file_mtime(hook_file);
358
386
      if(new_mtime > hf->mtime) {
359
 
         g_print("newer mtime: %s (%i)\n",hook_file,hf->mtime, new_mtime);
 
387
         g_debug("newer mtime: %s (%i > %i))",hook_file, new_mtime, hf->mtime);
360
388
         new_count++;
361
389
         hf->seen = FALSE;
362
390
         hf->mtime = new_mtime;
363
391
      }
 
392
 
364
393
   }
365
394
   g_dir_close(dir);
366
395
 
367
 
   g_print("hooks: %i (new: %i)\n", g_list_length(hook_files), new_count);
 
396
   g_debug("hooks: %i (new: %i)", g_list_length(hook_files), new_count);
368
397
   hook_dialog->hook_files = hook_files;
369
398
 
370
399
   GladeXML *xml = hook_dialog->glade_xml;
371
400
   GtkWidget *button_next = glade_xml_get_widget(xml, "button_next");
372
401
   assert(button_next);
373
402
 
374
 
   if (new_count == 0) {
375
 
      //g_print("gtk_widget_hide() called on tray_icon\n");
 
403
   switch(new_count) {
 
404
   case 0:
376
405
      gtk_widget_hide(GTK_WIDGET(ta->tray_icon));
377
406
      gtk_widget_hide(button_next);
378
 
   } else {
379
 
      //g_print("gtk_widget_show() called on tray_icon\n");
380
 
      hooks_trayicon_update_tooltip (ta, new_count);
 
407
      break;
 
408
   case 1:
 
409
      gtk_widget_show(GTK_WIDGET(ta->tray_icon));
 
410
      gtk_widget_hide(button_next);
 
411
      break;
 
412
   default:
381
413
      gtk_widget_show(GTK_WIDGET(ta->tray_icon));
382
414
      gtk_widget_show(button_next);
 
415
      break;
383
416
   }
 
417
   hooks_trayicon_update_tooltip (ta, new_count);
384
418
 
385
419
   return TRUE;
386
420
}
405
439
      t->seen = TRUE;
406
440
      seen = g_list_append(seen, t);
407
441
      
408
 
      //g_print("got: %s %i %i \n",buf,time,was_run);
 
442
      g_debug("got: %s %i %i ",buf,time,was_run);
409
443
   }
410
444
   g_free(filename);
411
445
   fclose(f);
433
467
                                 G_CALLBACK(cb_button_run), 
434
468
                                 (gpointer)ta);
435
469
 
 
470
   // setup a custom debug log handler
 
471
   g_log_set_handler (G_LOG_DOMAIN,
 
472
                      G_LOG_LEVEL_DEBUG,
 
473
                      debug_log_handler,
 
474
                      NULL);
 
475
 
 
476
 
436
477
   /* show dialog on click */
437
478
   g_signal_connect (G_OBJECT(ta->tray_icon),
438
479
                     "button-release-event",