[alacarte] util: Use GLib for xdg basedir spec stuff



commit 1abad9caa4f50321cdb499e71e2b1e34e7c33e8e
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Tue May 1 17:19:24 2012 -0400

    util: Use GLib for xdg basedir spec stuff

 Alacarte/MenuEditor.py |    4 +-
 Alacarte/util.py       |   79 ++++++++++++++---------------------------------
 2 files changed, 26 insertions(+), 57 deletions(-)
---
diff --git a/Alacarte/MenuEditor.py b/Alacarte/MenuEditor.py
index fd13ac5..ddba8c0 100644
--- a/Alacarte/MenuEditor.py
+++ b/Alacarte/MenuEditor.py
@@ -217,7 +217,7 @@ class MenuEditor(object):
 
     def canRevert(self, item):
         if isinstance(item, GMenu.TreeEntry):
-            if util.getItemPath(item.get_desktop_file_id()):
+            if util.getItemPath(item.get_desktop_file_id()) is not None:
                 path = util.getUserItemPath()
                 if os.path.isfile(os.path.join(path, item.get_desktop_file_id())):
                     return True
@@ -226,7 +226,7 @@ class MenuEditor(object):
                 file_id = os.path.split(item.get_desktop_file_path())[1]
             else:
                 file_id = item.get_menu_id() + '.directory'
-            if util.getDirectoryPath(file_id):
+            if util.getDirectoryPath(file_id) is not None:
                 path = util.getUserDirectoryPath()
                 if os.path.isfile(os.path.join(path, file_id)):
                     return True
diff --git a/Alacarte/util.py b/Alacarte/util.py
index c491c4d..de6831f 100644
--- a/Alacarte/util.py
+++ b/Alacarte/util.py
@@ -80,75 +80,44 @@ def getUniqueUndoFile(filepath):
             append += 1
     return new_filepath
 
-def getUserMenuPath():
-    menu_dir = None
-    if os.environ.has_key('XDG_CONFIG_HOME'):
-        menu_dir = os.path.join(os.environ['XDG_CONFIG_HOME'], 'menus')
-    else:
-        menu_dir = os.path.join(os.environ['HOME'], '.config', 'menus')
-    #move .config out of the way if it's not a dir, it shouldn't be there
-    if os.path.isfile(os.path.split(menu_dir)[0]):
-        os.rename(os.path.split(menu_dir)[0], os.path.split(menu_dir)[0] + '.old')
-    if not os.path.isdir(menu_dir):
-        os.makedirs(menu_dir)
-    return menu_dir
-
 def getItemPath(file_id):
-    if os.environ.has_key('XDG_DATA_DIRS'):
-        for system_path in os.environ['XDG_DATA_DIRS'].split(':'):
-            file_path = os.path.join(system_path, 'applications', file_id)
-            if os.path.isfile(file_path):
-                return file_path
-    file_path = os.path.join('/', 'usr', 'share', 'applications', file_id)
-    if os.path.isfile(file_path):
-        return file_path
-    return False
+    for path in GLib.get_system_data_dirs():
+        file_path = os.path.join(path, 'applications', file_id)
+        if os.path.isfile(file_path):
+            return file_path
+    return None
 
 def getUserItemPath():
-    item_dir = None
-    if os.environ.has_key('XDG_DATA_HOME'):
-        item_dir = os.path.join(os.environ['XDG_DATA_HOME'], 'applications')
-    else:
-        item_dir = os.path.join(os.environ['HOME'], '.local', 'share', 'applications')
+    item_dir = os.path.join(GLib.get_user_data_dir(), 'applications')
     if not os.path.isdir(item_dir):
         os.makedirs(item_dir)
     return item_dir
 
 def getDirectoryPath(file_id):
-    home = getUserDirectoryPath()
-    file_path = os.path.join(home, file_id)
-    if os.path.isfile(file_path):
-        return file_path
-    if os.environ.has_key('XDG_DATA_DIRS'):
-        for system_path in os.environ['XDG_DATA_DIRS'].split(':'):
-            file_path = os.path.join(system_path, 'desktop-directories', file_id)
-            if os.path.isfile(file_path):
-                return file_path
-    file_path = os.path.join('/', 'usr', 'share', 'desktop-directories', file_id)
-    if os.path.isfile(file_path):
-        return file_path
-    return False
+    for path in GLib.get_system_data_dirs():
+        file_path = os.path.join(path, 'desktop-directories', file_id)
+        if os.path.isfile(file_path):
+            return file_path
+    return None
 
 def getUserDirectoryPath():
-    menu_dir = None
-    if os.environ.has_key('XDG_DATA_HOME'):
-        menu_dir = os.path.join(os.environ['XDG_DATA_HOME'], 'desktop-directories')
-    else:
-        menu_dir = os.path.join(os.environ['HOME'], '.local', 'share', 'desktop-directories')
+    menu_dir = os.path.join(GLib.get_user_data_dir(), 'desktop-directories')
+    if not os.path.isdir(menu_dir):
+        os.makedirs(menu_dir)
+    return menu_dir
+
+def getUserMenuPath():
+    menu_dir = os.path.join(GLib.get_user_config_dir(), 'menus')
     if not os.path.isdir(menu_dir):
         os.makedirs(menu_dir)
     return menu_dir
 
-def getSystemMenuPath(file_name):
-    if os.environ.has_key('XDG_CONFIG_DIRS'):
-        for system_path in os.environ['XDG_CONFIG_DIRS'].split(':'):
-            file_path = os.path.join(system_path, 'menus', file_name)
-            if os.path.isfile(file_path):
-                return file_path
-    file_path = os.path.join('/', 'etc', 'xdg', 'menus', file_name)
-    if os.path.isfile(file_path):
-        return file_path
-    return False
+def getSystemMenuPath(file_id):
+    for path in GLib.get_system_config_dirs():
+        file_path = os.path.join(path, 'menus', file_id)
+        if os.path.isfile(file_path):
+            return file_path
+    return None
 
 def getUserMenuXml(tree):
     system_file = getSystemMenuPath(os.path.basename(tree.get_canonical_menu_path()))



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