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> |
| 23 | |
| 24 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 25 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 26 | } |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 27 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 28 | |
| 29 | #include "rapidxml.hpp" |
| 30 | #include "objects.hpp" |
| 31 | #include "../data.hpp" |
Dees_Troy | 3ee47bc | 2013-01-25 21:47:37 +0000 | [diff] [blame] | 32 | #include "../twrp-functions.hpp" |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 33 | #include "../adbbu/libtwadbbu.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 34 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 35 | int GUIFileSelector::mSortOrder = 0; |
| 36 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 37 | GUIFileSelector::GUIFileSelector(xml_node<>* node) : GUIScrollList(node) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 38 | { |
| 39 | xml_attribute<>* attr; |
| 40 | xml_node<>* child; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 41 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 42 | mFolderIcon = mFileIcon = NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 43 | mShowFolders = mShowFiles = mShowNavFolders = 1; |
| 44 | mUpdate = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 45 | mPathVar = "cwd"; |
Dees_Troy | c0583f5 | 2013-02-28 11:19:57 -0600 | [diff] [blame] | 46 | updateFileList = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 47 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 48 | // Load filter for filtering files (e.g. *.zip for only zips) |
that | 72b90e3 | 2015-02-23 22:57:14 +0100 | [diff] [blame] | 49 | child = FindNode(node, "filter"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 50 | if (child) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 51 | 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 |
that | 72b90e3 | 2015-02-23 22:57:14 +0100 | [diff] [blame] | 66 | child = FindNode(node, "path"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 67 | if (child) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 68 | attr = child->first_attribute("name"); |
| 69 | if (attr) |
| 70 | mPathVar = attr->value(); |
| 71 | attr = child->first_attribute("default"); |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 72 | if (attr) { |
| 73 | mPathDefault = attr->value(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 74 | DataManager::SetValue(mPathVar, attr->value()); |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 75 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // Handle the result variable |
that | 72b90e3 | 2015-02-23 22:57:14 +0100 | [diff] [blame] | 79 | child = FindNode(node, "data"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 80 | if (child) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 81 | 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 |
that | 72b90e3 | 2015-02-23 22:57:14 +0100 | [diff] [blame] | 90 | child = FindNode(node, "sort"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 91 | if (child) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 92 | 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 |
that | 72b90e3 | 2015-02-23 22:57:14 +0100 | [diff] [blame] | 103 | child = FindNode(node, "selection"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 104 | if (child && (attr = child->first_attribute("name"))) |
| 105 | mSelection = attr->value(); |
| 106 | else |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 107 | mSelection = "0"; |
| 108 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 109 | // Get folder and file icons if present |
that | 72b90e3 | 2015-02-23 22:57:14 +0100 | [diff] [blame] | 110 | child = FindNode(node, "icon"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 111 | if (child) { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 112 | mFolderIcon = LoadAttrImage(child, "folder"); |
| 113 | mFileIcon = LoadAttrImage(child, "file"); |
Vojtech Bocek | 7cc278b | 2013-02-24 01:40:19 +0100 | [diff] [blame] | 114 | } |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 115 | int iconWidth = std::max(mFolderIcon->GetWidth(), mFileIcon->GetWidth()); |
| 116 | int iconHeight = std::max(mFolderIcon->GetHeight(), mFileIcon->GetHeight()); |
| 117 | SetMaxIconSize(iconWidth, iconHeight); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 118 | |
| 119 | // Fetch the file/folder list |
| 120 | std::string value; |
| 121 | DataManager::GetValue(mPathVar, value); |
Dees_Troy | 80a11d9 | 2013-01-25 16:36:07 +0000 | [diff] [blame] | 122 | GetFileList(value); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | GUIFileSelector::~GUIFileSelector() |
| 126 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | int GUIFileSelector::Update(void) |
| 130 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 131 | if (!isConditionTrue()) |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 132 | return 0; |
| 133 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 134 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 142 | mUpdate = 1; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 143 | } else |
| 144 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 145 | } |
| 146 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 147 | if (mUpdate) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 148 | mUpdate = 0; |
| 149 | if (Render() == 0) |
| 150 | return 2; |
| 151 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 152 | return 0; |
| 153 | } |
| 154 | |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 155 | int GUIFileSelector::NotifyVarChange(const std::string& varName, const std::string& value) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 156 | { |
that | fa30aca | 2015-02-13 01:22:22 +0100 | [diff] [blame] | 157 | GUIScrollList::NotifyVarChange(varName, value); |
| 158 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 159 | if (!isConditionTrue()) |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 160 | return 0; |
| 161 | |
Dees_Troy | 146d72a | 2013-03-11 17:46:19 +0000 | [diff] [blame] | 162 | if (varName.empty()) { |
| 163 | // Always clear the data variable so we know to use it |
| 164 | DataManager::SetValue(mVariable, ""); |
| 165 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 166 | if (varName == mPathVar || varName == mSortVariable) { |
Dees_Troy | 4622cf9 | 2013-03-01 15:29:36 -0600 | [diff] [blame] | 167 | if (varName == mSortVariable) { |
| 168 | DataManager::GetValue(mSortVariable, mSortOrder); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 169 | } else { |
| 170 | // Reset the list to the top |
| 171 | SetVisibleListLocation(0); |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 172 | if (value.empty()) |
| 173 | DataManager::SetValue(mPathVar, mPathDefault); |
Dees_Troy | c0583f5 | 2013-02-28 11:19:57 -0600 | [diff] [blame] | 174 | } |
| 175 | updateFileList = true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 176 | mUpdate = 1; |
| 177 | return 0; |
| 178 | } |
| 179 | return 0; |
| 180 | } |
| 181 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 182 | bool GUIFileSelector::fileSort(FileData d1, FileData d2) |
| 183 | { |
| 184 | if (d1.fileName == ".") |
| 185 | return -1; |
| 186 | if (d2.fileName == ".") |
| 187 | return 0; |
that | c12a7f0 | 2016-01-28 20:52:16 +0100 | [diff] [blame] | 188 | if (d1.fileName == "..") |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 189 | return -1; |
that | c12a7f0 | 2016-01-28 20:52:16 +0100 | [diff] [blame] | 190 | if (d2.fileName == "..") |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 191 | return 0; |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 192 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 193 | 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_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 197 | return d1.fileSize < d2.fileSize; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 198 | 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_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 201 | return d1.fileSize > d2.fileSize; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 202 | 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_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 205 | return d1.lastModified < d2.lastModified; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 206 | 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_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 209 | return d1.lastModified > d2.lastModified; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 210 | 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 Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 215 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | int 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 Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 229 | if (d == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 230 | LOGINFO("Unable to open '%s'\n", folder.c_str()); |
Dees_Troy | 80a11d9 | 2013-01-25 16:36:07 +0000 | [diff] [blame] | 231 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 242 | return -1; |
| 243 | } |
| 244 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 245 | while ((de = readdir(d)) != NULL) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 246 | FileData data; |
| 247 | |
| 248 | data.fileName = de->d_name; |
| 249 | if (data.fileName == ".") |
| 250 | continue; |
| 251 | if (data.fileName == ".." && folder == "/") |
| 252 | continue; |
that | c12a7f0 | 2016-01-28 20:52:16 +0100 | [diff] [blame] | 253 | |
| 254 | data.fileType = de->d_type; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 255 | |
| 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_Troy | 3ee47bc | 2013-01-25 21:47:37 +0000 | [diff] [blame] | 266 | if (data.fileType == DT_UNKNOWN) { |
| 267 | data.fileType = TWFunc::Get_D_Type_From_Stat(path); |
| 268 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 269 | if (data.fileType == DT_DIR) { |
that | c12a7f0 | 2016-01-28 20:52:16 +0100 | [diff] [blame] | 270 | if (mShowNavFolders || (data.fileName != "." && data.fileName != "..")) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 271 | mFolderList.push_back(data); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 272 | } 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 bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 274 | if (mExtn == ".ab" && twadbbu::Check_ADB_Backup_File(path)) |
| 275 | mFolderList.push_back(data); |
| 276 | else |
| 277 | mFileList.push_back(data); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | } |
| 281 | closedir(d); |
| 282 | |
| 283 | std::sort(mFolderList.begin(), mFolderList.end(), fileSort); |
| 284 | std::sort(mFileList.begin(), mFileList.end(), fileSort); |
Dees_Troy | c0583f5 | 2013-02-28 11:19:57 -0600 | [diff] [blame] | 285 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | void GUIFileSelector::SetPageFocus(int inFocus) |
| 290 | { |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 291 | GUIScrollList::SetPageFocus(inFocus); |
| 292 | if (inFocus) { |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 293 | std::string value; |
| 294 | DataManager::GetValue(mPathVar, value); |
| 295 | if (value.empty()) |
| 296 | DataManager::SetValue(mPathVar, mPathDefault); |
Dees_Troy | c0583f5 | 2013-02-28 11:19:57 -0600 | [diff] [blame] | 297 | updateFileList = true; |
Dees_Troy | c0583f5 | 2013-02-28 11:19:57 -0600 | [diff] [blame] | 298 | mUpdate = 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 299 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 300 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 301 | |
| 302 | size_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 | |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame] | 309 | void GUIFileSelector::RenderItem(size_t itemindex, int yPos, bool selected) |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 310 | { |
| 311 | size_t folderSize = mShowFolders ? mFolderList.size() : 0; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 312 | |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame] | 313 | ImageResource* icon; |
| 314 | std::string text; |
| 315 | |
| 316 | if (itemindex < folderSize) { |
| 317 | text = mFolderList.at(itemindex).fileName; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 318 | icon = mFolderIcon; |
that | c12a7f0 | 2016-01-28 20:52:16 +0100 | [diff] [blame] | 319 | if (text == "..") |
| 320 | text = gui_lookup("up_a_level", "(Up A Level)"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 321 | } else { |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame] | 322 | text = mFileList.at(itemindex - folderSize).fileName; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 323 | icon = mFileIcon; |
| 324 | } |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame] | 325 | |
| 326 | RenderStdItem(yPos, selected, icon, text.c_str()); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | void 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; |
that | c12a7f0 | 2016-01-28 20:52:16 +0100 | [diff] [blame] | 347 | if (str == "..") { |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 348 | 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 bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 361 | 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 Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 363 | 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 | } |