blob: 5c287c34df4cf487093f6b44699578b653ae638a [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 mFolderIcon = mFileIcon = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -040044 mShowFolders = mShowFiles = mShowNavFolders = 1;
45 mUpdate = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040046 mPathVar = "cwd";
Dees_Troyc0583f52013-02-28 11:19:57 -060047 updateFileList = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040048
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010049 // Load filter for filtering files (e.g. *.zip for only zips)
Dees_Troy51a0e822012-09-05 15:24:24 -040050 child = node->first_node("filter");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010051 if (child) {
Dees_Troy51a0e822012-09-05 15:24:24 -040052 attr = child->first_attribute("extn");
53 if (attr)
54 mExtn = attr->value();
55 attr = child->first_attribute("folders");
56 if (attr)
57 mShowFolders = atoi(attr->value());
58 attr = child->first_attribute("files");
59 if (attr)
60 mShowFiles = atoi(attr->value());
61 attr = child->first_attribute("nav");
62 if (attr)
63 mShowNavFolders = atoi(attr->value());
64 }
65
66 // Handle the path variable
67 child = node->first_node("path");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010068 if (child) {
Dees_Troy51a0e822012-09-05 15:24:24 -040069 attr = child->first_attribute("name");
70 if (attr)
71 mPathVar = attr->value();
72 attr = child->first_attribute("default");
73 if (attr)
74 DataManager::SetValue(mPathVar, attr->value());
75 }
76
77 // Handle the result variable
78 child = node->first_node("data");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010079 if (child) {
Dees_Troy51a0e822012-09-05 15:24:24 -040080 attr = child->first_attribute("name");
81 if (attr)
82 mVariable = attr->value();
83 attr = child->first_attribute("default");
84 if (attr)
85 DataManager::SetValue(mVariable, attr->value());
86 }
87
88 // Handle the sort variable
89 child = node->first_node("sort");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010090 if (child) {
Dees_Troy51a0e822012-09-05 15:24:24 -040091 attr = child->first_attribute("name");
92 if (attr)
93 mSortVariable = attr->value();
94 attr = child->first_attribute("default");
95 if (attr)
96 DataManager::SetValue(mSortVariable, attr->value());
97
98 DataManager::GetValue(mSortVariable, mSortOrder);
99 }
100
101 // Handle the selection variable
102 child = node->first_node("selection");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100103 if (child && (attr = child->first_attribute("name")))
104 mSelection = attr->value();
105 else
Dees_Troy51a0e822012-09-05 15:24:24 -0400106 mSelection = "0";
107
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100108 // Get folder and file icons if present
109 child = node->first_node("icon");
110 if (child) {
thatf6ed8fc2015-02-14 20:23:16 +0100111 mFolderIcon = LoadAttrImage(child, "folder");
112 mFileIcon = LoadAttrImage(child, "file");
Vojtech Bocek7cc278b2013-02-24 01:40:19 +0100113 }
thatf6ed8fc2015-02-14 20:23:16 +0100114 int iconWidth = std::max(mFolderIcon->GetWidth(), mFileIcon->GetWidth());
115 int iconHeight = std::max(mFolderIcon->GetHeight(), mFileIcon->GetHeight());
116 SetMaxIconSize(iconWidth, iconHeight);
Dees_Troy51a0e822012-09-05 15:24:24 -0400117
118 // Fetch the file/folder list
119 std::string value;
120 DataManager::GetValue(mPathVar, value);
Dees_Troy80a11d92013-01-25 16:36:07 +0000121 GetFileList(value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400122}
123
124GUIFileSelector::~GUIFileSelector()
125{
Dees_Troy51a0e822012-09-05 15:24:24 -0400126}
127
128int GUIFileSelector::Update(void)
129{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100130 if(!isConditionTrue())
131 return 0;
132
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100133 GUIScrollList::Update();
134
135 // Update the file list if needed
136 if (updateFileList) {
137 string value;
138 DataManager::GetValue(mPathVar, value);
139 if (GetFileList(value) == 0) {
140 updateFileList = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400141 mUpdate = 1;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100142 } else
143 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400144 }
145
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100146 if (mUpdate) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400147 mUpdate = 0;
148 if (Render() == 0)
149 return 2;
150 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400151 return 0;
152}
153
Vojtech Bocek07220562014-02-08 02:05:33 +0100154int GUIFileSelector::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400155{
thatfa30aca2015-02-13 01:22:22 +0100156 GUIScrollList::NotifyVarChange(varName, value);
157
Vojtech Bocekede51c52014-02-07 23:58:09 +0100158 if(!isConditionTrue())
159 return 0;
160
Dees_Troy146d72a2013-03-11 17:46:19 +0000161 if (varName.empty()) {
162 // Always clear the data variable so we know to use it
163 DataManager::SetValue(mVariable, "");
164 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100165 if (varName == mPathVar || varName == mSortVariable) {
Dees_Troy4622cf92013-03-01 15:29:36 -0600166 if (varName == mSortVariable) {
167 DataManager::GetValue(mSortVariable, mSortOrder);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100168 } else {
169 // Reset the list to the top
170 SetVisibleListLocation(0);
Dees_Troyc0583f52013-02-28 11:19:57 -0600171 }
172 updateFileList = true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400173 mUpdate = 1;
174 return 0;
175 }
176 return 0;
177}
178
Dees_Troy51a0e822012-09-05 15:24:24 -0400179bool GUIFileSelector::fileSort(FileData d1, FileData d2)
180{
181 if (d1.fileName == ".")
182 return -1;
183 if (d2.fileName == ".")
184 return 0;
185 if (d1.fileName == TW_FILESELECTOR_UP_A_LEVEL)
186 return -1;
187 if (d2.fileName == TW_FILESELECTOR_UP_A_LEVEL)
188 return 0;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500189
Dees_Troy51a0e822012-09-05 15:24:24 -0400190 switch (mSortOrder) {
191 case 3: // by size largest first
192 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
193 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) < 0);
Dees_Troy6ef66352013-02-21 08:26:57 -0600194 return d1.fileSize < d2.fileSize;
Dees_Troy51a0e822012-09-05 15:24:24 -0400195 case -3: // by size smallest first
196 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
197 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) > 0);
Dees_Troy6ef66352013-02-21 08:26:57 -0600198 return d1.fileSize > d2.fileSize;
Dees_Troy51a0e822012-09-05 15:24:24 -0400199 case 2: // by last modified date newest first
200 if (d1.lastModified == d2.lastModified)
201 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) < 0);
Dees_Troy6ef66352013-02-21 08:26:57 -0600202 return d1.lastModified < d2.lastModified;
Dees_Troy51a0e822012-09-05 15:24:24 -0400203 case -2: // by date oldest first
204 if (d1.lastModified == d2.lastModified)
205 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) > 0);
Dees_Troy6ef66352013-02-21 08:26:57 -0600206 return d1.lastModified > d2.lastModified;
Dees_Troy51a0e822012-09-05 15:24:24 -0400207 case -1: // by name descending
208 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) > 0);
209 default: // should be a 1 - sort by name ascending
210 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) < 0);
211 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100212 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400213}
214
215int GUIFileSelector::GetFileList(const std::string folder)
216{
217 DIR* d;
218 struct dirent* de;
219 struct stat st;
220
221 // Clear all data
222 mFolderList.clear();
223 mFileList.clear();
224
225 d = opendir(folder.c_str());
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100226 if (d == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000227 LOGINFO("Unable to open '%s'\n", folder.c_str());
Dees_Troy80a11d92013-01-25 16:36:07 +0000228 if (folder != "/" && (mShowNavFolders != 0 || mShowFiles != 0)) {
229 size_t found;
230 found = folder.find_last_of('/');
231 if (found != string::npos) {
232 string new_folder = folder.substr(0, found);
233
234 if (new_folder.length() < 2)
235 new_folder = "/";
236 DataManager::SetValue(mPathVar, new_folder);
237 }
238 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400239 return -1;
240 }
241
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100242 while ((de = readdir(d)) != NULL) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400243 FileData data;
244
245 data.fileName = de->d_name;
246 if (data.fileName == ".")
247 continue;
248 if (data.fileName == ".." && folder == "/")
249 continue;
Dees_Troy3ee47bc2013-01-25 21:47:37 +0000250 if (data.fileName == "..") {
Dees_Troy51a0e822012-09-05 15:24:24 -0400251 data.fileName = TW_FILESELECTOR_UP_A_LEVEL;
Dees_Troy3ee47bc2013-01-25 21:47:37 +0000252 data.fileType = DT_DIR;
253 } else {
254 data.fileType = de->d_type;
255 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400256
257 std::string path = folder + "/" + data.fileName;
258 stat(path.c_str(), &st);
259 data.protection = st.st_mode;
260 data.userId = st.st_uid;
261 data.groupId = st.st_gid;
262 data.fileSize = st.st_size;
263 data.lastAccess = st.st_atime;
264 data.lastModified = st.st_mtime;
265 data.lastStatChange = st.st_ctime;
266
Dees_Troy3ee47bc2013-01-25 21:47:37 +0000267 if (data.fileType == DT_UNKNOWN) {
268 data.fileType = TWFunc::Get_D_Type_From_Stat(path);
269 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100270 if (data.fileType == DT_DIR) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400271 if (mShowNavFolders || (data.fileName != "." && data.fileName != TW_FILESELECTOR_UP_A_LEVEL))
272 mFolderList.push_back(data);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100273 } else if (data.fileType == DT_REG || data.fileType == DT_LNK || data.fileType == DT_BLK) {
274 if (mExtn.empty() || (data.fileName.length() > mExtn.length() && data.fileName.substr(data.fileName.length() - mExtn.length()) == mExtn)) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400275 mFileList.push_back(data);
276 }
277 }
278 }
279 closedir(d);
280
281 std::sort(mFolderList.begin(), mFolderList.end(), fileSort);
282 std::sort(mFileList.begin(), mFileList.end(), fileSort);
Dees_Troyc0583f52013-02-28 11:19:57 -0600283
Dees_Troy51a0e822012-09-05 15:24:24 -0400284 return 0;
285}
286
287void GUIFileSelector::SetPageFocus(int inFocus)
288{
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100289 GUIScrollList::SetPageFocus(inFocus);
290 if (inFocus) {
Dees_Troyc0583f52013-02-28 11:19:57 -0600291 updateFileList = true;
Dees_Troyc0583f52013-02-28 11:19:57 -0600292 mUpdate = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400293 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500294}
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100295
296size_t GUIFileSelector::GetItemCount()
297{
298 size_t folderSize = mShowFolders ? mFolderList.size() : 0;
299 size_t fileSize = mShowFiles ? mFileList.size() : 0;
300 return folderSize + fileSize;
301}
302
thatf6ed8fc2015-02-14 20:23:16 +0100303int GUIFileSelector::GetListItem(size_t item_index, ImageResource*& icon, std::string &text)
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100304{
305 size_t folderSize = mShowFolders ? mFolderList.size() : 0;
306 size_t fileSize = mShowFiles ? mFileList.size() : 0;
307
308 if (item_index < folderSize) {
309 text = mFolderList.at(item_index).fileName;
310 icon = mFolderIcon;
311 } else {
312 text = mFileList.at(item_index - folderSize).fileName;
313 icon = mFileIcon;
314 }
315 return 0;
316}
317
318void GUIFileSelector::NotifySelect(size_t item_selected)
319{
320 size_t folderSize = mShowFolders ? mFolderList.size() : 0;
321 size_t fileSize = mShowFiles ? mFileList.size() : 0;
322
323 if (item_selected < folderSize + fileSize) {
324 // We've selected an item!
325 std::string str;
326 if (item_selected < folderSize) {
327 std::string cwd;
328
329 str = mFolderList.at(item_selected).fileName;
330 if (mSelection != "0")
331 DataManager::SetValue(mSelection, str);
332 DataManager::GetValue(mPathVar, cwd);
333
334 // Ignore requests to do nothing
335 if (str == ".") return;
336 if (str == TW_FILESELECTOR_UP_A_LEVEL) {
337 if (cwd != "/") {
338 size_t found;
339 found = cwd.find_last_of('/');
340 cwd = cwd.substr(0,found);
341
342 if (cwd.length() < 2) cwd = "/";
343 }
344 } else {
345 // Add a slash if we're not the root folder
346 if (cwd != "/") cwd += "/";
347 cwd += str;
348 }
349
350 if (mShowNavFolders == 0 && mShowFiles == 0) {
351 // nav folders and files are disabled, this is probably the restore list and we need to save chosen location to mVariable instead of mPathVar
352 DataManager::SetValue(mVariable, cwd);
353 } else {
354 // We are changing paths, so we need to set mPathVar
355 DataManager::SetValue(mPathVar, cwd);
356 }
357 } else if (!mVariable.empty()) {
358 str = mFileList.at(item_selected - folderSize).fileName;
359 if (mSelection != "0")
360 DataManager::SetValue(mSelection, str);
361
362 std::string cwd;
363 DataManager::GetValue(mPathVar, cwd);
364 if (cwd != "/")
365 cwd += "/";
366 DataManager::SetValue(mVariable, cwd + str);
367 }
368 }
369 mUpdate = 1;
370}