~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_system.c

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Le Folgoc
  • Date: 2007-08-15 12:33:24 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20070815123324-8snt7sp6zzj7vx1c
Tags: 1.0~beta1-0ubuntu1
* New upstream release.
* Upstream changes:
  - Adds mount/unmount options for volumes (removable media)
  - Adds Open Terminal Here option where applicable
  - Search for Files with an external application
  - The trash icon now shows whether there's anything in the trash
  - Large internal clean-up
  - Other minor bug fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
#include "model_system.h"
25
25
#include "model.h"
 
26
 
 
27
#include <string.h>
 
28
 
 
29
#include <glib.h>
 
30
 
26
31
#include <libxfce4util/libxfce4util.h>
27
 
#include <string.h>
28
 
 
29
 
#define bookmarks_system_check_existence data
30
 
 
31
 
struct _BookmarksSystem
32
 
{
33
 
    GPtrArray *bookmarks;
34
 
};
35
 
 
36
 
 
37
 
BookmarksSystem*
38
 
places_bookmarks_system_init()
39
 
{
40
 
    BookmarksSystem *b = g_new0(BookmarksSystem, 1);
41
 
 
42
 
    BookmarkInfo *bookmark;
43
 
    b->bookmarks = g_ptr_array_sized_new(4);
44
 
    
 
32
 
 
33
#define EXO_API_SUBJECT_TO_CHANGE
 
34
#include <thunar-vfs/thunar-vfs.h>
 
35
 
 
36
 
 
37
#define pbg_priv(pbg) ((PBSysData*) pbg->priv)
 
38
 
 
39
typedef struct
 
40
{
 
41
    ThunarVfsPath *trash_path;
 
42
 
 
43
    /* These are the things that might "change" */
 
44
    gboolean       check_changed;   /* starts off false to indicate the following are meaningless */
 
45
    gboolean       desktop_exists;
 
46
    gboolean       trash_is_empty;
 
47
 
 
48
} PBSysData;
 
49
 
 
50
static void
 
51
pbsys_free_desktop_bookmark(PlacesBookmark *bookmark)
 
52
{
 
53
    g_assert(bookmark != NULL);
 
54
 
 
55
    if(bookmark->uri != NULL)
 
56
        g_free(bookmark->uri);
 
57
 
 
58
    g_free(bookmark);
 
59
}
 
60
 
 
61
static void
 
62
pbsys_free_trash_bookmark(PlacesBookmark *bookmark)
 
63
{
 
64
    g_assert(bookmark != NULL);
 
65
 
 
66
    if(bookmark->icon != NULL)
 
67
        g_free(bookmark->icon);
 
68
 
 
69
    g_free(bookmark);
 
70
}
 
71
 
 
72
static GList*
 
73
pbsys_get_bookmarks(PlacesBookmarkGroup *bookmark_group)
 
74
{
 
75
    GList *bookmarks = NULL;           /* we'll return this */
 
76
    PlacesBookmark *bookmark;
 
77
    ThunarVfsInfo *trash_info;
45
78
    const gchar *home_dir = xfce_get_homedir();
46
79
 
 
80
    pbg_priv(bookmark_group)->check_changed = TRUE;
 
81
 
47
82
    // These icon names are consistent with Thunar.
48
83
 
49
84
    // Home
50
 
    bookmark = g_new0(BookmarkInfo, 1);
51
 
    bookmark->label = g_strdup(g_get_user_name());
52
 
    bookmark->uri = g_strdup(home_dir);
53
 
    bookmark->icon = g_strdup("gnome-fs-home");
54
 
    bookmark->show = TRUE;
55
 
    bookmark->bookmarks_system_check_existence = NULL;
56
 
    g_ptr_array_add(b->bookmarks, bookmark);
 
85
    bookmark                = g_new0(PlacesBookmark, 1);
 
86
    bookmark->label         = (gchar*) g_get_user_name();
 
87
    bookmark->uri           = (gchar*) home_dir;
 
88
    bookmark->icon          = "gnome-fs-home";
 
89
    bookmarks = g_list_append(bookmarks, bookmark);
57
90
 
58
91
    // Trash
59
 
    bookmark = g_new0(BookmarkInfo, 1);
60
 
    bookmark->label = g_strdup(_("Trash"));
61
 
    bookmark->uri = g_strdup("trash:///");
62
 
    bookmark->icon = g_strdup("gnome-fs-trash-full");
63
 
    bookmark->show = TRUE;
64
 
    bookmark->bookmarks_system_check_existence = NULL;
65
 
    g_ptr_array_add(b->bookmarks, bookmark);
 
92
    bookmark                = g_new0(PlacesBookmark, 1);
 
93
    bookmark->label         = _("Trash");
 
94
    bookmark->uri           = "trash:///";
 
95
    bookmark->uri_scheme    = PLACES_URI_SCHEME_TRASH;
 
96
    bookmark->free          = pbsys_free_trash_bookmark;;
 
97
 
 
98
    /* Try for an icon from ThunarVFS to indicate whether trash is empty or not */
 
99
    
 
100
    trash_info = thunar_vfs_info_new_for_path(pbg_priv(bookmark_group)->trash_path, NULL);
 
101
    if(trash_info->custom_icon != NULL){
 
102
        bookmark->icon = g_strdup(trash_info->custom_icon);
 
103
        pbg_priv(bookmark_group)->trash_is_empty = (strcmp("gnome-fs-trash-full", bookmark->icon) != 0);
 
104
    }else{
 
105
        bookmark->icon = g_strdup("gnome-fs-trash-full");
 
106
        pbg_priv(bookmark_group)->trash_is_empty = FALSE;
 
107
    }
 
108
    thunar_vfs_info_unref(trash_info);
 
109
 
 
110
    bookmarks = g_list_append(bookmarks, bookmark);
66
111
 
67
112
    // Desktop
68
 
    bookmark = g_new0(BookmarkInfo, 1);
69
 
    bookmark->uri = g_build_filename(home_dir, "Desktop", NULL);
70
 
    bookmark->label = g_strdup(_("Desktop"));
71
 
    bookmark->icon = g_strdup("gnome-fs-desktop");
72
 
    bookmark->show = g_file_test(bookmark->uri, G_FILE_TEST_IS_DIR);
73
 
    bookmark->bookmarks_system_check_existence = (gpointer) 1;
74
 
    g_ptr_array_add(b->bookmarks, bookmark);
 
113
    bookmark                = g_new0(PlacesBookmark, 1);
 
114
    bookmark->label         = _("Desktop");
 
115
    bookmark->uri           = g_build_filename(home_dir, "Desktop", NULL);
 
116
    bookmark->icon          = "gnome-fs-desktop";
 
117
    bookmark->free          = pbsys_free_desktop_bookmark;
 
118
 
 
119
    if(g_file_test(bookmark->uri, G_FILE_TEST_IS_DIR)){
 
120
        pbg_priv(bookmark_group)->desktop_exists = TRUE;
 
121
        bookmarks = g_list_append(bookmarks, bookmark);
 
122
    }else{
 
123
        pbg_priv(bookmark_group)->desktop_exists = FALSE;
 
124
        places_bookmark_free(bookmark);
 
125
    }
75
126
    
76
127
    // File System (/)
77
 
    bookmark = g_new0(BookmarkInfo, 1);
78
 
    bookmark->label = g_strdup(_("File System"));
79
 
    bookmark->uri = g_strdup("/");
80
 
    bookmark->icon = g_strdup("gnome-dev-harddisk");
81
 
    bookmark->show = TRUE;
82
 
    bookmark->bookmarks_system_check_existence = NULL;
83
 
    g_ptr_array_add(b->bookmarks, bookmark);
 
128
    bookmark                = g_new0(PlacesBookmark, 1);
 
129
    bookmark->label         = _("File System");
 
130
    bookmark->uri           = "/";
 
131
    bookmark->icon          = "gnome-dev-harddisk";
 
132
    bookmarks = g_list_append(bookmarks, bookmark);
84
133
 
85
 
    return b;
86
 
}
 
134
    return bookmarks;
 
135
};
87
136
 
