[tomboy/xdg-migration2: 1/16] Use XDG specification instead of ~/.tomboy. Incomplete, no migration.



commit 11395ff285b02b6a1456ff7bec3c9cfd7c227ff3
Author: Sandy Armstrong <sanfordarmstrong gmail com>
Date:   Sat Aug 22 09:12:14 2009 -0700

    Use XDG specification instead of ~/.tomboy.  Incomplete, no migration.
    
    Still need migration, some instances of directory creation, a good way to
    notify users of the change, and a decision about what to do for Windows/Mac.

 Tomboy.mdp                                     |    1 +
 Tomboy/GnomeApplication.cs                     |   40 +++++++++--
 Tomboy/Hyena/XdgBaseDirectorySpec.cs           |   82 ++++++++++++++++++++++++
 Tomboy/Makefile.am                             |    2 +
 Tomboy/NativeApplication.cs                    |    6 ++-
 Tomboy/NoteManager.cs                          |    2 +-
 Tomboy/Synchronization/FileSystemSyncServer.cs |    6 +-
 Tomboy/Synchronization/FuseSyncServiceAddin.cs |    2 +-
 Tomboy/Synchronization/TomboySyncClient.cs     |    4 +-
 Tomboy/Tomboy.cs                               |    6 +-
 10 files changed, 132 insertions(+), 19 deletions(-)
---
diff --git a/Tomboy.mdp b/Tomboy.mdp
index 20565d7..2efa275 100644
--- a/Tomboy.mdp
+++ b/Tomboy.mdp
@@ -185,6 +185,7 @@
     <File name="Tomboy/Addins/WebSyncService/OAuth/Mono.Rocks/IEnumerable.cs" subtype="Code" buildaction="Compile" />
     <File name="Tomboy/Addins/WebSyncService/Api/RootInfo.cs" subtype="Code" buildaction="Compile" />
     <File name="Tomboy/gtk-sharp-beans/Global.cs" subtype="Code" buildaction="Compile" />
+    <File name="Tomboy/Hyena/XdgBaseDirectorySpec.cs" subtype="Code" buildaction="Compile" />
   </Contents>
   <References>
     <ProjectReference type="Gac" localcopy="True" refto="gdk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f" />
diff --git a/Tomboy/GnomeApplication.cs b/Tomboy/GnomeApplication.cs
index cb9312e..41821f9 100644
--- a/Tomboy/GnomeApplication.cs
+++ b/Tomboy/GnomeApplication.cs
@@ -7,17 +7,35 @@ using System.Xml;
 using Mono.Unix;
 using Mono.Unix.Native;
 
