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 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 27 | } |
| 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 | |
| 34 | #define TW_FILESELECTOR_UP_A_LEVEL "(Up A Level)" |
| 35 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 36 | int GUIFileSelector::mSortOrder = 0; |
| 37 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 38 | GUIFileSelector::GUIFileSelector(xml_node<>* node) : GUIScrollList(node) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 39 | { |
| 40 | xml_attribute<>* attr; |
| 41 | xml_node<>* child; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 42 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 43 | mFolderIcon = mFileIcon = NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 44 | mShowFolders = mShowFiles = mShowNavFolders = 1; |
| 45 | mUpdate = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 46 | mPathVar = "cwd"; |
Dees_Troy | c0583f5 | 2013-02-28 11:19:57 -0600 | [diff] [blame] | 47 | updateFileList = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 48 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 49 | // Load filter for filtering files (e.g. *.zip for only zips) |
that | 72b90e3 | 2015-02-23 22:57:14 +0100 | [diff] [blame^] | 50 | child = FindNode(node, "filter"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 51 | if (child) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 52 | 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 |
that | 72b90e3 | 2015-02-23 22:57:14 +0100 | [diff] [blame^] | 67 | child = FindNode(node, "path"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 68 | if (child) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 69 | attr = child->first_attribute("name"); |
| 70 | if (attr) |
| 71 | mPathVar = attr->value(); |
| 72 | attr = child->first_attribute("default"); |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 73 | if (attr) { |
| 74 | mPathDefault = attr->value(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 75 | DataManager::SetValue(mPathVar, attr->value()); |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 76 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // Handle the result variable |
that | 72b90e3 | 2015-02-23 22:57:14 +0100 | [diff] [blame^] | 80 | child = FindNode(node, "data"); |
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 | 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 |
that | 72b90e3 | 2015-02-23 22:57:14 +0100 | [diff] [blame^] | 91 | child = FindNode(node, "sort"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 92 | if (child) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 93 | 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 |
that | 72b90e3 | 2015-02-23 22:57:14 +0100 | [diff] [blame^] | 104 | child = FindNode(node, "selection"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 105 | if (child && (attr = child->first_attribute("name"))) |
| 106 | mSelection = attr->value(); |
| 107 | else |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 108 | mSelection = "0"; |
| 109 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 110 | // Get folder and file icons if present |
that | 72b90e3 | 2015-02-23 22:57:14 +0100 | [diff] [blame^] | 111 | child = FindNode(node, "icon"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 112 | if (child) { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 113 | mFolderIcon = LoadAttrImage(child, "folder"); |
| 114 | mFileIcon = LoadAttrImage(child, "file"); |
Vojtech Bocek | 7cc278b | 2013-02-24 01:40:19 +0100 | [diff] [blame] | 115 | } |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 116 | int iconWidth = std::max(mFolderIcon->GetWidth(), mFileIcon->GetWidth()); |
| 117 | int iconHeight = std::max(mFolderIcon->GetHeight(), mFileIcon->GetHeight()); |
| 118 | SetMaxIconSize(iconWidth, iconHeight); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 119 | |
| 120 | // Fetch the file/folder list |
| 121 | std::string value; |
| 122 | DataManager::GetValue(mPathVar, value); |
Dees_Troy | 80a11d9 | 2013-01-25 16:36:07 +0000 | [diff] [blame] | 123 | GetFileList(value); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | GUIFileSelector::~GUIFileSelector() |
| 127 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | int GUIFileSelector::Update(void) |
| 131 | { |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 132 | if(!isConditionTrue()) |
| 133 | return 0; |
| 134 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 135 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 143 | mUpdate = 1; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 144 | } else |
| 145 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 146 | } |
| 147 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 148 | if (mUpdate) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 149 | mUpdate = 0; |
| 150 | if (Render() == 0) |
| 151 | return 2; |
| 152 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 153 | return 0; |
| 154 | } |
| 155 | |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 156 | int GUIFileSelector::NotifyVarChange(const std::string& varName, const std::string& value) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 157 | { |
that | fa30aca | 2015-02-13 01:22:22 +0100 | [diff] [blame] | 158 | GUIScrollList::NotifyVarChange(varName, value); |
| 159 | |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 160 | if(!isConditionTrue()) |
| 161 | return 0; |
| 162 | |
Dees_Troy | 146d72a | 2013-03-11 17:46:19 +0000 | [diff] [blame] | 163 | if (varName.empty()) { |
| 164 | // Always clear the data variable so we know to use it |
| 165 | DataManager::SetValue(mVariable, ""); |
| 166 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 167 | if (varName == mPathVar || varName == mSortVariable) { |
Dees_Troy | 4622cf9 | 2013-03-01 15:29:36 -0600 | [diff] [blame] | 168 | if (varName == mSortVariable) { |
| 169 | DataManager::GetValue(mSortVariable, mSortOrder); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 170 | } else { |
| 171 | // Reset the list to the top |
| 172 | SetVisibleListLocation(0); |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 173 | if (value.empty()) |
| 174 | DataManager::SetValue(mPathVar, mPathDefault); |
Dees_Troy | c0583f5 | 2013-02-28 11:19:57 -0600 | [diff] [blame] | 175 | } |
| 176 | updateFileList = true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 177 | mUpdate = 1; |
| 178 | return 0; |
| 179 | } |
| 180 | return 0; |
| 181 | } |
| 182 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 183 | bool 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 Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 193 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 194 | 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_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 198 | return d1.fileSize < d2.fileSize; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 199 | 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_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 202 | return d1.fileSize > d2.fileSize; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 203 | 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_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 206 | return d1.lastModified < d2.lastModified; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 207 | 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_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 210 | return d1.lastModified > d2.lastModified; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 211 | 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 Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 216 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | int 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 Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 230 | if (d == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 231 | LOGINFO("Unable to open '%s'\n", folder.c_str()); |
Dees_Troy | 80a11d9 | 2013-01-25 16:36:07 +0000 | [diff] [blame] | 232 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 243 | return -1; |
| 244 | } |
| 245 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 246 | while ((de = readdir(d)) != NULL) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 247 | FileData data; |
| 248 | |
| 249 | data.fileName = de->d_name; |
| 250 | if (data.fileName == ".") |
| 251 | continue; |
| 252 | if (data.fileName == ".." && folder == "/") |
| 253 | continue; |
Dees_Troy | 3ee47bc | 2013-01-25 21:47:37 +0000 | [diff] [blame] | 254 | if (data.fileName == "..") { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 255 | data.fileName = TW_FILESELECTOR_UP_A_LEVEL; |
Dees_Troy | 3ee47bc | 2013-01-25 21:47:37 +0000 | [diff] [blame] | 256 | data.fileType = DT_DIR; |
| 257 | } else { |
| 258 | data.fileType = de->d_type; |
| 259 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 260 | |
| 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_Troy | 3ee47bc | 2013-01-25 21:47:37 +0000 | [diff] [blame] | 271 | if (data.fileType == DT_UNKNOWN) { |
| 272 | data.fileType = TWFunc::Get_D_Type_From_Stat(path); |
| 273 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 274 | if (data.fileType == DT_DIR) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 275 | if (mShowNavFolders || (data.fileName != "." && data.fileName != TW_FILESELECTOR_UP_A_LEVEL)) |
| 276 | mFolderList.push_back(data); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 277 | } 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 279 | 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_Troy | c0583f5 | 2013-02-28 11:19:57 -0600 | [diff] [blame] | 287 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | void GUIFileSelector::SetPageFocus(int inFocus) |
| 292 | { |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 293 | GUIScrollList::SetPageFocus(inFocus); |
| 294 | if (inFocus) { |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 295 | std::string value; |
| 296 | DataManager::GetValue(mPathVar, value); |
| 297 | if (value.empty()) |
| 298 | DataManager::SetValue(mPathVar, mPathDefault); |
Dees_Troy | c0583f5 | 2013-02-28 11:19:57 -0600 | [diff] [blame] | 299 | updateFileList = true; |
Dees_Troy | c0583f5 | 2013-02-28 11:19:57 -0600 | [diff] [blame] | 300 | mUpdate = 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 301 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 302 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 303 | |
| 304 | size_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 | |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 311 | int GUIFileSelector::GetListItem(size_t item_index, ImageResource*& icon, std::string &text) |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 312 | { |
| 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 | |
| 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; |
| 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 | } |