[Xfce4-commits] <xfce4-notes-plugin:master> Add configurable notes path

Mike Massonnet noreply at xfce.org
Mon Nov 15 07:44:01 CET 2010


Updating branch refs/heads/master
         to f5d46e944af70a9295936e92713120c1717a822d (commit)
       from c75e25ed2aaaa38cdecc1e4149c96739ba3b6444 (commit)

commit f5d46e944af70a9295936e92713120c1717a822d
Author: Mike Massonnet <mmassonnet at xfce.org>
Date:   Mon Nov 15 07:41:36 2010 +0100

    Add configurable notes path
    
    Xnp.Application has a new notes-path property that can be tweaked
    through Xfconf. The new directory must be writable and empty.
    
    Added a directory button inside the settings dialog to choose the new
    notes path.

 lib/application.vala       |   68 ++++++++++++++++++++++++++++++++++++-------
 src/xfce4-notes-settings.c |   66 ++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 122 insertions(+), 12 deletions(-)

diff --git a/lib/application.vala b/lib/application.vala
index df4414f..1f45804 100644
--- a/lib/application.vala
+++ b/lib/application.vala
@@ -25,17 +25,11 @@ namespace Xnp {
 	public class Application : GLib.Object {
 
 		private SList<Xnp.Window> window_list;
-		private string notes_path;
+		public string notes_path { get; set construct; }
 		public string config_file { get; construct; }
 		private Xfconf.Channel xfconf_channel;
 
 		construct {
-			notes_path = "%s/notes".printf (GLib.Environment.get_user_data_dir ());
-		}
-
-		public Application (string config_file) {
-			GLib.Object (config_file: config_file);
-
 			var notesgtkrc = "%s/xfce4/xfce4-notes.gtkrc".printf (GLib.Environment.get_user_config_dir ());
 			Gtk.rc_parse (notesgtkrc);
 
@@ -56,19 +50,27 @@ namespace Xnp {
 			}
 
 			xfconf_channel = new Xfconf.Channel.with_property_base ("xfce4-panel", "/plugins/notes");
-			update_color ();
 
+			update_color ();
 			xfconf_channel.property_changed["/global/background-color"].connect (() => {
 				update_color ();
 			});
-
 			Gtk.Settings.get_default ().notify["gtk-theme-name"].connect (() => {
 				update_color ();
 			});
 
+			if (notes_path == null) {
+				var default_path = "%s/notes".printf (GLib.Environment.get_user_data_dir ());
+				notes_path = xfconf_channel.get_string ("/global/notes-path", default_path);
+			}
+			xfconf_channel.property_changed["/global/notes-path"].connect (() => {
+				update_notes_path ();
+			});
+
 			string name;
 			bool found = false;
 			try {
+				/* Load existing windows */
 				var dir = Dir.open (notes_path, 0);
 				while ((name = dir.read_name ()) != null) {
 					create_window (name);
@@ -79,10 +81,19 @@ namespace Xnp {
 				GLib.DirUtils.create_with_parents (notes_path, 0700);
 			}
 			if (found == false) {
+				/* Create first-run window */
 				create_window ();
 			}
 		}
 
+		public Application (string config_file) {
+			GLib.Object (config_file: config_file);
+		}
+
+		public Application.with_notes_path (string config_file, string notes_path) {
+			GLib.Object (config_file: config_file, notes_path: notes_path);
+		}
+
 		~Application () {
 			save_windows_configuration ();
 			xfconf_channel = null;
@@ -93,6 +104,41 @@ namespace Xnp {
 			}
 		}
 
+		private void update_notes_path () {
+			var new_notes_path = xfconf_channel.get_string ("/global/notes-path", notes_path);
+			if (notes_path == new_notes_path) {
+				return;
+			}
+
+			/* Check that the new path is empty */
+			try {
+				var dir = Dir.open (new_notes_path, 0);
+				if (dir.read_name () != null) {
+					var error_dialog = new Gtk.MessageDialog (null, 0, Gtk.MessageType.ERROR, Gtk.ButtonsType.CLOSE,
+						_("Select notes path"));
+					error_dialog.format_secondary_text (_("The selected directory (%s) for the new notes path already contains files. You must select or create an empty directory."), new_notes_path);
+					error_dialog.run ();
+					error_dialog.destroy ();
+					xfconf_channel.set_string ("/global/notes-path", notes_path);
+					return;
+				}
+			}
+			catch (GLib.Error e) {
+			}
+
+			/* Create/move to the new path */
+			var dirname = Path.get_dirname (new_notes_path);
+			if (GLib.DirUtils.create_with_parents (dirname, 0700) != 0 || GLib.FileUtils.rename (notes_path, new_notes_path) != 0) {
+				var error_dialog = new Gtk.MessageDialog (null, 0, Gtk.MessageType.ERROR, Gtk.ButtonsType.CLOSE,
+					_("Select notes path"));
+				error_dialog.format_secondary_text (_("Unable to select directory for new notes path: %s"), strerror (errno));
+				error_dialog.run ();
+				error_dialog.destroy ();
+				xfconf_channel.set_string ("/global/notes-path", notes_path);
+				return;
+			}
+		}
+
 		private void update_color () {
 			string color = xfconf_channel.get_string ("/global/background-color", "#F7EB96");
 			if (color == "GTK+") {
@@ -243,8 +289,8 @@ namespace Xnp {
 				while ((name = dir.read_name ()) != null) {
 					try {
 						string contents;
-						string filename = "%s/%s".printf (path, name);
-						GLib.FileUtils.get_contents (filename, out contents, null);
+						var file = File.new_for_path ("%s/%s".printf (path, name));
+						GLib.FileUtils.get_contents (file.get_path (), out contents, null);
 						var note = window.insert_note ();
 						note.name = name;
 						var buffer = note.text_view.get_buffer ();
diff --git a/src/xfce4-notes-settings.c b/src/xfce4-notes-settings.c
index f66bb13..00f8b51 100644
--- a/src/xfce4-notes-settings.c
+++ b/src/xfce4-notes-settings.c
@@ -30,6 +30,9 @@
 #include "defines.h"
 #include "color.h"
 
+static GtkWidget *notes_path_button_new ();
+static void cb_notes_path_changed (GtkFileChooserButton *button, gpointer data);
+
 enum
 {
   COMBOBOX_TABS_NONE,
@@ -77,6 +80,7 @@ static gboolean timeout_cb_background_changed (gchar *color);
 static GtkWidget *color_button_new ();
 static gboolean cb_color_button_pressed (GtkButton *button, GdkEventButton *event, gpointer data);
 
+static GtkWidget *parent_window = NULL;
 static XfconfChannel *xfconf_channel = NULL;
 static GtkWidget *color_combobox = NULL;
 static GtkWidget *color_button = NULL;
@@ -91,7 +95,7 @@ prop_dialog_new (void)
     xfconf_channel = xfconf_channel_new_with_property_base ("xfce4-panel", "/plugins/notes");
 
   /* Dialog */
-  dialog =
+  parent_window = dialog =
     xfce_titled_dialog_new_with_buttons (_("Notes"), NULL,
                                          GTK_DIALOG_DESTROY_WITH_PARENT|GTK_DIALOG_NO_SEPARATOR,
                                          GTK_STOCK_CLOSE, GTK_RESPONSE_OK,
@@ -126,6 +130,16 @@ prop_dialog_new (void)
                           G_TYPE_BOOLEAN, G_OBJECT (button), "active");
   gtk_box_pack_start (GTK_BOX (box), button, TRUE, FALSE, 0);
 
+  /* Notes path */
+  hbox = gtk_hbox_new (FALSE, BORDER);
+  gtk_box_pack_start (GTK_BOX (box), hbox, TRUE, FALSE, 0);
+
+  label = gtk_label_new (_("Notes path:"));
+  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
+
+  button = notes_path_button_new (dialog);
+  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
+
   /* Tabs position */
   hbox = gtk_hbox_new (FALSE, BORDER);
   gtk_box_pack_start (GTK_BOX (box), hbox, TRUE, FALSE, 0);
@@ -198,6 +212,56 @@ prop_dialog_new (void)
 }
 
 static GtkWidget *
+notes_path_button_new (void)
+{
+  GtkWidget *dialog, *button;
+  gchar *default_path, *notes_path;
+
+  default_path = g_strdup_printf ("%s/notes", g_get_user_data_dir ());
+  notes_path = xfconf_channel_get_string (xfconf_channel, "/global/notes-path", default_path);
+
+  dialog = gtk_file_chooser_dialog_new (_("Select notes path"), GTK_WINDOW (parent_window),
+                                        GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
+                                        GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+                                        GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
+                                        NULL);
+  gtk_window_set_icon_name (GTK_WINDOW (dialog), GTK_STOCK_DIRECTORY);
+
+  button = GTK_WIDGET (g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
+                                     "title", _("Select notes path"),
+                                     "width-chars", 15,
+                                     "action", GTK_FILE_CHOOSER_ACTION_OPEN,
+                                     "dialog", dialog,
+                                     NULL));
+  g_object_set (dialog, "action", GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, NULL);
+  gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (button), notes_path);
+
+  g_signal_connect (button, "file-set", G_CALLBACK (cb_notes_path_changed), NULL);
+
+  g_free (default_path);
+  g_free (notes_path);
+
+  return button;
+}
+
+static void
+cb_notes_path_changed (GtkFileChooserButton *button,
+                       gpointer data)
+{
+  GFile *file;
+  gchar *notes_path;
+
+  file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (button));
+  notes_path = g_file_get_path (file);
+
+  if (notes_path != NULL)
+    xfconf_channel_set_string (xfconf_channel, "/global/notes-path", notes_path);
+
+  g_object_unref (file);
+  g_free (notes_path);
+}
+
+static GtkWidget *
 tabs_combo_box_new (void)
 {
   GtkWidget *combobox;



More information about the Xfce4-commits mailing list