[gnome-panel] clock: Convert to GResource



commit aa2895d338c319f2cd22953937ec1991234fd9bf
Author: Vincent Untz <vuntz gnome org>
Date:   Thu Feb 2 14:04:27 2012 +0100

    clock: Convert to GResource

 applets/clock/Makefile.am         |   20 ++++++++------
 applets/clock/clock-face.c        |   23 ++++++++---------
 applets/clock/clock-map.c         |   35 ++++++++++++++++++-------
 applets/clock/clock-utils.c       |   50 +++++++++++++++++++++++++++++++++++++
 applets/clock/clock-utils.h       |    4 +++
 applets/clock/clock.c             |   22 +++-------------
 applets/clock/clock.gresource.xml |   18 +++++++++++++
 applets/clock/clock.h             |    1 +
 applets/clock/pixmaps/Makefile.am |    5 +--
 9 files changed, 126 insertions(+), 52 deletions(-)
---
diff --git a/applets/clock/Makefile.am b/applets/clock/Makefile.am
index 5fe5a0a..b2a1df8 100644
--- a/applets/clock/Makefile.am
+++ b/applets/clock/Makefile.am
@@ -52,9 +52,6 @@ CLOCK_CPPFLAGS =						\
 	-I$(srcdir)/../../libpanel-applet			\
 	-I$(top_builddir)/libpanel-applet			\
 	-DGNOMELOCALEDIR=\""$(prefix)/$(DATADIRNAME)/locale"\"	\
-	-DBUILDERDIR=\""$(uidir)"\"				\
-	-DCLOCK_MENU_UI_DIR=\""$(xmluidir)"\"			\
-	-DICONDIR=\""$(datadir)/gnome-panel/pixmaps"\"		\
 	-DCLOCK_EDS_ICONDIR="\"$(CLOCK_EDS_ICONDIR)\""		\
 	-DGWEATHER_I_KNOW_THIS_IS_UNSTABLE
 
@@ -133,6 +130,8 @@ clock-typebuiltins.h: $(clock_enum_headers)
 BUILT_SOURCES =					\
 	clock-marshallers.c			\
 	clock-marshallers.h			\
+	clock-resources.c			\
+	clock-resources.h			\
 	clock-typebuiltins.c			\
 	clock-typebuiltins.h
 
@@ -160,11 +159,15 @@ org.gnome.panel.applet.ClockAppletFactory.service: $(service_in_files)
 		$< > $@
 endif
 
-uidir		= $(datadir)/gnome-panel/ui
-ui_DATA		= clock.ui
+ui_FILES =		\
+	clock.ui	\
+	clock-menu.xml
 
-xmluidir 	= $(datadir)/gnome-panel/ui
-xmlui_DATA	= clock-menu.xml
+clock-resources.c: clock.gresource.xml $(shell $(GLIB_COMPILE_RESOURCES) --generate-dependencies clock.gresource.xml)
+	$(AM_V_GEN)$(GLIB_COMPILE_RESOURCES) --target=$@ --sourcedir=$(srcdir) --generate --c-name clock $(srcdir)/$<
+
+clock-resources.h: clock.gresource.xml $(shell $(GLIB_COMPILE_RESOURCES) --generate-dependencies clock.gresource.xml)
+	$(AM_V_GEN)$(GLIB_COMPILE_RESOURCES) --target=$@ --sourcedir=$(srcdir) --generate --c-name clock $(srcdir)/$<
 
 schemasdir       = $(GCONF_SCHEMA_FILE_DIR)
 schemas_in_files = clock.schemas.in
@@ -188,8 +191,7 @@ endif
 EXTRA_DIST =						\
 	org.gnome.panel.ClockApplet.panel-applet.in.in	\
 	$(schemas_in_files)				\
-	$(xmlui_DATA)					\
-	$(ui_DATA)					\
+	$(ui_FILES)					\
 	clock-marshallers.list				\
 	$(service_in_files)
 
diff --git a/applets/clock/clock-face.c b/applets/clock/clock-face.c
index a974634..7ffe1fe 100644
--- a/applets/clock/clock-face.c
+++ b/applets/clock/clock-face.c
@@ -15,10 +15,10 @@
 #include <math.h>
 #include <time.h>
 
