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

« back to all changes in this revision

Viewing changes to src/hooks.c

  • Committer: mvo
  • Date: 2005-09-11 07:15:43 UTC
  • Revision ID: gustavo@niemeyer.net-20050911071543-022d65c46ec8e13b
* src/update.c:
  - make the timeout for the notification stuff 5s
* src/hooks.c:
  - add DontShowAfterReboot key
* src/hook-remove.c:
  - added a possible future hook-remove application that is save
    to be run as root

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
   return strcmp(((HookFile*)a)->filename, b);
53
53
}
54
54
 
55
 
time_t hook_file_mtime(char *filename)
 
55
time_t hook_file_mtime(const gchar *filename)
56
56
{
57
57
   struct stat buf;
58
58
   char *file = g_strdup_printf("%s/%s",HOOKS_DIR, filename);
100
100
/* mark a given HookFile as run */
101
101
gboolean mark_hook_file_as_run(HookDialog *hook_dialog, HookFile *hf)
102
102
{
103
 
   // FIXME: TEST THIS CODE
104
 
   //g_debug("mark_hook_file_as_run: %s\n",filename);
 
103
   if(hf == NULL)
 
104
      return FALSE;
 
105
 
 
106
   g_debug("mark_hook_file_as_run: %s\n",hf->filename);
105
107
   hf->cmd_run = TRUE;
106
108
 
107
109
   return TRUE;
211
213
   char *term = g_strstrip(rfc822_header_lookup(rfc822, "Terminal"));
212
214
   g_object_set_data(G_OBJECT(button_run),"cmd", g_strdup(cmd));
213
215
   g_object_set_data(G_OBJECT(button_run),"term", g_strdup(term));
214
 
   g_object_set_data(G_OBJECT(button_run),"hook_file", g_strdup(hook_file));
 
216
   g_object_set_data(G_OBJECT(button_run),"hook_file", hf);
215
217
   if(cmd != NULL) {
216
218
      gtk_widget_show(button_run);
217
219
   } else {
245
247
void cb_button_run(GtkWidget *self, void *data)
246
248
{
247
249
   //g_debug("cb_button_run()\n");
248
 
   gchar *argv[4];
 
250
   gchar *cmdline;
249
251
 
250
252
   TrayApplet *ta = (TrayApplet *)data;
251
253
   HookDialog *hook_dialog = (HookDialog *)ta->user_data;
252
254
 
253
255
   /* mark the current hook file as run */
254
 
   gchar *hook_file = g_object_get_data(G_OBJECT(self), "hook_file");
255
 
   mark_hook_file_as_run(hook_dialog, (const char*)hook_file);
 
256
   HookFile *hf = g_object_get_data(G_OBJECT(self), "hook_file");
 
257
   mark_hook_file_as_run(hook_dialog, hf);
256
258
 
257
259
   gchar *cmd = g_object_get_data(G_OBJECT(self), "cmd");
258
260
   gchar *term = g_object_get_data(G_OBJECT(self), "term");
263
265
   }
264
266
 
265
267
   if(term != NULL && !g_ascii_strncasecmp(term, "true",-1)) {
266
 
      argv[0] = "gnome-terminal";
267
 
      argv[1] = "-e";
268
 
      argv[2] = cmd;
269
 
      argv[3] = NULL;
270
 
   } else {
271
 
      argv[0] = cmd;
272
 
      argv[1] = NULL;
273
 
   }
274
 
   g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH , 
275
 
                 NULL, NULL, NULL, NULL);
 
268
      cmdline = g_strdup_printf("gnome-terminal -e %s",cmd);
 
269
   } else 
 
270
      cmdline = g_strdup(cmd);
 
271
 
 
272
   g_spawn_command_line_async(cmdline, NULL);
 
273
   g_free(cmdline);
276
274
}
277
275
 
278
276
void cb_button_next(GtkWidget *self, void *data)
345
343
        return TRUE;
346
344
}
347
345
 
 
346
gboolean is_hook_relevant(const gchar *hook_file)
 
