~noskcaj/ubuntu/utopic/xfce4-timer-plugin/1.6.0

« back to all changes in this revision

Viewing changes to src/.svn/text-base/xfcetimer.c.svn-base

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Le Folgoc
  • Date: 2010-04-10 21:12:05 UTC
  • mfrom: (0.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100410211205-n26dows55c2qtith
Tags: 0.6.1-3
* debian/patches/01_improve-timer-stop.patch: refreshed, added .pc directory
  to POTFILES.skip, fixes FTBFS.                              closes: #560662
* debian/control: added myself to uploaders.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *
 
3
 *  Copyright (C) 2005 Kemal Ilgar Eroglu <kieroglu@math.washington.edu>
 
4
 *
 
5
 *  This program is free software; you can redistribute it and/or modify
 
6
 *  it under the terms of the GNU General Public License as published by
 
7
 *  the Free Software Foundation; either version 2 of the License, or
 
8
 *  (at your option) any later version.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU Library General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
18
 */
 
19
 
 
20
 
 
21
#define TIMEOUT_TIME    2000 /* Countdown update period in milliseconds */
 
22
#define PBAR_THICKNESS  10
 
23
 
 
24
#define BORDER 8
 
25
#define WIDGET_SPACING 2
 
26
 
 
27
#ifdef HAVE_CONFIG_H
 
28
#include <config.h>
 
29
#endif
 
30
 
 
31
#include <stdio.h>
 
32
#include <stdlib.h>
 
33
#include <time.h>
 
34
#include <string.h>
 
35
 
 
36
#include <gtk/gtk.h>
 
37
 
 
38
#include <libxfce4util/libxfce4util.h>
 
39
#include <libxfcegui4/xfce_iconbutton.h>
 
40
#include <libxfce4panel/xfce-panel-plugin.h>
 
41
#include <libxfcegui4/dialogs.h>
 
42
 
 
43
#include "xfcetimer.h"
 
44
 
 
45
static gboolean create_plugin_control (XfcePanelPlugin *plugin);
 
46
XFCE_PANEL_PLUGIN_REGISTER_EXTERNAL(create_plugin_control);
 
47
 
 
48
void make_menu(plugin_data *pd);
 
49
 
 
50
/**
 
51
 * This is the timeout function that
 
52
 * repeats the alarm.
 
53
**/
 
54
static gboolean repeat_alarm (gpointer data){
 
55
 
 
56
  plugin_data *pd=(plugin_data *)data;
 
57
 
 
58
  /* Don't repeat anymore */
 
59
  if(pd->rem_repetitions == 0){
 
60
 
 
61
    if(pd->timeout_command)
 
62
        g_free(pd->timeout_command);
 
63
    pd->timeout_command=NULL;
 
64
    pd->alarm_repeating=FALSE;
 
65
    make_menu(pd);
 
66
 
 
67
    return FALSE;
 
68
 
 
69
 
 
70
  }
 
71
 
 
72
  g_spawn_command_line_async (pd->timeout_command,NULL);
 
73
  pd->rem_repetitions = pd->rem_repetitions -1 ;
 
74
  return TRUE;
 
75
}
 
76
 
 
77
 
 
78
/**
 
79
 * This is the timeout function that updates the
 
80
 * tooltip, pbar and keeps track of elapsed time
 
81
**/
 
82
static gboolean timeout_function (gpointer data){
 
83
 
 
84
  plugin_data *pd=(plugin_data *)data;
 
85
  gint elapsed_sec,remaining;
 
86
  gchar tiptext[32];
 
87
  GtkWidget *dialog;
 
88
  gulong zip;
 
89
 
 
90
  elapsed_sec=(gint)g_timer_elapsed(pd->timer,&zip);
 
91
 
 
92
  /*g_fprintf(stderr,"\nElapsed %d seconds of %d",elapsed_sec,pd-
 
93
                    >timeout_period_in_sec);*/
 
94
 
 
95
  /* If countdown is not over, update tooltip */
 
96
  if(elapsed_sec < pd->timeout_period_in_sec){
 
97
 
 
98
     remaining=pd->timeout_period_in_sec-elapsed_sec;
 
99
 
 
100
     if(remaining>=3600)
 
101
       g_snprintf(tiptext,31,_("%dh %dm %ds left"),remaining/3600, (remaining%3600)/60,
 
102
            remaining%60);
 
103
     else if (remaining>=60)
 
104
       g_snprintf(tiptext,31,_("%dm %ds left"),remaining/60, remaining%60);
 
105
     else
 
106
       g_snprintf(tiptext,31,_("%ds left"),remaining);
 
107
 
 
108
     gtk_progress_bar_set_fraction  (GTK_PROGRESS_BAR(pd->pbar),
 
109
                    ((gdouble)elapsed_sec)/pd->                         timeout_period_in_sec);
 
110
 
 
111
     gtk_tooltips_set_tip(pd->tip,GTK_WIDGET(pd->base),tiptext,NULL);
 
112
 
 
113
     return TRUE;
 
114
 
 
115
  }
 
116
 
 
117
  /* Countdown is over, stop timer and free resources */
 
118
 
 
119
  /*g_fprintf(stderr,"\nTimer command is ==> %s...",pd->timeout_command);*/
 
120
 
 
121
  if( (strlen(pd->timeout_command)==0) || !pd->nowin_if_alarm ) {
 
122
    gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(pd->pbar),1);
 
123
    dialog = gtk_message_dialog_new     (NULL,
 
124
                                    GTK_DIALOG_DESTROY_WITH_PARENT,
 
125
                                    GTK_MESSAGE_WARNING,
 
126
                                    GTK_BUTTONS_CLOSE,
 
127
                                    _("Beeep! :) \nTime is up!"));
 
128
 
 
129
    g_signal_connect_swapped (dialog, "response",
 
130
                           G_CALLBACK (gtk_widget_destroy),
 
131
                           dialog);
 
132
    gtk_widget_show(dialog);
 
133
  }
 
134
 
 
135
  if(strlen(pd->timeout_command)>0) {
 
136
 
 
137
    g_spawn_command_line_async (pd->timeout_command,NULL);
 
138
 
 
139
    if(pd->repeat_alarm){
 
140
        pd->alarm_repeating=TRUE;
 
141
        pd->rem_repetitions=pd->repetitions;
 
142
        if(pd->repeat_timeout!=0)
 
143
            g_source_remove(pd->repeat_timeout);
 
144
        pd->repeat_timeout = g_timeout_add(pd->repeat_interval*1000,repeat_alarm,pd);
 
145
    }
 
146
    else
 
147
    {
 
148
        if(pd->timeout_command)
 
149
            g_free(pd->timeout_command);
 
150
        pd->timeout_command=NULL;
 
151
    }
 
152
  }
 
153
 
 
154
  if(pd->timer)
 
155
     g_timer_destroy(pd->timer);
 
156
  pd->timer=NULL;
 
157
  gtk_tooltips_disable(pd->tip);
 
158
 
 
159
  pd->timeout=0;
 
160
 
 
161
  pd->timer_on=FALSE;
 
162
 
 
163
  /* reset pbar */
 
164
  gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(pd->pbar),0);
 
165
 
 
166
  make_menu(pd);
 
167
 
 
168
  /* This function won't be called again */
 
169
  return FALSE;
 
170
 
 
171
}
 
172
 
 
173
/**
 
174
 * This is the callback function called when a timer
 
175
 * is selected in the popup menu
 
176
**/
 
177
 
 
178
static void timer_selected (GtkWidget* menuitem, GdkEventButton* event, gpointer data){
 
179
 
 
180
  plugin_data *pd=(plugin_data *)data;
 
181
  gint row_count;
 
182
 
 
183
  row_count=0;
 
184
 
 
185
  /* Find the index of the menuitem selected, save it in pd->selected. Not very
 
186
     elegant, though */
 
187
  while (GTK_MENU_ITEM(menuitem)!=g_array_index(pd->menuarray,GtkMenuItem*,row_count) )
 
188
     row_count++;
 
189
 
 
190
  pd->selected=row_count;
 
191
 
 
192
  /*g_fprintf(stderr,"\n Selecten menuitem is %d",row_count);*/
 
193
 
 
194
}
 
195
 
 
196
/**
 
197
 * This is the callback function called when the
 
198
 * start/stop item is selected in the popup menu
 
199
**/
 
