summaryrefslogtreecommitdiff
path: root/apps/plugins/random_folder_advance_config.c
blob: 7d055c417270777d03a7e86107ce07d8552af59c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2006 Jonathan Gordon
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "plugin.h"

PLUGIN_HEADER

static struct plugin_api* rb;
static bool abort;
static int fd;
static int dirs_count;
static int lasttick;
#define RFA_FILE ROCKBOX_DIR "/folder_advance_list.dat"
char *buffer = NULL;
int buffer_size;
struct file_format {
    int count;
    char folder[][MAX_PATH];
};
struct file_format *list = NULL;
#if CONFIG_KEYPAD == PLAYER_PAD 

#elif (CONFIG_KEYPAD == RECORDER_PAD) \
   || (CONFIG_KEYPAD == ONDIO_PAD)

#elif (CONFIG_KEYPAD == IRIVER_H100_PAD) \
   || (CONFIG_KEYPAD == IRIVER_H300_PAD)

#elif (CONFIG_KEYPAD == IPOD_4G_PAD) \
   || (CONFIG_KEYPAD == IPOD_3G_PAD)

#elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD

#elif CONFIG_KEYPAD == IAUDIO_X5_PAD

#elif CONFIG_KEYPAD == GIGABEAT_PAD

#elif CONFIG_KEYPAD == SANSA_E200_PAD

#elif CONFIG_KEYPAD == IRIVER_H10_PAD

#endif

void update_screen(bool clear)
{
    char buf[15];
#if defined(HAVE_LCD_BITMAP) || defined(HAVE_REMOTE_LCD) /* always bitmap */
    int i;
    FOR_NB_SCREENS(i)
    {
        rb->snprintf(buf,15,"Folders: %d",dirs_count);
        if(clear)
            rb->screens[i]->clear_display();
        rb->screens[i]->putsxy(0,0,buf);
        rb->screens[i]->update();
    }
#else
    rb->snprintf(buf,15,"Folders: %d",dirs_count);
    if(clear)
        rb->lcd_clear_display();
    rb->lcd_puts(0,0,buf);
#endif
}

void traversedir(char* location, char* name)
{
    struct dirent *entry;
    DIR* dir;
    char fullpath[MAX_PATH];
    bool check = false;

    rb->snprintf(fullpath, sizeof(fullpath), "%s/%s", location, name);
    dir = rb->opendir(fullpath);
    if (dir) {
        entry = rb->readdir(dir);
        while (entry) {
            if (abort == true)
                break;
            /* Skip .. and . */
            if (entry->d_name[0] == '.')
            {
                if (    !rb->strcmp(entry->d_name,".")
                     || !rb->strcmp(entry->d_name,"..")
                     || !rb->strcmp(entry->d_name,".rockbox"))
                    check = false;
                else check = true;
            }
            else check = true;
            
            if (check)
            {
                if (entry->attribute & ATTR_DIRECTORY) {
                    char *start, path[MAX_PATH];
                    dirs_count++;
                    rb->snprintf(path,MAX_PATH,"%s/%s",fullpath,entry->d_name);
                    start = &path[rb->strlen(path)];
                    rb->memset(start,0,&path[MAX_PATH-1]-start);
                    rb->write(fd,path,MAX_PATH);
                    traversedir(fullpath, entry->d_name);
                }
            }
            if (*rb->current_tick - lasttick > (HZ/2)) {
                update_screen(false);
                lasttick = *rb->current_tick;
                if (rb->action_userabort(TIMEOUT_NOBLOCK))
                {
                    abort = true;
                    break;
                }
            }

            entry = rb->readdir(dir);
        }
        rb->closedir(dir);
    }
}
void generate(void)
{
    dirs_count = 0;
    abort = false;
    fd = rb->open(RFA_FILE,O_CREAT|O_WRONLY);
    rb->write(fd,&dirs_count,sizeof(int));
    if (fd < 0)
    {
        rb->splash(HZ, true, "Couldnt open %s", RFA_FILE);
        return;
    }
#ifndef HAVE_LCD_CHARCELLS
    update_screen(true);
#endif
    lasttick = *rb->current_tick;

    traversedir("", "");
    rb->lseek(fd,0,SEEK_SET);
    rb->write(fd,&dirs_count,sizeof(int));
    rb->close(fd);
}
char *list_get_name_cb(int selected_item,void* data,char* buf)
{
    (void)data;
    rb->strcpy(buf,list->folder[selected_item]);
    return buf;
}

