[Goodies-commits] r2663 - in xfce4-places-plugin/branches/volumes: . panel-plugin
Diego Ongaro
ongardie at xfce.org
Thu Apr 5 08:46:30 CEST 2007
Author: ongardie
Date: 2007-04-05 06:46:29 +0000 (Thu, 05 Apr 2007)
New Revision: 2663
Added:
xfce4-places-plugin/branches/volumes/HACKING
xfce4-places-plugin/branches/volumes/panel-plugin/model.c
xfce4-places-plugin/branches/volumes/panel-plugin/model.h
xfce4-places-plugin/branches/volumes/panel-plugin/model_system.c
xfce4-places-plugin/branches/volumes/panel-plugin/model_user.c
xfce4-places-plugin/branches/volumes/panel-plugin/model_volumes.c
Modified:
xfce4-places-plugin/branches/volumes/ChangeLog
xfce4-places-plugin/branches/volumes/panel-plugin/Makefile.am
xfce4-places-plugin/branches/volumes/panel-plugin/places.c
xfce4-places-plugin/branches/volumes/panel-plugin/places.h
xfce4-places-plugin/branches/volumes/panel-plugin/unescape_uri.c
Log:
2007-04-05 Diego Ongaro <ongardie at gmail.com>
* Uncoupled model from view/controller as explained in HACKING.
Modified: xfce4-places-plugin/branches/volumes/ChangeLog
===================================================================
--- xfce4-places-plugin/branches/volumes/ChangeLog 2007-04-04 22:24:07 UTC (rev 2662)
+++ xfce4-places-plugin/branches/volumes/ChangeLog 2007-04-05 06:46:29 UTC (rev 2663)
@@ -1,23 +1,30 @@
-2007-04-03 Diego Ongaro ongardie at gmail.com
+2007-04-05 Diego Ongaro <ongardie at gmail.com>
+ * Uncoupled model from view/controller as explained in HACKING.
+
+2007-04-03 Diego Ongaro <ongardie at gmail.com>
+
* Improved managing volumes when a signal comes in
-2007-04-02 Diego Ongaro ongardie at gmail.com
+2007-04-02 Diego Ongaro <ongardie at gmail.com>
* Added volumes as items in menu
* Added dumb signal-listening for volume changes
- (reloads volumes, rebuilds menu when anything changes)
+ (reloads volumes, rebuilds menu when anything changes)
-2007-04-01 Diego Ongaro ongardie at gmail.com
+2007-04-01 Diego Ongaro <ongardie at gmail.com>
* Branched to add volume support.
* Added Thunar VFS dependency.
-2007-03-25 Diego Ongaro ongardie at gmail.com
+2007-03-25 Diego Ongaro <ongardie at gmail.com>
* Fixed bug 3053 (Plugin doesn't launch thunar) by dropping "/usr/bin/"
* Fixed bug 3027 (Places menu showing %20) by copying glib code
-2007-03-14 Diego Ongaro ongardie at gmail.com
+2007-03-14 Diego Ongaro <ongardie at gmail.com>
* initial beta release, packaged outside svn
+
+
+# vi:set ts=8 sw=8 tw=72 noet ai nocindent:
Added: xfce4-places-plugin/branches/volumes/HACKING
===================================================================
--- xfce4-places-plugin/branches/volumes/HACKING (rev 0)
+++ xfce4-places-plugin/branches/volumes/HACKING 2007-04-05 06:46:29 UTC (rev 2663)
@@ -0,0 +1,48 @@
+The Model Infrastructure
+================================================================================
+
+model.h provides a convenient BookmarkInfo struct
+model.c provides the 4 "composite" functions:
+
+ 1) static Bookmarks*
+ places_bookmarks_init();
+
+ * This function is in charge of creating the Bookmarks struct.
+ * It delegates to the children (e.g. system, volumes, user), which are
+ each in charge of creating their own struct.
+
+ 2) static void
+ places_bookmarks_visit(Bookmarks *b, gpointer pass_thru,
+ item callback, separator callback);
+
+ * This function allows the view to (re-)build it's menu, while keeping it
+ uncoupled from the model.
+ * The model agrees to call the item callback with some pass-thru data and
+ a label, uri, and icon for each item. It fulfills this through
+ delegation to its children. Each child should then iterate over its
+ bookmarks and call the item callback. Additionally, the model calls the
+ separator callback where it finds appropriate.
+
+ 3) static gboolean
+ places_bookmarks_changed(Bookmarks *b);
+
+ * The return value of this function indicates whether the model has been
+ altered since the last call to this function.
+ * The expected side effect of this function, then, is that updates may
+ occur as a result of this trigger, and some state memory should be reset.
+ * The model delegates to its children and OR's the results. It ensures
+ that each child's changed function gets called.
+
+ 4) static void
+ places_bookmarks_finalize(Bookmarks *b);
+
+ * This function is in charge of cleaning up any resources that have been
+ created, either in the init function or later.
+ * It delegates to the children so they can clean up their own resources.
+
+Changes To This File
+================================================================================
+
+2007-04-05 Diego Ongaro <ongardie at gmail.com>
+
+ * Created file
Modified: xfce4-places-plugin/branches/volumes/panel-plugin/Makefile.am
===================================================================
--- xfce4-places-plugin/branches/volumes/panel-plugin/Makefile.am 2007-04-04 22:24:07 UTC (rev 2662)
+++ xfce4-places-plugin/branches/volumes/panel-plugin/Makefile.am 2007-04-05 06:46:29 UTC (rev 2663)
@@ -11,10 +11,16 @@
xfce4-places-plugin
xfce4_places_plugin_SOURCES = \
- unescape_uri.c \
+ places.c \
places.h \
- places.c
+ model.c \
+ model.h \
+ model_system.c \
+ model_volumes.c \
+ model_user.c \
+ unescape_uri.c
+
xfce4_places_plugin_CFLAGS = \
$(GTK_CFLAGS) \
$(LIBXFCE4UTIL_CFLAGS) \
Added: xfce4-places-plugin/branches/volumes/panel-plugin/model.c
===================================================================
--- xfce4-places-plugin/branches/volumes/panel-plugin/model.c (rev 0)
+++ xfce4-places-plugin/branches/volumes/panel-plugin/model.c 2007-04-05 06:46:29 UTC (rev 2663)
@@ -0,0 +1,77 @@
+/* xfce4-places-plugin
+ *
+ * Copyright (c) 2007 Diego Ongaro <ongardie at 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.
+ */
+
+#include "model_system.c"
+#include "model_volumes.c"
+#include "model_user.c"
+
+typedef struct
+{
+ BookmarksSystem *system;
+ BookmarksVolumes *volumes;
+ BookmarksUser *user;
+} Bookmarks;
+
+static Bookmarks*
+places_bookmarks_init()
+{
+ DBG("initializing model");
+
+ Bookmarks *b = g_new0(Bookmarks, 1);
+
+ b->system = places_bookmarks_system_init();
+ b->volumes = places_bookmarks_volumes_init();
+ b->user = places_bookmarks_user_init();
+
+ return b;
+}
+
+static void
+places_bookmarks_visit(Bookmarks *b,
+ gpointer pass_thru,
+ BOOKMARK_ITEM_FUNC(item_func),
+ BOOKMARK_SEPARATOR_FUNC(separator_func))
+{
+ places_bookmarks_system_visit(b->system, pass_thru, item_func, separator_func);
+ places_bookmarks_volumes_visit(b->volumes, pass_thru, item_func, separator_func);
+ separator_func(pass_thru);
+ places_bookmarks_user_visit(b->user, pass_thru, item_func, separator_func);
+}
+
+static gboolean
+places_bookmarks_changed(Bookmarks *b)
+{
+ // avoid short-circuit of || since changed() has side-effects
+ gboolean changed = FALSE;
+ changed = places_bookmarks_system_changed(b->system) || changed;
+ changed = places_bookmarks_volumes_changed(b->volumes) || changed;
+ changed = places_bookmarks_user_changed(b->user) || changed;
+ return changed || TRUE;
+}
+
+static void
+places_bookmarks_finalize(Bookmarks *b)
+{
+ places_bookmarks_system_finalize(b->system);
+ places_bookmarks_volumes_finalize(b->volumes);
+ places_bookmarks_user_finalize(b->user);
+ g_free(b);
+}
+
+// vim: ai et tabstop=4
Added: xfce4-places-plugin/branches/volumes/panel-plugin/model.h
===================================================================
--- xfce4-places-plugin/branches/volumes/panel-plugin/model.h (rev 0)
+++ xfce4-places-plugin/branches/volumes/panel-plugin/model.h 2007-04-05 06:46:29 UTC (rev 2663)
@@ -0,0 +1,37 @@
+/* xfce4-places-plugin
+ *
+ * Copyright (c) 2007 Diego Ongaro <ongardie at 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.
+ */
+
+#ifndef _XFCE_PANEL_PLACES_MODEL_H
+#define _XFCE_PANEL_PLACES_MODEL_H
+
+#include <glib.h>
+
+#define BOOKMARK_ITEM_FUNC(symbol) void (*symbol) (gpointer, const gchar*, const gchar*, const gchar*)
+#define BOOKMARK_SEPARATOR_FUNC(symbol) void (*symbol) (gpointer)
+
+typedef struct
+{
+ gchar *label;
+ gchar *uri;
+ gchar *icon;
+ gpointer *data;
+} BookmarkInfo;
+
+#endif
+// vim: ai et tabstop=4
Added: xfce4-places-plugin/branches/volumes/panel-plugin/model_system.c
===================================================================
--- xfce4-places-plugin/branches/volumes/panel-plugin/model_system.c (rev 0)
+++ xfce4-places-plugin/branches/volumes/panel-plugin/model_system.c 2007-04-05 06:46:29 UTC (rev 2663)
@@ -0,0 +1,99 @@
+/* xfce4-places-plugin
+ *
+ * Copyright (c) 2007 Diego Ongaro <ongardie at 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.
+ */
+
+#include "model.h"
+#include <libxfce4util/libxfce4util.h>
+
+typedef struct
+{
+ GPtrArray *bookmarks;
+} BookmarksSystem;
+
+static BookmarksSystem*
+places_bookmarks_system_init()
+{
+ BookmarksSystem *b = g_new0(BookmarksSystem, 1);
+
+ BookmarkInfo *bookmark;
+ b->bookmarks = g_ptr_array_sized_new(4);
+
+ const gchar *home_dir = xfce_get_homedir();
+
+ // These icon names are consistent with Thunar.
+
+ // Home
+ bookmark = g_new0(BookmarkInfo, 1);
+ bookmark->label = g_strdup(g_get_user_name());
+ bookmark->uri = g_strdup(home_dir);
+ bookmark->icon = "gnome-fs-home";
+ g_ptr_array_add(b->bookmarks, bookmark);
+
+ // Trash
+ bookmark = g_new0(BookmarkInfo, 1);
+ bookmark->label = "Trash";
+ bookmark->uri = "trash:///";
+ bookmark->icon = "gnome-fs-trash-full";
+ g_ptr_array_add(b->bookmarks, bookmark);
+
+ // Desktop
+ bookmark = g_new0(BookmarkInfo, 1);
+ bookmark->label = "Desktop";
+ bookmark->uri = g_build_filename(home_dir, "Desktop", NULL);
+ bookmark->icon = "gnome-fs-desktop";
+ g_ptr_array_add(b->bookmarks, bookmark);
+
+ // File System (/)
+ bookmark = g_new0(BookmarkInfo, 1);
+ bookmark->label = "File System";
+ bookmark->uri = "/";
+ bookmark->icon = "gnome-dev-harddisk";
+ g_ptr_array_add(b->bookmarks, bookmark);
+
+ return b;
+}
+
+static gboolean
+places_bookmarks_system_changed(BookmarksSystem *b)
+{
+ return FALSE;
+}
+
+static void
+places_bookmarks_system_visit(BookmarksSystem *b,
+ gpointer pass_thru,
+ BOOKMARK_ITEM_FUNC(item_func),
+ BOOKMARK_SEPARATOR_FUNC(separator_func))
+{
+ guint k;
+ BookmarkInfo *bi;
+
+ for(k=0; k < b->bookmarks->len; k++){
+ bi = g_ptr_array_index(b->bookmarks, k);
+ item_func(pass_thru, bi->label, bi->uri, bi->icon);
+ }
+}
+
+static void
+places_bookmarks_system_finalize(BookmarksSystem *b)
+{
+ g_ptr_array_free(b->bookmarks, TRUE);
+ g_free(b);
+}
+
+// vim: ai et tabstop=4
Added: xfce4-places-plugin/branches/volumes/panel-plugin/model_user.c
===================================================================
--- xfce4-places-plugin/branches/volumes/panel-plugin/model_user.c (rev 0)
+++ xfce4-places-plugin/branches/volumes/panel-plugin/model_user.c 2007-04-05 06:46:29 UTC (rev 2663)
@@ -0,0 +1,141 @@
+/* xfce4-places-plugin
+ *
+ * Copyright (c) 2007 Diego Ongaro <ongardie at 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.
+ */
+
+#include "model.h"
+#include <glib/gstdio.h>
+#include "unescape_uri.c"
+
+typedef struct
+{
+ GPtrArray *bookmarks;
+ gchar *filename;
+ time_t loaded;
+} BookmarksUser;
+
+
+// internal
+
+static void
+places_bookmarks_user_reinit(BookmarksUser *b)
+{
+ DBG("initializing");
+ BookmarkInfo *bi;
+
+ gchar *contents;
+ gchar **split;
+ gchar **lines;
+ int i;
+
+ if (!g_file_get_contents(b->filename, &contents, NULL, NULL)) {
+ DBG("Error opening gtk bookmarks file");
+ }else{
+
+ lines = g_strsplit (contents, "\n", -1);
+ g_free(contents);
+
+ for (i = 0; lines[i]; i++) {
+ if(!lines[i][0])
+ continue;
+
+ bi = g_new0(BookmarkInfo, 1);
+ bi->icon = "gnome-fs-directory";
+
+ // See if the line is in the form "file:///path" or "file:///path friendly-name"
+ split = g_strsplit(lines[i], " ", 2);
+ if(split[1]){
+ bi->label = g_strdup(split[1]);
+ bi->uri = g_strdup(split[0]);
+ }else{
+ bi->label = places_unescape_uri_string(g_strrstr(lines[i], "/") + sizeof(gchar));
+ bi->uri = g_strdup(lines[i]);
+ }
+
+ g_ptr_array_add(b->bookmarks, bi);
+ g_free(split);
+ }
+
+ g_strfreev(lines);
+ }
+}
+
+static time_t
+places_bookmarks_user_get_mtime(BookmarksUser *b)
+{
+ struct stat buf;
+ if(g_stat(b->filename, &buf) == 0)
+ return buf.st_mtime;
+ return 0;
+}
+
+// external
+
+static BookmarksUser*
+places_bookmarks_user_init()
+{
+ BookmarksUser *b = g_new0(BookmarksUser, 1);
+
+ b->filename = g_build_filename(xfce_get_homedir(), ".gtk-bookmarks", NULL);
+ b->bookmarks = g_ptr_array_new();
+ b->loaded = places_bookmarks_user_get_mtime(b);
+
+ places_bookmarks_user_reinit(b);
+ return b;
+}
+
+static gboolean
+places_bookmarks_user_changed(BookmarksUser *b)
+{
+ time_t mtime = places_bookmarks_user_get_mtime(b);
+
+ if(mtime > b->loaded){
+ g_ptr_array_free(b->bookmarks, TRUE);
+ b->bookmarks = g_ptr_array_new();
+ b->loaded = mtime;
+ places_bookmarks_user_reinit(b);
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+static void
+places_bookmarks_user_visit(BookmarksUser *b,
+ gpointer pass_thru,
+ BOOKMARK_ITEM_FUNC(item_func),
+ BOOKMARK_SEPARATOR_FUNC(separator_func))
+{
+ guint k;
+ BookmarkInfo *bi;
+
+ for(k=0; k < b->bookmarks->len; k++){
+ bi = g_ptr_array_index(b->bookmarks, k);
+ item_func(pass_thru, bi->label, bi->uri, bi->icon);
+ }
+}
+
+static void
+places_bookmarks_user_finalize(BookmarksUser *b)
+{
+ g_ptr_array_free(b->bookmarks, TRUE);
+ g_free(b->filename);
+ g_free(b);
+}
+
+
+// vim: ai et tabstop=4
Added: xfce4-places-plugin/branches/volumes/panel-plugin/model_volumes.c
===================================================================
--- xfce4-places-plugin/branches/volumes/panel-plugin/model_volumes.c (rev 0)
+++ xfce4-places-plugin/branches/volumes/panel-plugin/model_volumes.c 2007-04-05 06:46:29 UTC (rev 2663)
@@ -0,0 +1,234 @@
+/* xfce4-places-plugin
+ *
+ * Copyright (c) 2007 Diego Ongaro <ongardie at 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.
+ */
+
+#include "model.h"
+#include <thunar-vfs/thunar-vfs.h>
+
+typedef struct
+{
+ GPtrArray *bookmarks;
+ gboolean changed;
+ ThunarVfsVolumeManager *volume_manager;
+} BookmarksVolumes;
+
+static gboolean places_show_volume(ThunarVfsVolume *volume);
+static void places_bookmarks_volumes_add(BookmarksVolumes *b, const GList *volumes);
+
+
+/********** ThunarVFS Callbacks **********/
+
+static void
+places_cb_volume_changed(ThunarVfsVolume *volume, BookmarksVolumes *b){
+ DBG("volume changed");
+ // unfortunately there tends to be like 3 of these in a row
+
+ guint k;
+
+ if(places_show_volume(volume)){
+
+ // make sure it's in the array
+ for(k = 0; k < b->bookmarks->len; k++){
+ BookmarkInfo *bi = g_ptr_array_index(b->bookmarks, k);
+ if(THUNAR_VFS_VOLUME(bi->data) == volume)
+ break;
+ }
+
+ if(k == b->bookmarks->len){ // it's not there
+ DBG("adding volume to array");
+
+ places_bookmarks_volumes_add(b, g_list_prepend(NULL, volume));
+ b->changed = TRUE;
+ }else{
+ DBG("volume already in array");
+ }
+
+ }else{
+ // make sure it's not in the array
+ for(k = 0; k < b->bookmarks->len; k++){
+ BookmarkInfo *bi = g_ptr_array_index(b->bookmarks, k);
+ if(THUNAR_VFS_VOLUME(bi->data) == volume){ // it is there
+ DBG("dropping volume from array");
+
+ bi = g_ptr_array_remove_index(b->bookmarks, k);
+ g_object_unref(bi->data);
+ g_free(bi);
+
+ b->changed = TRUE;
+ }
+ }
+ }
+}
+
+static void
+places_cb_volumes_added(ThunarVfsVolumeManager *volume_manager, const GList *volumes, BookmarksVolumes *b){
+ DBG("volumes added");
+ places_bookmarks_volumes_add(b, volumes);
+ b->changed = TRUE;
+}
+
+static void
+places_cb_volumes_removed(ThunarVfsVolumeManager *volume_manager, const GList *volumes, BookmarksVolumes *b){
+ DBG("volumes removed");
+
+ GList *vol_iter;
+ guint k;
+
+ // step through existing bookmarks
+ for(k = 0; k < b->bookmarks->len; k++){
+ BookmarkInfo *bi = g_ptr_array_index(b->bookmarks, k);
+
+ // step through removals
+ vol_iter = (GList*) volumes;
+ while(vol_iter){
+ if(bi->data == vol_iter->data){ // it is there
+
+ // delete the bookmark
+ bi = g_ptr_array_remove_index(b->bookmarks, k);
+ DBG("Removing bookmark %s", bi->label);
+
+ if(bi->data)
+ g_object_unref(bi->data);
+ g_free(bi);
+
+ b->changed = TRUE;
+ }
+
+ vol_iter = vol_iter->next;
+ }
+ }
+}
+
+// internal
+static gboolean
+places_show_volume(ThunarVfsVolume *volume){
+
+ DBG("Volume: %s [mounted=%x removable=%x present=%x]", thunar_vfs_volume_get_name(volume),
+ thunar_vfs_volume_is_mounted(volume),
+ thunar_vfs_volume_is_removable(volume),
+ thunar_vfs_volume_is_present(volume));
+
+ return thunar_vfs_volume_is_mounted(volume) &&
+ thunar_vfs_volume_is_removable(volume) &&
+ thunar_vfs_volume_is_present(volume);
+}
+
+
+static void
+places_bookmarks_volumes_add(BookmarksVolumes *b, const GList *volumes)
+{
+ ThunarVfsVolume *volume;
+ BookmarkInfo *bi;
+ GtkIconTheme *icon_theme = gtk_icon_theme_get_default();
+
+ while(volumes){
+ volume = THUNAR_VFS_VOLUME(volumes->data);
+
+ g_signal_connect (volume, "changed",
+ G_CALLBACK(places_cb_volume_changed), b);
+
+ if(places_show_volume(volume)){
+
+ g_object_ref(volume);
+
+ bi = g_new0(BookmarkInfo, 1);
+ bi->label = g_strdup( thunar_vfs_volume_get_name(volume) );
+ bi->uri = thunar_vfs_path_dup_uri( thunar_vfs_volume_get_mount_point(volume) );
+ bi->icon = g_strdup( thunar_vfs_volume_lookup_icon_name(volume, icon_theme) );
+ bi->data = (gpointer) volume;
+
+ g_ptr_array_add(b->bookmarks, bi);
+ }
+
+ volumes = volumes->next;
+ }
+}
+
+// external
+
+static BookmarksVolumes*
+places_bookmarks_volumes_init()
+{
+ DBG("init");
+ BookmarksVolumes *b = g_new0(BookmarksVolumes, 1);
+
+ thunar_vfs_init();
+
+ b->bookmarks = g_ptr_array_new();
+ b->changed = FALSE;
+ b->volume_manager = thunar_vfs_volume_manager_get_default();
+
+ places_bookmarks_volumes_add(b, thunar_vfs_volume_manager_get_volumes(b->volume_manager));
+
+ g_signal_connect (b->volume_manager, "volumes-added",
+ G_CALLBACK (places_cb_volumes_added), b);
+
+ g_signal_connect (b->volume_manager, "volumes-removed",
+ G_CALLBACK (places_cb_volumes_removed), b);
+
+ DBG("done");
+
+ return b;
+}
+
+static void
+places_bookmarks_volumes_finalize(BookmarksVolumes *b)
+{
+ guint k;
+
+ thunar_vfs_shutdown();
+ g_object_unref(b->volume_manager);
+
+ for(k = 0; k < b->bookmarks->len; k++){
+ BookmarkInfo *bi = g_ptr_array_remove_index(b->bookmarks, k);
+ if(bi->data)
+ g_object_unref(bi->data);
+ }
+
+ g_ptr_array_free(b->bookmarks, TRUE);
+ g_free(b);
+}
+
+static gboolean
+places_bookmarks_volumes_changed(BookmarksVolumes *b)
+{
+ if(b->changed){
+ b->changed = FALSE;
+ return TRUE;
+ }else{
+ return FALSE;
+ }
+}
+
+static void
+places_bookmarks_volumes_visit(BookmarksVolumes *b,
+ gpointer pass_thru,
+ BOOKMARK_ITEM_FUNC(item_func),
+ BOOKMARK_SEPARATOR_FUNC(separator_func))
+{
+ guint k;
+ BookmarkInfo *bi;
+
+ for(k=0; k < b->bookmarks->len; k++){
+ bi = g_ptr_array_index(b->bookmarks, k);
+ item_func(pass_thru, bi->label, bi->uri, bi->icon);
+ }
+}
+
+
+// vim: ai et tabstop=4
Modified: xfce4-places-plugin/branches/volumes/panel-plugin/places.c
===================================================================
--- xfce4-places-plugin/branches/volumes/panel-plugin/places.c 2007-04-04 22:24:07 UTC (rev 2662)
+++ xfce4-places-plugin/branches/volumes/panel-plugin/places.c 2007-04-05 06:46:29 UTC (rev 2663)
@@ -22,34 +22,18 @@
#endif
#include <gtk/gtk.h>
-#include <glib/gstdio.h>
-#include <libxfce4util/libxfce4util.h>
#include <libxfce4panel/xfce-panel-plugin.h>
#include <libxfce4panel/xfce-arrow-button.h>
#include <libxfce4panel/xfce-hvbox.h>
#include <libxfce4panel/xfce-panel-convenience.h>
#include <libxfcegui4/libxfcegui4.h>
-#include <thunar-vfs/thunar-vfs.h>
+#include "model.c"
#include "places.h"
-#include "unescape_uri.c"
#define PLUGIN_NAME "places"
-/********** Structs **********/
-static BookmarkInfo*
-places_construct_BookmarkInfo(gchar *label, gchar *uri, gchar *icon)
-{
- BookmarkInfo *info = g_new(BookmarkInfo, 1);
- info->label = label;
- info->uri = uri;
- info->icon = icon;
- info->volume = NULL;
- return info;
-}
-
-
/********** Initialization **********/
XFCE_PANEL_PLUGIN_REGISTER_EXTERNAL(places_construct);
@@ -58,21 +42,15 @@
{
DBG ("Construct: %s", PLUGIN_NAME);
- thunar_vfs_init();
-
PlacesData *pd = panel_slice_new0(PlacesData);
pd->plugin = plugin;
- places_init_bookmarks(pd);
+
+ pd->bookmarks = places_bookmarks_init();
+
places_init_ui(pd);
//xfce_textdomain (GETTEXT_PACKAGE, LOCALEDIR, "UTF-8");
- g_signal_connect (pd->bookmarks_volumes_manager, "volumes-added",
- G_CALLBACK (places_cb_volumes_added), pd);
-
- g_signal_connect (pd->bookmarks_volumes_manager, "volumes-removed",
- G_CALLBACK (places_cb_volumes_removed), pd);
-
g_signal_connect (pd->panel_button, "button-release-event",
G_CALLBACK (places_cb_button_act), NULL);
@@ -91,146 +69,6 @@
}
static void
-places_init_bookmarks(PlacesData *pd)
-{
- DBG("initializing");
-
- pd->bookmarks_system = g_ptr_array_sized_new(4);
- places_init_bookmarks_system(pd);
-
- pd->bookmarks_volumes = g_ptr_array_new();
- pd->bookmarks_volumes_changed = FALSE;
- pd->bookmarks_volumes_manager = thunar_vfs_volume_manager_get_default();
- places_init_bookmarks_volumes(pd);
-
- pd->bookmarks_user_filename = g_build_filename(xfce_get_homedir(), ".gtk-bookmarks", NULL);
- pd->bookmarks_user_loaded = -1;
- pd->bookmarks_user = g_ptr_array_new();
- places_init_bookmarks_user(pd);
-
-}
-
-static void
-places_init_bookmarks_system(PlacesData *pd)
-{
- DBG("initializing");
-
- const gchar *home_dir = xfce_get_homedir();
-
- // These icon names are consistent with Thunar.
-
- // Home
- g_ptr_array_add(pd->bookmarks_system, places_construct_BookmarkInfo(g_strdup(g_get_user_name()),
- g_strdup(home_dir), "gnome-fs-home"));
-
- // Trash
- g_ptr_array_add(pd->bookmarks_system, places_construct_BookmarkInfo("Trash",
- "trash:///", "gnome-fs-trash-full"));
-
- // Desktop
- g_ptr_array_add(pd->bookmarks_system, places_construct_BookmarkInfo("Desktop",
- g_build_filename(home_dir, "Desktop", NULL), "gnome-fs-desktop"));
-
- // File System (/)
- g_ptr_array_add(pd->bookmarks_system, places_construct_BookmarkInfo("File System",
- "/", "gnome-dev-harddisk"));
-}
-
-static gboolean
-places_show_volume(ThunarVfsVolume *volume){
-
- DBG("Volume: %s [mounted=%x removable=%x present=%x]", thunar_vfs_volume_get_name(volume),
- thunar_vfs_volume_is_mounted(volume),
- thunar_vfs_volume_is_removable(volume),
- thunar_vfs_volume_is_present(volume));
-
- return thunar_vfs_volume_is_mounted(volume) &&
- thunar_vfs_volume_is_removable(volume) &&
- thunar_vfs_volume_is_present(volume);
-}
-
-
-static void
-places_bookmarks_volumes_add(PlacesData *pd, const GList *volumes)
-{
- ThunarVfsVolume *volume;
-
- while(volumes){
- volume = THUNAR_VFS_VOLUME(volumes->data);
-
- g_signal_connect (volume, "changed",
- G_CALLBACK (places_cb_volume_changed), pd);
-
- if(places_show_volume(volume)){
-
- g_object_ref(volume);
-
- BookmarkInfo *bi = places_construct_BookmarkInfo(
- g_strdup(thunar_vfs_volume_get_name(volume)),
- thunar_vfs_path_dup_uri(thunar_vfs_volume_get_mount_point(volume)),
- g_strdup(thunar_vfs_volume_lookup_icon_name(volume, gtk_icon_theme_get_default())));
- bi->volume = volume;
-
- g_ptr_array_add(pd->bookmarks_volumes, bi);
- }
-
- volumes = volumes->next;
- }
-
-}
-
-static void
-places_init_bookmarks_volumes(PlacesData *pd)
-{
- DBG("initializing");
-
- places_bookmarks_volumes_add(pd, thunar_vfs_volume_manager_get_volumes(pd->bookmarks_volumes_manager));
-
-}
-
-
-static void
-places_init_bookmarks_user(PlacesData *pd)
-{
- DBG("initializing");
-
- gchar *contents;
- gchar **split;
- gchar **lines;
- int i;
-
- if (!g_file_get_contents(pd->bookmarks_user_filename, &contents, NULL, NULL)) {
- DBG("Error opening gtk bookmarks file");
- }else{
-
- pd->bookmarks_user_loaded = places_get_bookmarks_user_mtime(pd);
-
- lines = g_strsplit (contents, "\n", -1);
- g_free (contents);
-
- for (i = 0; lines[i]; i++) {
- if(!lines[i][0])
- continue;
-
- // See if the line is in the form "file:///path" or "file:///path friendly-name"
- split = g_strsplit(lines[i], " ", 2);
- if(split[1]){
- g_ptr_array_add(pd->bookmarks_user, places_construct_BookmarkInfo(g_strdup(split[1]),
- g_strdup(split[0]), "gnome-fs-directory"));
- }else{
- g_ptr_array_add(pd->bookmarks_user, places_construct_BookmarkInfo(places_unescape_uri_string(g_strrstr(lines[i], "/") + sizeof(gchar)),
- g_strdup(lines[i]), "gnome-fs-directory"));
- }
- g_free(split);
-
- }
-
- g_strfreev(lines);
- }
-}
-
-
-static void
places_init_ui(PlacesData *pd)
{
DBG("initializing");
@@ -280,95 +118,71 @@
}
static void
-places_init_panel_menu(PlacesData *pd)
+places_build_menu_item(gpointer _pd, const gchar *label, const gchar *uri, const gchar *icon)
{
- DBG("initializing");
+ g_assert(_pd);
+ g_return_if_fail(label && label != "");
+ g_return_if_fail(uri && uri != "");
- pd->panel_menu = gtk_menu_new();
- pd->panel_menu_open = FALSE;
-
- places_init_panel_menu_system(pd);
-
- places_init_panel_menu_volumes(pd);
+ PlacesData *pd = (PlacesData*) _pd;
+ GtkWidget *item = gtk_image_menu_item_new_with_label(label);
- // separator
- gtk_menu_shell_append(GTK_MENU_SHELL(pd->panel_menu),
- places_bookmark_info_to_gtk_menu_item(NULL));
+ if(G_LIKELY(icon != NULL)){
+ GdkPixbuf *pb = xfce_themed_icon_load(icon, 16);
+
+ if(G_LIKELY(pb != NULL)){
+ GtkWidget *image = gtk_image_new_from_pixbuf(pb);
+ g_object_unref(pb);
+ gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
+ }
+ }
- places_init_panel_menu_user(pd);
-
-
- gtk_widget_show(pd->panel_menu);
- // This helps allocate resources beforehand so it'll pop up faster the first time
- gtk_widget_realize(pd->panel_menu);
-
- gtk_menu_attach_to_widget(GTK_MENU(pd->panel_menu), pd->panel_arrow, NULL);
-
+ g_signal_connect(item, "button-release-event",
+ G_CALLBACK(places_cb_menu_item_act), (gchar*) uri);
+ gtk_menu_shell_append(GTK_MENU_SHELL(pd->panel_menu), item);
}
-
static void
-places_init_panel_menu_system(PlacesData *pd)
+places_build_menu_separator(gpointer _pd)
{
- DBG("initializing");
-
- int k;
- for(k = 0; k < pd->bookmarks_system->len; k++){
- BookmarkInfo *i = g_ptr_array_index(pd->bookmarks_system, k);
- gtk_menu_shell_append(GTK_MENU_SHELL(pd->panel_menu),
- places_bookmark_info_to_gtk_menu_item(i));
- }
+ g_assert(_pd);
+ PlacesData *pd = (PlacesData*) _pd;
+ gtk_menu_shell_append(GTK_MENU_SHELL(pd->panel_menu),
+ gtk_menu_item_new());
}
static void
-places_init_panel_menu_volumes(PlacesData *pd)
+places_init_panel_menu(PlacesData *pd)
{
DBG("initializing");
+ g_assert(pd && pd->panel_menu == NULL);
- int k;
- for(k = 0; k < pd->bookmarks_volumes->len; k++){
- BookmarkInfo *i = g_ptr_array_index(pd->bookmarks_volumes, k);
- gtk_menu_shell_append(GTK_MENU_SHELL(pd->panel_menu),
- places_bookmark_info_to_gtk_menu_item(i));
- }
-}
+ pd->panel_menu = gtk_menu_new();
+ pd->panel_menu_open = FALSE;
+
+ places_bookmarks_visit(pd->bookmarks, pd,
+ places_build_menu_item,
+ places_build_menu_separator);
+ gtk_widget_show_all(pd->panel_menu);
-static void
-places_init_panel_menu_user(PlacesData *pd)
-{
- DBG("initializing");
+ // This helps allocate resources beforehand so it'll pop up faster the first time
+ gtk_widget_realize(pd->panel_menu);
+
+ gtk_menu_attach_to_widget(GTK_MENU(pd->panel_menu), pd->panel_arrow, NULL);
- int k;
- for(k = 0; k < pd->bookmarks_user->len; k++){
- BookmarkInfo *i = g_ptr_array_index(pd->bookmarks_user, k);
- gtk_menu_shell_append(GTK_MENU_SHELL(pd->panel_menu),
- places_bookmark_info_to_gtk_menu_item(i));
-
- }
}
-
-
/********** Library **********/
static void
places_load_thunar(const gchar *path)
{
gchar *cmd = g_strconcat("thunar ", path, NULL);
- xfce_exec(cmd, FALSE, FALSE, NULL);
+ xfce_exec(cmd, FALSE, TRUE, NULL);
g_free(cmd);
}
-static time_t
-places_get_bookmarks_user_mtime(PlacesData *pd)
-{
- struct stat buf;
- if(g_stat(pd->bookmarks_user_filename, &buf) == 0)
- return buf.st_mtime;
- return 0;
-}
-
/********** UI Helpers **********/
static void
@@ -383,34 +197,6 @@
pd->panel_menu_open = FALSE;
}
-static GtkWidget*
-places_bookmark_info_to_gtk_menu_item(BookmarkInfo *i)
-{
- GtkWidget *item;
- GtkWidget *image;
-
- if(i != NULL) { // regular item
- item = gtk_image_menu_item_new_with_label(i->label);
-
- GdkPixbuf *pb = xfce_themed_icon_load(i->icon, 16);
- if(pb != NULL){
- image = gtk_image_new_from_pixbuf(pb);
- g_object_unref(pb);
- gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
- }
-
- g_signal_connect (item, "button-release-event",
- G_CALLBACK(places_cb_menu_item_act), i);
-
- } else { //separator
- item = gtk_menu_item_new();
- }
-
- gtk_widget_show(item);
-
- return item;
-}
-
static void
places_ui_redraw(PlacesData *pd)
{
@@ -448,89 +234,6 @@
}
-/********** ThunarVFS Callbacks **********/
-
-static void
-places_cb_volume_changed(ThunarVfsVolume *volume, PlacesData *pd){
- DBG("volume changed");
- // unfortunately there tends to be like 3 of these in a row
-
- guint k;
-
- if(places_show_volume(volume)){
-
- // make sure it's in the array
- for(k = 0; k < pd->bookmarks_volumes->len; k++){
- BookmarkInfo *bi = g_ptr_array_index(pd->bookmarks_volumes, k);
- if(bi->volume == volume)
- break;
- }
-
- if(k == pd->bookmarks_volumes->len){ // it's not there
- DBG("adding volume to array");
-
- places_bookmarks_volumes_add(pd, g_list_prepend(NULL, volume));
- pd->bookmarks_volumes_changed = TRUE;
- }else{
- DBG("volume already in array");
- }
-
- }else{
- // make sure it's not in the array
- for(k = 0; k < pd->bookmarks_volumes->len; k++){
- BookmarkInfo *bi = g_ptr_array_index(pd->bookmarks_volumes, k);
- if(bi->volume == volume){ // it is there
- DBG("dropping volume from array");
-
- bi = g_ptr_array_remove_index(pd->bookmarks_volumes, k);
- g_object_unref(bi->volume);
- g_free(bi);
-
- pd->bookmarks_volumes_changed = TRUE;
- }
- }
- }
-}
-
-static void
-places_cb_volumes_added(ThunarVfsVolumeManager *volume_manager, const GList *volumes, PlacesData *pd){
- DBG("volumes added");
- places_bookmarks_volumes_add(pd, volumes);
- pd->bookmarks_volumes_changed = TRUE;
-}
-static void
-places_cb_volumes_removed(ThunarVfsVolumeManager *volume_manager, const GList *volumes, PlacesData *pd){
- DBG("volumes removed");
-
- GList *vol_iter;
- guint k;
-
- // step through existing bookmarks
- for(k = 0; k < pd->bookmarks_volumes->len; k++){
- BookmarkInfo *bi = g_ptr_array_index(pd->bookmarks_volumes, k);
-
- // step through removals
- vol_iter = (GList*) volumes;
- while(vol_iter){
- if(bi->volume == vol_iter->data){ // it is there
-
- // delete the bookmark
- bi = g_ptr_array_remove_index(pd->bookmarks_volumes, k);
- DBG("Removing bookmark %s", bi->label);
-
- if(bi->volume)
- g_object_unref(bi->volume);
- g_free(bi);
-
- pd->bookmarks_volumes_changed = TRUE;
- }
-
- vol_iter = vol_iter->next;
- }
- }
-}
-
-
/********** Gtk Callbacks **********/
static gboolean
@@ -630,26 +333,13 @@
// This will make it behave like a mouse release when it's really a mouse press
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(arrow), TRUE);
- gboolean volumes_changed = pd->bookmarks_volumes_changed;
- gboolean user_changed = places_get_bookmarks_user_mtime(pd) > pd->bookmarks_user_loaded;
-
- if(volumes_changed || user_changed){
-
+ DBG("Has the model changed?");
+ if(places_bookmarks_changed(pd->bookmarks)){
+ DBG("Model changed");
+
gtk_widget_destroy(pd->panel_menu);
-
- if(volumes_changed){
- DBG("volumes changed");
- pd->bookmarks_volumes_changed = FALSE;
- }
+ pd->panel_menu = NULL;
- if(user_changed){
- DBG("bookmarks file changed");
-
- g_ptr_array_free(pd->bookmarks_user, TRUE);
- pd->bookmarks_user = g_ptr_array_new();
- places_init_bookmarks_user(pd);
- }
-
places_init_panel_menu(pd);
g_signal_connect (pd->panel_menu, "deactivate",
G_CALLBACK(places_cb_menu_close), pd);
@@ -665,11 +355,11 @@
}
static gboolean
-places_cb_menu_item_act(GtkWidget *widget, GdkEventButton *event, BookmarkInfo *item)
+places_cb_menu_item_act(GtkWidget *widget, GdkEventButton *event, const gchar* uri)
{
DBG("load thunar for item");
- places_load_thunar((const gchar*) item->uri);
+ places_load_thunar(uri);
return FALSE;
}
@@ -682,9 +372,7 @@
DBG("load thunar at home directory");
- // TODO: don't open if they dragged mouse away
- places_load_thunar(g_get_home_dir());
-
+ places_load_thunar(NULL);
return FALSE;
}
@@ -693,21 +381,8 @@
{
DBG ("Free data: %s", PLUGIN_NAME);
g_assert(pd != NULL);
- guint k;
-
- thunar_vfs_shutdown();
- g_object_unref(pd->bookmarks_volumes_manager);
-
- for(k = 0; k < pd->bookmarks_volumes->len; k++){
- BookmarkInfo *bi = g_ptr_array_remove_index(pd->bookmarks_volumes, k);
- if(bi->volume)
- g_object_unref(bi->volume);
- }
-
- g_ptr_array_free(pd->bookmarks_system, TRUE);
- g_ptr_array_free(pd->bookmarks_volumes, TRUE);
- g_ptr_array_free(pd->bookmarks_user, TRUE);
- g_free(pd->bookmarks_user_filename);
+
+ places_bookmarks_finalize(pd->bookmarks);
panel_slice_free(PlacesData, pd);
}
Modified: xfce4-places-plugin/branches/volumes/panel-plugin/places.h
===================================================================
--- xfce4-places-plugin/branches/volumes/panel-plugin/places.h 2007-04-04 22:24:07 UTC (rev 2662)
+++ xfce4-places-plugin/branches/volumes/panel-plugin/places.h 2007-04-05 06:46:29 UTC (rev 2663)
@@ -33,61 +33,28 @@
gboolean panel_menu_open;
int panel_size;
- // bookmarks
- GPtrArray *bookmarks_system;
- GPtrArray *bookmarks_volumes;
- gboolean bookmarks_volumes_changed;
- ThunarVfsVolumeManager *bookmarks_volumes_manager;
- GPtrArray *bookmarks_user;
- gchar *bookmarks_user_filename;
- time_t bookmarks_user_loaded;
+ gpointer bookmarks;
} PlacesData;
-
-typedef struct
-{
- gchar *label;
- gchar *uri;
- gchar *icon;
- ThunarVfsVolume *volume;
-} BookmarkInfo;
-
-static BookmarkInfo* places_construct_BookmarkInfo(gchar *label, gchar *uri, gchar *icon);
-
// Init
static void places_construct(XfcePanelPlugin*);
- static void places_init_bookmarks(PlacesData*);
- static void places_init_bookmarks_system(PlacesData*);
- static void places_init_bookmarks_volumes(PlacesData*);
- static void places_init_bookmarks_user(PlacesData*);
- static void places_init_ui(PlacesData*);
- static void places_init_panel_menu(PlacesData*);
- static void places_init_panel_menu_system(PlacesData*);
- static void places_init_panel_menu_volumes(PlacesData*);
- static void places_init_panel_menu_user(PlacesData*);
+static void places_init_ui(PlacesData*);
+static void places_init_panel_menu(PlacesData*);
-
// Library
static void places_load_thunar(const gchar*);
-static time_t places_get_bookmarks_user_mtime(PlacesData*);
// UI Helpers
static void places_close_menu(PlacesData*);
-static GtkWidget* places_bookmark_info_to_gtk_menu_item(BookmarkInfo*);
static void places_ui_redraw(PlacesData*);
-// ThunarVFS Callbacks
-static void places_cb_volume_changed(ThunarVfsVolume*, PlacesData*);
-static void places_cb_volumes_added(ThunarVfsVolumeManager*, const GList*, PlacesData*);
-static void places_cb_volumes_removed(ThunarVfsVolumeManager*, const GList*, PlacesData*);
-
// GTK Callbacks
static gboolean places_cb_size_changed(XfcePanelPlugin*, int, PlacesData*);
static void places_cb_menu_position(GtkMenu*, int*, int*, gboolean*, PlacesData*);
static void places_cb_menu_close(GtkMenuShell*, PlacesData*);
static gboolean places_cb_menu_open(GtkButton*, GdkEventButton*, PlacesData*);
-static gboolean places_cb_menu_item_act(GtkWidget*, GdkEventButton*, BookmarkInfo*);
+static gboolean places_cb_menu_item_act(GtkWidget*, GdkEventButton*, const gchar*);
static gboolean places_cb_button_act(GtkWidget*, GdkEventButton*, gpointer);
static void places_cb_free_data(XfcePanelPlugin*, PlacesData*);
Modified: xfce4-places-plugin/branches/volumes/panel-plugin/unescape_uri.c
===================================================================
--- xfce4-places-plugin/branches/volumes/panel-plugin/unescape_uri.c 2007-04-04 22:24:07 UTC (rev 2662)
+++ xfce4-places-plugin/branches/volumes/panel-plugin/unescape_uri.c 2007-04-05 06:46:29 UTC (rev 2663)
@@ -27,6 +27,9 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
+#ifndef _XFCE_PANEL_PLACES_UNESCAPE_URI_C
+#define _XFCE_PANEL_PLACES_UNESCAPE_URI_C
+
#include <string.h>
#include <glib.h>
@@ -116,3 +119,5 @@
places_unescape_uri_string(const gchar* escaped) {
return g_unescape_uri_string(escaped, -1, "", FALSE);
}
+
+#endif
More information about the Goodies-commits
mailing list