200
 
 
201
static void start_stop_selected (GtkWidget* menuitem, GdkEventButton* event, gpointer
 
202
                                        data){
 
203
 
 
204
  plugin_data *pd=(plugin_data *)data;
 
205
  GtkTreeIter iter;
 
206
  gboolean valid;
 
207
  GSList *group=NULL;
 
208
  gchar *timerinfo,*tout_command;
 
209
  gchar temp[8];
 
210
  gint row_count,cur_h,cur_m,cur_s,time;
 
211
  gint timeout_period;
 
212
  gboolean is_cd;
 
213
  GTimeVal timeval;
 
214
  struct tm *current;
 
215
 
 
216
  /* If counting down, we stop the timer and free the resources */
 
217
  if(pd->timer_on){
 
218
 
 
219
    /*g_fprintf(stderr,"\nTimer is running, shutting down...");*/
 
220
    if(pd->timer)
 
221
       g_timer_destroy(pd->timer);
 
222
    if(pd->timeout)
 
223
       g_source_remove(pd->timeout);
 
224
    if(pd->timeout_command)
 
225
       g_free(pd->timeout_command);
 
226
 
 
227
    pd->timer=NULL;
 
228
    pd->timeout_command=NULL;
 
229
    pd->timeout=0;
 
230
    pd->timer_on=FALSE;
 
231
 
 
232
    /* Disable tooltips, reset pbar */
 
233
    gtk_tooltips_disable(pd->tip);
 
234
    gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(pd->pbar),0);
 
235
 
 
236
    /* update menu*/
 
237
    make_menu(pd);
 
238
 
 
239
    return;
 
240
 
 
241
  }
 
242
 
 
243
  /* If we're here then the timer is off, so we start it */
 
244
 
 
245
  /*g_fprintf(stderr,"\nStarting timer...");*/
 
246
 
 
247
  valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(pd->list), &iter);
 
248
  row_count=0;
 
249
 
 
250
  /* Empty timer list-> Nothing to do. pd->selected=0, though. */
 
251
  if(!valid)
 
252
    return;
 
253
 
 
254
  /* Search the list item with the  right index */
 
255
  while (valid && row_count!=pd->selected){
 
256
      valid = gtk_tree_model_iter_next (GTK_TREE_MODEL(pd->list), &iter);
 
257
      row_count++;
 
258
  }
 
259
 
 
260
  gtk_tree_model_get    (GTK_TREE_MODEL(pd->list), &iter, 2, &timerinfo, 3,
 
261
            &tout_command, 4, &is_cd ,5, &time, -1);
 
262
 
 
263
  /* This will not be freed until the timeout is destroyed */
 
264
  pd->timeout_command=tout_command;
 
265
 
 
266
  /* If it's a 24h type alarm, we find the difference with current time
 
267
     Here 'time' is in minutes */
 
268
  if(!is_cd) {
 
269
 
 
270
     g_get_current_time(&timeval);
 
271
     current = localtime((time_t *)&timeval.tv_sec);
 
272
     strftime(temp,7,"%H",current);
 
273
     cur_h=atoi(temp);
 
274
     /*g_fprintf(stderr,"\n Current time: %d : ",cur_h);*/
 
275
     strftime(temp,7,"%M",current);
 
276
     cur_m=atoi(temp);
 
277
     /*g_fprintf(stderr,"%d : ",cur_m);*/
 
278
     strftime(temp,7,"%S",current);
 
279
     cur_s=atoi(temp);
 
280
     /*g_fprintf(stderr,"%d \n",cur_s);*/
 
281
 
 
282
     timeout_period=time*60 - ((60*cur_h + cur_m)*60 + cur_s);
 
283
 
 
284
     if(timeout_period <0)
 
285
        timeout_period+= 24*60*60;
 
286
 
 
287
  }
 
288
  /* Else 'time' already gives the countdown period in seconds */
 
289
  else
 
290
      timeout_period=time;
 
291
 
 
292
  pd->timeout_period_in_sec=timeout_period;
 
293
 
 
294
  /* start the timer */
 
295
  pd->timer=g_timer_new();
 
296
  pd->timer_on=TRUE;
 
297
 
 
298
  /* update stuff */
 
299
  make_menu(pd);
 
300
 
 
301
  gtk_tooltips_set_tip(pd->tip, GTK_WIDGET(pd->base), timerinfo, NULL);
 
302
  gtk_tooltips_enable(pd->tip);
 
303
  g_free(timerinfo);
 
304
 
 
305
  g_timer_start(pd->timer);
 
306
  pd->timeout = g_timeout_add(TIMEOUT_TIME, timeout_function,pd);
 
307
 
 
308
 
 
309
}
 
310
 
 
311
 
 
312
/**
 
313
 * Callback when "Stop the alarm" is selected in the popup menu
 
314
**/
 
315
 
 
316
static void stop_repeating_alarm (GtkWidget* menuitem, GdkEventButton* event, gpointer
 
317
                                        data){
 
318
   plugin_data *pd=(plugin_data *)data;
 
319
 
 
320
   g_source_remove(pd->repeat_timeout);
 
321
 
 
322
   pd->alarm_repeating=FALSE;
 
323
 
 
324
   if(pd->timeout_command){
 
325
    g_free(pd->timeout_command);
 
326
    pd->timeout_command=NULL;
 
327
   }
 
328
 
 
329
   make_menu(pd);
 
330
 
 
331
}
 
332
 
 
333
/**
 
334
 * Callback when clicking on pbar. Pops the menu up/down
 
335
**/
 
336
static void pbar_clicked (GtkWidget *pbar, GdkEventButton *event, gpointer data){
 
337
 
 
338
    plugin_data *pd=(plugin_data *)data;
 
339
 
 
340
    /*g_fprintf(stderr,"\nReceived click on button %d",event->button);*/
 
341
 
 
342
 
 
343
    if(!pd->menu){
 
344
      g_fprintf(stderr,"\nNo menu\n");
 
345
      return;
 
346
    }
 
347
 
 
348
    if(event->button==1)
 
349
      gtk_menu_popup (GTK_MENU(pd->menu),NULL,NULL,NULL,NULL,event->button,event->time);
 
350
    else
 
351
      gtk_menu_popdown(GTK_MENU(pd->menu));
 
352
 
 
353
}
 
354
 
 
355
/**
 
356
 * This function generates the popup menu
 
357
**/
 
358
void make_menu(plugin_data *pd){
 
359
 
 
360
  GtkTreeIter iter;
 
361
  gboolean valid;
 
362
  GSList *group=NULL;
 
363
  GtkWidget *menuitem;
 
364
  gchar *timername,*timerinfo;
 
365
  gchar itemtext[256];
 
366
  gint row_count;
 
367
 
 
368
 
 
369
  /* Destroy the existing one */
 
370
  if(pd->menu)
 
371
    gtk_widget_destroy(pd->menu);
 
372
 
 
373
  if(pd->menuarray)
 
374
    g_array_free(pd->menuarray,TRUE);
 
375
 
 
376
  pd->menu=gtk_menu_new();
 
377
  pd->menuarray=g_array_new(FALSE,TRUE,sizeof(menuitem));
 
378
 
 
379
 
 
380
  valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(pd->list), &iter);
 
381
  row_count=0;
 
382
 
 
383
  while (valid){
 
384
 
 
385
      /* Run through the list, read name and timer period info */
 
386
 
 
387
      /*g_fprintf(stderr,"\nMaking menuitem %d while selected is %d",row_count,pd->
 
388
                                selected);*/
 
389
      gtk_tree_model_get(GTK_TREE_MODEL(pd->list),&iter,1,&timername,2,&timerinfo,-1);
 
390
      g_snprintf(itemtext,255,"%s (%s)",timername,timerinfo);
 
391
      menuitem=gtk_radio_menu_item_new_with_label(group,itemtext);
 
392
      gtk_widget_show(menuitem);
 
393
      g_free(timername);
 
394
      g_free(timerinfo);
 
395
      group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (menuitem));
 
396
      g_signal_connect  (G_OBJECT(menuitem),"button_press_event",
 
397
            G_CALLBACK(timer_selected),pd);
 
398
      /* The selected timer is always active */
 
399
      if(row_count==pd->selected)
 
400
        gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),TRUE);
 
401
      /* others are disabled when timer or alarm is already running */
 
402
      else if(pd->timer_on || pd->alarm_repeating)
 
403
        gtk_widget_set_sensitive(GTK_WIDGET(menuitem),FALSE);
 
404
 
 
405
      gtk_menu_shell_append(GTK_MENU_SHELL(pd->menu),menuitem);
 
406
 
 
407
      /*g_fprintf(stderr,"\nAdding menuitem with label : %s",itemtext);*/
 
408
 
 
409
      /* We add the address of menuitem to the array */
 
410
      g_array_append_val(pd->menuarray,menuitem);
 
411
 
 
412
      valid = gtk_tree_model_iter_next (GTK_TREE_MODEL(pd->list), &iter);
 
413
      row_count++;
 
414
  }
 
415
 
 
416
  /* Horizontal line (empty item) */
 
417
  menuitem=gtk_menu_item_new();
 
418
  gtk_menu_shell_append(GTK_MENU_SHELL(pd->menu),menuitem);
 
419
  gtk_widget_show(menuitem);
 
420
 
 
421
  /* Start/stop menu item */
 