347
{
 
348
   g_debug("is_hook_relevant()\n");
 
349
   gboolean res = TRUE;
 
350
 
 
351
   char *filename = g_strdup_printf("%s%s",HOOKS_DIR,hook_file);
 
352
   FILE *f = fopen(filename, "r");
 
353
   if(f == NULL) {
 
354
      // can't open, can't be relevant
 
355
      return FALSE; 
 
356
   }
 
357
   struct rfc822_header *rfc822 = rfc822_parse_stanza(f);
 
358
 
 
359
   // check the DontShowAfterReboot flag
 
360
   gchar *b = rfc822_header_lookup(rfc822, "DontShowAfterReboot");
 
361
   if(b != NULL && g_ascii_strncasecmp(b, "true",-1) == 0) {
 
362
 
 
363
      // read the uptime information
 
364
      double uptime=0, idle=0;
 
365
      char buf[1024];
 
366
      int fd = open("/proc/uptimex", 0);
 
367
      int local_n = read(fd, buf, sizeof(buf)-1);
 
368
      buf[local_n] = '\0';
 
369
      char *savelocale = setlocale(LC_NUMERIC, NULL);
 
370
      setlocale(LC_NUMERIC,"C");
 
371
      sscanf(buf, "%lf %lf", &uptime,&idle);
 
372
      close(fd);
 
373
      setlocale(LC_NUMERIC,savelocale);
 
374
 
 
375
      time_t mtime = hook_file_mtime(hook_file);
 
376
      time_t now = time(NULL);
 
377
 
 
378
      g_debug("now: %i mtime: %i uptime: %f\n",now,mtime,uptime);
 
379
      g_debug("diff: %i  uptime: %f\n",now-mtime,uptime);
 
380
      if((int)uptime > 0 && (now - mtime) > (int)uptime) {
 
381
         //g_print("not relevant because of reboot: %s\n",hook_file);
 
382
         res = FALSE;
 
383
      }
 
384
   }
 
385
 
 
386
   fclose(f);
 
387
   g_free(filename);
 
388
   rfc822_header_free_all(rfc822);
 
389
   return res;
 
390
}
348
391
 
349
392
gboolean check_update_hooks(TrayApplet *ta)
350
393
{
359
402
   if(dir == NULL)
360
403
      g_critical("can't read %s directory\n",HOOKS_DIR);
361
404
 
362
 
   int new_count = 0;
 
405
   int unseen_count = 0;
363
406
   GList *hook_files = hook_dialog->hook_files;
364
407
   while((hook_file=g_dir_read_name(dir)) != NULL) {
 
408
 
 
409
      // check if the hook still applies (for e.g. DontShowAfterReboot)
 
410
      if(!is_hook_relevant(hook_file))
 
411
         continue;
 
412
 
365
413
      // see if we already know about this hook filename
366
414
      GList *elm = g_list_find_custom(hook_dialog->hook_files,hook_file,
367
415
                                      compare_hook_func);
368
416
      // not seen before, add to the list
369
417
      if(elm == NULL) {
370
418
         g_debug("never seen before: %s",hook_file);
371
 
         new_count++;
 
419
         unseen_count++;
372
420
         HookFile *t = g_new0(HookFile, 1);
373
421
         t->filename = strdup(hook_file);
374
422
         t->mtime = hook_file_mtime(hook_file);
383
431
 
384
432
      // we have not seen it yet
385
433
      if(hf->seen == FALSE) {
386
 
         new_count++;
 
434
         unseen_count++;
387
435
         continue;
388
436
      }
389
437
 
391
439
      time_t new_mtime = hook_file_mtime(hook_file);
392
440
      if(new_mtime > hf->mtime) {
393
441
         g_debug("newer mtime: %s (%i > %i))",hook_file, new_mtime, hf->mtime);
394
 
         new_count++;
 
442
         unseen_count++;
395
443
         hf->seen = FALSE;
396
444
         hf->mtime = new_mtime;
397
445
      }
399
447
   }
400
448
   g_dir_close(dir);
401
449
 
402
 
   g_debug("hooks: %i (new: %i)", g_list_length(hook_files), new_count);
 
450
   g_debug("hooks: %i (new: %i)", g_list_length(hook_files), unseen_count);
403
451
   hook_dialog->hook_files = hook_files;
404
452
 
405
453
   GladeXML *xml = hook_dialog->glade_xml;
406
454
   GtkWidget *button_next = glade_xml_get_widget(xml, "button_next");
407
455
   assert(button_next);
408
456
 
409
 
   switch(new_count) {
 
457
   switch(unseen_count) {
410
458
   case 0:
411
459
      gtk_widget_hide(GTK_WIDGET(ta->tray_icon));
412
460
      gtk_widget_hide(button_next);
420
468
      gtk_widget_show(button_next);
421
469
      break;
422
470
   }
423
 
   hooks_trayicon_update_tooltip (ta, new_count);
 
471
   hooks_trayicon_update_tooltip (ta, unseen_count);
424
472
 
425
473
   return TRUE;
426
474
}