[Xfce4-commits] [apps/xfdashboard] 03/04: Set up theme object basically to have it in a basic operational state and can be used when "theme-changing" signal is emitted.

noreply at xfce.org noreply at xfce.org
Thu Jun 30 10:03:31 CEST 2016


This is an automated email from the git hooks/post-receive script.

nomad pushed a commit to branch master
in repository apps/xfdashboard.

commit 0adfc89f46fad18204444aef357fb5f5453e6145
Author: Stephan Haller <nomad at froevel.de>
Date:   Thu Jun 30 09:56:31 2016 +0200

    Set up theme object basically to have it in a basic operational state and can be used when "theme-changing" signal is emitted.
---
 libxfdashboard/application.c |   4 +-
 libxfdashboard/theme.c       | 285 +++++++++++++++++++++----------------------
 libxfdashboard/theme.h       |   8 +-
 3 files changed, 144 insertions(+), 153 deletions(-)

diff --git a/libxfdashboard/application.c b/libxfdashboard/application.c
index 839ce9b..a2bfc50 100644
--- a/libxfdashboard/application.c
+++ b/libxfdashboard/application.c
@@ -285,13 +285,13 @@ static void _xfdashboard_application_set_theme_name(XfdashboardApplication *self
 	if(g_strcmp0(priv->themeName, inThemeName)!=0)
 	{
 		/* Create new theme instance */
-		theme=xfdashboard_theme_new();
+		theme=xfdashboard_theme_new(inThemeName);
 
 		/* Emit signal that theme is going to be loaded and changed */
 		g_signal_emit(self, XfdashboardApplicationSignals[SIGNAL_THEME_CHANGING], 0, theme);
 
 		/* Load theme */
-		if(!xfdashboard_theme_load(theme, inThemeName, &error))
+		if(!xfdashboard_theme_load(theme, &error))
 		{
 			/* Show critical warning at console */
 			g_critical(_("Could not load theme '%s': %s"),
diff --git a/libxfdashboard/theme.c b/libxfdashboard/theme.c
index 3f8160c..d84467f 100644
--- a/libxfdashboard/theme.c
+++ b/libxfdashboard/theme.c
@@ -54,6 +54,8 @@ struct _XfdashboardThemePrivate
 	gchar						*themeComment;
 
 	/* Instance related */
+	gboolean					loaded;
+
 	XfdashboardThemeCSS			*styling;
 	XfdashboardThemeLayout		*layout;
 	XfdashboardThemeEffects		*effects;
@@ -80,66 +82,8 @@ static GParamSpec* XfdashboardThemeProperties[PROP_LAST]={ 0, };
 #define XFDASHBOARD_THEME_FILE							"xfdashboard.theme"
 #define XFDASHBOARD_THEME_GROUP							"Xfdashboard Theme"
 
-/* Release allocated resources in theme instance */
-static void _xfdashboard_theme_clean(XfdashboardTheme *self)
-{
-	XfdashboardThemePrivate		*priv;
-
-	g_return_if_fail(XFDASHBOARD_IS_THEME(self));
-
-	priv=self->priv;
-
-	/* Release allocated resources */
-	if(priv->themeName)
-	{
-		g_free(priv->themeName);
-		priv->themeName=NULL;
-		g_object_notify_by_pspec(G_OBJECT(self), XfdashboardThemeProperties[PROP_NAME]);
-	}
-
-	if(priv->themePath)
-	{
-		g_free(priv->themePath);
-		priv->themePath=NULL;
-		g_object_notify_by_pspec(G_OBJECT(self), XfdashboardThemeProperties[PROP_PATH]);
-	}
-
-	if(priv->themeDisplayName)
-	{
-		g_free(priv->themeDisplayName);
-		priv->themeDisplayName=NULL;
-		g_object_notify_by_pspec(G_OBJECT(self), XfdashboardThemeProperties[PROP_DISPLAY_NAME]);
-	}
-
-	if(priv->themeComment)
-	{
-		g_free(priv->themeComment);
-		priv->themeComment=NULL;
-		g_object_notify_by_pspec(G_OBJECT(self), XfdashboardThemeProperties[PROP_COMMENT]);
-	}
-
-	if(priv->styling)
-	{
-		g_object_unref(priv->styling);
-		priv->styling=NULL;
-	}
-
-	if(priv->layout)
-	{
-		g_object_unref(priv->layout);
-		priv->layout=NULL;
-	}
-
-	if(priv->effects)
-	{
-		g_object_unref(priv->effects);
-		priv->effects=NULL;
-	}
-}
-
 /* Load theme file and all listed resources in this file */
 static gboolean _xfdashboard_theme_load_resources(XfdashboardTheme *self,
-													const gchar *inThemePath,
 													GError **outError)
 {
 	XfdashboardThemePrivate		*priv;
@@ -151,14 +95,27 @@ static gboolean _xfdashboard_theme_load_resources(XfdashboardTheme *self,
 	gint						counter;
 
 	g_return_val_if_fail(XFDASHBOARD_IS_THEME(self), FALSE);
-	g_return_val_if_fail(inThemePath!=NULL && *inThemePath!=0, FALSE);
 	g_return_val_if_fail(outError==NULL || *outError==NULL, FALSE);
 
 	priv=self->priv;
 	error=NULL;
 
+	/* Check that theme was found */
+	if(!priv->themePath)
+	{
+		/* Set error */
+		g_set_error(outError,
+					XFDASHBOARD_THEME_ERROR,
+					XFDASHBOARD_THEME_ERROR_THEME_NOT_FOUND,
+					_("Theme '%s' not found"),
+					priv->themeName);
+
+		/* Return FALSE to indicate error */
+		return(FALSE);
+	}
+
 	/* Load theme file */
-	themeFile=g_build_filename(inThemePath, XFDASHBOARD_THEME_FILE, NULL);
+	themeFile=g_build_filename(priv->themePath, XFDASHBOARD_THEME_FILE, NULL);
 
 	themeKeyFile=g_key_file_new();
 	if(!g_key_file_load_from_file(themeKeyFile,
@@ -170,8 +127,6 @@ static gboolean _xfdashboard_theme_load_resources(XfdashboardTheme *self,
 		g_propagate_error(outError, error);
 
 		/* Release allocated resources */
-		_xfdashboard_theme_clean(self);
-
 		if(themeFile) g_free(themeFile);
 		if(themeKeyFile) g_key_file_free(themeKeyFile);
 
@@ -195,8 +150,6 @@ static gboolean _xfdashboard_theme_load_resources(XfdashboardTheme *self,
 		g_propagate_error(outError, error);
 
 		/* Release allocated resources */
-		_xfdashboard_theme_clean(self);
-
 		if(themeKeyFile) g_key_file_free(themeKeyFile);
 
 		/* Return FALSE to indicate error */
@@ -217,8 +170,6 @@ static gboolean _xfdashboard_theme_load_resources(XfdashboardTheme *self,
 		g_propagate_error(outError, error);
 
 		/* Release allocated resources */
-		_xfdashboard_theme_clean(self);
-
 		if(themeKeyFile) g_key_file_free(themeKeyFile);
 
 		/* Return FALSE to indicate error */
@@ -237,21 +188,18 @@ static gboolean _xfdashboard_theme_load_resources(XfdashboardTheme *self,
 		g_propagate_error(outError, error);
 
 		/* Release allocated resources */
-		_xfdashboard_theme_clean(self);
-
 		if(themeKeyFile) g_key_file_free(themeKeyFile);
 
 		/* Return FALSE to indicate error */
 		return(FALSE);
 	}
 
-	priv->styling=xfdashboard_theme_css_new(inThemePath);
 	counter=0;
 	resource=resources;
 	while(*resource)
 	{
 		/* Get path and file for style resource */
-		resourceFile=g_build_filename(inThemePath, *resource, NULL);
+		resourceFile=g_build_filename(priv->themePath, *resource, NULL);
 
 		/* Try to load style resource */
 		if(!xfdashboard_theme_css_add_file(priv->styling, resourceFile, counter, &error))
@@ -260,8 +208,6 @@ static gboolean _xfdashboard_theme_load_resources(XfdashboardTheme *self,
 			g_propagate_error(outError, error);
 
 			/* Release allocated resources */
-			_xfdashboard_theme_clean(self);
-
 			if(resources) g_strfreev(resources);
 			if(resourceFile) g_free(resourceFile);
 			if(themeKeyFile) g_key_file_free(themeKeyFile);
@@ -291,20 +237,17 @@ static gboolean _xfdashboard_theme_load_resources(XfdashboardTheme *self,
 		g_propagate_error(outError, error);
 
 		/* Release allocated resources */
-		_xfdashboard_theme_clean(self);
-
 		if(themeKeyFile) g_key_file_free(themeKeyFile);
 
 		/* Return FALSE to indicate error */
 		return(FALSE);
 	}
 
-	priv->layout=xfdashboard_theme_layout_new();
 	resource=resources;
 	while(*resource)
 	{
 		/* Get path and file for style resource */
-		resourceFile=g_build_filename(inThemePath, *resource, NULL);
+		resourceFile=g_build_filename(priv->themePath, *resource, NULL);
 
 		/* Try to load style resource */
 		if(!xfdashboard_theme_layout_add_file(priv->layout, resourceFile, &error))
@@ -313,8 +256,6 @@ static gboolean _xfdashboard_theme_load_resources(XfdashboardTheme *self,
 			g_propagate_error(outError, error);
 
 			/* Release allocated resources */
-			_xfdashboard_theme_clean(self);
-
 			if(resources) g_strfreev(resources);
 			if(resourceFile) g_free(resourceFile);
 			if(themeKeyFile) g_key_file_free(themeKeyFile);
@@ -349,20 +290,17 @@ static gboolean _xfdashboard_theme_load_resources(XfdashboardTheme *self,
 			g_propagate_error(outError, error);
 
 			/* Release allocated resources */
-			_xfdashboard_theme_clean(self);
-
 			if(themeKeyFile) g_key_file_free(themeKeyFile);
 
 			/* Return FALSE to indicate error */
 			return(FALSE);
 		}
 
-		priv->effects=xfdashboard_theme_effects_new();
 		resource=resources;
 		while(*resource)
 		{
 			/* Get path and file for style resource */
-			resourceFile=g_build_filename(inThemePath, *resource, NULL);
+			resourceFile=g_build_filename(priv->themePath, *resource, NULL);
 
 			/* Try to load style resource */
 			if(!xfdashboard_theme_effects_add_file(priv->effects, resourceFile, &error))
@@ -371,8 +309,6 @@ static gboolean _xfdashboard_theme_load_resources(XfdashboardTheme *self,
 				g_propagate_error(outError, error);
 
 				/* Release allocated resources */
-				_xfdashboard_theme_clean(self);
-
 				if(resources) g_strfreev(resources);
 				if(resourceFile) g_free(resourceFile);
 				if(themeKeyFile) g_key_file_free(themeKeyFile);
@@ -391,7 +327,6 @@ static gboolean _xfdashboard_theme_load_resources(XfdashboardTheme *self,
 		g_strfreev(resources);
 	}
 
-
 	/* Release allocated resources */
 	if(themeKeyFile) g_key_file_free(themeKeyFile);
 
@@ -491,15 +426,108 @@ static gchar* _xfdashboard_theme_lookup_path_for_theme(XfdashboardTheme *self,
 	return(NULL);
 }
 
+/* Theme's name was set so lookup pathes and initialize but do not load resources */
+static void _xfdashboard_theme_set_theme_name(XfdashboardTheme *self, const gchar *inThemeName)
+{
+	XfdashboardThemePrivate		*priv;
+	gchar						*themePath;
+
+	g_return_if_fail(XFDASHBOARD_IS_THEME(self));
+	g_return_if_fail(inThemeName && *inThemeName);
+
+	priv=self->priv;
+
+	/* The theme name must not be set already */
+	if(priv->themeName)
+	{
+		/* Show error message */
+		g_critical(_("Cannot change theme name to '%s' because it is already set to '%s'"),
+					inThemeName,
+					priv->themeName);
+
+		return;
+	}
+
+	/* Lookup path of theme by lookup at all possible paths for theme file */
+	themePath=_xfdashboard_theme_lookup_path_for_theme(self, inThemeName);
+	if(!themePath)
+	{
+		g_critical(_("Theme '%s' not found"), inThemeName);
+
+		/* Return here because looking up path failed */
+		return;
+	}
+
+	/* Set up internal variable and notify about property changes */
+	priv->themeName=g_strdup(inThemeName);
+	g_object_notify_by_pspec(G_OBJECT(self), XfdashboardThemeProperties[PROP_NAME]);
+
+	priv->themePath=g_strdup(themePath);
+	g_object_notify_by_pspec(G_OBJECT(self), XfdashboardThemeProperties[PROP_PATH]);
+
+	/* Initialize theme resources */
+	priv->styling=xfdashboard_theme_css_new(priv->themePath);
+	priv->layout=xfdashboard_theme_layout_new();
+	priv->effects=xfdashboard_theme_effects_new();
+
+	/* Release allocated resources */
+	if(themePath) g_free(themePath);
+}
+
 /* IMPLEMENTATION: GObject */
 
 /* Dispose this object */
 static void _xfdashboard_theme_dispose(GObject *inObject)
 {
 	XfdashboardTheme			*self=XFDASHBOARD_THEME(inObject);
+	XfdashboardThemePrivate		*priv=self->priv;
 
 	/* Release allocated resources */
-	_xfdashboard_theme_clean(self);
+	if(priv->themeName)
+	{
+		g_free(priv->themeName);
+		priv->themeName=NULL;
+		g_object_notify_by_pspec(G_OBJECT(self), XfdashboardThemeProperties[PROP_NAME]);
+	}
+
+	if(priv->themePath)
+	{
+		g_free(priv->themePath);
+		priv->themePath=NULL;
+		g_object_notify_by_pspec(G_OBJECT(self), XfdashboardThemeProperties[PROP_PATH]);
+	}
+
+	if(priv->themeDisplayName)
+	{
+		g_free(priv->themeDisplayName);
+		priv->themeDisplayName=NULL;
+		g_object_notify_by_pspec(G_OBJECT(self), XfdashboardThemeProperties[PROP_DISPLAY_NAME]);
+	}
+
+	if(priv->themeComment)
+	{
+		g_free(priv->themeComment);
+		priv->themeComment=NULL;
+		g_object_notify_by_pspec(G_OBJECT(self), XfdashboardThemeProperties[PROP_COMMENT]);
+	}
+
+	if(priv->styling)
+	{
+		g_object_unref(priv->styling);
+		priv->styling=NULL;
+	}
+
+	if(priv->layout)
+	{
+		g_object_unref(priv->layout);
+		priv->layout=NULL;
+	}
+
+	if(priv->effects)
+	{
+		g_object_unref(priv->effects);
+		priv->effects=NULL;
+	}
 
 	/* Call parent's class dispose method */
 	G_OBJECT_CLASS(xfdashboard_theme_parent_class)->dispose(inObject);
@@ -511,8 +539,14 @@ static void _xfdashboard_theme_set_property(GObject *inObject,
 											const GValue *inValue,
 											GParamSpec *inSpec)
 {
+	XfdashboardTheme			*self=XFDASHBOARD_THEME(inObject);
+
 	switch(inPropID)
 	{
+		case PROP_NAME:
+			_xfdashboard_theme_set_theme_name(self, g_value_get_string(inValue));
+			break;
+
 		default:
 			G_OBJECT_WARN_INVALID_PROPERTY_ID(inObject, inPropID, inSpec);
 			break;
@@ -568,18 +602,18 @@ void xfdashboard_theme_class_init(XfdashboardThemeClass *klass)
 	g_type_class_add_private(klass, sizeof(XfdashboardThemePrivate));
 
 	/* Define properties */
-	XfdashboardThemeProperties[PROP_PATH]=
-		g_param_spec_string("theme-path",
-							_("Theme path"),
-							_("Path where theme was found and loaded from"),
-							NULL,
-							G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
-
 	XfdashboardThemeProperties[PROP_NAME]=
 		g_param_spec_string("theme-name",
 							_("Theme name"),
 							_("Short name of theme which was used to lookup theme and folder name where theme is stored in"),
 							NULL,
+							G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
+
+	XfdashboardThemeProperties[PROP_PATH]=
+		g_param_spec_string("theme-path",
+							_("Theme path"),
+							_("Path where theme was found and loaded from"),
+							NULL,
 							G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
 
 	XfdashboardThemeProperties[PROP_DISPLAY_NAME]=
@@ -609,6 +643,7 @@ void xfdashboard_theme_init(XfdashboardTheme *self)
 	priv=self->priv=XFDASHBOARD_THEME_GET_PRIVATE(self);
 
 	/* Set default values */
+	priv->loaded=FALSE;
 	priv->themeName=NULL;
 	priv->themePath=NULL;
 	priv->themeDisplayName=NULL;
@@ -628,9 +663,11 @@ GQuark xfdashboard_theme_error_quark(void)
 /* IMPLEMENTATION: Public API */
 
 /* Create new instance */
-XfdashboardTheme* xfdashboard_theme_new(void)
+XfdashboardTheme* xfdashboard_theme_new(const gchar *inThemeName)
 {
-	return(XFDASHBOARD_THEME(g_object_new(XFDASHBOARD_TYPE_THEME, NULL)));
+	return(XFDASHBOARD_THEME(g_object_new(XFDASHBOARD_TYPE_THEME,
+											"theme-name", inThemeName,
+											NULL)));
 }
 
 /* Get path where this theme was found and loaded from */
@@ -667,87 +704,45 @@ const gchar* xfdashboard_theme_get_comment(XfdashboardTheme *self)
 
 /* Lookup named theme and load resources */
 gboolean xfdashboard_theme_load(XfdashboardTheme *self,
-								const gchar *inThemeName,
 								GError **outError)
 {
 	XfdashboardThemePrivate		*priv;
 	GError						*error;
-	gchar						*themePath;
 
 	g_return_val_if_fail(XFDASHBOARD_IS_THEME(self), FALSE);
-	g_return_val_if_fail(inThemeName!=NULL && *inThemeName!=0, FALSE);
 	g_return_val_if_fail(outError==NULL || *outError==NULL, FALSE);
 
 	priv=self->priv;
 	error=NULL;
 
-	/* Freeze notifications about property changes and collect them */
-	g_object_freeze_notify(G_OBJECT(self));
-
 	/* Check if a theme was already loaded */
-	if(priv->themeName)
+	if(priv->loaded)
 	{
 		g_set_error(outError,
 					XFDASHBOARD_THEME_ERROR,
 					XFDASHBOARD_THEME_ERROR_ALREADY_LOADED,
-					_("Theme '%s' requested but '%s' was already loaded"),
-					inThemeName,
+					_("Theme '%s' was already loaded"),
 					priv->themeName);
 
-		/* Thaw notifications and send them now */
-		g_object_thaw_notify(G_OBJECT(self));
-
 		return(FALSE);
 	}
 
-	/* Lookup path of theme by lookup at all possible paths for theme file */
-	themePath=_xfdashboard_theme_lookup_path_for_theme(self, inThemeName);
-	if(!themePath)
-	{
-		g_set_error(outError,
-					XFDASHBOARD_THEME_ERROR,
-					XFDASHBOARD_THEME_ERROR_THEME_NOT_FOUND,
-					_("Theme '%s' not found"),
-					inThemeName);
-
-		/* Thaw notifications and send them now */
-		g_object_thaw_notify(G_OBJECT(self));
-
-		/* Return FALSE to indicate error */
-		return(FALSE);
-	}
+	/* We set the loaded flag regardless if loading will be successful or not
+	 * because if loading theme fails this object is in undefined state for
+	 * re-using it to load theme again.
+	 */
+	priv->loaded=TRUE;
 
 	/* Load theme key file */
-	if(!_xfdashboard_theme_load_resources(self, themePath, &error))
+	if(!_xfdashboard_theme_load_resources(self, &error))
 	{
 		/* Set returned error */
 		g_propagate_error(outError, error);
 
-		/* Release allocated resources */
-		if(themePath) g_free(themePath);
-
-		/* Thaw notifications and send them now */
-		g_object_thaw_notify(G_OBJECT(self));
-
 		/* Return FALSE to indicate error */
 		return(FALSE);
 	}
 
-	/* Set up internal variables because theme was loaded successfully
-	 * and notify about property changes
-	 */
-	priv->themePath=g_strdup(themePath);
-	g_object_notify_by_pspec(G_OBJECT(self), XfdashboardThemeProperties[PROP_PATH]);
-
-	priv->themeName=g_strdup(inThemeName);
-	g_object_notify_by_pspec(G_OBJECT(self), XfdashboardThemeProperties[PROP_NAME]);
-
-	/* Release allocated resources */
-	if(themePath) g_free(themePath);
-
-	/* Thaw notifications and send them now */
-	g_object_thaw_notify(G_OBJECT(self));
-
 	/* If we found named themed and could load all resources successfully */
 	return(TRUE);
 }
diff --git a/libxfdashboard/theme.h b/libxfdashboard/theme.h
index addbec5..9e8fcb8 100644
--- a/libxfdashboard/theme.h
+++ b/libxfdashboard/theme.h
@@ -74,17 +74,14 @@ GQuark xfdashboard_theme_error_quark(void);
 
 typedef enum /*< prefix=XFDASHBOARD_THEME_ERROR >*/
 {
-	XFDASHBOARD_THEME_ERROR_GENERAL_ERROR,
-	XFDASHBOARD_THEME_ERROR_ALREADY_LOADED,
 	XFDASHBOARD_THEME_ERROR_THEME_NOT_FOUND,
-	XFDASHBOARD_THEME_ERROR_INVALID_THEME_FILE,
-	XFDASHBOARD_THEME_ERROR_RESOURCE_NOT_FOUND
+	XFDASHBOARD_THEME_ERROR_ALREADY_LOADED
 } XfdashboardThemeErrorEnum;
 
 /* Public API */
 GType xfdashboard_theme_get_type(void) G_GNUC_CONST;
 
-XfdashboardTheme* xfdashboard_theme_new(void);
+XfdashboardTheme* xfdashboard_theme_new(const gchar *inThemeName);
 
 const gchar* xfdashboard_theme_get_path(XfdashboardTheme *self);
 
@@ -93,7 +90,6 @@ const gchar* xfdashboard_theme_get_display_name(XfdashboardTheme *self);
 const gchar* xfdashboard_theme_get_comment(XfdashboardTheme *self);
 
 gboolean xfdashboard_theme_load(XfdashboardTheme *self,
-								const gchar *inThemeName,
 								GError **outError);
 
 XfdashboardThemeCSS* xfdashboard_theme_get_css(XfdashboardTheme *self);

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list