-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileBrowser.c
More file actions
212 lines (176 loc) · 7.39 KB
/
FileBrowser.c
File metadata and controls
212 lines (176 loc) · 7.39 KB
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
#include "FileBrowser.h"
#include "AppInst.h"
#include "AppUI.h"
#include <AEEStdLib.h>
struct FileEntryData {
CAppUIFileBrowser *fileBrowser;
FileInfo info;
};
static boolean HasFileExtension(const char *path, const char *extension) {
path = STRRCHR(path, '.');
if (path != NULL) {
return STRCMP(path, extension) == 0;
}
return FALSE;
}
static void InstallAppEntryCallback(CMenuEntry *entry, void *data_) {
struct FileEntryData *data = entry->parent->parent->userData;
int status = AppInst_ExtractAppliFromFile(data->fileBrowser->app, data->info.szName);
if (status == AEE_SUCCESS) {
CAppUIApp_ShowMessage(data->fileBrowser->app, entry->parent->parent,
(const AECHAR *) u"Success. Please reboot.");
} else {
AECHAR msg[64];
WSPRINTF(msg, sizeof(msg), (const AECHAR *) u"INSTALL FAILED: %d\n", status);
CAppUIApp_ShowMessage(data->fileBrowser->app, entry->parent->parent, msg);
}
}
static void OpenAsDirEntryCallback(CMenuEntry *entry, void *data_) {
struct FileEntryData *data = entry->parent->parent->userData;
CAppUIFileBrowser_ShowDirectory(data->fileBrowser, data->info.szName);
}
static CMenu *ShowFileSubmenuListEntry(CMenuEntry *entry, void *data_) {
struct FileEntryData *data = (struct FileEntryData *) data_;
data->fileBrowser->fileActionMenu.userData = data;
return &data->fileBrowser->fileActionMenu;
}
static void SelectFileBrowserListEntry(CMenuEntry *entry, void *data_) {
struct FileEntryData *data = (struct FileEntryData *) data_;
if (data->info.attrib & AEE_FA_DIR) {
CAppUIFileBrowser_ShowDirectory(data->fileBrowser, data->info.szName);
}
}
static void ReleaseFileBrowserListEntry(CMenuEntry *entry, void *data) {
FREE(data);
}
static void AddFileBrowserListEntryEx(CAppUIFileBrowser *self, CMenuEntryGroup *group,
const AECHAR *name, FileInfo *info) {
CMenuEntry *fileEntry = NULL;
if (SUCCESS == CMenuEntryGroup_AddNewEntry(group, name, &fileEntry)) {
struct FileEntryData *data = MALLOC(sizeof(struct FileEntryData));
data->fileBrowser = self;
data->info = *info;
fileEntry->userData = data;
fileEntry->releaseCallback = ReleaseFileBrowserListEntry;
fileEntry->callback = SelectFileBrowserListEntry;
if (!(info->attrib & AEE_FA_DIR)) {
fileEntry->submenuCallback = ShowFileSubmenuListEntry;
}
}
}
static void AddFileBrowserListEntry(CAppUIFileBrowser *self, CMenuEntryGroup *group,
FileInfo *info) {
const char *aName = STRRCHR(info->szName, '/');
if (aName == NULL) {
aName = info->szName;
} else {
aName++;
}
AECHAR name[sizeof(info->szName)];
STREXPAND((const byte *) aName, STRLEN(aName) + 1, name, sizeof(name));
AddFileBrowserListEntryEx(self, group, name, info);
}
static boolean GetParentDirectoryInfo(const char *directory, FileInfo *info) {
info->attrib = AEE_FA_DIR;
const char *pLastSlash = STRRCHR(directory, '/');
if (pLastSlash == NULL || STRCMP(directory, "fs:/") == 0) {
STRCPY(info->szName, "fs:/");
return FALSE;
} else {
STRNCPY(info->szName, directory, pLastSlash - directory);
info->szName[pLastSlash - directory] = 0;
if (STRCMP(info->szName, "fs:") == 0) {
STRCPY(info->szName, "fs:/");
return TRUE;
}
}
info->dwSize = 0;
info->dwCreationDate = 0;
return TRUE;
}
void CAppUIFileBrowser_ShowDirectory(CAppUIFileBrowser *self, const char *directory) {
CMenuEntryGroup_Clear(self->fileBrowserListGroup);
STRCPY(self->fileBrowserCurrentDirectory, directory);
FileInfo parentInfo;
if (GetParentDirectoryInfo(directory, &parentInfo)) {
AddFileBrowserListEntryEx(self, self->fileBrowserListGroup,
(const AECHAR *) u"..", &parentInfo);
}
if (SUCCESS == IFILEMGR_EnumInit(self->fileMgr, directory, TRUE)) {
FileInfo info;
while (IFILEMGR_EnumNext(self->fileMgr, &info)) {
// Sony Ericsson W53S has broken firmware in here and does not set that flag :(
info.attrib |= AEE_FA_DIR;
AddFileBrowserListEntry(self, self->fileBrowserListGroup, &info);
}
}
if (SUCCESS == IFILEMGR_EnumInit(self->fileMgr, directory, FALSE)) {
FileInfo info;
while (IFILEMGR_EnumNext(self->fileMgr, &info)) {
AddFileBrowserListEntry(self, self->fileBrowserListGroup, &info);
}
}
if (STRCMP(directory, "fs:/") == 0) {
FileInfo info = {0};
STRCPY(info.szName, "fs:/card0");
info.attrib = AEE_FA_DIR;
AddFileBrowserListEntryEx(self, self->fileBrowserListGroup, (const AECHAR *)u"fs:/card0", &info);
STRCPY(info.szName, "fs:/card0/pc");
info.attrib = AEE_FA_DIR;
AddFileBrowserListEntryEx(self, self->fileBrowserListGroup, (const AECHAR *)u"fs:/card0/pc", &info);
}
CMenuManager_OpenMenu(&self->app->menuManager, &self->fileBrowserMenu);
}
static void FileBrowserBackHandler(CMenuManager *manager, CMenu *menu, void *userData) {
CAppUIFileBrowser *self = (CAppUIFileBrowser *) userData;
FileInfo info;
if (GetParentDirectoryInfo(self->fileBrowserCurrentDirectory, &info)) {
CAppUIFileBrowser_ShowDirectory(self, info.szName);
} else {
CMenuManager_OpenMenu(&self->app->menuManager, &self->app->mainMenu);
}
}
int CAppUIFileBrowser_Init(CAppUIFileBrowser *self, CAppUIApp *app) {
self->app = app;
if (SUCCESS != ISHELL_CreateInstance(self->app->applet.m_pIShell, AEECLSID_FILEMGR,
(void **) &self->fileMgr))
goto onError;
{
CMenu_Init(&self->fileBrowserMenu);
if (SUCCESS != CMenu_AddNewGroup(&self->fileBrowserMenu, &self->fileBrowserListGroup))
goto onError;
self->fileBrowserListGroup->groupLabel = (const AECHAR *) u"Files";
self->fileBrowserMenu.backHandler = FileBrowserBackHandler;
self->fileBrowserMenu.userData = self;
}
{
CMenu_Init(&self->fileActionMenu);
CMenuEntryGroup *actionsGroup;
if (SUCCESS != CMenu_AddNewGroup(&self->fileActionMenu, &actionsGroup)) goto onError;
actionsGroup->groupLabel = (const AECHAR *) u"File Actions";
CMenuEntry *installFromZipEntry = NULL;
if (SUCCESS != CMenuEntryGroup_AddNewEntry(actionsGroup,
(const AECHAR *) u"Install from ZIP",
&installFromZipEntry))
goto onError;
installFromZipEntry->callback = InstallAppEntryCallback;
CMenuEntry *openAsDirEntry = NULL;
if (SUCCESS != CMenuEntryGroup_AddNewEntry(actionsGroup,
(const AECHAR *) u"Force open as dir.",
&openAsDirEntry))
goto onError;
openAsDirEntry->callback = OpenAsDirEntryCallback;
self->fileActionMenu.backMenu = &self->fileBrowserMenu;
}
return TRUE;
onError:
CAppUIFileBrowser_Free(self);
return FALSE;
}
void CAppUIFileBrowser_Free(CAppUIFileBrowser *self) {
CMenu_Free(&self->fileBrowserMenu);
if (self->fileMgr != NULL) {
IFILEMGR_Release(self->fileMgr);
self->fileMgr = NULL;
}
}