/gpump/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/gpump/trunk
20 by Gustav Hatvigsson
* Added a singeltonian class to keep settings data in.
1
#include "GPumpSettingsData.h"
2
3
G_DEFINE_TYPE (GPumpSettingsData, gpump_settings_data, G_TYPE_OBJECT)
4
5
6
/* Private datastructure */
21 by Gustav Hatvigsson
* Switched to GKeyFile instead of GSettings, for the time being.
7
struct GPumpSettingsData {
8
#if GPUMP_USE_GSETTINGS
9
  /* org.gego.gpump.ui */
10
  GSettings * ui_settings;
11
  /* org.gego.gpump */
12
  GSettings * gpump_settings;
13
#else
14
  GKeyFile * gpump_settings; /* Stores both UI and other settings... */
15
  GFile * gpump_settigs_file;
16
#endif /* GPUMP_USE_GSETTINGS */
17
}
18
19
/* * Override the default constructer *****************************************/
20 by Gustav Hatvigsson
* Added a singeltonian class to keep settings data in.
20
GObject * _constructor (GType type,
21
                        guint n_construct_params,
22
                        GObjectConstructParam * construct_params);
23
24
25
/******************************************************************************/
26
GPumpSettingsData * gpump_settings_data_get_default () {
27
  GPumpSettingsData * self = g_object_new (GPUMP_TYPE_SETTINGS_DATA,
28
                                           NULL);
29
  
30
  return self;
31
}
32
33
void gpump_settings_data_init (GPumpSettingsData * self) {
34
  g_print ("Init GPumpSettingsData\n");
35
  
21 by Gustav Hatvigsson
* Switched to GKeyFile instead of GSettings, for the time being.
36
  self->priv = GPUMP_SETTINGS_DATA_GET_PRIVATE (self);
37
  
38
#if GPUMP_USE_GSETTINGS
39
  /* This will not be compiled, this will have to be implemented later...
40
   * much later...
41
   */
42
  self->ui_settings = g_settings_new ("org.gego.gpump.ui");
20 by Gustav Hatvigsson
* Added a singeltonian class to keep settings data in.
43
  self->gpump_settings = g_settings_new ("org.gego.gpump");
44
#else
21 by Gustav Hatvigsson
* Switched to GKeyFile instead of GSettings, for the time being.
45
  
46
  /* Construct the path for the files, 1) the GPump settings 2) the GPump UI 
47
   * settings.
48
   *
49
   * The file names are defined static, so they do not need to be re-created,
50
   * each and every time te object is re-created.
51
   */
52
  
53
  self->priv->gpump_settings = g_key_file_new ();
54
  
55
  static gchar * gpump_settings_file_uri = NULL;
56
  
57
  if (!(gpump_settings_file_uri)) {
58
    
59
    gchar * base_config_dir = g_get_user_config_dir ();
60
    
61
    gchar * settings_basedir = g_strdup_printf ("%s/%s",
62
                                                base_config_dir,
63
                                                "gpump");
64
    g_free (base_config_dir);
65
    
66
    gpump_settings_file_uri = g_strdup_printf ("%s/%s",
67
                                               settings_basedir,
68
                                               "gpump.conf");
69
  }
70
  
71
  /* Do not want to re-create this object every time, now do we? */
72
  self->priv->gpump_settings_file = g_file_new_from_path (gpump_settings_uri);
73
  
74
  GError * err = NULL;
75
  if (!( g_key_file_load_from_file (self->priv->gpump_settings,
76
                                    pump_settings_file_uri,
77
                                    G_KEY_FILE_NONE,
78
                                    err)) {
79
    /* File does not exist or something else might be wrong. */
80
    if (err) {
81
      /* Did the file exist? */
82
      if (!(g_error_match(err, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_NOT_FOUND))) {
83
        g_error ("GPumpSettingsData:\nAn %s error occured:\n\t%s\n\n",
84
                 g_quark_to_string (err->domain),
85
                 err->message);
86
        g_clear_error (err);
87
      } else { 
88
        /* The file did not exist, thus we must create a key-file and store it.
89
         */
90
        g_clear_error (err);
91
        GOutputStream * out_stream = g_file_create (
92
                                               self->priv->gpump_settings_file,
93
                                               G_FILE_CREATE_PRIVATE,
94
                                               NULL,
95
                                               err);
96
        g_object_unref (out_stream);
97
        
98
        if (err) {
99
          g_error ("GPumpSettingsData: %s\n\t%s\n\n",
100
                   g_quark_to_string (err->domain),
101
                   err->messarge);
102
          g_clear_error (err);
103
        }
104
        
105
        /* UI settings */
106
        /* -1 is undefined, these are set when the main window is displayed */
107
        g_key_file_set_integer (self->priv->gpump_settings, "ui","x", -1);
108
        g_key_file_set_integer (self->priv->gpump_settings, "ui","y", -1);
109
        /* 500x500 is the default window size */
110
        g_key_file_set_integer (self->priv->gpump_settings, "ui","h", 500);
111
        g_key_file_set_integer (self->priv->gpump_settings, "ui","x", 500);
112
        /* main settings */
113
        g_key_file_set_boolean (self->priv->gpump_settings,
114
                                "main",
115
                                "use dark",
116
                                FALSE);
117
        
118
        /* commit data to file. */
119
        if (!(gpump_settings_data_commit (self, err))) {
120
          g_error ("GPumpSettingsData:\n An %s error occured:\n\t%s\n\n",
121
                   g_quark_to_string (err->domain),
122
                   err->messarge);
123
        }
124
      }
125
    }
126
  }
127
#endif /* GPUMP_USE_GSETTINGS */
128
}
20 by Gustav Hatvigsson
* Added a singeltonian class to keep settings data in.
129
130
131
void gpump_settings_data_class_init (GPumpSettingsDataClass * klass) {
132
  GObjectClass * parent = G_OBJECT_CLASS (klass);
133
  
134
  parent->constructor = _constructor;
135
  
21 by Gustav Hatvigsson
* Switched to GKeyFile instead of GSettings, for the time being.
136
  g_type_class_add_private (klass, sizeof (GPumpSettingsDataPrivate);
137
}
138
139
140
void gpump_settings_data_set (GPumpSettingsData * self,
141
                              gchar * name,
142
                              GVariant * data) {
143
#if GPUMP_USE_GSETTINGS
144
  /* TODO */
145
#else
146
  
147
#endif
148
}
149
150
151
GVariant * gpump_settings_data_get (GPumpSettingsData * self,
152
                                    gchar * name) {
153
#if GPUMP_USE_GSETTINGS
154
  /* TODO */
155
#else
156
  
157
#endif
158
}
159
160
161
gboolean gpump_settings_data_commit (GPumpSettingsData * self,
162
                                     GError * err) {
163
#if GPUMP_USE_GSETTINGS
164
  /* Do thothing */
165
  return TRUE;
166
#else
167
  g_assert (err == NULL);
168
  
169
  GFileOutputStream * out_stream = g_file_replace (
170
                                                self->priv->gpump_settings_file,
171
                                                NULL,
172
                                                FALSE,
173
                                                G_FILE_CREATE_PRIVATE,
174
                                                NULL,
175
                                                err);
176
  if (err) {
177
    g_object_unref (out_stream);
178
    return FALSE;
179
  }
180
  
181
  gsize * len = NULL;
182
  gchar * buff = g_key_file_to_data (self->priv->gpump_settings, len, err);
183
  
184
  if (err) {
185
   g_object_unref (out_stream);
186
   g_free (len);
187
   g_free (buff);
188
   return FALSE;
189
  }
190
  
191
  g_output_stream_write (out_stream,
192
                         buff,
193
                         len,
194
                         NULL,
195
                         err);
196
  
197
  if (err) {
198
    g_object_unref (out_stream);
199
    g_free (len);
200
    g_free (buff);
201
    return FALSE;
202
  }
203
  
204
  g_object_unref (out_stream);
205
  g_free (len);
206
  g_free (buff);
207
  
208
  return TRUE;
209
#endif
210
}
211
212
20 by Gustav Hatvigsson
* Added a singeltonian class to keep settings data in.
213
/* * Implementation of the constructor ****************************************
214
 *
215
 * See: blogs.gnome.org/xclaesse/2010/02/11/how-to-make-a-gobject-singleton/
216
 * for more information.
217
 */
218
GObject * _constructor (GType type,
219
                        guint n_construct_params,
220
                        GObjectConstructParam * construct_params) {
221
  static GObject * self = NULL;
222
  static GMutex mutex;
223
  
224
  g_mutex_lock (&mutex);
225
  if (self == NULL) {
226
    self = G_OBJECT_CLASS (gpump_settings_data_parent_class)->constructor (
227
      type, n_construct_params, construct_params);
228
    g_object_add_weak_pointer (self, (gpointer) &self);
229
    g_mutex_unlock (&mutex);
230
    return self;
231
  }
232
  g_mutex_unlock (&mutex);
233
  return g_object_ref (self);
234
}
235
236
237