422
 
 
423
  if(!pd->alarm_repeating){
 
424
      if(pd->timer_on)
 
425
        menuitem=gtk_menu_item_new_with_label(_("Stop timer"));
 
426
      else
 
427
        menuitem=gtk_menu_item_new_with_label(_("Start timer"));
 
428
 
 
429
      gtk_menu_shell_append (GTK_MENU_SHELL(pd->menu),menuitem);
 
430
      g_signal_connect  (G_OBJECT(menuitem),"button_press_event",
 
431
                G_CALLBACK(start_stop_selected),pd);
 
432
      gtk_widget_show(menuitem);
 
433
  }
 
434
 
 
435
  /* Stop repeating alarm if so */
 
436
  if(pd->alarm_repeating) {
 
437
    menuitem=gtk_menu_item_new_with_label(_("Stop the alarm"));
 
438
 
 
439
    gtk_menu_shell_append   (GTK_MENU_SHELL(pd->menu),menuitem);
 
440
    g_signal_connect    (G_OBJECT(menuitem),"button_press_event",
 
441
            G_CALLBACK(stop_repeating_alarm),pd);
 
442
    gtk_widget_show(menuitem);
 
443
 
 
444
    gtk_widget_show(pd->menu);
 
445
  }
 
446
 
 
447
}
 
448
 
 
449
 
 
450
/**
 
451
 * Callback to the OK button in the Add window
 
452
**/
 
453
static void ok_add(GtkButton *button, gpointer data){
 
454
 
 
455
  alarm_data *adata = (alarm_data *)data;
 
456
  GtkTreeIter iter;
 
457
  gint t1,t2,t3,t;
 
458
  gchar timeinfo[16];
 
459
 
 
460
  /* Add item to the list */
 
461
  gtk_list_store_append(adata->pd->list,&iter);
 
462
 
 
463
  gtk_list_store_set(GTK_LIST_STORE(adata->pd->list),&iter,
 
464
            0,adata->pd->count,
 
465
            1,gtk_entry_get_text(GTK_ENTRY(adata->name)),
 
466
            3,gtk_entry_get_text(GTK_ENTRY(adata->command)),
 
467
            4,gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(adata->
 
468
                                    rb1)),-1);
 
469
  /* Item count goes up by one */
 
470
  adata->pd->count=adata->pd->count+1;
 
471
 
 
472
  /* If the h-m-s format was chosen, convert time to seconds,
 
473
     save the choice into the list */
 
474
  if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(adata->rb1))){
 
475
 
 
476
    t1=gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(adata->timeh));
 
477
    t2=gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(adata->timem));
 
478
    t3=gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(adata->times));
 
479
    t=t1*3600+t2*60+t3;
 
480
 
 
481
    gtk_list_store_set(GTK_LIST_STORE(adata->pd->list),&iter,5,t,-1);
 
482
    if(t1>0)
 
483
       g_snprintf(timeinfo,15,_("%dh %dm %ds"),t1,t2,t3);
 
484
    else if(t2>0)
 
485
       g_snprintf(timeinfo,15,_("%dm %ds"),t2,t3);
 
486
    else
 
487
       g_snprintf(timeinfo,15,_("%ds"),t3);
 
488
 
 
489
    gtk_list_store_set(GTK_LIST_STORE(adata->pd->list),&iter,2,timeinfo,-1);
 
490
  }
 
491
  else{ /* The 24h format. Save time in minutes */
 
492
 
 
493
    t1=gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(adata->time_h));
 
494
    t2=gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(adata->time_m));
 
495
    t=t1*60+t2;
 
496
    gtk_list_store_set(GTK_LIST_STORE(adata->pd->list),&iter,5,t,-1);
 
497
    g_snprintf(timeinfo,9,_("At %02d:%02d"),t1,t2);
 
498
    gtk_list_store_set(GTK_LIST_STORE(adata->pd->list),&iter,2,timeinfo,-1);
 
499
 
 
500
  }
 
501
 
 
502
  /* Update menu */
 
503
  make_menu(adata->pd);
 
504
 
 
505
  /* Free resources */
 
506
  gtk_widget_destroy(GTK_WIDGET(adata->window));
 
507
 
 
508
  g_free(adata);
 
509
}
 
510
 
 
511
 
 
512
 
 
513
/**
 
514
 * Callback for cancelling Add and Edit. Just closes the window :).
 
515
**/
 
516
static void cancel_add_edit(GtkButton *button, gpointer data){
 
517
 
 
518
  alarm_data *adata=(alarm_data *)data;
 
519
 
 
520
  gtk_widget_destroy(GTK_WIDGET(adata->window));
 
521
 
 
522
  g_free(adata);
 
523
 
 
524
}
 
525
 
 
526
 
 
527
/**
 
528
 * Callback for OK button on Edit window. See ok_add for comments.
 
529
**/
 
530
static void ok_edit(GtkButton *button, gpointer data){
 
531
 
 
532
  alarm_data *adata = (alarm_data *)data;
 
533
  GtkTreeIter iter;
 
534
  gint t1,t2,t3,t;
 
535
  gchar timeinfo[10];
 
536
 
 
537
  GtkTreeSelection *select;
 
538
  GtkTreeModel *model;
 
539
 
 
540
  select = gtk_tree_view_get_selection (GTK_TREE_VIEW (adata->pd->tree));
 
541
 
 
542
  if (gtk_tree_selection_get_selected (select, &model, &iter))
 
543
  {
 
544
     gtk_list_store_set(GTK_LIST_STORE(adata->pd->list),&iter,
 
545
            1,gtk_entry_get_text(GTK_ENTRY(adata->name)),
 
546
            3,gtk_entry_get_text(GTK_ENTRY(adata->command)),
 
547
            4,gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(adata->
 
548
                                    rb1)),-1);
 
549
     if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(adata->rb1))){
 
550
 
 
551
        t1=gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(adata->timeh));
 
552
        t2=gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(adata->timem));
 
553
        t3=gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(adata->times));
 
554
        t=t1*3600+t2*60+t3;
 
555
        gtk_list_store_set(GTK_LIST_STORE(adata->pd->list),&iter,5,t,-1);
 
556
       if(t1>0)
 
557
          g_snprintf(timeinfo,15,_("%dh %dm %ds"),t1,t2,t3);
 
558
       else if(t2>0)
 
559
          g_snprintf(timeinfo,15,_("%dm %ds"),t2,t3);
 
560
       else
 
561
          g_snprintf(timeinfo,15,_("%ds"),t3);
 
562
 
 
563
        gtk_list_store_set(GTK_LIST_STORE(adata->pd->list),&iter,2,timeinfo,-1);
 
564
      }
 
565
      else{
 
566
 
 
567
        t1=gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(adata->time_h));
 
568
        t2=gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(adata->time_m));
 
569
        t=t1*60+t2;
 
570
        gtk_list_store_set(GTK_LIST_STORE(adata->pd->list),&iter,5,t,-1);
 
571
        g_snprintf(timeinfo,9,_("At %02d:%02d"),t1,t2);
 
572
        gtk_list_store_set(GTK_LIST_STORE(adata->pd->list),&iter,2,timeinfo,-1);
 
573
 
 
574
      }
 
575
 
 
576
  }
 
577
 
 
578
  make_menu(adata->pd);
 
579
 
 
580
  gtk_widget_destroy(GTK_WIDGET(adata->window));
 
581
 
 
582
  g_free(adata);
 
583
}
 
584
 
 
585
/**
 
586
 * Callback when first radio button in dialog has been selected.
 
587
 */
 
588
static void alarmdialog_countdown_toggled (GtkButton *button, gpointer data)
 
589
{
 
590
  //plugin_data *pd = (plugin_data *)data;
 
591
  alarm_data *adata = (alarm_data *) data;
 
592
  gboolean active;
 
593
 
 
594
  active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
 
595
  gtk_widget_set_sensitive(GTK_WIDGET(adata->timeh), active);
 
596
  gtk_widget_set_sensitive(GTK_WIDGET(adata->timem), active);
 
597
  gtk_widget_set_sensitive(GTK_WIDGET(adata->times), active);
 
598
}
 
599
 
 
600
/**
 
601
 * Callback when second radio button in dialog has been selected.
 
602
 */
 
603
static void alarmdialog_alarmtime_toggled (GtkButton *button, gpointer data)
 
604
{
 
605
  alarm_data *adata = (alarm_data *) data;
 
606
  gboolean active;
 
607
 
 
608
  active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
 
609
  gtk_widget_set_sensitive(GTK_WIDGET(adata->time_h), active);
 
610
  gtk_widget_set_sensitive(GTK_WIDGET(adata->time_m), active);
 
611
}
 
612
 
 
613
/**
 
614
 * Callback to the Add button in options window.
 
615
 * Creates the Add window.
 
616
**/
 
