>From e156890e27c21700d58d02a4160fbac400ac94f7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?"Rodolfo=20Garc=C3=ADa=20Pe=C3=B1as=20(kix)"?= <[email protected]>
Date: Tue, 3 Jul 2012 12:00:47 +0200
Subject: [PATCH 04/13] wDefaultGetImage splitted

The function wDefaultGetImage() is splitted in two:

1. get_default_icon_filename(): This function returns the full
   path of an icon. The function search the icon in the database
   using instance and class.
2. get_default_icon_rimage(): This function returns the RImage for
   a given image path (full path). This function validate the icon
   size, so the icon is fully usable.

The function get_default_icon_filename() includes, as new ease, add
the .app icons in the search (using wApplicationExtractDirPackIcon()
function). To do it, the command should be included, because this
function search '"command".app' icon. Setting the command to NULL,
this ease is not used.
To do it, we need the function wApplicationExtractDirPackIcon(), defined
at appicon.c, so we need set the function as non-static and provide their
prototype in appicon.h.

This patch includes too an extra pointer check at wDefaultGetStartWorkspace
to sure that WDWindowAttributes exists.
---
 src/appicon.c   |    2 +-
 src/appicon.h   |    2 ++
 src/wdefaults.c |   79 +++++++++++++++++++++++++++++++++++++++++++------------
 3 files changed, 65 insertions(+), 18 deletions(-)

diff --git a/src/appicon.c b/src/appicon.c
index 25f88ce..795b020 100644
--- a/src/appicon.c
+++ b/src/appicon.c
@@ -74,7 +74,7 @@ static void remove_from_appicon_list(WScreen *scr, WAppIcon 
*appicon);
 /* This function is used if the application is a .app. It checks if it has an 
icon in it
  * like for example /usr/local/GNUstep/Applications/WPrefs.app/WPrefs.tiff
  */
