~jan-hrdina/ubuntu/quantal/xfce4-places-plugin/1.4.0-upstream-import

« back to all changes in this revision

Viewing changes to panel-plugin/model_user.c

  • Committer: Bazaar Package Importer
  • Author(s): Gauvain Pocentek
  • Date: 2007-04-22 08:55:28 UTC
  • Revision ID: james.westby@ubuntu.com-20070422085528-ijr73vydcqg5l830
Tags: upstream-0.2.0
ImportĀ upstreamĀ versionĀ 0.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  xfce4-places-plugin
 
2
 *
 
3
 *  Copyright (c) 2007 Diego Ongaro <ongardie@gmail.com>
 
4
 *
 
5
 *  This program is free software; you can redistribute it and/or modify
 
6
 *  it under the terms of the GNU General Public License as published by
 
7
 *  the Free Software Foundation; either version 2 of the License, or
 
8
 *  (at your option) any later version.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU Library General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
18
 */
 
19
 
 
20
#ifdef HAVE_CONFIG_H
 
21
#  include <config.h>
 
22
#endif
 
23
 
 
24
#include "model_system.h"
 
25
#include "model_user.h"
 
26
#include "model.h"
 
27
 
 
28
#include <libxfce4util/libxfce4util.h>
 
29
#include <glib/gstdio.h>
 
30
 
 
31
 
 
32
#define bookmarks_user_dir_exists(path)    g_file_test(path, G_FILE_TEST_IS_DIR)
 
33
 
 
34
struct _BookmarksUser
 
35
{
 
36
  GPtrArray             *bookmarks;
 
37
  gchar                 *filename;
 
38
  time_t                 loaded;
 
39
 
 
40
  const BookmarksSystem *system;
 
41
 
 
42
};
 
43
 
 
44
// internal use
 
45
 
 
46
static void
 
47
places_bookmarks_user_reinit(BookmarksUser *b)
 
48
{
 
49
    DBG("initializing");
 
50
    // As of 2007-04-06, this is pretty much taken from/analogous to Thunar
 
51
 
 
52
    BookmarkInfo *bi;
 
53
    gchar  *name;
 
54
    gchar  *path;
 
55
    gchar   line[2048];
 
56
    FILE   *fp;
 
57
 
 
58
    fp = fopen(b->filename, "r");
 
59
 
 
60
    if(G_UNLIKELY(fp == NULL)){
 
61
        DBG("Error opening gtk bookmarks file");
 
62
        return;
 
63
    }
 
64
 
 
65
    while( fgets(line, sizeof(line), fp) != NULL )
 
66
    {
 
67
        /* strip leading/trailing whitespace */
 
68
        g_strstrip(line);
 
69
        
 
70
        /* skip over the URI */
 
71
        for (name = line; *name != '\0' && !g_ascii_isspace (*name); ++name)
 
72
            /* pass */;
 
73
        
 
74
        /* zero-terminate the URI */
 
75
        *name++ = '\0';
 
76
        
 
77
        /* check if we have a name */
 
78
        for (; g_ascii_isspace (*name); ++name)
 
79
            /* pass */;
 
80
        
 
81
        /* parse the URI */ // TODO: trash:// URI's
 
82
        path = g_filename_from_uri(line, NULL, NULL);
 
83
        if (G_UNLIKELY(path == NULL || *path == '\0'))
 
84
            continue;
 
85
 
 
86
        /* if we don't have a name, find it in the path */
 
87
        if(*name == '\0'){
 
88
            name = g_filename_display_basename(path);
 
89
            if(*name == '\0'){
 
90
                g_free(path);
 
91
                continue;
 
92
            }
 
93
        }else{
 
94
            name = g_strdup(name);
 
95
        }
 
96
 
 
97
        /* create the BookmarkInfo container */
 
98
        bi = g_new0(BookmarkInfo, 1);
 
99
        bi->uri = path;
 
100
        bi->label = name;
 
101
        bi->show = bookmarks_user_dir_exists(bi->uri);
 
102
        bi->icon = g_strdup("gnome-fs-directory");
 
103
 
 
104
        places_bookmarks_system_bi_system_mod((BookmarksSystem*) b->system, bi);
 
105
 
 
106
        g_ptr_array_add(b->bookmarks, bi);
 
107
    }
 
108
 
 
109
    fclose(fp);
 
110
}
 
111
 
 
112
static time_t
 
113
places_bookmarks_user_get_mtime(BookmarksUser *b)
 
114
{
 
115
    struct stat buf;
 
116
    if(g_stat(b->filename, &buf) == 0)
 
117
        return buf.st_mtime;
 
118
    return 0;
 
119
}
 
120
 
 
121
// external interface
 
122
 
 
123
BookmarksUser*
 
124
places_bookmarks_user_init(const BookmarksSystem *system)
 
125
 
126
    BookmarksUser *b = g_new0(BookmarksUser, 1);
 
127
    
 
128
    g_assert(system != NULL);
 
129
    b->system = system;
 
130
 
 
131
    b->filename = xfce_get_homefile(".gtk-bookmarks", NULL);
 
132
    b->bookmarks = g_ptr_array_new();
 
133
    b->loaded = places_bookmarks_user_get_mtime(b);
 
134
    
 
135
    places_bookmarks_user_reinit(b);
 
136
    return b;
 
137
}
 
138
 
 
139
gboolean
 
140
places_bookmarks_user_changed(BookmarksUser *b)
 
141
{
 
142
    // see if the file has changed
 
143
    time_t mtime = places_bookmarks_user_get_mtime(b);
 
144
    
 
145
    if(mtime > b->loaded){
 
146
        g_ptr_array_free(b->bookmarks, TRUE);
 
147
        b->bookmarks = g_ptr_array_new();
 
148
        b->loaded = mtime;
 
149
        places_bookmarks_user_reinit(b);
 
150
        return TRUE;
 
151
    }
 
152
 
 
153
    // see if any directories have been created or removed
 
154
    guint k;
 
155
    BookmarkInfo *bi;
 
156
    gboolean ret = FALSE;
 
157
 
 
158
    for(k=0; k < b->bookmarks->len; k++){
 
159
        bi = g_ptr_array_index(b->bookmarks, k);
 
160
        if(bi->show != bookmarks_user_dir_exists(bi->uri)){
 
161
            bi->show = !bi->show;
 
162
            ret = TRUE;
 
163
        }
 
164
    }
 
165
 
 
166
    return ret;
 
167
}
 
168
 
 
169
void
 
170
places_bookmarks_user_visit(BookmarksUser *b,  BookmarksVisitor *visitor)
 
171
{
 
172
    guint k;
 
173
    BookmarkInfo *bi;
 
174
    
 
175
    for(k=0; k < b->bookmarks->len; k++){
 
176
        bi = g_ptr_array_index(b->bookmarks, k);
 
177
        if(bi->show)
 
178
            visitor->item(visitor->pass_thru, bi->label, bi->uri, bi->icon);
 
179
    }
 
180
}
 
181
 
 
182
void
 
183
places_bookmarks_user_finalize(BookmarksUser *b)
 
184
{
 
185
    g_ptr_array_free(b->bookmarks, TRUE);
 
186
    b->bookmarks = NULL;
 
187
 
 
188
    g_free(b->filename);
 
189
    b->filename = NULL;
 
190
 
 
191
    b->system = NULL;
 
192
 
 
193
    g_free(b);
 
194
}
 
195
 
 
196
// vim: ai et tabstop=4