617
static void add_edit_clicked (GtkButton *buttonn, gpointer data){
 
618
 
 
619
  plugin_data *pd = (plugin_data *)data;
 
620
 
 
621
  GtkWindow *window;
 
622
  GtkLabel *label;
 
623
  GtkEntry *name,*command;
 
624
  GtkSpinButton *timeh,*timem,*times,*time_h,*time_m;
 
625
  GtkRadioButton *rb1,*rb2;
 
626
  GtkWidget *hbox,*vbox,*button;
 
627
  alarm_data *adata=g_new(alarm_data,1);
 
628
  gchar *nc; gboolean is_cd; gint time;
 
629
  GtkTreeIter iter;
 
630
  GtkTreeSelection *select;
 
631
  GtkTreeModel *model;
 
632
 
 
633
  window = (GtkWindow *)gtk_window_new(GTK_WINDOW_TOPLEVEL);
 
634
 
 
635
  adata->window=window;
 
636
  adata->pd=pd;
 
637
 
 
638
  gtk_window_set_modal(GTK_WINDOW(window),TRUE);
 
639
 
 
640
  vbox=gtk_vbox_new(FALSE, BORDER);
 
641
  gtk_container_add(GTK_CONTAINER(window),vbox);
 
642
  gtk_container_set_border_width(GTK_CONTAINER(window), BORDER);
 
643
 
 
644
  /***********/
 
645
  hbox=gtk_hbox_new(FALSE, BORDER);
 
646
  gtk_box_pack_start(GTK_BOX(vbox),hbox,FALSE,FALSE, WIDGET_SPACING);
 
647
 
 
648
  label = (GtkLabel *)gtk_label_new (_("Name:"));
 
649
  name = (GtkEntry *) gtk_entry_new_with_max_length(1023);
 
650
  adata->name=name;
 
651
 
 
652
  gtk_box_pack_start(GTK_BOX(hbox),GTK_WIDGET(label),FALSE,FALSE,0);
 
653
  gtk_box_pack_start(GTK_BOX(hbox),GTK_WIDGET(name),TRUE,TRUE,0);
 
654
 
 
655
  /**********/
 
656
  rb1=(GtkRadioButton *)gtk_radio_button_new_with_label(NULL,_("Enter the countdown time"));
 
657
  g_signal_connect(G_OBJECT(rb1), "toggled", G_CALLBACK(alarmdialog_countdown_toggled), adata);
 
658
  rb2=(GtkRadioButton *)gtk_radio_button_new_with_label(gtk_radio_button_get_group
 
659
                    (rb1),_("Enter the time of alarm (24h format)"));
 
660
  g_signal_connect(G_OBJECT(rb2), "toggled", G_CALLBACK(alarmdialog_alarmtime_toggled), adata);
 
661
  adata->rb1=rb1;
 
662
 
 
663
  gtk_box_pack_start(GTK_BOX(vbox),GTK_WIDGET(rb1),TRUE,TRUE,0);
 
664
 
 
665
  hbox=gtk_hbox_new(FALSE,0);
 
666
  gtk_box_pack_start(GTK_BOX(vbox),GTK_WIDGET(hbox),TRUE,TRUE,0);
 
667
 
 
668
  timeh = (GtkSpinButton *)gtk_spin_button_new_with_range(0,23,1);
 
669
  gtk_box_pack_start(GTK_BOX(hbox),GTK_WIDGET(timeh),FALSE,FALSE, WIDGET_SPACING);
 
670
  adata->timeh=timeh;
 
671
  label = (GtkLabel *)gtk_label_new (_("h  "));
 
672
  gtk_box_pack_start(GTK_BOX(hbox),GTK_WIDGET(label),FALSE,FALSE, WIDGET_SPACING);
 
673
  timem = (GtkSpinButton *)gtk_spin_button_new_with_range(0,59,1);
 
674
  gtk_box_pack_start(GTK_BOX(hbox),GTK_WIDGET(timem),FALSE,FALSE,WIDGET_SPACING);
 
675
  adata->timem=timem;
 
676
  label = (GtkLabel *)gtk_label_new (_("m  "));
 
677
  gtk_box_pack_start(GTK_BOX(hbox),GTK_WIDGET(label),FALSE,FALSE,WIDGET_SPACING);
 
678
  times = (GtkSpinButton *)gtk_spin_button_new_with_range(0,59,1);
 
679
  gtk_box_pack_start(GTK_BOX(hbox),GTK_WIDGET(times),FALSE,FALSE,WIDGET_SPACING);
 
680
  adata->times=times;
 
681
  label = (GtkLabel *)gtk_label_new (_("s  "));
 
682
  gtk_box_pack_start(GTK_BOX(hbox),GTK_WIDGET(label),FALSE,FALSE,WIDGET_SPACING);
 
683
 
 
684
  label = (GtkLabel *)gtk_label_new (_("or"));
 
685
  gtk_box_pack_start(GTK_BOX(vbox),GTK_WIDGET(label),TRUE,TRUE,BORDER);
 
686
 
 
687
 
 
688
  gtk_box_pack_start(GTK_BOX(vbox),GTK_WIDGET(rb2),TRUE,TRUE,0);
 
689
 
 
690
  hbox=gtk_hbox_new(FALSE,WIDGET_SPACING);
 
691
  gtk_box_pack_start(GTK_BOX(vbox),GTK_WIDGET(hbox),TRUE,TRUE,0);
 
692
 
 
693
  time_h = (GtkSpinButton *)gtk_spin_button_new_with_range(0,23,1);
 
694
  gtk_box_pack_start(GTK_BOX(hbox),GTK_WIDGET(time_h),FALSE,FALSE,0);
 
695
  adata->time_h=time_h;
 
696
  label = (GtkLabel *)gtk_label_new (":");
 
697
  gtk_box_pack_start(GTK_BOX(hbox),GTK_WIDGET(label),FALSE,FALSE,WIDGET_SPACING);
 
698
  time_m = (GtkSpinButton *)gtk_spin_button_new_with_range(0,59,1);
 
699
  gtk_box_pack_start(GTK_BOX(hbox),GTK_WIDGET(time_m),FALSE,FALSE,WIDGET_SPACING);
 
700
  adata->time_m=time_m;
 
701
 
 
702
  /****************/
 
703
 
 
704
  hbox=gtk_hbox_new(FALSE, BORDER);
 
705
  gtk_box_pack_start(GTK_BOX(vbox),GTK_WIDGET(hbox),TRUE,TRUE,WIDGET_SPACING);
 
706
 
 
707
  label = (GtkLabel *)gtk_label_new (_("Command to run:"));
 
708
  gtk_box_pack_start(GTK_BOX(hbox),GTK_WIDGET(label),FALSE,FALSE,WIDGET_SPACING);
 
709
  command = (GtkEntry *)gtk_entry_new_with_max_length(1023);
 
710
  adata->command=command;
 
711
  gtk_box_pack_start(GTK_BOX(hbox),GTK_WIDGET(command),TRUE,TRUE,WIDGET_SPACING);
 
712
 
 
713
  /****************/
 
714
 
 
715
  //hbox=gtk_hbox_new(TRUE,0);
 
716
  hbox = gtk_hbutton_box_new();
 
717
  gtk_button_box_set_layout(GTK_BUTTON_BOX(hbox), GTK_BUTTONBOX_END);
 
718
  gtk_button_box_set_spacing(GTK_BUTTON_BOX(hbox), BORDER);
 
719
  gtk_box_pack_start(GTK_BOX(vbox),hbox,TRUE,TRUE,WIDGET_SPACING);
 
720
  //gtk_container_set_border_width(GTK_CONTAINER(hbox), BORDER);
 
721
 
 
722
  button=gtk_button_new_from_stock(GTK_STOCK_CANCEL);
 
723
  gtk_box_pack_start(GTK_BOX(hbox),button,TRUE,TRUE,0);
 
724
  g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(cancel_add_edit),adata);
 
725
 
 
726
  button=gtk_button_new_from_stock(GTK_STOCK_OK);
 
727
  gtk_box_pack_start(GTK_BOX(hbox),button,TRUE,TRUE,0);
 
728
  if(GTK_WIDGET(buttonn)==pd->buttonadd)
 
729
     g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(ok_add),adata);
 
730
  else
 
731
     g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(ok_edit),adata);
 
732
 
 
733
 
 
734
 
 
735
  /* If this is the add window, we're done */
 
736
  if(GTK_WIDGET(buttonn)==pd->buttonadd) {
 
737
    gtk_window_set_title(window,_("Add new alarm"));
 
738
    gtk_widget_show_all(GTK_WIDGET(window));
 
739
    alarmdialog_alarmtime_toggled(GTK_BUTTON(rb2), adata);
 
740
    return;
 
741
  }
 
742
 
 
743
  /* Else fill the values in the boxes with the current choices */
 
744
  select = gtk_tree_view_get_selection (GTK_TREE_VIEW (pd->tree));
 
745
  /*gtk_tree_selection_set_mode (select, GTK_SELECTION_SINGLE);*/
 
746
 
 
747
  if (gtk_tree_selection_get_selected (select, &model, &iter)){
 
748
      gtk_tree_model_get(model,&iter,1,&nc,-1);
 
749
      gtk_entry_set_text(GTK_ENTRY(name),nc);
 
750
      g_free(nc);
 
751
 
 
752
      gtk_tree_model_get(model,&iter,3,&nc,-1);
 
753
      gtk_entry_set_text(GTK_ENTRY(command),nc);
 
754
      g_free(nc);
 
755
 
 
756
      gtk_tree_model_get(model,&iter,4,&is_cd,-1);
 
757
      gtk_tree_model_get(model,&iter,5,&time,-1);
 
758
 
 
759
      if(is_cd){
 
760
 
 
761
         gtk_spin_button_set_value(GTK_SPIN_BUTTON(timeh),time/3600);
 
762
         gtk_spin_button_set_value(GTK_SPIN_BUTTON(timem),(time%3600)/60);
 
763
         gtk_spin_button_set_value(GTK_SPIN_BUTTON(times),time%60);
 
764
         alarmdialog_alarmtime_toggled(GTK_BUTTON(rb2), adata);
 
765
         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(rb1),TRUE);
 
766
      }
 
767
      else{
 
768
 
 
769
         gtk_spin_button_set_value(GTK_SPIN_BUTTON(time_h),time/60);
 
770
         gtk_spin_button_set_value(GTK_SPIN_BUTTON(time_m),time%60);
 
771
         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(rb2),TRUE); // active by default
 
772
      }
 
773
 
 
774
  }
 
775
 
 
776
  gtk_window_set_title(window,_("Edit alarm"));
 
777
  gtk_widget_show_all(GTK_WIDGET(window));
 
778
 
 
779
}
 
780
 
 
781
 
 
782
/**
 
783
 * Calllback for the remove button in the options
 
784
**/
 
785
static void remove_clicked(GtkButton *button, gpointer data){
 
786
 
 
787
  plugin_data *pd = (plugin_data *)data;
 
788
 
 
789
  GtkTreeIter iter,iter_remove;
 
790
  GtkTreeSelection *select;
 
791
  GtkTreePath *path;
 
792
  GtkTreeModel *model;
 
793
  gboolean valid;
 
794
  gint row_count;
 
795
 
 
796
  /* Get the selected row */
 
797
  select=gtk_tree_view_get_selection(GTK_TREE_VIEW(pd->tree));
 
798
 
 
799
  valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(pd->list), &iter);
 
800
 
 
801
  row_count=0;
 
802
 
 
803
  while (valid){
 
804
 
 
805
     /* Re-index the other rows */
 
806
 
 
807
     /* The selected one is removed, the corresponding menuitem array is also
 
808
        updated */
 
809
     if(gtk_tree_selection_iter_is_selected(select,&iter)){
 
810
       /*g_fprintf(stderr,"\n Removing node %d ...\n", row_count);*/
 
811
       iter_remove=iter; /* Mark to be deleted */
 
812
       g_array_remove_index(pd->menuarray,row_count);
 
813
       if(pd->selected==row_count) /* Update the index of the selected item */
 
814
          pd->selected=0; /* If the selected is deleted, new selected one is the first
 
815
                one. The first radiomenuitem gets activated anyway */
 
816
       else if(pd->selected > row_count)
 
817
          pd->selected=pd->selected-1;  /* Those coming after are shifted one behind */
 
818
     }
 
819
     else{
 
820
       /* Save new index on the remaning ones */
 
821
       gtk_list_store_set (pd->list, &iter, 0,row_count, -1);
 
822
       row_count++;
 
823
    }
 
824
 
 
825
    valid = gtk_tree_model_iter_next (GTK_TREE_MODEL(pd->list), &iter);
 
826
 
 
827
  }
 
828
 
 
829
  /* Remove the marked one */
 
830
  gtk_list_store_remove (pd->list, &iter_remove);
 
831
 
 
832
  /* Update item count and menu */
 
833
  pd->count=row_count;
 
834
 
 
835
  make_menu(pd);
 
836
 
 
837
}
 
838
 
 
839
 
 
840
/**
 
841
 * Adds the progressbar, taking into account the orientation.
 
842
 * pd->pbar is not destroyed, just reparented (saves fraction setting code etc.).
 
843
**/
 
844
static void add_pbar(XfcePanelPlugin *plugin, plugin_data *pd){
 
845
 
 
846
  gtk_widget_hide(pd->eventbox);
 
847
 
 
848
  xfce_textdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR, "UTF-8");
 
849
 
 
850
  /* Always true except at initialization */
 
851
  if(pd->box){
 
852
    g_object_ref(G_OBJECT(pd->pbar));
 
853
    gtk_container_remove(GTK_CONTAINER(pd->box),pd->pbar);
 
854
    gtk_widget_destroy(pd->box);
 
855
  }
 
856
 
 
857
  /* vertical bar */
 
858
  if(xfce_panel_plugin_get_orientation(plugin)==GTK_ORIENTATION_HORIZONTAL){
 
859
    pd->box=gtk_hbox_new(TRUE,0);
 
860
    gtk_container_add(GTK_CONTAINER(pd->eventbox),pd->box);
 
861
    gtk_progress_bar_set_orientation    (GTK_PROGRESS_BAR(pd->
 
862
                    pbar),GTK_PROGRESS_BOTTOM_TO_TOP);
 
863
    gtk_widget_set_size_request(GTK_WIDGET(pd->pbar),PBAR_THICKNESS,0);
 
864
    gtk_box_pack_start(GTK_BOX(pd->box),gtk_vseparator_new(),FALSE,FALSE,0);
 
865
    gtk_box_pack_start(GTK_BOX(pd->box),pd->pbar,FALSE,FALSE,0);
 
866
    gtk_box_pack_start(GTK_BOX(pd->box),gtk_vseparator_new(),FALSE,FALSE,0);
 
867
 
 
868
  }
 
869
  else{ /* horizontal bar */
 
870
    pd->box=gtk_vbox_new(TRUE,0);
 
871
    gtk_container_add(GTK_CONTAINER(pd->eventbox),pd->box);
 
872
    gtk_progress_bar_set_orientation    (GTK_PROGRESS_BAR(pd->
 
873
                    pbar),GTK_PROGRESS_LEFT_TO_RIGHT);
 
874
    gtk_widget_set_size_request(GTK_WIDGET(pd->pbar),0,PBAR_THICKNESS);
 
875
    gtk_box_pack_start(GTK_BOX(pd->box),gtk_hseparator_new(),FALSE,FALSE,0);
 
876
    gtk_box_pack_start(GTK_BOX(pd->box),pd->pbar,FALSE,FALSE,0);
 
877
    gtk_box_pack_start(GTK_BOX(pd->box),gtk_hseparator_new(),FALSE,FALSE,0);
 
878
 
 
879
  }
 
880
 
 
881
  gtk_widget_show_all(pd->eventbox);
 
882
}
 
883
 
 
884
/**
 
885
 * Callback for orientation change of panel. Just calls add_pbar.
 
886
**/
 
887
static void orient_change(XfcePanelPlugin *plugin, GtkOrientation orient, plugin_data *pd){
 
888
 
 
889
  add_pbar(plugin,pd);
 
890
}
 
891
 
 
892
/**
 
893
 * Loads the list from a keyfile, then saves them in a list_store
 
894
**/
 
895
static void load_settings(plugin_data *pd)
 