88
137
gboolean
89
 
places_bookmarks_system_changed(BookmarksSystem *b)
90
 
{
91
 
    guint k;
92
 
    BookmarkInfo *bi;
93
 
    gboolean ret = FALSE;
94
 
    
95
 
    for(k=0; k < b->bookmarks->len; k++){
96
 
        bi = g_ptr_array_index(b->bookmarks, k);
97
 
        if(bi->bookmarks_system_check_existence && bi->show != g_file_test(bi->uri, G_FILE_TEST_IS_DIR)){
98
 
            bi->show = !bi->show;
99
 
            ret = TRUE;
100
 
        }
101
 
    }
102
 
    
103
 
    return ret;
104
 
}
105
 
 
106
 
void
107
 
places_bookmarks_system_visit(BookmarksSystem *b,
108
 
                              BookmarksVisitor *visitor)
109
 
{
110
 
    guint k;
111
 
    BookmarkInfo *bi;
112
 
    
113
 
    for(k=0; k < b->bookmarks->len; k++){
114
 
        bi = g_ptr_array_index(b->bookmarks, k);
115
 
        if(bi->show)
116
 
            visitor->item(visitor->pass_thru, bi->label, bi->uri, bi->icon);
117
 
    }
118
 
}
119
 
 
120
 
void
121
 