+using Hyena;
+
 namespace Tomboy
 {
 	public class GnomeApplication : INativeApplication
 	{
 		private Gnome.Program program;
-		private string confDir;
+		private static string confDir;
+		private static string dataDir;
+		private static string cacheDir;
+		private const string tomboyDirName = "tomboy";
 
-		public GnomeApplication ()
+		static GnomeApplication ()
 		{
-			confDir = Path.Combine (Environment.GetEnvironmentVariable ("HOME"),
-			                        ".tomboy");
+			string dataDir =
+				Path.Combine (XdgBaseDirectorySpec.GetUserDirectory ("XDG_DATA_HOME",
+				                                                     Path.Combine (".local", "share")),
+				              tomboyDirName);
+			string confDir =
+				Path.Combine (XdgBaseDirectorySpec.GetUserDirectory ("XDG_CONFIG_HOME",
+				                                                     ".config"),
+				              tomboyDirName);
+			string cacheDir =
+				Path.Combine (XdgBaseDirectorySpec.GetUserDirectory ("XDG_CACHE_HOME",
+				                                                     ".cache"),
+				              tomboyDirName);
+			
+			// TODO: Create with 0700 perms if they don't exist
+			//        (probably best to do this in NoteManager, AddinManager, etc, or Tomboy.cs?)
 		}
 
 		public void Initialize (string locale_dir,
@@ -152,10 +170,16 @@ namespace Tomboy
 			GtkBeans.Global.ShowUri (screen, help_uri);
 		}
 		
-		public string ConfDir {
-			get {
-				return confDir;
-			}
+		public string DataDirectory {
+			get { return dataDir; }
+		}
+		
+		public string ConfigurationDirectory {
+			get { return confDir; }
+		}
+		
+		public string CacheDirectory {
+			get { return cacheDir; }
 		}
 	}
 }
diff --git a/Tomboy/Hyena/XdgBaseDirectorySpec.cs b/Tomboy/Hyena/XdgBaseDirectorySpec.cs
new file mode 100644
index 0000000..e92084e
--- /dev/null
+++ b/Tomboy/Hyena/XdgBaseDirectorySpec.cs
@@ -0,0 +1,82 @@
+//
+// XdgBaseDirectorySpec.cs
+//
+// Author:
+//   Aaron Bockover <abockover novell com>
+//
+// Copyright (C) 2006-2008 Novell, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.IO;
+
+namespace Hyena
+{
+    public static class XdgBaseDirectorySpec
+    {
+        public static string GetUserDirectory (string key, string fallback)
+        {
+            string home_dir = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
+            string config_dir = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData);
+            
+            string env_path = Environment.GetEnvironmentVariable (key);
+            if (!String.IsNullOrEmpty (env_path)) {
+                return env_path;
+            }
+
+            string user_dirs_path = Path.Combine (config_dir, "user-dirs.dirs");
+
+            if (!File.Exists (user_dirs_path)) {
+                return Path.Combine (home_dir, fallback);
+            }
+
+            try {
+                using (StreamReader reader = new StreamReader (user_dirs_path)) {
+                    string line;
+                    while ((line = reader.ReadLine ()) != null) {
+                        line = line.Trim ();
+                        int delim_index = line.IndexOf ('=');
+                        if (delim_index > 8 && line.Substring (0, delim_index) == key) {
+                            string path = line.Substring (delim_index + 1).Trim ('"');
+                            bool relative = false;
+
+                            if (path.StartsWith ("$HOME/")) {
+                                relative = true;
+                                path = path.Substring (6);
+                            } else if (path.StartsWith ("~")) {
+                                relative = true;
+                                path = path.Substring (1);
+                            } else if (!path.StartsWith ("/")) {
+                                relative = true;
+                            }
+
+                            return relative ? Path.Combine (home_dir, path) : path;
+                        }
+                    }
+                }
+            } catch (FileNotFoundException) {
+            }
+            
+            return Path.Combine (home_dir, fallback);
+        }
+    }
+}
diff --git a/Tomboy/Makefile.am b/Tomboy/Makefile.am
index 16a0a46..28c14ef 100644
--- a/Tomboy/Makefile.am
+++ b/Tomboy/Makefile.am
@@ -121,6 +121,8 @@ CSFILES = 					\
 	$(srcdir)/Notebooks/*.cs		\
 	$(srcdir)/Synchronization/*.cs		\
 	\
+	$(srcdir)/Hyena/*.cs			\
+	\
 	$(GNOME_USED_CSFILES)			\
 	$(WIN_USED_CSFILES)			\
 	$(OSX_USED_CSFILES)			\
diff --git a/Tomboy/NativeApplication.cs b/Tomboy/NativeApplication.cs
index 13587aa..94d1845 100644
--- a/Tomboy/NativeApplication.cs
+++ b/Tomboy/NativeApplication.cs
@@ -18,7 +18,11 @@ namespace Tomboy
 		void Exit (int exitcode);
 		void StartMainLoop ();
 
-		string ConfDir { get; }
+		string CacheDirectory { get; }
+
+		string ConfigurationDirectory { get; }
+
+		string DataDirectory { get; }
 
 		void OpenUrl (string url, Gdk.Screen screen);
 
diff --git a/Tomboy/NoteManager.cs b/Tomboy/NoteManager.cs
index c4c6503..60788c8 100644
--- a/Tomboy/NoteManager.cs
+++ b/Tomboy/NoteManager.cs
@@ -77,7 +77,7 @@ public NoteManager (string directory) :
 
 		protected virtual AddinManager CreateAddinManager ()
 		{
-			string tomboy_conf_dir = Services.NativeApplication.ConfDir;
+			string tomboy_conf_dir = Services.NativeApplication.ConfigurationDirectory;
 
 			return new AddinManager (tomboy_conf_dir);
 		}
diff --git a/Tomboy/Synchronization/FileSystemSyncServer.cs b/Tomboy/Synchronization/FileSystemSyncServer.cs
index ed143e6..cf3d7ff 100644
--- a/Tomboy/Synchronization/FileSystemSyncServer.cs
+++ b/Tomboy/Synchronization/FileSystemSyncServer.cs
@@ -13,7 +13,7 @@ namespace Tomboy.Sync
 		private string serverId;
 
 		private string serverPath;
-		private string notePath;
+		private string cachePath;
 		private string lockPath;
 		private string manifestPath;
 
@@ -32,7 +32,7 @@ namespace Tomboy.Sync
 			if (!Directory.Exists (serverPath))
 				throw new DirectoryNotFoundException (serverPath);
 
-			notePath = Tomboy.DefaultNoteManager.NoteDirectoryPath;
+			cachePath = Services.NativeApplication.CacheDirectory;
 			lockPath = Path.Combine (serverPath, "lock");
 			manifestPath = Path.Combine (serverPath, "manifest.xml");
 
@@ -100,7 +100,7 @@ namespace Tomboy.Sync
 		{
 			Dictionary<string, NoteUpdate> noteUpdates = new Dictionary<string, NoteUpdate> ();
 
-			string tempPath = Path.Combine (notePath, "sync_temp");
+			string tempPath = Path.Combine (cachePath, "sync_temp");
 			if (!Directory.Exists (tempPath)) {
 				Directory.CreateDirectory (tempPath);
 			} else {
diff --git a/Tomboy/Synchronization/FuseSyncServiceAddin.cs b/Tomboy/Synchronization/FuseSyncServiceAddin.cs
index da09b0f..e285808 100644
--- a/Tomboy/Synchronization/FuseSyncServiceAddin.cs
+++ b/Tomboy/Synchronization/FuseSyncServiceAddin.cs
@@ -265,7 +265,7 @@ namespace Tomboy.Sync
 
 		private void SetUpMountPath ()
 		{
-			string notesPath = Tomboy.DefaultNoteManager.NoteDirectoryPath;
+			string notesPath = Services.NativeApplication.CacheDirectory;
 			mountPath = Path.Combine (notesPath, "sync-" + Id); // TODO: Best mount path name?
 		}
 
diff --git a/Tomboy/Synchronization/TomboySyncClient.cs b/Tomboy/Synchronization/TomboySyncClient.cs
index d85b46b..ed0632e 100644
--- a/Tomboy/Synchronization/TomboySyncClient.cs
+++ b/Tomboy/Synchronization/TomboySyncClient.cs
@@ -20,12 +20,12 @@ namespace Tomboy.Sync
 		{
 			// TODO: Why doesn't OnChanged ever get fired?!
 			FileSystemWatcher w = new FileSystemWatcher ();
-			w.Path = Tomboy.DefaultNoteManager.NoteDirectoryPath;
+			w.Path = Services.NativeApplication.CacheDirectory;
 			w.Filter = localManifestFileName;
 			w.Changed += OnChanged;
 
 			localManifestFilePath =
-			        Path.Combine (Tomboy.DefaultNoteManager.NoteDirectoryPath,
+			        Path.Combine (Services.NativeApplication.CacheDirectory,
 			                      localManifestFileName);
 			Parse (localManifestFilePath);
 
diff --git a/Tomboy/Tomboy.cs b/Tomboy/Tomboy.cs
index 97d6df9..ee039b7 100644
--- a/Tomboy/Tomboy.cs
+++ b/Tomboy/Tomboy.cs
@@ -124,7 +124,7 @@ namespace Tomboy
 				RegisterSessionManagerRestart (
 				        Environment.GetEnvironmentVariable ("TOMBOY_WRAPPER_PATH"),
 				        args,
-				        new string [] { "TOMBOY_PATH=" + note_path  });
+				        new string [] { "TOMBOY_PATH=" + note_path  }); // TODO: Pass along XDG_*?
 				StartTrayIcon ();
 			}
 
@@ -143,10 +143,10 @@ namespace Tomboy
 			        override_path :
 			        Environment.GetEnvironmentVariable ("TOMBOY_PATH");
 			if (note_path == null)
-				note_path = Services.NativeApplication.ConfDir;
+				note_path = Services.NativeApplication.DataDirectory;
 
 			// Tilde expand
-			return note_path.Replace ("~", Environment.GetEnvironmentVariable ("HOME"));
+			return note_path.Replace ("~", Environment.GetEnvironmentVariable ("HOME")); // TODO: Wasted work
 		}
 
 		static void RegisterPanelAppletFactory ()



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]