1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
22 #include "lib/playback_control.h"
26 #define MAX_LIST_SIZE 400
28 #define MAX_LINE_LEN (DESC_SIZE + 1)
37 EDIT_SHOPPING_LIST
= 0,
41 #define VIEW_TYPE_SIZE VIEW_SHOPPING_LIST + 1
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
)
63 rb
->strlcpy(buf
, items_list
[view_id_list
[selected_item
]].desc
, buf_len
);
67 static enum themable_icons
list_get_icon_cb(int selected_item
, void *data
)
70 if (items_list
[view_id_list
[selected_item
]].flag
== FL_CATEGORY
)
72 else if (items_list
[view_id_list
[selected_item
]].flag
== FL_SET
)
78 bool save_changes(void)
83 fd
= rb
->open(filename
,O_WRONLY
|O_CREAT
|O_TRUNC
);
86 rb
->splash(HZ
*2, "Changes NOT saved");
90 rb
->lcd_clear_display();
91 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
94 for (i
= 0;i
< total_item_count
; i
++)
96 switch (items_list
[i
].flag
)
100 rb
->fdprintf(fd
,"#%s\n",items_list
[i
].desc
);
105 rb
->fdprintf(fd
,"!%s\n",items_list
[i
].desc
);
110 rb
->fdprintf(fd
," %s\n",items_list
[i
].desc
);
115 /* save current view */
116 rb
->fdprintf(fd
,"$%d%d\n",view
, show_categories
);
118 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
126 void create_view(struct gui_synclist
*lists
)
128 unsigned int cnt
= 0;
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
);
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
;
157 if (items_list
[j
].flag
== FL_CATEGORY
)
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
);
171 bool toggle(int selected_item
)
173 if (items_list
[view_id_list
[selected_item
]].flag
== FL_CATEGORY
)
175 else if (items_list
[view_id_list
[selected_item
]].flag
== FL_SET
)
176 items_list
[view_id_list
[selected_item
]].flag
= FL_CLEARED
;
178 items_list
[view_id_list
[selected_item
]].flag
= FL_SET
;
182 void update_category_string(void)
185 rb
->strcpy(category_string
,"Hide categories");
187 rb
->strcpy(category_string
,"Show categories");
190 enum plugin_status
load_file(void)
193 static char temp_line
[DESC_SIZE
];
194 static struct items_list_s new_item
;
195 static int count
= 0;
197 total_item_count
= 0;
199 fd
= rb
->open(filename
,O_RDONLY
);
202 rb
->splashf(HZ
*2,"Couldn't open file: %s",filename
);
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
);
215 unsigned int val
= temp_line
[1] - '0';
216 if (val
< VIEW_TYPE_SIZE
)
223 unsigned int val
= temp_line
[2] - '0';
226 show_categories
= val
;
227 update_category_string();
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
);
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
;
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",
276 /* this is the plugin entry point */
277 enum plugin_status
plugin_start(const void* parameter
)
279 struct gui_synclist lists
;
285 rb
->lcd_set_backdrop(NULL
);
288 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
293 rb
->strcpy(filename
,(char*)parameter
);
295 if (load_file() == PLUGIN_ERROR
)
301 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
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);
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
);
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
))
323 case ACTION_STD_CONTEXT
:
326 changed
|= toggle(cur_sel
);
329 case ACTION_STD_MENU
:
333 case EDIT_SHOPPING_LIST
:
335 MENUITEM_STRINGLIST(menu
, "Options", NULL
,
336 "View shopping list",
341 "Show Playback Menu",);
343 switch (rb
->do_menu(&menu
, NULL
, NULL
, false))
347 /* view shopping list */
348 view
= VIEW_SHOPPING_LIST
;
354 /* clear all items */
356 for (i
= 0; i
< total_item_count
; i
++)
358 if (items_list
[i
].flag
== FL_SET
)
359 items_list
[i
].flag
= FL_CLEARED
;
368 for (i
= 0; i
< total_item_count
; i
++)
370 if (items_list
[i
].flag
== FL_CLEARED
)
371 items_list
[i
].flag
= FL_SET
;
378 /* toggle categories */
379 show_categories
^= true;
380 update_category_string();
386 /* revert to saved */
387 if (load_file() == PLUGIN_ERROR
)
394 playback_control(NULL
);
405 case VIEW_SHOPPING_LIST
:
407 MENUITEM_STRINGLIST(menu
, "Options", NULL
,
412 "Show Playback Menu",);
414 switch (rb
->do_menu(&menu
, NULL
, NULL
, false))
419 view
= EDIT_SHOPPING_LIST
;
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
;
438 /* toggle categories */
439 show_categories
^= true;
440 update_category_string();
446 /* revert to saved */
447 if (load_file() == PLUGIN_ERROR
)
454 playback_control(NULL
);
467 case ACTION_STD_CANCEL
:
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);