blob: 4a26349cde90eea54ad339823556f31b30c7c439 [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{
Dees_Troy51a0e822012-09-05 15:24:24 -0400144}
145
146int GUIFileSelector::Update(void)
147{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100148 if(!isConditionTrue())
149 return 0;
150
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100151 GUIScrollList::Update();
152
153 // Update the file list if needed
154 if (updateFileList) {
155 string value;
156 DataManager::GetValue(mPathVar, value);
157 if (GetFileList(value) == 0) {
158 updateFileList = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400159 mUpdate = 1;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100160 } else
161 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400162 }
163
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100164 if (mUpdate) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400165 mUpdate = 0;
166 if (Render() == 0)
167 return 2;
168 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400169 return 0;
170}
171
Vojtech Bocek07220562014-02-08 02:05:33 +0100172int GUIFileSelector::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400173{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100174 if(!isConditionTrue())
175 return 0;
176
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100177 GUIScrollList::NotifyVarChange(varName, value);
178
Dees_Troy146d72a2013-03-11 17:46:19 +0000179 if (varName.empty()) {
180 // Always clear the data variable so we know to use it
181 DataManager::SetValue(mVariable, "");
182 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100183 if (varName == mPathVar || varName == mSortVariable) {
Dees_Troy4622cf92013-03-01 15:29:36 -0600184 if (varName == mSortVariable) {
185 DataManager::GetValue(mSortVariable, mSortOrder);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100186 } else {
187 // Reset the list to the top
188 SetVisibleListLocation(0);
Dees_Troyc0583f52013-02-28 11:19:57 -0600189 }
190 updateFileList = true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400191 mUpdate = 1;
192 return 0;
193 }
194 return 0;
195}
196
Dees_Troy51a0e822012-09-05 15:24:24 -0400197bool GUIFileSelector::fileSort(FileData d1, FileData d2)
198{
199 if (d1.fileName == ".")
200 return -1;
201 if (d2.fileName == ".")
202 return 0;
203 if (d1.fileName == TW_FILESELECTOR_UP_A_LEVEL)
204 return -1;
205 if (d2.fileName == TW_FILESELECTOR_UP_A_LEVEL)
206 return 0;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500207
Dees_Troy51a0e822012-09-05 15:24:24 -0400208 switch (mSortOrder) {
209 case 3: // by size largest first
210 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
211 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) < 0);
Dees_Troy6ef66352013-02-21 08:26:57 -0600212 return d1.fileSize < d2.fileSize;
Dees_Troy51a0e822012-09-05 15:24:24 -0400213 case -3: // by size smallest first
214 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
215 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) > 0);
Dees_Troy6ef66352013-02-21 08:26:57 -0600216 return d1.fileSize > d2.fileSize;
Dees_Troy51a0e822012-09-05 15:24:24 -0400217 case 2: // by last modified date newest first
218 if (d1.lastModified == d2.lastModified)
219 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) < 0);
Dees_Troy6ef66352013-02-21 08:26:57 -0600220 return d1.lastModified < d2.lastModified;
Dees_Troy51a0e822012-09-05 15:24:24 -0400221 case -2: // by date oldest first
222 if (d1.lastModified == d2.lastModified)
223 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) > 0);
Dees_Troy6ef66352013-02-21 08:26:57 -0600224 return d1.lastModified > d2.lastModified;
Dees_Troy51a0e822012-09-05 15:24:24 -0400225 case -1: // by name descending
226 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) > 0);
227 default: // should be a 1 - sort by name ascending
228 return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) < 0);
229 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100230 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400231}
232
233int GUIFileSelector::GetFileList(const std::string folder)
234{
235 DIR* d;
236 struct dirent* de;
237 struct stat st;
238
239 // Clear all data
240 mFolderList.clear();
241 mFileList.clear();
242
243 d = opendir(folder.c_str());
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100244 if (d == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000245 LOGINFO("Unable to open '%s'\n", folder.c_str());
Dees_Troy80a11d92013-01-25 16:36:07 +0000246 if (folder != "/" && (mShowNavFolders != 0 || mShowFiles != 0)) {
247 size_t found;
248 found = folder.find_last_of('/');
249 if (found != string::npos) {
250 string new_folder = folder.substr(0, found);
251
252 if (new_folder.length() < 2)
253 new_folder = "/";
254 DataManager::SetValue(mPathVar, new_folder);
255 }
256 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400257 return -1;
258 }
259
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100260 while ((de = readdir(d)) != NULL) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400261 FileData data;
262
263 data.fileName = de->d_name;
264 if (data.fileName == ".")
265 continue;
266 if (data.fileName == ".." && folder == "/")
267 continue;
Dees_Troy3ee47bc2013-01-25 21:47:37 +0000268 if (data.fileName == "..") {
Dees_Troy51a0e822012-09-05 15:24:24 -0400269 data.fileName = TW_FILESELECTOR_UP_A_LEVEL;
Dees_Troy3ee47bc2013-01-25 21:47:37 +0000270 data.fileType = DT_DIR;
271 } else {
272 data.fileType = de->d_type;
273 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400274
275 std::string path = folder + "/" + data.fileName;
276 stat(path.c_str(), &st);
277 data.protection = st.st_mode;
278 data.userId = st.st_uid;
279 data.groupId = st.st_gid;
280 data.fileSize = st.st_size;
281 data.lastAccess = st.st_atime;
282 data.lastModified = st.st_mtime;
283 data.lastStatChange = st.st_ctime;
284
Dees_Troy3ee47bc2013-01-25 21:47:37 +0000285 if (data.fileType == DT_UNKNOWN) {
286 data.fileType = TWFunc::Get_D_Type_From_Stat(path);
287 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100288 if (data.fileType == DT_DIR) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400289 if (mShowNavFolders || (data.fileName != "." && data.fileName != TW_FILESELECTOR_UP_A_LEVEL))
290 mFolderList.push_back(data);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100291 } else if (data.fileType == DT_REG || data.fileType == DT_LNK || data.fileType == DT_BLK) {
292 if (mExtn.empty() || (data.fileName.length() > mExtn.length() && data.fileName.substr(data.fileName.length() - mExtn.length()) == mExtn)) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400293 mFileList.push_back(data);
294 }
295 }
296 }
297 closedir(d);
298
299 std::sort(mFolderList.begin(), mFolderList.end(), fileSort);
300 std::sort(mFileList.begin(), mFileList.end(), fileSort);
Dees_Troyc0583f52013-02-28 11:19:57 -0600301
Dees_Troy51a0e822012-09-05 15:24:24 -0400302 return 0;
303}
304
305void GUIFileSelector::SetPageFocus(int inFocus)
306{
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100307 GUIScrollList::SetPageFocus(inFocus);
308 if (inFocus) {
Dees_Troyc0583f52013-02-28 11:19:57 -0600309 updateFileList = true;
Dees_Troyc0583f52013-02-28 11:19:57 -0600310 mUpdate = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400311 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500312}
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100313
314size_t GUIFileSelector::GetItemCount()
315{
316 size_t folderSize = mShowFolders ? mFolderList.size() : 0;
317 size_t fileSize = mShowFiles ? mFileList.size() : 0;
318 return folderSize + fileSize;
319}
320
321int GUIFileSelector::GetListItem(size_t item_index, Resource*& icon, std::string &text)
322{
323 size_t folderSize = mShowFolders ? mFolderList.size() : 0;
324 size_t fileSize = mShowFiles ? mFileList.size() : 0;
325
326 if (item_index < folderSize) {
327 text = mFolderList.at(item_index).fileName;
328 icon = mFolderIcon;
329 } else {
330 text = mFileList.at(item_index - folderSize).fileName;
331 icon = mFileIcon;
332 }
333 return 0;
334}
335
336void GUIFileSelector::NotifySelect(size_t item_selected)
337{
338 size_t folderSize = mShowFolders ? mFolderList.size() : 0;
339 size_t fileSize = mShowFiles ? mFileList.size() : 0;
340
341 if (item_selected < folderSize + fileSize) {
342 // We've selected an item!
343 std::string str;
344 if (item_selected < folderSize) {
345 std::string cwd;
346
347 str = mFolderList.at(item_selected).fileName;
348 if (mSelection != "0")
349 DataManager::SetValue(mSelection, str);
350 DataManager::GetValue(mPathVar, cwd);
351
352 // Ignore requests to do nothing
353 if (str == ".") return;
354 if (str == TW_FILESELECTOR_UP_A_LEVEL) {
355 if (cwd != "/") {
356 size_t found;
357 found = cwd.find_last_of('/');
358 cwd = cwd.substr(0,found);
359
360 if (cwd.length() < 2) cwd = "/";
361 }
362 } else {
363 // Add a slash if we're not the root folder
364 if (cwd != "/") cwd += "/";
365 cwd += str;
366 }
367
368 if (mShowNavFolders == 0 && mShowFiles == 0) {
369 // nav folders and files are disabled, this is probably the restore list and we need to save chosen location to mVariable instead of mPathVar
370 DataManager::SetValue(mVariable, cwd);
371 } else {
372 // We are changing paths, so we need to set mPathVar
373 DataManager::SetValue(mPathVar, cwd);
374 }
375 } else if (!mVariable.empty()) {
376 str = mFileList.at(item_selected - folderSize).fileName;
377 if (mSelection != "0")
378 DataManager::SetValue(mSelection, str);
379
380 std::string cwd;
381 DataManager::GetValue(mPathVar, cwd);
382 if (cwd != "/")
383 cwd += "/";
384 DataManager::SetValue(mVariable, cwd + str);
385 }
386 }
387 mUpdate = 1;
388}