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 | int mIconWidth = 0, mIconHeight = 0, mFolderIconHeight = 0, mFileIconHeight = 0, mFolderIconWidth = 0, mFileIconWidth = 0; |
| 44 | mFolderIcon = mFileIcon = NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 45 | mShowFolders = mShowFiles = mShowNavFolders = 1; |
| 46 | mUpdate = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 47 | mPathVar = "cwd"; |
Dees_Troy | c0583f5 | 2013-02-28 11:19:57 -0600 | [diff] [blame] | 48 | updateFileList = false; |
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 | // Load filter for filtering files (e.g. *.zip for only zips) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 51 | child = node->first_node("filter"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 52 | if (child) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 53 | 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 Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 69 | if (child) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 70 | 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 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 |
| 90 | child = node->first_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 |
| 103 | child = node->first_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 |
| 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 Bocek | 7cc278b | 2013-02-24 01:40:19 +0100 | [diff] [blame] | 118 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 119 | if (mFolderIcon && mFolderIcon->GetResource()) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 120 | mFolderIconWidth = gr_get_width(mFolderIcon->GetResource()); |
| 121 | mFolderIconHeight = gr_get_height(mFolderIcon->GetResource()); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 122 | if (mFolderIconHeight > mIconHeight) |
| 123 | mIconHeight = mFolderIconHeight; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 124 | mIconWidth = mFolderIconWidth; |
| 125 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 126 | if (mFileIcon && mFileIcon->GetResource()) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 127 | mFileIconWidth = gr_get_width(mFileIcon->GetResource()); |
| 128 | mFileIconHeight = gr_get_height(mFileIcon->GetResource()); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 129 | if (mFileIconHeight > mIconHeight) |
| 130 | mIconHeight = mFileIconHeight; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 131 | if (mFileIconWidth > mIconWidth) |
| 132 | mIconWidth = mFileIconWidth; |
| 133 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 134 | SetMaxIconSize(mIconWidth, mIconHeight); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 135 | |
| 136 | // Fetch the file/folder list |
| 137 | std::string value; |
| 138 | DataManager::GetValue(mPathVar, value); |
Dees_Troy | 80a11d9 | 2013-01-25 16:36:07 +0000 | [diff] [blame] | 139 | GetFileList(value); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | GUIFileSelector::~GUIFileSelector() |
| 143 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | int GUIFileSelector::Update(void) |
| 147 | { |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 148 | if(!isConditionTrue()) |
| 149 | return 0; |
| 150 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 151 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 159 | mUpdate = 1; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 160 | } else |
| 161 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 162 | } |
| 163 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 164 | if (mUpdate) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 165 | mUpdate = 0; |
| 166 | if (Render() == 0) |
| 167 | return 2; |
| 168 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 169 | return 0; |
| 170 | } |
| 171 | |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 172 | int GUIFileSelector::NotifyVarChange(const std::string& varName, const std::string& value) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 173 | { |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 174 | if(!isConditionTrue()) |
| 175 | return 0; |
| 176 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 177 | GUIScrollList::NotifyVarChange(varName, value); |
| 178 | |
Dees_Troy | 146d72a | 2013-03-11 17:46:19 +0000 | [diff] [blame] | 179 | if (varName.empty()) { |
| 180 | // Always clear the data variable so we know to use it |
| 181 | DataManager::SetValue(mVariable, ""); |
| 182 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 183 | if (varName == mPathVar || varName == mSortVariable) { |
Dees_Troy | 4622cf9 | 2013-03-01 15:29:36 -0600 | [diff] [blame] | 184 | if (varName == mSortVariable) { |
| 185 | DataManager::GetValue(mSortVariable, mSortOrder); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 186 | } else { |
| 187 | // Reset the list to the top |
| 188 | SetVisibleListLocation(0); |
Dees_Troy | c0583f5 | 2013-02-28 11:19:57 -0600 | [diff] [blame] | 189 | } |
| 190 | updateFileList = true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 191 | mUpdate = 1; |
| 192 | return 0; |
| 193 | } |
| 194 | return 0; |
| 195 | } |
| 196 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 197 | bool 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 Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 207 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 208 | 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_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 212 | return d1.fileSize < d2.fileSize; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 213 | 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_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 216 | return d1.fileSize > d2.fileSize; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 217 | 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_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 220 | return d1.lastModified < d2.lastModified; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 221 | 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_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 224 | return d1.lastModified > d2.lastModified; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 225 | 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 Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 230 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | int 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 Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 244 | if (d == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 245 | LOGINFO("Unable to open '%s'\n", folder.c_str()); |
Dees_Troy | 80a11d9 | 2013-01-25 16:36:07 +0000 | [diff] [blame] | 246 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 257 | return -1; |
| 258 | } |
| 259 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 260 | while ((de = readdir(d)) != NULL) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 261 | FileData data; |
| 262 | |
| 263 | data.fileName = de->d_name; |
| 264 | if (data.fileName == ".") |
| 265 | continue; |
| 266 | if (data.fileName == ".." && folder == "/") |
| 267 | continue; |
Dees_Troy | 3ee47bc | 2013-01-25 21:47:37 +0000 | [diff] [blame] | 268 | if (data.fileName == "..") { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 269 | data.fileName = TW_FILESELECTOR_UP_A_LEVEL; |
Dees_Troy | 3ee47bc | 2013-01-25 21:47:37 +0000 | [diff] [blame] | 270 | data.fileType = DT_DIR; |
| 271 | } else { |
| 272 | data.fileType = de->d_type; |
| 273 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 274 | |
| 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_Troy | 3ee47bc | 2013-01-25 21:47:37 +0000 | [diff] [blame] | 285 | if (data.fileType == DT_UNKNOWN) { |
| 286 | data.fileType = TWFunc::Get_D_Type_From_Stat(path); |
| 287 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 288 | if (data.fileType == DT_DIR) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 289 | if (mShowNavFolders || (data.fileName != "." && data.fileName != TW_FILESELECTOR_UP_A_LEVEL)) |
| 290 | mFolderList.push_back(data); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 291 | } 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 293 | 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_Troy | c0583f5 | 2013-02-28 11:19:57 -0600 | [diff] [blame] | 301 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | void GUIFileSelector::SetPageFocus(int inFocus) |
| 306 | { |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 307 | GUIScrollList::SetPageFocus(inFocus); |
| 308 | if (inFocus) { |
Dees_Troy | c0583f5 | 2013-02-28 11:19:57 -0600 | [diff] [blame] | 309 | updateFileList = true; |
Dees_Troy | c0583f5 | 2013-02-28 11:19:57 -0600 | [diff] [blame] | 310 | mUpdate = 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 311 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 312 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 313 | |
| 314 | size_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 | |
| 321 | int 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 | |
| 336 | void 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 | } |