[gnome-applets/wip/muktupavels/per-applet-notes-file: 23/24] sticky-notes: cleanup stickynotes_load




commit 4fccd7b83dbd15cd6a8cd83cba246a1d4732b17e
Author: Sebastian Geiger <sbastig gmx net>
Date:   Mon Apr 13 16:44:04 2020 +0200

    sticky-notes: cleanup stickynotes_load
    
    This commit extracts the get_notes_file and migrate_legacy_note_file
    functions to make stickynotes_load more readable.

 gnome-applets/sticky-notes/sticky-notes.c | 153 +++++++++++++++++++-----------
 1 file changed, 96 insertions(+), 57 deletions(-)
---
diff --git a/gnome-applets/sticky-notes/sticky-notes.c b/gnome-applets/sticky-notes/sticky-notes.c
index 70f1710f7..c47084db5 100644
--- a/gnome-applets/sticky-notes/sticky-notes.c
+++ b/gnome-applets/sticky-notes/sticky-notes.c
@@ -1303,12 +1303,102 @@ get_unique_filename (void)
   return filename;
 }
 
+static char *
+get_notes_file (StickyNotesApplet *applet)
+{
+  const char *dir;
+  char *filename;
+  char *notes_file;
+
+  dir = g_get_user_config_dir ();
+  filename = g_settings_get_string (applet->settings, KEY_FILENAME);
+
+  g_free (applet->filename);
+
+  if (*filename == '\0')
+    {
+      g_free (filename);
+      filename = get_unique_filename ();
+
+      notes_file = g_build_filename (dir,
+                                     "gnome-applets",
+                                     "sticky-notes",
+                                     filename,
+                                     NULL);
+
+
+
+      applet->filename = filename;
+      g_settings_set_string (applet->settings, KEY_FILENAME, filename);
+    }
+  else
+    {
+      applet->filename = filename;
+      notes_file = g_build_filename (dir,
+                                     "gnome-applets",
+                                     "sticky-notes",
+                                     filename,
+                                     NULL);
+    }
+
+  return notes_file;
+}
+
+/**
+ * The sticky-notes file had various locations in past versions of the applet.
+ *
+ * Until version 3.24 the location was: `~/.config/gnome-applets/stickynotes`.
+ * In version 3.26 the location changed to: `<XDG_CONFIG_HOME>/gnome-applets/sticky-notes/sticky-notes.xml`.
+ * In version 3.38 the location changed to: 
`<XDG_CONFIG_HOME>/gnome-applets/sticky-notes/sticky-notes-%d.xml`
+ * where `%d` is incremented for each notes applet on the panel.
+ */
+static void
+migrate_legacy_note_file (StickyNotesApplet *applet,
+                          const char        *notes_file)
+{
+  const char *dir;
+  char *old_file;
+
+  dir = g_get_user_config_dir ();
+
+  old_file = g_build_filename (dir,
+                               "gnome-applets",
+                               "sticky-notes",
+                               "sticky-notes.xml",
+                               NULL);
+
+  if (g_file_test (old_file, G_FILE_TEST_EXISTS))
+    {
+      g_rename (old_file, notes_file);
+
+      g_free (old_file);
+      old_file = g_build_filename (dir,
+                                   "gnome-applets",
+                                   "stickynotes",
+                                   NULL);
+
+      if (g_file_test (old_file, G_FILE_TEST_EXISTS))
+        g_unlink (old_file);
+    }
+  else
+    {
+      g_free (old_file);
+      old_file = g_build_filename (dir,
+                                   "gnome-applets",
+                                   "stickynotes",
+                                   NULL);
+
+      if (g_file_test (old_file, G_FILE_TEST_EXISTS))
+        g_rename (old_file, notes_file);
+    }
+
+  g_free (old_file);
+}
+
 /* Load all sticky notes from an XML configuration file */
 void
 stickynotes_load (StickyNotesApplet *applet)
 {
-       const char *dir;
-       char *filename;
        char *notes_file;
        xmlDocPtr doc;
        xmlNodePtr root;
@@ -1318,62 +1408,11 @@ stickynotes_load (StickyNotesApplet *applet)
        GList *new_nodes; /* Lists of xmlNodePtr's */
        int x, y, w, h;
 
-       dir = g_get_user_config_dir ();
-       filename = g_settings_get_string (applet->settings, KEY_FILENAME);
-       g_free (applet->filename);
-
-       if (*filename == '\0') {
-               char *old_file;
-
-               g_free (filename);
-               filename = get_unique_filename ();
-
-               notes_file = g_build_filename (dir,
-                                              "gnome-applets",
-                                              "sticky-notes",
-                                              filename,
-                                              NULL);
-
-               old_file = g_build_filename (dir,
-                                            "gnome-applets",
-                                            "sticky-notes",
-                                            "sticky-notes.xml",
-                                            NULL);
-
-               if (g_file_test (old_file, G_FILE_TEST_EXISTS)) {
-                       g_rename (old_file, notes_file);
-
-                       g_free (old_file);
-                       old_file = g_build_filename (dir,
-                                                    "gnome-applets",
-                                                    "stickynotes",
-                                                    NULL);
-
-                       if (g_file_test (old_file, G_FILE_TEST_EXISTS))
-                               g_unlink (old_file);
-               } else {
-                       g_free (old_file);
-                       old_file = g_build_filename (dir,
-                                                    "gnome-applets",
-                                                    "stickynotes",
-                                                    NULL);
-
-                       if (g_file_test (old_file, G_FILE_TEST_EXISTS))
-                               g_rename (old_file, notes_file);
-               }
+  notes_file = get_notes_file (applet);
 
-               applet->filename = filename;
-               g_settings_set_string (applet->settings, KEY_FILENAME, filename);
-
-               g_free (old_file);
-       } else {
-               applet->filename = filename;
-               notes_file = g_build_filename (dir,
-                                              "gnome-applets",
-                                              "sticky-notes",
-                                              filename,
-                                              NULL);
-       }
+  if (!g_file_test (notes_file, G_FILE_TEST_EXISTS)) {
+    migrate_legacy_note_file (applet, notes_file);
+  }
 
        if (!g_file_test (notes_file, G_FILE_TEST_EXISTS)) {
                g_free (notes_file);


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