-#include <librsvg/rsvg.h>
-
+#include "clock.h"
 #include "clock-face.h"
 #include "clock-location.h"
+#include "clock-utils.h"
 
 static GHashTable *pixbuf_cache = NULL;
 
@@ -434,21 +434,20 @@ clock_face_load_face (ClockFace *this, gint width, gint height)
         }
 
         /* The pixbuf is not cached, let's load it */
-	name = g_strconcat (ICONDIR, "/clock-face-", size_string[priv->size],
+	name = g_strconcat (CLOCK_RESOURCE_PATH "icons/",
+			    "clock-face-", size_string[priv->size],
                             "-", daytime_string[priv->timeofday], ".svg",
                             NULL);
-	priv->face_pixbuf = rsvg_pixbuf_from_file_at_size (name,
-                                                           width, height,
-                                                           NULL);
+	priv->face_pixbuf = clock_utils_pixbuf_from_svg_resource_at_size (name,
+									  width, height);
 	g_free (name);
 
 	if (!priv->face_pixbuf) {
-                name = g_strconcat (ICONDIR, "/clock-face-",
-                                    size_string[priv->size], ".svg", NULL);
-                priv->face_pixbuf = rsvg_pixbuf_from_file_at_size (name,
-                                                                   width,
-                                                                   height,
-                                                                   NULL);
+		name = g_strconcat (CLOCK_RESOURCE_PATH "icons/",
+				    "clock-face-", size_string[priv->size], ".svg",
+				    NULL);
+		priv->face_pixbuf = clock_utils_pixbuf_from_svg_resource_at_size (name,
+										  width, height);
                 g_free (name);
         }
 
diff --git a/applets/clock/clock-map.c b/applets/clock/clock-map.c
index b6de7de..5046e43 100644
--- a/applets/clock/clock-map.c
+++ b/applets/clock/clock-map.c
@@ -28,9 +28,9 @@ enum {
 };
 
 static char *marker_files[MARKER_NB] = {
-        ICONDIR "/clock-map-location-marker.png",
-        ICONDIR "/clock-map-location-hilight.png",
-        ICONDIR "/clock-map-location-current.png"
+        "clock-map-location-marker.png",
+        "clock-map-location-hilight.png",
+        "clock-map-location-current.png"
 };
 
 static guint signals[LAST_SIGNAL];
@@ -136,8 +136,17 @@ clock_map_init (ClockMap *this)
         g_assert (sizeof (marker_files)/sizeof (char *) == MARKER_NB);
 
         for (i = 0; i < MARKER_NB; i++) {
-                priv->location_marker_pixbuf[i] = gdk_pixbuf_new_from_file
-                                                  (marker_files[i], NULL);
+                char *resource;
+                GInputStream *stream;
+
+                resource = g_strconcat (CLOCK_RESOURCE_PATH "icons/", marker_files[i], NULL);
+                stream = g_resources_open_stream (resource, 0, NULL);
+                g_free (resource);
+
+                if (stream != NULL) {
+                        priv->location_marker_pixbuf[i] = gdk_pixbuf_new_from_stream (stream, NULL, NULL);
+                        g_object_unref (stream);
+                }
         }
 }
 
@@ -209,11 +218,17 @@ clock_map_refresh (ClockMap *this)
         }
 
         if (!priv->stock_map_pixbuf) {
-                GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file_at_scale
-                        (ICONDIR "/clock-map.png",
-                         priv->width, priv->height, FALSE, NULL);
-
-                priv->stock_map_pixbuf = pixbuf;
+                GInputStream *stream = g_resources_open_stream (CLOCK_RESOURCE_PATH "icons/clock-map.png",
+                                                                0, NULL);
+                if (stream != NULL) {
+                        GdkPixbuf *pixbuf = gdk_pixbuf_new_from_stream_at_scale (stream,
+                                                                                 priv->width, priv->height,
+                                                                                 FALSE,
+                                                                                 NULL, NULL);
+                        g_object_unref (stream);
+
+                        priv->stock_map_pixbuf = pixbuf;
+                }
         }
 
         clock_map_place_locations (this);
