src/util/gui/file-select.c (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "file-select.h"
7
8#include "util/gui/font.h"
9#include "util/gui/menu.h"
10#include "util/vfs.h"
11
12#include <stdlib.h>
13
14#define ITERATION_SIZE 5
15#define SCANNING_THRESHOLD_1 50
16#ifdef _3DS
17// 3DS is slooooow at opening files
18#define SCANNING_THRESHOLD_2 10
19#else
20#define SCANNING_THRESHOLD_2 50
21#endif
22
23static void _cleanFiles(struct GUIMenuItemList* currentFiles) {
24 size_t size = GUIMenuItemListSize(currentFiles);
25 size_t i;
26 for (i = 1; i < size; ++i) {
27 free(GUIMenuItemListGetPointer(currentFiles, i)->title);
28 }
29 GUIMenuItemListClear(currentFiles);
30}
31
32static void _upDirectory(char* currentPath) {
33 char* end = strrchr(currentPath, '/');
34 if (!end) {
35 return;
36 }
37 if (end == currentPath) {
38 end[1] = '\0';
39 return;
40 }
41 end[0] = '\0';
42 if (end[1]) {
43 return;
44 }
45 // TODO: What if there was a trailing slash?
46}
47
48static int _strpcmp(const void* a, const void* b) {
49 return strcasecmp(((const struct GUIMenuItem*) a)->title, ((const struct GUIMenuItem*) b)->title);
50}
51
52static bool _refreshDirectory(struct GUIParams* params, const char* currentPath, struct GUIMenuItemList* currentFiles, bool (*filter)(struct VFile*)) {
53 _cleanFiles(currentFiles);
54
55 struct VDir* dir = VDirOpen(currentPath);
56 if (!dir) {
57 return false;
58 }
59 *GUIMenuItemListAppend(currentFiles) = (struct GUIMenuItem) { .title = "(Up)" };
60 size_t i = 0;
61 size_t items = 0;
62 struct VDirEntry* de;
63 while ((de = dir->listNext(dir))) {
64 ++i;
65 if (!(i % SCANNING_THRESHOLD_1)) {
66 uint32_t input = 0;
67 GUIPollInput(params, &input, 0);
68 if (input & (1 << GUI_INPUT_CANCEL)) {
69 return false;
70 }
71
72 params->drawStart();
73 if (params->guiPrepare) {
74 params->guiPrepare();
75 }
76 GUIFontPrintf(params->font, 0, GUIFontHeight(params->font), GUI_TEXT_LEFT, 0xFFFFFFFF, "(scanning for items: %zu)", i);
77 GUIFontPrintf(params->font, 0, GUIFontHeight(params->font) * 2, GUI_TEXT_LEFT, 0xFFFFFFFF, "%s", currentPath);
78 if (params->guiFinish) {
79 params->guiFinish();
80 }
81 params->drawEnd();
82 }
83 const char* name = de->name(de);
84 if (name[0] == '.') {
85 continue;
86 }
87 *GUIMenuItemListAppend(currentFiles) = (struct GUIMenuItem) { .title = strdup(name) };
88 ++items;
89 }
90 qsort(GUIMenuItemListGetPointer(currentFiles, 1), GUIMenuItemListSize(currentFiles) - 1, sizeof(struct GUIMenuItem), _strpcmp);
91 i = 0;
92 size_t item = 0;
93 while (item < GUIMenuItemListSize(currentFiles)) {
94 ++i;
95 if (!(i % SCANNING_THRESHOLD_2)) {
96 uint32_t input = 0;
97 GUIPollInput(params, &input, 0);
98 if (input & (1 << GUI_INPUT_CANCEL)) {
99 return false;
100 }
101
102 params->drawStart();
103 if (params->guiPrepare) {
104 params->guiPrepare();
105 }
106 GUIFontPrintf(params->font, 0, GUIFontHeight(params->font), GUI_TEXT_LEFT, 0xFFFFFFFF, "(scanning item %zu of %zu)", i, items);
107 GUIFontPrintf(params->font, 0, GUIFontHeight(params->font) * 2, GUI_TEXT_LEFT, 0xFFFFFFFF, "%s", currentPath);
108 if (params->guiFinish) {
109 params->guiFinish();
110 }
111 params->drawEnd();
112 }
113 struct VFile* vf = dir->openFile(dir, GUIMenuItemListGetPointer(currentFiles, item)->title, O_RDONLY);
114 if (!vf) {
115 ++item;
116 continue;
117 }
118 if (filter && !filter(vf)) {
119 free(GUIMenuItemListGetPointer(currentFiles, item)->title);
120 GUIMenuItemListShift(currentFiles, item, 1);
121 } else {
122 ++item;
123 }
124 vf->close(vf);
125 }
126 dir->close(dir);
127
128 return true;
129}
130
131bool GUISelectFile(struct GUIParams* params, char* outPath, size_t outLen, bool (*filter)(struct VFile*)) {
132 struct GUIMenu menu = {
133 .title = "Select file",
134 .subtitle = params->currentPath,
135 .index = params->fileIndex,
136 };
137 GUIMenuItemListInit(&menu.items, 0);
138 _refreshDirectory(params, params->currentPath, &menu.items, filter);
139
140 while (true) {
141 struct GUIMenuItem* item;
142 enum GUIMenuExitReason reason = GUIShowMenu(params, &menu, &item);
143 params->fileIndex = menu.index;
144 if (reason == GUI_MENU_EXIT_CANCEL) {
145 break;
146 }
147 if (reason == GUI_MENU_EXIT_ACCEPT) {
148 if (params->fileIndex == 0) {
149 _upDirectory(params->currentPath);
150 if (!_refreshDirectory(params, params->currentPath, &menu.items, filter)) {
151 break;
152 }
153 } else {
154 size_t len = strlen(params->currentPath);
155 const char* sep = PATH_SEP;
156 if (params->currentPath[len - 1] == *sep) {
157 sep = "";
158 }
159 snprintf(outPath, outLen, "%s%s%s", params->currentPath, sep, item->title);
160
161 struct GUIMenuItemList newFiles;
162 GUIMenuItemListInit(&newFiles, 0);
163 if (!_refreshDirectory(params, outPath, &newFiles, filter)) {
164 _cleanFiles(&newFiles);
165 GUIMenuItemListDeinit(&newFiles);
166 struct VFile* vf = VFileOpen(outPath, O_RDONLY);
167 if (!vf) {
168 continue;
169 }
170 if (!filter || filter(vf)) {
171 vf->close(vf);
172 _cleanFiles(&menu.items);
173 GUIMenuItemListDeinit(&menu.items);
174 return true;
175 }
176 vf->close(vf);
177 break;
178 } else {
179 _cleanFiles(&menu.items);
180 GUIMenuItemListDeinit(&menu.items);
181 menu.items = newFiles;
182 strncpy(params->currentPath, outPath, PATH_MAX);
183 }
184 }
185 params->fileIndex = 0;
186 menu.index = 0;
187 }
188 if (reason == GUI_MENU_EXIT_BACK) {
189 if (strncmp(params->currentPath, params->basePath, PATH_MAX) == 0) {
190 break;
191 }
192 _upDirectory(params->currentPath);
193 if (!_refreshDirectory(params, params->currentPath, &menu.items, filter)) {
194 break;
195 }
196 params->fileIndex = 0;
197 menu.index = 0;
198 }
199 }
200
201 _cleanFiles(&menu.items);
202 GUIMenuItemListDeinit(&menu.items);
203 return false;
204}