Fix crashing of control panel aplet, create config directory if missing
[callnotify] / src / usr / lib / hildon-control-panel / libcallnotify.c
1 #include <hildon-cp-plugin/hildon-cp-plugin-interface.h>
2 #include <hildon/hildon.h>
3 #include <gtk/gtk.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/stat.h>
7 #include <sys/types.h>
8
9 osso_return_t execute(osso_context_t *osso, gpointer data, gboolean user_activated)
10 {
11         char a = 'y';
12         char b = 'y';
13         char c = 'y';
14         GtkWidget *dialog;
15         GtkWidget *btnVisual;
16         GtkWidget *btnVibrate;
17         GtkWidget *btnSound;
18         GtkWidget *slider;
19         GtkWidget *lblText;
20         GtkAdjustment * adj;
21         gdouble sldValue = 0.0;
22         gdouble newValue = 0.0;
23         char fileContent[64];
24         char *fileDouble;
25         int i;
26         gint response;
27         FILE *inputFilePtr;
28
29         /* Create dialog with OK and Cancel buttons. Leave the separator out,
30          * as we do not have any content. */
31         dialog = gtk_dialog_new_with_buttons(
32                 "Call Notify Settings",
33                 GTK_WINDOW(data),
34                 GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR,
35                 GTK_STOCK_OK,
36                 GTK_RESPONSE_OK,
37                 GTK_STOCK_CANCEL,
38                 GTK_RESPONSE_CANCEL,
39                 NULL);
40
41         /* ... add something to the dialog ... */
42         btnVisual = (GtkWidget*)hildon_check_button_new(HILDON_SIZE_FULLSCREEN_WIDTH | HILDON_SIZE_FINGER_HEIGHT);
43         gtk_button_set_label (GTK_BUTTON (btnVisual), "Visual Notification");
44
45         btnVibrate = (GtkWidget*)hildon_check_button_new(HILDON_SIZE_FULLSCREEN_WIDTH | HILDON_SIZE_FINGER_HEIGHT);
46         gtk_button_set_label (GTK_BUTTON (btnVibrate), "Vibrate Notification");
47
48         btnSound = (GtkWidget*)hildon_check_button_new(HILDON_SIZE_FULLSCREEN_WIDTH | HILDON_SIZE_FINGER_HEIGHT);
49         gtk_button_set_label (GTK_BUTTON (btnSound), "Sound Notification");
50
51         lblText = (GtkWidget*)gtk_label_new("Please select notification interval:");
52
53
54         slider = gtk_hscale_new_with_range(0.0, 60.0, 0.1);
55         adj = gtk_range_get_adjustment(GTK_RANGE(slider));
56
57
58         inputFilePtr = fopen("/home/user/.config/CallNotify/conf.txt", "r");
59
60         if (inputFilePtr != NULL)
61         {
62
63                 fgets(fileContent, 12,inputFilePtr);
64
65
66                 if (fileContent[0]=='y')
67                         hildon_check_button_set_active (HILDON_CHECK_BUTTON(btnVisual), TRUE);
68                 if (fileContent[2]=='y')
69                         hildon_check_button_set_active (HILDON_CHECK_BUTTON(btnVibrate) , TRUE);
70                 if (fileContent[4]=='y')
71                         hildon_check_button_set_active (HILDON_CHECK_BUTTON(btnSound), TRUE);
72
73                 (char*)strtok(fileContent, ";");
74                 (char*)strtok(NULL, ";");
75                 (char*)strtok(NULL, ";");
76
77                 fileDouble = strtok(NULL, ";");
78
79                 if (fileDouble)
80                 {
81
82                         // replace , with .
83                         for (i=0;  i<strlen(fileDouble); i++)
84                                 if (fileDouble[i]==',')
85                                         fileDouble[i] = '.';
86
87                         sldValue = g_strtod(fileDouble, NULL);
88
89                 }
90
91                 fclose(inputFilePtr);
92
93         }
94
95         gtk_adjustment_set_value(adj, sldValue);
96
97         gtk_container_add(GTK_CONTAINER (GTK_DIALOG(dialog)->vbox),     btnVisual);
98         gtk_container_add(GTK_CONTAINER (GTK_DIALOG(dialog)->vbox),     btnVibrate);
99         gtk_container_add(GTK_CONTAINER (GTK_DIALOG(dialog)->vbox),     btnSound);
100         gtk_container_add(GTK_CONTAINER (GTK_DIALOG(dialog)->vbox),     lblText);
101         gtk_container_add(GTK_CONTAINER (GTK_DIALOG(dialog)->vbox),     slider);
102
103         gtk_widget_show_all(dialog);
104
105         /* Wait until user finishes the dialog. */
106         response = gtk_dialog_run(GTK_DIALOG(dialog));
107
108         if (response == GTK_RESPONSE_OK)
109         {
110                 /* ... do something with the dialog stuff ... */
111                 inputFilePtr = fopen("/home/user/.config/CallNotify/conf.txt", "w");
112
113                 if (inputFilePtr == NULL)
114                 {
115                         mkdir("/home/user/.config/CallNotify", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
116                         inputFilePtr = fopen("/home/user/.config/CallNotify/conf.txt", "w");
117                 }
118
119                 if (inputFilePtr != NULL)
120                 {
121                         a = hildon_check_button_get_active(HILDON_CHECK_BUTTON(btnVisual)) ? 'y' : 'n';
122                         b = hildon_check_button_get_active(HILDON_CHECK_BUTTON(btnVibrate)) ? 'y' : 'n';
123                         c = hildon_check_button_get_active(HILDON_CHECK_BUTTON(btnSound)) ? 'y' : 'n';
124
125                         newValue = gtk_adjustment_get_value(adj);
126
127                         fprintf(inputFilePtr, "%c;%c;%c;%.1f\n",a,b,c,newValue);
128                         fclose(inputFilePtr);
129                 }
130
131         }
132
133         /* Free the dialog (and it's children) */
134
135         gtk_widget_destroy(GTK_WIDGET(dialog));
136
137         return OSSO_OK;
138 }
139
140 osso_return_t save_state(osso_context_t *osso, gpointer data)
141 {
142         /* ... save state ... */
143
144         return OSSO_OK;
145 }