File: model_system.c

package info (click to toggle)
xfce4-places-plugin 1.8.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,388 kB
  • sloc: sh: 4,383; ansic: 3,096; makefile: 102
file content (284 lines) | stat: -rw-r--r-- 8,414 bytes parent folder | download
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
/*  xfce4-places-plugin
 *
 *  Model: system bookmarks (e.g., home folder, desktop)
 *
 *  Copyright (c) 2007-2009 Diego Ongaro <ongardie@gmail.com>
 *
 *  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 of the License, 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 Library 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.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include "model_system.h"
#include "model.h"
#include "support.h"

#include <string.h>

#include <glib.h>
#include <gio/gio.h>

#include <libxfce4util/libxfce4util.h>

#define TRASH          1

#define pbg_priv(pbg) ((PBSysData*) pbg->priv)

typedef struct
{

    /* These are the things that might "change" */
    gboolean       check_changed;   /* starts off false to indicate the following are meaningless */
    gchar         *desktop_dir;     /* NULL => no desktop or desktop is same as home */
#if TRASH
    gboolean       trash_is_empty;
    GFile *trash_path;
#endif

} PBSysData;
 
static void
pbsys_finalize_desktop_bookmark(PlacesBookmark *bookmark)
{
    g_assert(bookmark != NULL);

    if(bookmark->uri != NULL){
        g_free(bookmark->uri);
        bookmark->uri = NULL;
    }
}

#if TRASH
static void
pbsys_finalize_trash_bookmark(PlacesBookmark *bookmark)
{
    g_assert(bookmark != NULL);

    if(bookmark->icon != NULL){
        g_object_unref(bookmark->icon);
        bookmark->icon = NULL;
    }
}
#endif

#if TRASH
static gboolean
pbsys_trash_is_empty(GFileInfo *trash_info)
{
    guint item_count = g_file_info_get_attribute_uint32(trash_info,
                                                        G_FILE_ATTRIBUTE_TRASH_ITEM_COUNT);
    if(item_count == 0)
        return TRUE;
    else
        return FALSE;
}
#endif


static gchar*
pbsys_desktop_dir(void)
{
    const gchar *home_dir = xfce_get_homedir();
    gchar *desktop_dir = NULL;

    /* get the xdg desktop directory, or possibly NULL */
    desktop_dir = g_strdup(g_get_user_special_dir(G_USER_DIRECTORY_DESKTOP));

    /* if xdg desktop is the same as home, pretend it's not there */
    if(g_strcmp0(desktop_dir, home_dir) == 0){
        g_free(desktop_dir);
        return NULL;
    }

    /* fall back to ~/Desktop */
    if(desktop_dir == NULL)
        desktop_dir = g_build_filename(home_dir, "Desktop", NULL);

    /* make sure the directory is there */
    if(!g_file_test(desktop_dir, G_FILE_TEST_IS_DIR)){
        g_free(desktop_dir);
        return NULL;
    }

    return desktop_dir;
}

