Fix crashing of control panel aplet, create config directory if missing
[callnotify] / src / usr / lib / hildon-control-panel / libcallnotify.c
index 5dc4013..5e60b96 100644 (file)
@@ -1,7 +1,10 @@
 #include <hildon-cp-plugin/hildon-cp-plugin-interface.h>
+#include <hildon/hildon.h>
 #include <gtk/gtk.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 
 osso_return_t execute(osso_context_t *osso, gpointer data, gboolean user_activated)
 {
@@ -17,10 +20,11 @@ osso_return_t execute(osso_context_t *osso, gpointer data, gboolean user_activat
        GtkAdjustment * adj;
        gdouble sldValue = 0.0;
        gdouble newValue = 0.0;
-       char *fileContent;
+       char fileContent[64];
        char *fileDouble;
        int i;
        gint response;
+       FILE *inputFilePtr;
 
        /* Create dialog with OK and Cancel buttons. Leave the separator out,
         * as we do not have any content. */
@@ -35,7 +39,6 @@ osso_return_t execute(osso_context_t *osso, gpointer data, gboolean user_activat
                NULL);
 
        /* ... add something to the dialog ... */
-       //btnVisual = gtk_check_button_new_with_label();
        btnVisual = (GtkWidget*)hildon_check_button_new(HILDON_SIZE_FULLSCREEN_WIDTH | HILDON_SIZE_FINGER_HEIGHT);
        gtk_button_set_label (GTK_BUTTON (btnVisual), "Visual Notification");
 
@@ -49,44 +52,47 @@ osso_return_t execute(osso_context_t *osso, gpointer data, gboolean user_activat
 
 
        slider = gtk_hscale_new_with_range(0.0, 60.0, 0.1);
+       adj = gtk_range_get_adjustment(GTK_RANGE(slider));
 
 
-               FILE *inputFilePtr = fopen("/home/user/.config/CallNotify/conf.txt", "r");
+       inputFilePtr = fopen("/home/user/.config/CallNotify/conf.txt", "r");
 
-               if (inputFilePtr != NULL)
-               {
+       if (inputFilePtr != NULL)
+       {
 
+               fgets(fileContent, 12,inputFilePtr);
 
-                   fgets(fileContent, 12,inputFilePtr);
 
+               if (fileContent[0]=='y')
+                       hildon_check_button_set_active (HILDON_CHECK_BUTTON(btnVisual), TRUE);
+               if (fileContent[2]=='y')
+                       hildon_check_button_set_active (HILDON_CHECK_BUTTON(btnVibrate) , TRUE);
+               if (fileContent[4]=='y')
+                       hildon_check_button_set_active (HILDON_CHECK_BUTTON(btnSound), TRUE);
 
-                   if (fileContent[0]=='y')
-                       hildon_check_button_set_active (GTK_BUTTON(btnVisual), TRUE);
-                   if (fileContent[2]=='y')
-                       hildon_check_button_set_active (GTK_BUTTON(btnVibrate) , TRUE);
-                   if (fileContent[4]=='y')
-                       hildon_check_button_set_active (GTK_BUTTON(btnSound), TRUE);
+               (char*)strtok(fileContent, ";");
+               (char*)strtok(NULL, ";");
+               (char*)strtok(NULL, ";");
 
-                       (char*)strtok(fileContent, ";");
-                       (char*)strtok(NULL, ";");
-                       (char*)strtok(NULL, ";");
+               fileDouble = strtok(NULL, ";");
 
-                       fileDouble = strtok(NULL, ";");
+               if (fileDouble)
+               {
 
                        // replace , with .
                        for (i=0;  i<strlen(fileDouble); i++)
                                if (fileDouble[i]==',')
                                        fileDouble[i] = '.';
 
-                   sldValue = g_strtod(fileDouble, NULL);
-
-                   adj = gtk_range_get_adjustment(GTK_RANGE(slider));
-                   gtk_adjustment_set_value(adj, sldValue);
+                       sldValue = g_strtod(fileDouble, NULL);
 
                }
 
-                fclose(inputFilePtr);
+               fclose(inputFilePtr);
+
+       }
 
+       gtk_adjustment_set_value(adj, sldValue);
 
        gtk_container_add(GTK_CONTAINER (GTK_DIALOG(dialog)->vbox),     btnVisual);
        gtk_container_add(GTK_CONTAINER (GTK_DIALOG(dialog)->vbox),     btnVibrate);
@@ -102,20 +108,25 @@ osso_return_t execute(osso_context_t *osso, gpointer data, gboolean user_activat
        if (response == GTK_RESPONSE_OK)
        {
                /* ... do something with the dialog stuff ... */
-               //FILE *f_write =
                inputFilePtr = fopen("/home/user/.config/CallNotify/conf.txt", "w");
 
+               if (inputFilePtr == NULL)
+               {
+                       mkdir("/home/user/.config/CallNotify", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
+                       inputFilePtr = fopen("/home/user/.config/CallNotify/conf.txt", "w");
+               }
 
-               a = hildon_check_button_get_active(GTK_BUTTON(btnVisual)) ? 'y' : 'n';
-               b = hildon_check_button_get_active(GTK_BUTTON(btnVibrate)) ? 'y' : 'n';
-               c = hildon_check_button_get_active(GTK_BUTTON(btnSound)) ? 'y' : 'n';
-
-               newValue = gtk_adjustment_get_value(adj);
-
+               if (inputFilePtr != NULL)
+               {
+                       a = hildon_check_button_get_active(HILDON_CHECK_BUTTON(btnVisual)) ? 'y' : 'n';
+                       b = hildon_check_button_get_active(HILDON_CHECK_BUTTON(btnVibrate)) ? 'y' : 'n';
+                       c = hildon_check_button_get_active(HILDON_CHECK_BUTTON(btnSound)) ? 'y' : 'n';
 
-               fprintf(inputFilePtr, "%c;%c;%c;%.1f\n",a,b,c,newValue);
+                       newValue = gtk_adjustment_get_value(adj);
 
-               fclose(inputFilePtr);
+                       fprintf(inputFilePtr, "%c;%c;%c;%.1f\n",a,b,c,newValue);
+                       fclose(inputFilePtr);
+               }
 
        }