summaryrefslogtreecommitdiffstats
path: root/apps/plugins/shopper.c
blob: 7129291c104b18760dc09635cc7ea8f6531f1831 (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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2010 Daniel Rigby
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "plugin.h"
#include "lib/playback_control.h"



#define MAX_LIST_SIZE 400
#define DESC_SIZE 40
#define MAX_LINE_LEN (DESC_SIZE + 1)

enum flag_type {
    FL_CLEARED = 0,
    FL_SET,
    FL_CATEGORY
};

enum view_type {
    EDIT_SHOPPING_LIST = 0,
    VIEW_SHOPPING_LIST
};

#define VIEW_TYPE_SIZE VIEW_SHOPPING_LIST + 1

struct items_list_s {
    unsigned int id;
    enum flag_type flag;
    char desc[DESC_SIZE];
};

static struct items_list_s items_list[MAX_LIST_SIZE];
static int total_item_count = 0;
static int view_id_list[MAX_LIST_SIZE];
static int view_item_count;
static enum view_type view = EDIT_SHOPPING_LIST;
static char filename[MAX_PATH];
static bool changed = false;
static bool show_categories = true;
static char category_string[] = "Hide categories";

static const char *list_get_name_cb(int selected_item, void* data,
                                    char* buf, size_t buf_len)
{
    (void)data;
    rb->strlcpy(buf, items_list[view_id_list[selected_item]].desc, buf_len);
    return buf;
}

static enum themable_icons list_get_icon_cb(int selected_item, void *data)
{
    (void)data;
    if (items_list[view_id_list[selected_item]].flag == FL_CATEGORY)
        return Icon_Rockbox;
    else if (items_list[view_id_list[selected_item]].flag == FL_SET)
        return Icon_Cursor;
    else
        return Icon_NOICON;
}

static bool save_changes(void)
{
    int fd;
    int i;

    fd = rb->open(filename,O_WRONLY|O_CREAT|O_TRUNC);
    if (fd < 0)
    {
        rb->splash(HZ*2, "Changes NOT saved");
        return false;
    }

    rb->lcd_clear_display();
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
    rb->cpu_boost(1);
#endif
    for (i = 0;i < total_item_count; i++)
    {
        switch (items_list[i].flag)
        {
            case FL_CATEGORY:
            {
                rb->fdprintf(fd,"#%s\n",items_list[i].desc);
                break;
            }
            case FL_SET:
            {
                rb->fdprintf(fd,"!%s\n",items_list[i].desc);
                break;
            }
            case FL_CLEARED:
            {
                rb->fdprintf(fd," %s\n",items_list[i].desc);
                break;
            }
        }
    }
    /* save current view */
    rb->fdprintf(fd,"$%d%d\n",view, show_categories);

#ifdef HAVE_ADJUSTABLE_CPU_FREQ
    rb->cpu_boost(0);
#endif
    rb->close(fd);

    return true;
}

static void create_view(struct gui_synclist *lists)
{
    unsigned int cnt = 0;
    int i, j;

    switch (view)
    {
        case EDIT_SHOPPING_LIST:
        {
            for (i = 0; i < total_item_count; i++)
            {
                if (show_categories || (items_list[i].flag != FL_CATEGORY))
                    view_id_list[cnt++] = i;
            }
            view_item_count = cnt;
            rb->gui_synclist_set_title(lists,"Select items",Icon_Playlist);
            break;
        }
        case VIEW_SHOPPING_LIST:
        {
            for (i = 0; i < total_item_count; i++)
            {
                if ((items_list[i].flag == FL_CATEGORY) && show_categories)
                {
                    for (j = i+1; j < total_item_count; j++)
                    {
                        if (items_list[j].flag == FL_SET)
                        {
                            view_id_list[cnt++] = i;
                            break;
                        }
                        if (items_list[j].flag == FL_CATEGORY)
                            break;
                    }
                }
                else if (items_list[i].flag == FL_SET)
                    view_id_list[cnt++] = i;
            }
            view_item_count = cnt;
            rb->gui_synclist_set_title(lists,"Shopping list",Icon_Playlist);
            break;
        }
    }
}

static bool toggle(int selected_item)
{
    if (items_list[view_id_list[selected_item]].flag == FL_CATEGORY)
        return false;
    else if (items_list[view_id_list[selected_item]].flag == FL_SET)
        items_list[view_id_list[selected_item]].flag = FL_CLEARED;
    else
        items_list[view_id_list[selected_item]].flag = FL_SET;
    return true;
}

static void update_category_string(void)
{
    if (show_categories)
        rb->strcpy(category_string,"Hide categories");
    else
        rb->strcpy(category_string,"Show categories");
}

static enum plugin_status load_file(void)
{
    int fd;
    static char temp_line[DESC_SIZE];
    static struct items_list_s new_item;
    static int count = 0;
    int linelen;
    total_item_count = 0;

    fd = rb->open(filename,O_RDONLY);
    if (fd < 0)
    {
        rb->splashf(HZ*2,"Couldn't open file: %s",filename);
        return PLUGIN_ERROR;
    }

    /* read in the file */
    while (rb->read_line(fd,temp_line,MAX_LINE_LEN))
    {
        if (rb->strncmp(temp_line, "$", 1) == 0)
        {
            /* read view preferences */
            linelen = rb->strlen(temp_line);
            if (linelen >= 2)
            {
                unsigned int val = temp_line[1] - '0';
                if (val < VIEW_TYPE_SIZE)
                {
                    view = val;
                }
            }
            if (linelen >= 3)
            {
                unsigned int val = temp_line[2] - '0';
                if (val <= 2)
                {
                    show_categories = val;
                    update_category_string();
                }
            }
        }
        else
        {
            new_item.id = count;
            if (rb->strncmp(temp_line, " ", 1) == 0)
            {
                /* read description, flag = cleared */
                new_item.flag = FL_CLEARED;
                rb->memcpy(new_item.desc, &temp_line[1], DESC_SIZE);
            }
            else if (rb->strncmp(temp_line, "!", 1) == 0)
            {
                /* read description, flag = set */
                new_item.flag = FL_SET;
                rb->memcpy(new_item.desc, &temp_line[1], DESC_SIZE);
            }
            else if (rb->strncmp(temp_line, "#", 1) == 0)
            {
                /* read description, flag = category */
                new_item.flag = FL_CATEGORY;
                rb->memcpy(new_item.desc, &temp_line[1], DESC_SIZE);
            }
            else
            {
                /* read description, flag = cleared */
                new_item.flag = FL_CLEARED;
                rb->memcpy(new_item.desc, temp_line, DESC_SIZE);
            }
            items_list[total_item_count] = new_item;
            total_item_count++;
            if (total_item_count == MAX_LIST_SIZE)
            {
                total_item_count = MAX_LIST_SIZE - 1;
                rb->splashf(HZ*2, "Truncating shopping list to %d items",
                            MAX_LIST_SIZE - 1);
                changed = true;
                rb->close(fd);
                return PLUGIN_OK;
            }
        }
    }
    rb->close(fd);
    changed = false;
    return PLUGIN_OK;
}

/* this is the plugin entry point */
enum plugin_status plugin_start(const void* parameter)
{
    struct gui_synclist lists;
    bool exit = false;
    int button;
    int cur_sel = 0;

#if LCD_DEPTH > 1
    rb->lcd_set_backdrop(NULL);
#endif

#ifdef HAVE_ADJUSTABLE_CPU_FREQ
    rb->cpu_boost(1);
#endif
    if (parameter)
    {
        rb->strcpy(filename,(char*)parameter);

        if (load_file() == PLUGIN_ERROR)
            return PLUGIN_ERROR;
    }
    else
        return PLUGIN_ERROR;

#ifdef HAVE_ADJUSTABLE_CPU_FREQ
    rb->cpu_boost(0);
#endif
    /* now dump it in the list */
    rb->gui_synclist_init(&lists,list_get_name_cb,0, false, 1, NULL);
    rb->gui_synclist_set_icon_callback(&lists, list_get_icon_cb);
    rb->gui_synclist_limit_scroll(&lists,true);
    create_view(&lists);
    rb->gui_synclist_set_nb_items(&lists,view_item_count);
    rb->gui_synclist_select_item(&lists, 0);
    rb->gui_synclist_draw(&lists);
    rb->lcd_update();

    while (!exit)
    {
        rb->gui_synclist_draw(&lists);
        cur_sel = rb->gui_synclist_get_sel_pos(&lists);
        button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
        if (rb->gui_synclist_do_button(&lists,&button,LIST_WRAP_UNLESS_HELD))
            continue;
        switch (button)
        {
            case ACTION_STD_OK:
            {
                changed |= toggle(cur_sel);
                break;
            }
            case ACTION_STD_MENU:
            case ACTION_STD_CONTEXT:
            {
                switch(view)
                {
                    case EDIT_SHOPPING_LIST:
                    {
                        MENUITEM_STRINGLIST(menu, "Options", NULL,
                                                  "View shopping list",
                                                  "Clear all items",
                                                  "Mark all items",
                                                  category_string,
                                                  "Revert to saved",
                                                  "Show Playback Menu",
                                                  "Quit without saving",
                                                  "Quit");

                        switch (rb->do_menu(&menu, NULL, NULL, false))
                        {
                            case 0:
                            {
                                /* view shopping list */
                                view = VIEW_SHOPPING_LIST;
                                changed = true;
                                break;
                            }
                            case 1:
                            {
                                /* clear all items */
                                int i;
                                for (i = 0; i < total_item_count; i++)
                                {
                                    if (items_list[i].flag == FL_SET)
                                        items_list[i].flag = FL_CLEARED;
                                }
                                changed = true;
                                break;
                            }
                            case 2:
                            {
                                /* mark all items */
                                int i;
                                for (i = 0; i < total_item_count; i++)
                                {
                                    if (items_list[i].flag == FL_CLEARED)
                                        items_list[i].flag = FL_SET;
                                }
                                changed = true;
                                break;
                            }
                            case 3:
                            {
                                /* toggle categories */
                                show_categories ^= true;
                                update_category_string();
                                changed = true;
                                break;
                            }
                            case 4:
                            {
                                /* revert to saved */
                                if (load_file() == PLUGIN_ERROR)
                                    return PLUGIN_ERROR;
                                break;
                            }
                            case 5:
                            {
                                /* playback menu */
                                playback_control(NULL);
                                break;
                            }
                            case 6:
                            {
                                /* Quit without saving */
                                exit = 1;
                                break;
                            }
                            case 7:
                            {
                                /* Save and quit */
                                if (changed)
                                    save_changes();
                                exit = 1;
                                break;
                            }
                            default:
                            {
                                break;
                            }
                        }
                        break;
                    }

                    case VIEW_SHOPPING_LIST:
                    {
                        MENUITEM_STRINGLIST(menu, "Options", NULL,
                                                  "Edit list",
                                                  "Reset list",
                                                  category_string,
                                                  "Revert to saved",
                                                  "Show Playback Menu",
                                                  "Quit without saving",
                                                  "Quit");

                        switch (rb->do_menu(&menu, NULL, NULL, false))
                        {
                            case 0:
                            {
                                /* edit list */
                                view = EDIT_SHOPPING_LIST;
                                changed = true;
                                break;
                            }
                            case 1:
                            {
                                /* reset list */
                                int i;
                                for (i = 0; i < total_item_count; i++)
                                {
                                    if (items_list[i].flag == FL_SET)
                                        items_list[i].flag = FL_CLEARED;
                                }
                                view = EDIT_SHOPPING_LIST;
                                changed = true;
                                break;
                            }
                            case 2:
                            {
                                /* toggle categories */
                                show_categories ^= true;
                                update_category_string();
                                changed = true;
                                break;
                            }
                            case 3:
                            {
                                /* revert to saved */
                                if (load_file() == PLUGIN_ERROR)
                                    return PLUGIN_ERROR;
                                break;
                            }
                            case 4:
                            {
                                /* playback menu */
                                playback_control(NULL);
                                break;
                            }
                            case 5:
                            {
                                /* Quit without saving */
                                exit = 1;
                                break;
                            }
                            case 6:
                            {
                                /* Save and quit */
                                if (changed)
                                    save_changes();
                                exit = 1;
                                break;
                            }
                            default:
                            {
                                break;
                            }
                        }
                        break;
                    }
                }
                break;
            }
            case ACTION_STD_CANCEL:
            {
                if (changed)
                    save_changes();
                exit = 1;
                break;
            }
        }

        create_view(&lists);
        rb->gui_synclist_set_nb_items(&lists,view_item_count);
        if (view_item_count > 0 && view_item_count <= cur_sel)
            rb->gui_synclist_select_item(&lists,view_item_count-1);
    }
    return PLUGIN_OK;
}