Onle one table row per WPS tag
[maemo-rb.git] / apps / plugins / theme_remove.c
blobf818ac4afe16401e1e9d43e0bc37e43468459855
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "plugin.h"
21 #include "lib/configfile.h"
23 PLUGIN_HEADER
25 /* taken from apps/gui/wps_parser.c */
26 #define WPS_DEFAULTCFG WPS_DIR "/rockbox_default.wps"
27 #define RWPS_DEFAULTCFG WPS_DIR "/rockbox_default.rwps"
29 #define CONFIG_FILENAME "theme_remove.cfg"
30 #define LOG_FILENAME "/theme_remove_log.txt"
31 #define RB_FONTS_CONFIG VIEWERS_DIR "/rockbox-fonts.config"
33 enum remove_option {
34 ALWAYS_REMOVE,
35 NEVER_REMOVE,
36 REMOVE_IF_NOT_USED,
37 ASK_FOR_REMOVAL,
38 NUM_REMOVE_OPTION
41 struct remove_setting {
42 const char *name;
43 const char *prefix, *suffix;
44 char value[MAX_PATH];
45 int option;
46 int (*func)(struct remove_setting *);
47 bool used;
50 static int remove_wps(struct remove_setting *);
51 #ifdef HAVE_LCD_BITMAP
52 static int remove_icons(struct remove_setting *setting);
53 #endif
55 enum remove_settings {
56 #ifdef HAVE_LCD_BITMAP
57 REMOVE_FONT,
58 #endif
59 REMOVE_WPS,
60 #ifdef HAVE_LCD_BITMAP
61 REMOVE_SBS,
62 #endif
63 #ifdef HAVE_REMOTE_LCD
64 REMOVE_RWPS,
65 REMOVE_RSBS,
66 #endif
67 #if LCD_DEPTH > 1
68 REMOVE_BACKDROP,
69 #endif
70 #ifdef HAVE_LCD_BITMAP
71 REMOVE_ICON,
72 REMOVE_VICON,
73 #endif
74 #ifdef HAVE_REMOTE_LCD
75 REMOVE_RICON,
76 REMOVE_RVICON,
77 #endif
78 #ifdef HAVE_LCD_COLOR
79 REMOVE_COLOURS,
80 #endif
81 NUM_REMOVE_ITEMS
84 static bool create_log = true;
85 static struct remove_setting remove_list[NUM_REMOVE_ITEMS] = {
86 #ifdef HAVE_LCD_BITMAP
87 [REMOVE_FONT] = { "font", FONT_DIR "/", ".fnt", "",
88 ASK_FOR_REMOVAL, NULL, false },
89 #endif
90 [REMOVE_WPS] = { "wps", WPS_DIR "/", ".wps", "",
91 REMOVE_IF_NOT_USED, remove_wps, false },
92 #ifdef HAVE_LCD_BITMAP
93 [REMOVE_SBS] = { "sbs", SBS_DIR "/", ".sbs", "",
94 REMOVE_IF_NOT_USED, remove_wps, false },
95 #endif
96 #ifdef HAVE_REMOTE_LCD
97 [REMOVE_RWPS] = { "rwps", WPS_DIR "/", ".rwps", "",
98 REMOVE_IF_NOT_USED, remove_wps, false },
99 [REMOVE_RSBS] = { "rsbs", SBS_DIR "/", ".rsbs", "",
100 REMOVE_IF_NOT_USED, remove_wps, false },
101 #endif
102 #if LCD_DEPTH > 1
103 [REMOVE_BACKDROP] = { "backdrop", BACKDROP_DIR "/", ".bmp", "",
104 REMOVE_IF_NOT_USED, NULL, false },
105 #endif
106 #ifdef HAVE_LCD_BITMAP
107 [REMOVE_ICON] = { "iconset", ICON_DIR "/", ".bmp", "",
108 ASK_FOR_REMOVAL, NULL, false },
109 [REMOVE_VICON] = { "viewers iconset", ICON_DIR "/", ".bmp", "",
110 ASK_FOR_REMOVAL, remove_icons, false },
111 #endif
112 #ifdef HAVE_REMOTE_LCD
113 [REMOVE_RICON] = { "remote iconset", ICON_DIR "/", ".bmp", "",
114 ASK_FOR_REMOVAL, NULL, false },
115 [REMOVE_RVICON] = { "remote viewers iconset", ICON_DIR "/", ".bmp", "",
116 ASK_FOR_REMOVAL, NULL, false },
117 #endif
118 #ifdef HAVE_LCD_COLOR
119 [REMOVE_COLOURS] = { "filetype colours", THEME_DIR "/", ".colours", "",
120 ASK_FOR_REMOVAL, NULL, false },
121 #endif
123 static char *option_names[NUM_REMOVE_OPTION] = {
124 "always", "never", "not used", "ask",
126 static struct configdata config[] = {
127 #ifdef HAVE_LCD_BITMAP
128 { TYPE_INT, 0, NUM_REMOVE_OPTION,
129 { .int_p = &remove_list[REMOVE_FONT].option },
130 "remove font", option_names },
131 #endif
132 { TYPE_INT, 0, NUM_REMOVE_OPTION,
133 { .int_p = &remove_list[REMOVE_WPS].option },
134 "remove wps", option_names },
135 #ifdef HAVE_LCD_BITMAP
136 { TYPE_INT, 0, NUM_REMOVE_OPTION,
137 { .int_p = &remove_list[REMOVE_SBS].option },
138 "remove sbs", option_names },
139 #endif
140 #ifdef HAVE_REMOTE_LCD
141 { TYPE_INT, 0, NUM_REMOVE_OPTION,
142 { .int_p = &remove_list[REMOVE_RWPS].option },
143 "remove rwps", option_names },
144 { TYPE_INT, 0, NUM_REMOVE_OPTION,
145 { .int_p = &remove_list[REMOVE_RSBS].option },
146 "remove rsbs", option_names },
147 #endif
148 #if LCD_DEPTH > 1
149 { TYPE_INT, 0, NUM_REMOVE_OPTION,
150 { .int_p = &remove_list[REMOVE_BACKDROP].option },
151 "remove backdrop", option_names },
152 #endif
153 #ifdef HAVE_LCD_BITMAP
154 { TYPE_INT, 0, NUM_REMOVE_OPTION,
155 { .int_p = &remove_list[REMOVE_ICON].option },
156 "remove iconset", option_names },
157 { TYPE_INT, 0, NUM_REMOVE_OPTION,
158 { .int_p = &remove_list[REMOVE_VICON].option },
159 "remove viconset", option_names },
160 #endif
161 #ifdef HAVE_REMOTE_LCD
162 { TYPE_INT, 0, NUM_REMOVE_OPTION,
163 { .int_p = &remove_list[REMOVE_RICON].option },
164 "remove riconset", option_names },
165 { TYPE_INT, 0, NUM_REMOVE_OPTION,
166 { .int_p = &remove_list[REMOVE_RVICON].option },
167 "remove rviconset", option_names },
168 #endif
169 #ifdef HAVE_LCD_COLOR
170 { TYPE_INT, 0, NUM_REMOVE_OPTION,
171 { .int_p = &remove_list[REMOVE_COLOURS].option },
172 "remove colours", option_names },
173 #endif
174 {TYPE_BOOL, 0, 1, { .bool_p = &create_log },
175 "create log", NULL},
177 static const int nb_config = sizeof(config)/sizeof(*config);
178 static char themefile[MAX_PATH];
179 static int log_fd = -1;
181 static int show_mess(const char *text, const char *file)
183 static char buf[MAX_PATH*2];
185 if (file)
186 rb->snprintf(buf, sizeof(buf), "%s: %s", text, file);
187 else
188 rb->snprintf(buf, sizeof(buf), "%s", text);
190 DEBUGF("%s\n", buf);
191 if (log_fd >= 0)
192 rb->fdprintf(log_fd, "%s\n", buf);
194 rb->splash(0, buf);
195 rb->sleep(HZ/4);
197 return 0;
200 /* set full path of file. */
201 static void set_file_name(char *buf, const char*file,
202 struct remove_setting *setting)
204 int len1, len2;
205 if (rb->strncasecmp(file, setting->prefix, rb->strlen(setting->prefix)))
206 rb->snprintf(buf, MAX_PATH, "%s%s", setting->prefix, file);
207 else
208 rb->strlcpy(buf, file, MAX_PATH);
209 len1 = rb->strlen(buf);
210 len2 = rb->strlen(setting->suffix);
211 if (rb->strcasecmp(buf+len1-len2, setting->suffix))
212 rb->strlcpy(&buf[len1], setting->suffix, MAX_PATH-len1);
215 /* taken from apps/onplay.c */
216 /* helper function to remove a non-empty directory */
217 static int remove_dir(char* dirname, int len)
219 int result = 0;
220 DIR* dir;
221 int dirlen = rb->strlen(dirname);
223 dir = rb->opendir(dirname);
224 if (!dir)
225 return -1; /* open error */
227 while (true)
229 struct dirent* entry;
230 /* walk through the directory content */
231 entry = rb->readdir(dir);
232 if (!entry)
233 break;
235 dirname[dirlen] ='\0';
237 /* append name to current directory */
238 rb->snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
239 if (entry->attribute & ATTR_DIRECTORY)
241 /* remove a subdirectory */
242 if (!rb->strcmp((char *)entry->d_name, ".") ||
243 !rb->strcmp((char *)entry->d_name, ".."))
244 continue; /* skip these */
246 result = remove_dir(dirname, len); /* recursion */
247 if (result)
248 break;
250 else
252 /* remove a file */
253 result = rb->remove(dirname);
255 if (ACTION_STD_CANCEL == rb->get_action(CONTEXT_STD, TIMEOUT_NOBLOCK))
257 show_mess("Canceled", NULL);
258 result = -1;
259 break;
262 rb->closedir(dir);
264 if (!result)
265 { /* remove the now empty directory */
266 dirname[dirlen] = '\0'; /* terminate to original length */
268 result = rb->rmdir(dirname);
269 show_mess("Removed", dirname);
272 return result;
275 static int remove_wps(struct remove_setting *setting)
277 char bmpdir[MAX_PATH];
278 char *p;
279 rb->strcpy(bmpdir, setting->value);
280 p = rb->strrchr(bmpdir, '.');
281 if (p) *p = 0;
282 if (!rb->dir_exists(bmpdir))
283 return 0;
284 return remove_dir(bmpdir, MAX_PATH);
287 #ifdef HAVE_LCD_BITMAP
288 static int remove_icons(struct remove_setting *setting)
290 char path[MAX_PATH];
291 char *p;
292 rb->strcpy(path, setting->value);
293 p = rb->strrchr(path, '.');
294 rb->strlcpy(p, ".icons", path+MAX_PATH-p);
296 if (!rb->file_exists(path))
298 return 0;
300 if (rb->remove(path))
302 show_mess("Failed", path);
303 return 1;
305 show_mess("Removed", path);
306 return 0;
308 #endif
310 #ifdef HAVE_LCD_BITMAP
311 static char font_file[MAX_PATH];
312 #endif
313 static bool is_deny_file(const char *file)
315 const char *deny_files[] = {
316 WPS_DEFAULTCFG,
317 RWPS_DEFAULTCFG,
318 #ifdef HAVE_LCD_BITMAP
319 font_file,
320 #endif
321 NULL
323 const char **p = deny_files;
324 while ( *p )
326 if (!rb->strcmp(file, *p))
327 return true;
328 p++;
330 return false;
333 static void check_whether_used_in_setting(void)
335 const char *setting_files[] = {
336 #ifdef HAVE_LCD_BITMAP
337 rb->global_settings->font_file,
338 #endif
339 rb->global_settings->wps_file,
340 #ifdef HAVE_LCD_BITMAP
341 rb->global_settings->sbs_file,
342 #endif
343 #ifdef HAVE_REMOTE_LCD
344 rb->global_settings->rwps_file,
345 rb->global_settings->rsbs_file,
346 #endif
347 #if LCD_DEPTH > 1
348 rb->global_settings->backdrop_file,
349 #endif
350 #ifdef HAVE_LCD_BITMAP
351 rb->global_settings->icon_file,
352 rb->global_settings->viewers_icon_file,
353 #endif
354 #ifdef HAVE_REMOTE_LCD
355 rb->global_settings->remote_icon_file,
356 rb->global_settings->remote_viewers_icon_file,
357 #endif
358 #ifdef HAVE_LCD_COLOR
359 rb->global_settings->colors_file,
360 #endif
362 char tempfile[MAX_PATH];
363 int i;
364 for (i=0; i<NUM_REMOVE_ITEMS; i++)
366 struct remove_setting *setting = &remove_list[i];
367 if (setting->value[0])
369 set_file_name(tempfile, setting_files[i], setting);
370 if (!rb->strcasecmp(tempfile, setting->value))
371 setting->used = true;
375 static void check_whether_used_in_file(const char *cfgfile)
377 char line[MAX_PATH];
378 char settingfile[MAX_PATH];
379 char *p;
380 int fd;
381 char *name, *value;
382 int i;
384 if (!rb->strcasecmp(themefile, cfgfile))
385 return;
386 fd = rb->open(cfgfile, O_RDONLY);
387 if (fd < 0)
388 return;
389 while (rb->read_line(fd, line, sizeof(line)) > 0)
391 if (!rb->settings_parseline(line, &name, &value))
392 continue;
393 /* remove trailing spaces. */
394 p = value+rb->strlen(value)-1;
395 while (*p == ' ') *p-- = 0;
396 if (*value == 0 || !rb->strcmp(value, "-"))
397 continue;
398 for (i=0; i<NUM_REMOVE_ITEMS; i++)
400 struct remove_setting *setting = &remove_list[i];
401 if (!rb->strcmp(name, setting->name))
403 if (setting->value[0])
405 set_file_name(settingfile, value, setting);
406 if (!rb->strcasecmp(settingfile, setting->value))
407 setting->used = true;
409 break;
413 rb->close(fd);
415 static void check_whether_used(void)
417 char cfgfile[MAX_PATH];
418 DIR *dir;
420 check_whether_used_in_setting();
421 #ifdef HAVE_LCD_BITMAP
422 /* mark font files come from rockbox-font.zip as used and don't remove
423 * them automatically as themes may depend on those fonts. */
424 if (remove_list[REMOVE_FONT].option == REMOVE_IF_NOT_USED)
425 check_whether_used_in_file(RB_FONTS_CONFIG);
426 #endif
428 dir = rb->opendir(THEME_DIR);
429 if (!dir)
430 return; /* open error */
432 while (true)
434 struct dirent* entry;
435 char *p;
436 int i;
437 /* walk through the directory content */
438 entry = rb->readdir(dir);
439 if (!entry)
440 break;
441 p = rb->strrchr(entry->d_name, '.');
442 if (!p || rb->strcmp(p, ".cfg"))
443 continue;
445 rb->snprintf(cfgfile, MAX_PATH, "%s/%s", THEME_DIR, entry->d_name);
446 check_whether_used_in_file(cfgfile);
447 /* break the loop if all files need to be checked in the theme
448 * turned out to be used. */
449 for (i = 0; i < NUM_REMOVE_ITEMS; i++)
451 struct remove_setting *setting = &remove_list[i];
452 if (setting->option == REMOVE_IF_NOT_USED)
454 if (setting->value[0] && !setting->used)
455 break;
458 if (i == NUM_REMOVE_ITEMS)
459 break;
461 rb->closedir(dir);
464 static int remove_file(struct remove_setting *setting)
466 if (!rb->file_exists(setting->value))
468 show_mess("Doesn't exist", setting->value);
469 return 0;
471 if (is_deny_file(setting->value))
473 show_mess("Denied", setting->value);
474 return 0;
476 switch (setting->option)
478 case ALWAYS_REMOVE:
479 break;
480 case NEVER_REMOVE:
481 show_mess("Skipped", setting->value);
482 return 0;
483 break;
484 case REMOVE_IF_NOT_USED:
485 if (setting->used)
487 show_mess("Used", setting->value);
488 return 0;
490 break;
491 case ASK_FOR_REMOVAL:
492 default:
494 const char *message_lines[] = { "Delete?", setting->value };
495 const struct text_message text_message = { message_lines, 2 };
496 if (rb->gui_syncyesno_run(&text_message, NULL, NULL) != YESNO_YES)
498 show_mess("Skipped", setting->value);
499 return 0;
502 break;
504 if (rb->remove(setting->value))
506 show_mess("Failed", setting->value);
507 return -1;
509 if (setting->func && setting->func(setting))
510 return -1;
511 show_mess("Removed", setting->value);
512 return 1;
514 static int remove_theme(void)
516 static char line[MAX_PATH];
517 int fd;
518 int i, num_removed = 0;
519 char *name, *value;
520 bool needs_to_check_whether_used = false;
522 /* initialize for safe */
523 for (i=0; i<NUM_REMOVE_ITEMS; i++)
524 remove_list[i].value[0] = 0;
526 /* load settings */
527 fd = rb->open(themefile, O_RDONLY);
528 if (fd < 0) return fd;
529 while (rb->read_line(fd, line, sizeof(line)) > 0)
531 if (!rb->settings_parseline(line, &name, &value))
532 continue;
533 /* remove trailing spaces. */
534 char *p = value+rb->strlen(value)-1;
535 while (*p == ' ') *p-- = 0;
536 if (*value == 0 || !rb->strcmp(value, "-"))
537 continue;
538 for (i=0; i<NUM_REMOVE_ITEMS; i++)
540 struct remove_setting *setting = &remove_list[i];
541 if (!rb->strcmp(name, setting->name))
543 set_file_name(setting->value, value, setting);
544 if(setting->option == REMOVE_IF_NOT_USED)
545 needs_to_check_whether_used = true;
546 break;
550 rb->close(fd);
552 if(needs_to_check_whether_used)
553 check_whether_used();
555 /* now remove file assosiated to the theme. */
556 for (i=0; i<NUM_REMOVE_ITEMS; i++)
558 if (remove_list[i].value[0])
560 int ret = remove_file(&remove_list[i]);
561 if (ret < 0)
562 return ret;
563 num_removed += ret;
567 /* remove the setting file iff it is in theme directory to protect
568 * aginst accidental removal of non theme cfg file. if the file is
569 * not in the theme directory, the file may not be a theme cfg file. */
570 if (rb->strncasecmp(themefile, THEME_DIR "/", sizeof(THEME_DIR "/")-1))
572 show_mess("Skipped", themefile);
574 else if (rb->remove(themefile))
576 show_mess("Failed", themefile);
577 return -1;
579 else
581 show_mess("Removed", themefile);
582 rb->reload_directory();
583 num_removed++;
585 return num_removed;
588 static bool option_changed = false;
589 static bool option_menu(void)
591 MENUITEM_STRINGLIST(option_menu, "Remove Options", NULL,
592 /* same order as remove_list */
593 #ifdef HAVE_LCD_BITMAP
594 "Font",
595 #endif
596 "WPS",
597 #ifdef HAVE_LCD_BITMAP
598 "Statusbar Skin",
599 #endif
600 #ifdef HAVE_REMOTE_LCD
601 "Remote WPS",
602 "Remote Statusbar Skin",
603 #endif
604 #if LCD_DEPTH > 1
605 "Backdrop",
606 #endif
607 #ifdef HAVE_LCD_BITMAP
608 "Iconset", "Viewers Iconset",
609 #endif
610 #ifdef HAVE_REMOTE_LCD
611 "Remote Iconset", "Remote Viewers Iconset",
612 #endif
613 #ifdef HAVE_LCD_COLOR
614 "Filetype Colours",
615 #endif
616 "Create Log File");
617 struct opt_items remove_names[] = {
618 {"Always Remove", -1}, {"Never Remove", -1},
619 {"Remove if not Used", -1}, {"Ask for Removal", -1},
621 int selected = 0, result;
623 while (1)
625 result = rb->do_menu(&option_menu, &selected, NULL, false);
626 if (result >= 0 && result < NUM_REMOVE_ITEMS)
628 struct remove_setting *setting = &remove_list[result];
629 int prev_option = setting->option;
630 if (rb->set_option(option_menu_[result], &setting->option, INT,
631 remove_names, NUM_REMOVE_OPTION, NULL))
632 return true;
633 if (prev_option != setting->option)
634 option_changed = true;
636 else if (result == NUM_REMOVE_ITEMS)
638 bool prev_value = create_log;
639 if(rb->set_bool("Create Log File", &create_log))
640 return true;
641 if (prev_value != create_log)
642 option_changed = true;
644 else if (result == MENU_ATTACHED_USB)
645 return true;
646 else
647 return false;
650 return false;
653 enum plugin_status plugin_start(const void* parameter)
655 static char title[64];
656 char *p;
657 MENUITEM_STRINGLIST(menu, title, NULL,
658 "Remove Theme", "Remove Options",
659 "Quit");
660 int selected = 0, ret;
661 bool exit = false;
663 if (!parameter)
664 return PLUGIN_ERROR;
666 rb->snprintf(title, sizeof(title), "Remove %s",
667 rb->strrchr(parameter, '/')+1);
668 if((p = rb->strrchr(title, '.')))
669 *p = 0;
671 #ifdef HAVE_LCD_BITMAP
672 rb->snprintf(font_file, MAX_PATH, FONT_DIR "/%s.fnt",
673 rb->global_settings->font_file);
674 #endif
675 rb->strlcpy(themefile, parameter, MAX_PATH);
676 if (!rb->file_exists(themefile))
678 rb->splash(HZ, "File open error!");
679 return PLUGIN_ERROR;
681 configfile_load(CONFIG_FILENAME, config, nb_config, 0);
682 while (!exit)
684 switch (rb->do_menu(&menu, &selected, NULL, false))
686 case 0:
687 if(create_log)
689 log_fd = rb->open(LOG_FILENAME, O_WRONLY|O_CREAT|O_APPEND, 0666);
690 if(log_fd >= 0)
691 rb->fdprintf(log_fd, "---- %s ----\n", title);
692 else
693 show_mess("Couldn't open log file.", NULL);
695 ret = remove_theme();
696 p = (ret >= 0? "Successfully removed!": "Remove failure");
697 show_mess(p, NULL);
698 if(log_fd >= 0)
700 rb->fdprintf(log_fd, "----------------\n");
701 rb->close(log_fd);
702 log_fd = -1;
704 rb->lcd_clear_display();
705 rb->lcd_update();
706 rb->splashf(0, "%s %s", p, "Press any key to exit.");
707 rb->button_clear_queue();
708 rb->button_get(true);
709 exit = true;
710 break;
711 case 1:
712 if (option_menu())
713 return PLUGIN_USB_CONNECTED;
714 break;
715 case 2:
716 exit = true;
717 break;
718 case MENU_ATTACHED_USB:
719 return PLUGIN_USB_CONNECTED;
720 break;
721 default:
722 break;
725 if(option_changed)
726 configfile_save(CONFIG_FILENAME, config, nb_config, 0);
727 return PLUGIN_OK;