Add missing libraries to examples' Makefiles
[dockapps.git] / wmclockmon / wmclockmon-config / actions.c
blobf2e2ae72dcb8c675cdf89223fc5c5c38f725715b
1 /*
2 * actions.c
3 */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <strings.h>
9 #include <sys/types.h>
10 #include <ctype.h>
11 #include <pwd.h>
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include "defines.h"
15 #include "variables.h"
16 #include "mainwindow.h"
17 #include "tools.h"
18 #include "edit.h"
19 #include "actions.h"
21 void quit_app(void) {
22 FREE(command);
23 FREE(config_file);
24 FREE(light_color);
25 FREE(msgcmd);
26 FREE(style_dir);
27 FREE(style_name);
28 free_alrm(&alarms);
29 gtk_main_quit();
33 static void set_list(void) {
35 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(alarmlist));
36 GtkListStore *store = GTK_LIST_STORE(model);
37 GtkTreeIter iter;
38 Alarm *alrm = alarms;
40 gtk_list_store_clear(store);
42 while (alrm) {
44 gtk_list_store_append(store, &iter);
45 gtk_list_store_set (store, &iter,
46 COL_STATUS, alrm->on ? "ON" : "OFF",
47 COL_HOUR, alrm->time ? alrm->time : "ERROR",
48 COL_DAY, alrm->date ? alrm->date : "ALL",
49 COL_MESSAGE, alrm->msg ? alrm->msg : "",
50 COL_ALARM, alrm,
51 -1);
53 alrm = alrm->next;
57 clear_alarmlist_selection();
61 void set_values(void) {
62 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(wid_backlight), backlight);
63 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(wid_blink),
64 switch_authorized);
65 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(wid_h12), h12);
66 if (time_mode == 1)
67 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(wid_itm), 1);
68 else if (time_mode == 2)
69 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(wid_bin), 1);
70 else
71 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(wid_clk), 1);
72 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(wid_locale), use_locale);
73 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(wid_showcal), showcal);
74 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(wid_calalrms), calalrms);
75 if (light_color) gtk_entry_set_text(GTK_ENTRY(wid_color), light_color);
76 if (command) gtk_entry_set_text(GTK_ENTRY(wid_command), command);
77 if (msgcmd) gtk_entry_set_text(GTK_ENTRY(wid_msgcmd), msgcmd);
78 if (style_name) gtk_entry_set_text(GTK_ENTRY(wid_stylename), style_name);
79 if (style_dir) gtk_entry_set_text(GTK_ENTRY(wid_styledir), style_dir);
81 set_list();
85 void save_datas(void) {
86 FREE(style_dir);
87 FREE(style_name);
88 FREE(light_color);
89 FREE(command);
90 FREE(msgcmd);
92 style_dir = xstrdup(gtk_entry_get_text(GTK_ENTRY(wid_styledir)));
93 style_name = xstrdup(gtk_entry_get_text(GTK_ENTRY(wid_stylename)));
94 light_color = xstrdup(gtk_entry_get_text(GTK_ENTRY(wid_color)));
95 command = xstrdup(gtk_entry_get_text(GTK_ENTRY(wid_command)));
96 msgcmd = xstrdup(gtk_entry_get_text(GTK_ENTRY(wid_msgcmd)));
97 backlight = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wid_backlight));
98 switch_authorized = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wid_blink));
99 h12 = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wid_h12));
100 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wid_itm)))
101 time_mode = 1;
102 else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wid_bin)))
103 time_mode = 2;
104 else
105 time_mode = 0;
106 use_locale = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wid_locale));
107 showcal = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wid_showcal));
108 calalrms = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wid_calalrms));
110 save_cfgfile();
114 void do_add(void) {
115 alrm_add(&alarms, newalarm);
116 set_list();
117 FREE(newalarm);
121 void add_alarm(void) {
122 edit_dialog("Add alarm...", TRUE, "12:00", NULL, NULL, do_add);
126 static Alarm *
127 get_alarm(GtkTreeModel **modelp, GtkTreeIter *iterp) {
129 GtkTreeSelection *selection;
130 GtkTreeModel *model;
131 GtkTreeIter iter;
132 Alarm *alrm = NULL;
134 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(alarmlist));
135 if (gtk_tree_selection_get_selected(selection, &model, &iter))
136 gtk_tree_model_get(model, &iter, COL_ALARM, (gpointer) &alrm, -1);
138 if (modelp)
139 *modelp = model;
141 if (iterp)
142 *iterp = iter;
144 return alrm;
149 void do_change(void) {
150 Alarm *alrm;
151 char *time = NULL, *date = NULL, *ison = NULL, *mesg = NULL, *at;
152 char *tokstr = xstrdup(newalarm);
153 char *toksav = tokstr;
155 alrm = get_alarm(NULL, NULL);
156 if (!alrm)
157 return;
159 at = strchr(newalarm, '@');
160 if (at) ison = strtok(tokstr, "@");
161 time = strtok(at ? NULL : tokstr, "-.");
162 if (strchr(newalarm, '-')) date = strtok(NULL, ".");
163 mesg = strtok(NULL, "\n\0");
165 FREE(alrm->entry);
166 FREE(alrm->time);
167 FREE(alrm->date);
168 FREE(alrm->msg);
169 alrm->entry = xstrdup(newalarm);
170 alrm->time = time ? xstrdup(time) : NULL;
171 alrm->date = date ? xstrdup(date) : NULL;
172 alrm->on = ison ? getbool(ison) : TRUE;
173 alrm->msg = mesg ? xstrdup(mesg) : NULL;
174 set_list();
175 FREE(newalarm);
176 FREE(toksav);
180 void edit_entry(void) {
181 Alarm *alrm;
183 alrm = get_alarm(NULL, NULL);
184 if (!alrm)
185 return;
187 edit_dialog("Edit alarm...", alrm->on, alrm->time, alrm->date, alrm->msg, do_change);
191 void switch_onoff(void) {
192 Alarm *alrm;
193 GtkTreeModel *model;
194 GtkTreeIter iter;
196 /* get selected default box */
197 alrm = get_alarm(&model, &iter);
198 if (!alrm)
199 return;
200 alrm->on = !alrm->on;
201 /* set text in both rows */
202 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
203 COL_STATUS, alrm->on ? "ON" : "OFF",
204 -1);
208 void remove_alarm(void) {
209 Alarm *alrm, *prev;
211 alrm = get_alarm(NULL, NULL);
212 if (!alrm)
213 return;
215 prev = alarms;
216 while (prev && (prev->next != alrm)) {
217 prev = prev->next;
219 if (alrm == alarms) {
220 alarms = alrm->next;
221 } else if (prev != NULL) {
222 prev->next = alrm->next;
223 } else {
224 fprintf(stderr, "Error when removing alarm...\n");
226 FREE(alrm->entry);
227 FREE(alrm->time);
228 FREE(alrm->date);
229 FREE(alrm->msg);
230 FREE(alrm);
231 set_list();