Use the AMS_LOWMEM define to indicate memory size as the .lds files do in config...
[kugel-rb.git] / apps / plugins / properties.c
blob81e10812ccf475c4e3e3b447c8855f25e4a4d684
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"
23 PLUGIN_HEADER
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 char* filesize2string(long long size, char* pstr, int len)
44 /* margin set at 10K boundary: 10239 B +1 => 10 KB
45 routine below is 200 bytes smaller than cascaded if/else :)
46 not using build-in function because of huge values (long long) */
47 const char* kgb[4] = { "B", "KB", "MB", "GB" };
48 int i = 0;
49 while(true)
51 if((size < 10240) || (i > 2))
53 /* depends on the order in the above array */
54 rb->snprintf(pstr, len, "%ld %s", (long)size, kgb[i]);
55 break;
57 size >>= 10; /* div by 1024 */
58 i++;
60 return pstr;
63 static bool file_properties(char* selected_file)
65 bool found = false;
66 char tstr[MAX_PATH];
67 DIR* dir;
68 struct dirent* entry;
69 struct mp3entry id3;
71 char* ptr = rb->strrchr(selected_file, '/') + 1;
72 int dirlen = (ptr - selected_file);
73 rb->strncpy(tstr, selected_file, dirlen);
74 tstr[dirlen] = 0;
76 dir = rb->opendir(tstr);
77 if (dir)
79 while(0 != (entry = rb->readdir(dir)))
81 if(!rb->strcmp(entry->d_name, selected_file+dirlen))
83 rb->snprintf(str_dirname, sizeof str_dirname, "Path: %s",
84 tstr);
85 rb->snprintf(str_filename, sizeof str_filename, "Name: %s",
86 selected_file+dirlen);
87 rb->snprintf(str_size, sizeof str_size, "Size: %s",
88 filesize2string(entry->size, tstr, sizeof tstr));
89 rb->snprintf(str_date, sizeof str_date, "Date: %04d/%02d/%02d",
90 ((entry->wrtdate >> 9 ) & 0x7F) + 1980, /* year */
91 ((entry->wrtdate >> 5 ) & 0x0F), /* month */
92 ((entry->wrtdate ) & 0x1F)); /* day */
93 rb->snprintf(str_time, sizeof str_time, "Time: %02d:%02d",
94 ((entry->wrttime >> 11) & 0x1F), /* hour */
95 ((entry->wrttime >> 5 ) & 0x3F)); /* minutes */
97 num_properties = 5;
99 #if (CONFIG_CODEC == SWCODEC)
100 int fd = rb->open(selected_file, O_RDONLY);
101 if (fd >= 0 &&
102 rb->get_metadata(&id3, fd, selected_file))
103 #else
104 if (!rb->mp3info(&id3, selected_file))
105 #endif
107 long dur = id3.length / 1000; /* seconds */
108 rb->snprintf(str_artist, sizeof str_artist,
109 "Artist: %s", id3.artist ? id3.artist : "");
110 rb->snprintf(str_title, sizeof str_title,
111 "Title: %s", id3.title ? id3.title : "");
112 rb->snprintf(str_album, sizeof str_album,
113 "Album: %s", id3.album ? id3.album : "");
114 num_properties += 3;
116 if (dur > 0)
118 if (dur < 3600)
119 rb->snprintf(str_duration, sizeof str_duration,
120 "Duration: %d:%02d", (int)(dur / 60),
121 (int)(dur % 60));
122 else
123 rb->snprintf(str_duration, sizeof str_duration,
124 "Duration: %d:%02d:%02d",
125 (int)(dur / 3600),
126 (int)(dur % 3600 / 60),
127 (int)(dur % 60));
128 num_properties++;
131 #if (CONFIG_CODEC == SWCODEC)
132 rb->close(fd);
133 #endif
134 found = true;
135 break;
138 rb->closedir(dir);
140 return found;
143 typedef struct {
144 char dirname[MAX_PATH];
145 int len;
146 unsigned int dc;
147 unsigned int fc;
148 long long bc;
149 char tstr[64];
150 char tstr2[64];
151 } DPS;
153 static bool _dir_properties(DPS* dps)
155 /* recursively scan directories in search of files
156 and informs the user of the progress */
157 bool result;
158 int dirlen;
159 DIR* dir;
160 struct dirent* entry;
162 result = true;
163 dirlen = rb->strlen(dps->dirname);
164 dir = rb->opendir(dps->dirname);
165 if (!dir)
166 return false; /* open error */
168 /* walk through the directory content */
169 while(result && (0 != (entry = rb->readdir(dir))))
171 /* append name to current directory */
172 rb->snprintf(dps->dirname+dirlen, dps->len-dirlen, "/%s",
173 entry->d_name);
175 if (entry->attribute & ATTR_DIRECTORY)
177 if (!rb->strcmp((char *)entry->d_name, ".") ||
178 !rb->strcmp((char *)entry->d_name, ".."))
179 continue; /* skip these */
181 dps->dc++; /* new directory */
182 rb->lcd_clear_display();
183 rb->lcd_puts(0,0,"SCANNING...");
184 rb->lcd_puts(0,1,dps->dirname);
185 rb->lcd_puts(0,2,entry->d_name);
186 rb->snprintf(dps->tstr, 64, "Directories: %d", dps->dc);
187 rb->lcd_puts(0,3,dps->tstr);
188 rb->snprintf(dps->tstr, 64, "Files: %d", dps->fc);
189 rb->lcd_puts(0,4,dps->tstr);
190 rb->snprintf(dps->tstr, 64, "Size: %s",
191 filesize2string(dps->bc, dps->tstr2, 64));
192 rb->lcd_puts(0,5,dps->tstr);
193 rb->lcd_update();
195 /* recursion */
196 result = _dir_properties(dps);
198 else
200 dps->fc++; /* new file */
201 dps->bc += entry->size;
203 if(ACTION_STD_CANCEL == rb->get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
204 result = false;
205 rb->yield();
207 rb->closedir(dir);
208 return result;
211 static bool dir_properties(char* selected_file)
213 DPS dps;
214 char tstr[64];
215 rb->strncpy(dps.dirname, selected_file, MAX_PATH);
216 dps.len = MAX_PATH;
217 dps.dc = 0;
218 dps.fc = 0;
219 dps.bc = 0;
220 if(false == _dir_properties(&dps))
221 return false;
223 rb->strncpy(str_dirname, selected_file, MAX_PATH);
224 rb->snprintf(str_dircount, sizeof str_dircount, "Subdirs: %d", dps.dc);
225 rb->snprintf(str_filecount, sizeof str_filecount, "Files: %d", dps.fc);
226 rb->snprintf(str_size, sizeof str_size, "Size: %s",
227 filesize2string(dps.bc, tstr, sizeof tstr));
228 num_properties = 4;
229 return true;
232 char * get_props(int selected_item, void* data, char *buffer, size_t buffer_len)
234 (void)data;
236 switch(selected_item)
238 case 0:
239 rb->strncpy(buffer, str_dirname, buffer_len);
240 break;
241 case 1:
242 rb->strncpy(buffer, its_a_dir ? str_dircount : str_filename,
243 buffer_len);
244 break;
245 case 2:
246 rb->strncpy(buffer, its_a_dir ? str_filecount : str_size, buffer_len);
247 break;
248 case 3:
249 rb->strncpy(buffer, its_a_dir ? str_size : str_date, buffer_len);
250 break;
251 case 4:
252 rb->strncpy(buffer, its_a_dir ? "" : str_time, buffer_len);
253 break;
254 case 5:
255 rb->strncpy(buffer, its_a_dir ? "" : str_artist, buffer_len);
256 break;
257 case 6:
258 rb->strncpy(buffer, its_a_dir ? "" : str_title, buffer_len);
259 break;
260 case 7:
261 rb->strncpy(buffer, its_a_dir ? "" : str_album, buffer_len);
262 break;
263 case 8:
264 rb->strncpy(buffer, its_a_dir ? "" : str_duration, buffer_len);
265 break;
266 default:
267 rb->strncpy(buffer, "ERROR", buffer_len);
268 break;
270 return buffer;
273 enum plugin_status plugin_start(const void* parameter)
275 struct gui_synclist properties_lists;
276 int button;
277 bool prev_show_statusbar;
278 bool quit = false;
279 char file[MAX_PATH];
280 rb->strcpy(file, (const char *) parameter);
282 /* determine if it's a file or a directory */
283 bool found = false;
284 DIR* dir;
285 struct dirent* entry;
286 char* ptr = rb->strrchr(file, '/') + 1;
287 int dirlen = (ptr - file);
288 rb->strncpy(str_dirname, file, dirlen);
289 str_dirname[dirlen] = 0;
291 dir = rb->opendir(str_dirname);
292 if (dir)
294 while(0 != (entry = rb->readdir(dir)))
296 if(!rb->strcmp(entry->d_name, file+dirlen))
298 its_a_dir = entry->attribute & ATTR_DIRECTORY ? true : false;
299 found = true;
300 break;
303 rb->closedir(dir);
305 /* now we know if it's a file or a dir or maybe something failed */
307 if(!found)
309 /* weird: we couldn't find the entry. This Should Never Happen (TM) */
310 rb->splashf(0, "File/Dir not found: %s", file);
311 rb->action_userabort(TIMEOUT_BLOCK);
312 return PLUGIN_OK;
315 /* get the info depending on its_a_dir */
316 if(!(its_a_dir ? dir_properties(file) : file_properties(file)))
318 /* something went wrong (to do: tell user what it was (nesting,...) */
319 rb->splash(0, "Failed to gather information");
320 rb->action_userabort(TIMEOUT_BLOCK);
321 return PLUGIN_OK;
324 /* prepare the list for the user */
325 prev_show_statusbar = rb->global_settings->statusbar;
326 rb->global_settings->statusbar = false;
328 rb->gui_synclist_init(&properties_lists, &get_props, file, false, 1, NULL);
329 rb->gui_synclist_set_title(&properties_lists, its_a_dir ?
330 "Directory properties" :
331 "File properties", NOICON);
332 rb->gui_synclist_set_icon_callback(&properties_lists, NULL);
333 rb->gui_synclist_set_nb_items(&properties_lists, num_properties);
334 rb->gui_synclist_limit_scroll(&properties_lists, true);
335 rb->gui_synclist_select_item(&properties_lists, 0);
336 rb->gui_synclist_draw(&properties_lists);
338 while(!quit)
340 button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
341 if (rb->gui_synclist_do_button(&properties_lists,&button,LIST_WRAP_ON))
342 continue;
343 switch(button)
345 case ACTION_STD_CANCEL:
346 quit = true;
347 break;
348 default:
349 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
351 rb->global_settings->statusbar = prev_show_statusbar;
352 return PLUGIN_USB_CONNECTED;
356 rb->global_settings->statusbar = prev_show_statusbar;
358 return PLUGIN_OK;