Use Tango as default test theme.
[framework/uifw/efreet.git] / src / bin / ef_icon_theme.c
1 #include "Efreet.h"
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <Ecore_File.h>
6
7 #ifndef PATH_MAX
8 #define PATH_MAX 4096
9 #endif
10
11 #define SIZE 128
12 #define THEME "Tango"
13 #define FREE(x) do { free(x); x = NULL; } while (0);
14
15 static void ef_icon_theme_themes_find(const char *search_dir,
16                                         Ecore_Hash *themes);
17 static void ef_icons_find(Efreet_Icon_Theme *theme, Ecore_List *themes,
18                                                     Ecore_Hash *icons);
19 static void ef_read_dir(const char *dir, Ecore_Hash *icons);
20
21 int
22 ef_cb_efreet_icon_theme(void)
23 {
24     int ret = 1;
25     const char *tmp;
26
27     unsetenv("XDG_DATA_HOME");
28     efreet_shutdown();
29     putenv("HOME=/var/tmp");
30     efreet_init();
31
32     tmp = efreet_icon_user_dir_get();
33     if (strcmp(tmp, "/var/tmp/.local/share/icons"))
34     {
35         printf("efreet_icon_user_dir_get() returned incorrect "
36                 "value (%s) on HOME=/var/tmp\n", tmp);
37         ret = 0;
38     }
39
40     efreet_shutdown();
41     unsetenv("HOME");
42     efreet_init();
43
44     tmp = efreet_icon_user_dir_get();
45     if (strcmp(tmp, "/tmp/.local/share/icons"))
46     {
47         printf("efreet_icon_user_dir_get() returned incorrect "
48                 "value (%s) on HOME=\n", tmp);
49         ret = 0;
50     }
51
52     return ret;
53 }
54
55 int
56 ef_cb_efreet_icon_theme_list(void)
57 {
58     int ret = 1;
59     Ecore_List *themes;
60     Ecore_Hash *dirs;
61     Efreet_Icon_Theme *theme;
62     Ecore_List *icon_dirs;
63     const char *dir;
64     char buf[PATH_MAX];
65
66     dirs = ecore_hash_new(ecore_str_hash, ecore_str_compare);
67     ecore_hash_free_key_cb_set(dirs, free);
68
69     icon_dirs = efreet_data_dirs_get();
70     ecore_list_first_goto(icon_dirs);
71
72     ef_icon_theme_themes_find(efreet_icon_user_dir_get(), dirs);
73     while ((dir = ecore_list_next(icon_dirs)))
74     {
75         snprintf(buf, sizeof(buf), "%s/icons", dir);
76         ef_icon_theme_themes_find(buf, dirs);
77     }
78     ef_icon_theme_themes_find("/usr/share/pixmaps", dirs);
79
80     themes = efreet_icon_theme_list_get();
81     ecore_list_first_goto(themes);
82     while ((theme = ecore_list_next(themes)))
83     {
84         if (ecore_hash_get(dirs, theme->name.internal))
85             ecore_hash_remove(dirs, theme->name.internal);
86         else
87         {
88             printf("efreet_icon_theme_list_get() returned %s which we didn't "
89                     "see when scanning the directories.\n", theme->name.internal);
90             ret = 0;
91         }
92     }
93     ecore_list_destroy(themes);
94
95     themes = ecore_hash_keys(dirs);
96     if (ecore_list_count(themes) > 0)
97     {
98         char *dir;
99
100         printf("efreet_icon_theme_list_get() missed: ");
101         ecore_list_first_goto(themes);
102         while ((dir = ecore_list_next(themes)))
103             printf("%s ", dir);
104         printf("\n");
105
106         ret = 0;
107     }
108     ecore_list_destroy(themes);
109     ecore_hash_destroy(dirs);
110
111     return ret;
112 }
113
114 static void
115 ef_icon_theme_themes_find(const char *search_dir, Ecore_Hash *themes)
116 {
117     Ecore_List *dirs;
118     char *dir;
119
120     if (!search_dir || !themes) return;
121
122     dirs = ecore_file_ls(search_dir);
123     if (!dirs) return;
124
125     while ((dir = ecore_list_first_remove(dirs)))
126     {
127         char p[PATH_MAX];
128
129         /* if we've already added the theme we're done */
130         if (ecore_hash_get(themes, dir))
131         {
132             free(dir);
133             continue;
134         }
135
136         /* if the index.theme file exists we open it and look for the hidden
137          * flag. */
138         snprintf(p, sizeof(p), "%s/%s/index.theme", search_dir, dir);
139         if (ecore_file_exists(p))
140         {
141             Efreet_Ini *ini;
142             char *d;
143             int skip = 0;
144
145             ini = efreet_ini_new(p);
146             efreet_ini_section_set(ini, "Icon Theme");
147
148             if (efreet_ini_boolean_get(ini, "Hidden")) skip = 1;
149             if (!efreet_ini_localestring_get(ini, "Name")) skip = 1;
150             efreet_ini_free(ini);
151
152             if (!skip)
153             {
154                 d = strdup(dir);
155                 ecore_hash_set(themes, d, d);
156             }
157         }
158         free(dir);
159     }
160     ecore_list_destroy(dirs);
161 }
162
163 const char *icons[] =
164 {
165     "address-book-new",
166     "application-exit",
167     "appointment-new",
168     "contact-new",
169     "dialog-apply",
170     "dialog-cancel",
171     "dialog-close",
172     "dialog-ok",
173     "document-new",
174     "document-open",
175     "document-open-recent",
176     "document-page-setup",
177     "document-print",
178     "document-print-preview",
179     "document-properties",
180     "document-revert",
181     "document-save",
182     "document-save-as",
183     "edit-copy",
184     "edit-cut",
185     "edit-delete",
186     "edit-find",
187     "edit-find-replace",
188     "edit-paste",
189     "edit-redo",
190     "edit-select-all",
191     "edit-undo",
192     "format-indent-less",
193     "format-indent-more",
194     "format-justify-center",
195     "format-justify-fill",
196     "format-justify-left",
197     "format-justify-right",
198     "format-text-direction-ltr",
199     "format-text-direction-rtl",
200     "format-text-bold",
201     "format-text-italic",
202     "format-text-underline",
203     "format-text-strikethrough",
204     "go-bottom",
205     "go-down",
206     "go-first",
207     "go-home",
208     "go-jump",
209     "go-last",
210     "go-next",
211     "go-previous",
212     "go-top",
213     "go-up",
214     "help-about",
215     "help-contents",
216     "help-faq",
217     "insert-image",
218     "insert-link",
219     "insert-object",
220     "insert-text",
221     "list-add",
222     "list-remove",
223     "mail-forward",
224     "mail-mark-important",
225     "mail-mark-junk",
226     "mail-mark-notjunk",
227     "mail-mark-read",
228     "mail-mark-unread",
229     "mail-message-new",
230     "mail-reply-all",
231     "mail-reply-sender",
232     "mail-send-receive",
233     "media-eject",
234     "media-playback-pause",
235     "media-playback-start",
236     "media-playback-stop",
237     "media-record",
238     "media-seek-backward",
239     "media-seek-forward",
240     "media-skip-backward",
241     "media-skip-forward",
242     "system-lock-screen",
243     "system-log-out",
244     "system-run",
245     "system-search",
246     "system-search",
247     "tools-check-spelling",
248     "view-fullscreen",
249     "view-refresh",
250     "view-sort-ascending",
251     "view-sort-descending",
252     "window-close",
253     "window-new",
254     "zoom-best-fit",
255     "zoom-in",
256     "zoom-original",
257     "zoom-out",
258     "process-working",
259     "accessories-calculator",
260     "accessories-character-map",
261     "accessories-dictionary",
262     "accessories-text-editor",
263     "help-browser",
264     "multimedia-volume-control",
265 #if 0
266     "preferences-desktop-accessibility",
267     "preferences-desktop-font",
268     "preferences-desktop-keyboard",
269     "preferences-desktop-locale",
270     "preferences-desktop-multimedia",
271     "preferences-desktop-screensaver",
272     "preferences-desktop-theme",
273     "preferences-desktop-wallpaper",
274     "system-file-manager",
275     "system-software-update",
276     "utilities-terminal",
277     "applications-accessories",
278     "applications-development",
279     "applications-games",
280     "applications-graphics",
281     "applications-internet",
282     "applications-multimedia",
283     "applications-office",
284     "applications-other",
285     "applications-system",
286     "applications-utilities",
287     "preferences-desktop",
288     "preferences-desktop-accessibility",
289     "preferences-desktop-peripherals",
290     "preferences-desktop-personal",
291     "preferences-other",
292     "preferences-system",
293     "preferences-system-network",
294     "system-help",
295     "audio-card",
296     "audio-input-microphone",
297     "battery",
298     "camera-photo",
299     "camera-video",
300     "computer",
301     "drive-cdrom",
302     "drive-harddisk",
303     "drive-removable-media",
304     "input-gaming",
305     "input-keyboard",
306     "input-mouse",
307     "media-cdrom",
308     "media-floppy",
309     "multimedia-player",
310     "multimedia-player",
311     "network-wired",
312     "network-wireless",
313     "printer",
314     "emblem-default",
315     "emblem-documents",
316     "emblem-downloads",
317     "emblem-favorite",
318     "emblem-important",
319     "emblem-mail",
320     "emblem-photos",
321     "emblem-readonly",
322     "emblem-shared",
323     "emblem-symbolic-link",
324     "emblem-synchronized",
325     "emblem-system",
326     "emblem-unreadable",
327     "face-angel",
328     "face-crying",
329     "face-devil-grin",
330     "face-devil-sad",
331     "face-glasses",
332     "face-kiss",
333     "face-monkey",
334     "face-plain",
335     "face-sad",
336     "face-smile",
337     "face-smile-big",
338     "face-smirk",
339     "face-surprise",
340     "face-wink",
341     "application-x-executable",
342     "audio-x-generic",
343     "font-x-generic",
344     "image-x-generic",
345     "package-x-generic",
346     "text-html",
347     "text-x-generic",
348     "text-x-generic-template",
349     "text-x-script",
350     "video-x-generic",
351     "x-office-address-book",
352     "x-office-calendar",
353     "x-office-document",
354     "x-office-presentation",
355     "x-office-spreadsheet",
356     "folder",
357     "folder-remote",
358     "network-server",
359     "network-workgroup",
360     "start-here",
361     "user-desktop",
362     "user-home",
363     "user-trash",
364     "appointment-missed",
365     "appointment-soon",
366     "audio-volume-high",
367     "audio-volume-low",
368     "audio-volume-medium",
369     "audio-volume-muted",
370     "battery-caution",
371     "battery-low",
372     "dialog-error",
373     "dialog-information",
374     "dialog-password",
375     "dialog-question",
376     "dialog-warning",
377     "folder-drag-accept",
378     "folder-open",
379     "folder-visiting",
380     "image-loading",
381     "image-missing",
382     "mail-attachment",
383     "mail-unread",
384     "mail-read",
385     "mail-replied",
386     "mail-signed",
387     "mail-signed-verified",
388     "media-playlist-repeat",
389     "media-playlist-shuffle",
390     "network-error",
391     "network-idle",
392     "network-offline",
393     "network-receive",
394     "network-transmit",
395     "network-transmit-receive",
396     "printer-error",
397     "printer-printing",
398     "software-update-available",
399     "software-update-urgent",
400     "sync-error",
401     "sync-synchronizing",
402     "task-due",
403     "task-passed-due",
404     "user-away",
405     "user-idle",
406     "user-offline",
407     "user-online",
408     "user-trash-full",
409     "weather-clear",
410     "weather-clear-night",
411     "weather-few-clouds",
412     "weather-few-clouds-night",
413     "weather-fog",
414     "weather-overcast",
415     "weather-severe-alert",
416     "weather-showers",
417     "weather-showers-scattered",
418     "weather-snow",
419     "weather-storm",
420 #endif
421     NULL
422 };
423
424 int
425 ef_cb_efreet_icon_match(void)
426 {
427     int i, ret = 1;
428     Ecore_Hash *icon_hash;
429     Efreet_Icon_Theme *theme;
430     Ecore_List *themes;
431
432     themes = efreet_icon_theme_list_get();
433     ecore_list_first_goto(themes);
434     while ((theme = ecore_list_next(themes)))
435     {
436         if (!strcmp(theme->name.internal, THEME))
437             break;
438     }
439
440     if (!theme)
441     {
442         printf("Theme not installed, SKIPPED.\n");
443         ecore_list_destroy(themes);
444         return 1;
445     }
446
447     icon_hash = ecore_hash_new(ecore_str_hash, ecore_str_compare);
448     ecore_hash_free_key_cb_set(icon_hash, free);
449     ecore_hash_free_value_cb_set(icon_hash, free);
450
451     ef_icons_find(theme, themes, icon_hash);
452     ecore_list_destroy(themes);
453
454     double start = ecore_time_get();
455     for (i = 0; icons[i] != NULL; i++)
456     {
457         char *path, *s;
458
459         path = efreet_icon_path_find(THEME, icons[i], SIZE);
460
461         if (!path)
462         {
463 #if 0
464             if (ecore_hash_get(icon_hash, icons[i]))
465             {
466                 printf("NOT FOUND %s\n", icons[i]);
467                 ret = 0;
468             }
469 #endif
470             continue;
471         }
472
473         s = strrchr(path, '.');
474         if (s) *s = '\0';
475         s = strrchr(path, '/');
476         if (s) s++;
477
478         if (s && strcmp(s, icons[i]))
479         {
480             printf("Name mismatch name (%s) vs ef (%s)\n", icons[i], s);
481             ret = 0;
482         }
483         free(path);
484     }
485     printf("Time: %f\n", (ecore_time_get() - start));
486     ecore_hash_destroy(icon_hash);
487
488     start = ecore_time_get();
489     for (i = 0; icons[i] != NULL; i++)
490     {
491         char *path, *s;
492
493         path = efreet_icon_path_find(THEME, icons[i], SIZE);
494
495         if (!path) continue;
496
497         s = strrchr(path, '.');
498         if (s) *s = '\0';
499         s = strrchr(path, '/');
500         if (s) s++;
501
502         if (s && strcmp(s, icons[i]))
503         {
504             printf("Name mismatch name (%s) vs ef (%s)\n", icons[i], s);
505             ret = 0;
506         }
507         free(path);
508     }
509     printf("Time: %f\n", (ecore_time_get() - start));
510
511     return ret;
512 }
513
514 static void
515 ef_icons_find(Efreet_Icon_Theme *theme, Ecore_List *themes, Ecore_Hash *icons)
516 {
517     char path[PATH_MAX];
518
519     if (!theme || !icons) return;
520
521     if (theme->paths.count == 1)
522     {
523         Efreet_Icon_Theme_Directory *dir;
524
525         ecore_list_first_goto(theme->directories);
526         while ((dir = ecore_list_next(theme->directories)))
527         {
528             if (theme->paths.count > 1)
529             {
530                 Ecore_List *list;
531                 char *tmp;
532
533                 list = theme->paths.path;
534                 ecore_list_first_goto(list);
535                 while ((tmp = ecore_list_next(list)))
536                 {
537                     snprintf(path, sizeof(path), "%s/%s/", tmp, dir->name);
538                     ef_read_dir(path, icons);
539                 }
540             }
541             else if (theme->paths.count == 1)
542             {
543                 snprintf(path, sizeof(path), "%s/%s/", (char *)theme->paths.path, dir->name);
544                 ef_read_dir(path, icons);
545             }
546         }
547     }
548     else if (theme->paths.count > 1)
549     {
550         const char *theme_path;
551
552         ecore_list_first_goto(theme->paths.path);
553         while ((theme_path = ecore_list_next(theme->paths.path)))
554         {
555             Efreet_Icon_Theme_Directory *dir;
556
557             ecore_list_first_goto(theme->directories);
558             while ((dir = ecore_list_next(theme->directories)))
559             {
560                 snprintf(path, sizeof(path), "%s/%s/", theme_path, dir->name);
561                 ef_read_dir(path, icons);
562             }
563         }
564     }
565
566     if (theme->inherits)
567     {
568         Efreet_Icon_Theme *parent_theme;
569         char *parent;
570
571         ecore_list_first_goto(theme->inherits);
572         while ((parent = ecore_list_next(theme->inherits)))
573         {
574             ecore_list_first_goto(themes);
575             while ((parent_theme = ecore_list_next(themes)))
576             {
577                 if (!strcmp(parent_theme->name.internal, parent))
578                     ef_icons_find(parent_theme, themes, icons);
579             }
580         }
581     }
582     else
583     {
584         Efreet_Icon_Theme *parent_theme;
585
586         ecore_list_first_goto(themes);
587         while ((parent_theme = ecore_list_next(themes)))
588         {
589             if (!strcmp(parent_theme->name.internal, "hicolor"))
590                 ef_icons_find(parent_theme, themes, icons);
591         }
592     }
593
594     ef_read_dir("/usr/share/pixmaps", icons);
595 }
596
597 static void
598 ef_read_dir(const char *dir, Ecore_Hash *icons)
599 {
600     Ecore_List *files;
601     char *file;
602
603     if (!dir || !icons) return;
604
605     files = ecore_file_ls(dir);
606     if (!files) return;
607
608     while ((file = ecore_list_first_remove(files)))
609     {
610         char *p;
611
612         p = strrchr(file, '.');
613         if (!p)
614         {
615             FREE(file);
616             continue;
617         }
618
619         if (!strcmp(p, ".png") || !strcmp(p, ".xpm"))
620         {
621             *p = '\0';
622
623             p = strrchr(file, '/');
624             if (p) p++;
625             if (p) ecore_hash_set(icons, strdup(p), strdup(p));
626         }
627
628         FREE(file);
629     }
630     ecore_list_destroy(files);
631 }