~ubuntu-branches/ubuntu/hardy/pcmanfm/hardy-backports

« back to all changes in this revision

Viewing changes to src/ptk/ptk-bookmarks.c

  • Committer: Bazaar Package Importer
  • Author(s): J?r?me Guelfucci
  • Date: 2008-07-01 00:40:37 UTC
  • mfrom: (5.1.3 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080701004037-q6pfacskp0xnk10k
Tags: 0.4.3-1~hardy1
Automated backport upload; no source changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
*/
12
12
 
13
13
#include "ptk-bookmarks.h"
 
14
#include "vfs-file-monitor.h"
14
15
 
15
16
#include <stdio.h>
16
17
#include <string.h>
 
18
#include <gtk/gtk.h>
17
19
 
18
20
const char bookmarks_file_name[] = ".gtk-bookmarks";
19
21
static PtkBookmarks bookmarks = {0};
 
22
static VFSFileMonitor* monitor = NULL;
20
23
 
21
24
typedef struct
22
25
{
41
44
    }
42
45
}
43
46
 
44
 
/*
45
 
  Get a self-maintained list of bookmarks
46
 
  This is read from "~/.gtk-bookmarks".
47
 
*/
48
 
PtkBookmarks* ptk_bookmarks_get ()
 
47
static void load( const char* path )
49
48
{
50
49
    FILE* file;
51
 
    gchar* path;
52
50
    gchar* upath;
53
51
    gchar* uri;
54
52
    gchar* item;
57
55
    char line[1024];
58
56
    gsize name_len, upath_len;
59
57
 
 
58
    file = fopen( path, "r" );
 
59
    if( file )
 
60
    {
 
61
        while( fgets( line, sizeof(line), file ) )
 
62
        {
 
63
            /* Every line is an URI containing no space charactetrs
 
64
               with its name appended (optional) */
 
65
            uri = strtok( line, " \r\n" );
 
66
            if( ! uri || !*uri )
 
67
                continue;
 
68
            path = g_filename_from_uri(uri, NULL, NULL);
 
69
            if( path )
 
70
            {
 
71
                upath = g_filename_to_utf8(path, -1, NULL, &upath_len, NULL);
 
72
                g_free( (gpointer) path );
 
73
                if( upath )
 
74
                {
 
75
                    name = strtok( NULL, "\r\n" );
 
76
                    if( name )
 
77
                    {
 
78
                        name_len = strlen( name );
 
79
                        basename = NULL;
 
80
                    }
 
81
                    else
 
82
                    {
 
83
                        name = basename = g_path_get_basename( upath );
 
84
                        name_len = strlen( basename );
 
85
                    }
 
86
                    item = ptk_bookmarks_item_new( name, name_len,
 
87
                                                   upath, upath_len );
 
88
                    bookmarks.list = g_list_append( bookmarks.list,
 
89
                                                    item );
 
90
                    g_free(upath);
 
91
                    g_free( basename );
 
92
                }
 
93
            }
 
94
        }
 
95
        fclose( file );
 
96
    }
 
97
}
 
98
 
 
99
static void on_bookmark_file_changed( VFSFileMonitor* fm,
 
100
                                        VFSFileMonitorEvent event,
 
101
                                        const char* file_name,
 
102
                                        gpointer user_data )
 
103
{
 
104
    /* This callback is called from IO channel handler insode VFSFileMonotor. */
 
105
    GDK_THREADS_ENTER();
 
106
 
 
107
    g_list_foreach( bookmarks.list, (GFunc)g_free, NULL );
 
108
    g_list_free( bookmarks.list );
 
109
    bookmarks.list = 0;
 
110
 
 
111
    load( file_name );
 
112
 
 
113
    ptk_bookmarks_notify();
 
114
    GDK_THREADS_LEAVE();
 
115
}
 
116
 
 
117
/*
 
118
  Get a self-maintained list of bookmarks
 
119
  This is read from "~/.gtk-bookmarks".
 
120
*/
 
121
PtkBookmarks* ptk_bookmarks_get ()
 
