1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
21 #include "lib/configfile.h"
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"
41 struct remove_setting
{
43 const char *prefix
, *suffix
;
46 int (*func
)(struct remove_setting
*);
50 static int remove_wps(struct remove_setting
*);
51 #ifdef HAVE_LCD_BITMAP
52 static int remove_icons(struct remove_setting
*setting
);
55 enum remove_settings
{
56 #ifdef HAVE_LCD_BITMAP
60 #ifdef HAVE_LCD_BITMAP
63 #ifdef HAVE_REMOTE_LCD
70 #ifdef HAVE_LCD_BITMAP
74 #ifdef HAVE_REMOTE_LCD
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 },
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 },
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 },
103 [REMOVE_BACKDROP
] = { "backdrop", BACKDROP_DIR
"/", ".bmp", "",
104 REMOVE_IF_NOT_USED
, NULL
, false },
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 },
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 },
118 #ifdef HAVE_LCD_COLOR
119 [REMOVE_COLOURS
] = { "filetype colours", THEME_DIR
"/", ".colours", "",
120 ASK_FOR_REMOVAL
, NULL
, false },
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
},
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
},
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
},
149 { TYPE_INT
, 0, NUM_REMOVE_OPTION
,
150 { .int_p
= &remove_list
[REMOVE_BACKDROP
].option
},
151 "remove backdrop", option_names
},
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
},
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
},
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
},
174 {TYPE_BOOL
, 0, 1, { .bool_p
= &create_log
},
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];
186 rb
->snprintf(buf
, sizeof(buf
), "%s: %s", text
, file
);
188 rb
->snprintf(buf
, sizeof(buf
), "%s", text
);
192 rb
->fdprintf(log_fd
, "%s\n", buf
);
200 /* set full path of file. */
201 static void set_file_name(char *buf
, const char*file
,
202 struct remove_setting
*setting
)
205 if (rb
->strncasecmp(file
, setting
->prefix
, rb
->strlen(setting
->prefix
)))
206 rb
->snprintf(buf
, MAX_PATH
, "%s%s", setting
->prefix
, file
);
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
)
221 int dirlen
= rb
->strlen(dirname
);
223 dir
= rb
->opendir(dirname
);
225 return -1; /* open error */
229 struct dirent
* entry
;
230 /* walk through the directory content */
231 entry
= rb
->readdir(dir
);
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 */
253 result
= rb
->remove(dirname
);
255 if (ACTION_STD_CANCEL
== rb
->get_action(CONTEXT_STD
, TIMEOUT_NOBLOCK
))
257 show_mess("Canceled", NULL
);
265 { /* remove the now empty directory */
266 dirname
[dirlen
] = '\0'; /* terminate to original length */
268 result
= rb
->rmdir(dirname
);
269 show_mess("Removed", dirname
);
275 static int remove_wps(struct remove_setting
*setting
)
277 char bmpdir
[MAX_PATH
];
279 rb
->strcpy(bmpdir
, setting
->value
);
280 p
= rb
->strrchr(bmpdir
, '.');
282 if (!rb
->dir_exists(bmpdir
))
284 return remove_dir(bmpdir
, MAX_PATH
);
287 #ifdef HAVE_LCD_BITMAP
288 static int remove_icons(struct remove_setting
*setting
)
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
))
300 if (rb
->remove(path
))
302 show_mess("Failed", path
);
305 show_mess("Removed", path
);
310 #ifdef HAVE_LCD_BITMAP
311 static char font_file
[MAX_PATH
];
313 static bool is_deny_file(const char *file
)
315 const char *deny_files
[] = {
318 #ifdef HAVE_LCD_BITMAP
323 const char **p
= deny_files
;
326 if (!rb
->strcmp(file
, *p
))
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
,
339 rb
->global_settings
->wps_file
,
340 #ifdef HAVE_LCD_BITMAP
341 rb
->global_settings
->sbs_file
,
343 #ifdef HAVE_REMOTE_LCD
344 rb
->global_settings
->rwps_file
,
345 rb
->global_settings
->rsbs_file
,
348 rb
->global_settings
->backdrop_file
,
350 #ifdef HAVE_LCD_BITMAP
351 rb
->global_settings
->icon_file
,
352 rb
->global_settings
->viewers_icon_file
,
354 #ifdef HAVE_REMOTE_LCD
355 rb
->global_settings
->remote_icon_file
,
356 rb
->global_settings
->remote_viewers_icon_file
,
358 #ifdef HAVE_LCD_COLOR
359 rb
->global_settings
->colors_file
,
362 char tempfile
[MAX_PATH
];
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
)
378 char settingfile
[MAX_PATH
];
384 if (!rb
->strcasecmp(themefile
, cfgfile
))
386 fd
= rb
->open(cfgfile
, O_RDONLY
);
389 while (rb
->read_line(fd
, line
, sizeof(line
)) > 0)
391 if (!rb
->settings_parseline(line
, &name
, &value
))
393 /* remove trailing spaces. */
394 p
= value
+rb
->strlen(value
)-1;
395 while (*p
== ' ') *p
-- = 0;
396 if (*value
== 0 || !rb
->strcmp(value
, "-"))
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;
415 static void check_whether_used(void)
417 char cfgfile
[MAX_PATH
];
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
);
428 dir
= rb
->opendir(THEME_DIR
);
430 return; /* open error */
434 struct dirent
* entry
;
437 /* walk through the directory content */
438 entry
= rb
->readdir(dir
);
441 p
= rb
->strrchr(entry
->d_name
, '.');
442 if (!p
|| rb
->strcmp(p
, ".cfg"))
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
)
458 if (i
== NUM_REMOVE_ITEMS
)
464 static int remove_file(struct remove_setting
*setting
)
466 if (!rb
->file_exists(setting
->value
))
468 show_mess("Doesn't exist", setting
->value
);
471 if (is_deny_file(setting
->value
))
473 show_mess("Denied", setting
->value
);
476 switch (setting
->option
)
481 show_mess("Skipped", setting
->value
);
484 case REMOVE_IF_NOT_USED
:
487 show_mess("Used", setting
->value
);
491 case ASK_FOR_REMOVAL
:
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
);
504 if (rb
->remove(setting
->value
))
506 show_mess("Failed", setting
->value
);
509 if (setting
->func
&& setting
->func(setting
))
511 show_mess("Removed", setting
->value
);
514 static int remove_theme(void)
516 static char line
[MAX_PATH
];
518 int i
, num_removed
= 0;
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;
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
))
533 /* remove trailing spaces. */
534 char *p
= value
+rb
->strlen(value
)-1;
535 while (*p
== ' ') *p
-- = 0;
536 if (*value
== 0 || !rb
->strcmp(value
, "-"))
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;
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
]);
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
);
581 show_mess("Removed", themefile
);
582 rb
->reload_directory();
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
597 #ifdef HAVE_LCD_BITMAP
600 #ifdef HAVE_REMOTE_LCD
602 "Remote Statusbar Skin",
607 #ifdef HAVE_LCD_BITMAP
608 "Iconset", "Viewers Iconset",
610 #ifdef HAVE_REMOTE_LCD
611 "Remote Iconset", "Remote Viewers Iconset",
613 #ifdef HAVE_LCD_COLOR
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
;
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
))
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
))
641 if (prev_value
!= create_log
)
642 option_changed
= true;
644 else if (result
== MENU_ATTACHED_USB
)
653 enum plugin_status
plugin_start(const void* parameter
)
655 static char title
[64];
657 MENUITEM_STRINGLIST(menu
, title
, NULL
,
658 "Remove Theme", "Remove Options",
660 int selected
= 0, ret
;
666 rb
->snprintf(title
, sizeof(title
), "Remove %s",
667 rb
->strrchr(parameter
, '/')+1);
668 if((p
= rb
->strrchr(title
, '.')))
671 #ifdef HAVE_LCD_BITMAP
672 rb
->snprintf(font_file
, MAX_PATH
, FONT_DIR
"/%s.fnt",
673 rb
->global_settings
->font_file
);
675 rb
->strlcpy(themefile
, parameter
, MAX_PATH
);
676 if (!rb
->file_exists(themefile
))
678 rb
->splash(HZ
, "File open error!");
681 configfile_load(CONFIG_FILENAME
, config
, nb_config
, 0);
684 switch (rb
->do_menu(&menu
, &selected
, NULL
, false))
689 log_fd
= rb
->open(LOG_FILENAME
, O_WRONLY
|O_CREAT
|O_APPEND
, 0666);
691 rb
->fdprintf(log_fd
, "---- %s ----\n", title
);
693 show_mess("Couldn't open log file.", NULL
);
695 ret
= remove_theme();
696 p
= (ret
>= 0? "Successfully removed!": "Remove failure");
700 rb
->fdprintf(log_fd
, "----------------\n");
704 rb
->lcd_clear_display();
706 rb
->splashf(0, "%s %s", p
, "Press any key to exit.");
707 rb
->button_clear_queue();
708 rb
->button_get(true);
713 return PLUGIN_USB_CONNECTED
;
718 case MENU_ATTACHED_USB
:
719 return PLUGIN_USB_CONNECTED
;
726 configfile_save(CONFIG_FILENAME
, config
, nb_config
, 0);