static GList*
pbsys_get_bookmarks(PlacesBookmarkGroup *bookmark_group)
{
    GList *bookmarks = NULL;           /* we'll return this */
    PlacesBookmark *bookmark;
    PlacesBookmarkAction *open, *terminal;
#if TRASH
    GFileInfo *trash_info;
#endif
    const gchar *home_dir = xfce_get_homedir();
    gchar *desktop_dir;

    pbg_priv(bookmark_group)->check_changed = TRUE;

    /* These icon names are consistent with Thunar. */

    /* Home */
    bookmark                = places_bookmark_create((gchar*) g_get_user_name());
    bookmark->uri           = (gchar*) home_dir;
    bookmark->icon          = g_themed_icon_new("user-home");

    terminal                 = places_create_open_terminal_action(bookmark);
    bookmark->actions        = g_list_prepend(bookmark->actions, terminal);
    open                     = places_create_open_action(bookmark);
    bookmark->actions        = g_list_prepend(bookmark->actions, open);
    bookmark->primary_action = open;

    bookmarks = g_list_append(bookmarks, bookmark);

#if TRASH
    /* Trash */
    bookmark                = places_bookmark_create(_("Trash"));
    bookmark->uri           = "trash:///";
    bookmark->uri_scheme    = PLACES_URI_SCHEME_TRASH;
    bookmark->finalize      = pbsys_finalize_trash_bookmark;;

    /* Try for an icon to indicate whether trash is empty or not */
    
    trash_info = g_file_query_info(pbg_priv(bookmark_group)->trash_path,
                    "trash::*",
                    G_FILE_QUERY_INFO_NONE,
                    NULL, NULL);
    pbg_priv(bookmark_group)->trash_is_empty = pbsys_trash_is_empty(trash_info);
    if (bookmark->icon != NULL)
        g_object_unref(bookmark->icon);
    if (pbg_priv(bookmark_group)->trash_is_empty)
        bookmark->icon = g_themed_icon_new("user-trash");
    else
        bookmark->icon = g_themed_icon_new("user-trash-full");
    g_object_unref(trash_info);

    open                     = places_create_open_action(bookmark);
    bookmark->actions        = g_list_prepend(bookmark->actions, open);
    bookmark->primary_action = open;

    bookmarks = g_list_append(bookmarks, bookmark);
#endif

    /* Desktop */
    desktop_dir = pbsys_desktop_dir();

    g_free(pbg_priv(bookmark_group)->desktop_dir);
    pbg_priv(bookmark_group)->desktop_dir = g_strdup(desktop_dir);

    if(desktop_dir != NULL){
        bookmark                = places_bookmark_create(_("Desktop"));
        bookmark->uri           = desktop_dir;
        bookmark->icon          = g_themed_icon_new("user-desktop");
        bookmark->finalize      = pbsys_finalize_desktop_bookmark;


        terminal                 = places_create_open_terminal_action(bookmark);
        bookmark->actions        = g_list_prepend(bookmark->actions, terminal);
        open                     = places_create_open_action(bookmark);
        bookmark->actions        = g_list_prepend(bookmark->actions, open);
        bookmark->primary_action = open;

        bookmarks = g_list_append(bookmarks, bookmark);
    }

    /* File System (/) */
    bookmark                = places_bookmark_create(_("File System"));
    bookmark->uri           = "/";
    bookmark->icon          = g_themed_icon_new("drive-harddisk");

    terminal                 = places_create_open_terminal_action(bookmark);
    bookmark->actions        = g_list_prepend(bookmark->actions, terminal);
    open                     = places_create_open_action(bookmark);
    bookmark->actions        = g_list_prepend(bookmark->actions, open);
    bookmark->primary_action = open;

    bookmarks = g_list_append(bookmarks, bookmark);

    return bookmarks;
};

static gboolean
pbsys_changed(PlacesBookmarkGroup *bookmark_group)
{
    gchar *desktop_dir;
#if TRASH
    gboolean trash_is_empty;
    GFileInfo *trash_info;
#endif

    if(!pbg_priv(bookmark_group)->check_changed)
        return FALSE;
    
    /* Check if desktop now exists and didn't before */
    desktop_dir = pbsys_desktop_dir();
    if(g_strcmp0(desktop_dir, pbg_priv(bookmark_group)->desktop_dir) != 0){
        g_free(desktop_dir);
        return TRUE;
    }else
        g_free(desktop_dir);

#if TRASH
    /* see if trash gets a different icon (e.g., was empty, now full) */
    trash_info = g_file_query_info(pbg_priv(bookmark_group)->trash_path,
                    "trash::*",
                    G_FILE_QUERY_INFO_NONE,
                    NULL, NULL);
    trash_is_empty = pbsys_trash_is_empty(trash_info);
    g_object_unref(trash_info);
    
    if(trash_is_empty != pbg_priv(bookmark_group)->trash_is_empty)
        return TRUE;
#endif

    return FALSE;
}

static void
pbsys_finalize(PlacesBookmarkGroup *bookmark_group)
{
#if TRASH
    g_object_unref(pbg_priv(bookmark_group)->trash_path);
#endif

    g_free(pbg_priv(bookmark_group)->desktop_dir);

    g_free(pbg_priv(bookmark_group));

}

PlacesBookmarkGroup*
places_bookmarks_system_create(void)
{
    PlacesBookmarkGroup *bookmark_group;
    
    bookmark_group = places_bookmark_group_create();
    bookmark_group->get_bookmarks = pbsys_get_bookmarks;
    bookmark_group->changed       = pbsys_changed;
    bookmark_group->finalize      = pbsys_finalize;
    bookmark_group->priv          = g_new0(PBSysData, 1);
    
#if TRASH
    pbg_priv(bookmark_group)->trash_path = g_file_new_for_uri("trash:///");
#endif

    return bookmark_group;
}
   

/* vim: set ai et tabstop=4: */