Include config.h for strncmp RaaA maemo fix
[maemo-rb.git] / apps / plugins / random_folder_advance_config.c
blobf66aa40ba7bbbde84d061779b2f4472e412a825a
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 Jonathan Gordon
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "config.h"
22 #include "plugin.h"
23 #include "file.h"
27 static bool cancel;
28 static int fd;
29 static int dirs_count;
30 static int lasttick;
31 #define RFA_FILE ROCKBOX_DIR "/folder_advance_list.dat"
32 #define RFADIR_FILE ROCKBOX_DIR "/folder_advance_dir.txt"
33 #define RFA_FILE_TEXT ROCKBOX_DIR "/folder_advance_list.txt"
34 #define MAX_REMOVED_DIRS 10
36 char *buffer = NULL;
37 size_t buffer_size;
38 int num_replaced_dirs = 0;
39 char removed_dirs[MAX_REMOVED_DIRS][MAX_PATH];
40 struct file_format {
41 int count;
42 char folder[][MAX_PATH];
44 struct file_format *list = NULL;
46 void update_screen(bool clear)
48 char buf[15];
49 int i;
51 rb->snprintf(buf,sizeof(buf),"Folders: %d",dirs_count);
52 FOR_NB_SCREENS(i)
54 if(clear)
55 rb->screens[i]->clear_display();
56 rb->screens[i]->putsxy(0,0,buf);
57 rb->screens[i]->update();
61 void traversedir(char* location, char* name)
63 struct dirent *entry;
64 DIR* dir;
65 char fullpath[MAX_PATH], path[MAX_PATH];
66 bool check = false;
67 int i;
69 rb->snprintf(fullpath, sizeof(fullpath), "%s/%s", location, name);
70 dir = rb->opendir(fullpath);
71 if (dir) {
72 entry = rb->readdir(dir);
73 while (entry) {
74 if (cancel)
75 break;
76 /* Skip .. and . */
77 if (entry->d_name[0] == '.')
79 if ( !rb->strcmp(entry->d_name,".")
80 || !rb->strcmp(entry->d_name,"..")
81 || !rb->strcmp(entry->d_name,".rockbox"))
82 check = false;
83 else check = true;
85 else check = true;
87 /* check if path is removed directory, if so dont enter it */
88 rb->snprintf(path, MAX_PATH, "%s/%s", fullpath, entry->d_name);
89 while(path[0] == '/')
90 rb->strlcpy(path, path + 1, sizeof(path));
91 for(i = 0; i < num_replaced_dirs; i++)
93 if(!rb->strcmp(path, removed_dirs[i]))
95 check = false;
96 break;
100 if (check)
102 struct dirinfo info = rb->dir_get_info(dir, entry);
103 if (info.attribute & ATTR_DIRECTORY) {
104 char *start;
105 dirs_count++;
106 rb->snprintf(path,MAX_PATH,"%s/%s",fullpath,entry->d_name);
107 start = &path[rb->strlen(path)];
108 rb->memset(start,0,&path[MAX_PATH-1]-start);
109 rb->write(fd,path,MAX_PATH);
110 traversedir(fullpath, entry->d_name);
113 if (*rb->current_tick - lasttick > (HZ/2)) {
114 update_screen(false);
115 lasttick = *rb->current_tick;
116 if (rb->action_userabort(TIMEOUT_NOBLOCK))
118 cancel = true;
119 break;
123 entry = rb->readdir(dir);
125 rb->closedir(dir);
129 bool custom_dir(void)
131 DIR* dir_check;
132 char *starts, line[MAX_PATH], formatted_line[MAX_PATH];
133 static int fd2;
134 char buf[11];
135 int i, errors = 0;
137 /* populate removed dirs array */
138 if((fd2 = rb->open(RFADIR_FILE,O_RDONLY)) >= 0)
140 while ((rb->read_line(fd2, line, MAX_PATH - 1)) > 0)
142 if ((line[0] == '-') && (line[1] == '/') &&
143 (num_replaced_dirs < MAX_REMOVED_DIRS))
145 num_replaced_dirs ++;
146 rb->strlcpy(removed_dirs[num_replaced_dirs - 1], line + 2,
147 sizeof(line));
150 rb->close(fd2);
153 if((fd2 = rb->open(RFADIR_FILE,O_RDONLY)) >= 0)
155 while ((rb->read_line(fd2, line, MAX_PATH - 1)) > 0)
157 /* blank lines and removed dirs ignored */
158 if (rb->strlen(line) && ((line[0] != '-') || (line[1] != '/')))
160 /* remove preceeding '/'s from the line */
161 while(line[0] == '/')
162 rb->strlcpy(line, line + 1, sizeof(line));
164 rb->snprintf(formatted_line, MAX_PATH, "/%s", line);
166 dir_check = rb->opendir(formatted_line);
168 if (dir_check)
170 rb->closedir(dir_check);
171 starts = &formatted_line[rb->strlen(formatted_line)];
172 rb->memset(starts, 0, &formatted_line[MAX_PATH-1]-starts);
173 bool write_line = true;
175 for(i = 0; i < num_replaced_dirs; i++)
177 if(!rb->strcmp(line, removed_dirs[i]))
179 write_line = false;
180 break;
184 if(write_line)
186 dirs_count++;
187 rb->write(fd, formatted_line, MAX_PATH);
190 traversedir("", line);
192 else
194 errors ++;
195 rb->snprintf(buf,sizeof(buf),"Not found:");
196 FOR_NB_SCREENS(i)
198 rb->screens[i]->puts(0,0,buf);
199 rb->screens[i]->puts(0, errors, line);
201 update_screen(false);
205 rb->close(fd2);
206 if(errors)
207 /* Press button to continue */
208 rb->get_action(CONTEXT_STD, TIMEOUT_BLOCK);
210 else
211 return false;
212 return true;
215 void generate(void)
217 dirs_count = 0;
218 cancel = false;
219 fd = rb->open(RFA_FILE,O_CREAT|O_WRONLY, 0666);
220 rb->write(fd,&dirs_count,sizeof(int));
221 if (fd < 0)
223 rb->splashf(HZ, "Couldnt open %s", RFA_FILE);
224 return;
226 #ifndef HAVE_LCD_CHARCELLS
227 update_screen(true);
228 #endif
229 lasttick = *rb->current_tick;
231 if(!custom_dir())
232 traversedir("", "");
234 rb->lseek(fd,0,SEEK_SET);
235 rb->write(fd,&dirs_count,sizeof(int));
236 rb->close(fd);
237 rb->splash(HZ, "Done");
240 static const char* list_get_name_cb(int selected_item, void* data,
241 char* buf, size_t buf_len)
243 (void)data;
244 rb->strlcpy(buf, list->folder[selected_item], buf_len);
245 return buf;
248 int load_list(void)
250 int myfd = rb->open(RFA_FILE,O_RDONLY);
251 if (myfd < 0)
252 return -1;
253 buffer = rb->plugin_get_audio_buffer(&buffer_size);
254 if (!buffer)
256 return -2;
259 rb->read(myfd,buffer,buffer_size);
260 rb->close(myfd);
261 list = (struct file_format *)buffer;
263 return 0;
266 int save_list(void)
268 int myfd = rb->creat(RFA_FILE, 0666);
269 if (myfd < 0)
271 rb->splash(HZ, "Could Not Open " RFA_FILE);
272 return -1;
274 int dirs_count = 0, i = 0;
275 rb->write(myfd,&dirs_count,sizeof(int));
276 for ( ;i<list->count;i++)
278 if (list->folder[i][0] != ' ')
280 dirs_count++;
281 rb->write(myfd,list->folder[i],MAX_PATH);
284 rb->lseek(myfd,0,SEEK_SET);
285 rb->write(myfd,&dirs_count,sizeof(int));
286 rb->close(myfd);
288 return 1;
291 int edit_list(void)
293 struct gui_synclist lists;
294 bool exit = false;
295 int button,i;
296 int selection, ret = 0;
298 /* load the dat file if not already done */
299 if ((list == NULL || list->count == 0) && (i = load_list()) != 0)
301 rb->splashf(HZ*2, "Could not load %s, rv = %d", RFA_FILE, i);
302 return -1;
305 dirs_count = list->count;
307 rb->gui_synclist_init(&lists,list_get_name_cb,0, false, 1, NULL);
308 rb->gui_synclist_set_icon_callback(&lists,NULL);
309 rb->gui_synclist_set_nb_items(&lists,list->count);
310 rb->gui_synclist_limit_scroll(&lists,true);
311 rb->gui_synclist_select_item(&lists, 0);
313 while (!exit)
315 rb->gui_synclist_draw(&lists);
316 button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
317 if (rb->gui_synclist_do_button(&lists,&button,LIST_WRAP_UNLESS_HELD))
318 continue;
319 selection = rb->gui_synclist_get_sel_pos(&lists);
320 switch (button)
322 case ACTION_STD_OK:
323 list->folder[selection][0] = ' ';
324 list->folder[selection][1] = '\0';
325 break;
326 case ACTION_STD_CONTEXT:
328 int len;
329 MENUITEM_STRINGLIST(menu, "Remove Menu", NULL,
330 "Remove Folder", "Remove Folder Tree");
332 switch (rb->do_menu(&menu, NULL, NULL, false))
334 case 0:
335 list->folder[selection][0] = ' ';
336 list->folder[selection][1] = '\0';
337 break;
338 case 1:
340 char temp[MAX_PATH];
341 rb->strcpy(temp,list->folder[selection]);
342 len = rb->strlen(temp);
343 for (i=0;i<list->count;i++)
345 if (!rb->strncmp(list->folder[i],temp,len))
347 list->folder[i][0] = ' ';
348 list->folder[i][1] = '\0';
352 break;
355 break;
356 case ACTION_STD_CANCEL:
358 MENUITEM_STRINGLIST(menu, "Exit Menu", NULL,
359 "Save and Exit", "Ignore Changes and Exit");
361 switch (rb->do_menu(&menu, NULL, NULL, false))
363 case 0:
364 save_list();
365 case 1:
366 exit = true;
367 ret = -2;
370 break;
373 return ret;
376 int export_list_to_file_text(void)
378 int i = 0;
379 /* load the dat file if not already done */
380 if ((list == NULL || list->count == 0) && (i = load_list()) != 0)
382 rb->splashf(HZ*2, "Could not load %s, rv = %d", RFA_FILE, i);
383 return 0;
386 if (list->count <= 0)
388 rb->splashf(HZ*2, "no dirs in list file: %s", RFA_FILE);
389 return 0;
392 /* create and open the file */
393 int myfd = rb->creat(RFA_FILE_TEXT, 0666);
394 if (myfd < 0)
396 rb->splashf(HZ*4, "failed to open: fd = %d, file = %s",
397 myfd, RFA_FILE_TEXT);
398 return -1;
401 /* write each directory to file */
402 for (i = 0; i < list->count; i++)
404 if (list->folder[i][0] != ' ')
406 rb->fdprintf(myfd, "%s\n", list->folder[i]);
410 rb->close(myfd);
411 rb->splash(HZ, "Done");
412 return 1;
415 int import_list_from_file_text(void)
417 char line[MAX_PATH];
419 buffer = rb->plugin_get_audio_buffer(&buffer_size);
420 if (buffer == NULL)
422 rb->splash(HZ*2, "failed to get audio buffer");
423 return -1;
426 int myfd = rb->open(RFA_FILE_TEXT, O_RDONLY);
427 if (myfd < 0)
429 rb->splashf(HZ*2, "failed to open: %s", RFA_FILE_TEXT);
430 return -1;
433 /* set the list structure, and initialize count */
434 list = (struct file_format *)buffer;
435 list->count = 0;
437 while ((rb->read_line(myfd, line, MAX_PATH - 1)) > 0)
439 /* copy the dir name, and skip the newline */
440 int len = rb->strlen(line);
441 /* remove CRs */
442 if (len > 0)
444 if (line[len-1] == 0x0A || line[len-1] == 0x0D)
445 line[len-1] = 0x00;
446 if (len > 1 &&
447 (line[len-2] == 0x0A || line[len-2] == 0x0D))
448 line[len-2] = 0x00;
451 rb->strcpy(list->folder[list->count++], line);
454 rb->close(myfd);
456 if (list->count == 0)
458 load_list();
460 else
462 save_list();
464 rb->splash(HZ, "Done");
465 return list->count;
468 int start_shuffled_play(void)
470 int *order;
471 size_t max_shuffle_size;
472 int i = 0;
474 /* get memory for shuffling */
475 order=rb->plugin_get_buffer(&max_shuffle_size);
476 max_shuffle_size/=sizeof(int);
477 if (order==NULL || max_shuffle_size==0)
479 rb->splashf(HZ*2, "Not enough memory for shuffling");
480 return 0;
483 /* load the dat file if not already done */
484 if ((list == NULL || list->count == 0) && (i = load_list()) != 0)
486 rb->splashf(HZ*2, "Could not load %s, rv = %d", RFA_FILE, i);
487 return 0;
490 if (list->count <= 0)
492 rb->splashf(HZ*2, "no dirs in list file: %s", RFA_FILE);
493 return 0;
496 /* shuffle the thing */
497 rb->srand(*rb->current_tick);
498 if(list->count>(int)max_shuffle_size)
500 rb->splashf(HZ*2, "Too many folders: %d (room for %d)", list->count,(int)max_shuffle_size);
501 return 0;
503 for(i=0;i<list->count;i++)
504 order[i]=i;
506 for(i = list->count - 1; i >= 0; i--)
508 /* the rand is from 0 to RAND_MAX, so adjust to our value range */
509 int candidate = rb->rand() % (i + 1);
511 /* now swap the values at the 'i' and 'candidate' positions */
512 int store = order[candidate];
513 order[candidate] = order[i];
514 order[i] = store;
517 /* We don't want whatever is playing */
518 if (!(rb->playlist_remove_all_tracks(NULL) == 0
519 && rb->playlist_create(NULL, NULL) == 0))
521 rb->splashf(HZ*2, "Could not clear playlist");
522 return 0;
525 /* add the lot to the playlist */
526 for (i = 0; i < list->count; i++)
528 if (list->folder[order[i]][0] != ' ')
530 rb->playlist_insert_directory(NULL,list->folder[order[i]],PLAYLIST_INSERT_LAST,false,false);
532 if (rb->action_userabort(TIMEOUT_NOBLOCK))
534 break;
537 rb->splash(HZ, "Done");
538 rb->playlist_start(0,0);
539 return 1;
542 enum plugin_status main_menu(void)
544 bool exit = false;
545 MENUITEM_STRINGLIST(menu, "Main Menu", NULL,
546 "Generate Folder List",
547 "Edit Folder List",
548 "Export List To Textfile",
549 "Import List From Textfile",
550 "Play Shuffled",
551 "Quit");
553 while (!exit)
555 switch (rb->do_menu(&menu, NULL, NULL, false))
557 case 0: /* generate */
558 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
559 rb->cpu_boost(true);
560 #endif
561 generate();
562 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
563 rb->cpu_boost(false);
564 #endif
565 #ifdef HAVE_REMOTE_LCD
566 rb->remote_backlight_on();
567 #endif
568 rb->backlight_on();
569 break;
570 case 1:
571 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
572 rb->cpu_boost(true);
573 #endif
574 if (edit_list() < 0)
575 exit = true;
576 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
577 rb->cpu_boost(false);
578 #endif
579 #ifdef HAVE_REMOTE_LCD
580 rb->remote_backlight_on();
581 #endif
582 rb->backlight_on();
583 break;
584 case 2: /* export to textfile */
585 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
586 rb->cpu_boost(true);
587 #endif
588 export_list_to_file_text();
589 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
590 rb->cpu_boost(false);
591 #endif
592 #ifdef HAVE_REMOTE_LCD
593 rb->remote_backlight_on();
594 #endif
595 rb->backlight_on();
596 break;
597 case 3: /* import from textfile */
598 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
599 rb->cpu_boost(true);
600 #endif
601 import_list_from_file_text();
602 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
603 rb->cpu_boost(false);
604 #endif
605 #ifdef HAVE_REMOTE_LCD
606 rb->remote_backlight_on();
607 #endif
608 rb->backlight_on();
609 break;
610 case 4:
611 if (!start_shuffled_play())
612 return PLUGIN_ERROR;
613 else
614 return PLUGIN_GOTO_WPS;
615 case 5:
616 return PLUGIN_OK;
619 return PLUGIN_OK;
622 enum plugin_status plugin_start(const void* parameter)
624 (void)parameter;
626 cancel = false;
628 return main_menu();