[gnome-clocks] Load css and image files from the correct dir



commit 6d44834e86ee5d11ce6c70f776944f7b43e57997
Author: Paolo Borelli <pborelli gnome org>
Date:   Tue Aug 14 23:21:57 2012 +0200

    Load css and image files from the correct dir
    
    Detect at runtime if we are running from an installed setup or from the
    source dir and load the files from the appropriate directory.
    The directories are specified with the GNOME_CLOCKS_DATA_PATH and
    GNOME_CLOCKS_IMAGE_PATH environment variables.
    In the future it may make sense to add also HELP_DIR and LOCALE_DIR.
    
    To be honest I am not thrilled by this approach, but it seems like the
    de facto standard (d-feet, jokosher, etc do the same)
    
    The new utils.py file contains a Dirs class that makes accessing this
    values easier.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=681883

 gnome-clocks           |   26 +++++++++++++++++++++-----
 gnomeclocks/utils.py   |   39 +++++++++++++++++++++++++++++++++++++++
 gnomeclocks/widgets.py |   10 ++++++----
 3 files changed, 66 insertions(+), 9 deletions(-)
---
diff --git a/gnome-clocks b/gnome-clocks
index 9f4f7f5..35f62cf 100755
--- a/gnome-clocks
+++ b/gnome-clocks
@@ -21,10 +21,27 @@
  Author: Seif Lotfy <seif lotfy collabora co uk>
 """
 
+import os
+import sys
+
+# Detect if we are running from the source dir
+if os.path.exists("gnome-clocks.doap"):
+    ENV_PATHS = {
+        "GNOME_CLOCKS_DATA_PATH": "data/",
+        "GNOME_CLOCKS_IMAGE_PATH": "data/",
+    }
+else:
+    ENV_PATHS = {
+        "GNOME_CLOCKS_DATA_PATH": "/usr/share/gnome-clocks/",
+        "GNOME_CLOCKS_IMAGE_PATH": "/usr/share/gnome-clocks/pixmaps/",
+    }
+
+for var, path in ENV_PATHS.iteritems():
+    os.environ.setdefault(var, path)
+
 from gi.repository import Gtk, Gio, GObject, Gdk
 from gnomeclocks.clocks import World, Alarm, Timer, Stopwatch
-
-import sys
+from gnomeclocks.utils import Dirs
 
 class Window (Gtk.ApplicationWindow):
     def __init__ (self, app):
@@ -33,9 +50,8 @@ class Window (Gtk.ApplicationWindow):
         self.set_wmclass("Clocks", "Clocks")
 
         css_provider = Gtk.CssProvider()
-        css_provider.load_from_path("data/gtk-style.css")
-        #self.set_hide_titlebar_when_maximized (True)        
-        self.set_icon_from_file ('data/preferences-system-time.png')
+        css_provider.load_from_path(os.path.join (Dirs.get_data_dir (), "gtk-style.css"))
+        self.set_icon_from_file (os.path.join (Dirs.get_image_dir (), 'preferences-system-time.png'))
         context = Gtk.StyleContext()
         context.add_provider_for_screen (Gdk.Screen.get_default (),
                                          css_provider,
diff --git a/gnomeclocks/utils.py b/gnomeclocks/utils.py
new file mode 100644
index 0000000..ef92232
--- /dev/null
+++ b/gnomeclocks/utils.py
@@ -0,0 +1,39 @@
+"""
+ Copyright (c) 2011-2012 Collabora, Ltd.
+
+ Gnome Clocks 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 (at your
+ option) any later version.
+
+ Gnome Clocks is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ for more details.
+
+ You should have received a copy of the GNU General Public License along
+ with Gnome Documents; if not, write to the Free Software Foundation,
+ Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+ Author: Seif Lotfy <seif lotfy collabora co uk>
+"""
+
+import os
+
+
+class Dirs:
+    @staticmethod
+    def get_data_dir():
+        try:
+            path = os.environ['GNOME_CLOCKS_DATA_PATH']
+        except:
+            path = "../data"
+        return path
+
+    @staticmethod
+    def get_image_dir():
+        try:
+            path = os.environ['GNOME_CLOCKS_IMAGE_PATH']
+        except:
+            path = "../data"
+        return path
diff --git a/gnomeclocks/widgets.py b/gnomeclocks/widgets.py
index 39d5893..dcd3380 100644
--- a/gnomeclocks/widgets.py
+++ b/gnomeclocks/widgets.py
@@ -21,6 +21,8 @@
 from gi.repository import Gtk, Gdk, GdkPixbuf, GObject, Gio, PangoCairo, Pango, GWeather
 from storage import Location
 from alarm import AlarmItem
+from utils import Dirs
+import os
 import cairo, time
 
 
@@ -138,9 +140,9 @@ class DigitalClock ():
     def get_image(self):
         local_time = self.get_local_time ()
         if local_time.tm_hour > 7 and local_time.tm_hour < 19:
-            return "data/cities/day.png"
+            return os.path.join (Dirs.get_image_dir (), "cities", "day.png")
         else:
-            return "data/cities/night.png"
+            return os.path.join (Dirs.get_image_dir (), "cities", "night.png")
 
     def get_is_day(self):
         local_time = self.get_local_time ()
@@ -321,9 +323,9 @@ class AlarmWidget():
         t = time_given
         isDay = self.get_is_day(t)
         if isDay == True:
-            img = "data/cities/day.png"
+            img = os.path.join (Dirs.get_image_dir (), "cities", "day.png")
         else:
-            img = "data/cities/night.png"
+            img = os.path.join (Dirs.get_image_dir (), "cities", "night.png")
         self.drawing.render(t, img, isDay)
 
     def get_system_clock_format(self):



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