summaryrefslogtreecommitdiffstats
path: root/apps/plugins/stats.c
blob: b6a824e2086a19f560829d01e7c20e69be750481 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2005 Jonas Haggqvist
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "plugin.h"

PLUGIN_HEADER

static struct plugin_api* rb;
static int files, dirs, musicfiles;
static int lasttick;
static bool abort;
#ifdef HAVE_LCD_BITMAP
static int fontwidth, fontheight;
#endif

#if CONFIG_KEYPAD == PLAYER_PAD 
#define STATS_STOP BUTTON_STOP

#elif (CONFIG_KEYPAD == RECORDER_PAD) \
   || (CONFIG_KEYPAD == ONDIO_PAD) \
   || (CONFIG_KEYPAD == ARCHOS_AV300_PAD)
#define STATS_STOP BUTTON_OFF

#elif (CONFIG_KEYPAD == IRIVER_H100_PAD) \
   || (CONFIG_KEYPAD == IRIVER_H300_PAD)
#define STATS_STOP BUTTON_OFF
#define STATS_STOP_REMOTE BUTTON_RC_STOP

#elif (CONFIG_KEYPAD == IPOD_4G_PAD) \
   || (CONFIG_KEYPAD == IPOD_3G_PAD)
#define STATS_STOP BUTTON_MENU

#elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
#define STATS_STOP BUTTON_PLAY

#elif CONFIG_KEYPAD == IAUDIO_X5_PAD
#define STATS_STOP BUTTON_POWER
#define STATS_STOP_REMOTE BUTTON_RC_PLAY

#elif CONFIG_KEYPAD == GIGABEAT_PAD
#define STATS_STOP BUTTON_A

#elif CONFIG_KEYPAD == SANSA_E200_PAD
#define STATS_STOP BUTTON_POWER

#elif CONFIG_KEYPAD == IRIVER_H10_PAD
#define STATS_STOP BUTTON_POWER

#endif

/* TODO: Better get the exts from the filetypes var in tree.c */
const char *music_exts[] = {"mp3","mp2","mp1","mpa","ogg",
        "wav","flac","ac3","a52","mpc","wv","m4a","m4b","mp4",
        "shn","aif","aiff"};

void prn(const char *str, int y)
{
#ifdef HAVE_LCD_BITMAP
    rb->lcd_putsxy(0,y,str);
#ifdef HAVE_REMOTE_LCD
    rb->lcd_remote_putsxy(0,y,str);    
#endif
#else
    rb->lcd_puts(0,y,str);   
#endif
}

void update_screen(void)
{
    char buf[15];
#ifdef HAVE_LCD_BITMAP
    rb->lcd_clear_display();
#ifdef HAVE_REMOTE_LCD
    rb->lcd_remote_clear_display();
#endif
    
    rb->snprintf(buf, sizeof(buf), "Files: %d", files);
    prn(buf,0);

    rb->snprintf(buf, sizeof(buf), "Music: %d", musicfiles);
    prn(buf,fontheight);
    
    rb->snprintf(buf, sizeof(buf), "Dirs: %d", dirs);
    prn(buf,fontheight*2);
    rb->lcd_update();
#ifdef HAVE_REMOTE_LCD
    rb->lcd_remote_update();
#endif
#else
    rb->snprintf(buf, sizeof(buf), "Files:%5d", files);
    prn(buf,0);
    rb->snprintf(buf, sizeof(buf), "Dirs: %5d", dirs);
    prn(buf,1);
#endif
}

void traversedir(char* location, char* name)
{
    int button;
    struct dirent *entry;
    DIR* dir;
    char fullpath[MAX_PATH];

    rb->snprintf(fullpath, sizeof(fullpath), "%s/%s", location, name);
    dir = rb->opendir(fullpath);
    if (dir) {
        entry = rb->readdir(dir);
        while (entry) {
            if (abort == true)
                break;
            /* Skip .. and . */
            if (rb->strcmp(entry->d_name, ".") && rb->strcmp(entry->d_name, ".."))
            {
                if (entry->attribute & ATTR_DIRECTORY) {
                    traversedir(fullpath, entry->d_name);
                    dirs++;
                }
                else {
                    char *ptr = rb->strrchr(entry->d_name,'.'); 
                    files++;
                    /* Might want to only count .mp3, .ogg etc. */
                    if(ptr){
                        unsigned i;
                        ptr++;
                        for(i=0;i<sizeof(music_exts)/sizeof(char*);i++)
                            if(!rb->strcasecmp(ptr,music_exts[i])){
                                musicfiles++; break;
                            }
                        
                    }
                }
            }
            if (*rb->current_tick - lasttick > (HZ/2)) {
                update_screen();
                lasttick = *rb->current_tick;
                button = rb->button_get(false);
                if (button == STATS_STOP
#ifdef HAVE_REMOTE_LCD
                    || button == STATS_STOP_REMOTE
#endif
                    ) {
                    abort = true;
                    break;
                }
            }

            entry = rb->readdir(dir);
        }
        rb->closedir(dir);
    }
}

enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
{
    int button;

    (void)parameter;

    rb = api;
    files = 0;
    dirs = 0;
    musicfiles = 0;
    abort = false;

#ifdef HAVE_LCD_BITMAP
    rb->lcd_getstringsize("Files: ", &fontwidth, &fontheight);
#endif
    rb->splash(HZ, true, "Counting...");
    update_screen();
    lasttick = *rb->current_tick;

    traversedir("", "");
    if (abort == true) {
        rb->splash(HZ, true, "Aborted");
        return PLUGIN_OK;
    }
    update_screen();
#ifdef HAVE_REMOTE_LCD
    rb->remote_backlight_on();
#endif
    rb->backlight_on();
    rb->splash(HZ, true, "Done");
    update_screen();
    button = rb->button_get(true);
    while (1) {
        switch (button) {
#ifdef HAVE_REMOTE_LCD
            case STATS_STOP_REMOTE:
#endif
            case STATS_STOP:
                return PLUGIN_OK;
                break;
            default:
                if (rb->default_event_handler(button) == SYS_USB_CONNECTED) {
                    return PLUGIN_USB_CONNECTED;
                }
                break;
        }
        return PLUGIN_OK;
    }
}