896
{
 
897
 
 
898
  gchar groupname[8];
 
899
  const gchar *timerstring;
 
900
  gint groupnum,time;
 
901
  gboolean is_cd;
 
902
  GtkTreeIter iter;
 
903
  XfceRc *rc;
 
904
 
 
905
  //if ((file=xfce_panel_plugin_lookup_rc_file (pd->base)) != NULL) {
 
906
 
 
907
 
 
908
      if (g_file_test(pd->configfile,G_FILE_TEST_EXISTS)) {
 
909
 
 
910
          rc = xfce_rc_simple_open (pd->configfile, TRUE);
 
911
 
 
912
 
 
913
          if (rc != NULL)
 
914
          {
 
915
 
 
916
 
 
917
          groupnum=0;
 
918
          g_sprintf(groupname,"G0");
 
919
 
 
920
 
 
921
          while(xfce_rc_has_group(rc,groupname)){
 
922
 
 
923
                 xfce_rc_set_group(rc,groupname);
 
924
 
 
925
             /*g_fprintf(stderr,"\nLoading item %d\n",groupnum);*/
 
926
             gtk_list_store_append(pd->list,&iter);
 
927
 
 
928
             timerstring=(gchar *)xfce_rc_read_entry(rc,"timername","No name");
 
929
             gtk_list_store_set(pd->list,&iter,0,groupnum,1,timerstring,-1);
 
930
             /* g_free(timerstring); */ /* Entries read are not freed ! */
 
931
 
 
932
             timerstring=(gchar *)xfce_rc_read_entry(rc,"timercommand","");
 
933
             gtk_list_store_set(pd->list,&iter,3,timerstring,-1);
 
934
             /*g_fprintf(stderr,"\nLoaded timer command ==> %s... with length %d",
 
935
                            timerstring,strlen(timerstring));*/
 
936
             /*g_free(timerstring);*/
 
937
 
 
938
             timerstring=(gchar *)xfce_rc_read_entry(rc,"timerinfo","");
 
939
             gtk_list_store_set(pd->list,&iter,2,timerstring,-1);
 
940
 
 
941
             /*g_free(timerstring);*/
 
942
 
 
943
             is_cd=xfce_rc_read_bool_entry(rc,"is_countdown",TRUE);
 
944
             time=xfce_rc_read_int_entry(rc,"time",0);
 
945
 
 
946
             gtk_list_store_set(pd->list,&iter,4,is_cd,5,time,-1);
 
947
 
 
948
             groupnum++;
 
949
             g_snprintf(groupname,5,"G%d",groupnum);
 
950
 
 
951
          } /* end of while loop */
 
952
 
 
953
          pd->count=groupnum;
 
954
 
 
955
 
 
956
          /* Read other options */
 
957
          if(xfce_rc_has_group(rc,"others")){
 
958
            xfce_rc_set_group(rc,"others");
 
959
            pd->nowin_if_alarm= xfce_rc_read_bool_entry
 
960
                (rc,"nowin_if_alarm",FALSE);
 
961
            pd->repeat_alarm= xfce_rc_read_bool_entry
 
962
                (rc,"repeat_alarm",FALSE);
 
963
            pd->repetitions= xfce_rc_read_int_entry
 
964
                (rc,"repetitions",1);
 
965
            pd->repeat_interval= xfce_rc_read_int_entry
 
966
                (rc,"repeat_interval",10);
 
967
 
 
968
          }
 
969
 
 
970
          add_pbar(pd->base,pd);
 
971
 
 
972
             xfce_rc_close(rc);
 
973
          }
 
974
 
 
975
//   }
 
976
  }
 
977
 
 
978
}
 
979
 
 
980
 
 
981
/**
 
982
 * Saves the list to a keyfile, backup a permanent copy
 
983
**/
 
984
static void save_settings(XfcePanelPlugin *plugin, plugin_data *pd){
 
985
 
 
986
  gchar *timername,*timercommand,*timerinfo;
 
987
  gint time;
 
988
  gboolean is_cd;
 
989
  gchar settings[1024];
 
990
  gchar settingsbak[1024];
 
991
  gchar line[1024];
 
992
  gchar groupname[8];
 
993
  GtkTreeIter iter;
 
994
  gboolean valid;
 
995
  gint row_count;
 
996
  gsize size;
 
997
 
 
998
  FILE *conffile;
 
999
 
 
1000
  XfceRc *rc;
 
1001
  gchar *file, *contents=NULL;
 
1002
 
 
1003
  if (!(file = xfce_panel_plugin_save_location (plugin, TRUE)))
 
1004
    return;
 
1005
 
 
1006
  // We do this to start a fresh config file, otherwise if the old config file is longer,
 
1007
  // the tail will not get truncated. See
 
1008
  // http://bugzilla.xfce.org/show_bug.cgi?id=2647
 
1009
  // for a related bug report.
 
1010
  conffile = fopen(file,"w");
 
1011
  if (conffile)
 
1012
    fclose(conffile);
 
1013
 
 
1014
  rc = xfce_rc_simple_open (file, FALSE);
 
1015
 
 
1016
  if (!rc)
 
1017
    return;
 
1018
 
 
1019
 
 
1020
 
 
1021
  /*g_fprintf(stderr,"\n Running write\n");*/
 
1022
 
 
1023
  valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(pd->list), &iter);
 
1024
 
 
1025
  row_count=0;
 
1026
 
 
1027
  while (valid){
 
1028
 
 
1029
      gtk_tree_model_get (GTK_TREE_MODEL(pd->list), &iter,
 
1030
            1, &timername,
 
1031
            2, &timerinfo,
 
1032
            3, &timercommand,
 
1033
            4, &is_cd,
 
1034
            5, &time, -1);
 
1035
 
 
1036
      g_snprintf(groupname,7,"G%d",row_count);
 
1037
      xfce_rc_set_group(rc,groupname);
 
1038
 
 
1039
      xfce_rc_write_entry(rc,"timername",timername);
 
1040
 
 
1041
      xfce_rc_write_int_entry(rc,"time",time);
 
1042
 
 
1043
      xfce_rc_write_entry(rc,"timercommand",timercommand);
 
1044
 
 
1045
      xfce_rc_write_entry(rc,"timerinfo",timerinfo);
 
1046
 
 
1047
      xfce_rc_write_bool_entry(rc,"is_countdown",is_cd);
 
1048
 
 
1049
      g_free(timername);
 
1050
      g_free(timercommand);
 
1051
      g_free(timerinfo);
 
1052
 
 
1053
      row_count ++;
 
1054
      valid = gtk_tree_model_iter_next (GTK_TREE_MODEL(pd->list), &iter);
 
1055
  }
 
1056
 
 
1057
 
 
1058
  /* save the other options */
 
1059
  xfce_rc_set_group(rc,"others");
 
1060
 
 
1061
  xfce_rc_write_bool_entry(rc,"nowin_if_alarm",pd->nowin_if_alarm);
 
1062
  xfce_rc_write_bool_entry(rc,"repeat_alarm",pd->repeat_alarm);
 
1063
  xfce_rc_write_int_entry(rc,"repetitions",pd->repetitions);
 
1064
  xfce_rc_write_int_entry(rc,"repeat_interval",pd->repeat_interval);
 
1065
 
 
1066
  xfce_rc_close(rc);
 
1067
 
 
1068
  conffile = fopen (pd->configfile,"w");
 
1069
  /* We backup a permanent copy, which we'll use to load settings */
 
1070
  if (conffile && g_file_get_contents(file,&contents,NULL,NULL)){
 
1071
    fputs(contents,conffile);
 
1072
    fclose(conffile);
 
1073
  }
 
1074
 
 
1075
  g_free(file);
 
1076
  if(contents)
 
1077
    g_free(contents);
 
1078
 
 
1079
}
 
1080
 
 
1081
 
 
1082
/**
 
1083
 * Activates the Edit and Remove buttons when an item in the list is selected
 
1084
**/
 
1085
 
 
1086
static void tree_selected (GtkTreeSelection *select, gpointer data){
 
1087
 
 
1088
  plugin_data *pd=(plugin_data *)data;
 
1089
 
 
1090
  gtk_widget_set_sensitive(pd->buttonedit,TRUE);
 
1091
  gtk_widget_set_sensitive(pd->buttonremove,TRUE);
 
1092
 
 
1093
}
 
1094
 
 
1095
 
 
1096
/******************/
 
1097
/* plugin_free    */
 
1098
/******************/
 
1099
 
 
1100
static void
 
1101
plugin_free (XfcePanelPlugin *plugin, plugin_data *pd)
 
1102
{
 
1103
  /*plugin_data *pd;
 
1104
 
 
1105
  g_return_if_fail(ctrl != NULL);
 
1106
  g_return_if_fail(ctrl-> data != NULL);
 
1107
 
 
1108
  pd = (plugin_data*) ctrl->data;*/
 
1109
 
 
1110
  /* remove timeouts */
 
1111
  if (pd->timeout!=0) g_source_remove(pd->timeout);
 
1112
  if (pd->repeat_timeout!=0) g_source_remove(pd->repeat_timeout);
 
1113
 
 
1114
  /*save_settings(pd);*/
 
1115
 
 
1116
  if(pd->timer)
 
1117
    g_timer_destroy(pd->timer);
 
1118
 
 
1119
  if(pd->timeout_command)
 
1120
    g_free(pd->timeout_command);
 
1121
 
 
1122
  if(pd->configfile)
 
1123
    g_free(pd->configfile);
 
1124
 
 
1125
  gtk_object_destroy(GTK_OBJECT(pd->tip));
 
1126
 
 
1127
  if(pd->menuarray)
 
1128
    g_array_free(pd->menuarray,TRUE);
 
1129
 
 
1130
  /* destroy all widgets */
 
1131
  gtk_widget_destroy(GTK_WIDGET(pd->eventbox));
 
1132
 
 
1133
  /* destroy the tooltips */
 
1134
  /*gtk_object_destroy(GTK_OBJECT(pd->tip));*/
 
1135
 
 
1136
  //if(G_IS_OBJECT(pd->list))
 
1137
  //  g_free(pd->list);
 
1138
  //else
 
1139
  //  g_fprintf(stderr,"\npd->list is non-object");
 
1140
 
 
1141
  /* free the plugin data structure */
 
1142
  g_free(pd);
 
1143
 
 
1144
  gtk_main_quit();
 
1145
}
 
