>From aeaa522a6585530f1cc286b4aaf9e95ebaed6998 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?"Rodolfo=20Garc=C3=ADa=20Pe=C3=B1as=20(kix)"?= <[email protected]>
Date: Wed, 6 Jun 2012 10:07:06 +0200
Subject: [PATCH 04/10] Function get_name_for_icon splitted
The function get_name_for_icon returns now the name of the icon,
without the full icon path and without extension (.xpm). Now is
not static.
The full path, including the folder creation, is done now by
create_path(). This function can be used to create folder structures.
This function is much better, because supports "infinite" folders,
not like the old get_name_for_icon which could only create the
specific folder for Cache Icons.
Using this functions, wIconStore() do the same work, but in a better
way, more clear.
---
src/icon.c | 104 ++++++++++++++++++++++++++++++++++++++++--------------------
src/icon.h | 1 +
2 files changed, 70 insertions(+), 35 deletions(-)
diff --git a/src/icon.c b/src/icon.c
index 57e1cfe..36b104b 100644
--- a/src/icon.c
+++ b/src/icon.c
@@ -398,57 +398,78 @@ Bool wIconChangeImageFile(WIcon * icon, char *file)
return !error;
}
-static char *get_name_for_icon(WWindow * wwin)
+char *get_name_for_icon(WWindow *wwin)
{
- char *prefix, *suffix;
- char *path;
+ char *suffix;
int len;
if (wwin->wm_class && wwin->wm_instance) {
- int len = strlen(wwin->wm_class) + strlen(wwin->wm_instance) +
2;
+ len = strlen(wwin->wm_class) + strlen(wwin->wm_instance) + 2;
suffix = wmalloc(len);
snprintf(suffix, len, "%s.%s", wwin->wm_instance,
wwin->wm_class);
} else if (wwin->wm_class) {
- int len = strlen(wwin->wm_class) + 1;
+ len = strlen(wwin->wm_class) + 1;
suffix = wmalloc(len);
snprintf(suffix, len, "%s", wwin->wm_class);
} else if (wwin->wm_instance) {
- int len = strlen(wwin->wm_instance) + 1;
+ len = strlen(wwin->wm_instance) + 1;
suffix = wmalloc(len);
snprintf(suffix, len, "%s", wwin->wm_instance);
} else {
return NULL;
}
- prefix = wusergnusteppath();
- len = strlen(prefix) + 64 + strlen(suffix);
- path = wmalloc(len + 1);
- snprintf(path, len, "%s/Library/WindowMaker", prefix);
-
- if (access(path, F_OK) != 0) {
- if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR)) {
- werror(_("could not create directory %s"), path);
- wfree(path);
- wfree(suffix);
- return NULL;
+ return suffix;
+}
+
+/* Check if the path exists, else, create it */
+static int create_path(char *path)
+{
+ char *tpath, *fpath, *lpath, *ldir;
+ int len = 0;
+
+ if (access(path, F_OK) == 0)
+ return 0;
+
+ tpath = fpath = NULL;
+ lpath = wstrdup(path);
+ ldir = strtok(lpath, "/");
+
+ do {
+ /* First step? */
+ if (!fpath) {
+ fpath = wrealloc(NULL, strlen(ldir) + 2);
+ sprintf(fpath, "/%s", ldir);
+ } else {
+ len = strlen(fpath) + strlen(ldir) + 2;
+ tpath = malloc(len);
+ snprintf(tpath, len, "%s", fpath);
+
+ fpath = wrealloc(fpath, len);
+ if (!fpath) {
+ wwarning(_("out of memory during expansion of
\"%%w\""));
+ wfree(tpath);
+ wfree(lpath);
+ return -1;
+ }
+
+ snprintf(fpath, len, "%s/%s", tpath, ldir);
+ wfree(tpath);
}
- }
- strcat(path, "/CachedPixmaps");
- if (access(path, F_OK) != 0) {
- if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
- werror(_("could not create directory %s"), path);
- wfree(path);
- wfree(suffix);
- return NULL;
+
+ if (access(fpath, F_OK) != 0) {
+ if (mkdir(fpath, S_IRUSR | S_IWUSR | S_IXUSR)) {
+ werror(_("could not create directory %s"),
fpath);
+ wfree(fpath);
+ wfree(lpath);
+ return -1;
+ }
}
- }
+ } while ((ldir = strtok(NULL, "/")) != NULL);
- strcat(path, "/");
- strcat(path, suffix);
- strcat(path, ".xpm");
- wfree(suffix);
+ wfree(lpath);
- return path;
+ return 0;
}
static char *get_icon_cache_path(void)
@@ -497,20 +518,33 @@ static RImage *get_wwindow_image_from_wmhints(WWindow
*wwin, WIcon *icon)
*/
char *wIconStore(WIcon * icon)
{
- char *path;
+ char *path, *dir_path, *file;
+ int len = 0;
RImage *image = NULL;
WWindow *wwin = icon->owner;
if (!wwin)
return NULL;
- path = get_name_for_icon(wwin);
- if (!path)
+ dir_path = get_icon_cache_path();
+ if (!dir_path)
+ return NULL;
+
+ file = get_name_for_icon(wwin);
+ if (!file) {
+ wfree(dir_path);
return NULL;
+ }
+
+ len = strlen(dir_path) + strlen(file) + 6;
+ path = wmalloc(len);
+ snprintf(path, len, "%s/%s.xpm", dir_path, file);
+ wfree(dir_path);
+ wfree(file);
/* If icon exists, exit */
if (access(path, F_OK) == 0)
- return path;
+ return path;
if (wwin->net_icon_image)
image = RRetainImage(wwin->net_icon_image);
diff --git a/src/icon.h b/src/icon.h
index 09b4190..7da3ef9 100644
--- a/src/icon.h
+++ b/src/icon.h
@@ -68,6 +68,7 @@ Bool wIconChangeImageFile(WIcon *icon, char *file);
RImage * wIconValidateIconSize(WScreen *scr, RImage *icon, int max_size);
char * wIconStore(WIcon *icon);
+char *get_name_for_icon(WWindow *wwin);
#ifdef NEWAPPICON
void wIconSetHighlited(WIcon *icon, Bool flag);
--
1.7.10
--
||// //\\// Rodolfo "kix" Garcia
||\\// //\\ http://www.kix.es/
>From aeaa522a6585530f1cc286b4aaf9e95ebaed6998 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?"Rodolfo=20Garc=C3=ADa=20Pe=C3=B1as=20(kix)"?= <[email protected]>
Date: Wed, 6 Jun 2012 10:07:06 +0200
Subject: [PATCH 04/10] Function get_name_for_icon splitted
The function get_name_for_icon returns now the name of the icon,
without the full icon path and without extension (.xpm). Now is
not static.
The full path, including the folder creation, is done now by
create_path(). This function can be used to create folder structures.
This function is much better, because supports "infinite" folders,
not like the old get_name_for_icon which could only create the
specific folder for Cache Icons.
Using this functions, wIconStore() do the same work, but in a better
way, more clear.
---
src/icon.c | 104 ++++++++++++++++++++++++++++++++++++++++--------------------
src/icon.h | 1 +
2 files changed, 70 insertions(+), 35 deletions(-)
diff --git a/src/icon.c b/src/icon.c
index 57e1cfe..36b104b 100644
--- a/src/icon.c
+++ b/src/icon.c
@@ -398,57 +398,78 @@ Bool wIconChangeImageFile(WIcon * icon, char *file)
return !error;
}
-static char *get_name_for_icon(WWindow * wwin)
+char *get_name_for_icon(WWindow *wwin)
{
- char *prefix, *suffix;
- char *path;
+ char *suffix;
int len;
if (wwin->wm_class && wwin->wm_instance) {
- int len = strlen(wwin->wm_class) + strlen(wwin->wm_instance) + 2;
+ len = strlen(wwin->wm_class) + strlen(wwin->wm_instance) + 2;
suffix = wmalloc(len);
snprintf(suffix, len, "%s.%s", wwin->wm_instance, wwin->wm_class);
} else if (wwin->wm_class) {
- int len = strlen(wwin->wm_class) + 1;
+ len = strlen(wwin->wm_class) + 1;
suffix = wmalloc(len);
snprintf(suffix, len, "%s", wwin->wm_class);
} else if (wwin->wm_instance) {
- int len = strlen(wwin->wm_instance) + 1;
+ len = strlen(wwin->wm_instance) + 1;
suffix = wmalloc(len);
snprintf(suffix, len, "%s", wwin->wm_instance);
} else {
return NULL;
}
- prefix = wusergnusteppath();
- len = strlen(prefix) + 64 + strlen(suffix);
- path = wmalloc(len + 1);
- snprintf(path, len, "%s/Library/WindowMaker", prefix);
-
- if (access(path, F_OK) != 0) {
- if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR)) {
- werror(_("could not create directory %s"), path);
- wfree(path);
- wfree(suffix);
- return NULL;
+ return suffix;
+}
+
+/* Check if the path exists, else, create it */
+static int create_path(char *path)
+{
+ char *tpath, *fpath, *lpath, *ldir;
+ int len = 0;
+
+ if (access(path, F_OK) == 0)
+ return 0;
+
+ tpath = fpath = NULL;
+ lpath = wstrdup(path);
+ ldir = strtok(lpath, "/");
+
+ do {
+ /* First step? */
+ if (!fpath) {
+ fpath = wrealloc(NULL, strlen(ldir) + 2);
+ sprintf(fpath, "/%s", ldir);
+ } else {
+ len = strlen(fpath) + strlen(ldir) + 2;
+ tpath = malloc(len);
+ snprintf(tpath, len, "%s", fpath);
+
+ fpath = wrealloc(fpath, len);
+ if (!fpath) {
+ wwarning(_("out of memory during expansion of \"%%w\""));
+ wfree(tpath);
+ wfree(lpath);
+ return -1;
+ }
+
+ snprintf(fpath, len, "%s/%s", tpath, ldir);
+ wfree(tpath);
}
- }
- strcat(path, "/CachedPixmaps");
- if (access(path, F_OK) != 0) {
- if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
- werror(_("could not create directory %s"), path);
- wfree(path);
- wfree(suffix);
- return NULL;
+
+ if (access(fpath, F_OK) != 0) {
+ if (mkdir(fpath, S_IRUSR | S_IWUSR | S_IXUSR)) {
+ werror(_("could not create directory %s"), fpath);
+ wfree(fpath);
+ wfree(lpath);
+ return -1;
+ }
}
- }
+ } while ((ldir = strtok(NULL, "/")) != NULL);
- strcat(path, "/");
- strcat(path, suffix);
- strcat(path, ".xpm");
- wfree(suffix);
+ wfree(lpath);
- return path;
+ return 0;
}
static char *get_icon_cache_path(void)
@@ -497,20 +518,33 @@ static RImage *get_wwindow_image_from_wmhints(WWindow *wwin, WIcon *icon)
*/
char *wIconStore(WIcon * icon)
{
- char *path;
+ char *path, *dir_path, *file;
+ int len = 0;
RImage *image = NULL;
WWindow *wwin = icon->owner;
if (!wwin)
return NULL;
- path = get_name_for_icon(wwin);
- if (!path)
+ dir_path = get_icon_cache_path();
+ if (!dir_path)
+ return NULL;
+
+ file = get_name_for_icon(wwin);
+ if (!file) {
+ wfree(dir_path);
return NULL;
+ }
+
+ len = strlen(dir_path) + strlen(file) + 6;
+ path = wmalloc(len);
+ snprintf(path, len, "%s/%s.xpm", dir_path, file);
+ wfree(dir_path);
+ wfree(file);
/* If icon exists, exit */
if (access(path, F_OK) == 0)
- return path;
+ return path;
if (wwin->net_icon_image)
image = RRetainImage(wwin->net_icon_image);
diff --git a/src/icon.h b/src/icon.h
index 09b4190..7da3ef9 100644
--- a/src/icon.h
+++ b/src/icon.h
@@ -68,6 +68,7 @@ Bool wIconChangeImageFile(WIcon *icon, char *file);
RImage * wIconValidateIconSize(WScreen *scr, RImage *icon, int max_size);
char * wIconStore(WIcon *icon);
+char *get_name_for_icon(WWindow *wwin);
#ifdef NEWAPPICON
void wIconSetHighlited(WIcon *icon, Bool flag);
--
1.7.10