Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1 | /* |
| 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 18 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 19 | #include <string.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 20 | #include <sys/stat.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 21 | #include <dirent.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 22 | #include <algorithm> |
mauronofrio | 38ed6a7 | 2020-01-10 16:44:28 +0100 | [diff] [blame] | 23 | #ifdef __ANDROID_API_M__ |
| 24 | #include <vector> |
| 25 | #ifdef __ANDROID_API_N__ |
| 26 | #include <android-base/strings.h> |
| 27 | #else |
| 28 | #include <base/strings.h> |
| 29 | #endif |
| 30 | #else |
| 31 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 32 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 33 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 34 | } |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 35 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 36 | |
| 37 | #include "rapidxml.hpp" |
| 38 | #include "objects.hpp" |
| 39 | #include "../data.hpp" |
Dees_Troy | 3ee47bc | 2013-01-25 21:47:37 +0000 | [diff] [blame] | 40 | #include "../twrp-functions.hpp" |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 41 | #include "../adbbu/libtwadbbu.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 42 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 43 | int GUIFileSelector::mSortOrder = 0; |
| 44 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 45 | GUIFileSelector::GUIFileSelector(xml_node<>* node) : GUIScrollList(node) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 46 | { |
| 47 | xml_attribute<>* attr; |
| 48 | xml_node<>* child; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 49 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 50 | mFolderIcon = mFileIcon = NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 51 | mShowFolders = mShowFiles = mShowNavFolders = 1; |
| 52 | mUpdate = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 53 | mPathVar = "cwd"; |
Dees_Troy | c0583f5 | 2013-02-28 11:19:57 -0600 | [diff] [blame] | 54 | updateFileList = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 55 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 56 | // Load filter for filtering files (e.g. *.zip for only zips) |
that | 72b90e3 | 2015-02-23 22:57:14 +0100 | [diff] [blame] | 57 | child = FindNode(node, "filter"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 58 | if (child) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 59 | attr = child->first_attribute("extn"); |
| 60 | if (attr) |
| 61 | mExtn = attr->value(); |
| 62 | attr = child->first_attribute("folders"); |
| 63 | if (attr) |
| 64 | mShowFolders = atoi(attr->value()); |
| 65 | attr = child->first_attribute("files"); |
| 66 | if (attr) |
| 67 | mShowFiles = atoi(attr->value()); |
| 68 | attr = child->first_attribute("nav"); |
| 69 | if (attr) |
| 70 | mShowNavFolders = atoi(attr->value()); |
| 71 | } |
Ian Macdonald | e7c34e5 | 2021-01-23 18:52:28 +0100 | [diff] [blame^] | 72 | child = FindNode(node, "prfxfilter"); |
| 73 | if (child) { |
| 74 | attr = child->first_attribute("prfx"); |
| 75 | if (attr) |
| 76 | mPrfx = attr->value(); |
| 77 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 78 | |
| 79 | // Handle the path variable |
that | 72b90e3 | 2015-02-23 22:57:14 +0100 | [diff] [blame] | 80 | child = FindNode(node, "path"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 81 | if (child) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 82 | attr = child->first_attribute("name"); |
| 83 | if (attr) |
| 84 | mPathVar = attr->value(); |
| 85 | attr = child->first_attribute("default"); |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 86 | if (attr) { |
| 87 | mPathDefault = attr->value(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 88 | DataManager::SetValue(mPathVar, attr->value()); |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 89 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // Handle the result variable |
that | 72b90e3 | 2015-02-23 22:57:14 +0100 | [diff] [blame] | 93 | child = FindNode(node, "data"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 94 | if (child) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 95 | attr = child->first_attribute("name"); |
| 96 | if (attr) |
| 97 | mVariable = attr->value(); |
| 98 | attr = child->first_attribute("default"); |
| 99 | if (attr) |
| 100 | DataManager::SetValue(mVariable, attr->value()); |
| 101 | } |
| 102 | |
| 103 | // Handle the sort variable |
that | 72b90e3 | 2015-02-23 22:57:14 +0100 | [diff] [blame] | 104 | child = FindNode(node, "sort"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 105 | if (child) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 106 | attr = child->first_attribute("name"); |
| 107 | if (attr) |
| 108 | mSortVariable = attr->value(); |
| 109 | attr = child->first_attribute("default"); |
| 110 | if (attr) |
| 111 | DataManager::SetValue(mSortVariable, attr->value()); |
| 112 | |
| 113 | DataManager::GetValue(mSortVariable, mSortOrder); |
| 114 | } |
| 115 | |
| 116 | // Handle the selection variable |
that | 72b90e3 | 2015-02-23 22:57:14 +0100 | [diff] [blame] | 117 | child = FindNode(node, "selection"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 118 | if (child && (attr = child->first_attribute("name"))) |
| 119 | mSelection = attr->value(); |
| 120 | else |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 121 | mSelection = "0"; |
| 122 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 123 | // Get folder and file icons if present |
that | 72b90e3 | 2015-02-23 22:57:14 +0100 | [diff] [blame] | 124 | child = FindNode(node, "icon"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 125 | if (child) { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 126 | mFolderIcon = LoadAttrImage(child, "folder"); |
| 127 | mFileIcon = LoadAttrImage(child, "file"); |
Vojtech Bocek | 7cc278b | 2013-02-24 01:40:19 +0100 | [diff] [blame] | 128 | } |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 129 | int iconWidth = 0, iconHeight = 0; |
| 130 | if (mFolderIcon && mFolderIcon->GetResource() && mFileIcon && mFileIcon->GetResource()) { |
| 131 | iconWidth = std::max(mFolderIcon->GetWidth(), mFileIcon->GetWidth()); |
| 132 | iconHeight = std::max(mFolderIcon->GetHeight(), mFileIcon->GetHeight()); |
| 133 | } else if (mFolderIcon && mFolderIcon->GetResource()) { |
| 134 | iconWidth = mFolderIcon->GetWidth(); |
| 135 | iconHeight = mFolderIcon->GetHeight(); |
| 136 | } else if (mFileIcon && mFileIcon->GetResource()) { |
| 137 | iconWidth = mFileIcon->GetWidth(); |
| 138 | iconHeight = mFileIcon->GetHeight(); |
| 139 | } |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 140 | SetMaxIconSize(iconWidth, iconHeight); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 141 | |
| 142 | // Fetch the file/folder list |
| 143 | std::string value; |
| 144 | DataManager::GetValue(mPathVar, value); |
Dees_Troy | 80a11d9 | 2013-01-25 16:36:07 +0000 | [diff] [blame] | 145 | GetFileList(value); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | GUIFileSelector::~GUIFileSelector() |
| 149 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | int GUIFileSelector::Update(void) |
| 153 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 154 | if (!isConditionTrue()) |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 155 | return 0; |
| 156 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 157 | GUIScrollList::Update(); |
| 158 | |
| 159 | // Update the file list if needed |
| 160 | if (updateFileList) { |
| 161 | string value; |
| 162 | DataManager::GetValue(mPathVar, value); |
| 163 | if (GetFileList(value) == 0) { |
| 164 | updateFileList = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 165 | mUpdate = 1; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 166 | } else |
| 167 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 168 | } |
| 169 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 170 | if (mUpdate) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 171 | mUpdate = 0; |
| 172 | if (Render() == 0) |
| 173 | return 2; |
| 174 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 175 | return 0; |
| 176 | } |
| 177 | |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 178 | int GUIFileSelector::NotifyVarChange(const std::string& varName, const std::string& value) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 179 | { |
that | fa30aca | 2015-02-13 01:22:22 +0100 | [diff] [blame] | 180 | GUIScrollList::NotifyVarChange(varName, value); |
| 181 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 182 | if (!isConditionTrue()) |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 183 | return 0; |
| 184 | |
Dees_Troy | 146d72a | 2013-03-11 17:46:19 +0000 | [diff] [blame] | 185 | if (varName.empty()) { |
| 186 | // Always clear the data variable so we know to use it |
| 187 | DataManager::SetValue(mVariable, ""); |
| 188 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 189 | if (varName == mPathVar || varName == mSortVariable) { |
Dees_Troy | 4622cf9 | 2013-03-01 15:29:36 -0600 | [diff] [blame] | 190 | if (varName == mSortVariable) { |
| 191 | DataManager::GetValue(mSortVariable, mSortOrder); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 192 | } else { |
| 193 | // Reset the list to the top |
| 194 | SetVisibleListLocation(0); |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 195 | if (value.empty()) |
| 196 | DataManager::SetValue(mPathVar, mPathDefault); |
Dees_Troy | c0583f5 | 2013-02-28 11:19:57 -0600 | [diff] [blame] | 197 | } |
| 198 | updateFileList = true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 199 | mUpdate = 1; |
| 200 | return 0; |
| 201 | } |
| 202 | return 0; |
| 203 | } |
| 204 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 205 | bool GUIFileSelector::fileSort(FileData d1, FileData d2) |
| 206 | { |
| 207 | if (d1.fileName == ".") |
| 208 | return -1; |
| 209 | if (d2.fileName == ".") |
| 210 | return 0; |
that | c12a7f0 | 2016-01-28 20:52:16 +0100 | [diff] [blame] | 211 | if (d1.fileName == "..") |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 212 | return -1; |
that | c12a7f0 | 2016-01-28 20:52:16 +0100 | [diff] [blame] | 213 | if (d2.fileName == "..") |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 214 | return 0; |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 215 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 216 | switch (mSortOrder) { |
| 217 | case 3: // by size largest first |
| 218 | 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 |
| 219 | return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) < 0); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 220 | return d1.fileSize < d2.fileSize; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 221 | case -3: // by size smallest first |
| 222 | 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 |
| 223 | return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) > 0); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 224 | return d1.fileSize > d2.fileSize; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 225 | case 2: // by last modified date newest first |
| 226 | if (d1.lastModified == d2.lastModified) |
| 227 | return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) < 0); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 228 | return d1.lastModified < d2.lastModified; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 229 | case -2: // by date oldest first |
| 230 | if (d1.lastModified == d2.lastModified) |
| 231 | return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) > 0); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 232 | return d1.lastModified > d2.lastModified; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 233 | case -1: // by name descending |
| 234 | return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) > 0); |
| 235 | default: // should be a 1 - sort by name ascending |
| 236 | return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) < 0); |
| 237 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 238 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | int GUIFileSelector::GetFileList(const std::string folder) |
| 242 | { |
| 243 | DIR* d; |
| 244 | struct dirent* de; |
| 245 | struct stat st; |
| 246 | |
| 247 | // Clear all data |
| 248 | mFolderList.clear(); |
| 249 | mFileList.clear(); |
| 250 | |
| 251 | d = opendir(folder.c_str()); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 252 | if (d == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 253 | LOGINFO("Unable to open '%s'\n", folder.c_str()); |
Dees_Troy | 80a11d9 | 2013-01-25 16:36:07 +0000 | [diff] [blame] | 254 | if (folder != "/" && (mShowNavFolders != 0 || mShowFiles != 0)) { |
| 255 | size_t found; |
| 256 | found = folder.find_last_of('/'); |
| 257 | if (found != string::npos) { |
| 258 | string new_folder = folder.substr(0, found); |
| 259 | |
| 260 | if (new_folder.length() < 2) |
| 261 | new_folder = "/"; |
| 262 | DataManager::SetValue(mPathVar, new_folder); |
| 263 | } |
| 264 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 265 | return -1; |
| 266 | } |
| 267 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 268 | while ((de = readdir(d)) != NULL) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 269 | FileData data; |
Ian Macdonald | e7c34e5 | 2021-01-23 18:52:28 +0100 | [diff] [blame^] | 270 | bool match = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 271 | |
| 272 | data.fileName = de->d_name; |
| 273 | if (data.fileName == ".") |
| 274 | continue; |
| 275 | if (data.fileName == ".." && folder == "/") |
| 276 | continue; |
that | c12a7f0 | 2016-01-28 20:52:16 +0100 | [diff] [blame] | 277 | |
| 278 | data.fileType = de->d_type; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 279 | |
| 280 | std::string path = folder + "/" + data.fileName; |
| 281 | stat(path.c_str(), &st); |
| 282 | data.protection = st.st_mode; |
| 283 | data.userId = st.st_uid; |
| 284 | data.groupId = st.st_gid; |
| 285 | data.fileSize = st.st_size; |
| 286 | data.lastAccess = st.st_atime; |
| 287 | data.lastModified = st.st_mtime; |
| 288 | data.lastStatChange = st.st_ctime; |
| 289 | |
Dees_Troy | 3ee47bc | 2013-01-25 21:47:37 +0000 | [diff] [blame] | 290 | if (data.fileType == DT_UNKNOWN) { |
| 291 | data.fileType = TWFunc::Get_D_Type_From_Stat(path); |
| 292 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 293 | if (data.fileType == DT_DIR) { |
that | c12a7f0 | 2016-01-28 20:52:16 +0100 | [diff] [blame] | 294 | if (mShowNavFolders || (data.fileName != "." && data.fileName != "..")) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 295 | mFolderList.push_back(data); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 296 | } else if (data.fileType == DT_REG || data.fileType == DT_LNK || data.fileType == DT_BLK) { |
mauronofrio | 38ed6a7 | 2020-01-10 16:44:28 +0100 | [diff] [blame] | 297 | #ifdef __ANDROID_API_M__ |
| 298 | std::vector<std::string> mExtnResults = android::base::Split(mExtn, ";"); |
| 299 | for (const std::string& mExtnElement : mExtnResults) |
| 300 | { |
| 301 | std::string mExtnName = android::base::Trim(mExtnElement); |
Ian Macdonald | e7c34e5 | 2021-01-23 18:52:28 +0100 | [diff] [blame^] | 302 | if (mExtnName.empty() || (data.fileName.length() >= mExtnName.length() && data.fileName.substr(data.fileName.length() - mExtnName.length()) == mExtnName)) { |
mauronofrio | 38ed6a7 | 2020-01-10 16:44:28 +0100 | [diff] [blame] | 303 | if (mExtnName == ".ab" && twadbbu::Check_ADB_Backup_File(path)) |
| 304 | mFolderList.push_back(data); |
| 305 | else |
| 306 | mFileList.push_back(data); |
Ian Macdonald | e7c34e5 | 2021-01-23 18:52:28 +0100 | [diff] [blame^] | 307 | match = true; |
| 308 | break; |
| 309 | } |
mauronofrio | 38ed6a7 | 2020-01-10 16:44:28 +0100 | [diff] [blame] | 310 | } |
Ian Macdonald | e7c34e5 | 2021-01-23 18:52:28 +0100 | [diff] [blame^] | 311 | |
| 312 | if (!match) { |
| 313 | std::vector<std::string> mPrfxResults = android::base::Split(mPrfx, ";"); |
| 314 | for (const std::string& mPrfxElement : mPrfxResults) |
| 315 | { |
| 316 | std::string mPrfxName = android::base::Trim(mPrfxElement); |
| 317 | if (!mPrfxName.empty() && data.fileName.length() >= mPrfxName.length() && data.fileName.substr(0, mPrfxName.length()) == mPrfxName) { |
| 318 | mFileList.push_back(data); |
| 319 | } |
mauronofrio | 38ed6a7 | 2020-01-10 16:44:28 +0100 | [diff] [blame] | 320 | #else //On android 5.1 we can't use android::base::Trim and Split so just use the first extension written in the list |
| 321 | std::size_t seppos = mExtn.find_first_of(";"); |
| 322 | std::string mExtnf; |
| 323 | if (seppos!=std::string::npos){ |
| 324 | mExtnf = mExtn.substr(0, seppos); |
| 325 | } else { |
Ian Macdonald | e7c34e5 | 2021-01-23 18:52:28 +0100 | [diff] [blame^] | 326 | mExtnf = mExtn; |
mauronofrio | 38ed6a7 | 2020-01-10 16:44:28 +0100 | [diff] [blame] | 327 | } |
Ian Macdonald | e7c34e5 | 2021-01-23 18:52:28 +0100 | [diff] [blame^] | 328 | if (mExtnf.empty() || (data.fileName.length() >= mExtnf.length() && data.fileName.substr(data.fileName.length() - mExtnf.length()) == mExtnf)) { |
mauronofrio | 38ed6a7 | 2020-01-10 16:44:28 +0100 | [diff] [blame] | 329 | if (mExtnf == ".ab" && twadbbu::Check_ADB_Backup_File(path)) |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 330 | mFolderList.push_back(data); |
| 331 | else |
| 332 | mFileList.push_back(data); |
Ian Macdonald | e7c34e5 | 2021-01-23 18:52:28 +0100 | [diff] [blame^] | 333 | match = true; |
| 334 | } |
| 335 | |
| 336 | if (!match) { |
| 337 | std::size_t seppos = mPrfx.find_first_of(";"); |
| 338 | std::string mPrfxf; |
| 339 | if (seppos!=std::string::npos){ |
| 340 | mPrfxf = mPrfx.substr(0, seppos); |
| 341 | } else { |
| 342 | mPrfxf = mPrfx; |
| 343 | } |
| 344 | if (!mPrfxf.empty() && data.fileName.length() >= mPrfxf.length() && data.fileName.substr(0, mPrfxf.length()) == mPrfxf) { |
| 345 | mFileList.push_back(data); |
mauronofrio | 38ed6a7 | 2020-01-10 16:44:28 +0100 | [diff] [blame] | 346 | #endif |
Ian Macdonald | e7c34e5 | 2021-01-23 18:52:28 +0100 | [diff] [blame^] | 347 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | } |
| 351 | closedir(d); |
| 352 | |
| 353 | std::sort(mFolderList.begin(), mFolderList.end(), fileSort); |
| 354 | std::sort(mFileList.begin(), mFileList.end(), fileSort); |
Dees_Troy | c0583f5 | 2013-02-28 11:19:57 -0600 | [diff] [blame] | 355 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | void GUIFileSelector::SetPageFocus(int inFocus) |
| 360 | { |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 361 | GUIScrollList::SetPageFocus(inFocus); |
| 362 | if (inFocus) { |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 363 | std::string value; |
| 364 | DataManager::GetValue(mPathVar, value); |
| 365 | if (value.empty()) |
| 366 | DataManager::SetValue(mPathVar, mPathDefault); |
Dees_Troy | c0583f5 | 2013-02-28 11:19:57 -0600 | [diff] [blame] | 367 | updateFileList = true; |
Dees_Troy | c0583f5 | 2013-02-28 11:19:57 -0600 | [diff] [blame] | 368 | mUpdate = 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 369 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 370 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 371 | |
| 372 | size_t GUIFileSelector::GetItemCount() |
| 373 | { |
| 374 | size_t folderSize = mShowFolders ? mFolderList.size() : 0; |
| 375 | size_t fileSize = mShowFiles ? mFileList.size() : 0; |
| 376 | return folderSize + fileSize; |
| 377 | } |
| 378 | |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame] | 379 | void GUIFileSelector::RenderItem(size_t itemindex, int yPos, bool selected) |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 380 | { |
| 381 | size_t folderSize = mShowFolders ? mFolderList.size() : 0; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 382 | |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame] | 383 | ImageResource* icon; |
| 384 | std::string text; |
| 385 | |
| 386 | if (itemindex < folderSize) { |
| 387 | text = mFolderList.at(itemindex).fileName; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 388 | icon = mFolderIcon; |
that | c12a7f0 | 2016-01-28 20:52:16 +0100 | [diff] [blame] | 389 | if (text == "..") |
| 390 | text = gui_lookup("up_a_level", "(Up A Level)"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 391 | } else { |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame] | 392 | text = mFileList.at(itemindex - folderSize).fileName; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 393 | icon = mFileIcon; |
| 394 | } |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame] | 395 | |
| 396 | RenderStdItem(yPos, selected, icon, text.c_str()); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | void GUIFileSelector::NotifySelect(size_t item_selected) |
| 400 | { |
| 401 | size_t folderSize = mShowFolders ? mFolderList.size() : 0; |
| 402 | size_t fileSize = mShowFiles ? mFileList.size() : 0; |
| 403 | |
| 404 | if (item_selected < folderSize + fileSize) { |
| 405 | // We've selected an item! |
| 406 | std::string str; |
| 407 | if (item_selected < folderSize) { |
| 408 | std::string cwd; |
| 409 | |
| 410 | str = mFolderList.at(item_selected).fileName; |
| 411 | if (mSelection != "0") |
| 412 | DataManager::SetValue(mSelection, str); |
| 413 | DataManager::GetValue(mPathVar, cwd); |
| 414 | |
| 415 | // Ignore requests to do nothing |
| 416 | if (str == ".") return; |
that | c12a7f0 | 2016-01-28 20:52:16 +0100 | [diff] [blame] | 417 | if (str == "..") { |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 418 | if (cwd != "/") { |
| 419 | size_t found; |
| 420 | found = cwd.find_last_of('/'); |
| 421 | cwd = cwd.substr(0,found); |
| 422 | |
| 423 | if (cwd.length() < 2) cwd = "/"; |
| 424 | } |
| 425 | } else { |
| 426 | // Add a slash if we're not the root folder |
| 427 | if (cwd != "/") cwd += "/"; |
| 428 | cwd += str; |
| 429 | } |
| 430 | |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 431 | if (mShowNavFolders == 0 && (mShowFiles == 0 || mExtn == ".ab")) { |
| 432 | // this is probably the restore list and we need to save chosen location to mVariable instead of mPathVar |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 433 | DataManager::SetValue(mVariable, cwd); |
| 434 | } else { |
| 435 | // We are changing paths, so we need to set mPathVar |
| 436 | DataManager::SetValue(mPathVar, cwd); |
| 437 | } |
| 438 | } else if (!mVariable.empty()) { |
| 439 | str = mFileList.at(item_selected - folderSize).fileName; |
| 440 | if (mSelection != "0") |
| 441 | DataManager::SetValue(mSelection, str); |
| 442 | |
| 443 | std::string cwd; |
| 444 | DataManager::GetValue(mPathVar, cwd); |
| 445 | if (cwd != "/") |
| 446 | cwd += "/"; |
| 447 | DataManager::SetValue(mVariable, cwd + str); |
| 448 | } |
| 449 | } |
| 450 | mUpdate = 1; |
| 451 | } |