void edit_list(void)
{
    struct gui_synclist lists;
    bool exit = false;
    int button,i;
    int selection;
    fd = rb->open(RFA_FILE,O_RDONLY);
    if (fd < 0)
        return;
    buffer = rb->plugin_get_audio_buffer(&buffer_size);
    if (!buffer)
        return;
    rb->read(fd,buffer,buffer_size);
    rb->close(fd);
    list = (struct file_format *)buffer;
    dirs_count = list->count;
    
    rb->gui_synclist_init(&lists,list_get_name_cb,0, false, 1);
    rb->gui_synclist_set_icon_callback(&lists,NULL);
    rb->gui_synclist_set_nb_items(&lists,list->count);
    rb->gui_synclist_limit_scroll(&lists,true);
    rb->gui_synclist_select_item(&lists, 0);
    
    while (!exit)
    {
        rb->gui_synclist_draw(&lists);
        button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
        if (rb->gui_synclist_do_button(&lists,button))
            continue;
        selection = rb->gui_synclist_get_sel_pos(&lists);
        switch (button)
        {
            case ACTION_STD_OK:
                list->folder[selection][0] = ' ';
                list->folder[selection][1] = '\0';
                break;
            case ACTION_STD_CONTEXT:
            {
                int m, len;
                static const struct menu_item items[] = {
                    { "Remove Folder", NULL },
                    { "Remove Folder Tree", NULL },
                };
                m = rb->menu_init(items, sizeof(items) / sizeof(*items),
                                  NULL, NULL, NULL, NULL);

                switch (rb->menu_show(m))
                {
                    case 0:
                        list->folder[selection][0] = ' ';
                        list->folder[selection][1] = '\0';
                        break;
                    case 1:
                    {
                        char temp[MAX_PATH];
                        rb->strcpy(temp,list->folder[selection]);
                        len = rb->strlen(temp);
                        for (i=0;i<list->count;i++)
                        {
                            if (!rb->strncmp(list->folder[i],temp,len))
                            {
                                list->folder[i][0] = ' ';
                                list->folder[i][1] = '\0';
                            }
                        }
                    }
                        break;
                }
                rb->menu_exit(m);
            }
            break;
            case ACTION_STD_CANCEL:
            {
                int m;
                static const struct menu_item items[] = {
                    { "Save and Exit", NULL },
                    { "Ignore Changes and Exit", NULL },
                };
                m = rb->menu_init(items, sizeof(items) / sizeof(*items),
                                  NULL, NULL, NULL, NULL);

                switch (rb->menu_show(m))
                {
                    case 0:
                        exit = true;
                        rb->splash(HZ*2, true, "Saving " RFA_FILE);
                        fd = rb->open(RFA_FILE, O_CREAT|O_WRONLY);
                        if (fd < 0)
                        {
                            rb->splash(HZ, true, "Could Not Open " RFA_FILE);
                            break;
                        }
                        dirs_count = 0;
                        rb->write(fd,&dirs_count,sizeof(int));
                        for (i=0;i<list->count;i++)
                        {
                            if (list->folder[i][0] != ' ')
                            {
                                dirs_count++;
                                rb->write(fd,list->folder[i],MAX_PATH);
                            }
                        }
                        rb->lseek(fd,0,SEEK_SET);
                        rb->write(fd,&dirs_count,sizeof(int));
                        rb->close(fd);
                    case 1:
                        exit = true;
                }
                rb->menu_exit(m);
            }
                break;
        }
    }
}
int main_menu(void)
{
    int m;
    static const struct menu_item items[] = {
        { "Generate Folder List", NULL },
        { "Edit Folder List", NULL },
        { "Quit", NULL },
    };
    m = rb->menu_init(items, sizeof(items) / sizeof(*items),
                      NULL, NULL, NULL, NULL);

    switch (rb->menu_show(m))
    {
        case 0: /* generate */
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
            rb->cpu_boost(true);
#endif
            generate();
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
            rb->cpu_boost(false);
#endif
#ifdef HAVE_REMOTE_LCD
            rb->remote_backlight_on();
#endif
            rb->backlight_on();
            break;
        case 1:
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
            rb->cpu_boost(true);
#endif
            edit_list();
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
            rb->cpu_boost(false);
#endif
#ifdef HAVE_REMOTE_LCD
            rb->remote_backlight_on();
#endif
            rb->backlight_on();
            break;
        case 2:
            rb->menu_exit(m);
            return 1;
    }
    rb->menu_exit(m);
    return 0;
}

enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
{
    (void)parameter;

    rb = api;
    abort = false;
    
    while (!main_menu())
        ;
    return PLUGIN_OK;
}