[Goodies-commits] r7449 - in xfce4-notes-plugin/trunk: . panel-plugin

Mike Massonnet mmassonnet at xfce.org
Tue Jun 2 13:28:26 CEST 2009


Author: mmassonnet
Date: 2009-06-02 11:28:26 +0000 (Tue, 02 Jun 2009)
New Revision: 7449

Modified:
   xfce4-notes-plugin/trunk/ChangeLog
   xfce4-notes-plugin/trunk/panel-plugin/application.vala
   xfce4-notes-plugin/trunk/panel-plugin/hypertextview.vala
   xfce4-notes-plugin/trunk/panel-plugin/window.vala
Log:
Load/Save notes from files

Modified: xfce4-notes-plugin/trunk/ChangeLog
===================================================================
--- xfce4-notes-plugin/trunk/ChangeLog	2009-05-31 12:13:31 UTC (rev 7448)
+++ xfce4-notes-plugin/trunk/ChangeLog	2009-06-02 11:28:26 UTC (rev 7449)
@@ -1,3 +1,16 @@
+2009-05-31  Mike Massonnet <mmassonnet at xfce.org>
+
+Load/Save notes from files
+	* panel-plugin/window.vala:
+	  - New signals note-inserted, note-deleted and note-renamed sent with
+	  the Xnp.Note in parameters along side with the insert_note(),
+	  delete_note() and rename_note() methods.
+	* panel-plugin/application.vala:
+	  - Load the initial existing notes.
+	  - Delete and create the files for the notes.
+	* panel-plugin/window.vala, panel-plugin/application.vala:
+	  - Popup a question before deleting a window and a note.
+
 2009-05-30  Mike Massonnet <mmassonnet at xfce.org>
 
 Request a 22x22 minimum size for the buttons close/next/prev/...

Modified: xfce4-notes-plugin/trunk/panel-plugin/application.vala
===================================================================
--- xfce4-notes-plugin/trunk/panel-plugin/application.vala	2009-05-31 12:13:31 UTC (rev 7448)
+++ xfce4-notes-plugin/trunk/panel-plugin/application.vala	2009-06-02 11:28:26 UTC (rev 7449)
@@ -2,9 +2,6 @@
  *  Notes - panel plugin for Xfce Desktop Environment
  *  Copyright (c) 2009  Mike Massonnet <mmassonnet at xfce.org>
  *
- *  TODO:
- *  - Nothing
- *
  *  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
