blob: c7805c42a85f342707f900cec642a55e97a43e2f [file] [log] [blame]
Dees_Troya13d74f2013-03-24 08:54:55 -05001/*
2 Copyright 2012 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
Dees_Troy51a0e822012-09-05 15:24:24 -040019#include <string.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040020#include <sys/stat.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040021#include <dirent.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040022#include <algorithm>
23
24extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000025#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040026#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040027}
28
29#include "rapidxml.hpp"
30#include "objects.hpp"
31#include "../data.hpp"
Dees_Troy3ee47bc2013-01-25 21:47:37 +000032#include "../twrp-functions.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040033
34#define TW_FILESELECTOR_UP_A_LEVEL "(Up A Level)"
35
Dees_Troy51a0e822012-09-05 15:24:24 -040036int GUIFileSelector::mSortOrder = 0;
37
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010038GUIFileSelector::GUIFileSelector(xml_node<>* node) : GUIScrollList(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040039{
40 xml_attribute<>* attr;
41 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -040042
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010043 int mIconWidth = 0, mIconHeight = 0, mFolderIconHeight = 0, mFileIconHeight = 0, mFolderIconWidth = 0, mFileIconWidth = 0;
44 mFolderIcon = mFileIcon = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -040045 mShowFolders = mShowFiles = mShowNavFolders = 1;
46 mUpdate = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040047 mPathVar = "cwd";
Dees_Troyc0583f52013-02-28 11:19:57 -060048 updateFileList = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040049
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010050 // Load filter for filtering files (e.g. *.zip for only zips)
Dees_Troy51a0e822012-09-05 15:24:24 -040051 child = node->first_node("filter");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010052 if (child) {
Dees_Troy51a0e822012-09-05 15:24:24 -040053 attr = child->first_attribute("extn");
54 if (attr)
55 mExtn = attr->value();
56 attr = child->first_attribute("folders");
57 if (attr)
58 mShowFolders = atoi(attr->value());
59 attr = child->first_attribute("files");
60 if (attr)
61 mShowFiles = atoi(attr->value());
62 attr = child->first_attribute("nav");
63 if (attr)
64 mShowNavFolders = atoi(attr->value());
65 }
66
67 // Handle the path variable
68 child = node->first_node("path");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010069 if (child) {
Dees_Troy51a0e822012-09-05 15:24:24 -040070 attr = child->first_attribute("name");
71 if (attr)
72 mPathVar = attr->value();
73 attr = child->first_attribute("default");
74 if (attr)
75 DataManager::SetValue(mPathVar, attr->value());
76 }
77
78 // Handle the result variable
79 child = node->first_node("data");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010080 if (child) {
Dees_Troy51a0e822012-09-05 15:24:24 -040081 attr = child->first_attribute("name");
82 if (attr)
83 mVariable = attr->value();
84 attr = child->first_attribute("default");
85 if (attr)
86 DataManager::SetValue(mVariable, attr->value());
87 }
88
89 // Handle the sort variable
90 child = node->first_node("sort");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010091 if (child) {
Dees_Troy51a0e822012-09-05 15:24:24 -040092 attr = child->first_attribute("name");
93 if (attr)
94 mSortVariable = attr->value();
95 attr = child->first_attribute("default");
96 if (attr)
97 DataManager::SetValue(mSortVariable, attr->value());
98
99 DataManager::GetValue(mSortVariable, mSortOrder);
100 }
101
102 // Handle the selection variable
103 child = node->first_node("selection");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100104 if (child && (attr = child->first_attribute("name")))
105 mSelection = attr->value();
106 else
Dees_Troy51a0e822012-09-05 15:24:24 -0400107 mSelection = "0";
108
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100109 // Get folder and file icons if present
110 child = node->first_node("icon");
111 if (child) {
112 attr = child->first_attribute("folder");
113 if (attr)
114 mFolderIcon = PageManager::FindResource(attr->value());
115 attr = child->first_attribute("file");
116 if (attr)
117 mFileIcon = PageManager::FindResource(attr->value());
Vojtech Bocek7cc278b2013-02-24 01:40:19 +0100118 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100119 if (mFolderIcon && mFolderIcon->GetResource()) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400120 mFolderIconWidth = gr_get_width(mFolderIcon->GetResource());
121 mFolderIconHeight = gr_get_height(mFolderIcon->GetResource());
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100122 if (mFolderIconHeight > mIconHeight)
123 mIconHeight = mFolderIconHeight;
Dees_Troy51a0e822012-09-05 15:24:24 -0400124 mIconWidth = mFolderIconWidth;
125 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100126 if (mFileIcon && mFileIcon->GetResource()) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400127 mFileIconWidth = gr_get_width(mFileIcon->GetResource());
128 mFileIconHeight = gr_get_height(mFileIcon->GetResource());
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100129 if (mFileIconHeight > mIconHeight)
130 mIconHeight = mFileIconHeight;
Dees_Troy51a0e822012-09-05 15:24:24 -0400131 if (mFileIconWidth > mIconWidth)
132 mIconWidth = mFileIconWidth;
133 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100134 SetMaxIconSize(mIconWidth, mIconHeight);
Dees_Troy51a0e822012-09-05 15:24:24 -0400135
136 // Fetch the file/folder list
137 std::string value;
138 DataManager::GetValue(mPathVar, value);
Dees_Troy80a11d92013-01-25 16:36:07 +0000139 GetFileList(value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400140}
141
142GUIFileSelector::~GUIFileSelector()
143{
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100144 delete mFileIcon;
145 delete mFolderIcon;
Dees_Troy51a0e822012-09-05 15:24:24 -0400146}
147
148int GUIFileSelector::Update(void)
149{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100150 if(!isConditionTrue())
151 return 0;
152
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100153 GUIScrollList::Update();
154
155 // Update the file list if needed
156 if (updateFileList) {
157 string value;
158 DataManager::GetValue(mPathVar, value);
159 if (GetFileList(value) == 0) {
160 updateFileList = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400161 mUpdate = 1;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100162 } else
163 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400164 }
165
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100166 if (mUpdate) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400167 mUpdate = 0;
168 if (Render() == 0)
169 return 2;
170 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400171 return 0;
172}
173
Vojtech Bocek07220562014-02-08 02:05:33 +0100174int GUIFileSelector::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400175{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100176 if(!isConditionTrue())
177 return 0;
178
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100179 GUIScrollList::NotifyVarChange(varName, value);
180
Dees_Troy146d72a2013-03-11 17:46:19 +0000181 if (varName.empty()) {
182 // Always clear the data variable so we know to use it
183 DataManager::SetValue(mVariable, "");
184 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100185 if (varName == mPathVar || varName == mSortVariable) {
Dees_Troy4622cf92013-03-01 15:29:36 -0600186 if (varName == mSortVariable) {
187 DataManager::GetValue(mSortVariable, mSortOrder);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100188 } else {
189 // Reset the list to the top
190 SetVisibleListLocation(0);
Dees_Troyc0583f52013-02-28 11:19:57 -0600191 }
192 updateFileList = true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400193 mUpdate = 1;
194 return 0;
195 }
196 return 0;
197}
198
Dees_Troy51a0e822012-09-05 15:24:24 -0400199bool GUIFileSelector::fileSort(FileData d1, FileData d2)
200{
201 if (d1.fileName == ".")
202 return -1;
203 if (d2.fileName == ".")
204 return 0;
205 if (d1.fileName == TW_FILESELECTOR_UP_A_LEVEL)
206 return -1;
207 if (d2.fileName == TW_FILESELECTOR_UP_A_LEVEL)
208 return 0;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500209
Dees_Troy51a0e822012-09-05 15:24:24 -0400210 switch (mSortOrder) {
211 case 3: // by size largest first
212 if (d1.fileSize == d2.fileSize || d1.fileType == DT_DIR) // some directories report a different size than others - but this is not the size of the files inside the directory, so we just sort by name on directories
213 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) < 0);
Dees_Troy6ef66352013-02-21 08:26:57 -0600214 return d1.fileSize < d2.fileSize;
Dees_Troy51a0e822012-09-05 15:24:24 -0400215 case -3: // by size smallest first
216 if (d1.fileSize == d2.fileSize || d1.fileType == DT_DIR) // some directories report a different size than others - but this is not the size of the files inside the directory, so we just sort by name on directories
217 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) > 0);
Dees_Troy6ef66352013-02-21 08:26:57 -0600218 return d1.fileSize > d2.fileSize;
Dees_Troy51a0e822012-09-05 15:24:24 -0400219 case 2: // by last modified date newest first
220 if (d1.lastModified == d2.lastModified)
221 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) < 0);
Dees_Troy6ef66352013-02-21 08:26:57 -0600222 return d1.lastModified < d2.lastModified;
Dees_Troy51a0e822012-09-05 15:24:24 -0400223 case -2: // by date oldest first
224 if (d1.lastModified == d2.lastModified)
225 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) > 0);
Dees_Troy6ef66352013-02-21 08:26:57 -0600226 return d1.lastModified > d2.lastModified;
Dees_Troy51a0e822012-09-05 15:24:24 -0400227 case -1: // by name descending
228 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) > 0);
229 default: // should be a 1 - sort by name ascending
230 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) < 0);
231 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100232 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400233}
234
235int GUIFileSelector::GetFileList(const std::string folder)
236{
237 DIR* d;
238 struct dirent* de;
239 struct stat st;
240
241 // Clear all data
242 mFolderList.clear();
243 mFileList.clear();
244
245 d = opendir(folder.c_str());
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100246 if (d == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000247 LOGINFO("Unable to open '%s'\n", folder.c_str());
Dees_Troy80a11d92013-01-25 16:36:07 +0000248 if (folder != "/" && (mShowNavFolders != 0 || mShowFiles != 0)) {
249 size_t found;
250 found = folder.find_last_of('/');
251 if (found != string::npos) {
252 string new_folder = folder.substr(0, found);
253
254 if (new_folder.length() < 2)
255 new_folder = "/";
256 DataManager::SetValue(mPathVar, new_folder);
257 }
258 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400259 return -1;
260 }
261
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100262 while ((de = readdir(d)) != NULL) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400263 FileData data;
264
265 data.fileName = de->d_name;
266 if (data.fileName == ".")
267 continue;
268 if (data.fileName == ".." && folder == "/")
269 continue;
Dees_Troy3ee47bc2013-01-25 21:47:37 +0000270 if (data.fileName == "..") {
Dees_Troy51a0e822012-09-05 15:24:24 -0400271 data.fileName = TW_FILESELECTOR_UP_A_LEVEL;
Dees_Troy3ee47bc2013-01-25 21:47:37 +0000272 data.fileType = DT_DIR;
273 } else {
274 data.fileType = de->d_type;
275 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400276
277 std::string path = folder + "/" + data.fileName;
278 stat(path.c_str(), &st);
279 data.protection = st.st_mode;
280 data.userId = st.st_uid;
281 data.groupId = st.st_gid;
282 data.fileSize = st.st_size;
283 data.lastAccess = st.st_atime;
284 data.lastModified = st.st_mtime;
285 data.lastStatChange = st.st_ctime;
286
Dees_Troy3ee47bc2013-01-25 21:47:37 +0000287 if (data.fileType == DT_UNKNOWN) {
288 data.fileType = TWFunc::Get_D_Type_From_Stat(path);
289 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100290 if (data.fileType == DT_DIR) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400291 if (mShowNavFolders || (data.fileName != "." && data.fileName != TW_FILESELECTOR_UP_A_LEVEL))
292 mFolderList.push_back(data);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100293 } else if (data.fileType == DT_REG || data.fileType == DT_LNK || data.fileType == DT_BLK) {
294 if (mExtn.empty() || (data.fileName.length() > mExtn.length() && data.fileName.substr(data.fileName.length() - mExtn.length()) == mExtn)) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400295 mFileList.push_back(data);
296 }
297 }
298 }
299 closedir(d);
300
301 std::sort(mFolderList.begin(), mFolderList.end(), fileSort);
302 std::sort(mFileList.begin(), mFileList.end(), fileSort);
Dees_Troyc0583f52013-02-28 11:19:57 -0600303
Dees_Troy51a0e822012-09-05 15:24:24 -0400304 return 0;
305}
306
307void GUIFileSelector::SetPageFocus(int inFocus)
308{
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100309 GUIScrollList::SetPageFocus(inFocus);
310 if (inFocus) {
Dees_Troyc0583f52013-02-28 11:19:57 -0600311 updateFileList = true;
Dees_Troyc0583f52013-02-28 11:19:57 -0600312 mUpdate = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400313 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500314}
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100315
316size_t GUIFileSelector::GetItemCount()
317{
318 size_t folderSize = mShowFolders ? mFolderList.size() : 0;
319 size_t fileSize = mShowFiles ? mFileList.size() : 0;
320 return folderSize + fileSize;
321}
322
323int GUIFileSelector::GetListItem(size_t item_index, Resource*& icon, std::string &text)
324{
325 size_t folderSize = mShowFolders ? mFolderList.size() : 0;
326 size_t fileSize = mShowFiles ? mFileList.size() : 0;
327
328 if (item_index < folderSize) {
329 text = mFolderList.at(item_index).fileName;
330 icon = mFolderIcon;
331 } else {
332 text = mFileList.at(item_index - folderSize).fileName;
333 icon = mFileIcon;
334 }
335 return 0;
336}
337
338void GUIFileSelector::NotifySelect(size_t item_selected)
339{
340 size_t folderSize = mShowFolders ? mFolderList.size() : 0;
341 size_t fileSize = mShowFiles ? mFileList.size() : 0;
342
343 if (item_selected < folderSize + fileSize) {
344 // We've selected an item!
345 std::string str;
346 if (item_selected < folderSize) {
347 std::string cwd;
348
349 str = mFolderList.at(item_selected).fileName;
350 if (mSelection != "0")
351 DataManager::SetValue(mSelection, str);
352 DataManager::GetValue(mPathVar, cwd);
353
354 // Ignore requests to do nothing
355 if (str == ".") return;
356 if (str == TW_FILESELECTOR_UP_A_LEVEL) {
357 if (cwd != "/") {
358 size_t found;
359 found = cwd.find_last_of('/');
360 cwd = cwd.substr(0,found);
361
362 if (cwd.length() < 2) cwd = "/";
363 }
364 } else {
365 // Add a slash if we're not the root folder
366 if (cwd != "/") cwd += "/";
367 cwd += str;
368 }
369
370 if (mShowNavFolders == 0 && mShowFiles == 0) {
371 // nav folders and files are disabled, this is probably the restore list and we need to save chosen location to mVariable instead of mPathVar
372 DataManager::SetValue(mVariable, cwd);
373 } else {
374 // We are changing paths, so we need to set mPathVar
375 DataManager::SetValue(mPathVar, cwd);
376 }
377 } else if (!mVariable.empty()) {
378 str = mFileList.at(item_selected - folderSize).fileName;
379 if (mSelection != "0")
380 DataManager::SetValue(mSelection, str);
381
382 std::string cwd;
383 DataManager::GetValue(mPathVar, cwd);
384 if (cwd != "/")
385 cwd += "/";
386 DataManager::SetValue(mVariable, cwd + str);
387 }
388 }
389 mUpdate = 1;
390}