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