summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Soffke <christian.soffke@gmail.com>2024-07-20 17:54:41 +0200
committerChristian Soffke <christian.soffke@gmail.com>2024-07-21 13:50:39 +0200
commita2865555783c368ec1abf87dffabbaf64c572936 (patch)
treeb2655e30e61739f90aae24b1dabca1e102762112
parenta983e532fa1ab46aa4c0b8fefcf5fd51a3716481 (diff)
downloadrockbox-a2865555783c368ec1abf87dffabbaf64c572936.tar.gz
rockbox-a2865555783c368ec1abf87dffabbaf64c572936.zip
plugins: properties: Add Directory Track Info
Change-Id: I4bf5ececc73a52afa6c5ff46f23b5ad2368826c0
-rw-r--r--apps/plugins/properties.c205
1 files changed, 129 insertions, 76 deletions
diff --git a/apps/plugins/properties.c b/apps/plugins/properties.c
index 32bc8b9150..73ec64be80 100644
--- a/apps/plugins/properties.c
+++ b/apps/plugins/properties.c
@@ -30,6 +30,7 @@ struct dir_stats {
30 int len; 30 int len;
31 unsigned int dir_count; 31 unsigned int dir_count;
32 unsigned int file_count; 32 unsigned int file_count;
33 unsigned int audio_file_count;
33 unsigned long long byte_count; 34 unsigned long long byte_count;
34}; 35};
35 36
@@ -52,6 +53,7 @@ static char str_dirname[MAX_PATH];
52static char str_size[64]; 53static char str_size[64];
53static char str_dircount[64]; 54static char str_dircount[64];
54static char str_filecount[64]; 55static char str_filecount[64];
56static char str_audio_filecount[64];
55static char str_date[64]; 57static char str_date[64];
56static char str_time[64]; 58static char str_time[64];
57 59
@@ -68,16 +70,20 @@ static const unsigned char* const props_file[] =
68 ID2P(LANG_PROPERTIES_SIZE), str_size, 70 ID2P(LANG_PROPERTIES_SIZE), str_size,
69 ID2P(LANG_PROPERTIES_DATE), str_date, 71 ID2P(LANG_PROPERTIES_DATE), str_date,
70 ID2P(LANG_PROPERTIES_TIME), str_time, 72 ID2P(LANG_PROPERTIES_TIME), str_time,
73
71 ID2P(LANG_MENU_SHOW_ID3_INFO), "...", 74 ID2P(LANG_MENU_SHOW_ID3_INFO), "...",
72}; 75};
73 76
74#define NUM_DIR_PROPERTIES 4 77#define NUM_DIR_PROPERTIES 4
78#define NUM_AUDIODIR_PROPERTIES 1 + NUM_DIR_PROPERTIES
75static const unsigned char* const props_dir[] = 79static const unsigned char* const props_dir[] =
76{ 80{
77 ID2P(LANG_PROPERTIES_PATH), str_dirname, 81 ID2P(LANG_PROPERTIES_PATH), str_dirname,
78 ID2P(LANG_PROPERTIES_SUBDIRS), str_dircount, 82 ID2P(LANG_PROPERTIES_SUBDIRS), str_dircount,
79 ID2P(LANG_PROPERTIES_FILES), str_filecount, 83 ID2P(LANG_PROPERTIES_FILES), str_filecount,
80 ID2P(LANG_PROPERTIES_SIZE), str_size, 84 ID2P(LANG_PROPERTIES_SIZE), str_size,
85
86 ID2P(LANG_MENU_SHOW_ID3_INFO), str_audio_filecount,
81}; 87};
82 88
83static const int32_t units[] = 89static const int32_t units[] =
@@ -88,11 +94,11 @@ static const int32_t units[] =
88 LANG_GIBIBYTE 94 LANG_GIBIBYTE
89}; 95};
90 96
91static unsigned human_size_log(unsigned long long size) 97static unsigned int human_size_log(unsigned long long size)
92{ 98{
93 const size_t n = sizeof(units)/sizeof(units[0]); 99 const size_t n = sizeof(units)/sizeof(units[0]);
94 100
95 unsigned i; 101 unsigned int i;
96 /* margin set at 10K boundary: 10239 B +1 => 10 KB */ 102 /* margin set at 10K boundary: 10239 B +1 => 10 KB */
97 for(i=0; i < n-1 && size >= 10*1024; i++) 103 for(i=0; i < n-1 && size >= 10*1024; i++)
98 size >>= 10; /* div by 1024 */ 104 size >>= 10; /* div by 1024 */
@@ -103,10 +109,8 @@ static unsigned human_size_log(unsigned long long size)
103static bool file_properties(const char* selected_file) 109static bool file_properties(const char* selected_file)
104{ 110{
105 bool found = false; 111 bool found = false;
106 DIR* dir;
107 struct dirent* entry; 112 struct dirent* entry;
108 113 DIR* dir = rb->opendir(str_dirname);
109 dir = rb->opendir(str_dirname);
110 if (dir) 114 if (dir)
111 { 115 {
112 while(0 != (entry = rb->readdir(dir))) 116 while(0 != (entry = rb->readdir(dir)))
@@ -114,7 +118,7 @@ static bool file_properties(const char* selected_file)
114 struct dirinfo info = rb->dir_get_info(dir, entry); 118 struct dirinfo info = rb->dir_get_info(dir, entry);
115 if(!rb->strcmp(entry->d_name, str_filename)) 119 if(!rb->strcmp(entry->d_name, str_filename))
116 { 120 {
117 unsigned log; 121 unsigned int log;
118 log = human_size_log((unsigned long)info.size); 122 log = human_size_log((unsigned long)info.size);
119 nsize = ((unsigned long)info.size) >> (log*10); 123 nsize = ((unsigned long)info.size) >> (log*10);
120 size_unit = units[log]; 124 size_unit = units[log];
@@ -126,7 +130,7 @@ static bool file_properties(const char* selected_file)
126 rb->snprintf(str_time, sizeof str_time, "%02d:%02d:%02d", 130 rb->snprintf(str_time, sizeof str_time, "%02d:%02d:%02d",
127 tm.tm_hour, tm.tm_min, tm.tm_sec); 131 tm.tm_hour, tm.tm_min, tm.tm_sec);
128 132
129 if (!rb->mp3info(&id3, selected_file)) 133 if (props_type != PROPS_PLAYLIST && !rb->mp3info(&id3, selected_file))
130 props_type = PROPS_ID3; 134 props_type = PROPS_ID3;
131 found = true; 135 found = true;
132 break; 136 break;
@@ -137,32 +141,29 @@ static bool file_properties(const char* selected_file)
137 return found; 141 return found;
138} 142}
139 143
140static bool _dir_properties(struct dir_stats *stats) 144/* Recursively scans directories in search of files
145 * and informs the user of the progress.
146 */
147static bool _dir_properties(struct dir_stats *stats, bool (*id3_cb)(const char*))
141{ 148{
142 /* recursively scan directories in search of files 149 bool result = true;
143 and informs the user of the progress */ 150 static long last_tick = 0;
144 bool result;
145 static long lasttick=0;
146 int dirlen;
147 DIR* dir;
148 struct dirent* entry; 151 struct dirent* entry;
149 152 int dirlen = rb->strlen(stats->dirname);
150 result = true; 153 DIR* dir = rb->opendir(stats->dirname);
151 dirlen = rb->strlen(stats->dirname);
152 dir = rb->opendir(stats->dirname);
153 if (!dir) 154 if (!dir)
154 { 155 {
155 rb->splashf(HZ*2, "%s", stats->dirname); 156 rb->splashf(HZ*2, "open error: %s", stats->dirname);
156 return false; /* open error */ 157 return false;
157 } 158 }
158 159
159 /* walk through the directory content */ 160 /* walk through the directory content */
160 while(result && (0 != (entry = rb->readdir(dir)))) 161 while(result && (0 != (entry = rb->readdir(dir))))
161 { 162 {
162 struct dirinfo info = rb->dir_get_info(dir, entry); 163 struct dirinfo info = rb->dir_get_info(dir, entry);
163 /* append name to current directory */ 164
164 rb->snprintf(stats->dirname+dirlen, stats->len-dirlen, "/%s", 165 rb->snprintf(stats->dirname + dirlen, stats->len - dirlen, "/%s",
165 entry->d_name); 166 entry->d_name); /* append name to current directory */
166 167
167 if (info.attribute & ATTR_DIRECTORY) 168 if (info.attribute & ATTR_DIRECTORY)
168 { 169 {
@@ -170,31 +171,43 @@ static bool _dir_properties(struct dir_stats *stats)
170 !rb->strcmp((char *)entry->d_name, "..")) 171 !rb->strcmp((char *)entry->d_name, ".."))
171 continue; /* skip these */ 172 continue; /* skip these */
172 173
173 stats->dir_count++; /* new directory */ 174 if (!id3_cb)
174 if (*rb->current_tick - lasttick > (HZ/8))
175 { 175 {
176 unsigned log; 176 stats->dir_count++; /* new directory */
177 lasttick = *rb->current_tick; 177 if (*rb->current_tick - last_tick > (HZ/8))
178 rb->lcd_clear_display(); 178 {
179 rb->lcd_puts(0,0,"SCANNING..."); 179 unsigned int log;
180 rb->lcd_puts(0,1,stats->dirname); 180 last_tick = *(rb->current_tick);
181 rb->lcd_putsf(0,2,"Directories: %d", stats->dir_count); 181 rb->lcd_clear_display();
182 rb->lcd_putsf(0,3,"Files: %d", stats->file_count); 182 rb->lcd_puts(0, 0, "SCANNING...");
183 log = human_size_log(stats->byte_count); 183 rb->lcd_puts(0, 1, stats->dirname);
184 rb->lcd_putsf(0,4,"Size: %lu %s", 184 rb->lcd_putsf(0, 2, "Directories: %d", stats->dir_count);
185 (unsigned long)(stats->byte_count >> (10*log)), 185 rb->lcd_putsf(0, 3, "Files: %d (Audio: %d)",
186 rb->str(units[log])); 186 stats->file_count, stats->audio_file_count);
187 rb->lcd_update(); 187 log = human_size_log(stats->byte_count);
188 rb->lcd_putsf(0, 4, "Size: %lu %s",
189 (unsigned long)(stats->byte_count >> (10*log)),
190 rb->str(units[log]));
191 rb->lcd_update();
192 }
188 } 193 }
189 194
190 /* recursion */ 195 result = _dir_properties(stats, id3_cb); /* recursion */
191 result = _dir_properties(stats);
192 } 196 }
193 else 197 else if (!id3_cb)
194 { 198 {
195 stats->file_count++; /* new file */ 199 stats->file_count++; /* new file */
196 stats->byte_count += info.size; 200 stats->byte_count += info.size;
201 if (rb->filetype_get_attr(entry->d_name) == FILE_ATTR_AUDIO)
202 stats->audio_file_count++;
197 } 203 }
204 else if (rb->filetype_get_attr(entry->d_name) == FILE_ATTR_AUDIO)
205 {
206 rb->splash_progress(mul_id3_count, stats->audio_file_count, "%s (%s)",
207 rb->str(LANG_WAIT), rb->str(LANG_OFF_ABORT));
208 id3_cb(stats->dirname); /* allow metadata to be collected */
209 }
210
198 if(ACTION_STD_CANCEL == rb->get_action(CONTEXT_STD,TIMEOUT_NOBLOCK)) 211 if(ACTION_STD_CANCEL == rb->get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
199 result = false; 212 result = false;
200 rb->yield(); 213 rb->yield();
@@ -203,35 +216,48 @@ static bool _dir_properties(struct dir_stats *stats)
203 return result; 216 return result;
204} 217}
205 218
206static bool dir_properties(const char* selected_file, struct dir_stats *stats) 219/* 1) If id3_cb is null, dir_properties calculates all dir stats, including the
220 * audio file count.
221 *
222 * 2) If id3_cb points to a function, dir_properties will call it for every audio
223 * file encountered, to allow the file's metadata to be collected. The displayed
224 * progress bar's maximum value is set to the audio file count.
225 * Stats are assumed to have already been generated by a preceding run.
226 */
227static bool dir_properties(const char* selected_file, struct dir_stats *stats,
228 bool (*id3_cb)(const char*))
207{ 229{
208 unsigned log; 230 unsigned int log;
231 bool success;
209 232
210 rb->strlcpy(stats->dirname, selected_file, MAX_PATH); 233 rb->strlcpy(stats->dirname, selected_file, MAX_PATH);
234 if (id3_cb)
235 rb->splash_progress_set_delay(HZ / 2); /* hide progress bar for 0.5s */
211 236
212#ifdef HAVE_ADJUSTABLE_CPU_FREQ 237#ifdef HAVE_ADJUSTABLE_CPU_FREQ
213 rb->cpu_boost(true); 238 rb->cpu_boost(true);
214#endif 239#endif
215 240 success = _dir_properties(stats, id3_cb);
216 if (!_dir_properties(stats)) 241
217 {
218#ifdef HAVE_ADJUSTABLE_CPU_FREQ
219 rb->cpu_boost(false);
220#endif
221 return false;
222 }
223
224#ifdef HAVE_ADJUSTABLE_CPU_FREQ 242#ifdef HAVE_ADJUSTABLE_CPU_FREQ
225 rb->cpu_boost(false); 243 rb->cpu_boost(false);
226#endif 244#endif
227 245
228 rb->strlcpy(str_dirname, selected_file, MAX_PATH); 246 if (!success)
229 rb->snprintf(str_dircount, sizeof str_dircount, "%d", stats->dir_count); 247 return false;
230 rb->snprintf(str_filecount, sizeof str_filecount, "%d", stats->file_count); 248
231 log = human_size_log(stats->byte_count); 249 if (!id3_cb)
232 nsize = (long) (stats->byte_count >> (log*10)); 250 {
233 size_unit = units[log]; 251 rb->strlcpy(str_dirname, selected_file, MAX_PATH);
234 rb->snprintf(str_size, sizeof str_size, "%ld %s", nsize, rb->str(size_unit)); 252 rb->snprintf(str_dircount, sizeof str_dircount, "%d", stats->dir_count);
253 rb->snprintf(str_filecount, sizeof str_filecount, "%d", stats->file_count);
254 rb->snprintf(str_audio_filecount, sizeof str_filecount, "%d",
255 stats->audio_file_count);
256 log = human_size_log(stats->byte_count);
257 nsize = (long) (stats->byte_count >> (log*10));
258 size_unit = units[log];
259 rb->snprintf(str_size, sizeof str_size, "%ld %s", nsize, rb->str(size_unit));
260 }
235 return true; 261 return true;
236} 262}
237 263
@@ -313,7 +339,18 @@ static int speak_property_selection(int selected_item, void *data)
313static int browse_file_or_dir(struct dir_stats *stats) 339static int browse_file_or_dir(struct dir_stats *stats)
314{ 340{
315 struct gui_synclist properties_lists; 341 struct gui_synclist properties_lists;
316 int button; 342 int button, nb_items;
343
344 if (props_type == PROPS_FILE)
345 nb_items = NUM_FILE_PROPERTIES;
346 else if (props_type == PROPS_PLAYLIST)
347 nb_items = NUM_PLAYLIST_PROPERTIES;
348 else if (stats->audio_file_count)
349 nb_items = NUM_AUDIODIR_PROPERTIES;
350 else
351 nb_items = NUM_DIR_PROPERTIES;
352
353 nb_items *= 2;
317 354
318 rb->gui_synclist_init(&properties_lists, &get_props, stats, false, 2, NULL); 355 rb->gui_synclist_init(&properties_lists, &get_props, stats, false, 2, NULL);
319 rb->gui_synclist_set_title(&properties_lists, 356 rb->gui_synclist_set_title(&properties_lists,
@@ -324,10 +361,7 @@ static int browse_file_or_dir(struct dir_stats *stats)
324 rb->gui_synclist_set_icon_callback(&properties_lists, NULL); 361 rb->gui_synclist_set_icon_callback(&properties_lists, NULL);
325 if (rb->global_settings->talk_menu) 362 if (rb->global_settings->talk_menu)
326 rb->gui_synclist_set_voice_callback(&properties_lists, speak_property_selection); 363 rb->gui_synclist_set_voice_callback(&properties_lists, speak_property_selection);
327 rb->gui_synclist_set_nb_items(&properties_lists, 364 rb->gui_synclist_set_nb_items(&properties_lists, nb_items);
328 2 * (props_type == PROPS_FILE ? NUM_FILE_PROPERTIES :
329 props_type == PROPS_PLAYLIST ?
330 NUM_PLAYLIST_PROPERTIES : NUM_DIR_PROPERTIES));
331 rb->gui_synclist_select_item(&properties_lists, 0); 365 rb->gui_synclist_select_item(&properties_lists, 0);
332 rb->gui_synclist_draw(&properties_lists); 366 rb->gui_synclist_draw(&properties_lists);
333 rb->gui_synclist_speak_item(&properties_lists); 367 rb->gui_synclist_speak_item(&properties_lists);
@@ -341,9 +375,10 @@ static int browse_file_or_dir(struct dir_stats *stats)
341 switch(button) 375 switch(button)
342 { 376 {
343 case ACTION_STD_OK: 377 case ACTION_STD_OK:
344 if (props_type == PROPS_PLAYLIST && 378 if ((props_type == PROPS_PLAYLIST || props_type == PROPS_DIR) &&
345 rb->gui_synclist_get_sel_pos(&properties_lists) 379 rb->gui_synclist_get_sel_pos(&properties_lists)
346 == ARRAY_SIZE(props_file) - 2) 380 == (props_type == PROPS_DIR ?
381 ARRAY_SIZE(props_dir) : ARRAY_SIZE(props_file)) - 2)
347 return -1; 382 return -1;
348 break; 383 break;
349 case ACTION_STD_CANCEL: 384 case ACTION_STD_CANCEL:
@@ -398,18 +433,36 @@ static bool has_pl_extension(const char* filename)
398 return (dot && (!rb->strcasecmp(dot, ".m3u") || !rb->strcasecmp(dot, ".m3u8"))); 433 return (dot && (!rb->strcasecmp(dot, ".m3u") || !rb->strcasecmp(dot, ".m3u8")));
399} 434}
400 435
401/* Assemble track info from a database table or the contents of a playlist file */ 436/* Assemble track info from a database table, the contents of a playlist file, or a dir */
402static bool assemble_track_info(const char *filename) 437static bool assemble_track_info(const char *filename, struct dir_stats *stats)
403{ 438{
404 props_type = PROPS_MUL_ID3; 439 if (!filename)
440 props_type = PROPS_MUL_ID3;
405 mul_id3_count = skipped_count = 0; 441 mul_id3_count = skipped_count = 0;
406 442
407 if ( (filename && !rb->playlist_entries_iterate(filename, NULL, &mul_id3_add))
408#ifdef HAVE_TAGCACHE 443#ifdef HAVE_TAGCACHE
409 || (!filename && !rb->tagtree_subentries_do_action(&mul_id3_add)) 444 if (props_type == PROPS_MUL_ID3 && !rb->tagtree_subentries_do_action(&mul_id3_add))
445 return false;
446 else
410#endif 447#endif
411 || mul_id3_count == 0) 448 {
449 if (props_type == PROPS_DIR)
450 {
451 if (!dir_properties(filename, stats, &mul_id3_add))
452 return false;
453 }
454 else if(props_type == PROPS_PLAYLIST)
455 {
456 if (!rb->playlist_entries_iterate(filename, NULL, &mul_id3_add))
457 return false;
458 }
459 }
460
461 if (mul_id3_count == 0)
462 {
463 rb->splashf(HZ*2, "None found");
412 return false; 464 return false;
465 }
413 else if (mul_id3_count > 1) /* otherwise, the retrieved id3 can be used as-is */ 466 else if (mul_id3_count > 1) /* otherwise, the retrieved id3 can be used as-is */
414 finalize_id3(&id3); 467 finalize_id3(&id3);
415 468
@@ -427,6 +480,7 @@ enum plugin_status plugin_start(const void* parameter)
427 .len = MAX_PATH, 480 .len = MAX_PATH,
428 .dir_count = 0, 481 .dir_count = 0,
429 .file_count = 0, 482 .file_count = 0,
483 .audio_file_count = 0,
430 .byte_count = 0, 484 .byte_count = 0,
431 }; 485 };
432 486
@@ -438,7 +492,7 @@ enum plugin_status plugin_start(const void* parameter)
438 rb->touchscreen_set_mode(rb->global_settings->touch_mode); 492 rb->touchscreen_set_mode(rb->global_settings->touch_mode);
439#endif 493#endif
440 494
441 if (file[0] == '/') /* single file selected */ 495 if (file[0] == '/') /* single file or folder selected */
442 { 496 {
443 const char* file_name = rb->strrchr(file, '/') + 1; 497 const char* file_name = rb->strrchr(file, '/') + 1;
444 int dirlen = (file_name - file); 498 int dirlen = (file_name - file);
@@ -457,9 +511,8 @@ enum plugin_status plugin_start(const void* parameter)
457 if (props_type == PROPS_FILE && has_pl_extension(file)) 511 if (props_type == PROPS_FILE && has_pl_extension(file))
458 props_type = PROPS_PLAYLIST; 512 props_type = PROPS_PLAYLIST;
459 513
460 /* get the info depending on its_a_dir */
461 if(!(props_type == PROPS_DIR ? 514 if(!(props_type == PROPS_DIR ?
462 dir_properties(file, &stats) : file_properties(file))) 515 dir_properties(file, &stats, NULL) : file_properties(file)))
463 { 516 {
464 /* something went wrong (to do: tell user what it was (nesting,...) */ 517 /* something went wrong (to do: tell user what it was (nesting,...) */
465 rb->splash(0, ID2P(LANG_PROPERTIES_FAIL)); 518 rb->splash(0, ID2P(LANG_PROPERTIES_FAIL));
@@ -469,7 +522,7 @@ enum plugin_status plugin_start(const void* parameter)
469 } 522 }
470 /* database table selected */ 523 /* database table selected */
471 else if (rb->strcmp(file, MAKE_ACT_STR(ACTIVITY_DATABASEBROWSER)) 524 else if (rb->strcmp(file, MAKE_ACT_STR(ACTIVITY_DATABASEBROWSER))
472 || !assemble_track_info(NULL)) 525 || !assemble_track_info(NULL, NULL))
473 return PLUGIN_ERROR; 526 return PLUGIN_ERROR;
474 527
475 FOR_NB_SCREENS(i) 528 FOR_NB_SCREENS(i)
@@ -480,7 +533,7 @@ enum plugin_status plugin_start(const void* parameter)
480 else if (props_type == PROPS_MUL_ID3) 533 else if (props_type == PROPS_MUL_ID3)
481 ret = rb->browse_id3(&id3, 0, 0, NULL, mul_id3_count); /* database tracks */ 534 ret = rb->browse_id3(&id3, 0, 0, NULL, mul_id3_count); /* database tracks */
482 else if ((ret = browse_file_or_dir(&stats)) < 0) 535 else if ((ret = browse_file_or_dir(&stats)) < 0)
483 ret = assemble_track_info(file) ? /* playlist tracks */ 536 ret = assemble_track_info(file, &stats) ? /* playlist or folder tracks */
484 rb->browse_id3(&id3, 0, 0, NULL, mul_id3_count) : -1; 537 rb->browse_id3(&id3, 0, 0, NULL, mul_id3_count) : -1;
485 538
486 FOR_NB_SCREENS(i) 539 FOR_NB_SCREENS(i)