>From 2177b31af8a130ec7672a1be9770fea3b2e81c5b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?"Rodolfo=20Garc=C3=ADa=20Pe=C3=B1as=20(kix)"?= <[email protected]>
Date: Fri, 15 Jun 2012 18:25:23 +0200
Subject: [PATCH] New save_app_icon and extractIcon removed

The function save_app_icon has big changes, because is similar
to wAppIconSave:
- The function get the name of the icon, then checks if the icon
  is in the filesystem. If exists, return. If not exists and
  is defined in the database, then it re-creates it. This is not
  needed, because wIconStore checks if the file exists in the
  filesystem, then is checked twice. Because save_app_icon must
  save the icon always (replacing extractIcon), then we can call
  wIconStore without check nothing. wIconStore do the work.
- The function save_app_icon now calls wApplicationSaveIconPathFor()
  and save the icon path in the Dictionary. Then the icon can be
  found. This is new, but must be done (it was a bug).

The new function save_app_icon is similar to wAppIconSave. The
only different is that wAppIconSave is called for an icon (docks,...)
and save_app_icon for windows. We cannot use only one function and
join both functions, because docks don't have windows. The trick is
move the common part to a different function, save_app_icon_core.

save_app_icon() only needs the WApplication argument, then WWindow is
removed.

This commit includes:

Application.c:
- The appicon is always created and saved, only painted if needed.
- The extractIcon function is removed, because their behaiour is
  done by save_app_icon. Now save_app_icon is called allways, if
  no_appicon is set or not. Because the extractIcon is removed,
  the function GetProgramNameForWindow (misc.c) can be deleted.
- The app_icon stuff is moved from application.c to appicon.c:
  - wApplicationExtractDirPackIcon() moved and now static.
  - app_icon_create_from_docks() moved and now is not static,
    because this the only function is called in application.c
  - findDockIconFor() moved (was and is static).
  - wApplicationSaveIconPathFor() moved and now static.
- Some includes and externs removed in application.c.

Appicon.h:
- wAppIconChangeImage() revoved, because is only declared.
---
 src/appicon.c     |  189 +++++++++++++++++++++++++++++++++++++++++++----------
 src/appicon.h     |   11 ++--
 src/application.c |  171 +++---------------------------------------------
 src/application.h |    4 --
 src/icon.c        |    2 +-
 src/misc.c        |    6 --
 6 files changed, 171 insertions(+), 212 deletions(-)

diff --git a/src/appicon.c b/src/appicon.c
index 0083944..03f5e35 100644
--- a/src/appicon.c
+++ b/src/appicon.c
@@ -26,6 +26,7 @@
 #include <X11/Xutil.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 #include <sys/stat.h>
 #include <errno.h>
 
@@ -58,12 +59,49 @@
 /**** Global variables ****/
 extern Cursor wCursor[WCUR_LAST];
 extern WPreferences wPreferences;
+extern WDDomain *WDWindowAttributes;
 
 #define MOD_MASK       wPreferences.modifier_mask
 
 void appIconMouseDown(WObjDescriptor * desc, XEvent * event);
 static void iconDblClick(WObjDescriptor * desc, XEvent * event);
 static void iconExpose(WObjDescriptor * desc, XEvent * event);
+static void wApplicationSaveIconPathFor(char *iconPath, char *wm_instance, 
char *wm_class);
+
+/* 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)
+{
+       char *iconPath = NULL;
+       char *tmp = NULL;
+
+       if (strstr(path, ".app")) {
+               tmp = wmalloc(strlen(path) + 16);
+
+               if (scr->flags.supports_tiff) {
+                       strcpy(tmp, path);
+                       strcat(tmp, ".tiff");
+                       if (access(tmp, R_OK) == 0)
+                               iconPath = tmp;
+               }
+
+               if (!iconPath) {
+                       strcpy(tmp, path);
+                       strcat(tmp, ".xpm");
+                       if (access(tmp, R_OK) == 0)
+                               iconPath = tmp;
+               }
+
+               if (!iconPath)
+                       wfree(tmp);
+
+               if (iconPath) {
+                       wApplicationSaveIconPathFor(iconPath, wm_instance, 
wm_class);
+                       wfree(iconPath);
+               }
+       }
+}
 
 WAppIcon *wAppIconCreateForDock(WScreen * scr, char *command, char 
*wm_instance, char *wm_class, int tile)
 {
@@ -359,22 +397,6 @@ void wAppIconPaint(WAppIcon * aicon)
                               0, 0, wPreferences.icon_size, 
wPreferences.icon_size);
 }
 
-Bool wAppIconSave(WAppIcon *aicon)
-{
-       char *path;
-
-       if (!aicon->docked || aicon->attracted) return True;
-
-       path = wIconStore(aicon->icon);
-       if (!path)
-               return False;
-
-       wApplicationSaveIconPathFor(path, aicon->wm_instance, aicon->wm_class);
-
-       wfree(path);
-       return True;
-}
-
 #define canBeDocked(wwin)  ((wwin) && ((wwin)->wm_class||(wwin)->wm_instance))
 
 /* main_window may not have the full command line; try to find one which does 
*/
@@ -844,32 +866,131 @@ void appIconMouseDown(WObjDescriptor * desc, XEvent * 
event)
        }
 }
 
-void save_app_icon(WWindow *wwin, WApplication *wapp)
+/* This function save the application icon and store the path in the 
Dictionary */
+static void wApplicationSaveIconPathFor(char *iconPath, char *wm_instance, 
char *wm_class)
+{
+       WMPropList *dict = WDWindowAttributes->dictionary;
+       WMPropList *adict, *key, *iconk;
+       WMPropList *val;
+       char *tmp;
+
+       tmp = get_name_for_instance_class(wm_instance, wm_class);
+       key = WMCreatePLString(tmp);
+       wfree(tmp);
+
+       adict = WMGetFromPLDictionary(dict, key);
+       iconk = WMCreatePLString("Icon");
+
+       if (adict) {
+               val = WMGetFromPLDictionary(adict, iconk);
+       } else {
+               /* no dictionary for app, so create one */
+               adict = WMCreatePLDictionary(NULL, NULL);
+               WMPutInPLDictionary(dict, key, adict);
+               WMReleasePropList(adict);
+               val = NULL;
+       }
+
+       if (!val) {
+               val = WMCreatePLString(iconPath);
+               WMPutInPLDictionary(adict, iconk, val);
+               WMReleasePropList(val);
+       } else {
+               val = NULL;
+       }
+
+       WMReleasePropList(key);
+       WMReleasePropList(iconk);
+
+       if (val && !wPreferences.flags.noupdates)
+               UpdateDomainFile(WDWindowAttributes);
+}
+
+/* Internal application to save the application icon */
+static void save_app_icon_core(WAppIcon *aicon)
 {
-       char *tmp, *path;
-       struct stat dummy;
-       WScreen *scr = NULL;
+       char *path;
+
+       path = wIconStore(aicon->icon);
+       if (!path)
+               return;
+
+       wApplicationSaveIconPathFor(path, aicon->wm_instance, aicon->wm_class);
 
+       wfree(path);
+}
+
+/* Save the application icon */
+/* This function is used when the icon don't have window, like dock or clip */
+void wAppIconSave(WAppIcon *aicon)
+{
+       if (!aicon->docked || aicon->attracted)
+               return;
+
+       save_app_icon_core(aicon);
+}
+
+/* Save the application icon */
+/* This function is used by normal windows */
+void save_app_icon(WApplication *wapp)
+{
        if (!wapp->app_icon)
                return;
 
-       scr = wwin->screen_ptr;
-       tmp = wDefaultGetIconFile(scr, wapp->app_icon->wm_instance, 
wapp->app_icon->wm_class, True);
+       save_app_icon_core(wapp->app_icon);
+}
 
-       /* If the icon was saved by us from the client supplied icon, but is
-        * missing, recreate it. */
-       if (tmp && strstr(tmp, "Library/WindowMaker/CachedPixmaps") != NULL &&
-           stat(tmp, &dummy) != 0 && errno == ENOENT) {
-               wmessage(_("recreating missing icon '%s'"), tmp);
-               path = wIconStore(wapp->app_icon->icon);
-               if (path)
-                       wfree(path);
+static WAppIcon *findDockIconFor(WDock * dock, Window main_window)
+{
+       WAppIcon *aicon = NULL;
 
-               wIconUpdate(wapp->app_icon->icon);
-               wAppIconPaint(wapp->app_icon);
+       aicon = wDockFindIconForWindow(dock, main_window);
+       if (!aicon) {
+               wDockTrackWindowLaunch(dock, main_window);
+               aicon = wDockFindIconForWindow(dock, main_window);
+       }
+       return aicon;
+}
+
+void app_icon_create_from_docks(WWindow *wwin, WApplication *wapp, Window 
main_window)
+{
+       WScreen *scr = wwin->screen_ptr;
+
+       /* Create the application icon */
+       wapp->app_icon = NULL;
+
+       if (scr->last_dock)
+               wapp->app_icon = findDockIconFor(scr->last_dock, main_window);
+
+       /* check main dock if we did not find it in last dock */
+       if (!wapp->app_icon && scr->dock)
+               wapp->app_icon = findDockIconFor(scr->dock, main_window);
+
+       /* finally check clips */
+       if (!wapp->app_icon) {
+               int i;
+               for (i = 0; i < scr->workspace_count; i++) {
+                       WDock *dock = scr->workspaces[i]->clip;
+                       if (dock)
+                               wapp->app_icon = findDockIconFor(dock, 
main_window);
+                       if (wapp->app_icon)
+                               break;
+               }
        }
 
-       /* if the displayed icon was supplied by the client, save the icon */
-       if (!tmp || strstr(tmp, "Library/WindowMaker/CachedPixmaps") != NULL)
+       /* If created, then set some flags */
+       if (wapp->app_icon) {
+               WWindow *mainw = wapp->main_window_desc;
+
+               wapp->app_icon->running = 1;
+               wapp->app_icon->icon->force_paint = 1;
+               wapp->app_icon->icon->owner = mainw;
+               if (mainw->wm_hints && (mainw->wm_hints->flags & 
IconWindowHint))
+                       wapp->app_icon->icon->icon_win = 
mainw->wm_hints->icon_window;
+
+               wAppIconPaint(wapp->app_icon);
                wAppIconSave(wapp->app_icon);
+       } else {
+               wapp->app_icon = wAppIconCreate(wapp->main_window_desc);
+       }
 }
diff --git a/src/appicon.h b/src/appicon.h
index 774c3e3..e401696 100644
--- a/src/appicon.h
+++ b/src/appicon.h
@@ -70,17 +70,16 @@ typedef struct WAppIcon {
 } WAppIcon;
 
 WAppIcon *wAppIconCreate(WWindow *leader_win);
-WAppIcon * wAppIconCreateForDock(WScreen *scr, char *command, char 
*wm_instance,
-                                char *wm_class, int tile);
+WAppIcon *wAppIconCreateForDock(WScreen *scr, char *command, char *wm_instance,
+                               char *wm_class, int tile);
 
+void app_icon_create_from_docks(WWindow *wwin, WApplication *wapp, Window 
main_window);
 void wAppIconDestroy(WAppIcon *aicon);
 void wAppIconPaint(WAppIcon *aicon);
 void wAppIconMove(WAppIcon *aicon, int x, int y);
 void makeAppIconFor(WApplication * wapp);
 void removeAppIconFor(WApplication * wapp);
-void save_app_icon(WWindow *wwin, WApplication *wapp);
+void save_app_icon(WApplication *wapp);
 void paint_app_icon(WApplication *wapp);
-
-Bool wAppIconChangeImage(WAppIcon *icon, char *file);
-Bool wAppIconSave(WAppIcon *aicon);
+void wAppIconSave(WAppIcon *aicon);
 #endif
diff --git a/src/application.c b/src/application.c
index ca47203..97943e9 100644
--- a/src/application.c
+++ b/src/application.c
@@ -21,9 +21,7 @@
 #include "wconfig.h"
 
 #include <X11/Xlib.h>
-
 #include <string.h>
-#include <unistd.h>
 
 #include "WindowMaker.h"
 #include "menu.h"
@@ -31,12 +29,10 @@
 #ifdef USER_MENU
 #include "usermenu.h"
 #endif                         /* USER_MENU */
-#include "appicon.h"
 #include "application.h"
 #include "appmenu.h"
+#include "appicon.h"
 #include "properties.h"
-#include "stacking.h"
-#include "actions.h"
 #include "workspace.h"
 #include "dock.h"
 
@@ -44,8 +40,6 @@
 
 extern XContext wAppWinContext;
 extern XContext wWinContext;
-extern WPreferences wPreferences;
-extern WDDomain *WDWindowAttributes;
 
 /******** Local variables ********/
 
@@ -83,145 +77,6 @@ WApplication *wApplicationOf(Window window)
        return wapp;
 }
 
-static WAppIcon *findDockIconFor(WDock * dock, Window main_window)
-{
-       WAppIcon *aicon = NULL;
-
-       aicon = wDockFindIconForWindow(dock, main_window);
-       if (!aicon) {
-               wDockTrackWindowLaunch(dock, main_window);
-               aicon = wDockFindIconForWindow(dock, main_window);
-       }
-       return aicon;
-}
-
-static void extractIcon(WWindow * wwin)
-{
-       char *progname;
-
-       /* Get the application name */
-       progname = GetProgramNameForWindow(wwin->client_win);
-       if (progname) {
-               /* Save the icon path if the application is ".app" */
-               wApplicationExtractDirPackIcon(wwin->screen_ptr, progname, 
wwin->wm_instance, wwin->wm_class);
-               wfree(progname);
-       }
-}
-
-void wApplicationSaveIconPathFor(char *iconPath, char *wm_instance, char 
*wm_class)
-{
-       WMPropList *dict = WDWindowAttributes->dictionary;
-       WMPropList *adict, *key, *iconk;
-       WMPropList *val;
-       char *tmp;
-
-       tmp = get_name_for_instance_class(wm_instance, wm_class);
-       key = WMCreatePLString(tmp);
-       wfree(tmp);
-
-       adict = WMGetFromPLDictionary(dict, key);
-       iconk = WMCreatePLString("Icon");
-
-       if (adict) {
-               val = WMGetFromPLDictionary(adict, iconk);
-       } else {
-               /* no dictionary for app, so create one */
-               adict = WMCreatePLDictionary(NULL, NULL);
-               WMPutInPLDictionary(dict, key, adict);
-               WMReleasePropList(adict);
-               val = NULL;
-       }
-
-       if (!val) {
-               val = WMCreatePLString(iconPath);
-               WMPutInPLDictionary(adict, iconk, val);
-               WMReleasePropList(val);
-       } else {
-               val = NULL;
-       }
-
-       WMReleasePropList(key);
-       WMReleasePropList(iconk);
-
-       if (val && !wPreferences.flags.noupdates)
-               UpdateDomainFile(WDWindowAttributes);
-}
-
-/* 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
- */
-void wApplicationExtractDirPackIcon(WScreen * scr, char *path, char 
*wm_instance, char *wm_class)
-{
-       char *iconPath = NULL;
-       char *tmp = NULL;
-
-       if (strstr(path, ".app")) {
-               tmp = wmalloc(strlen(path) + 16);
-
-               if (scr->flags.supports_tiff) {
-                       strcpy(tmp, path);
-                       strcat(tmp, ".tiff");
-                       if (access(tmp, R_OK) == 0)
-                               iconPath = tmp;
-               }
-
-               if (!iconPath) {
-                       strcpy(tmp, path);
-                       strcat(tmp, ".xpm");
-                       if (access(tmp, R_OK) == 0)
-                               iconPath = tmp;
-               }
-
-               if (!iconPath)
-                       wfree(tmp);
-
-               if (iconPath) {
-                       wApplicationSaveIconPathFor(iconPath, wm_instance, 
wm_class);
-                       wfree(iconPath);
-               }
-       }
-}
-
-static void app_icon_create_from_docks(WWindow *wwin, WApplication *wapp, 
Window main_window)
-{
-       WScreen *scr = wwin->screen_ptr;
-
-       if (scr->last_dock)
-               wapp->app_icon = findDockIconFor(scr->last_dock, main_window);
-
-       /* check main dock if we did not find it in last dock */
-       if (!wapp->app_icon && scr->dock)
-               wapp->app_icon = findDockIconFor(scr->dock, main_window);
-
-       /* finally check clips */
-       if (!wapp->app_icon) {
-               int i;
-               for (i = 0; i < scr->workspace_count; i++) {
-                       WDock *dock = scr->workspaces[i]->clip;
-                       if (dock)
-                               wapp->app_icon = findDockIconFor(dock, 
main_window);
-                       if (wapp->app_icon)
-                               break;
-               }
-       }
-
-       /* If created, then set some flags */
-       if (wapp->app_icon) {
-               WWindow *mainw = wapp->main_window_desc;
-
-               wapp->app_icon->running = 1;
-               wapp->app_icon->icon->force_paint = 1;
-               wapp->app_icon->icon->owner = mainw;
-               if (mainw->wm_hints && (mainw->wm_hints->flags & 
IconWindowHint))
-                       wapp->app_icon->icon->icon_win = 
mainw->wm_hints->icon_window;
-
-               wAppIconPaint(wapp->app_icon);
-               wAppIconSave(wapp->app_icon);
-       } else {
-               wapp->app_icon = wAppIconCreate(wapp->main_window_desc);
-       }
-}
-
 WApplication *wApplicationCreate(WWindow * wwin)
 {
        WScreen *scr = wwin->screen_ptr;
@@ -269,8 +124,6 @@ WApplication *wApplicationCreate(WWindow * wwin)
        wapp->main_window_desc->fake_group = wwin->fake_group;
        wapp->main_window_desc->net_icon_image = 
RRetainImage(wwin->net_icon_image);
 
-       extractIcon(wapp->main_window_desc);
-
        leader = wWindowFor(main_window);
        if (leader)
                leader->main_window = main_window;
@@ -290,20 +143,17 @@ WApplication *wApplicationCreate(WWindow * wwin)
        /* application descriptor */
        XSaveContext(dpy, main_window, wAppWinContext, (XPointer) wapp);
 
-       /* Create the application icon */
-       wapp->app_icon = NULL;
-       if (!WFLAGP(wapp->main_window_desc, no_appicon)) {
-               /* Create the application icon using the icon from docks
-                * If not found in docks, create a new icon 
-                * using the function wAppIconCreate() */
-               app_icon_create_from_docks(wwin, wapp, main_window);
+       /* Create the application icon using the icon from docks
+        * If not found in docks, create a new icon 
+        * using the function wAppIconCreate() */
+       app_icon_create_from_docks(wwin, wapp, main_window);
 
-               /* Now, paint the icon */
-               paint_app_icon(wapp);
+       /* Save the app_icon in a file */
+       save_app_icon(wapp);
 
-               /* Save the app_icon in a file */
-               save_app_icon(wwin, wapp);
-       }
+       /* Now, paint the icon */
+       if (!WFLAGP(wapp->main_window_desc, no_appicon))
+               paint_app_icon(wapp);
 
        return wapp;
 }
@@ -381,4 +231,3 @@ void wApplicationDeactivate(WApplication *wapp)
        }
 #endif
 }
-
diff --git a/src/application.h b/src/application.h
index 31cbe68..1a95999 100644
--- a/src/application.h
+++ b/src/application.h
@@ -48,10 +48,6 @@ typedef struct WApplication {
 WApplication *wApplicationCreate(struct WWindow *wwin);
 WApplication *wApplicationOf(Window window);
 void wApplicationDestroy(WApplication *wapp);
-void wApplicationExtractDirPackIcon(WScreen *scr,char *path, char *wm_instance,
-                                    char *wm_class);
-void wApplicationSaveIconPathFor(char *iconPath, char *wm_instance,
-                                char *wm_class);
 
 void wAppBounce(WApplication *);
 void wAppBounceWhileUrgent(WApplication *);
diff --git a/src/icon.c b/src/icon.c
index 59161d3..a270a35 100644
--- a/src/icon.c
+++ b/src/icon.c
@@ -469,7 +469,7 @@ static RImage *get_wwindow_image_from_wmhints(WWindow 
*wwin, WIcon *icon)
 
 /*
  * wIconStore--
- *     Stores the client supplied icon at 
~/GNUstep/Library/WindowMaker/CachedPixmaps
+ *     Stores the client supplied icon at CACHE_ICON_PATH
  * and returns the path for that icon. Returns NULL if there is no
  * client supplied icon or on failure.
  *
diff --git a/src/misc.c b/src/misc.c
index fd283c2..389ad00 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -1094,9 +1094,3 @@ char *GetCommandForWindow(Window win)
 {
        return getCommandForWindow(win, 0);
 }
-
-/* Free result when done */
-char *GetProgramNameForWindow(Window win)
-{
-       return getCommandForWindow(win, 1);
-}
-- 
1.7.10

-- 
||// //\\// Rodolfo "kix" Garcia
||\\// //\\ http://www.kix.es/
>From 2177b31af8a130ec7672a1be9770fea3b2e81c5b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?"Rodolfo=20Garc=C3=ADa=20Pe=C3=B1as=20(kix)"?= <[email protected]>
Date: Fri, 15 Jun 2012 18:25:23 +0200
Subject: [PATCH] New save_app_icon and extractIcon removed

The function save_app_icon has big changes, because is similar
to wAppIconSave:
- The function get the name of the icon, then checks if the icon
  is in the filesystem. If exists, return. If not exists and
  is defined in the database, then it re-creates it. This is not
  needed, because wIconStore checks if the file exists in the
  filesystem, then is checked twice. Because save_app_icon must
  save the icon always (replacing extractIcon), then we can call
  wIconStore without check nothing. wIconStore do the work.
- The function save_app_icon now calls wApplicationSaveIconPathFor()
  and save the icon path in the Dictionary. Then the icon can be
  found. This is new, but must be done (it was a bug).

The new function save_app_icon is similar to wAppIconSave. The
only different is that wAppIconSave is called for an icon (docks,...)
and save_app_icon for windows. We cannot use only one function and
join both functions, because docks don't have windows. The trick is
move the common part to a different function, save_app_icon_core.

save_app_icon() only needs the WApplication argument, then WWindow is
removed.

This commit includes:

Application.c:
- The appicon is always created and saved, only painted if needed.
- The extractIcon function is removed, because their behaiour is
  done by save_app_icon. Now save_app_icon is called allways, if
  no_appicon is set or not. Because the extractIcon is removed,
  the function GetProgramNameForWindow (misc.c) can be deleted.
- The app_icon stuff is moved from application.c to appicon.c:
  - wApplicationExtractDirPackIcon() moved and now static.
  - app_icon_create_from_docks() moved and now is not static,
    because this the only function is called in application.c
  - findDockIconFor() moved (was and is static).
  - wApplicationSaveIconPathFor() moved and now static.
- Some includes and externs removed in application.c.

Appicon.h:
- wAppIconChangeImage() revoved, because is only declared.
---
 src/appicon.c     |  189 +++++++++++++++++++++++++++++++++++++++++++----------
 src/appicon.h     |   11 ++--
 src/application.c |  171 +++---------------------------------------------
 src/application.h |    4 --
 src/icon.c        |    2 +-
 src/misc.c        |    6 --
 6 files changed, 171 insertions(+), 212 deletions(-)

diff --git a/src/appicon.c b/src/appicon.c
index 0083944..03f5e35 100644
--- a/src/appicon.c
+++ b/src/appicon.c
@@ -26,6 +26,7 @@
 #include <X11/Xutil.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 #include <sys/stat.h>
 #include <errno.h>
 
@@ -58,12 +59,49 @@
 /**** Global variables ****/
 extern Cursor wCursor[WCUR_LAST];
 extern WPreferences wPreferences;
+extern WDDomain *WDWindowAttributes;
 
 #define MOD_MASK       wPreferences.modifier_mask
 
 void appIconMouseDown(WObjDescriptor * desc, XEvent * event);
 static void iconDblClick(WObjDescriptor * desc, XEvent * event);
 static void iconExpose(WObjDescriptor * desc, XEvent * event);
+static void wApplicationSaveIconPathFor(char *iconPath, char *wm_instance, char *wm_class);
+
+/* 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)
+{
+	char *iconPath = NULL;
+	char *tmp = NULL;
+
+	if (strstr(path, ".app")) {
+		tmp = wmalloc(strlen(path) + 16);
+
+		if (scr->flags.supports_tiff) {
+			strcpy(tmp, path);
+			strcat(tmp, ".tiff");
+			if (access(tmp, R_OK) == 0)
+				iconPath = tmp;
+		}
+
+		if (!iconPath) {
+			strcpy(tmp, path);
+			strcat(tmp, ".xpm");
+			if (access(tmp, R_OK) == 0)
+				iconPath = tmp;
+		}
+
+		if (!iconPath)
+			wfree(tmp);
+
+		if (iconPath) {
+			wApplicationSaveIconPathFor(iconPath, wm_instance, wm_class);
+			wfree(iconPath);
+		}
+	}
+}
 
 WAppIcon *wAppIconCreateForDock(WScreen * scr, char *command, char *wm_instance, char *wm_class, int tile)
 {
@@ -359,22 +397,6 @@ void wAppIconPaint(WAppIcon * aicon)
 			       0, 0, wPreferences.icon_size, wPreferences.icon_size);
 }
 
-Bool wAppIconSave(WAppIcon *aicon)
-{
-	char *path;
-
-	if (!aicon->docked || aicon->attracted) return True;
-
-	path = wIconStore(aicon->icon);
-	if (!path)
-		return False;
-
-	wApplicationSaveIconPathFor(path, aicon->wm_instance, aicon->wm_class);
-
-	wfree(path);
-	return True;
-}
-
 #define canBeDocked(wwin)  ((wwin) && ((wwin)->wm_class||(wwin)->wm_instance))
 
 /* main_window may not have the full command line; try to find one which does */
@@ -844,32 +866,131 @@ void appIconMouseDown(WObjDescriptor * desc, XEvent * event)
 	}
 }
 
-void save_app_icon(WWindow *wwin, WApplication *wapp)
+/* This function save the application icon and store the path in the Dictionary */
+static void wApplicationSaveIconPathFor(char *iconPath, char *wm_instance, char *wm_class)
+{
+	WMPropList *dict = WDWindowAttributes->dictionary;
+	WMPropList *adict, *key, *iconk;
+	WMPropList *val;
+	char *tmp;
+
+	tmp = get_name_for_instance_class(wm_instance, wm_class);
+	key = WMCreatePLString(tmp);
+	wfree(tmp);
+
+	adict = WMGetFromPLDictionary(dict, key);
+	iconk = WMCreatePLString("Icon");
+
+	if (adict) {
+		val = WMGetFromPLDictionary(adict, iconk);
+	} else {
+		/* no dictionary for app, so create one */
+		adict = WMCreatePLDictionary(NULL, NULL);
+		WMPutInPLDictionary(dict, key, adict);
+		WMReleasePropList(adict);
+		val = NULL;
+	}
+
+	if (!val) {
+		val = WMCreatePLString(iconPath);
+		WMPutInPLDictionary(adict, iconk, val);
+		WMReleasePropList(val);
+	} else {
+		val = NULL;
+	}
+
+	WMReleasePropList(key);
+	WMReleasePropList(iconk);
+
+	if (val && !wPreferences.flags.noupdates)
+		UpdateDomainFile(WDWindowAttributes);
+}
+
+/* Internal application to save the application icon */
+static void save_app_icon_core(WAppIcon *aicon)
 {
-	char *tmp, *path;
-	struct stat dummy;
-	WScreen *scr = NULL;
+	char *path;
+
+	path = wIconStore(aicon->icon);
+	if (!path)
+		return;
+
+	wApplicationSaveIconPathFor(path, aicon->wm_instance, aicon->wm_class);
 
+	wfree(path);
+}
+
+/* Save the application icon */
+/* This function is used when the icon don't have window, like dock or clip */
+void wAppIconSave(WAppIcon *aicon)
+{
+	if (!aicon->docked || aicon->attracted)
+		return;
+
+	save_app_icon_core(aicon);
+}
+
+/* Save the application icon */
+/* This function is used by normal windows */
+void save_app_icon(WApplication *wapp)
+{
 	if (!wapp->app_icon)
 		return;
 
-	scr = wwin->screen_ptr;
-	tmp = wDefaultGetIconFile(scr, wapp->app_icon->wm_instance, wapp->app_icon->wm_class, True);
+	save_app_icon_core(wapp->app_icon);
+}
 
-	/* If the icon was saved by us from the client supplied icon, but is
-	 * missing, recreate it. */
-	if (tmp && strstr(tmp, "Library/WindowMaker/CachedPixmaps") != NULL &&
-	    stat(tmp, &dummy) != 0 && errno == ENOENT) {
-		wmessage(_("recreating missing icon '%s'"), tmp);
-		path = wIconStore(wapp->app_icon->icon);
-		if (path)
-			wfree(path);
+static WAppIcon *findDockIconFor(WDock * dock, Window main_window)
+{
+	WAppIcon *aicon = NULL;
 
-		wIconUpdate(wapp->app_icon->icon);
-		wAppIconPaint(wapp->app_icon);
+	aicon = wDockFindIconForWindow(dock, main_window);
+	if (!aicon) {
+		wDockTrackWindowLaunch(dock, main_window);
+		aicon = wDockFindIconForWindow(dock, main_window);
+	}
+	return aicon;
+}
+
+void app_icon_create_from_docks(WWindow *wwin, WApplication *wapp, Window main_window)
+{
+	WScreen *scr = wwin->screen_ptr;
+
+	/* Create the application icon */
+	wapp->app_icon = NULL;
+
+	if (scr->last_dock)
+		wapp->app_icon = findDockIconFor(scr->last_dock, main_window);
+
+	/* check main dock if we did not find it in last dock */
+	if (!wapp->app_icon && scr->dock)
+		wapp->app_icon = findDockIconFor(scr->dock, main_window);
+
+	/* finally check clips */
+	if (!wapp->app_icon) {
+		int i;
+		for (i = 0; i < scr->workspace_count; i++) {
+			WDock *dock = scr->workspaces[i]->clip;
+			if (dock)
+				wapp->app_icon = findDockIconFor(dock, main_window);
+			if (wapp->app_icon)
+				break;
+		}
 	}
 
-	/* if the displayed icon was supplied by the client, save the icon */
-	if (!tmp || strstr(tmp, "Library/WindowMaker/CachedPixmaps") != NULL)
+	/* If created, then set some flags */
+	if (wapp->app_icon) {
+		WWindow *mainw = wapp->main_window_desc;
+
+		wapp->app_icon->running = 1;
+		wapp->app_icon->icon->force_paint = 1;
+		wapp->app_icon->icon->owner = mainw;
+		if (mainw->wm_hints && (mainw->wm_hints->flags & IconWindowHint))
+			wapp->app_icon->icon->icon_win = mainw->wm_hints->icon_window;
+
+		wAppIconPaint(wapp->app_icon);
 		wAppIconSave(wapp->app_icon);
+	} else {
+		wapp->app_icon = wAppIconCreate(wapp->main_window_desc);
+	}
 }
diff --git a/src/appicon.h b/src/appicon.h
index 774c3e3..e401696 100644
--- a/src/appicon.h
+++ b/src/appicon.h
@@ -70,17 +70,16 @@ typedef struct WAppIcon {
 } WAppIcon;
 
 WAppIcon *wAppIconCreate(WWindow *leader_win);
-WAppIcon * wAppIconCreateForDock(WScreen *scr, char *command, char *wm_instance,
-				 char *wm_class, int tile);
+WAppIcon *wAppIconCreateForDock(WScreen *scr, char *command, char *wm_instance,
+				char *wm_class, int tile);
 
+void app_icon_create_from_docks(WWindow *wwin, WApplication *wapp, Window main_window);
 void wAppIconDestroy(WAppIcon *aicon);
 void wAppIconPaint(WAppIcon *aicon);
 void wAppIconMove(WAppIcon *aicon, int x, int y);
 void makeAppIconFor(WApplication * wapp);
 void removeAppIconFor(WApplication * wapp);
-void save_app_icon(WWindow *wwin, WApplication *wapp);
+void save_app_icon(WApplication *wapp);
 void paint_app_icon(WApplication *wapp);
-
-Bool wAppIconChangeImage(WAppIcon *icon, char *file);
-Bool wAppIconSave(WAppIcon *aicon);
+void wAppIconSave(WAppIcon *aicon);
 #endif
diff --git a/src/application.c b/src/application.c
index ca47203..97943e9 100644
--- a/src/application.c
+++ b/src/application.c
@@ -21,9 +21,7 @@
 #include "wconfig.h"
 
 #include <X11/Xlib.h>
-
 #include <string.h>
-#include <unistd.h>
 
 #include "WindowMaker.h"
 #include "menu.h"
@@ -31,12 +29,10 @@
 #ifdef USER_MENU
 #include "usermenu.h"
 #endif				/* USER_MENU */
-#include "appicon.h"
 #include "application.h"
 #include "appmenu.h"
+#include "appicon.h"
 #include "properties.h"
-#include "stacking.h"
-#include "actions.h"
 #include "workspace.h"
 #include "dock.h"
 
@@ -44,8 +40,6 @@
 
 extern XContext wAppWinContext;
 extern XContext wWinContext;
-extern WPreferences wPreferences;
-extern WDDomain *WDWindowAttributes;
 
 /******** Local variables ********/
 
@@ -83,145 +77,6 @@ WApplication *wApplicationOf(Window window)
 	return wapp;
 }
 
-static WAppIcon *findDockIconFor(WDock * dock, Window main_window)
-{
-	WAppIcon *aicon = NULL;
-
-	aicon = wDockFindIconForWindow(dock, main_window);
-	if (!aicon) {
-		wDockTrackWindowLaunch(dock, main_window);
-		aicon = wDockFindIconForWindow(dock, main_window);
-	}
-	return aicon;
-}
-
-static void extractIcon(WWindow * wwin)
-{
-	char *progname;
-
-	/* Get the application name */
-	progname = GetProgramNameForWindow(wwin->client_win);
-	if (progname) {
-		/* Save the icon path if the application is ".app" */
-		wApplicationExtractDirPackIcon(wwin->screen_ptr, progname, wwin->wm_instance, wwin->wm_class);
-		wfree(progname);
-	}
-}
-
-void wApplicationSaveIconPathFor(char *iconPath, char *wm_instance, char *wm_class)
-{
-	WMPropList *dict = WDWindowAttributes->dictionary;
-	WMPropList *adict, *key, *iconk;
-	WMPropList *val;
-	char *tmp;
-
-	tmp = get_name_for_instance_class(wm_instance, wm_class);
-	key = WMCreatePLString(tmp);
-	wfree(tmp);
-
-	adict = WMGetFromPLDictionary(dict, key);
-	iconk = WMCreatePLString("Icon");
-
-	if (adict) {
-		val = WMGetFromPLDictionary(adict, iconk);
-	} else {
-		/* no dictionary for app, so create one */
-		adict = WMCreatePLDictionary(NULL, NULL);
-		WMPutInPLDictionary(dict, key, adict);
-		WMReleasePropList(adict);
-		val = NULL;
-	}
-
-	if (!val) {
-		val = WMCreatePLString(iconPath);
-		WMPutInPLDictionary(adict, iconk, val);
-		WMReleasePropList(val);
-	} else {
-		val = NULL;
-	}
-
-	WMReleasePropList(key);
-	WMReleasePropList(iconk);
-
-	if (val && !wPreferences.flags.noupdates)
-		UpdateDomainFile(WDWindowAttributes);
-}
-
-/* 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
- */
-void wApplicationExtractDirPackIcon(WScreen * scr, char *path, char *wm_instance, char *wm_class)
-{
-	char *iconPath = NULL;
-	char *tmp = NULL;
-
-	if (strstr(path, ".app")) {
-		tmp = wmalloc(strlen(path) + 16);
-
-		if (scr->flags.supports_tiff) {
-			strcpy(tmp, path);
-			strcat(tmp, ".tiff");
-			if (access(tmp, R_OK) == 0)
-				iconPath = tmp;
-		}
-
-		if (!iconPath) {
-			strcpy(tmp, path);
-			strcat(tmp, ".xpm");
-			if (access(tmp, R_OK) == 0)
-				iconPath = tmp;
-		}
-
-		if (!iconPath)
-			wfree(tmp);
-
-		if (iconPath) {
-			wApplicationSaveIconPathFor(iconPath, wm_instance, wm_class);
-			wfree(iconPath);
-		}
-	}
-}
-
-static void app_icon_create_from_docks(WWindow *wwin, WApplication *wapp, Window main_window)
-{
-	WScreen *scr = wwin->screen_ptr;
-
-	if (scr->last_dock)
-		wapp->app_icon = findDockIconFor(scr->last_dock, main_window);
-
-	/* check main dock if we did not find it in last dock */
-	if (!wapp->app_icon && scr->dock)
-		wapp->app_icon = findDockIconFor(scr->dock, main_window);
-
-	/* finally check clips */
-	if (!wapp->app_icon) {
-		int i;
-		for (i = 0; i < scr->workspace_count; i++) {
-			WDock *dock = scr->workspaces[i]->clip;
-			if (dock)
-				wapp->app_icon = findDockIconFor(dock, main_window);
-			if (wapp->app_icon)
-				break;
-		}
-	}
-
-	/* If created, then set some flags */
-	if (wapp->app_icon) {
-		WWindow *mainw = wapp->main_window_desc;
-
-		wapp->app_icon->running = 1;
-		wapp->app_icon->icon->force_paint = 1;
-		wapp->app_icon->icon->owner = mainw;
-		if (mainw->wm_hints && (mainw->wm_hints->flags & IconWindowHint))
-			wapp->app_icon->icon->icon_win = mainw->wm_hints->icon_window;
-
-		wAppIconPaint(wapp->app_icon);
-		wAppIconSave(wapp->app_icon);
-	} else {
-		wapp->app_icon = wAppIconCreate(wapp->main_window_desc);
-	}
-}
-
 WApplication *wApplicationCreate(WWindow * wwin)
 {
 	WScreen *scr = wwin->screen_ptr;
@@ -269,8 +124,6 @@ WApplication *wApplicationCreate(WWindow * wwin)
 	wapp->main_window_desc->fake_group = wwin->fake_group;
 	wapp->main_window_desc->net_icon_image = RRetainImage(wwin->net_icon_image);
 
-	extractIcon(wapp->main_window_desc);
-
 	leader = wWindowFor(main_window);
 	if (leader)
 		leader->main_window = main_window;
@@ -290,20 +143,17 @@ WApplication *wApplicationCreate(WWindow * wwin)
 	/* application descriptor */
 	XSaveContext(dpy, main_window, wAppWinContext, (XPointer) wapp);
 
-	/* Create the application icon */
-	wapp->app_icon = NULL;
-	if (!WFLAGP(wapp->main_window_desc, no_appicon)) {
-		/* Create the application icon using the icon from docks
-		 * If not found in docks, create a new icon 
-		 * using the function wAppIconCreate() */
-		app_icon_create_from_docks(wwin, wapp, main_window);
+	/* Create the application icon using the icon from docks
+	 * If not found in docks, create a new icon 
+	 * using the function wAppIconCreate() */
+	app_icon_create_from_docks(wwin, wapp, main_window);
 
-		/* Now, paint the icon */
-		paint_app_icon(wapp);
+	/* Save the app_icon in a file */
+	save_app_icon(wapp);
 
-		/* Save the app_icon in a file */
-		save_app_icon(wwin, wapp);
-	}
+	/* Now, paint the icon */
+	if (!WFLAGP(wapp->main_window_desc, no_appicon))
+		paint_app_icon(wapp);
 
 	return wapp;
 }
@@ -381,4 +231,3 @@ void wApplicationDeactivate(WApplication *wapp)
 	}
 #endif
 }
-
diff --git a/src/application.h b/src/application.h
index 31cbe68..1a95999 100644
--- a/src/application.h
+++ b/src/application.h
@@ -48,10 +48,6 @@ typedef struct WApplication {
 WApplication *wApplicationCreate(struct WWindow *wwin);
 WApplication *wApplicationOf(Window window);
 void wApplicationDestroy(WApplication *wapp);
-void wApplicationExtractDirPackIcon(WScreen *scr,char *path, char *wm_instance,
-                                    char *wm_class);
-void wApplicationSaveIconPathFor(char *iconPath, char *wm_instance,
-				 char *wm_class);
 
 void wAppBounce(WApplication *);
 void wAppBounceWhileUrgent(WApplication *);
diff --git a/src/icon.c b/src/icon.c
index 59161d3..a270a35 100644
--- a/src/icon.c
+++ b/src/icon.c
@@ -469,7 +469,7 @@ static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
 
 /*
  * wIconStore--
- * 	Stores the client supplied icon at ~/GNUstep/Library/WindowMaker/CachedPixmaps
+ * 	Stores the client supplied icon at CACHE_ICON_PATH
  * and returns the path for that icon. Returns NULL if there is no
  * client supplied icon or on failure.
  *
diff --git a/src/misc.c b/src/misc.c
index fd283c2..389ad00 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -1094,9 +1094,3 @@ char *GetCommandForWindow(Window win)
 {
 	return getCommandForWindow(win, 0);
 }
-
-/* Free result when done */
-char *GetProgramNameForWindow(Window win)
-{
-	return getCommandForWindow(win, 1);
-}
-- 
1.7.10

Reply via email to