wmauda: Fix installation dir
[dockapps.git] / wmcalendar-0.5.2 / Src / settings.c
blob5b634618d191374489f61490b94436e8b7f08d8c
1 #include "settings.h"
2 void set_lang(int language){lang = language;}
3 void setAppicon(int app){appicon = app;}
4 void enter_callback( GtkWidget *widget, GtkWidget *entry ){
5 strcpy(application, gtk_entry_get_text(GTK_ENTRY(entry)));}
6 int getAppicon(){return appicon;}
7 int get_start_of_week(){return start_of_week;}
8 int get_lang(){return lang;}
9 int get_debug(){return debug;}
10 void set_debug(int deb){debug = deb;}
11 char* getVersion(){return WMCALENDAR_VERSION;}
12 const char* get_application(){return application;}
13 const char* get_icsfile(){return icsfile;}
15 /*------------------------------------------------------
16 * destroy
17 -----------------------------------------------------*/
18 void destroy (GtkWidget * widget, gpointer data){
19 gtk_main_quit ();
24 /*------------------------------------------------------
25 * getSettings
26 -----------------------------------------------------*/
27 void getSettings(){
28 char buf[15];
29 struct tm *timptr = malloc(sizeof(struct tm));
30 FILE *stream;
31 int lSize;
32 char *set = malloc(4096);
33 char *pch;
34 int i;
35 int next = 0;
36 int version = 0;
37 strcpy (rcfile, (char *) getenv ("HOME"));
38 strcat(rcfile, "/.wmcalendarrc");
39 stream = fopen(rcfile,"r");
41 for(i = 1; i < 8; i++){
42 timptr->tm_wday = (8-i)%7;
43 strftime(buf, 15, "%A", timptr);
44 daystr[i]=malloc(15);
45 strcpy(daystr[i], buf);
47 free(timptr);
48 if(stream == 0){ /* no rcfile */
49 writeSettings();
50 printf("created %s with default settings:\nICON:evolution\nLANG:default\nAPP:%s\nICS:%s\n\n",
51 rcfile, application, icsfile );
52 return;
54 else{
55 fseek (stream , 0 , SEEK_END);
56 lSize = ftell (stream);
57 rewind (stream);
58 fread (set,1,lSize,stream);
60 pch = strtok (set,"\n=");
61 while (pch != NULL)
63 switch(next){
64 case 1:
65 strcpy(icsfile, pch);
66 next = 0;
67 break;
69 case 2:
70 strcpy(application, pch);
71 next = 0;
72 break;
74 case 3:
75 if(!strcmp(pch, "farsi"))
76 lang = 1;
77 next = 0;
78 break;
80 case 4:
81 if(!strcmp(pch, "mozilla"))
82 appicon = 1;
83 else if(!strcmp(pch, "evolution"))
84 appicon = 0;
85 else
86 appicon = 2;
87 next = 0;
88 break;
90 case 5:
91 if(!strcmp(pch,WMCALENDAR_VERSION))
92 version = 1;
93 next = 0;
94 break;
96 case 6:
97 start_of_week = atoi(pch);
98 next = 0;
99 break;
101 default:
102 if(!strcmp(pch, "ICS"))
103 next = 1;
104 else if(!strcmp(pch, "APP"))
105 next = 2;
106 else if(!strcmp(pch, "LANG"))
107 next = 3;
108 else if(!strcmp(pch, "ICON"))
109 next = 4;
110 else if(!strcmp(pch, "VERSION"))
111 next = 5;
112 else if(!strcmp(pch, "STARTOFWEEK"))
113 next = 6;
114 else
115 next = 0;
117 pch = strtok (NULL, "=\n");
120 fclose(stream);
121 free(set);
122 if(!version){
123 writeSettings();
124 printf("Old rcfile detected!\nCreated new %s with default settings:", rcfile);
125 printf("\nICON:evolution\nLANG:default\nAPP:%s\nICS:%s\n\n", application, icsfile );
127 if(get_debug())printf("settings:\nICON:%d\nLANG:%d\nAPP:%s\nRC:%s\nICS:%s\n\n",
128 getAppicon(), lang, application, rcfile, icsfile );
133 /*------------------------------------------------------
134 * writeSettings
135 -----------------------------------------------------*/
136 void writeSettings(){
137 char *set = malloc(1024);
138 char *bufint = malloc(5);
139 FILE *stream;
140 if(debug)printf("write settings\n");
141 stream = fopen(rcfile,"w");
142 if(strlen(icsfile)==0){
143 strcpy(icsfile, (char *) getenv ("HOME"));
144 strcat(icsfile , "/evolution/local/Calendar/calendar.ics");
146 if(strlen(application)==0)
147 strcpy(application , "evolution");
148 strcpy(set, "ICS=");
149 strcat(set, icsfile);
150 strcat(set, "\nAPP=");
151 strcat(set, application);
152 if(appicon == 0)
153 strcat(set, "\nICON=evolution");
154 if(appicon == 1)
155 strcat(set, "\nICON=mozilla");
156 if(appicon == 2)
157 strcat(set, "\nICON=other");
158 if(lang == 1)
159 strcat(set, "\nLANG=farsi");
160 else
161 strcat(set, "\nLANG=default");
162 strcat(set,"\nVERSION=");
163 strcat(set,WMCALENDAR_VERSION);
164 strcat(set,"\nSTARTOFWEEK=");
165 sprintf(bufint, "%d",start_of_week);
166 strcat(set, bufint);
167 strcat(set,"\n");
168 fwrite(set, 1, strlen(set), stream);
169 fclose(stream);
170 free(set);
171 free(bufint);
176 /*------------------------------------------------------
177 * SettingsSeperator
178 -----------------------------------------------------*/
179 void SettingsSeperator(GtkWidget *box1){
180 GtkWidget *separator;
181 separator = gtk_hseparator_new ();
182 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
183 gtk_widget_show (separator);
189 /*------------------------------------------------------
190 * SettingsLabel
191 -----------------------------------------------------*/
192 void SettingsLabel(GtkWidget *box1, char* str){
193 GtkWidget *label;
194 label = gtk_label_new (str);
195 gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
196 gtk_box_pack_start (GTK_BOX (box1), label, TRUE, TRUE, 3);
197 gtk_widget_show (label);
203 /*------------------------------------------------------
204 * openSettings
205 -----------------------------------------------------*/
206 void openSettings(){
207 GtkWidget *window;
208 GtkWidget *box1;
209 GtkWidget *box2;
210 GtkWidget *entry;
211 GtkWidget *button;
212 GtkWidget *menu;
213 GtkWidget *optionmenu;
214 GtkWidget *menuitem;
215 GSList *group;
216 int ii;
217 char title[30];
218 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
219 gtk_window_set_policy(GTK_WINDOW (window), FALSE, FALSE, FALSE);
221 gtk_signal_connect (GTK_OBJECT (window), "destroy",
222 GTK_SIGNAL_FUNC(destroy),
223 NULL);
224 sprintf(title, "wmCalendar %s", WMCALENDAR_VERSION);
225 gtk_window_set_title (GTK_WINDOW (window), title);
226 gtk_container_set_border_width (GTK_CONTAINER (window), 0);
228 box1 = gtk_vbox_new (FALSE, 0);
229 gtk_container_add (GTK_CONTAINER (window), box1);
230 gtk_widget_show (box1);
232 /* -------------- Applaunch icon --------------------------*/
233 SettingsLabel(box1, "Applaunch icon");
235 box2 = gtk_hbox_new (FALSE, 10);
236 gtk_container_set_border_width (GTK_CONTAINER (box2), 0);
237 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 3);
238 gtk_widget_show (box2);
240 /* create appicon boxes*/
241 button = gtk_radio_button_new_with_label (NULL, "Evolution");
242 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
243 GTK_SIGNAL_FUNC(setAppicon),
245 if(appicon == 0)
246 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
247 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
248 gtk_widget_show (button);
250 group = gtk_radio_button_group (GTK_RADIO_BUTTON (button));
251 button = gtk_radio_button_new_with_label(group, "Mozilla");
252 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
253 GTK_SIGNAL_FUNC(setAppicon),
254 (gpointer)1);
255 if(appicon == 1)
256 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
257 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
258 gtk_widget_show (button);
260 button = gtk_radio_button_new_with_label(
261 gtk_radio_button_group (GTK_RADIO_BUTTON (button)),
262 "Other");
263 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
264 GTK_SIGNAL_FUNC(setAppicon),
265 (gpointer)2);
266 if(appicon == 2)
267 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
268 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
269 gtk_widget_show (button);
271 SettingsSeperator(box1);
273 /* -------------- Applaunch command -----------------------*/
274 SettingsLabel(box1, "Applaunch command");
276 entry = gtk_entry_new_with_max_length (100);
277 gtk_signal_connect(GTK_OBJECT(entry), "changed",
278 GTK_SIGNAL_FUNC(enter_callback),
279 entry);
280 gtk_entry_set_text (GTK_ENTRY (entry), application);
281 gtk_box_pack_start (GTK_BOX (box1), entry, TRUE, TRUE, 3);
282 gtk_widget_show (entry);
284 SettingsSeperator(box1);
286 /* -------------- Language -------------------------------*/
287 SettingsLabel(box1, "Language");
289 /* create Language boxes */
290 box2 = gtk_hbox_new (FALSE, 10);
291 gtk_container_set_border_width (GTK_CONTAINER (box2), 0);
292 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 3);
293 gtk_widget_show (box2);
295 button = gtk_radio_button_new_with_label (NULL, "default");
296 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
297 GTK_SIGNAL_FUNC(set_lang),
299 if(lang == 0)
300 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
301 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
302 gtk_widget_show (button);
304 group = gtk_radio_button_group (GTK_RADIO_BUTTON (button));
305 button = gtk_radio_button_new_with_label(group, "farsi");
306 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
307 GTK_SIGNAL_FUNC(set_lang),
308 (gpointer)1);
309 if(lang == 1)
310 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
311 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
312 gtk_widget_show (button);
314 SettingsSeperator(box1);
316 /* -------------- first day of week --------------------------*/
317 SettingsLabel(box1, "Week starts");
318 optionmenu = gtk_option_menu_new();
319 menu = gtk_menu_new();
320 for(ii = 7; ii > 0; ii--){
321 menuitem = gtk_menu_item_new_with_label(daystr[ii]);
322 gtk_widget_show (menuitem);
323 gtk_menu_append(menu, menuitem);
325 gtk_menu_set_active(GTK_MENU (menu), 7 - start_of_week);
326 gtk_option_menu_set_menu(GTK_OPTION_MENU(optionmenu),menu);
327 gtk_box_pack_start (GTK_BOX (box1), optionmenu, TRUE, TRUE, 5);
328 gtk_widget_show (optionmenu);
330 SettingsSeperator(box1);
333 /* -------------- Buttons -------------------------------*/
334 button = gtk_button_new_with_label ("change iCalendar file");
335 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
336 GTK_SIGNAL_FUNC(changeFilename),
337 NULL);
338 gtk_box_pack_start (GTK_BOX (box1), button, TRUE, TRUE, 2);
339 gtk_widget_show (button);
341 button = gtk_button_new_with_label ("save");
342 gtk_signal_connect (GTK_OBJECT (button), "clicked",
343 (GtkSignalFunc) setFirstDay,
344 GTK_OPTION_MENU (optionmenu));
345 gtk_box_pack_start (GTK_BOX (box1), button, TRUE, FALSE, 2);
346 gtk_widget_show (button);
348 button = gtk_button_new_with_label ("close");
349 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
350 (GtkSignalFunc) gtk_widget_destroy,
351 GTK_OBJECT (window));
352 gtk_box_pack_start (GTK_BOX (box1), button, TRUE, TRUE, 2);
353 gtk_widget_show (button);
354 gtk_widget_show (window);
355 gtk_main();
360 /*------------------------------------------------------
361 * setFirstDay
362 -----------------------------------------------------*/
363 void setFirstDay(GtkWidget *widget, GtkWidget *optionmenu ){
364 gchar *daystring;
365 int ii;
366 gtk_label_get (GTK_LABEL (GTK_BIN (optionmenu)->child), &daystring);
367 for(ii = 1; ii < 8; ii++)
368 if(!strcmp(daystring, daystr[ii]))
369 start_of_week = ii;
371 writeSettings();
376 /*------------------------------------------------------
377 * changeFilename
378 -----------------------------------------------------*/
379 void changeFilename(){
380 static GtkWidget *settings;
382 settings = gtk_file_selection_new("iCalendar file");
384 gtk_file_selection_set_filename (GTK_FILE_SELECTION(settings),
385 icsfile);
386 /* Connect the ok_button to file_ok_sel function */
387 gtk_signal_connect(GTK_OBJECT (GTK_FILE_SELECTION (settings)->ok_button),
388 "clicked",(GtkSignalFunc) file_ok_sel, (gpointer) settings);
389 gtk_signal_connect(GTK_OBJECT (settings), "destroy",
390 GTK_SIGNAL_FUNC (destroy), NULL);
391 gtk_signal_connect_object(GTK_OBJECT (GTK_FILE_SELECTION(settings)->cancel_button),
392 "clicked", (GtkSignalFunc) gtk_widget_destroy, GTK_OBJECT (settings));
394 gtk_window_position (GTK_WINDOW(settings), GTK_WIN_POS_MOUSE );
395 gtk_widget_show (settings);
396 gtk_main ();
401 /*------------------------------------------------------
402 * file_ok_sel
403 -----------------------------------------------------*/
404 static void file_ok_sel( GtkWidget *w,
405 GtkFileSelection *fs )
407 strcpy(icsfile, gtk_file_selection_get_filename (GTK_FILE_SELECTION (fs)));
408 gtk_widget_destroy((GtkWidget*)fs);
409 writeSettings();