places_bookmarks_system_finalize(BookmarksSystem *b)
122
 
{
123
 
    g_ptr_array_free(b->bookmarks, TRUE);
124
 
    g_free(b);
125
 
}
126
 
 
127
 
/*
128
 
 * A bookmark with the same path as a system path should have the system icon.
129
 
 * Such a bookmark with its default label should also use the system label.
130
 
 */
131
 
void
132
 
places_bookmarks_system_bi_system_mod(BookmarksSystem *b, BookmarkInfo *other){
133
 
    g_assert(b != NULL);
134
 
    g_assert(other != NULL);
135
 
    g_assert(other->icon != NULL);
136
 
    g_assert(other->label != NULL);
137
 
 
138
 
    gboolean label_is_default;
139
 
    gchar   *default_label;
140
 
    
141
 
    default_label    = g_filename_display_basename(other->uri);
142
 
    label_is_default = (strcmp(default_label, other->label) == 0);
143
 
    g_free(default_label);
144
 
 
145
 
    BookmarkInfo *bi;
146
 
    guint k;
147
 
    for(k=0; k < b->bookmarks->len; k++){
148
 
        bi = g_ptr_array_index(b->bookmarks, k);
149
 
 
150
 
        if(G_UNLIKELY(strcmp(other->uri, bi->uri) == 0)){
151
 
            g_free(other->icon);
152
 
            other->icon = g_strdup(bi->icon);
153
 
 
154
 
            if(label_is_default && strcmp(other->label, bi->label) != 0){
155
 
                g_free(other->label);
156
 
                other->label = g_strdup(bi->label);
157
 
            }
158
 
            
159
 
            return;
160
 
        }
161
 
    }
162
 
}
 
138
pbsys_changed(PlacesBookmarkGroup *bookmark_group)
 
139
{
 
140
    gchar *uri;
 
141
    gboolean trash_is_empty = FALSE;
 
142
    ThunarVfsInfo *trash_info;
 
143
    
 
144
    if(!pbg_priv(bookmark_group)->check_changed)
 
145
        return FALSE;
 
146
    
 
147
    /* Check if desktop now exists and didn't before */
 
148
    uri = g_build_filename(xfce_get_homedir(), "Desktop", NULL);
 
149
    if(g_file_test(uri, G_FILE_TEST_IS_DIR) != pbg_priv(bookmark_group)->desktop_exists){
 
150
        g_free(uri);
 
151
        return TRUE;
 
152
    }else
 
153
        g_free(uri);
 
154
 
 
155
    // see if trash gets a different icon (e.g., was empty, now full)
 
156
    trash_info = thunar_vfs_info_new_for_path(pbg_priv(bookmark_group)->trash_path, NULL);
 
157
    if(trash_info->custom_icon != NULL)
 
158
        trash_is_empty = (strcmp("gnome-fs-trash-full", trash_info->custom_icon) != 0);
 
159
    thunar_vfs_info_unref(trash_info);
 
160
    
 
161
    if(trash_is_empty != pbg_priv(bookmark_group)->trash_is_empty)
 
162
        return TRUE;
 
163
 
 
164
    return FALSE;
 
165
}
 
166
 
 
167
static void
 
168
pbsys_finalize(PlacesBookmarkGroup *bookmark_group)
 
169
{
 
170
    thunar_vfs_path_unref(pbg_priv(bookmark_group)->trash_path);
 
171
    thunar_vfs_shutdown();
 
172
    
 
173
    g_free(pbg_priv(bookmark_group));
 
174
 
 
175
    g_free(bookmark_group);
 
176
}
 
177
 
 
178
PlacesBookmarkGroup*
 
179
places_bookmarks_system_create()
 
180
{
 
181
    PlacesBookmarkGroup *bookmark_group = g_new0(PlacesBookmarkGroup, 1);
 
182
    bookmark_group->get_bookmarks = pbsys_get_bookmarks;
 
183
    bookmark_group->changed       = pbsys_changed;
 
184
    bookmark_group->finalize      = pbsys_finalize;
 
185
    bookmark_group->priv          = g_new0(PBSysData, 1);
 
186
    
 
187
    thunar_vfs_init();
 
188
    pbg_priv(bookmark_group)->trash_path = thunar_vfs_path_get_for_trash();
 
189
 
 
190
    return bookmark_group;
 
191
}
 
192
   
163
193
 
164
194
// vim: ai et tabstop=4