1146
 
 
1147
/***************************/
 
1148
/* options dialog response */
 
1149
/***************************/
 
1150
static void
 
1151
options_dialog_response (GtkWidget *dlg, int reponse, plugin_data *pd)
 
1152
{
 
1153
    gtk_widget_destroy (dlg);
 
1154
    xfce_panel_plugin_unblock_menu (pd->base);
 
1155
    save_settings(pd->base,pd);
 
1156
}
 
1157
 
 
1158
/********************************/
 
1159
/* nowin_if_alarm toggle callback */
 
1160
/********************************/
 
1161
static void toggle_nowin_if_alarm(GtkToggleButton *button, gpointer data){
 
1162
 
 
1163
  plugin_data *pd=(plugin_data *)data;
 
1164
 
 
1165
  pd->nowin_if_alarm = gtk_toggle_button_get_active(button);
 
1166
 
 
1167
}
 
1168
 
 
1169
/***************************************/
 
1170
/* toggle_repeat_alarm toggle callback */
 
1171
/***************************************/
 
1172
static void toggle_repeat_alarm(GtkToggleButton *button, gpointer data){
 
1173
 
 
1174
  plugin_data *pd=(plugin_data *)data;
 
1175
 
 
1176
  pd->repeat_alarm = gtk_toggle_button_get_active(button);
 
1177
  gtk_widget_set_sensitive(GTK_WIDGET(pd->spin_interval), pd->repeat_alarm);
 
1178
  gtk_widget_set_sensitive(GTK_WIDGET(pd->spin_repeat), pd->repeat_alarm);
 
1179
 
 
1180
  gtk_widget_set_sensitive(pd->repeat_alarm_box,pd->repeat_alarm);
 
1181
 
 
1182
}
 
1183
 
 
1184
/***************************************/
 
1185
/* Spinbutton 1 (#of alarm repetitions */
 
1186
/* value change callback               */
 
1187
/***************************************/
 
1188
static void spin1_changed(GtkSpinButton *button, gpointer data){
 
1189
 
 
1190
  plugin_data *pd=(plugin_data *)data;
 
1191
 
 
1192
  pd->repetitions = gtk_spin_button_get_value_as_int(button);
 
1193
 
 
1194
}
 
1195
 
 
1196
/***************************************/
 
1197
/* Spinbutton 1 (alarm repetition      */
 
1198
/* interval) value change callback     */
 
1199
/***************************************/
 
1200
static void spin2_changed(GtkSpinButton *button, gpointer data){
 
1201
 
 
1202
  plugin_data *pd=(plugin_data *)data;
 
1203
 
 
1204
  pd->repeat_interval = gtk_spin_button_get_value_as_int(button);
 
1205
 
 
1206
}
 
1207
 
 
1208
/******************/
 
1209
/* options dialog */
 
1210
/******************/
 
1211
static void plugin_create_options (XfcePanelPlugin *plugin,plugin_data *pd) {
 
1212
 
 
1213
  GtkWidget *vbox=gtk_vbox_new(FALSE,0); /*outermost box */
 
1214
  GtkWidget *hbox=gtk_hbox_new(FALSE,0); /* holds the treeview and buttons */
 
1215
  GtkWidget *buttonbox,*button,*sw,*tree,*spinbutton;
 
1216
  GtkTreeSelection *select;
 
1217
  GtkCellRenderer *renderer;
 
1218
  GtkTreeViewColumn *column;
 
1219
  GtkTreeIter iter;
 
1220
 
 
1221
  GtkWidget *dlg, *header;
 
1222
 
 
1223
 
 
1224
  xfce_panel_plugin_block_menu (plugin);
 
1225
 
 
1226
  dlg = gtk_dialog_new_with_buttons (_("Xfce 4 Timer Plugin"),
 
1227
              GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (plugin))),
 
1228
              GTK_DIALOG_DESTROY_WITH_PARENT |
 
1229
              GTK_DIALOG_NO_SEPARATOR,
 
1230
              GTK_STOCK_CLOSE, GTK_RESPONSE_OK,
 
1231
              NULL);
 
1232
 
 
1233
  g_signal_connect (dlg, "response", G_CALLBACK (options_dialog_response),
 
1234
                    pd);
 
1235
 
 
1236
  gtk_container_set_border_width (GTK_CONTAINER(dlg), BORDER);
 
1237
 
 
1238
  header = xfce_create_header (NULL, _("Xfce4 Timer Options"));
 
1239
  gtk_widget_set_size_request (GTK_BIN (header)->child, 200, 32);
 
1240
  //gtk_container_set_border_width (GTK_CONTAINER (header), 6);
 
1241
  gtk_widget_show (header);
 
1242
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), header,
 
1243
                      FALSE, TRUE, 0);
 
1244
 
 
1245
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), vbox,
 
1246
                      TRUE, TRUE, WIDGET_SPACING);
 
1247
 
 
1248
 
 
1249
  hbox = gtk_hbox_new(FALSE, BORDER);
 
1250
  gtk_box_pack_start(GTK_BOX(vbox),hbox,TRUE,TRUE,BORDER);
 
1251
 
 
1252
  sw = gtk_scrolled_window_new (NULL, NULL);
 
1253
 
 
1254
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
 
1255
                                           GTK_SHADOW_ETCHED_IN);
 
1256
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
 
1257
                 GTK_POLICY_AUTOMATIC,
 
1258
                 GTK_POLICY_AUTOMATIC);
 
1259
 
 
1260
  gtk_box_pack_start(GTK_BOX(hbox),sw,TRUE,TRUE,0);
 
1261
 
 
1262
  tree=gtk_tree_view_new_with_model(GTK_TREE_MODEL(pd->list));
 
1263
  pd->tree=tree;
 
1264
  gtk_tree_view_set_rules_hint  (GTK_TREE_VIEW (tree), TRUE);
 
1265
  gtk_tree_selection_set_mode   (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree)),
 
1266
                 GTK_SELECTION_SINGLE);
 
1267
 
 
1268
  renderer = gtk_cell_renderer_text_new ();
 
1269
 
 
1270
  column = gtk_tree_view_column_new_with_attributes (_("Timer name"), renderer,
 
1271
                            "text", 1, NULL);
 
1272
  gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);
 
1273
 
 
1274
 
 
1275
  column = gtk_tree_view_column_new_with_attributes (_("Countdown period /\nAlarm time"),
 
1276
                            renderer, "text", 2, NULL);
 
1277
  gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);
 
1278
 
 
1279
  column = gtk_tree_view_column_new_with_attributes (_("Alarm command"), renderer,
 
1280
                            "text", 3, NULL);
 
1281
  gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);
 
1282
 
 
1283
 
 
1284
  if(tree)
 
1285
     gtk_container_add(GTK_CONTAINER(sw),tree);
 
1286
  else
 
1287
     g_fprintf(stderr,"\n pd->tree is NULL\n");
 
1288
 
 
1289
  gtk_widget_set_size_request(GTK_WIDGET(sw),350,250);
 
1290
 
 
1291
  select = gtk_tree_view_get_selection (GTK_TREE_VIEW (pd->tree));
 
1292
  gtk_tree_selection_set_mode (select, GTK_SELECTION_SINGLE);
 
1293
  g_signal_connect  (G_OBJECT (select), "changed",
 
1294
            G_CALLBACK(tree_selected), pd);
 
1295
 
 
1296
 
 
1297
  buttonbox=gtk_vbutton_box_new();
 
1298
  gtk_button_box_set_layout(GTK_BUTTON_BOX(buttonbox),GTK_BUTTONBOX_START);
 
1299
  gtk_button_box_set_spacing(GTK_BUTTON_BOX(buttonbox),WIDGET_SPACING<<1);
 
1300
  gtk_box_pack_start(GTK_BOX(hbox),buttonbox,FALSE,FALSE,0);
 
1301
 
 
1302
  button = gtk_button_new_from_stock (GTK_STOCK_ADD);
 
1303
  pd->buttonadd=button;
 
1304
  gtk_box_pack_start(GTK_BOX (buttonbox), button, FALSE, FALSE,WIDGET_SPACING<<1);
 
1305
  gtk_widget_set_sensitive(button,TRUE);
 
1306
  g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK(add_edit_clicked),                                     pd);
 
1307
 
 
1308
 
 
1309
  button = gtk_button_new_from_stock (GTK_STOCK_EDIT);
 
1310
  pd->buttonedit=button;
 
1311
  gtk_box_pack_start(GTK_BOX (buttonbox), button, FALSE, FALSE,WIDGET_SPACING<<1);
 