diff --git a/applets/clock/clock-utils.c b/applets/clock/clock-utils.c
index c9581e0..4292be2 100644
--- a/applets/clock/clock-utils.c
+++ b/applets/clock/clock-utils.c
@@ -34,6 +34,9 @@
 
 #include <gtk/gtk.h>
 
+#include <librsvg/rsvg.h>
+#include <librsvg/rsvg-cairo.h>
+
 #include "clock.h"
 
 #include "clock-utils.h"
@@ -125,3 +128,50 @@ clock_utils_display_help (GtkWidget  *widget,
 		gtk_widget_show (dialog);
 	}
 }
+
+GdkPixbuf *
+clock_utils_pixbuf_from_svg_resource_at_size (const char *resource,
+					      int         width,
+					      int         height)
+{
+	GInputStream      *stream = NULL;
+	RsvgHandle        *handle = NULL;
+	RsvgDimensionData  svg_dimensions;
+	GdkPixbuf         *pixbuf = NULL;
+	cairo_surface_t   *surface = NULL;
+	cairo_matrix_t     matrix;
+	cairo_t           *cr = NULL;
+
+	stream = g_resources_open_stream (resource, 0, NULL);
+	if (!stream)
+		goto out;
+
+	handle = rsvg_handle_new ();
+	if (!handle)
+		goto out;
+
+	if (!rsvg_handle_read_stream_sync (handle, stream, NULL, NULL))
+		goto out;
+
+	rsvg_handle_get_dimensions (handle, &svg_dimensions);
+
+	surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
+	cr = cairo_create (surface);
+	cairo_matrix_init_scale (&matrix,
+				 ((double) width / svg_dimensions.width),
+				 ((double) height / svg_dimensions.height));
+	cairo_transform (cr, &matrix);
+	rsvg_handle_render_cairo (handle, cr);
+	cairo_destroy (cr);
+
+	pixbuf = gdk_pixbuf_get_from_surface (surface, 0, 0, width, height);
+	cairo_surface_destroy (surface);
+
+out:
+	if (handle)
+		rsvg_handle_close (handle, NULL);
+	if (stream)
+		g_object_unref (stream);
+
+	return pixbuf;
+}
diff --git a/applets/clock/clock-utils.h b/applets/clock/clock-utils.h
index 831f488..fb3bea0 100644
--- a/applets/clock/clock-utils.h
+++ b/applets/clock/clock-utils.h
@@ -48,6 +48,10 @@ void clock_utils_display_help (GtkWidget  *widget,
 			       const char *doc_id,
 			       const char *link_id);
 
+GdkPixbuf *clock_utils_pixbuf_from_svg_resource_at_size (const char *resource,
+							 int         width,
+							 int         height);
+
 G_END_DECLS
 
 #endif /* __CLOCK_UTILS_H__ */
diff --git a/applets/clock/clock.c b/applets/clock/clock.c
index d0922da..eacc336 100644
--- a/applets/clock/clock.c
+++ b/applets/clock/clock.c
@@ -2432,9 +2432,6 @@ fill_clock_applet (PanelApplet *applet)
 	ClockData      *cd;
         GtkActionGroup *action_group;
         GtkAction      *action;
-        gchar          *ui_path;
-        char           *filename;
-	GError         *error;
 
 	panel_applet_add_preferences (applet, CLOCK_SCHEMA_DIR, NULL);
 	panel_applet_set_flags (applet, PANEL_APPLET_EXPAND_MINOR);
@@ -2450,17 +2447,7 @@ fill_clock_applet (PanelApplet *applet)
 
 	cd->builder = gtk_builder_new ();
 	gtk_builder_set_translation_domain (cd->builder, GETTEXT_PACKAGE);
