~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_volumes.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.h"
 
25
#include "model_volumes.h"
 
26
#include <thunar-vfs/thunar-vfs.h>
 
27
#include <libxfce4util/libxfce4util.h>
 
28
 
 
29
struct _BookmarksVolumes
 
30
{
 
31
    GPtrArray *bookmarks;
 
32
    gboolean   changed;
 
33
    ThunarVfsVolumeManager *volume_manager;
 
34
};
 
35
 
 
36
static gboolean 
 
37
places_bookmarks_volumes_show_volume(ThunarVfsVolume *volume);
 
38
 
 
39
static void
 
40
places_bookmarks_volumes_add(BookmarksVolumes *b, const GList *volumes);
 
41
 
 
42
 
 
43
/********** ThunarVFS Callbacks **********/
 
44
 
 
45
void
 
46
places_bookmarks_volumes_cb_changed(ThunarVfsVolume *volume, 
 
47
                                    BookmarksVolumes *b)
 
48
{
 
49
    DBG("volume changed"); 
 
50
    // unfortunately there tends to be like 3 of these in a row
 
51
 
 
52
    BookmarkInfo *bi;
 
53
    GList *volumes;
 
54
    guint k;
 
55
 
 
56
    if(places_bookmarks_volumes_show_volume(volume)){
 
57
 
 
58
        // make sure it's in the array
 
59
        for(k = 0; k < b->bookmarks->len; k++){
 
60
            bi = g_ptr_array_index(b->bookmarks, k);
 
61
            if(THUNAR_VFS_VOLUME(bi->data) == volume)
 
62
                break;
 
63
        }
 
64
 
 
65
        if(k == b->bookmarks->len){ // it's not there
 
66
            DBG("adding volume to array");
 
67
 
 
68
            volumes = g_list_prepend(NULL, volume);
 
69
            places_bookmarks_volumes_add(b, volumes);
 
70
            g_list_free(volumes);
 
71
            b->changed = TRUE;
 
72
        }else{
 
73
            DBG("volume already in array");
 
74
        }
 
75
 
 
76
    }else{
 
77
        // make sure it's not in the array
 
78
        for(k = 0; k < b->bookmarks->len; k++){
 
79
            bi = g_ptr_array_index(b->bookmarks, k);
 
80
            if(THUNAR_VFS_VOLUME(bi->data) == volume){ // it is there
 
81
                DBG("dropping volume from array");
 
82
                
 
83
                bi = g_ptr_array_remove_index(b->bookmarks, k);
 
84
                g_object_unref(bi->data);
 
85
                bi->data = NULL;
 
86
                g_free(bi);
 
87
                
 
88
                b->changed = TRUE;
 
89
            }
 
90
        }
 
91
    }
 
92
}
 
93
 
 
94
void
 
95
places_bookmarks_volumes_cb_added(ThunarVfsVolumeManager *volume_manager,
 
96
                                  const GList *volumes, 
 
97
                                  BookmarksVolumes *b)
 
98
{
 
99
    DBG("volumes added");
 
100
    places_bookmarks_volumes_add(b, volumes);
 
101
    b->changed = TRUE;
 
102
}
 
103
 
 
104
void
 
105
places_bookmarks_volumes_cb_removed(ThunarVfsVolumeManager *volume_manager, 
 
106
                                    const GList *volumes, 
 
107
                                    BookmarksVolumes *b)
 
108
{
 
109
    DBG("volumes removed");
 
110
 
 
111
    BookmarkInfo *bi;
 
112
    GList *vol_iter;
 
113
    guint k;
 
114
 
 
115
    // step through existing bookmarks
 
116
    for(k = 0; k < b->bookmarks->len; k++){
 
117
        bi = g_ptr_array_index(b->bookmarks, k);
 
118
 
 
119
        // step through removals
 
120
        vol_iter = (GList*) volumes;
 
121
        while(vol_iter){
 
122
            if(bi->data == vol_iter->data){ // it is there
 
123
                
 
124
                // delete the bookmark
 
125
                bi = g_ptr_array_remove_index(b->bookmarks, k);
 
126
                DBG("Removing bookmark %s", bi->label);
 
127
                
 
128
                if(bi->data != NULL){
 
129
                    g_object_unref(bi->data);
 
130
                    bi->data = NULL;
 
131
                }
 
132
                g_free(bi);
 
133
                
 
134
                b->changed = TRUE;
 
135
                break;
 
136
            }
 
137
 
 
138
            vol_iter = vol_iter->next;
 
139
        }
 
140
    }
 
141
}
 
142
 
 
143
// internal
 
144
static gboolean
 
