blob: c4aaab1685cad10b90fe11c13e50f9830cb49559 [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}
Ethan Yonkerfbb43532015-12-28 21:54:50 +010027#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040028
29#include "rapidxml.hpp"
30#include "objects.hpp"
31#include "../data.hpp"
Dees_Troy3ee47bc2013-01-25 21:47:37 +000032#include "../twrp-functions.hpp"
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -040033#include "../adbbu/libtwadbbu.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040034
Dees_Troy51a0e822012-09-05 15:24:24 -040035int GUIFileSelector::mSortOrder = 0;
36
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010037GUIFileSelector::GUIFileSelector(xml_node<>* node) : GUIScrollList(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040038{
39 xml_attribute<>* attr;
40 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -040041
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010042 mFolderIcon = mFileIcon = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -040043 mShowFolders = mShowFiles = mShowNavFolders = 1;
44 mUpdate = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040045 mPathVar = "cwd";
Dees_Troyc0583f52013-02-28 11:19:57 -060046 updateFileList = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040047
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010048 // Load filter for filtering files (e.g. *.zip for only zips)
that72b90e32015-02-23 22:57:14 +010049 child = FindNode(node, "filter");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010050 if (child) {
Dees_Troy51a0e822012-09-05 15:24:24 -040051 attr = child->first_attribute("extn");
52 if (attr)
53 mExtn = attr->value();
54 attr = child->first_attribute("folders");
55 if (attr)
56 mShowFolders = atoi(attr->value());
57 attr = child->first_attribute("files");
58 if (attr)
59 mShowFiles = atoi(attr->value());
60 attr = child->first_attribute("nav");
61 if (attr)
62 mShowNavFolders = atoi(attr->value());
63 }
64
65 // Handle the path variable
that72b90e32015-02-23 22:57:14 +010066 child = FindNode(node, "path");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010067 if (child) {
Dees_Troy51a0e822012-09-05 15:24:24 -040068 attr = child->first_attribute("name");
69 if (attr)
70 mPathVar = attr->value();
71 attr = child->first_attribute("default");
Ethan Yonker21ff02a2015-02-18 14:35:00 -060072 if (attr) {
73 mPathDefault = attr->value();
Dees_Troy51a0e822012-09-05 15:24:24 -040074 DataManager::SetValue(mPathVar, attr->value());
Ethan Yonker21ff02a2015-02-18 14:35:00 -060075 }
Dees_Troy51a0e822012-09-05 15:24:24 -040076 }
77
78 // Handle the result variable
that72b90e32015-02-23 22:57:14 +010079 child = FindNode(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
that72b90e32015-02-23 22:57:14 +010090 child = FindNode(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
that72b90e32015-02-23 22:57:14 +0100103 child = FindNode(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
that72b90e32015-02-23 22:57:14 +0100110 child = FindNode(node, "icon");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100111 if (child) {
thatf6ed8fc2015-02-14 20:23:16 +0100112 mFolderIcon = LoadAttrImage(child, "folder");
113 mFileIcon = LoadAttrImage(child, "file");
Vojtech Bocek7cc278b2013-02-24 01:40:19 +0100114 }
thatf6ed8fc2015-02-14 20:23:16 +0100115 int iconWidth = std::max(mFolderIcon->GetWidth(), mFileIcon->GetWidth());
116 int iconHeight = std::max(mFolderIcon->GetHeight(), mFileIcon->GetHeight());
117 SetMaxIconSize(iconWidth, iconHeight);
Dees_Troy51a0e822012-09-05 15:24:24 -0400118
119 // Fetch the file/folder list
120 std::string value;
121 DataManager::GetValue(mPathVar, value);
Dees_Troy80a11d92013-01-25 16:36:07 +0000122 GetFileList(value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400123}
124
125GUIFileSelector::~GUIFileSelector()
126{
Dees_Troy51a0e822012-09-05 15:24:24 -0400127}
128
129int GUIFileSelector::Update(void)
130{
Matt Mowera8a89d12016-12-30 18:10:37 -0600131 if (!isConditionTrue())
Vojtech Bocekede51c52014-02-07 23:58:09 +0100132 return 0;
133
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100134 GUIScrollList::Update();
135
136 // Update the file list if needed
137 if (updateFileList) {
138 string value;
139 DataManager::GetValue(mPathVar, value);
140 if (GetFileList(value) == 0) {
141 updateFileList = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400142 mUpdate = 1;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100143 } else
144 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400145 }
146
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100147 if (mUpdate) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400148 mUpdate = 0;
149 if (Render() == 0)
150 return 2;
151 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400152 return 0;
153}
154
Vojtech Bocek07220562014-02-08 02:05:33 +0100155int GUIFileSelector::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400156{
thatfa30aca2015-02-13 01:22:22 +0100157 GUIScrollList::NotifyVarChange(varName, value);
158
Matt Mowera8a89d12016-12-30 18:10:37 -0600159 if (!isConditionTrue())
Vojtech Bocekede51c52014-02-07 23:58:09 +0100160 return 0;
161
Dees_Troy146d72a2013-03-11 17:46:19 +0000162 if (varName.empty()) {
163 // Always clear the data variable so we know to use it
164 DataManager::SetValue(mVariable, "");
165 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100166 if (varName == mPathVar || varName == mSortVariable) {
Dees_Troy4622cf92013-03-01 15:29:36 -0600167 if (varName == mSortVariable) {
168 DataManager::GetValue(mSortVariable, mSortOrder);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100169 } else {
170 // Reset the list to the top
171 SetVisibleListLocation(0);
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600172 if (value.empty())
173 DataManager::SetValue(mPathVar, mPathDefault);
Dees_Troyc0583f52013-02-28 11:19:57 -0600174 }
175 updateFileList = true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400176 mUpdate = 1;
177 return 0;
178 }
179 return 0;
180}
181
Dees_Troy51a0e822012-09-05 15:24:24 -0400182bool GUIFileSelector::fileSort(FileData d1, FileData d2)
183{
184 if (d1.fileName == ".")
185 return -1;
186 if (d2.fileName == ".")
187 return 0;
thatc12a7f02016-01-28 20:52:16 +0100188 if (d1.fileName == "..")
Dees_Troy51a0e822012-09-05 15:24:24 -0400189 return -1;
thatc12a7f02016-01-28 20:52:16 +0100190 if (d2.fileName == "..")
Dees_Troy51a0e822012-09-05 15:24:24 -0400191 return 0;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500192
Dees_Troy51a0e822012-09-05 15:24:24 -0400193 switch (mSortOrder) {
194 case 3: // by size largest first
195 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
196 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) < 0);
Dees_Troy6ef66352013-02-21 08:26:57 -0600197 return d1.fileSize < d2.fileSize;
Dees_Troy51a0e822012-09-05 15:24:24 -0400198 case -3: // by size smallest first
199 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
200 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) > 0);
Dees_Troy6ef66352013-02-21 08:26:57 -0600201 return d1.fileSize > d2.fileSize;
Dees_Troy51a0e822012-09-05 15:24:24 -0400202 case 2: // by last modified date newest first
203 if (d1.lastModified == d2.lastModified)
204 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) < 0);
Dees_Troy6ef66352013-02-21 08:26:57 -0600205 return d1.lastModified < d2.lastModified;
Dees_Troy51a0e822012-09-05 15:24:24 -0400206 case -2: // by date oldest first
207 if (d1.lastModified == d2.lastModified)
208 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) > 0);
Dees_Troy6ef66352013-02-21 08:26:57 -0600209 return d1.lastModified > d2.lastModified;
Dees_Troy51a0e822012-09-05 15:24:24 -0400210 case -1: // by name descending
211 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) > 0);
212 default: // should be a 1 - sort by name ascending
213 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) < 0);
214 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100215 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400216}
217
218int GUIFileSelector::GetFileList(const std::string folder)
219{
220 DIR* d;
221 struct dirent* de;
222 struct stat st;
223
224 // Clear all data
225 mFolderList.clear();
226 mFileList.clear();
227
228 d = opendir(folder.c_str());
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100229 if (d == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000230 LOGINFO("Unable to open '%s'\n", folder.c_str());
Dees_Troy80a11d92013-01-25 16:36:07 +0000231 if (folder != "/" && (mShowNavFolders != 0 || mShowFiles != 0)) {
232 size_t found;
233 found = folder.find_last_of('/');
234 if (found != string::npos) {
235 string new_folder = folder.substr(0, found);
236
237 if (new_folder.length() < 2)
238 new_folder = "/";
239 DataManager::SetValue(mPathVar, new_folder);
240 }
241 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400242 return -1;
243 }
244
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100245 while ((de = readdir(d)) != NULL) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400246 FileData data;
247
248 data.fileName = de->d_name;
249 if (data.fileName == ".")
250 continue;
251 if (data.fileName == ".." && folder == "/")
252 continue;
thatc12a7f02016-01-28 20:52:16 +0100253
254 data.fileType = de->d_type;
Dees_Troy51a0e822012-09-05 15:24:24 -0400255
256 std::string path = folder + "/" + data.fileName;
257 stat(path.c_str(), &st);
258 data.protection = st.st_mode;
259 data.userId = st.st_uid;
260 data.groupId = st.st_gid;
261 data.fileSize = st.st_size;
262 data.lastAccess = st.st_atime;
263 data.lastModified = st.st_mtime;
264 data.lastStatChange = st.st_ctime;
265
Dees_Troy3ee47bc2013-01-25 21:47:37 +0000266 if (data.fileType == DT_UNKNOWN) {
267 data.fileType = TWFunc::Get_D_Type_From_Stat(path);
268 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100269 if (data.fileType == DT_DIR) {
thatc12a7f02016-01-28 20:52:16 +0100270 if (mShowNavFolders || (data.fileName != "." && data.fileName != ".."))
Dees_Troy51a0e822012-09-05 15:24:24 -0400271 mFolderList.push_back(data);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100272 } else if (data.fileType == DT_REG || data.fileType == DT_LNK || data.fileType == DT_BLK) {
273 if (mExtn.empty() || (data.fileName.length() > mExtn.length() && data.fileName.substr(data.fileName.length() - mExtn.length()) == mExtn)) {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400274 if (mExtn == ".ab" && twadbbu::Check_ADB_Backup_File(path))
275 mFolderList.push_back(data);
276 else
277 mFileList.push_back(data);
Dees_Troy51a0e822012-09-05 15:24:24 -0400278 }
279 }
280 }
281 closedir(d);
282
283 std::sort(mFolderList.begin(), mFolderList.end(), fileSort);
284 std::sort(mFileList.begin(), mFileList.end(), fileSort);
Dees_Troyc0583f52013-02-28 11:19:57 -0600285
Dees_Troy51a0e822012-09-05 15:24:24 -0400286 return 0;
287}
288
289void GUIFileSelector::SetPageFocus(int inFocus)
290{
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100291 GUIScrollList::SetPageFocus(inFocus);
292 if (inFocus) {
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600293 std::string value;
294 DataManager::GetValue(mPathVar, value);
295 if (value.empty())
296 DataManager::SetValue(mPathVar, mPathDefault);
Dees_Troyc0583f52013-02-28 11:19:57 -0600297 updateFileList = true;
Dees_Troyc0583f52013-02-28 11:19:57 -0600298 mUpdate = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400299 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500300}
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100301
302size_t GUIFileSelector::GetItemCount()
303{
304 size_t folderSize = mShowFolders ? mFolderList.size() : 0;
305 size_t fileSize = mShowFiles ? mFileList.size() : 0;
306 return folderSize + fileSize;
307}
308
that0af77952015-02-25 08:52:19 +0100309void GUIFileSelector::RenderItem(size_t itemindex, int yPos, bool selected)
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100310{
311 size_t folderSize = mShowFolders ? mFolderList.size() : 0;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100312
that0af77952015-02-25 08:52:19 +0100313 ImageResource* icon;
314 std::string text;
315
316 if (itemindex < folderSize) {
317 text = mFolderList.at(itemindex).fileName;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100318 icon = mFolderIcon;
thatc12a7f02016-01-28 20:52:16 +0100319 if (text == "..")
320 text = gui_lookup("up_a_level", "(Up A Level)");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100321 } else {
that0af77952015-02-25 08:52:19 +0100322 text = mFileList.at(itemindex - folderSize).fileName;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100323 icon = mFileIcon;
324 }
that0af77952015-02-25 08:52:19 +0100325
326 RenderStdItem(yPos, selected, icon, text.c_str());
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100327}
328
329void GUIFileSelector::NotifySelect(size_t item_selected)
330{
331 size_t folderSize = mShowFolders ? mFolderList.size() : 0;
332 size_t fileSize = mShowFiles ? mFileList.size() : 0;
333
334 if (item_selected < folderSize + fileSize) {
335 // We've selected an item!
336 std::string str;
337 if (item_selected < folderSize) {
338 std::string cwd;
339
340 str = mFolderList.at(item_selected).fileName;
341 if (mSelection != "0")
342 DataManager::SetValue(mSelection, str);
343 DataManager::GetValue(mPathVar, cwd);
344
345 // Ignore requests to do nothing
346 if (str == ".") return;
thatc12a7f02016-01-28 20:52:16 +0100347 if (str == "..") {
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100348 if (cwd != "/") {
349 size_t found;
350 found = cwd.find_last_of('/');
351 cwd = cwd.substr(0,found);
352
353 if (cwd.length() < 2) cwd = "/";
354 }
355 } else {
356 // Add a slash if we're not the root folder
357 if (cwd != "/") cwd += "/";
358 cwd += str;
359 }
360
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400361 if (mShowNavFolders == 0 && (mShowFiles == 0 || mExtn == ".ab")) {
362 // this is probably the restore list and we need to save chosen location to mVariable instead of mPathVar
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100363 DataManager::SetValue(mVariable, cwd);
364 } else {
365 // We are changing paths, so we need to set mPathVar
366 DataManager::SetValue(mPathVar, cwd);
367 }
368 } else if (!mVariable.empty()) {
369 str = mFileList.at(item_selected - folderSize).fileName;
370 if (mSelection != "0")
371 DataManager::SetValue(mSelection, str);
372
373 std::string cwd;
374 DataManager::GetValue(mPathVar, cwd);
375 if (cwd != "/")
376 cwd += "/";
377 DataManager::SetValue(mVariable, cwd + str);
378 }
379 }
380 mUpdate = 1;
381}