~ubuntu-branches/ubuntu/utopic/nautilus/utopic-proposed

« back to all changes in this revision

Viewing changes to libnautilus-private/nautilus-desktop-metadata.c

  • Committer: Package Import Robot
  • Author(s): Tim Lunn
  • Date: 2014-01-02 16:50:06 UTC
  • mfrom: (1.17.43)
  • Revision ID: package-import@ubuntu.com-20140102165006-1ghk9ropzitdc8pp
Tags: 1:3.10.1-0ubuntu1
* New upstream release
* debian/control.in: bump depend on gtk+ 3.9.11
* debian/patches:
  - Dropped git patches, that are included in new version
  - 06_never_exec_nonexec_launchers.patch, refreshed
  - disconnect-extra-location-widgets.patch, dropped included in new version
  - 16_unity_new_documents.patch, refreshed
  - ubuntu_show_titlebar.patch, disable CSD titlebar on non-GNOME sessions
  - ubuntu_titlebar_css.patch, theme fixes (LP: #1272602)

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 
27
27
#include "nautilus-desktop-metadata.h"
28
28
 
29
 
#include "nautilus-directory-notify.h"
30
 
#include "nautilus-file-private.h"
31
29
#include "nautilus-file-utilities.h"
32
 
 
33
 
#include <glib/gstdio.h>
34
 
 
35
 
#include <sys/types.h>
36
 
#include <sys/stat.h>
37
 
#include <fcntl.h>
38
 
 
39
 
static guint save_in_idle_source_id = 0;
 
30
#include "nautilus-keyfile-metadata.h"
40
31
 
41
32
static gchar *
42
33
get_keyfile_path (void)
47
38
        retval = g_build_filename (xdg_dir, "desktop-metadata", NULL);
48
39
 
49
40
        g_free (xdg_dir);
50
 
        
51
 
        return retval;
52
 
}
53
 
 
54
 
static gboolean
55
 
save_in_idle_cb (gpointer data)
56
 
{
57
 
        GKeyFile *keyfile = data;
58
 
        gchar *contents, *filename;
59
 
        gsize length;
60
 
        GError *error = NULL;
61
 
 
62
 
        save_in_idle_source_id = 0;
63
 
 
64
 
        contents = g_key_file_to_data (keyfile, &length, NULL);
65
 
        filename = get_keyfile_path ();
66
 
 
67
 
        if (contents != NULL) {
68
 
                g_file_set_contents (filename,
69
 
                                     contents, length,
70
 
                                     &error);
71
 
                g_free (contents);
72
 
        }
73
 
 
74
 
        if (error != NULL) {
75
 
                g_warning ("Couldn't save the desktop metadata keyfile to disk: %s",
76
 
                           error->message);
77
 
                g_error_free (error);
78
 
        }
79
 
 
80
 
        g_free (filename);
81
 
 
82
 
        return FALSE;
83
 
}
84
 
 
85
 
static void
86
 
save_in_idle (GKeyFile *keyfile)
87
 
{
88
 
        if (save_in_idle_source_id != 0) {
89
 
                g_source_remove (save_in_idle_source_id);
90
 
        }
91
 
 
92
 
        save_in_idle_source_id = g_idle_add (save_in_idle_cb, keyfile);
93
 
}
94
 
 
95
 
static GKeyFile *
96
 
load_metadata_keyfile (void)
97
 
{
98
 
        GKeyFile *retval;
99
 
        GError *error = NULL;
100
 
        gchar *filename;
101
 
 
102
 
        retval = g_key_file_new ();
103
 
        filename = get_keyfile_path ();
104
 
 
105
 
        g_key_file_load_from_file (retval,
106
 
                                   filename,
107
 
                                   G_KEY_FILE_NONE,
108
 
                                   &error);
109
 
 
110
 
        if (error != NULL) {
111
 
                if (!g_error_matches (error,
112
 
                                      G_FILE_ERROR,
113
 
                                      G_FILE_ERROR_NOENT)) {
114
 
                        g_print ("Unable to open the desktop metadata keyfile: %s\n",
115
 
                                 error->message);
116
 
                }
117
 
 
118
 
                g_error_free (error);
119
 
        }
120
 
 
121
 
        g_free (filename);
122
 
 
123
 
        return retval;
124
 
}
125
 
 
126
 
static GKeyFile *
127
 
get_keyfile (void)
128
 
{
129
 
        static gboolean keyfile_loaded = FALSE;
130
 
        static GKeyFile *keyfile = NULL;
131
 
 
132
 
        if (!keyfile_loaded) {
133
 
                keyfile = load_metadata_keyfile ();
134
 
                keyfile_loaded = TRUE;
135
 
        }
136
 
 
137
 
        return keyfile;
 
41
 
 
42
        return retval;
138
43
}
139
44
 
140
45
void
143
48
                                      const gchar *key,
144
49
                                      const gchar *string)
145
50
{
146
 
        GKeyFile *keyfile;
147
 
 
148
 
        keyfile = get_keyfile ();
149
 
 
150
 
        g_key_file_set_string (keyfile,
151
 
                               name,
152
 
                               key,
153
 
                               string);
154
 
 
155
 
        save_in_idle (keyfile);
156
 
 
157
 
        if (nautilus_desktop_update_metadata_from_keyfile (file, name)) {
158
 
                nautilus_file_changed (file);
159
 
        }       
 
51
        gchar *keyfile_filename;
 
52
 
 
53
        keyfile_filename = get_keyfile_path ();
 
54
 
 
55
        nautilus_keyfile_metadata_set_string (file, keyfile_filename,
 
56
                                              name, key, string);
 
57
 
 
58
        g_free (keyfile_filename);
160
59
}
161
60
 