-        filename = g_build_filename (BUILDERDIR, "clock.ui", NULL);
-
-	error = NULL;
-	gtk_builder_add_from_file (cd->builder, filename, &error);
-        if (error) {
-		g_warning ("Error loading \"%s\": %s",
-			   filename, error->message);
-		g_error_free (error);
-	}
-
-        g_free (filename);
+	gtk_builder_add_from_resource (cd->builder, CLOCK_RESOURCE_PATH "clock.ui", NULL);
 
 	create_clock_widget (cd);
 
@@ -2492,10 +2479,9 @@ fill_clock_applet (PanelApplet *applet)
                                       clock_menu_actions,
                                       G_N_ELEMENTS (clock_menu_actions),
                                       cd);
-        ui_path = g_build_filename (CLOCK_MENU_UI_DIR, "clock-menu.xml", NULL);
-	panel_applet_setup_menu_from_file (PANEL_APPLET (cd->applet),
-					   ui_path, action_group);
-        g_free (ui_path);
+	panel_applet_setup_menu_from_resource (PANEL_APPLET (cd->applet),
+					       CLOCK_RESOURCE_PATH "clock-menu.xml",
+					       action_group);
 
 	action = gtk_action_group_get_action (action_group, "ClockPreferences");
 	g_object_bind_property (cd->applet, "locked-down",
diff --git a/applets/clock/clock.gresource.xml b/applets/clock/clock.gresource.xml
new file mode 100644
index 0000000..e457626
--- /dev/null
+++ b/applets/clock/clock.gresource.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+  <gresource prefix="/org/gnome/panel/applet/clock">
+    <file compressed="true">clock.ui</file>
+    <file compressed="true">clock-menu.xml</file>
+    <file alias="icons/clock-calendar-icon.png">pixmaps/clock-calendar-icon.png</file>
+    <file alias="icons/clock-face-large.svg">pixmaps/clock-face-large.svg</file>
+    <file alias="icons/clock-face-small.svg">pixmaps/clock-face-small.svg</file>
+    <file alias="icons/clock-face-small-morning.svg">pixmaps/clock-face-small-morning.svg</file>
+    <file alias="icons/clock-face-small-day.svg">pixmaps/clock-face-small-day.svg</file>
+    <file alias="icons/clock-face-small-evening.svg">pixmaps/clock-face-small-evening.svg</file>
+    <file alias="icons/clock-face-small-night.svg">pixmaps/clock-face-small-night.svg</file>
+    <file alias="icons/clock-map.png">pixmaps/clock-map.png</file>
+    <file alias="icons/clock-map-location-marker.png">pixmaps/clock-map-location-marker.png</file>
+    <file alias="icons/clock-map-location-current.png">pixmaps/clock-map-location-current.png</file>
+    <file alias="icons/clock-map-location-hilight.png">pixmaps/clock-map-location-hilight.png</file>
+  </gresource>
+</gresources>
diff --git a/applets/clock/clock.h b/applets/clock/clock.h
index 4aecea5..4cd0a12 100644
--- a/applets/clock/clock.h
+++ b/applets/clock/clock.h
@@ -30,6 +30,7 @@
 G_BEGIN_DECLS
 
 #define CLOCK_ICON "gnome-panel-clock"
+#define CLOCK_RESOURCE_PATH "/org/gnome/panel/applet/clock/"
 #define CLOCK_SCHEMA_DIR "/schemas/apps/clock_applet/prefs"
 
 G_END_DECLS
diff --git a/applets/clock/pixmaps/Makefile.am b/applets/clock/pixmaps/Makefile.am
index 26ce476..edf8b2f 100644
--- a/applets/clock/pixmaps/Makefile.am
+++ b/applets/clock/pixmaps/Makefile.am
@@ -1,5 +1,4 @@
-icondir = $(datadir)/gnome-panel/pixmaps
-icon_DATA =				\
+icon_FILES =				\
 	clock-calendar-icon.png		\
 	clock-face-large.svg		\
 	clock-face-small.svg		\
@@ -14,7 +13,7 @@ icon_DATA =				\
 
 EXTRA_DIST = 		\
 	clock-map.svg	\
-	$(icon_DATA)
+	$(icon_FILES)
 
 MAINTAINERCLEANFILES = Makefile.in
 



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