1312
  gtk_widget_set_sensitive(button,FALSE);
 
1313
  g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK(add_edit_clicked),                                     pd);
 
1314
 
 
1315
  button = gtk_button_new_from_stock (GTK_STOCK_REMOVE);
 
1316
  pd->buttonremove=button;
 
1317
  gtk_box_pack_start(GTK_BOX (buttonbox), button, FALSE, FALSE,WIDGET_SPACING);
 
1318
  gtk_widget_set_sensitive(button,FALSE);
 
1319
  g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK(remove_clicked),                                       pd);
 
1320
 
 
1321
  gtk_box_pack_start(GTK_BOX(vbox),gtk_hseparator_new(),FALSE,FALSE,BORDER);
 
1322
 
 
1323
  button=gtk_check_button_new_with_label(_("Don't display a warning  if an alarm command is set"));
 
1324
  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button),pd->nowin_if_alarm);
 
1325
  g_signal_connect(G_OBJECT(button),"toggled",G_CALLBACK(toggle_nowin_if_alarm),pd);
 
1326
  gtk_box_pack_start(GTK_BOX(vbox),button,FALSE,FALSE,WIDGET_SPACING);
 
1327
 
 
1328
 
 
1329
 
 
1330
  button=gtk_check_button_new_with_label(_("Repeat the alarm command:"));
 
1331
  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button),pd->repeat_alarm);
 
1332
  g_signal_connect(G_OBJECT(button),"toggled",G_CALLBACK(toggle_repeat_alarm),pd);
 
1333
  gtk_box_pack_start(GTK_BOX(vbox),button,FALSE,FALSE,WIDGET_SPACING);
 
1334
 
 
1335
  hbox = gtk_hbox_new (FALSE,0);
 
1336
  pd->repeat_alarm_box=hbox;
 
1337
  gtk_box_pack_start(GTK_BOX(hbox),gtk_label_new(_("Number of repetitions")),FALSE,FALSE,0);
 
1338
  spinbutton=gtk_spin_button_new_with_range(1,50,1);
 
1339
  pd->spin_repeat=spinbutton;
 
1340
  gtk_spin_button_set_value(GTK_SPIN_BUTTON(spinbutton), pd->repetitions);
 
1341
  g_signal_connect(G_OBJECT(spinbutton),"value-changed", G_CALLBACK(spin1_changed), pd);
 
1342
  gtk_box_pack_start(GTK_BOX(hbox),spinbutton,FALSE,FALSE,0);
 
1343
  gtk_box_pack_start(GTK_BOX(hbox),gtk_label_new(_("  Time interval (sec.)")),FALSE,FALSE,0);
 
1344
  spinbutton=gtk_spin_button_new_with_range(1,600,1);
 
1345
  pd->spin_interval=spinbutton;
 
1346
  gtk_box_pack_start(GTK_BOX(hbox),spinbutton,FALSE,FALSE,0);
 
1347
  gtk_spin_button_set_value(GTK_SPIN_BUTTON(spinbutton), pd->repeat_interval);
 
1348
  g_signal_connect(G_OBJECT(spinbutton),"value-changed", G_CALLBACK(spin2_changed), pd);
 
1349
 
 
1350
  gtk_box_pack_start(GTK_BOX(vbox),hbox,FALSE,FALSE,WIDGET_SPACING);
 
1351
  gtk_widget_set_sensitive(hbox,pd->repeat_alarm);
 
1352
 
 
1353
 
 
1354
 
 
1355
  gtk_widget_show_all(GTK_WIDGET(dlg));
 
1356
 
 
1357
}
 
1358
 
 
1359
 
 
1360
/**
 
1361
 * create_sample_control
 
1362
 *
 
1363
 * Create a new instance of the plugin.
 
1364
 *
 
1365
 * @control : #Control parent container
 
1366
 *
 
1367
 * Returns %TRUE on success, %FALSE on failure.
 
1368
 **/
 
1369
static gboolean
 
1370
create_plugin_control (XfcePanelPlugin *plugin)
 
1371
{
 
1372
 
 
1373
  GtkWidget *base,*menu,*socket,*menuitem,*box,*pbar2;
 
1374
  GtkTooltips *tooltip;
 
1375
  char command[1024];
 
1376
  gchar *filename,*pathname;
 
1377
 
 
1378
 
 
1379
 
 
1380
  plugin_data *pd=g_new(plugin_data,1);
 
1381
 
 
1382
  pd->base=plugin;
 
1383
  pd->count=0;
 
1384
  pd->pbar=gtk_progress_bar_new();
 
1385
  pd->list=gtk_list_store_new(6,
 
1386
         G_TYPE_INT,     /* Column 0: Index */
 
1387
         G_TYPE_STRING,  /* Column 1: Name */
 
1388
         G_TYPE_STRING,  /* Column 2: Timer period/alarm time */
 
1389
         G_TYPE_STRING,  /* Command to run */
 
1390
         G_TYPE_BOOLEAN, /* TRUE= Is countdown, i.e. h-m-s format.
 
1391
                    FALSE= 24h format */
 
1392
         G_TYPE_INT);    /* Timer period in seconds if countdown.
 
1393
                    Alarm time in minutes if 24h format is used,
 
1394
                    (i.e. 60 x Hr + Min) */
 
1395
 
 
1396
  pd->eventbox=gtk_event_box_new();
 
1397
  pd->box=NULL;
 
1398
  pd->timer_on=FALSE;
 
1399
  pd->timeout=0;
 
1400
  pd->buttonadd=NULL;
 
1401
  pd->buttonedit=NULL;
 
1402
  pd->buttonremove=NULL;
 
1403
  pd->menu=NULL;
 
1404
  pd->menuarray=NULL;
 
1405
  pd->selected=0;
 
1406
  pd->tip=gtk_tooltips_new();
 
1407
  pd->timeout_command=NULL;
 
1408
  pd->timer=NULL;
 
1409
  pd->nowin_if_alarm=FALSE;
 
1410
  pd->repeat_alarm=FALSE;
 
1411
  pd->repeat_alarm_box=NULL;
 
1412
  pd->repetitions=1;
 
1413
  pd->rem_repetitions=1;
 
1414
  pd->repeat_interval=10;
 
1415
  pd->alarm_repeating=FALSE;
 
1416
  pd->repeat_timeout=0;
 
1417
 
 
1418
  gtk_tooltips_set_tip(pd->tip, GTK_WIDGET(plugin), "", NULL);
 
1419
  gtk_tooltips_disable(pd->tip);
 
1420
 
 
1421
 
 
1422
  g_object_ref(pd->list);
 
1423
 
 
1424
  filename = xfce_panel_plugin_save_location (pd->base,TRUE);
 
1425
  pathname = g_path_get_dirname (filename);
 
1426
  pd->configfile = g_strconcat (pathname,"/XfceTimer.rc",NULL);
 
1427
  g_free(filename);
 
1428
  g_free(pathname);
 
1429
 
 
1430
  load_settings(pd);
 
1431
 
 
1432
  make_menu(pd);
 
1433
 
 
1434
  g_signal_connect  (G_OBJECT(pd->eventbox), "button_press_event",
 
1435
            G_CALLBACK(pbar_clicked), pd);
 
1436
 
 
1437
  gtk_progress_bar_set_bar_style    (GTK_PROGRESS_BAR(pd->pbar),
 
1438
                    GTK_PROGRESS_CONTINUOUS);
 
1439
  gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(pd->pbar),0);
 
1440
 
 
1441
  add_pbar(pd->base,pd);
 
1442
 
 
1443
  /* Trying to get a thin box, but no way */
 
1444
  /*gtk_widget_set_size_request(pd->eventbox,0,0);*/
 
1445
  gtk_widget_set_size_request(GTK_WIDGET(plugin),10,10);
 
1446
  xfce_panel_plugin_set_expand(plugin,FALSE);
 
1447
 
 
1448
  gtk_container_add(GTK_CONTAINER(plugin),pd->eventbox);
 
1449
 
 
1450
  gtk_widget_show_all(GTK_WIDGET(plugin));
 
1451
 
 
1452
  g_signal_connect (plugin, "free-data",
 
1453
                      G_CALLBACK (plugin_free), pd);
 
1454
 
 
1455
  g_signal_connect (plugin, "save",
 
1456
                      G_CALLBACK (save_settings), pd);
 
1457
 
 
1458
  g_signal_connect (plugin, "orientation-changed",
 
1459
                      G_CALLBACK (orient_change), pd);
 
1460
 
 
1461
  xfce_panel_plugin_menu_show_configure (plugin);
 
1462
  g_signal_connect (plugin, "configure-plugin",
 
1463
                      G_CALLBACK (plugin_create_options), pd);
 
1464
  return(TRUE);
 
1465
}
 
1466
 
 
1467