@@ -27,12 +24,28 @@
 	public class Application : GLib.Object {
 
 		private SList<Xnp.Window> window_list;
+		private string notes_path;
 
+		construct {
+			this.notes_path = "%s/notes".printf (GLib.Environment.get_user_data_dir ());
+		}
+
 		public Application () {
-			/* TODO Load existing windows */
-			/* Create an initial empty window */
-			create_window (null);
-			create_window (null);
+			string name;
+			bool found = false;
+			try {
+				var dir = Dir.open (this.notes_path, 0);
+				while ((name = dir.read_name ()) != null) {
+					create_window (name);
+					found = true;
+				}
+			}
+			catch (Error e) {
+				GLib.DirUtils.create_with_parents (this.notes_path, 0700);
+			}
+			if (found == false) {
+				create_window (null);
+			}
 		}
 
 		/*
@@ -43,10 +56,12 @@
 		 * create_window:
 		 *
 		 * Creates a new Xnp.Window and stores it inside window_list.
+		 * If a name is given, it assumes it can load existing notes.
 		 */
 		public void create_window (string? name) {
 			var window = new Xnp.Window ();
 
+			/* Set window name */
 			if (name == null) {
 				string window_name = "Notes";
 				int len = (int)this.window_list.length ();
@@ -64,13 +79,30 @@
 				window.name = name;
 			}
 
+			/* Add to window_list */
 			this.window_list.append (window);
 			foreach (var win in this.window_list) {
 				win.set_window_list (this.window_list);
 			}
 
-			this.load_window_data (window);
+			/* Insert initial notes */
+			if (name != null) {
+				this.load_window_data (window);
+			}
+			else {
+				var note = window.insert_note ();
 
+				string window_path = "%s/%s".printf (this.notes_path, window.name);
+				GLib.DirUtils.create_with_parents (window_path, 0700);
+				try {
+					string note_path = "%s/%s".printf (window_path, note.name);
+					GLib.FileUtils.set_contents (note_path, "", -1);
+				}
+				catch (FileError e) {
+				}
+			}
+
+			/* Connect signals */
 			window.action += (win, action) => {
 				if (action == "rename") {
 					rename_window (win);
@@ -83,8 +115,35 @@
 				}
 			};
 			window.save_data += (win, note) => {
-				debug ("save-data on %s::%s", win.name, note.name);
+				string path = "%s/%s/%s".printf (this.notes_path, win.name, note.name);
+				try {
+					Gtk.TextIter start, end;
+					var buffer = note.text_view.get_buffer ();
+					buffer.get_bounds (out start, out end);
+					string contents = buffer.get_text (start, end, true);
+					GLib.FileUtils.set_contents (path, contents, -1);
+				}
+				catch (FileError e) {
+					warning ("%s", e.message);
+				}
 			};
+			window.note_inserted += (win, note) => {
+				string path = "%s/%s/%s".printf (this.notes_path, win.name, note.name);
+				try {
+					GLib.FileUtils.set_contents (path, "", -1);
+				}
+				catch (FileError e) {
+				}
+			};
+			window.note_deleted += (win, note) => {
+				string path = "%s/%s/%s".printf (this.notes_path, win.name, note.name);
+				GLib.FileUtils.unlink (path);
+			};
+			window.note_renamed += (win, note, old_name) => {
+				string old_path = "%s/%s/%s".printf (this.notes_path, win.name, old_name);
+				string new_path = "%s/%s/%s".printf (this.notes_path, win.name, note.name);
+				GLib.FileUtils.rename (old_path, new_path);
+			};
 
 			window.show ();
 		}
@@ -92,12 +151,30 @@
 		/**
 		 * load_window_data:
 		 *
-		 * Looks up the window name for existing notes otherwise
-		 * inserts an initial empty note.
+		 * Loads existing notes inside the window.
 		 */
 		private void load_window_data (Xnp.Window window) {
-			/* TODO load existing notes */
-			window.insert_note ();
+			string name;
+			string path = "%s/%s".printf (this.notes_path, window.name);
+			try {
+				var dir = GLib.Dir.open (path, 0);
+				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 note = window.insert_note ();
+						note.name = name;
+						var buffer = note.text_view.get_buffer ();
+						buffer.set_text (contents, -1);
+					}
+					catch (FileError e) {
+						warning ("%s", e.message);
+					}
+				}
+			}
+			catch (FileError e) {
+			}
 		}
 
 		/**
@@ -124,7 +201,7 @@
 			int res = dialog.run ();
 			dialog.hide ();
 			if (res == Gtk.ResponseType.OK) {
-				string name = entry.text;
+				weak string name = entry.text;
 				if (window_name_exists (name)) {
 					var error_dialog = new Gtk.MessageDialog (window, Gtk.DialogFlags.DESTROY_WITH_PARENT,
 						Gtk.MessageType.ERROR, Gtk.ButtonsType.CLOSE, "The name %s is already in use", name);
@@ -132,7 +209,10 @@
 					error_dialog.destroy ();
 				}
 				else {
+					string old_path = "%s/%s".printf (this.notes_path, window.name);
+					string new_path = "%s/%s".printf (this.notes_path, name);
 					window.name = name;
+					GLib.FileUtils.rename (old_path, new_path);
 				}
 			}
 			dialog.destroy ();
@@ -144,6 +224,26 @@
 		 * Delete the window.
 		 */
 		private void delete_window (Xnp.Window window) {
+			var dialog = new Gtk.MessageDialog (window, Gtk.DialogFlags.DESTROY_WITH_PARENT,
+				Gtk.MessageType.QUESTION, Gtk.ButtonsType.YES_NO, "Are you sure you want to delete this group?");
+			int res = dialog.run ();
+			dialog.destroy ();
+			if (res != Gtk.ResponseType.YES)
+				return;
+
+			string name;
+			string path = "%s/%s".printf (this.notes_path, window.name);
+			try {
+			var dir = GLib.Dir.open (path, 0);
+			while ((name = dir.read_name ()) != null) {
+				string filename = "%s/%s".printf (path, name);
+				GLib.FileUtils.unlink (filename);
+			}
+			GLib.DirUtils.remove (path);
+			}
+			catch (FileError e) {
+			}
+
 			this.window_list.remove (window);
 			window.destroy ();
 

Modified: xfce4-notes-plugin/trunk/panel-plugin/hypertextview.vala
===================================================================
--- xfce4-notes-plugin/trunk/panel-plugin/hypertextview.vala	2009-05-31 12:13:31 UTC (rev 7448)
+++ xfce4-notes-plugin/trunk/panel-plugin/hypertextview.vala	2009-06-02 11:28:26 UTC (rev 7449)
@@ -53,7 +53,7 @@
 		}
 
 		construct {
-			this.font = "Sans 14";
+			this.font = "Sans 12";
 		}
 
 		public HypertextView () {

Modified: xfce4-notes-plugin/trunk/panel-plugin/window.vala
===================================================================
--- xfce4-notes-plugin/trunk/panel-plugin/window.vala	2009-05-31 12:13:31 UTC (rev 7448)
+++ xfce4-notes-plugin/trunk/panel-plugin/window.vala	2009-06-02 11:28:26 UTC (rev 7449)
@@ -108,6 +108,9 @@
 
 		public signal void action (string action);
 		public signal void save_data (Xnp.Note note);
+		public signal void note_inserted (Xnp.Note note);
+		public signal void note_deleted (Xnp.Note note);
+		public signal void note_renamed (Xnp.Note note, string old_name);
 
 		construct {
 			base.name = "xfce4-notes-plugin";
@@ -834,8 +837,8 @@
 				this.goright_box.sensitive = false;
 			}
 			else {
-				this.goleft_box.sensitive = page_num == 0 ? false : true;
-				this.goright_box.sensitive = page_num + 1 == n_pages ? false : true;
+				this.goleft_box.sensitive = page_num > 0 ? true : false;
+				this.goright_box.sensitive = page_num + 1 < n_pages ? true : false;
 			}
 		}
 
