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