Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2013 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 | */ |
| 18 | |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 19 | #include <string.h> |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 20 | #include <sys/stat.h> |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 21 | #include <dirent.h> |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 22 | |
| 23 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 24 | #include "../twcommon.h" |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 25 | #include "../minuitwrp/minui.h" |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | #include "rapidxml.hpp" |
| 29 | #include "objects.hpp" |
| 30 | #include "../data.hpp" |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 31 | #include "../partitions.hpp" |
| 32 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 33 | GUIPartitionList::GUIPartitionList(xml_node<>* node) : GUIScrollList(node) |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 34 | { |
| 35 | xml_attribute<>* attr; |
| 36 | xml_node<>* child; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 37 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 38 | int mIconWidth = 0, mIconHeight = 0, mSelectedIconHeight = 0, mSelectedIconWidth = 0, mUnselectedIconHeight = 0, mUnselectedIconWidth = 0; |
| 39 | mIconSelected = mIconUnselected = NULL; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 40 | mUpdate = 0; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 41 | updateList = false; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 42 | |
| 43 | child = node->first_node("icon"); |
| 44 | if (child) |
| 45 | { |
| 46 | attr = child->first_attribute("selected"); |
| 47 | if (attr) |
| 48 | mIconSelected = PageManager::FindResource(attr->value()); |
| 49 | attr = child->first_attribute("unselected"); |
| 50 | if (attr) |
| 51 | mIconUnselected = PageManager::FindResource(attr->value()); |
| 52 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 53 | |
| 54 | // Handle the result variable |
| 55 | child = node->first_node("data"); |
| 56 | if (child) |
| 57 | { |
| 58 | attr = child->first_attribute("name"); |
| 59 | if (attr) |
| 60 | mVariable = attr->value(); |
| 61 | attr = child->first_attribute("selectedlist"); |
| 62 | if (attr) |
| 63 | selectedList = attr->value(); |
| 64 | } |
| 65 | |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 66 | if (mIconSelected && mIconSelected->GetResource()) |
| 67 | { |
| 68 | mSelectedIconWidth = gr_get_width(mIconSelected->GetResource()); |
| 69 | mSelectedIconHeight = gr_get_height(mIconSelected->GetResource()); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 70 | if (mSelectedIconHeight > mIconHeight) |
| 71 | mIconHeight = mSelectedIconHeight; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 72 | mIconWidth = mSelectedIconWidth; |
| 73 | } |
| 74 | |
| 75 | if (mIconUnselected && mIconUnselected->GetResource()) |
| 76 | { |
| 77 | mUnselectedIconWidth = gr_get_width(mIconUnselected->GetResource()); |
| 78 | mUnselectedIconHeight = gr_get_height(mIconUnselected->GetResource()); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 79 | if (mUnselectedIconHeight > mIconHeight) |
| 80 | mIconHeight = mUnselectedIconHeight; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 81 | if (mUnselectedIconWidth > mIconWidth) |
| 82 | mIconWidth = mUnselectedIconWidth; |
| 83 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 84 | SetMaxIconSize(mIconWidth, mIconHeight); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 85 | |
| 86 | child = node->first_node("listtype"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 87 | if (child && (attr = child->first_attribute("name"))) { |
| 88 | ListType = attr->value(); |
| 89 | updateList = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 90 | } else { |
| 91 | mList.clear(); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 92 | LOGERR("No partition listtype specified for partitionlist GUI element\n"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 93 | return; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | GUIPartitionList::~GUIPartitionList() |
| 98 | { |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 99 | delete mIconSelected; |
| 100 | delete mIconUnselected; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | int GUIPartitionList::Update(void) |
| 104 | { |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 105 | if(!isConditionTrue()) |
| 106 | return 0; |
| 107 | |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 108 | // Check for changes in mount points if the list type is mount and update the list and render if needed |
| 109 | if (ListType == "mount") { |
| 110 | int listSize = mList.size(); |
| 111 | for (int i = 0; i < listSize; i++) { |
| 112 | if (PartitionManager.Is_Mounted_By_Path(mList.at(i).Mount_Point) && !mList.at(i).selected) { |
| 113 | mList.at(i).selected = 1; |
| 114 | mUpdate = 1; |
| 115 | } else if (!PartitionManager.Is_Mounted_By_Path(mList.at(i).Mount_Point) && mList.at(i).selected) { |
| 116 | mList.at(i).selected = 0; |
| 117 | mUpdate = 1; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 122 | GUIScrollList::Update(); |
| 123 | |
| 124 | if (updateList) { |
| 125 | int listSize = 0; |
| 126 | |
| 127 | // Completely update the list if needed -- Used primarily for |
| 128 | // restore as the list for restore will change depending on what |
| 129 | // partitions were backed up |
| 130 | mList.clear(); |
| 131 | PartitionManager.Get_Partition_List(ListType, &mList); |
| 132 | SetVisibleListLocation(0); |
| 133 | updateList = false; |
| 134 | mUpdate = 1; |
| 135 | if (ListType == "backup") |
| 136 | MatchList(); |
| 137 | } |
| 138 | |
| 139 | if (mUpdate) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 140 | mUpdate = 0; |
| 141 | if (Render() == 0) |
| 142 | return 2; |
| 143 | } |
| 144 | |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 145 | return 0; |
| 146 | } |
| 147 | |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 148 | int GUIPartitionList::NotifyVarChange(const std::string& varName, const std::string& value) |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 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::NotifyVarChange(varName, value); |
| 154 | |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 155 | if (varName == mVariable && !mUpdate) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 156 | { |
| 157 | if (ListType == "storage") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 158 | currentValue = value; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 159 | SetStoragePosition(); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 160 | } else if (ListType == "backup") { |
| 161 | MatchList(); |
| 162 | } else if (ListType == "restore") { |
| 163 | updateList = true; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 164 | SetVisibleListLocation(0); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | mUpdate = 1; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 168 | return 0; |
| 169 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 170 | return 0; |
| 171 | } |
| 172 | |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 173 | void GUIPartitionList::SetPageFocus(int inFocus) |
| 174 | { |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 175 | GUIScrollList::SetPageFocus(inFocus); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 176 | if (inFocus) { |
| 177 | if (ListType == "storage") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 178 | DataManager::GetValue(mVariable, currentValue); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 179 | SetStoragePosition(); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 180 | } |
| 181 | updateList = true; |
| 182 | mUpdate = 1; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | void GUIPartitionList::MatchList(void) { |
| 187 | int i, listSize = mList.size(); |
| 188 | string variablelist, searchvalue; |
| 189 | size_t pos; |
| 190 | |
| 191 | DataManager::GetValue(mVariable, variablelist); |
| 192 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 193 | for (i = 0; i < listSize; i++) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 194 | searchvalue = mList.at(i).Mount_Point + ";"; |
| 195 | pos = variablelist.find(searchvalue); |
| 196 | if (pos != string::npos) { |
| 197 | mList.at(i).selected = 1; |
| 198 | } else { |
| 199 | mList.at(i).selected = 0; |
| 200 | } |
| 201 | } |
| 202 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 203 | |
| 204 | void GUIPartitionList::SetStoragePosition() { |
| 205 | int listSize = mList.size(); |
| 206 | |
| 207 | for (int i = 0; i < listSize; i++) { |
| 208 | if (mList.at(i).Mount_Point == currentValue) { |
| 209 | mList.at(i).selected = 1; |
| 210 | SetVisibleListLocation(i); |
| 211 | } else { |
| 212 | mList.at(i).selected = 0; |
| 213 | SetVisibleListLocation(0); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | size_t GUIPartitionList::GetItemCount() |
| 219 | { |
| 220 | return mList.size(); |
| 221 | } |
| 222 | |
| 223 | int GUIPartitionList::GetListItem(size_t item_index, Resource*& icon, std::string &text) |
| 224 | { |
| 225 | text = mList.at(item_index).Display_Name; |
| 226 | if (mList.at(item_index).selected) |
| 227 | icon = mIconSelected; |
| 228 | else |
| 229 | icon = mIconUnselected; |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | void GUIPartitionList::NotifySelect(size_t item_selected) |
| 234 | { |
| 235 | if (item_selected < mList.size()) { |
| 236 | int listSize = mList.size(); |
| 237 | if (ListType == "mount") { |
| 238 | if (!mList.at(item_selected).selected) { |
| 239 | if (PartitionManager.Mount_By_Path(mList.at(item_selected).Mount_Point, true)) { |
| 240 | mList.at(item_selected).selected = 1; |
| 241 | PartitionManager.Add_MTP_Storage(mList.at(item_selected).Mount_Point); |
| 242 | mUpdate = 1; |
| 243 | } |
| 244 | } else { |
| 245 | if (PartitionManager.UnMount_By_Path(mList.at(item_selected).Mount_Point, true)) { |
| 246 | mList.at(item_selected).selected = 0; |
| 247 | mUpdate = 1; |
| 248 | } |
| 249 | } |
| 250 | } else if (!mVariable.empty()) { |
| 251 | if (ListType == "storage") { |
| 252 | int i; |
| 253 | std::string str = mList.at(item_selected).Mount_Point; |
| 254 | bool update_size = false; |
| 255 | TWPartition* Part = PartitionManager.Find_Partition_By_Path(str); |
| 256 | if (Part == NULL) { |
| 257 | LOGERR("Unable to locate partition for '%s'\n", str.c_str()); |
| 258 | return; |
| 259 | } |
| 260 | if (!Part->Is_Mounted() && Part->Removable) |
| 261 | update_size = true; |
| 262 | if (!Part->Mount(true)) { |
| 263 | // Do Nothing |
| 264 | } else if (update_size && !Part->Update_Size(true)) { |
| 265 | // Do Nothing |
| 266 | } else { |
| 267 | for (i=0; i<listSize; i++) |
| 268 | mList.at(i).selected = 0; |
| 269 | |
| 270 | if (update_size) { |
| 271 | char free_space[255]; |
| 272 | sprintf(free_space, "%llu", Part->Free / 1024 / 1024); |
| 273 | mList.at(item_selected).Display_Name = Part->Storage_Name + " ("; |
| 274 | mList.at(item_selected).Display_Name += free_space; |
| 275 | mList.at(item_selected).Display_Name += "MB)"; |
| 276 | } |
| 277 | mList.at(item_selected).selected = 1; |
| 278 | mUpdate = 1; |
| 279 | |
| 280 | DataManager::SetValue(mVariable, str); |
| 281 | } |
| 282 | } else { |
| 283 | if (ListType == "flashimg") { // only one item can be selected for flashing images |
| 284 | for (int i=0; i<listSize; i++) |
| 285 | mList.at(i).selected = 0; |
| 286 | } |
| 287 | if (mList.at(item_selected).selected) |
| 288 | mList.at(item_selected).selected = 0; |
| 289 | else |
| 290 | mList.at(item_selected).selected = 1; |
| 291 | |
| 292 | int i; |
| 293 | string variablelist; |
| 294 | for (i=0; i<listSize; i++) { |
| 295 | if (mList.at(i).selected) { |
| 296 | variablelist += mList.at(i).Mount_Point + ";"; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | mUpdate = 1; |
| 301 | if (selectedList.empty()) |
| 302 | DataManager::SetValue(mVariable, variablelist); |
| 303 | else |
| 304 | DataManager::SetValue(selectedList, variablelist); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | } |