blob: 8f6a7fa720a1fbbe721cf7467e862d5d1d73bb94 [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)
that72b90e32015-02-23 22:57:14 +010050 child = FindNode(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
that72b90e32015-02-23 22:57:14 +010067 child = FindNode(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");
Ethan Yonker21ff02a2015-02-18 14:35:00 -060073 if (attr) {
74 mPathDefault = attr->value();
Dees_Troy51a0e822012-09-05 15:24:24 -040075 DataManager::SetValue(mPathVar, attr->value());
Ethan Yonker21ff02a2015-02-18 14:35:00 -060076 }
Dees_Troy51a0e822012-09-05 15:24:24 -040077 }
78
79 // Handle the result variable
that72b90e32015-02-23 22:57:14 +010080 child = FindNode(node, "data");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010081 if (child) {
Dees_Troy51a0e822012-09-05 15:24:24 -040082 attr = child->first_attribute("name");
83 if (attr)
84 mVariable = attr->value();
85 attr = child->first_attribute("default");
86 if (attr)
87 DataManager::SetValue(mVariable, attr->value());
88 }
89
90 // Handle the sort variable
that72b90e32015-02-23 22:57:14 +010091 child = FindNode(node, "sort");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010092 if (child) {
Dees_Troy51a0e822012-09-05 15:24:24 -040093 attr = child->first_attribute("name");
94 if (attr)
95 mSortVariable = attr->value();
96 attr = child->first_attribute("default");
97 if (attr)
98 DataManager::SetValue(mSortVariable, attr->value());
99
100 DataManager::GetValue(mSortVariable, mSortOrder);
101 }
102
103 // Handle the selection variable
that72b90e32015-02-23 22:57:14 +0100104 child = FindNode(node, "selection");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100105 if (child && (attr = child->first_attribute("name")))
106 mSelection = attr->value();
107 else
Dees_Troy51a0e822012-09-05 15:24:24 -0400108 mSelection = "0";
109
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100110 // Get folder and file icons if present
that72b90e32015-02-23 22:57:14 +0100111 child = FindNode(node, "icon");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100112 if (child) {
thatf6ed8fc2015-02-14 20:23:16 +0100113 mFolderIcon = LoadAttrImage(child, "folder");
114 mFileIcon = LoadAttrImage(child, "file");
Vojtech Bocek7cc278b2013-02-24 01:40:19 +0100115 }
thatf6ed8fc2015-02-14 20:23:16 +0100116 int iconWidth = std::max(mFolderIcon->GetWidth(), mFileIcon->GetWidth());
117 int iconHeight = std::max(mFolderIcon->GetHeight(), mFileIcon->GetHeight());
118 SetMaxIconSize(iconWidth, iconHeight);
Dees_Troy51a0e822012-09-05 15:24:24 -0400119
120 // Fetch the file/folder list
121 std::string value;
122 DataManager::GetValue(mPathVar, value);
Dees_Troy80a11d92013-01-25 16:36:07 +0000123 GetFileList(value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400124}
125
126GUIFileSelector::~GUIFileSelector()
127{
Dees_Troy51a0e822012-09-05 15:24:24 -0400128}
129
130int GUIFileSelector::Update(void)
131{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100132 if(!isConditionTrue())
133 return 0;
134
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100135 GUIScrollList::Update();
136
137 // Update the file list if needed
138 if (updateFileList) {
139 string value;
140 DataManager::GetValue(mPathVar, value);
141 if (GetFileList(value) == 0) {
142 updateFileList = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400143 mUpdate = 1;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100144 } else
145 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400146 }
147
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100148 if (mUpdate) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400149 mUpdate = 0;
150 if (Render() == 0)
151 return 2;
152 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400153 return 0;
154}
155
Vojtech Bocek07220562014-02-08 02:05:33 +0100156int GUIFileSelector::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400157{
thatfa30aca2015-02-13 01:22:22 +0100158 GUIScrollList::NotifyVarChange(varName, value);
159
Vojtech Bocekede51c52014-02-07 23:58:09 +0100160 if(!isConditionTrue())
161 return 0;
162
Dees_Troy146d72a2013-03-11 17:46:19 +0000163 if (varName.empty()) {
164 // Always clear the data variable so we know to use it
165 DataManager::SetValue(mVariable, "");
166 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100167 if (varName == mPathVar || varName == mSortVariable) {
Dees_Troy4622cf92013-03-01 15:29:36 -0600168 if (varName == mSortVariable) {
169 DataManager::GetValue(mSortVariable, mSortOrder);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100170 } else {
171 // Reset the list to the top
172 SetVisibleListLocation(0);
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600173 if (value.empty())
174 DataManager::SetValue(mPathVar, mPathDefault);
Dees_Troyc0583f52013-02-28 11:19:57 -0600175 }
176 updateFileList = true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400177 mUpdate = 1;
178 return 0;
179 }
180 return 0;
181}
182
Dees_Troy51a0e822012-09-05 15:24:24 -0400183bool GUIFileSelector::fileSort(FileData d1, FileData d2)
184{
185 if (d1.fileName == ".")
186 return -1;
187 if (d2.fileName == ".")
188 return 0;
189 if (d1.fileName == TW_FILESELECTOR_UP_A_LEVEL)
190 return -1;
191 if (d2.fileName == TW_FILESELECTOR_UP_A_LEVEL)
192 return 0;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500193
Dees_Troy51a0e822012-09-05 15:24:24 -0400194 switch (mSortOrder) {
195 case 3: // by size largest 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 -3: // by size smallest first
200 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
201 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) > 0);
Dees_Troy6ef66352013-02-21 08:26:57 -0600202 return d1.fileSize > d2.fileSize;
Dees_Troy51a0e822012-09-05 15:24:24 -0400203 case 2: // by last modified date newest 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 -2: // by date oldest first
208 if (d1.lastModified == d2.lastModified)
209 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) > 0);
Dees_Troy6ef66352013-02-21 08:26:57 -0600210 return d1.lastModified > d2.lastModified;
Dees_Troy51a0e822012-09-05 15:24:24 -0400211 case -1: // by name descending
212 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) > 0);
213 default: // should be a 1 - sort by name ascending
214 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) < 0);
215 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100216 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400217}
218
219int GUIFileSelector::GetFileList(const std::string folder)
220{
221 DIR* d;
222 struct dirent* de;
223 struct stat st;
224
225 // Clear all data
226 mFolderList.clear();
227 mFileList.clear();
228
229 d = opendir(folder.c_str());
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100230 if (d == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000231 LOGINFO("Unable to open '%s'\n", folder.c_str());
Dees_Troy80a11d92013-01-25 16:36:07 +0000232 if (folder != "/" && (mShowNavFolders != 0 || mShowFiles != 0)) {
233 size_t found;
234 found = folder.find_last_of('/');
235 if (found != string::npos) {
236 string new_folder = folder.substr(0, found);
237
238 if (new_folder.length() < 2)
239 new_folder = "/";
240 DataManager::SetValue(mPathVar, new_folder);
241 }
242 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400243 return -1;
244 }
245
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100246 while ((de = readdir(d)) != NULL) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400247 FileData data;
248
249 data.fileName = de->d_name;
250 if (data.fileName == ".")
251 continue;
252 if (data.fileName == ".." && folder == "/")
253 continue;
Dees_Troy3ee47bc2013-01-25 21:47:37 +0000254 if (data.fileName == "..") {
Dees_Troy51a0e822012-09-05 15:24:24 -0400255 data.fileName = TW_FILESELECTOR_UP_A_LEVEL;
Dees_Troy3ee47bc2013-01-25 21:47:37 +0000256 data.fileType = DT_DIR;
257 } else {
258 data.fileType = de->d_type;
259 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400260
261 std::string path = folder + "/" + data.fileName;
262 stat(path.c_str(), &st);
263 data.protection = st.st_mode;
264 data.userId = st.st_uid;
265 data.groupId = st.st_gid;
266 data.fileSize = st.st_size;
267 data.lastAccess = st.st_atime;
268 data.lastModified = st.st_mtime;
269 data.lastStatChange = st.st_ctime;
270
Dees_Troy3ee47bc2013-01-25 21:47:37 +0000271 if (data.fileType == DT_UNKNOWN) {
272 data.fileType = TWFunc::Get_D_Type_From_Stat(path);
273 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100274 if (data.fileType == DT_DIR) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400275 if (mShowNavFolders || (data.fileName != "." && data.fileName != TW_FILESELECTOR_UP_A_LEVEL))
276 mFolderList.push_back(data);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100277 } else if (data.fileType == DT_REG || data.fileType == DT_LNK || data.fileType == DT_BLK) {
278 if (mExtn.empty() || (data.fileName.length() > mExtn.length() && data.fileName.substr(data.fileName.length() - mExtn.length()) == mExtn)) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400279 mFileList.push_back(data);
280 }
281 }
282 }
283 closedir(d);
284
285 std::sort(mFolderList.begin(), mFolderList.end(), fileSort);
286 std::sort(mFileList.begin(), mFileList.end(), fileSort);
Dees_Troyc0583f52013-02-28 11:19:57 -0600287
Dees_Troy51a0e822012-09-05 15:24:24 -0400288 return 0;
289}
290
291void GUIFileSelector::SetPageFocus(int inFocus)
292{
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100293 GUIScrollList::SetPageFocus(inFocus);
294 if (inFocus) {
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600295 std::string value;
296 DataManager::GetValue(mPathVar, value);
297 if (value.empty())
298 DataManager::SetValue(mPathVar, mPathDefault);
Dees_Troyc0583f52013-02-28 11:19:57 -0600299 updateFileList = true;
Dees_Troyc0583f52013-02-28 11:19:57 -0600300 mUpdate = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400301 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500302}
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100303
304size_t GUIFileSelector::GetItemCount()
305{
306 size_t folderSize = mShowFolders ? mFolderList.size() : 0;
307 size_t fileSize = mShowFiles ? mFileList.size() : 0;
308 return folderSize + fileSize;
309}
310
thatf6ed8fc2015-02-14 20:23:16 +0100311int GUIFileSelector::GetListItem(size_t item_index, ImageResource*& icon, std::string &text)
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100312{
313 size_t folderSize = mShowFolders ? mFolderList.size() : 0;
314 size_t fileSize = mShowFiles ? mFileList.size() : 0;
315
316 if (item_index < folderSize) {
317 text = mFolderList.at(item_index).fileName;
318 icon = mFolderIcon;
319 } else {
320 text = mFileList.at(item_index - folderSize).fileName;
321 icon = mFileIcon;
322 }
323 return 0;
324}
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;
344 if (str == TW_FILESELECTOR_UP_A_LEVEL) {
345 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}