Prepare new maemo release
[maemo-rb.git] / apps / plugins / shopper.c
blob15b44ca21ad51949a744652b04b6ec83ddfae0fd
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Daniel Rigby
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 "plugin.h"
22 #include "lib/playback_control.h"
26 #define MAX_LIST_SIZE 400
27 #define DESC_SIZE 40
28 #define MAX_LINE_LEN (DESC_SIZE + 1)
30 enum flag_type {
31 FL_CLEARED = 0,
32 FL_SET,
33 FL_CATEGORY
36 enum view_type {
37 EDIT_SHOPPING_LIST = 0,
38 VIEW_SHOPPING_LIST
41 #define VIEW_TYPE_SIZE VIEW_SHOPPING_LIST + 1
43 struct items_list_s {
44 unsigned int id;
45 enum flag_type flag;
46 char desc[DESC_SIZE];
49 static struct items_list_s items_list[MAX_LIST_SIZE];
50 static int total_item_count = 0;
51 static int view_id_list[MAX_LIST_SIZE];
52 static int view_item_count;
53 static enum view_type view = EDIT_SHOPPING_LIST;
54 static char filename[MAX_PATH];
55 static bool changed = false;
56 static bool show_categories = true;
57 static char category_string[] = "Hide categories";
59 static const char *list_get_name_cb(int selected_item, void* data,
60 char* buf, size_t buf_len)
62 (void)data;
63 rb->strlcpy(buf, items_list[view_id_list[selected_item]].desc, buf_len);
64 return buf;
67 static enum themable_icons list_get_icon_cb(int selected_item, void *data)
69 (void)data;
70 if (items_list[view_id_list[selected_item]].flag == FL_CATEGORY)
71 return Icon_Rockbox;
72 else if (items_list[view_id_list[selected_item]].flag == FL_SET)
73 return Icon_Cursor;
74 else
75 return Icon_NOICON;
78 static bool save_changes(void)
80 int fd;
81 int i;
83 fd = rb->open(filename,O_WRONLY|O_CREAT|O_TRUNC);
84 if (fd < 0)
86 rb->splash(HZ*2, "Changes NOT saved");
87 return false;
90 rb->lcd_clear_display();
91 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
92 rb->cpu_boost(1);
93 #endif
94 for (i = 0;i < total_item_count; i++)
96 switch (items_list[i].flag)
98 case FL_CATEGORY:
100 rb->fdprintf(fd,"#%s\n",items_list[i].desc);
101 break;
103 case FL_SET:
105 rb->fdprintf(fd,"!%s\n",items_list[i].desc);
106 break;
108 case FL_CLEARED:
110 rb->fdprintf(fd," %s\n",items_list[i].desc);
111 break;
115 /* save current view */
116 rb->fdprintf(fd,"$%d%d\n",view, show_categories);
118 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
119 rb->cpu_boost(0);
120 #endif
121 rb->close(fd);
123 return true;
126 static void create_view(struct gui_synclist *lists)
128 unsigned int cnt = 0;
129 int i, j;
131 switch (view)
133 case EDIT_SHOPPING_LIST:
135 for (i = 0; i < total_item_count; i++)
137 if (show_categories || (items_list[i].flag != FL_CATEGORY))
138 view_id_list[cnt++] = i;
140 view_item_count = cnt;
141 rb->gui_synclist_set_title(lists,"Select items",Icon_Playlist);
142 break;
144 case VIEW_SHOPPING_LIST:
146 for (i = 0; i < total_item_count; i++)
148 if ((items_list[i].flag == FL_CATEGORY) && show_categories)
150 for (j = i+1; j < total_item_count; j++)
152 if (items_list[j].flag == FL_SET)
154 view_id_list[cnt++] = i;
155 break;
157 if (items_list[j].flag == FL_CATEGORY)
158 break;
161 else if (items_list[i].flag == FL_SET)
162 view_id_list[cnt++] = i;
164 view_item_count = cnt;
165 rb->gui_synclist_set_title(lists,"Shopping list",Icon_Playlist);
166 break;
171 static bool toggle(int selected_item)
173 if (items_list[view_id_list[selected_item]].flag == FL_CATEGORY)
174 return false;
175 else if (items_list[view_id_list[selected_item]].flag == FL_SET)
176 items_list[view_id_list[selected_item]].flag = FL_CLEARED;
177 else
178 items_list[view_id_list[selected_item]].flag = FL_SET;
179 return true;
182 static void update_category_string(void)
184 if (show_categories)
185 rb->strcpy(category_string,"Hide categories");
186 else
187 rb->strcpy(category_string,"Show categories");
190 static enum plugin_status load_file(void)
192 int fd;
193 static char temp_line[DESC_SIZE];
194 static struct items_list_s new_item;
195 static int count = 0;
196 int linelen;
197 total_item_count = 0;
199 fd = rb->open(filename,O_RDONLY);
200 if (fd < 0)
202 rb->splashf(HZ*2,"Couldn't open file: %s",filename);
203 return PLUGIN_ERROR;
206 /* read in the file */
207 while (rb->read_line(fd,temp_line,MAX_LINE_LEN))
209 if (rb->strncmp(temp_line, "$", 1) == 0)
211 /* read view preferences */
212 linelen = rb->strlen(temp_line);
213 if (linelen >= 2)
215 unsigned int val = temp_line[1] - '0';
216 if (val < VIEW_TYPE_SIZE)
218 view = val;
221 if (linelen >= 3)
223 unsigned int val = temp_line[2] - '0';
224 if (val <= 2)
226 show_categories = val;
227 update_category_string();
231 else
233 new_item.id = count;
234 if (rb->strncmp(temp_line, " ", 1) == 0)
236 /* read description, flag = cleared */
237 new_item.flag = FL_CLEARED;
238 rb->memcpy(new_item.desc, &temp_line[1], DESC_SIZE);
240 else if (rb->strncmp(temp_line, "!", 1) == 0)
242 /* read description, flag = set */
243 new_item.flag = FL_SET;
244 rb->memcpy(new_item.desc, &temp_line[1], DESC_SIZE);
246 else if (rb->strncmp(temp_line, "#", 1) == 0)
248 /* read description, flag = category */
249 new_item.flag = FL_CATEGORY;
250 rb->memcpy(new_item.desc, &temp_line[1], DESC_SIZE);
252 else
254 /* read description, flag = cleared */
255 new_item.flag = FL_CLEARED;
256 rb->memcpy(new_item.desc, temp_line, DESC_SIZE);
258 items_list[total_item_count] = new_item;
259 total_item_count++;
260 if (total_item_count == MAX_LIST_SIZE)
262 total_item_count = MAX_LIST_SIZE - 1;
263 rb->splashf(HZ*2, "Truncating shopping list to %d items",
264 MAX_LIST_SIZE - 1);
265 changed = true;
266 rb->close(fd);
267 return PLUGIN_OK;
271 rb->close(fd);
272 changed = false;
273 return PLUGIN_OK;
276 /* this is the plugin entry point */
277 enum plugin_status plugin_start(const void* parameter)
279 struct gui_synclist lists;
280 bool exit = false;
281 int button;
282 int cur_sel = 0;
284 #if LCD_DEPTH > 1
285 rb->lcd_set_backdrop(NULL);
286 #endif
288 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
289 rb->cpu_boost(1);
290 #endif
291 if (parameter)
293 rb->strcpy(filename,(char*)parameter);
295 if (load_file() == PLUGIN_ERROR)
296 return PLUGIN_ERROR;
298 else
299 return PLUGIN_ERROR;
301 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
302 rb->cpu_boost(0);
303 #endif
304 /* now dump it in the list */
305 rb->gui_synclist_init(&lists,list_get_name_cb,0, false, 1, NULL);
306 rb->gui_synclist_set_icon_callback(&lists, list_get_icon_cb);
307 rb->gui_synclist_limit_scroll(&lists,true);
308 create_view(&lists);
309 rb->gui_synclist_set_nb_items(&lists,view_item_count);
310 rb->gui_synclist_select_item(&lists, 0);
311 rb->gui_synclist_draw(&lists);
312 rb->lcd_update();
314 while (!exit)
316 rb->gui_synclist_draw(&lists);
317 cur_sel = rb->gui_synclist_get_sel_pos(&lists);
318 button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
319 if (rb->gui_synclist_do_button(&lists,&button,LIST_WRAP_UNLESS_HELD))
320 continue;
321 switch (button)
323 case ACTION_STD_CONTEXT:
324 case ACTION_STD_OK:
326 changed |= toggle(cur_sel);
327 break;
329 case ACTION_STD_MENU:
331 switch(view)
333 case EDIT_SHOPPING_LIST:
335 MENUITEM_STRINGLIST(menu, "Options", NULL,
336 "View shopping list",
337 "Clear all items",
338 "Mark all items",
339 category_string,
340 "Revert to saved",
341 "Show Playback Menu",);
343 switch (rb->do_menu(&menu, NULL, NULL, false))
345 case 0:
347 /* view shopping list */
348 view = VIEW_SHOPPING_LIST;
349 changed = true;
350 break;
352 case 1:
354 /* clear all items */
355 int i;
356 for (i = 0; i < total_item_count; i++)
358 if (items_list[i].flag == FL_SET)
359 items_list[i].flag = FL_CLEARED;
361 changed = true;
362 break;
364 case 2:
366 /* mark all items */
367 int i;
368 for (i = 0; i < total_item_count; i++)
370 if (items_list[i].flag == FL_CLEARED)
371 items_list[i].flag = FL_SET;
373 changed = true;
374 break;
376 case 3:
378 /* toggle categories */
379 show_categories ^= true;
380 update_category_string();
381 changed = true;
382 break;
384 case 4:
386 /* revert to saved */
387 if (load_file() == PLUGIN_ERROR)
388 return PLUGIN_ERROR;
389 break;
391 case 5:
393 /* playback menu */
394 playback_control(NULL);
395 break;
397 default:
399 break;
402 break;
405 case VIEW_SHOPPING_LIST:
407 MENUITEM_STRINGLIST(menu, "Options", NULL,
408 "Edit list",
409 "Reset list",
410 category_string,
411 "Revert to saved",
412 "Show Playback Menu",);
414 switch (rb->do_menu(&menu, NULL, NULL, false))
416 case 0:
418 /* edit list */
419 view = EDIT_SHOPPING_LIST;
420 changed = true;
421 break;
423 case 1:
425 /* reset list */
426 int i;
427 for (i = 0; i < total_item_count; i++)
429 if (items_list[i].flag == FL_SET)
430 items_list[i].flag = FL_CLEARED;
432 view = EDIT_SHOPPING_LIST;
433 changed = true;
434 break;
436 case 2:
438 /* toggle categories */
439 show_categories ^= true;
440 update_category_string();
441 changed = true;
442 break;
444 case 3:
446 /* revert to saved */
447 if (load_file() == PLUGIN_ERROR)
448 return PLUGIN_ERROR;
449 break;
451 case 4:
453 /* playback menu */
454 playback_control(NULL);
455 break;
457 default:
459 break;
462 break;
465 break;
467 case ACTION_STD_CANCEL:
469 if (changed)
470 save_changes();
471 exit = 1;
472 break;
476 create_view(&lists);
477 rb->gui_synclist_set_nb_items(&lists,view_item_count);
478 if (view_item_count > 0 && view_item_count <= cur_sel)
479 rb->gui_synclist_select_item(&lists,view_item_count-1);
481 return PLUGIN_OK;