@@ -869,6 +872,7 @@
 
 			note.show ();
 			this.notebook.insert_page (note, null, page);
+			this.note_inserted (note);
 			return note;
 		}
 
@@ -887,10 +891,10 @@
 		 * Delete note at page @page.
 		 */
 		public void delete_note (int page) {
-			var child = this.notebook.get_nth_page (page);
+			var note = (Xnp.Note)this.notebook.get_nth_page (page);
 
-			if (((Xnp.Note)child).text_view.buffer.get_char_count () > 0) {
-				var dialog = new Gtk.MessageDialog (this, Gtk.DialogFlags.MODAL|Gtk.DialogFlags.DESTROY_WITH_PARENT,
+			if (note.text_view.buffer.get_char_count () > 0) {
+				var dialog = new Gtk.MessageDialog (this, Gtk.DialogFlags.DESTROY_WITH_PARENT,
 					Gtk.MessageType.QUESTION, Gtk.ButtonsType.YES_NO, "Are you sure you want to delete this note?");
 				int res = dialog.run ();
 				dialog.destroy ();
@@ -899,7 +903,8 @@
 			}
 
 			this.notebook.remove_page (page);
-			child.destroy ();
+			this.note_deleted (note);
+			note.destroy ();
 			if (this.notebook.get_n_pages () == 0)
 				this.insert_note ();
 		}
@@ -932,8 +937,20 @@
 
 			int res = dialog.run ();
 			dialog.hide ();
-			if (res == Gtk.ResponseType.OK)
-				note.name = entry.text;
+			if (res == Gtk.ResponseType.OK) {
+				weak string name = entry.text;
+				if (note_name_exists (name)) {
+					var error_dialog = new Gtk.MessageDialog (this, Gtk.DialogFlags.DESTROY_WITH_PARENT,
+						Gtk.MessageType.ERROR, Gtk.ButtonsType.CLOSE, "The name %s is already in use", name);
+					error_dialog.run ();
+					error_dialog.destroy ();
+				}
+				else {
+					string old_name = note.name;
+					note.name = name;
+					this.note_renamed (note, old_name);
+				}
+			}
 			dialog.destroy ();
 		}
 




More information about the Goodies-commits mailing list