162
 
#define STRV_TERMINATOR "@x-nautilus-desktop-metadata-term@"
163
 
 
164
61
void
165
62
nautilus_desktop_set_metadata_stringv (NautilusFile *file,
166
63
                                       const char *name,
167
64
                                       const char *key,
168
65
                                       const char * const *stringv)
169
66
{
170
 
        GKeyFile *keyfile;
171
 
        guint length;
172
 
        gchar **actual_stringv = NULL;
173
 
        gboolean free_strv = FALSE;
174
 
 
175
 
        keyfile = get_keyfile ();
176
 
 
177
 
        /* if we would be setting a single-length strv, append a fake
178
 
         * terminator to the array, to be able to differentiate it later from
179
 
         * the single string case
180
 
         */
181
 
        length = g_strv_length ((gchar **) stringv);
182
 
 
183
 
        if (length == 1) {
184
 
                actual_stringv = g_malloc0 (3 * sizeof (gchar *));
185
 
                actual_stringv[0] = (gchar *) stringv[0];
186
 
                actual_stringv[1] = STRV_TERMINATOR;
187
 
                actual_stringv[2] = NULL;
188
 
 
189
 
                length = 2;
190
 
                free_strv = TRUE;
191
 
        } else {
192
 
                actual_stringv = (gchar **) stringv;
193
 
        }
194
 
 
195
 
        g_key_file_set_string_list (keyfile,
196
 
                                    name,
197
 
                                    key,
198
 
                                    (const gchar **) actual_stringv,
199
 
                                    length);
200
 
 
201
 
        save_in_idle (keyfile);
202
 
 
203
 
        if (nautilus_desktop_update_metadata_from_keyfile (file, name)) {
204
 
                nautilus_file_changed (file);
205
 
        }
206
 
 
207
 
        if (free_strv) {
208
 
                g_free (actual_stringv);
209
 
        }
 
67
        gchar *keyfile_filename;
 
68
 
 
69
        keyfile_filename = get_keyfile_path ();
 
70
 
 
71
        nautilus_keyfile_metadata_set_stringv (file, keyfile_filename,
 
72
                                               name, key, stringv);
 
73
 
 
74
        g_free (keyfile_filename);
210
75
}
211
76
 
212
77
gboolean
213
78
nautilus_desktop_update_metadata_from_keyfile (NautilusFile *file,
214
79
                                               const gchar *name)
215
80
{
216
 
        gchar **keys, **values;
217
 
        const gchar *actual_values[2];
218
 
        const gchar *key, *value;
219
 
        gchar *gio_key;
220
 
        gsize length, values_length;
221
 
        GKeyFile *keyfile;
222
 
        GFileInfo *info;
223
 
        gint idx;
224
 
        gboolean res;
225
 
 
226
 
        keyfile = get_keyfile ();
227
 
 
228
 
        keys = g_key_file_get_keys (keyfile,
229
 
                                    name,
230
 
                                    &length,
231
 
                                    NULL);
232
 
 
233
 
        if (keys == NULL) {
234
 
                return FALSE;
235
 
        }
236
 
 
237
 
        info = g_file_info_new ();
238
 
 
239
 
        for (idx = 0; idx < length; idx++) {
240
 
                key = keys[idx];
241
 
                values = g_key_file_get_string_list (keyfile,
242
 
                                                     name,
243
 
                                                     key,
244
 
                                                     &values_length,
245
 
                                                     NULL);
246
 
 
247
 
                gio_key = g_strconcat ("metadata::", key, NULL);
248
 
 
249
 
                if (values_length < 1) {
250
 
                        continue;
251
 
                } else if (values_length == 1) {
252
 
                        g_file_info_set_attribute_string (info,
253
 
                                                          gio_key,
254
 
                                                          values[0]);
255
 
                } else if (values_length == 2) {
256
 
                        /* deal with the fact that single-length strv are stored
257
 
                         * with an additional terminator in the keyfile string, to differentiate
258
 
                         * them from the regular string case.
259
 
                         */
260
 
                        value = values[1];
261
 
 
262
 
                        if (g_strcmp0 (value, STRV_TERMINATOR) == 0) {
263
 
                                /* if the 2nd value is the terminator, remove it */
264
 
                                actual_values[0] = values[0];
265
 
                                actual_values[1] = NULL;
266
 
 
267
 
                                g_file_info_set_attribute_stringv (info,
268
 
                                                                   gio_key,
269
 
                                                                   (gchar **) actual_values);
270
 
                        } else {
271
 
                                /* otherwise, set it as a regular strv */
272
 
                                g_file_info_set_attribute_stringv (info,
273
 
                                                                   gio_key,
274
 
                                                                   values);
275
 
                        }
276
 
                } else {
277
 
                        g_file_info_set_attribute_stringv (info,
278
 
                                                           gio_key,
279
 
                                                           values);
280
 
                }
281
 
 
282
 
                g_free (gio_key);
283
 
                g_strfreev (values);
284
 
        }
285
 
 
286
 
        res = nautilus_file_update_metadata_from_info (file, info);
287
 
 
288
 
        g_strfreev (keys);
289
 
        g_object_unref (info);
290
 
 
291
 
        return res;
 
81
        gchar *keyfile_filename;
 
82
        gboolean result;
 
83
 
 
84
        keyfile_filename = get_keyfile_path ();
 
85
 
 
86
        result = nautilus_keyfile_metadata_update_from_keyfile (file,
 
87
                                                                keyfile_filename,
 
88
                                                                name);
 
89
 
 
90
        g_free (keyfile_filename);
 
91
        return result;
292
92
}