aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ephy-file-helpers.c
blob: c8ce0ce08bc5b8e530e3312e0ed7a13ea3e8650b (plain) (blame)
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 *  Copyright (C) 2002 Jorn Baayen
 *
 *  This program 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, or (at your option)
 *  any later version.
 *
 *  This program 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; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *  $Id$
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <libgnome/gnome-init.h>

#include "ephy-file-helpers.h"
#include "ephy-prefs.h"
#include "eel-gconf-extensions.h"

static GHashTable *files = NULL;

static char *dot_dir = NULL;
static char *tmp_dir = NULL;
static GList *del_on_exit = NULL;

const char *
ephy_file_tmp_dir (void)
{
    if (tmp_dir == NULL)
    {
        char *partial_name;
        char *full_name;

        partial_name = g_strconcat ("epiphany-", g_get_user_name (),
                        "-XXXXXX", NULL);
        full_name = g_build_filename (g_get_tmp_dir (), partial_name,
                          NULL);
        tmp_dir = mkdtemp (full_name);
        g_free (partial_name);

        if (tmp_dir == NULL)
        {
            g_free (full_name);
        }
    }

    return tmp_dir;
}

char *
ephy_file_downloads_dir (void)
{
    const char *translated_folder;
    char *converted, *downloads_dir;
    gboolean desktop_is_home;

    /* The name of the default downloads folder */
    translated_folder = _("Downloads");

    converted = g_filename_from_utf8 (translated_folder, -1, NULL, 
                      NULL, NULL);

    desktop_is_home = eel_gconf_get_boolean (CONF_DESKTOP_IS_HOME_DIR);

    if (desktop_is_home)
    {
        downloads_dir = g_build_filename
            (g_get_home_dir (), converted, NULL);
    }
    else
    {
        downloads_dir = g_build_filename
            (g_get_home_dir (), "Desktop", converted, NULL);
    }

    g_free (converted);

    return downloads_dir;
}

char *
ephy_file_tmp_filename (const char *base,
            const char *extension)
{
    int fd;
    char *name = g_strdup (base);

    fd = mkstemp (name);

    if (fd != -1)
    {
        unlink (name);
        close (fd);
    }
    else
    {
        g_free (name);

        return NULL;
    }

    if (extension)
    {
        char *tmp;
        tmp = g_strconcat (name, ".",
                   extension, NULL);
        g_free (name);
        name = tmp;
    }

    return name;
}

const char *
ephy_file (const char *filename)
{
    char *ret;
    int i;

    static char *paths[] =
    {
        SHARE_DIR "/",
        SHARE_DIR "/glade/",
        SHARE_DIR "/art/",
        SHARE_UNINSTALLED_DIR "/",
        SHARE_UNINSTALLED_DIR "/glade/",
        SHARE_UNINSTALLED_DIR "/art/"
    };

    g_assert (files != NULL);

    ret = g_hash_table_lookup (files, filename);
    if (ret != NULL)
        return ret;

    for (i = 0; i < (int) G_N_ELEMENTS (paths); i++)
    {
        ret = g_strconcat (paths[i], filename, NULL);
        if (g_file_test (ret, G_FILE_TEST_EXISTS) == TRUE)
        {
            g_hash_table_insert (files, g_strdup (filename), ret);
            return (const char *) ret;
        }
        g_free (ret);
    }

    g_warning (_("Failed to find %s"), filename);

    return NULL;
}

const char *
ephy_dot_dir (void)
{
    if (dot_dir == NULL)
    {
        dot_dir = g_build_filename (g_get_home_dir (),
                        GNOME_DOT_GNOME,
                        "epiphany",
                        NULL);
    }

    return dot_dir;
}

void
ephy_file_helpers_init (void)
{
    files = g_hash_table_new_full (g_str_hash,
                       g_str_equal,
                       (GDestroyNotify) g_free,
                       (GDestroyNotify) g_free);
}

static void
delete_files (GList *l)
{
    for (; l != NULL; l = l->next)
    {
        unlink (l->data);
    }
}

void
ephy_file_helpers_shutdown (void)
{
    g_hash_table_destroy (files);

    del_on_exit = g_list_reverse (del_on_exit);
    delete_files (del_on_exit);
    g_list_foreach (del_on_exit, (GFunc)g_free, NULL);
    g_list_free (del_on_exit);
    del_on_exit = NULL;

    if (tmp_dir != NULL)
    {
        rmdir (tmp_dir);
        g_free (tmp_dir);
        tmp_dir = NULL;
    }

    g_free (dot_dir);
    dot_dir = NULL;
}

void
ephy_ensure_dir_exists (const char *dir)
{
    if (g_file_test (dir, G_FILE_TEST_IS_DIR) == FALSE)
    {
        if (g_file_test (dir, G_FILE_TEST_EXISTS) == TRUE)
            g_error (_("%s exists, please move it out of the way."), dir);

        if (mkdir (dir, 488) != 0)
            g_error (_("Failed to create directory %s."), dir);
    }
}

static void
ephy_find_file_recursive (const char *path,
              const char *fname, GSList **l,
              gint depth, gint maxdepth)
{
    GDir *d = g_dir_open (path, 0, NULL);
    const gchar *f;
    if (d)
    {
        while ((f = g_dir_read_name (d)))
        {
            char *new_path = g_build_filename (path, f, NULL);
            if (depth < maxdepth)
            {
                ephy_find_file_recursive (new_path, fname, l,
                              depth + 1, maxdepth);
            }
            if (!strcmp (f, fname))
            {
                *l = g_slist_prepend (*l, new_path);
            }
            else
            {
                g_free (new_path);
            }
        }
        g_dir_close (d);
    }
}

GSList *
ephy_file_find (const char *path,
            const char *fname,
            gint maxdepth)
{
    GSList *ret = NULL;
    ephy_find_file_recursive (path, fname, &ret, 0, maxdepth);
    return ret;
}

gboolean
ephy_file_switch_temp_file (const char *filename,
                const char *filename_temp)
{
    char *old_file;
    gboolean old_exist;
    gboolean retval = TRUE;

    old_file = g_strconcat (filename, ".old", NULL);

    old_exist = g_file_test (filename, G_FILE_TEST_EXISTS);

    if (old_exist)
    {
        if (rename (filename, old_file) < 0)
        {
            g_warning ("Failed to rename %s to %s", filename, old_file);
            retval = FALSE;
            goto failed;
        }
    }

    if (rename (filename_temp, filename) < 0)
    {
        g_warning ("Failed to rename %s to %s", filename_temp, filename);

        if (rename (old_file, filename) < 0)
        {
            g_warning ("Failed to restore %s from %s",
                   filename, filename_temp);
        }
        retval = FALSE;
        goto failed;
    }

    if (old_exist)
    {
        if (unlink (old_file) < 0)
        {
            g_warning ("Failed to delete old file %s", old_file);
        }
    }

failed:
    g_free (old_file);

    return retval;
}

void
ephy_file_delete_on_exit (const char *path)
{
    del_on_exit = g_list_prepend (del_on_exit,
                      g_strdup (path));
}