blob: 9275d976d615b27b087d9401302295f5ddf95815 [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"
Dees_Troy51a0e822012-09-05 15:24:24 -040033
Dees_Troy51a0e822012-09-05 15:24:24 -040034int GUIFileSelector::mSortOrder = 0;
35
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010036GUIFileSelector::GUIFileSelector(xml_node<>* node) : GUIScrollList(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040037{
38 xml_attribute<>* attr;
39 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -040040
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010041 mFolderIcon = mFileIcon = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -040042 mShowFolders = mShowFiles = mShowNavFolders = 1;
43 mUpdate = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040044 mPathVar = "cwd";
Dees_Troyc0583f52013-02-28 11:19:57 -060045 updateFileList = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040046
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010047 // Load filter for filtering files (e.g. *.zip for only zips)
that72b90e32015-02-23 22:57:14 +010048 child = FindNode(node, "filter");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010049 if (child) {
Dees_Troy51a0e822012-09-05 15:24:24 -040050 attr = child->first_attribute("extn");
51 if (attr)
52 mExtn = attr->value();
53 attr = child->first_attribute("folders");
54 if (attr)
55 mShowFolders = atoi(attr->value());
56 attr = child->first_attribute("files");
57 if (attr)
58 mShowFiles = atoi(attr->value());
59 attr = child->first_attribute("nav");
60 if (attr)
61 mShowNavFolders = atoi(attr->value());
62 }
63
64 // Handle the path variable
that72b90e32015-02-23 22:57:14 +010065 child = FindNode(node, "path");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010066 if (child) {
Dees_Troy51a0e822012-09-05 15:24:24 -040067 attr = child->first_attribute("name");
68 if (attr)
69 mPathVar = attr->value();
70 attr = child->first_attribute("default");
Ethan Yonker21ff02a2015-02-18 14:35:00 -060071 if (attr) {
72 mPathDefault = attr->value();
Dees_Troy51a0e822012-09-05 15:24:24 -040073 DataManager::SetValue(mPathVar, attr->value());
Ethan Yonker21ff02a2015-02-18 14:35:00 -060074 }
Dees_Troy51a0e822012-09-05 15:24:24 -040075 }
76
77 // Handle the result variable
that72b90e32015-02-23 22:57:14 +010078 child = FindNode(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
that72b90e32015-02-23 22:57:14 +010089 child = FindNode(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
that72b90e32015-02-23 22:57:14 +0100102 child = FindNode(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
that72b90e32015-02-23 22:57:14 +0100109 child = FindNode(node, "icon");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100110 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);
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600171 if (value.empty())
172 DataManager::SetValue(mPathVar, mPathDefault);
Dees_Troyc0583f52013-02-28 11:19:57 -0600173 }
174 updateFileList = true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400175 mUpdate = 1;
176 return 0;
177 }
178 return 0;
179}
180
Dees_Troy51a0e822012-09-05 15:24:24 -0400181bool GUIFileSelector::fileSort(FileData d1, FileData d2)
182{
183 if (d1.fileName == ".")
184 return -1;
185 if (d2.fileName == ".")
186 return 0;
thatc12a7f02016-01-28 20:52:16 +0100187 if (d1.fileName == "..")
Dees_Troy51a0e822012-09-05 15:24:24 -0400188 return -1;
thatc12a7f02016-01-28 20:52:16 +0100189 if (d2.fileName == "..")
Dees_Troy51a0e822012-09-05 15:24:24 -0400190 return 0;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500191
Dees_Troy51a0e822012-09-05 15:24:24 -0400192 switch (mSortOrder) {
193 case 3: // by size largest first
194 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
195 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) < 0);
Dees_Troy6ef66352013-02-21 08:26:57 -0600196 return d1.fileSize < d2.fileSize;
Dees_Troy51a0e822012-09-05 15:24:24 -0400197 case -3: // by size smallest first
198 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
199 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) > 0);
Dees_Troy6ef66352013-02-21 08:26:57 -0600200 return d1.fileSize > d2.fileSize;
Dees_Troy51a0e822012-09-05 15:24:24 -0400201 case 2: // by last modified date newest first
202 if (d1.lastModified == d2.lastModified)
203 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) < 0);
Dees_Troy6ef66352013-02-21 08:26:57 -0600204 return d1.lastModified < d2.lastModified;
Dees_Troy51a0e822012-09-05 15:24:24 -0400205 case -2: // by date oldest first
206 if (d1.lastModified == d2.lastModified)
207 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) > 0);
Dees_Troy6ef66352013-02-21 08:26:57 -0600208 return d1.lastModified > d2.lastModified;
Dees_Troy51a0e822012-09-05 15:24:24 -0400209 case -1: // by name descending
210 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) > 0);
211 default: // should be a 1 - sort by name ascending
212 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) < 0);
213 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100214 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400215}
216
217int GUIFileSelector::GetFileList(const std::string folder)
218{
219 DIR* d;
220 struct dirent* de;
221 struct stat st;
222
223 // Clear all data
224 mFolderList.clear();
225 mFileList.clear();
226
227 d = opendir(folder.c_str());
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100228 if (d == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000229 LOGINFO("Unable to open '%s'\n", folder.c_str());
Dees_Troy80a11d92013-01-25 16:36:07 +0000230 if (folder != "/" && (mShowNavFolders != 0 || mShowFiles != 0)) {
231 size_t found;
232 found = folder.find_last_of('/');
233 if (found != string::npos) {
234 string new_folder = folder.substr(0, found);
235
236 if (new_folder.length() < 2)
237 new_folder = "/";
238 DataManager::SetValue(mPathVar, new_folder);
239 }
240 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400241 return -1;
242 }
243
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100244 while ((de = readdir(d)) != NULL) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400245 FileData data;
246
247 data.fileName = de->d_name;
248 if (data.fileName == ".")
249 continue;
250 if (data.fileName == ".." && folder == "/")
251 continue;
thatc12a7f02016-01-28 20:52:16 +0100252
253 data.fileType = de->d_type;
Dees_Troy51a0e822012-09-05 15:24:24 -0400254
255 std::string path = folder + "/" + data.fileName;
256 stat(path.c_str(), &st);
257 data.protection = st.st_mode;
258 data.userId = st.st_uid;
259 data.groupId = st.st_gid;
260 data.fileSize = st.st_size;
261 data.lastAccess = st.st_atime;
262 data.lastModified = st.st_mtime;
263 data.lastStatChange = st.st_ctime;
264
Dees_Troy3ee47bc2013-01-25 21:47:37 +0000265 if (data.fileType == DT_UNKNOWN) {
266 data.fileType = TWFunc::Get_D_Type_From_Stat(path);
267 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100268 if (data.fileType == DT_DIR) {
thatc12a7f02016-01-28 20:52:16 +0100269 if (mShowNavFolders || (data.fileName != "." && data.fileName != ".."))
Dees_Troy51a0e822012-09-05 15:24:24 -0400270 mFolderList.push_back(data);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100271 } else if (data.fileType == DT_REG || data.fileType == DT_LNK || data.fileType == DT_BLK) {
272 if (mExtn.empty() || (data.fileName.length() > mExtn.length() && data.fileName.substr(data.fileName.length() - mExtn.length()) == mExtn)) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400273 mFileList.push_back(data);
274 }
275 }
276 }
277 closedir(d);
278
279 std::sort(mFolderList.begin(), mFolderList.end(), fileSort);
280 std::sort(mFileList.begin(), mFileList.end(), fileSort);
Dees_Troyc0583f52013-02-28 11:19:57 -0600281
Dees_Troy51a0e822012-09-05 15:24:24 -0400282 return 0;
283}
284
285void GUIFileSelector::SetPageFocus(int inFocus)
286{
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100287 GUIScrollList::SetPageFocus(inFocus);
288 if (inFocus) {
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600289 std::string value;
290 DataManager::GetValue(mPathVar, value);
291 if (value.empty())
292 DataManager::SetValue(mPathVar, mPathDefault);
Dees_Troyc0583f52013-02-28 11:19:57 -0600293 updateFileList = true;
Dees_Troyc0583f52013-02-28 11:19:57 -0600294 mUpdate = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400295 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500296}
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100297
298size_t GUIFileSelector::GetItemCount()
299{
300 size_t folderSize = mShowFolders ? mFolderList.size() : 0;
301 size_t fileSize = mShowFiles ? mFileList.size() : 0;
302 return folderSize + fileSize;
303}
304
that0af77952015-02-25 08:52:19 +0100305void GUIFileSelector::RenderItem(size_t itemindex, int yPos, bool selected)
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100306{
307 size_t folderSize = mShowFolders ? mFolderList.size() : 0;
308 size_t fileSize = mShowFiles ? mFileList.size() : 0;
309
that0af77952015-02-25 08:52:19 +0100310 ImageResource* icon;
311 std::string text;
312
313 if (itemindex < folderSize) {
314 text = mFolderList.at(itemindex).fileName;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100315 icon = mFolderIcon;
thatc12a7f02016-01-28 20:52:16 +0100316 if (text == "..")
317 text = gui_lookup("up_a_level", "(Up A Level)");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100318 } else {
that0af77952015-02-25 08:52:19 +0100319 text = mFileList.at(itemindex - folderSize).fileName;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100320 icon = mFileIcon;
321 }
that0af77952015-02-25 08:52:19 +0100322
323 RenderStdItem(yPos, selected, icon, text.c_str());
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100324}
325
326void GUIFileSelector::NotifySelect(size_t item_selected)
327{
328 size_t folderSize = mShowFolders ? mFolderList.size() : 0;
329 size_t fileSize = mShowFiles ? mFileList.size() : 0;
330
331 if (item_selected < folderSize + fileSize) {
332 // We've selected an item!
333 std::string str;
334 if (item_selected < folderSize) {
335 std::string cwd;
336
337 str = mFolderList.at(item_selected).fileName;
338 if (mSelection != "0")
339 DataManager::SetValue(mSelection, str);
340 DataManager::GetValue(mPathVar, cwd);
341
342 // Ignore requests to do nothing
343 if (str == ".") return;
thatc12a7f02016-01-28 20:52:16 +0100344 if (str == "..") {
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100345 if (cwd != "/") {
346 size_t found;
347 found = cwd.find_last_of('/');
348 cwd = cwd.substr(0,found);
349
350 if (cwd.length() < 2) cwd = "/";
351 }
352 } else {
353 // Add a slash if we're not the root folder
354 if (cwd != "/") cwd += "/";
355 cwd += str;
356 }
357
358 if (mShowNavFolders == 0 && mShowFiles == 0) {
359 // nav folders and files are disabled, this is probably the restore list and we need to save chosen location to mVariable instead of mPathVar
360 DataManager::SetValue(mVariable, cwd);
361 } else {
362 // We are changing paths, so we need to set mPathVar
363 DataManager::SetValue(mPathVar, cwd);
364 }
365 } else if (!mVariable.empty()) {
366 str = mFileList.at(item_selected - folderSize).fileName;
367 if (mSelection != "0")
368 DataManager::SetValue(mSelection, str);
369
370 std::string cwd;
371 DataManager::GetValue(mPathVar, cwd);
372 if (cwd != "/")
373 cwd += "/";
374 DataManager::SetValue(mVariable, cwd + str);
375 }
376 }
377 mUpdate = 1;
378}