145
places_bookmarks_volumes_show_volume(ThunarVfsVolume *volume){
 
146
    
 
147
    DBG("Volume: %s [mounted=%x removable=%x present=%x]", thunar_vfs_volume_get_name(volume), 
 
148
                                                           thunar_vfs_volume_is_mounted(volume),
 
149
                                                           thunar_vfs_volume_is_removable(volume), 
 
150
                                                           thunar_vfs_volume_is_present(volume));
 
151
 
 
152
    return thunar_vfs_volume_is_mounted(volume) && 
 
153
           thunar_vfs_volume_is_removable(volume) && 
 
154
           thunar_vfs_volume_is_present(volume);
 
155
}
 
156
 
 
157
 
 
158
static void
 
159
places_bookmarks_volumes_add(BookmarksVolumes *b, const GList *volumes)
 
160
{
 
161
    ThunarVfsVolume *volume;
 
162
    BookmarkInfo *bi;
 
163
    GtkIconTheme *icon_theme = gtk_icon_theme_get_default();
 
164
 
 
165
    while(volumes){
 
166
        volume = THUNAR_VFS_VOLUME(volumes->data);
 
167
        
 
168
        g_signal_connect (volume, "changed",
 
169
                      G_CALLBACK(places_bookmarks_volumes_cb_changed), b);
 
170
 
 
171
        if(places_bookmarks_volumes_show_volume(volume)){
 
172
 
 
173
            g_object_ref(volume);
 
174
 
 
175
            bi          = g_new0(BookmarkInfo, 1);
 
176
            bi->label   = g_strdup( thunar_vfs_volume_get_name(volume) );
 
177
            bi->uri     = thunar_vfs_path_dup_uri( thunar_vfs_volume_get_mount_point(volume) );
 
178
            bi->icon    = g_strdup( thunar_vfs_volume_lookup_icon_name(volume, icon_theme) );
 
179
            bi->data    = (gpointer) volume;
 
180
 
 
181
            g_ptr_array_add(b->bookmarks, bi);
 
182
        }
 
183
 
 
184
        volumes = volumes->next;
 
185
    }
 
186
}
 
187
 
 
188
// external
 
189
 
 
190
BookmarksVolumes*
 
191
places_bookmarks_volumes_init()
 
192
{
 
193
    DBG("init");
 
194
    BookmarksVolumes *b = g_new0(BookmarksVolumes, 1);
 
195
 
 
196
    thunar_vfs_init();
 
197
    
 
198
    b->bookmarks = g_ptr_array_new();
 
199
    b->changed = FALSE;
 
200
    b->volume_manager = thunar_vfs_volume_manager_get_default();
 
201
    
 
202
    places_bookmarks_volumes_add(b, thunar_vfs_volume_manager_get_volumes(b->volume_manager));
 
203
 
 
204
    g_signal_connect (b->volume_manager, "volumes-added",
 
205
                      G_CALLBACK (places_bookmarks_volumes_cb_added), b);
 
206
 
 
207
    g_signal_connect (b->volume_manager, "volumes-removed",
 
208
                      G_CALLBACK (places_bookmarks_volumes_cb_removed), b);
 
209
 
 
210
    DBG("done");
 
211
 
 
212
    return b;
 
213
}
 
214
 
 
215
void
 
216
places_bookmarks_volumes_finalize(BookmarksVolumes *b)
 
217
{
 
218
    BookmarkInfo *bi;
 
219
    guint k;
 
220
 
 
221
    for(k = 0; k < b->bookmarks->len; k++){
 
222
        bi = g_ptr_array_remove_index(b->bookmarks, k);
 
223
        if(bi->data != NULL){
 
224
            g_object_unref(bi->data);
 
225
            bi->data = NULL;
 
226
        }
 
227
        g_free(bi);
 
228
    }
 
229
 
 
230
    g_object_unref(b->volume_manager);
 
231
    b->volume_manager = NULL;
 
232
    thunar_vfs_shutdown();
 
233
 
 
234
    g_ptr_array_free(b->bookmarks, TRUE);
 
235
    b->bookmarks = NULL;
 
236
 
 
237
    g_free(b);
 
238
}
 
239
 
 
240
gboolean
 
241
places_bookmarks_volumes_changed(BookmarksVolumes *b)
 
242
{
 
243
    if(b->changed){
 
244
        b->changed = FALSE;
 
245
        return TRUE;
 
246
    }else{
 
247
        return FALSE;
 
248
    }
 
249
}
 
250
 
 
251
void
 
252
places_bookmarks_volumes_visit(BookmarksVolumes *b, BookmarksVisitor *visitor)
 
253
{
 
254
    guint k;
 
255
    BookmarkInfo *bi;
 
256
    
 
257
    for(k=0; k < b->bookmarks->len; k++){
 
258
        bi = g_ptr_array_index(b->bookmarks, k);
 
259
        visitor->item(visitor->pass_thru, bi->label, bi->uri, bi->icon);
 
260
    }
 
261
}
 
262
 
 
263
 
 
264
// vim: ai et tabstop=4