Explicitely say 'minutes' when speaking the battery time, fixes FS#11932.
[kugel-rb.git] / apps / plugins / properties.c
blob80c6a83870005acb35208b26ada2327460c3262e
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 Peter D'Hoye
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"
25 bool its_a_dir = false;
27 char str_filename[MAX_PATH];
28 char str_dirname[MAX_PATH];
29 char str_size[64];
30 char str_dircount[64];
31 char str_filecount[64];
32 char str_date[64];
33 char str_time[64];
35 char str_title[MAX_PATH];
36 char str_artist[MAX_PATH];
37 char str_album[MAX_PATH];
38 char str_duration[32];
40 int num_properties;
42 static const char human_size_prefix[4] = { '\0', 'K', 'M', 'G' };
43 static unsigned human_size_log(long long size)
45 const size_t n = sizeof(human_size_prefix)/sizeof(human_size_prefix[0]);
47 unsigned i;
48 /* margin set at 10K boundary: 10239 B +1 => 10 KB */
49 for(i=0; i < n-1 && size >= 10*1024; i++)
50 size >>= 10; /* div by 1024 */
52 return i;
55 static bool file_properties(char* selected_file)
57 bool found = false;
58 char tstr[MAX_PATH];
59 DIR* dir;
60 struct dirent* entry;
61 struct mp3entry id3;
63 char* ptr = rb->strrchr(selected_file, '/') + 1;
64 int dirlen = (ptr - selected_file);
65 rb->strlcpy(tstr, selected_file, dirlen + 1);
67 dir = rb->opendir(tstr);
68 if (dir)
70 while(0 != (entry = rb->readdir(dir)))
72 struct dirinfo info = rb->dir_get_info(dir, entry);
73 if(!rb->strcmp(entry->d_name, selected_file+dirlen))
75 unsigned log;
76 rb->snprintf(str_dirname, sizeof str_dirname, "Path: %s", tstr);
77 rb->snprintf(str_filename, sizeof str_filename, "Name: %s",
78 selected_file+dirlen);
79 log = human_size_log(info.size);
80 rb->snprintf(str_size, sizeof str_size, "Size: %ld %cB",
81 info.size >> (log*10), human_size_prefix[log]);
82 rb->snprintf(str_date, sizeof str_date, "Date: %04d/%02d/%02d",
83 ((info.wrtdate >> 9 ) & 0x7F) + 1980, /* year */
84 ((info.wrtdate >> 5 ) & 0x0F), /* month */
85 ((info.wrtdate ) & 0x1F)); /* day */
86 rb->snprintf(str_time, sizeof str_time, "Time: %02d:%02d",
87 ((info.wrttime >> 11) & 0x1F), /* hour */
88 ((info.wrttime >> 5 ) & 0x3F)); /* minutes */
90 num_properties = 5;
92 #if (CONFIG_CODEC == SWCODEC)
93 int fd = rb->open(selected_file, O_RDONLY);
94 if (fd >= 0 &&
95 rb->get_metadata(&id3, fd, selected_file))
96 #else
97 if (!rb->mp3info(&id3, selected_file))
98 #endif
100 long dur = id3.length / 1000; /* seconds */
101 rb->snprintf(str_artist, sizeof str_artist,
102 "Artist: %s", id3.artist ? id3.artist : "");
103 rb->snprintf(str_title, sizeof str_title,
104 "Title: %s", id3.title ? id3.title : "");
105 rb->snprintf(str_album, sizeof str_album,
106 "Album: %s", id3.album ? id3.album : "");
107 num_properties += 3;
109 if (dur > 0)
111 if (dur < 3600)
112 rb->snprintf(str_duration, sizeof str_duration,
113 "Duration: %d:%02d", (int)(dur / 60),
114 (int)(dur % 60));
115 else
116 rb->snprintf(str_duration, sizeof str_duration,
117 "Duration: %d:%02d:%02d",
118 (int)(dur / 3600),
119 (int)(dur % 3600 / 60),
120 (int)(dur % 60));
121 num_properties++;
124 #if (CONFIG_CODEC == SWCODEC)
125 rb->close(fd);
126 #endif
127 found = true;
128 break;
131 rb->closedir(dir);
133 return found;
136 typedef struct {
137 char dirname[MAX_PATH];
138 int len;
139 unsigned int dc;
140 unsigned int fc;
141 long long bc;
142 } DPS;
144 static bool _dir_properties(DPS* dps)
146 /* recursively scan directories in search of files
147 and informs the user of the progress */
148 bool result;
149 static long lasttick=0;
150 int dirlen;
151 DIR* dir;
152 struct dirent* entry;
154 result = true;
155 dirlen = rb->strlen(dps->dirname);
156 dir = rb->opendir(dps->dirname);
157 if (!dir)
158 return false; /* open error */
160 /* walk through the directory content */
161 while(result && (0 != (entry = rb->readdir(dir))))
163 struct dirinfo info = rb->dir_get_info(dir, entry);
164 /* append name to current directory */
165 rb->snprintf(dps->dirname+dirlen, dps->len-dirlen, "/%s",
166 entry->d_name);
168 if (info.attribute & ATTR_DIRECTORY)
170 unsigned log;
172 if (!rb->strcmp((char *)entry->d_name, ".") ||
173 !rb->strcmp((char *)entry->d_name, ".."))
174 continue; /* skip these */
176 dps->dc++; /* new directory */
177 if (*rb->current_tick - lasttick > (HZ/8))
179 lasttick = *rb->current_tick;
180 rb->lcd_clear_display();
181 rb->lcd_puts(0,0,"SCANNING...");
182 rb->lcd_puts(0,1,dps->dirname);
183 rb->lcd_puts(0,2,entry->d_name);
184 rb->lcd_putsf(0,3,"Directories: %d", dps->dc);
185 rb->lcd_putsf(0,4,"Files: %d", dps->fc);
186 log = human_size_log(dps->bc);
187 rb->lcd_putsf(0,5,"Size: %ld %cB", (long) (dps->bc >> (10*log)),
188 human_size_prefix[log]);
189 rb->lcd_update();
192 /* recursion */
193 result = _dir_properties(dps);
195 else
197 dps->fc++; /* new file */
198 dps->bc += info.size;
200 if(ACTION_STD_CANCEL == rb->get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
201 result = false;
202 rb->yield();
204 rb->closedir(dir);
205 return result;
208 static bool dir_properties(char* selected_file)
210 unsigned log;
211 DPS dps = {
212 .len = MAX_PATH,
213 .dc = 0,
214 .fc = 0,
215 .bc = 0,
217 rb->strlcpy(dps.dirname, selected_file, MAX_PATH);
219 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
220 rb->cpu_boost(true);
221 #endif
223 if(false == _dir_properties(&dps))
224 return false;
226 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
227 rb->cpu_boost(false);
228 #endif
230 rb->strlcpy(str_dirname, selected_file, MAX_PATH);
231 rb->snprintf(str_dircount, sizeof str_dircount, "Subdirs: %d", dps.dc);
232 rb->snprintf(str_filecount, sizeof str_filecount, "Files: %d", dps.fc);
233 log = human_size_log(dps.bc);
234 rb->snprintf(str_size, sizeof str_size, "Size: %ld %cB",
235 (long) (dps.bc >> (log*10)), human_size_prefix[log]);
236 num_properties = 4;
237 return true;
240 static const char * get_props(int selected_item, void* data,
241 char *buffer, size_t buffer_len)
243 (void)data;
245 switch(selected_item)
247 case 0:
248 rb->strlcpy(buffer, str_dirname, buffer_len);
249 break;
250 case 1:
251 rb->strlcpy(buffer, its_a_dir ? str_dircount : str_filename,
252 buffer_len);
253 break;
254 case 2:
255 rb->strlcpy(buffer, its_a_dir ? str_filecount : str_size, buffer_len);
256 break;
257 case 3:
258 rb->strlcpy(buffer, its_a_dir ? str_size : str_date, buffer_len);
259 break;
260 case 4:
261 rb->strlcpy(buffer, its_a_dir ? "" : str_time, buffer_len);
262 break;
263 case 5:
264 rb->strlcpy(buffer, its_a_dir ? "" : str_artist, buffer_len);
265 break;
266 case 6:
267 rb->strlcpy(buffer, its_a_dir ? "" : str_title, buffer_len);
268 break;
269 case 7:
270 rb->strlcpy(buffer, its_a_dir ? "" : str_album, buffer_len);
271 break;
272 case 8:
273 rb->strlcpy(buffer, its_a_dir ? "" : str_duration, buffer_len);
274 break;
275 default:
276 return "ERROR";
278 return buffer;
281 enum plugin_status plugin_start(const void* parameter)
283 struct gui_synclist properties_lists;
284 #ifdef HAVE_LCD_BITMAP
285 int i;
286 #endif
287 int button;
288 bool quit = false, usb = false;
289 char file[MAX_PATH];
290 if(!parameter) return PLUGIN_ERROR;
291 rb->strcpy(file, (const char *) parameter);
293 /* determine if it's a file or a directory */
294 bool found = false;
295 DIR* dir;
296 struct dirent* entry;
297 char* ptr = rb->strrchr(file, '/') + 1;
298 int dirlen = (ptr - file);
299 rb->strlcpy(str_dirname, file, dirlen + 1);
301 dir = rb->opendir(str_dirname);
302 if (dir)
304 while(0 != (entry = rb->readdir(dir)))
306 if(!rb->strcmp(entry->d_name, file+dirlen))
308 struct dirinfo info = rb->dir_get_info(dir, entry);
309 its_a_dir = info.attribute & ATTR_DIRECTORY ? true : false;
310 found = true;
311 break;
314 rb->closedir(dir);
316 /* now we know if it's a file or a dir or maybe something failed */
318 if(!found)
320 /* weird: we couldn't find the entry. This Should Never Happen (TM) */
321 rb->splashf(0, "File/Dir not found: %s", file);
322 rb->action_userabort(TIMEOUT_BLOCK);
323 return PLUGIN_OK;
326 /* get the info depending on its_a_dir */
327 if(!(its_a_dir ? dir_properties(file) : file_properties(file)))
329 /* something went wrong (to do: tell user what it was (nesting,...) */
330 rb->splash(0, "Failed to gather information");
331 rb->action_userabort(TIMEOUT_BLOCK);
332 return PLUGIN_OK;
335 #ifdef HAVE_LCD_BITMAP
336 FOR_NB_SCREENS(i)
337 rb->viewportmanager_theme_enable(i, true, NULL);
338 #endif
340 rb->gui_synclist_init(&properties_lists, &get_props, file, false, 1, NULL);
341 rb->gui_synclist_set_title(&properties_lists, its_a_dir ?
342 "Directory properties" :
343 "File properties", NOICON);
344 rb->gui_synclist_set_icon_callback(&properties_lists, NULL);
345 rb->gui_synclist_set_nb_items(&properties_lists, num_properties);
346 rb->gui_synclist_limit_scroll(&properties_lists, true);
347 rb->gui_synclist_select_item(&properties_lists, 0);
348 rb->gui_synclist_draw(&properties_lists);
350 while(!quit)
352 button = rb->get_action(CONTEXT_LIST, HZ);
353 /* HZ so the status bar redraws corectly */
354 if (rb->gui_synclist_do_button(&properties_lists,&button,LIST_WRAP_ON))
355 continue;
356 switch(button)
358 case ACTION_STD_CANCEL:
359 quit = true;
360 break;
361 default:
362 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
364 quit = true;
365 usb = true;
367 break;
371 #ifdef HAVE_LCD_BITMAP
372 FOR_NB_SCREENS(i)
373 rb->viewportmanager_theme_undo(i, false);
374 #endif
376 return usb? PLUGIN_USB_CONNECTED: PLUGIN_OK;