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 | { |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 144 | delete mFileIcon; |
| 145 | delete mFolderIcon; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | int GUIFileSelector::Update(void) |
| 149 | { |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 150 | if(!isConditionTrue()) |
| 151 | return 0; |
| 152 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 153 | GUIScrollList::Update(); |
| 154 | |
| 155 | // Update the file list if needed |
| 156 | if (updateFileList) { |
| 157 | string value; |
| 158 | DataManager::GetValue(mPathVar, value); |
| 159 | if (GetFileList(value) == 0) { |
| 160 | updateFileList = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 161 | mUpdate = 1; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 162 | } else |
| 163 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 164 | } |
| 165 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 166 | if (mUpdate) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 167 | mUpdate = 0; |
| 168 | if (Render() == 0) |
| 169 | return 2; |
| 170 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 174 | int GUIFileSelector::NotifyVarChange(const std::string& varName, const std::string& value) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 175 | { |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 176 | if(!isConditionTrue()) |
| 177 | return 0; |
| 178 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 179 | GUIScrollList::NotifyVarChange(varName, value); |
| 180 | |
Dees_Troy | 146d72a | 2013-03-11 17:46:19 +0000 | [diff] [blame] | 181 | if (varName.empty()) { |
| 182 | // Always clear the data variable so we know to use it |
| 183 | DataManager::SetValue(mVariable, ""); |
| 184 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 185 | if (varName == mPathVar || varName == mSortVariable) { |
Dees_Troy | 4622cf9 | 2013-03-01 15:29:36 -0600 | [diff] [blame] | 186 | if (varName == mSortVariable) { |
| 187 | DataManager::GetValue(mSortVariable, mSortOrder); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 188 | } else { |
| 189 | // Reset the list to the top |
| 190 | SetVisibleListLocation(0); |
Dees_Troy | c0583f5 | 2013-02-28 11:19:57 -0600 | [diff] [blame] | 191 | } |
| 192 | updateFileList = true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 193 | mUpdate = 1; |
| 194 | return 0; |
| 195 | } |
| 196 | return 0; |
| 197 | } |
| 198 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 199 | bool GUIFileSelector::fileSort(FileData d1, FileData d2) |
| 200 | { |
| 201 | if (d1.fileName == ".") |
| 202 | return -1; |
| 203 | if (d2.fileName == ".") |
| 204 | return 0; |
| 205 | if (d1.fileName == TW_FILESELECTOR_UP_A_LEVEL) |
| 206 | return -1; |
| 207 | if (d2.fileName == TW_FILESELECTOR_UP_A_LEVEL) |
| 208 | return 0; |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 209 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 210 | switch (mSortOrder) { |
| 211 | case 3: // by size largest first |
| 212 | 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 |
| 213 | return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) < 0); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 214 | return d1.fileSize < d2.fileSize; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 215 | case -3: // by size smallest first |
| 216 | 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 |
| 217 | return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) > 0); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 218 | return d1.fileSize > d2.fileSize; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 219 | case 2: // by last modified date newest first |
| 220 | if (d1.lastModified == d2.lastModified) |
| 221 | return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) < 0); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 222 | return d1.lastModified < d2.lastModified; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 223 | case -2: // by date oldest first |
| 224 | if (d1.lastModified == d2.lastModified) |
| 225 | return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) > 0); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 226 | return d1.lastModified > d2.lastModified; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 227 | case -1: // by name descending |
| 228 | return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) > 0); |
| 229 | default: // should be a 1 - sort by name ascending |
| 230 | return (strcasecmp(d1.fileName.c_str(), d2.fileName.c_str()) < 0); |
| 231 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 232 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | int GUIFileSelector::GetFileList(const std::string folder) |
| 236 | { |
| 237 | DIR* d; |
| 238 | struct dirent* de; |
| 239 | struct stat st; |
| 240 | |
| 241 | // Clear all data |
| 242 | mFolderList.clear(); |
| 243 | mFileList.clear(); |
| 244 | |
| 245 | d = opendir(folder.c_str()); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 246 | if (d == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 247 | LOGINFO("Unable to open '%s'\n", folder.c_str()); |
Dees_Troy | 80a11d9 | 2013-01-25 16:36:07 +0000 | [diff] [blame] | 248 | if (folder != "/" && (mShowNavFolders != 0 || mShowFiles != 0)) { |
| 249 | size_t found; |
| 250 | found = folder.find_last_of('/'); |
| 251 | if (found != string::npos) { |
| 252 | string new_folder = folder.substr(0, found); |
| 253 | |
| 254 | if (new_folder.length() < 2) |
| 255 | new_folder = "/"; |
| 256 | DataManager::SetValue(mPathVar, new_folder); |
| 257 | } |
| 258 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 259 | return -1; |
| 260 | } |
| 261 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 262 | while ((de = readdir(d)) != NULL) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 263 | FileData data; |
| 264 | |
| 265 | data.fileName = de->d_name; |
| 266 | if (data.fileName == ".") |
| 267 | continue; |
| 268 | if (data.fileName == ".." && folder == "/") |
| 269 | continue; |
Dees_Troy | 3ee47bc | 2013-01-25 21:47:37 +0000 | [diff] [blame] | 270 | if (data.fileName == "..") { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 271 | data.fileName = TW_FILESELECTOR_UP_A_LEVEL; |
Dees_Troy | 3ee47bc | 2013-01-25 21:47:37 +0000 | [diff] [blame] | 272 | data.fileType = DT_DIR; |
| 273 | } else { |
| 274 | data.fileType = de->d_type; |
| 275 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 276 | |
| 277 | std::string path = folder + "/" + data.fileName; |
| 278 | stat(path.c_str(), &st); |
| 279 | data.protection = st.st_mode; |
| 280 | data.userId = st.st_uid; |
| 281 | data.groupId = st.st_gid; |
| 282 | data.fileSize = st.st_size; |
| 283 | data.lastAccess = st.st_atime; |
| 284 | data.lastModified = st.st_mtime; |
| 285 | data.lastStatChange = st.st_ctime; |
| 286 | |
Dees_Troy | 3ee47bc | 2013-01-25 21:47:37 +0000 | [diff] [blame] | 287 | if (data.fileType == DT_UNKNOWN) { |
| 288 | data.fileType = TWFunc::Get_D_Type_From_Stat(path); |
| 289 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 290 | if (data.fileType == DT_DIR) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 291 | if (mShowNavFolders || (data.fileName != "." && data.fileName != TW_FILESELECTOR_UP_A_LEVEL)) |
| 292 | mFolderList.push_back(data); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 293 | } else if (data.fileType == DT_REG || data.fileType == DT_LNK || data.fileType == DT_BLK) { |
| 294 | 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] | 295 | mFileList.push_back(data); |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | closedir(d); |
| 300 | |
| 301 | std::sort(mFolderList.begin(), mFolderList.end(), fileSort); |
| 302 | std::sort(mFileList.begin(), mFileList.end(), fileSort); |
Dees_Troy | c0583f5 | 2013-02-28 11:19:57 -0600 | [diff] [blame] | 303 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | void GUIFileSelector::SetPageFocus(int inFocus) |
| 308 | { |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 309 | GUIScrollList::SetPageFocus(inFocus); |
| 310 | if (inFocus) { |
Dees_Troy | c0583f5 | 2013-02-28 11:19:57 -0600 | [diff] [blame] | 311 | updateFileList = true; |
Dees_Troy | c0583f5 | 2013-02-28 11:19:57 -0600 | [diff] [blame] | 312 | mUpdate = 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 313 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 314 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 315 | |
| 316 | size_t GUIFileSelector::GetItemCount() |
| 317 | { |
| 318 | size_t folderSize = mShowFolders ? mFolderList.size() : 0; |
| 319 | size_t fileSize = mShowFiles ? mFileList.size() : 0; |
| 320 | return folderSize + fileSize; |
| 321 | } |
| 322 | |
| 323 | int GUIFileSelector::GetListItem(size_t item_index, Resource*& icon, std::string &text) |
| 324 | { |
| 325 | size_t folderSize = mShowFolders ? mFolderList.size() : 0; |
| 326 | size_t fileSize = mShowFiles ? mFileList.size() : 0; |
| 327 | |
| 328 | if (item_index < folderSize) { |
| 329 | text = mFolderList.at(item_index).fileName; |
| 330 | icon = mFolderIcon; |
| 331 | } else { |
| 332 | text = mFileList.at(item_index - folderSize).fileName; |
| 333 | icon = mFileIcon; |
| 334 | } |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | void GUIFileSelector::NotifySelect(size_t item_selected) |
| 339 | { |
| 340 | size_t folderSize = mShowFolders ? mFolderList.size() : 0; |
| 341 | size_t fileSize = mShowFiles ? mFileList.size() : 0; |
| 342 | |
| 343 | if (item_selected < folderSize + fileSize) { |
| 344 | // We've selected an item! |
| 345 | std::string str; |
| 346 | if (item_selected < folderSize) { |
| 347 | std::string cwd; |
| 348 | |
| 349 | str = mFolderList.at(item_selected).fileName; |
| 350 | if (mSelection != "0") |
| 351 | DataManager::SetValue(mSelection, str); |
| 352 | DataManager::GetValue(mPathVar, cwd); |
| 353 | |
| 354 | // Ignore requests to do nothing |
| 355 | if (str == ".") return; |
| 356 | if (str == TW_FILESELECTOR_UP_A_LEVEL) { |
| 357 | if (cwd != "/") { |
| 358 | size_t found; |
| 359 | found = cwd.find_last_of('/'); |
| 360 | cwd = cwd.substr(0,found); |
| 361 | |
| 362 | if (cwd.length() < 2) cwd = "/"; |
| 363 | } |
| 364 | } else { |
| 365 | // Add a slash if we're not the root folder |
| 366 | if (cwd != "/") cwd += "/"; |
| 367 | cwd += str; |
| 368 | } |
| 369 | |
| 370 | if (mShowNavFolders == 0 && mShowFiles == 0) { |
| 371 | // nav folders and files are disabled, this is probably the restore list and we need to save chosen location to mVariable instead of mPathVar |
| 372 | DataManager::SetValue(mVariable, cwd); |
| 373 | } else { |
| 374 | // We are changing paths, so we need to set mPathVar |
| 375 | DataManager::SetValue(mPathVar, cwd); |
| 376 | } |
| 377 | } else if (!mVariable.empty()) { |
| 378 | str = mFileList.at(item_selected - folderSize).fileName; |
| 379 | if (mSelection != "0") |
| 380 | DataManager::SetValue(mSelection, str); |
| 381 | |
| 382 | std::string cwd; |
| 383 | DataManager::GetValue(mPathVar, cwd); |
| 384 | if (cwd != "/") |
| 385 | cwd += "/"; |
| 386 | DataManager::SetValue(mVariable, cwd + str); |
| 387 | } |
| 388 | } |
| 389 | mUpdate = 1; |
| 390 | } |