when changing settings from the Talk and Voice window also update the main widgets...
[Rockbox.git] / apps / plugins / random_folder_advance_config.c
blob656daeaaf808b701d655881a19c45f1754448e76
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 Jonathan Gordon
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "plugin.h"
20 #include "oldmenuapi.h"
22 PLUGIN_HEADER
24 static struct plugin_api* rb;
25 static bool abort;
26 static int fd;
27 static int dirs_count;
28 static int lasttick;
29 #define RFA_FILE ROCKBOX_DIR "/folder_advance_list.dat"
30 #define RFADIR_FILE ROCKBOX_DIR "/folder_advance_dir.txt"
31 #define MAX_REMOVED_DIRS 10
33 char *buffer = NULL;
34 ssize_t buffer_size;
35 int num_replaced_dirs = 0;
36 char removed_dirs[MAX_REMOVED_DIRS][MAX_PATH];
37 struct file_format {
38 int count;
39 char folder[][MAX_PATH];
41 struct file_format *list = NULL;
42 #if CONFIG_KEYPAD == PLAYER_PAD
44 #elif (CONFIG_KEYPAD == RECORDER_PAD) \
45 || (CONFIG_KEYPAD == ONDIO_PAD)
47 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) \
48 || (CONFIG_KEYPAD == IRIVER_H300_PAD)
50 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
51 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
52 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
54 #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
56 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
58 #elif CONFIG_KEYPAD == GIGABEAT_PAD
60 #elif CONFIG_KEYPAD == SANSA_E200_PAD
62 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
64 #endif
66 void update_screen(bool clear)
68 char buf[15];
69 int i;
71 rb->snprintf(buf,sizeof(buf),"Folders: %d",dirs_count);
72 FOR_NB_SCREENS(i)
74 if(clear)
75 rb->screens[i]->clear_display();
76 rb->screens[i]->putsxy(0,0,buf);
77 rb->screens[i]->update();
81 void traversedir(char* location, char* name)
83 struct dirent *entry;
84 DIR* dir;
85 char fullpath[MAX_PATH], path[MAX_PATH];
86 bool check = false;
87 int i;
89 rb->snprintf(fullpath, sizeof(fullpath), "%s/%s", location, name);
90 dir = rb->opendir(fullpath);
91 if (dir) {
92 entry = rb->readdir(dir);
93 while (entry) {
94 if (abort == true)
95 break;
96 /* Skip .. and . */
97 if (entry->d_name[0] == '.')
99 if ( !rb->strcmp(entry->d_name,".")
100 || !rb->strcmp(entry->d_name,"..")
101 || !rb->strcmp(entry->d_name,".rockbox"))
102 check = false;
103 else check = true;
105 else check = true;
107 /* check if path is removed directory, if so dont enter it */
108 rb->snprintf(path, MAX_PATH, "%s/%s", fullpath, entry->d_name);
109 while(path[0] == '/')
110 rb->strncpy(path, path + 1, rb->strlen(path));
111 for(i = 0; i < num_replaced_dirs; i++)
113 if(!rb->strcmp(path, removed_dirs[i]))
115 check = false;
116 break;
120 if (check)
122 if (entry->attribute & ATTR_DIRECTORY) {
123 char *start;
124 dirs_count++;
125 rb->snprintf(path,MAX_PATH,"%s/%s",fullpath,entry->d_name);
126 start = &path[rb->strlen(path)];
127 rb->memset(start,0,&path[MAX_PATH-1]-start);
128 rb->write(fd,path,MAX_PATH);
129 traversedir(fullpath, entry->d_name);
132 if (*rb->current_tick - lasttick > (HZ/2)) {
133 update_screen(false);
134 lasttick = *rb->current_tick;
135 if (rb->action_userabort(TIMEOUT_NOBLOCK))
137 abort = true;
138 break;
142 entry = rb->readdir(dir);
144 rb->closedir(dir);
148 bool custom_dir(void)
150 DIR* dir_check;
151 char *starts, line[MAX_PATH], formatted_line[MAX_PATH];
152 static int fd2;
153 char buf[11];
154 int i, errors = 0;
156 /* populate removed dirs array */
157 if((fd2 = rb->open(RFADIR_FILE,O_RDONLY)) > 0)
159 while ((rb->read_line(fd2, line, MAX_PATH - 1)) > 0)
161 if ((line[0] == '-') && (line[1] == '/') &&
162 (num_replaced_dirs < MAX_REMOVED_DIRS))
164 num_replaced_dirs ++;
165 rb->strncpy(removed_dirs[num_replaced_dirs - 1], line + 2,
166 rb->strlen(line));
169 rb->close(fd2);
172 if((fd2 = rb->open(RFADIR_FILE,O_RDONLY)) > 0)
174 while ((rb->read_line(fd2, line, MAX_PATH - 1)) > 0)
176 /* blank lines and removed dirs ignored */
177 if (rb->strlen(line) && ((line[0] != '-') || (line[1] != '/')))
179 /* remove preceeding '/'s from the line */
180 while(line[0] == '/')
181 rb->strncpy(line, line + 1, rb->strlen(line));
183 rb->snprintf(formatted_line, MAX_PATH, "/%s", line);
185 dir_check = rb->opendir(formatted_line);
187 if (dir_check)
189 rb->closedir(dir_check);
190 starts = &formatted_line[rb->strlen(formatted_line)];
191 rb->memset(starts, 0, &formatted_line[MAX_PATH-1]-starts);
192 bool write_line = true;
194 for(i = 0; i < num_replaced_dirs; i++)
196 if(!rb->strcmp(line, removed_dirs[i]))
198 write_line = false;
199 break;
203 if(write_line)
205 dirs_count++;
206 rb->write(fd, formatted_line, MAX_PATH);
209 traversedir("", line);
211 else
213 errors ++;
214 rb->snprintf(buf,sizeof(buf),"Not found:");
215 FOR_NB_SCREENS(i)
217 rb->screens[i]->puts(0,0,buf);
218 rb->screens[i]->puts(0, errors, line);
220 update_screen(false);
224 rb->close(fd2);
225 if(errors)
226 /* Press button to continue */
227 rb->get_action(CONTEXT_STD, TIMEOUT_BLOCK);
229 else
230 return false;
231 return true;
234 void generate(void)
236 dirs_count = 0;
237 abort = false;
238 fd = rb->open(RFA_FILE,O_CREAT|O_WRONLY);
239 rb->write(fd,&dirs_count,sizeof(int));
240 if (fd < 0)
242 rb->splash(HZ, "Couldnt open %s", RFA_FILE);
243 return;
245 #ifndef HAVE_LCD_CHARCELLS
246 update_screen(true);
247 #endif
248 lasttick = *rb->current_tick;
250 if(!custom_dir())
251 traversedir("", "");
253 rb->lseek(fd,0,SEEK_SET);
254 rb->write(fd,&dirs_count,sizeof(int));
255 rb->close(fd);
257 char *list_get_name_cb(int selected_item,void* data,char* buf)
259 (void)data;
260 rb->strcpy(buf,list->folder[selected_item]);
261 return buf;
264 void edit_list(void)
266 struct gui_synclist lists;
267 bool exit = false;
268 int button,i;
269 int selection;
270 fd = rb->open(RFA_FILE,O_RDONLY);
271 if (fd < 0)
272 return;
273 buffer = rb->plugin_get_audio_buffer((size_t *)&buffer_size);
274 if (!buffer)
275 return;
276 rb->read(fd,buffer,buffer_size);
277 rb->close(fd);
278 list = (struct file_format *)buffer;
279 dirs_count = list->count;
281 rb->gui_synclist_init(&lists,list_get_name_cb,0, false, 1);
282 rb->gui_synclist_set_icon_callback(&lists,NULL);
283 rb->gui_synclist_set_nb_items(&lists,list->count);
284 rb->gui_synclist_limit_scroll(&lists,true);
285 rb->gui_synclist_select_item(&lists, 0);
287 while (!exit)
289 rb->gui_synclist_draw(&lists);
290 button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
291 if (rb->gui_synclist_do_button(&lists,&button,LIST_WRAP_UNLESS_HELD))
292 continue;
293 selection = rb->gui_synclist_get_sel_pos(&lists);
294 switch (button)
296 case ACTION_STD_OK:
297 list->folder[selection][0] = ' ';
298 list->folder[selection][1] = '\0';
299 break;
300 case ACTION_STD_CONTEXT:
302 int m, len;
303 static const struct menu_item items[] = {
304 { "Remove Folder", NULL },
305 { "Remove Folder Tree", NULL },
307 m = menu_init(rb, items, sizeof(items) / sizeof(*items),
308 NULL, NULL, NULL, NULL);
310 switch (menu_show(m))
312 case 0:
313 list->folder[selection][0] = ' ';
314 list->folder[selection][1] = '\0';
315 break;
316 case 1:
318 char temp[MAX_PATH];
319 rb->strcpy(temp,list->folder[selection]);
320 len = rb->strlen(temp);
321 for (i=0;i<list->count;i++)
323 if (!rb->strncmp(list->folder[i],temp,len))
325 list->folder[i][0] = ' ';
326 list->folder[i][1] = '\0';
330 break;
332 menu_exit(m);
334 break;
335 case ACTION_STD_CANCEL:
337 int m;
338 static const struct menu_item items[] = {
339 { "Save and Exit", NULL },
340 { "Ignore Changes and Exit", NULL },
342 m = menu_init(rb, items, sizeof(items) / sizeof(*items),
343 NULL, NULL, NULL, NULL);
345 switch (menu_show(m))
347 case 0:
348 exit = true;
349 rb->splash(HZ*2, "Saving " RFA_FILE);
350 fd = rb->open(RFA_FILE, O_CREAT|O_WRONLY);
351 if (fd < 0)
353 rb->splash(HZ, "Could Not Open " RFA_FILE);
354 break;
356 dirs_count = 0;
357 rb->write(fd,&dirs_count,sizeof(int));
358 for (i=0;i<list->count;i++)
360 if (list->folder[i][0] != ' ')
362 dirs_count++;
363 rb->write(fd,list->folder[i],MAX_PATH);
366 rb->lseek(fd,0,SEEK_SET);
367 rb->write(fd,&dirs_count,sizeof(int));
368 rb->close(fd);
369 case 1:
370 exit = true;
372 menu_exit(m);
374 break;
378 int main_menu(void)
380 int m;
381 static const struct menu_item items[] = {
382 { "Generate Folder List", NULL },
383 { "Edit Folder List", NULL },
384 { "Quit", NULL },
386 m = menu_init(rb, items, sizeof(items) / sizeof(*items),
387 NULL, NULL, NULL, NULL);
389 switch (menu_show(m))
391 case 0: /* generate */
392 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
393 rb->cpu_boost(true);
394 #endif
395 generate();
396 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
397 rb->cpu_boost(false);
398 #endif
399 #ifdef HAVE_REMOTE_LCD
400 rb->remote_backlight_on();
401 #endif
402 rb->backlight_on();
403 break;
404 case 1:
405 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
406 rb->cpu_boost(true);
407 #endif
408 edit_list();
409 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
410 rb->cpu_boost(false);
411 #endif
412 #ifdef HAVE_REMOTE_LCD
413 rb->remote_backlight_on();
414 #endif
415 rb->backlight_on();
416 break;
417 case 2:
418 menu_exit(m);
419 return 1;
421 menu_exit(m);
422 return 0;
425 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
427 (void)parameter;
429 rb = api;
430 abort = false;
432 while (!main_menu())
434 return PLUGIN_OK;