summaryrefslogtreecommitdiff
path: root/libcaja-private/caja-desktop-metadata.c
blob: 7fad726f2e11782ec4090088b6fb5410454c9647 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
 * Caja
 *
 * Copyright (C) 2011 Red Hat, Inc.
 *               2012 Stefano Karapetsas
 * Copyright (C) 2012-2021 The MATE developers
 *
 * Caja is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * Caja is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; see the file COPYING.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * Authors: Cosimo Cecchi <cosimoc@redhat.com>
 *          Stefano Karapetsas <stefano@karapetsas.com>
 */

#include <config.h>

#include "caja-desktop-metadata.h"

#include "caja-directory-notify.h"
#include "caja-file-private.h"
#include "caja-file-utilities.h"

#include <glib/gstdio.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

static guint save_in_idle_source_id = 0;

static gchar *
get_keyfile_path (void)
{
    gchar *xdg_dir, *retval;

    xdg_dir = caja_get_user_directory ();
    retval = g_build_filename (xdg_dir, "desktop-metadata", NULL);

    g_free (xdg_dir);

    return retval;
}

static gboolean
save_in_idle_cb (gpointer data)
{
    GKeyFile *keyfile = data;
    gchar *contents, *filename;
    gsize length;
    GError *error = NULL;

    save_in_idle_source_id = 0;

    contents = g_key_file_to_data (keyfile, &length, NULL);
    filename = get_keyfile_path ();

    if (contents != NULL) {
        g_file_set_contents (filename,
                     contents, length,
                     &error);
        g_free (contents);
    }

    if (error != NULL) {
        g_warning ("Couldn't save the desktop metadata keyfile to disk: %s",
               error->message);
        g_error_free (error);
    }

    g_free (filename);

    return FALSE;
}

static void
save_in_idle (GKeyFile *keyfile)
{
    if (save_in_idle_source_id != 0) {
        g_source_remove (save_in_idle_source_id);
    }

    save_in_idle_source_id = g_idle_add (save_in_idle_cb, keyfile);
}

static GKeyFile *
load_metadata_keyfile (void)
{
      GKeyFile *retval;
    GError *error = NULL;
    gchar *filename;

    retval = g_key_file_new ();
    filename = get_keyfile_path ();

    g_key_file_load_from_file (retval,
                   filename,
                   G_KEY_FILE_NONE,
                   &error);

    if (error != NULL) {
        if (!g_error_matches (error,
                      G_FILE_ERROR,
                      G_FILE_ERROR_NOENT)) {
            g_print ("Unable to open the desktop metadata keyfile: %s\n",
                 error->message);
        }

        g_error_free (error);
    }

    g_free (filename);

    return retval;
}

static GKeyFile *
get_keyfile (void)
{
    static gboolean keyfile_loaded = FALSE;
    static GKeyFile *keyfile = NULL;

    if (!keyfile_loaded) {
        keyfile = load_metadata_keyfile ();
        keyfile_loaded = TRUE;
    }

    return keyfile;
}

void
caja_desktop_set_metadata_string (CajaFile *file,
                                      const gchar *name,
                                      const gchar *key,
                                      const gchar *string)
{
    GKeyFile *keyfile;
    GError *error = NULL;

    keyfile = get_keyfile ();

    if (string != NULL) {
	    g_key_file_set_string (keyfile,
		           name,
		           key,
		           string);
    } else {
	    /* NULL as value is taken to mean that we want to remove the key */

	    g_key_file_remove_key (keyfile,
		                   name,
		                   key,
		                   &error);

	    if (error != NULL) {
		g_warning ("Couldn't remove the key '%s' from '%s' in the keyfile: %s",
		       key,
		       name,
		       error->message);
		g_error_free (error);
	    }
    }

    save_in_idle (keyfile);

    if (caja_desktop_update_metadata_from_keyfile (file, name)) {
        caja_file_changed (file);
    }
}

#define STRV_TERMINATOR "@x-caja-desktop-metadata-term@"

void
caja_desktop_set_metadata_stringv (CajaFile *file,
                                       const char *name,
                                       const char *key,
                                       const char * const *stringv)
{
    GKeyFile *keyfile;
    guint length;
    gchar **actual_stringv = NULL;
    gboolean free_strv = FALSE;

    keyfile = get_keyfile ();

    /* if we would be setting a single-length strv, append a fake
     * terminator to the array, to be able to differentiate it later from
     * the single string case
     */
    length = g_strv_length ((gchar **) stringv);

    if (length == 1) {
        actual_stringv = g_malloc0 (3 * sizeof (gchar *));
        actual_stringv[0] = (gchar *) stringv[0];
        actual_stringv[1] = STRV_TERMINATOR;
        actual_stringv[2] = NULL;

        length = 2;
        free_strv = TRUE;
    } else {
        actual_stringv = (gchar **) stringv;
    }

    g_key_file_set_string_list (keyfile,
                    name,
                    key,
                    (const gchar **) actual_stringv,
                    length);

    save_in_idle (keyfile);

    if (caja_desktop_update_metadata_from_keyfile (file, name)) {
        caja_file_changed (file);
    }

    if (free_strv) {
        g_free (actual_stringv);
    }
}

gboolean
caja_desktop_update_metadata_from_keyfile (CajaFile *file,
                           const gchar *name)
{
    gchar **keys, **values;
    const gchar *actual_values[2];
    const gchar *value;
    gsize length, values_length;
    GKeyFile *keyfile;
    GFileInfo *info;
    gsize idx;
    gboolean res;

    keyfile = get_keyfile ();

    keys = g_key_file_get_keys (keyfile,
                    name,
                    &length,
                    NULL);

    if (keys == NULL) {
        return FALSE;
    }

    info = g_file_info_new ();

    for (idx = 0; idx < length; idx++) {
        const gchar *key;
        gchar *gio_key;

        key = keys[idx];
        values = g_key_file_get_string_list (keyfile,
                             name,
                             key,
                             &values_length,
                             NULL);

        gio_key = g_strconcat ("metadata::", key, NULL);

        if (values_length < 1) {
            continue;
        } else if (values_length == 1) {
            g_file_info_set_attribute_string (info,
                              gio_key,
                              values[0]);
        } else if (values_length == 2) {
            /* deal with the fact that single-length strv are stored
             * with an additional terminator in the keyfile string, to differentiate
             * them from the regular string case.
             */
            value = values[1];

            if (g_strcmp0 (value, STRV_TERMINATOR) == 0) {
                /* if the 2nd value is the terminator, remove it */
                actual_values[0] = values[0];
                actual_values[1] = NULL;

                g_file_info_set_attribute_stringv (info,
                                   gio_key,
                                   (gchar **) actual_values);
            } else {
                /* otherwise, set it as a regular strv */
                g_file_info_set_attribute_stringv (info,
                                   gio_key,
                                   values);
            }
        } else {
            g_file_info_set_attribute_stringv (info,
                               gio_key,
                               values);
        }

        g_free (gio_key);
        g_strfreev (values);
    }

    res = caja_file_update_metadata_from_info (file, info);

    g_strfreev (keys);
    g_object_unref (info);

    return res;
}