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 | } |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [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 | |
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 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 65 | child = FindNode(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 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 82 | if (!isConditionTrue()) |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 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) { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 102 | // Completely update the list if needed -- Used primarily for |
| 103 | // restore as the list for restore will change depending on what |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 104 | // partitions were backed up |
| 105 | mList.clear(); |
| 106 | PartitionManager.Get_Partition_List(ListType, &mList); |
| 107 | SetVisibleListLocation(0); |
| 108 | updateList = false; |
| 109 | mUpdate = 1; |
Ethan Yonker | cfd6509 | 2015-02-14 10:39:21 -0600 | [diff] [blame] | 110 | if (ListType == "backup" || ListType == "flashimg") |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 111 | MatchList(); |
| 112 | } |
| 113 | |
| 114 | if (mUpdate) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 115 | mUpdate = 0; |
| 116 | if (Render() == 0) |
| 117 | return 2; |
| 118 | } |
| 119 | |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 120 | return 0; |
| 121 | } |
| 122 | |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 123 | int GUIPartitionList::NotifyVarChange(const std::string& varName, const std::string& value) |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 124 | { |
that | fa30aca | 2015-02-13 01:22:22 +0100 | [diff] [blame] | 125 | GUIScrollList::NotifyVarChange(varName, value); |
| 126 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 127 | if (!isConditionTrue()) |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 128 | return 0; |
| 129 | |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 130 | if (varName == mVariable && !mUpdate) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 131 | { |
| 132 | if (ListType == "storage") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 133 | currentValue = value; |
Ethan Yonker | cfd6509 | 2015-02-14 10:39:21 -0600 | [diff] [blame] | 134 | SetPosition(); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 135 | } else if (ListType == "backup") { |
| 136 | MatchList(); |
| 137 | } else if (ListType == "restore") { |
| 138 | updateList = true; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 139 | SetVisibleListLocation(0); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | mUpdate = 1; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 143 | return 0; |
| 144 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 145 | return 0; |
| 146 | } |
| 147 | |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 148 | void GUIPartitionList::SetPageFocus(int inFocus) |
| 149 | { |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 150 | GUIScrollList::SetPageFocus(inFocus); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 151 | if (inFocus) { |
Ethan Yonker | cfd6509 | 2015-02-14 10:39:21 -0600 | [diff] [blame] | 152 | if (ListType == "storage" || ListType == "flashimg") { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 153 | DataManager::GetValue(mVariable, currentValue); |
Ethan Yonker | cfd6509 | 2015-02-14 10:39:21 -0600 | [diff] [blame] | 154 | SetPosition(); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 155 | } |
| 156 | updateList = true; |
| 157 | mUpdate = 1; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | void GUIPartitionList::MatchList(void) { |
| 162 | int i, listSize = mList.size(); |
| 163 | string variablelist, searchvalue; |
| 164 | size_t pos; |
| 165 | |
| 166 | DataManager::GetValue(mVariable, variablelist); |
| 167 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 168 | for (i = 0; i < listSize; i++) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 169 | searchvalue = mList.at(i).Mount_Point + ";"; |
| 170 | pos = variablelist.find(searchvalue); |
| 171 | if (pos != string::npos) { |
| 172 | mList.at(i).selected = 1; |
| 173 | } else { |
| 174 | mList.at(i).selected = 0; |
| 175 | } |
| 176 | } |
| 177 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 178 | |
Ethan Yonker | cfd6509 | 2015-02-14 10:39:21 -0600 | [diff] [blame] | 179 | void GUIPartitionList::SetPosition() { |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 180 | int listSize = mList.size(); |
| 181 | |
Ethan Yonker | cfd6509 | 2015-02-14 10:39:21 -0600 | [diff] [blame] | 182 | SetVisibleListLocation(0); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 183 | for (int i = 0; i < listSize; i++) { |
| 184 | if (mList.at(i).Mount_Point == currentValue) { |
| 185 | mList.at(i).selected = 1; |
| 186 | SetVisibleListLocation(i); |
| 187 | } else { |
| 188 | mList.at(i).selected = 0; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | size_t GUIPartitionList::GetItemCount() |
| 194 | { |
| 195 | return mList.size(); |
| 196 | } |
| 197 | |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame] | 198 | void GUIPartitionList::RenderItem(size_t itemindex, int yPos, bool selected) |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 199 | { |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame] | 200 | // note: the "selected" parameter above is for the currently touched item |
| 201 | // don't confuse it with the more persistent "selected" flag per list item used below |
| 202 | ImageResource* icon = mList.at(itemindex).selected ? mIconSelected : mIconUnselected; |
| 203 | const std::string& text = mList.at(itemindex).Display_Name; |
| 204 | |
| 205 | RenderStdItem(yPos, selected, icon, text.c_str()); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | void GUIPartitionList::NotifySelect(size_t item_selected) |
| 209 | { |
| 210 | if (item_selected < mList.size()) { |
| 211 | int listSize = mList.size(); |
| 212 | if (ListType == "mount") { |
| 213 | if (!mList.at(item_selected).selected) { |
| 214 | if (PartitionManager.Mount_By_Path(mList.at(item_selected).Mount_Point, true)) { |
| 215 | mList.at(item_selected).selected = 1; |
| 216 | PartitionManager.Add_MTP_Storage(mList.at(item_selected).Mount_Point); |
| 217 | mUpdate = 1; |
| 218 | } |
| 219 | } else { |
| 220 | if (PartitionManager.UnMount_By_Path(mList.at(item_selected).Mount_Point, true)) { |
| 221 | mList.at(item_selected).selected = 0; |
| 222 | mUpdate = 1; |
| 223 | } |
| 224 | } |
| 225 | } else if (!mVariable.empty()) { |
| 226 | if (ListType == "storage") { |
| 227 | int i; |
| 228 | std::string str = mList.at(item_selected).Mount_Point; |
| 229 | bool update_size = false; |
| 230 | TWPartition* Part = PartitionManager.Find_Partition_By_Path(str); |
| 231 | if (Part == NULL) { |
| 232 | LOGERR("Unable to locate partition for '%s'\n", str.c_str()); |
| 233 | return; |
| 234 | } |
| 235 | if (!Part->Is_Mounted() && Part->Removable) |
| 236 | update_size = true; |
| 237 | if (!Part->Mount(true)) { |
| 238 | // Do Nothing |
| 239 | } else if (update_size && !Part->Update_Size(true)) { |
| 240 | // Do Nothing |
| 241 | } else { |
| 242 | for (i=0; i<listSize; i++) |
| 243 | mList.at(i).selected = 0; |
| 244 | |
| 245 | if (update_size) { |
| 246 | char free_space[255]; |
| 247 | sprintf(free_space, "%llu", Part->Free / 1024 / 1024); |
| 248 | mList.at(item_selected).Display_Name = Part->Storage_Name + " ("; |
| 249 | mList.at(item_selected).Display_Name += free_space; |
| 250 | mList.at(item_selected).Display_Name += "MB)"; |
| 251 | } |
| 252 | mList.at(item_selected).selected = 1; |
| 253 | mUpdate = 1; |
| 254 | |
| 255 | DataManager::SetValue(mVariable, str); |
| 256 | } |
| 257 | } else { |
| 258 | if (ListType == "flashimg") { // only one item can be selected for flashing images |
| 259 | for (int i=0; i<listSize; i++) |
| 260 | mList.at(i).selected = 0; |
| 261 | } |
| 262 | if (mList.at(item_selected).selected) |
| 263 | mList.at(item_selected).selected = 0; |
| 264 | else |
| 265 | mList.at(item_selected).selected = 1; |
| 266 | |
| 267 | int i; |
| 268 | string variablelist; |
| 269 | for (i=0; i<listSize; i++) { |
| 270 | if (mList.at(i).selected) { |
| 271 | variablelist += mList.at(i).Mount_Point + ";"; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | mUpdate = 1; |
| 276 | if (selectedList.empty()) |
| 277 | DataManager::SetValue(mVariable, variablelist); |
| 278 | else |
| 279 | DataManager::SetValue(selectedList, variablelist); |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | } |