Integrate test_header in test_view_mail and various coding style modifications
[rmail.git] / src / utils / icons_utils.c
blob23ccedcd815793728da7c98e6bc43ef4314f944b
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <errno.h>
7 /* Default Pixmap path */
8 #ifndef PIXMAP_PATH
9 #define PIXMAP_PATH "/usr/share/applets/pixmaps/"
10 #define PIXMAP_DEFAULT "/usr/share/applets/pixmaps/mail.png"
11 #endif
13 static char *make_pixmap_path(char *dest, char *src, size_t n)
15 char *icon = NULL;
16 char md5_str[16 * 2 + 1];
17 char *p;
18 size_t j;
20 /* PIXMAP_PATH + MD5_STR + '.png' + '\0' */
21 const size_t i = strlen(PIXMAP_PATH) + (2 * 16) + 4 + 1;
23 if (dest) {
24 if (n < i) {
25 fprintf(stderr, "%s - Buffer too short! [%lu/%lu]\n",
26 __func__, (unsigned long) n, (unsigned long) i);
27 return NULL;
29 icon = dest;
30 } else {
31 icon = malloc(i);
32 if (!icon) {
33 fprintf(stderr, "%s - MALLOC %s\n", __func__, strerror(errno));
34 return NULL;
38 memset(md5_str, 0, 16*2 +1);
39 p = md5_str;
40 for (j = 0; j < 16; j++) {
41 snprintf(p, 3, "%02x", (unsigned char) src[j]);
42 p += 2;
44 snprintf(icon, i, "%s%s.png", PIXMAP_PATH, md5_str);
45 #if DEBUG
46 fprintf(stderr, "%s - PIXMAP_PATH:\"%s\"\n", __func__, PIXMAP_PATH);
47 #endif
48 return icon;
51 char *icon_get_path(char *filename)
53 char *icon = NULL;
55 icon = make_pixmap_path(NULL, filename, 0);
56 if (icon) {
57 if (access(icon, F_OK | R_OK) != 0) {
58 #if DEBUG
59 fprintf(stderr, "%s - ACCESS:%s %s\n", __func__, icon, strerror(errno));
60 #endif
61 free(icon);
62 icon = PIXMAP_DEFAULT;
66 #if DEBUG
67 fprintf(stderr, "%s - icon:\"%s\"\n", __func__, icon);
68 #endif
69 return icon;
72 void icon_free_path(char *icon)
74 if ((icon) && (icon != (char *) &PIXMAP_DEFAULT)) {
75 free(icon);
76 icon = NULL;