Bump version numbers for 3.13
[maemo-rb.git] / apps / plugins / stats.c
blobff23911336f0888ace7af2fe7733358f059f5f0d
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Jonas Haggqvist
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/pluginlib_actions.h"
25 static int files, dirs, audiofiles, m3ufiles, imagefiles, videofiles, largestdir;
26 static int lasttick;
27 static bool cancel;
30 /* we use PLA */
31 #define STATS_STOP PLA_EXIT
32 #define STATS_STOP2 PLA_CANCEL
33 /* this set the context to use with PLA */
34 static const struct button_mapping *plugin_contexts[] = { pla_main_ctx };
36 /* we don't have yet a filetype attribute for image files */
37 static const char *image_exts[] = {"bmp","jpg","jpe","jpeg","png","ppm"};
39 /* neither for video ones */
40 static const char *video_exts[] = {"mpg","mpeg","mpv","m2v"};
42 static void prn(const char *str, int y)
44 rb->lcd_puts(0,y,str);
45 #ifdef HAVE_REMOTE_LCD
46 rb->lcd_remote_puts(0,y,str);
47 #endif
50 static void update_screen(void)
52 char buf[32];
54 rb->lcd_clear_display();
55 #ifdef HAVE_REMOTE_LCD
56 rb->lcd_remote_clear_display();
57 #endif
59 #ifdef HAVE_LCD_BITMAP
60 rb->snprintf(buf, sizeof(buf), "Total Files: %d", files);
61 prn(buf,0);
62 rb->snprintf(buf, sizeof(buf), "Audio: %d", audiofiles);
63 prn(buf,1);
64 rb->snprintf(buf, sizeof(buf), "Playlists: %d", m3ufiles);
65 prn(buf,2);
66 rb->snprintf(buf, sizeof(buf), "Images: %d", imagefiles);
67 prn(buf,3);
68 rb->snprintf(buf, sizeof(buf), "Videos: %d", videofiles);
69 prn(buf,4);
70 rb->snprintf(buf, sizeof(buf), "Directories: %d", dirs);
71 prn(buf,5);
72 rb->snprintf(buf, sizeof(buf), "Max files in Dir: %d", largestdir);
73 prn(buf,6);
74 #else
75 rb->snprintf(buf, sizeof(buf), "Files:%5d", files);
76 prn(buf,0);
77 rb->snprintf(buf, sizeof(buf), "Dirs: %5d", dirs);
78 prn(buf,1);
79 #endif
81 rb->lcd_update();
82 #ifdef HAVE_REMOTE_LCD
83 rb->lcd_remote_update();
84 #endif
87 static void traversedir(char* location, char* name)
89 int button;
90 struct dirent *entry;
91 DIR* dir;
92 char fullpath[MAX_PATH];
93 int files_in_dir = 0;
95 rb->snprintf(fullpath, sizeof(fullpath), "%s/%s", location, name);
96 dir = rb->opendir(fullpath);
97 if (dir) {
98 entry = rb->readdir(dir);
99 while (entry) {
100 if (cancel)
101 break;
102 /* Skip .. and . */
103 if (rb->strcmp(entry->d_name, ".") && rb->strcmp(entry->d_name, ".."))
105 struct dirinfo info = rb->dir_get_info(dir, entry);
106 if (info.attribute & ATTR_DIRECTORY) {
107 traversedir(fullpath, entry->d_name);
108 dirs++;
110 else {
111 files_in_dir++; files++;
113 /* get the filetype from the filename */
114 int attr = rb->filetype_get_attr(entry->d_name);
115 switch (attr & FILE_ATTR_MASK)
117 case FILE_ATTR_AUDIO:
118 audiofiles++;
119 break;
121 case FILE_ATTR_M3U:
122 m3ufiles++;
123 break;
125 default:
127 /* use hardcoded filetype_exts to count
128 * image and video files until we get
129 * new attributes added to filetypes.h */
130 char *ptr = rb->strrchr(entry->d_name,'.');
131 if(ptr) {
132 unsigned i;
133 ptr++;
134 for(i=0;i<ARRAYLEN(image_exts);i++) {
135 if(!rb->strcasecmp(ptr,image_exts[i])) {
136 imagefiles++; break;
140 if (i >= ARRAYLEN(image_exts)) {
141 /* not found above - try video files */
142 for(i=0;i<ARRAYLEN(video_exts);i++) {
143 if(!rb->strcasecmp(ptr,video_exts[i])) {
144 videofiles++; break;
149 } /* default: */
150 } /* switch */
154 if (*rb->current_tick - lasttick > (HZ/2)) {
155 update_screen();
156 lasttick = *rb->current_tick;
157 button = pluginlib_getaction(TIMEOUT_NOBLOCK, plugin_contexts,
158 ARRAYLEN(plugin_contexts));
159 if (button == STATS_STOP) {
160 cancel = true;
161 break;
165 entry = rb->readdir(dir);
167 rb->closedir(dir);
169 if (largestdir < files_in_dir)
170 largestdir = files_in_dir;
173 /* this is the plugin entry point */
174 enum plugin_status plugin_start(const void* parameter)
176 int button;
178 (void)parameter;
180 files = 0;
181 dirs = 0;
182 audiofiles = 0;
183 m3ufiles = 0;
184 imagefiles = 0;
185 videofiles = 0;
186 largestdir = 0;
187 cancel = false;
189 rb->splash(HZ, "Counting...");
190 update_screen();
191 lasttick = *rb->current_tick;
193 traversedir("", "");
194 if (cancel) {
195 rb->splash(HZ, "Aborted");
196 return PLUGIN_OK;
198 update_screen();
199 #ifdef HAVE_REMOTE_LCD
200 rb->remote_backlight_on();
201 #endif
202 rb->backlight_on();
203 rb->splash(HZ, "Done");
204 update_screen();
205 while (1) {
207 button = pluginlib_getaction(TIMEOUT_BLOCK, plugin_contexts,
208 ARRAYLEN(plugin_contexts));
209 switch (button) {
210 case STATS_STOP:
211 case STATS_STOP2:
212 return PLUGIN_OK;
213 break;
214 default:
215 if (rb->default_event_handler(button) == SYS_USB_CONNECTED) {
216 return PLUGIN_USB_CONNECTED;
218 break;
221 return PLUGIN_OK;