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