122
{
 
123
    gchar* path;
60
124
    if( 0 == bookmarks.n_ref )
61
125
    {
62
126
        path = g_build_filename( g_get_home_dir(), bookmarks_file_name, NULL );
63
 
        file = fopen( path, "r" );
 
127
        monitor = vfs_file_monitor_add_file( path, on_bookmark_file_changed, NULL );
 
128
        load( path );
64
129
        g_free( path );
65
 
        if( file )
66
 
        {
67
 
            while( fgets( line, sizeof(line), file ) )
68
 
            {
69
 
                /* Every line is an URI containing no space charactetrs
70
 
                   with its name appended (optional) */
71
 
                uri = strtok( line, " \r\n" );
72
 
                if( ! uri || !*uri )
73
 
                    continue;
74
 
                path = g_filename_from_uri(uri, NULL, NULL);
75
 
                if( path )
76
 
                {
77
 
                    upath = g_filename_to_utf8(path, -1, NULL, &upath_len, NULL);
78
 
                    g_free( path );
79
 
                    if( upath )
80
 
                    {
81
 
                        name = strtok( NULL, "\r\n" );
82
 
                        if( name )
83
 
                        {
84
 
                            name_len = strlen( name );
85
 
                            basename = NULL;
86
 
                        }
87
 
                        else
88
 
                        {
89
 
                            name = basename = g_path_get_basename( upath );
90
 
                            name_len = strlen( basename );
91
 
                        }
92
 
                        item = ptk_bookmarks_item_new( name, name_len,
93
 
                                                       upath, upath_len );
94
 
                        bookmarks.list = g_list_append( bookmarks.list,
95
 
                                                        item );
96
 
                        g_free(upath);
97
 
                        g_free( basename );
98
 
                    }
99
 
                }
100
 
            }
101
 
            fclose( file );
102
 
        }
103
130
    }
104
 
    ++bookmarks.n_ref;
 
131
    g_atomic_int_inc( &bookmarks.n_ref );
105
132
    return &bookmarks;
106
133
}
107
134
 
115
142
    g_list_foreach( bookmarks.list, (GFunc)g_free, NULL );
116
143
    g_list_free( bookmarks.list );
117
144
    bookmarks.list = new_list;
118
 
    ptk_bookmarks_notify();
 
145
 
 
146
    ptk_bookmarks_save();
 
147
    /* We don't need to fire the notify since this will be done by FAM. */
 
148
    /* ptk_bookmarks_notify(); */
119
149
}
120
150
 
121
151
/* Insert an item into bookmarks */
126
156
    bookmarks.list = g_list_insert( bookmarks.list,
127
157
                                    item, pos );
128
158
    ptk_bookmarks_notify();
 
159
    ptk_bookmarks_save();
129
160
}
130
161
 
131
162
/* Append an item into bookmarks */
136
167
    bookmarks.list = g_list_append( bookmarks.list,
137
168
                                    item );
138
169
    ptk_bookmarks_notify();
 
170
    ptk_bookmarks_save();
139
171
}
140
172
 
141
173
static GList* find_item( const char* path )
165
197
    {
166
198
        g_free( l->data );
167
199
        bookmarks.list = g_list_delete_link( bookmarks.list, l );
 
200
 
168
201
        ptk_bookmarks_notify();
 
202
        ptk_bookmarks_save();
169
203
    }
170
204
}
171
205
 
180
214
                                      path, strlen(path));
181
215
        g_free( l->data );
182
216
        l->data = item;
 
217
 
183
218
        ptk_bookmarks_notify();
 
219
        ptk_bookmarks_save();
184
220
    }
185
221
}
186
222
 
188
224
{
189
225
    gchar* item;
190
226
    const gchar* upath;
191
 
    int len;
192
227
    char* uri;
193
228
    char* path;
194
229
 
261
296
 
262
297
void ptk_bookmarks_unref ()
263
298
{
264
 
    --bookmarks.n_ref;
265
 
    if( bookmarks.n_ref <= 0 )
 
299
    if( g_atomic_int_dec_and_test(&bookmarks.n_ref) )
266
300
    {
 
301
        vfs_file_monitor_remove( monitor, on_bookmark_file_changed, NULL );
 
302
        monitor = NULL;
 
303
 
267
304
        bookmarks.n_ref = 0;
268
305
        if( bookmarks.list )
269
306
        {