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