Initial git release
[connectnow-hp] / src / connectnow-load-and-store.c
1 /*
2  *  connectnow home widget for the maemo desktop.
3  *  Copyright (C) 2010 Nicolai Hess
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 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 #include "connectnow-load-and-store.h"
21 #define CONNECTNOW_HOME_PLUGIN_SETTINGS_FILE "/.connectnow_home_widget"
22
23
24 void 
25 read_settings(ConnectNowHomePlugin *desktop_plugin)
26 {
27   GKeyFile *keyFile;
28   gchar* fileName;
29   gboolean fileExists;
30
31   keyFile = g_key_file_new();
32   fileName = g_strconcat(g_get_home_dir(), CONNECTNOW_HOME_PLUGIN_SETTINGS_FILE, NULL);
33   fileExists = g_key_file_load_from_file (keyFile, fileName, G_KEY_FILE_KEEP_COMMENTS, NULL);
34   if(fileExists)
35   {
36     GError *error = NULL;
37     if(g_key_file_has_key(keyFile, desktop_plugin->iD, "widget_size", NULL))
38     {
39       desktop_plugin->widget_size = g_key_file_get_integer(keyFile, desktop_plugin->iD, "widget_size", NULL);
40     }
41     if(g_key_file_has_key(keyFile, desktop_plugin->iD, "hide_connection_name", NULL))
42     {
43       desktop_plugin->hide_connection_name = g_key_file_get_boolean(keyFile, desktop_plugin->iD, "hide_connection_name", NULL);
44     }
45     if(g_key_file_has_key(keyFile, desktop_plugin->iD, "connectedIcon", NULL))
46     {
47       desktop_plugin->connectedImageName = g_key_file_get_string(keyFile, desktop_plugin->iD, "connectedIcon", NULL);
48     }
49     else
50     {
51       desktop_plugin->connectedImageName = g_strdup(GENERAL_CONNECTED);
52     }
53     if(g_key_file_has_key(keyFile, desktop_plugin->iD, "disconnectedIcon", NULL))
54     {
55       desktop_plugin->disconnectedImageName = g_key_file_get_string(keyFile, desktop_plugin->iD, "disconnectedIcon", NULL);
56     }
57     else
58     {
59       desktop_plugin->disconnectedImageName = g_strdup(GENERAL_DISCONNECTED);
60     }
61     desktop_plugin->connectedImage = load_image(desktop_plugin->connectedImageName);
62     desktop_plugin->disconnectedImage = load_image(desktop_plugin->disconnectedImageName);
63     desktop_plugin->connection_name = g_key_file_get_string(keyFile, desktop_plugin->iD, "connection_name", &error);
64     if(!error)
65     {
66       desktop_plugin->connection_id = g_key_file_get_string(keyFile, desktop_plugin->iD, "connection_id", &error);
67
68       if(!error)
69       {
70         g_key_file_free(keyFile);
71         g_free(fileName);
72         return;
73       }
74     }
75     g_error_free(error);
76     error = NULL;
77   }
78   desktop_plugin->connection_name = NULL;
79   desktop_plugin->connection_id = NULL;
80   g_key_file_free(keyFile);
81   g_free(fileName);
82 }
83
84 void
85 connect_now_save_settings(ConnectNowHomePlugin* desktop_plugin)
86 {
87   GKeyFile *keyFile;
88   gchar *fileData;
89   FILE *iniFile;
90   gsize size;
91   gchar *filename;
92   
93   keyFile = g_key_file_new();
94   filename = g_strconcat (g_get_home_dir(), CONNECTNOW_HOME_PLUGIN_SETTINGS_FILE, NULL);
95   g_key_file_load_from_file (keyFile, filename, G_KEY_FILE_KEEP_COMMENTS, NULL);
96   if(desktop_plugin->connection_id)
97     g_key_file_set_string(keyFile, desktop_plugin->iD, "connection_id", desktop_plugin->connection_id);
98   if(desktop_plugin->connection_name)
99     g_key_file_set_string(keyFile, desktop_plugin->iD, "connection_name", desktop_plugin->connection_name);
100   
101   g_key_file_set_integer(keyFile, desktop_plugin->iD, "widget_size", desktop_plugin->widget_size);
102   g_key_file_set_boolean(keyFile, desktop_plugin->iD, "hide_connection_name", desktop_plugin->hide_connection_name);
103   if(desktop_plugin->connectedImageName)
104   {
105     g_key_file_set_string(keyFile, desktop_plugin->iD, "connectedIcon", desktop_plugin->connectedImageName);
106   }
107   else
108   {
109     g_key_file_remove_key(keyFile, desktop_plugin->iD, "connectedIcon", NULL);
110   }
111   if(desktop_plugin->disconnectedImageName)
112   {
113     g_key_file_set_string(keyFile, desktop_plugin->iD, "disconnectedIcon", desktop_plugin->disconnectedImageName);
114   }
115   else
116   {
117     g_key_file_remove_key(keyFile, desktop_plugin->iD, "disconnectedIcon", NULL);
118   }
119   fileData = g_key_file_to_data (keyFile, &size, NULL);
120   g_file_set_contents(filename, fileData, size, NULL);
121
122   g_key_file_free (keyFile); 
123   g_free (fileData); 
124   g_free (filename); 
125 }
126
127 GdkPixbuf* 
128 load_image(const gchar* image_name)
129 {
130   GdkPixbuf* image = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(),
131                                               image_name,
132                                               CONNECTNOW_DEFAULT_WIDGET_WIDTH,
133                                               0,
134                                               NULL);
135   if(!image)
136   {
137     image = gdk_pixbuf_new_from_file_at_size(image_name,
138                                              CONNECTNOW_DEFAULT_WIDGET_WIDTH,
139                                              CONNECTNOW_DEFAULT_WIDGET_WIDTH,
140                                              NULL);
141   }
142   return image;
143 }