-static void wApplicationExtractDirPackIcon(WScreen * scr, char *path, char 
*wm_instance, char *wm_class)
+void wApplicationExtractDirPackIcon(WScreen * scr, char *path, char 
*wm_instance, char *wm_class)
 {
        char *iconPath = NULL;
        char *tmp = NULL;
diff --git a/src/appicon.h b/src/appicon.h
index 1fbd976..280843c 100644
--- a/src/appicon.h
+++ b/src/appicon.h
@@ -80,4 +80,6 @@ void makeAppIconFor(WApplication * wapp);
 void removeAppIconFor(WApplication * wapp);
 void save_appicon(WAppIcon *aicon, Bool dock);
 void paint_app_icon(WApplication *wapp);
+void wApplicationExtractDirPackIcon(WScreen * scr, char *path, char 
*wm_instance,
+                                   char *wm_class);
 #endif
diff --git a/src/wdefaults.c b/src/wdefaults.c
index a34d86e..1f52d7d 100644
--- a/src/wdefaults.c
+++ b/src/wdefaults.c
@@ -36,6 +36,7 @@
 
 #include "WindowMaker.h"
 #include "window.h"
+#include "appicon.h"
 #include "screen.h"
 #include "funcs.h"
 #include "workspace.h"
@@ -374,36 +375,80 @@ static WMPropList *get_generic_value(char *instance, char 
*class,
        return value;
 }
 
-RImage *wDefaultGetImage(WScreen * scr, char *winstance, char *wclass, int 
max_size)
+/* Get the file name of the image, using instance and class */
+char *get_default_icon_filename(WScreen *scr, char *winstance, char *wclass, 
char *command,
+                               Bool noDefault)
 {
-       char *file_name;
-       char *path;
-       RImage *image;
+       char *file_name = NULL;
+       char *file_path = NULL;
 
        /* Get the file name of the image, using instance and class */
-       file_name = wDefaultGetIconFile(winstance, wclass, False);
-       if (!file_name)
-               return NULL;
+       file_name = wDefaultGetIconFile(winstance, wclass, noDefault);
+
+       /* If the specific (or generic if noDefault is False) icon filename
+        * is not found, and command is specified, then include the .app icons
+        * and re-do the search, but now always including the default icon
+        * so the icon is found always. The .app is selected before default */
+       if (!file_name && scr && command) {
+               wApplicationExtractDirPackIcon(scr, command, winstance, wclass);
+               file_name = wDefaultGetIconFile(winstance, wclass, False);
+       }
 
-       /* Search the file image in the icon paths */
-       path = FindImage(wPreferences.icon_path, file_name);
+       /* Get the full path for the image file */
+       if (file_name) {
+               file_path = FindImage(wPreferences.icon_path, file_name);
+
+               if (!file_path)
+                       wwarning(_("could not find icon file \"%s\""), 
file_name);
+
+               /* FIXME: Here, if file_path don't exists, then the icon is in 
the
+                * "icon database" (WDWindowAttributes->dictionary), but the 
icon
+                * is not en disk. Therefore, we should remove it from the icon
+                * database.
+                * OTOH, probably the correct message should be "could not find 
the
+                * icon file %s, please, check your configuration files", 
because
+                * the icons are loaded in the icon database from the 
configuration
+                * files
+                */
+
+               /* Don't wfree(file_name) here, because is a pointer to the icon
+                * dictionary (WDWindowAttributes->dictionary) value.
+                */
+       }
 
-       if (!path) {
-               wwarning(_("could not find icon file \"%s\""), file_name);
+       return file_path;
+}
+
+/* This function returns the image picture for the file_name file */
+RImage *get_default_icon_rimage(WScreen *scr, char *file_name, int max_size)
+{
+       RImage *image = NULL;
+
+       if (!file_name)
                return NULL;
-       }
 
-       image = RLoadImage(scr->rcontext, path, 0);
+       image = RLoadImage(scr->rcontext, file_name, 0);
        if (!image)
-               wwarning(_("error loading image file \"%s\": %s"), path, 
RMessageForError(RErrorCode));
-
-       wfree(path);
+               wwarning(_("error loading image file \"%s\": %s"), file_name,
+                        RMessageForError(RErrorCode));
 
        image = wIconValidateIconSize(scr, image, max_size);
 
        return image;
 }
 
+RImage *wDefaultGetImage(WScreen * scr, char *winstance, char *wclass, int 
max_size)
+{
+       char *file_name = NULL;
+
+       /* Get the file name of the image, using instance and class */
+       file_name = get_default_icon_filename(scr, winstance, wclass, NULL, 
False);
+       if (!file_name)
+               return NULL;
+
+       return get_default_icon_rimage(scr, file_name, max_size);
+}
+
 int wDefaultGetStartWorkspace(WScreen * scr, char *instance, char *class)
 {
        WMPropList *value;
@@ -441,7 +486,7 @@ char *wDefaultGetIconFile(char *instance, char *class, Bool 
noDefault)
        if (!ANoTitlebar)
                init_wdefaults();
 
-       if (!WDWindowAttributes->dictionary)
+       if (!WDWindowAttributes || !WDWindowAttributes->dictionary)
                return NULL;
 
        value = get_generic_value(instance, class, AIcon, noDefault);
-- 
1.7.10

-- 
||// //\\// Rodolfo "kix" Garcia
||\\// //\\ http://www.kix.es/
>From e156890e27c21700d58d02a4160fbac400ac94f7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?"Rodolfo=20Garc=C3=ADa=20Pe=C3=B1as=20(kix)"?= <[email protected]>
Date: Tue, 3 Jul 2012 12:00:47 +0200
Subject: [PATCH 04/13] wDefaultGetImage splitted

The function wDefaultGetImage() is splitted in two:

1. get_default_icon_filename(): This function returns the full
   path of an icon. The function search the icon in the database
   using instance and class.
2. get_default_icon_rimage(): This function returns the RImage for
   a given image path (full path). This function validate the icon
   size, so the icon is fully usable.

The function get_default_icon_filename() includes, as new ease, add
the .app icons in the search (using wApplicationExtractDirPackIcon()
function). To do it, the command should be included, because this
function search '"command".app' icon. Setting the command to NULL,
this ease is not used.
To do it, we need the function wApplicationExtractDirPackIcon(), defined
at appicon.c, so we need set the function as non-static and provide their
prototype in appicon.h.

This patch includes too an extra pointer check at wDefaultGetStartWorkspace
to sure that WDWindowAttributes exists.
---
 src/appicon.c   |    2 +-
 src/appicon.h   |    2 ++
 src/wdefaults.c |   79 +++++++++++++++++++++++++++++++++++++++++++------------
 3 files changed, 65 insertions(+), 18 deletions(-)

diff --git a/src/appicon.c b/src/appicon.c
index 25f88ce..795b020 100644
--- a/src/appicon.c
+++ b/src/appicon.c
@@ -74,7 +74,7 @@ static void remove_from_appicon_list(WScreen *scr, WAppIcon *appicon);
 /* This function is used if the application is a .app. It checks if it has an icon in it
  * like for example /usr/local/GNUstep/Applications/WPrefs.app/WPrefs.tiff
  */
-static void wApplicationExtractDirPackIcon(WScreen * scr, char *path, char *wm_instance, char *wm_class)
+void wApplicationExtractDirPackIcon(WScreen * scr, char *path, char *wm_instance, char *wm_class)
 {
 	char *iconPath = NULL;
 	char *tmp = NULL;
diff --git a/src/appicon.h b/src/appicon.h
index 1fbd976..280843c 100644
--- a/src/appicon.h
+++ b/src/appicon.h
@@ -80,4 +80,6 @@ void makeAppIconFor(WApplication * wapp);
 void removeAppIconFor(WApplication * wapp);
 void save_appicon(WAppIcon *aicon, Bool dock);
 void paint_app_icon(WApplication *wapp);
+void wApplicationExtractDirPackIcon(WScreen * scr, char *path, char *wm_instance,
+				    char *wm_class);
 #endif
diff --git a/src/wdefaults.c b/src/wdefaults.c
index a34d86e..1f52d7d 100644
--- a/src/wdefaults.c
+++ b/src/wdefaults.c
@@ -36,6 +36,7 @@
 
 #include "WindowMaker.h"
 #include "window.h"
+#include "appicon.h"
 #include "screen.h"
 #include "funcs.h"
 #include "workspace.h"
@@ -374,36 +375,80 @@ static WMPropList *get_generic_value(char *instance, char *class,
 	return value;
 }
 
-RImage *wDefaultGetImage(WScreen * scr, char *winstance, char *wclass, int max_size)
+/* Get the file name of the image, using instance and class */
+char *get_default_icon_filename(WScreen *scr, char *winstance, char *wclass, char *command,
+				Bool noDefault)
 {
-	char *file_name;
-	char *path;
-	RImage *image;
+	char *file_name = NULL;
+	char *file_path = NULL;
 
 	/* Get the file name of the image, using instance and class */
-	file_name = wDefaultGetIconFile(winstance, wclass, False);
-	if (!file_name)
-		return NULL;
+	file_name = wDefaultGetIconFile(winstance, wclass, noDefault);
+
+	/* If the specific (or generic if noDefault is False) icon filename
+	 * is not found, and command is specified, then include the .app icons
+	 * and re-do the search, but now always including the default icon
+	 * so the icon is found always. The .app is selected before default */
+	if (!file_name && scr && command) {
+		wApplicationExtractDirPackIcon(scr, command, winstance, wclass);
+		file_name = wDefaultGetIconFile(winstance, wclass, False);
+	}
 
-	/* Search the file image in the icon paths */
-	path = FindImage(wPreferences.icon_path, file_name);
+	/* Get the full path for the image file */
+	if (file_name) {
+		file_path = FindImage(wPreferences.icon_path, file_name);
+
+		if (!file_path)
+			wwarning(_("could not find icon file \"%s\""), file_name);
+
+		/* FIXME: Here, if file_path don't exists, then the icon is in the
+		 * "icon database" (WDWindowAttributes->dictionary), but the icon
+		 * is not en disk. Therefore, we should remove it from the icon
+		 * database.
+		 * OTOH, probably the correct message should be "could not find the
+		 * icon file %s, please, check your configuration files", because
+		 * the icons are loaded in the icon database from the configuration
+		 * files
+		 */
+
+		/* Don't wfree(file_name) here, because is a pointer to the icon
+		 * dictionary (WDWindowAttributes->dictionary) value.
+		 */
+	}
 
-	if (!path) {
-		wwarning(_("could not find icon file \"%s\""), file_name);
+	return file_path;
+}
+
+/* This function returns the image picture for the file_name file */
+RImage *get_default_icon_rimage(WScreen *scr, char *file_name, int max_size)
+{
+	RImage *image = NULL;
+
+	if (!file_name)
 		return NULL;
-	}
 
-	image = RLoadImage(scr->rcontext, path, 0);
+	image = RLoadImage(scr->rcontext, file_name, 0);
 	if (!image)
-		wwarning(_("error loading image file \"%s\": %s"), path, RMessageForError(RErrorCode));
-
-	wfree(path);
+		wwarning(_("error loading image file \"%s\": %s"), file_name,
+			 RMessageForError(RErrorCode));
 
 	image = wIconValidateIconSize(scr, image, max_size);
 
 	return image;
 }
 
+RImage *wDefaultGetImage(WScreen * scr, char *winstance, char *wclass, int max_size)
+{
+	char *file_name = NULL;
+
+	/* Get the file name of the image, using instance and class */
+	file_name = get_default_icon_filename(scr, winstance, wclass, NULL, False);
+	if (!file_name)
+		return NULL;
+
+	return get_default_icon_rimage(scr, file_name, max_size);
+}
+
 int wDefaultGetStartWorkspace(WScreen * scr, char *instance, char *class)
 {
 	WMPropList *value;
@@ -441,7 +486,7 @@ char *wDefaultGetIconFile(char *instance, char *class, Bool noDefault)
 	if (!ANoTitlebar)
 		init_wdefaults();
 
-	if (!WDWindowAttributes->dictionary)
+	if (!WDWindowAttributes || !WDWindowAttributes->dictionary)
 		return NULL;
 
 	value = get_generic_value(instance, class, AIcon, noDefault);
-- 
1.7.10

Reply via email to