blob: 9c5b58e7d0eb9c130cfb4c2d5d765373821c3f39 [file] [log] [blame]
Dees_Troya13d74f2013-03-24 08:54:55 -05001/*
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_Troya13d74f2013-03-24 08:54:55 -050019#include <string.h>
Dees_Troya13d74f2013-03-24 08:54:55 -050020#include <sys/stat.h>
Dees_Troya13d74f2013-03-24 08:54:55 -050021#include <dirent.h>
Dees_Troya13d74f2013-03-24 08:54:55 -050022
23extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000024#include "../twcommon.h"
Dees_Troya13d74f2013-03-24 08:54:55 -050025}
Ethan Yonkerfbb43532015-12-28 21:54:50 +010026#include "../minuitwrp/minui.h"
Dees_Troya13d74f2013-03-24 08:54:55 -050027
28#include "rapidxml.hpp"
29#include "objects.hpp"
30#include "../data.hpp"
Dees_Troya13d74f2013-03-24 08:54:55 -050031#include "../partitions.hpp"
32
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010033GUIPartitionList::GUIPartitionList(xml_node<>* node) : GUIScrollList(node)
Dees_Troya13d74f2013-03-24 08:54:55 -050034{
35 xml_attribute<>* attr;
36 xml_node<>* child;
Dees_Troya13d74f2013-03-24 08:54:55 -050037
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010038 mIconSelected = mIconUnselected = NULL;
Dees_Troya13d74f2013-03-24 08:54:55 -050039 mUpdate = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -050040 updateList = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050041
Ethan Yonker21ff02a2015-02-18 14:35:00 -060042 child = FindNode(node, "icon");
Dees_Troya13d74f2013-03-24 08:54:55 -050043 if (child)
44 {
thatf6ed8fc2015-02-14 20:23:16 +010045 mIconSelected = LoadAttrImage(child, "selected");
46 mIconUnselected = LoadAttrImage(child, "unselected");
Dees_Troya13d74f2013-03-24 08:54:55 -050047 }
Dees_Troya13d74f2013-03-24 08:54:55 -050048
49 // Handle the result variable
Ethan Yonker21ff02a2015-02-18 14:35:00 -060050 child = FindNode(node, "data");
Dees_Troya13d74f2013-03-24 08:54:55 -050051 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 Yonker58f21322018-08-24 11:17:36 -050061 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 }
thatf6ed8fc2015-02-14 20:23:16 +010072 SetMaxIconSize(iconWidth, iconHeight);
Dees_Troya13d74f2013-03-24 08:54:55 -050073
Ethan Yonker21ff02a2015-02-18 14:35:00 -060074 child = FindNode(node, "listtype");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010075 if (child && (attr = child->first_attribute("name"))) {
76 ListType = attr->value();
77 updateList = true;
Dees_Troya13d74f2013-03-24 08:54:55 -050078 } else {
79 mList.clear();
Dees_Troy2673cec2013-04-02 20:22:16 +000080 LOGERR("No partition listtype specified for partitionlist GUI element\n");
Dees_Troya13d74f2013-03-24 08:54:55 -050081 return;
82 }
83}
84
85GUIPartitionList::~GUIPartitionList()
86{
Dees_Troya13d74f2013-03-24 08:54:55 -050087}
88
89int GUIPartitionList::Update(void)
90{
Matt Mowera8a89d12016-12-30 18:10:37 -060091 if (!isConditionTrue())
Vojtech Bocekede51c52014-02-07 23:58:09 +010092 return 0;
93
Dees_Troya13d74f2013-03-24 08:54:55 -050094 // 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 Yonker0a3a98f2015-02-05 00:48:28 +0100108 GUIScrollList::Update();
109
110 if (updateList) {
Matt Mowera8a89d12016-12-30 18:10:37 -0600111 // Completely update the list if needed -- Used primarily for
112 // restore as the list for restore will change depending on what
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100113 // partitions were backed up
114 mList.clear();
115 PartitionManager.Get_Partition_List(ListType, &mList);
116 SetVisibleListLocation(0);
117 updateList = false;
118 mUpdate = 1;
Ethan Yonkercfd65092015-02-14 10:39:21 -0600119 if (ListType == "backup" || ListType == "flashimg")
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100120 MatchList();
121 }
122
123 if (mUpdate) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500124 mUpdate = 0;
125 if (Render() == 0)
126 return 2;
127 }
128
Dees_Troya13d74f2013-03-24 08:54:55 -0500129 return 0;
130}
131
Vojtech Bocek07220562014-02-08 02:05:33 +0100132int GUIPartitionList::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troya13d74f2013-03-24 08:54:55 -0500133{
thatfa30aca2015-02-13 01:22:22 +0100134 GUIScrollList::NotifyVarChange(varName, value);
135
Matt Mowera8a89d12016-12-30 18:10:37 -0600136 if (!isConditionTrue())
Vojtech Bocekede51c52014-02-07 23:58:09 +0100137 return 0;
138
Dees_Troya13d74f2013-03-24 08:54:55 -0500139 if (varName == mVariable && !mUpdate)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200140 {
141 if (ListType == "storage") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500142 currentValue = value;
Ethan Yonkercfd65092015-02-14 10:39:21 -0600143 SetPosition();
Dees_Troya13d74f2013-03-24 08:54:55 -0500144 } else if (ListType == "backup") {
145 MatchList();
146 } else if (ListType == "restore") {
147 updateList = true;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100148 SetVisibleListLocation(0);
Dees_Troya13d74f2013-03-24 08:54:55 -0500149 }
150
151 mUpdate = 1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200152 return 0;
153 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500154 return 0;
155}
156
Dees_Troya13d74f2013-03-24 08:54:55 -0500157void GUIPartitionList::SetPageFocus(int inFocus)
158{
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100159 GUIScrollList::SetPageFocus(inFocus);
Dees_Troya13d74f2013-03-24 08:54:55 -0500160 if (inFocus) {
Ethan Yonkercfd65092015-02-14 10:39:21 -0600161 if (ListType == "storage" || ListType == "flashimg") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500162 DataManager::GetValue(mVariable, currentValue);
Ethan Yonkercfd65092015-02-14 10:39:21 -0600163 SetPosition();
Dees_Troya13d74f2013-03-24 08:54:55 -0500164 }
165 updateList = true;
166 mUpdate = 1;
167 }
168}
169
170void GUIPartitionList::MatchList(void) {
171 int i, listSize = mList.size();
172 string variablelist, searchvalue;
173 size_t pos;
174
175 DataManager::GetValue(mVariable, variablelist);
176
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100177 for (i = 0; i < listSize; i++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500178 searchvalue = mList.at(i).Mount_Point + ";";
179 pos = variablelist.find(searchvalue);
180 if (pos != string::npos) {
181 mList.at(i).selected = 1;
epicXb7337372021-02-24 23:12:08 +0530182 TWPartition* t_part = PartitionManager.Find_Partition_By_Path(mList.at(i).Mount_Point);
183 DataManager::SetValue("tw_is_slot_part", t_part != NULL ? (int) t_part->SlotSelect : 0);
Dees_Troya13d74f2013-03-24 08:54:55 -0500184 } else {
185 mList.at(i).selected = 0;
186 }
187 }
188}
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100189
Ethan Yonkercfd65092015-02-14 10:39:21 -0600190void GUIPartitionList::SetPosition() {
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100191 int listSize = mList.size();
192
Ethan Yonkercfd65092015-02-14 10:39:21 -0600193 SetVisibleListLocation(0);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100194 for (int i = 0; i < listSize; i++) {
195 if (mList.at(i).Mount_Point == currentValue) {
196 mList.at(i).selected = 1;
197 SetVisibleListLocation(i);
198 } else {
199 mList.at(i).selected = 0;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100200 }
201 }
202}
203
204size_t GUIPartitionList::GetItemCount()
205{
206 return mList.size();
207}
208
that0af77952015-02-25 08:52:19 +0100209void GUIPartitionList::RenderItem(size_t itemindex, int yPos, bool selected)
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100210{
that0af77952015-02-25 08:52:19 +0100211 // note: the "selected" parameter above is for the currently touched item
212 // don't confuse it with the more persistent "selected" flag per list item used below
213 ImageResource* icon = mList.at(itemindex).selected ? mIconSelected : mIconUnselected;
214 const std::string& text = mList.at(itemindex).Display_Name;
215
216 RenderStdItem(yPos, selected, icon, text.c_str());
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100217}
218
219void GUIPartitionList::NotifySelect(size_t item_selected)
220{
221 if (item_selected < mList.size()) {
222 int listSize = mList.size();
223 if (ListType == "mount") {
224 if (!mList.at(item_selected).selected) {
225 if (PartitionManager.Mount_By_Path(mList.at(item_selected).Mount_Point, true)) {
226 mList.at(item_selected).selected = 1;
227 PartitionManager.Add_MTP_Storage(mList.at(item_selected).Mount_Point);
228 mUpdate = 1;
229 }
230 } else {
231 if (PartitionManager.UnMount_By_Path(mList.at(item_selected).Mount_Point, true)) {
232 mList.at(item_selected).selected = 0;
233 mUpdate = 1;
234 }
235 }
236 } else if (!mVariable.empty()) {
237 if (ListType == "storage") {
238 int i;
239 std::string str = mList.at(item_selected).Mount_Point;
240 bool update_size = false;
241 TWPartition* Part = PartitionManager.Find_Partition_By_Path(str);
242 if (Part == NULL) {
243 LOGERR("Unable to locate partition for '%s'\n", str.c_str());
244 return;
245 }
246 if (!Part->Is_Mounted() && Part->Removable)
247 update_size = true;
248 if (!Part->Mount(true)) {
249 // Do Nothing
250 } else if (update_size && !Part->Update_Size(true)) {
251 // Do Nothing
252 } else {
253 for (i=0; i<listSize; i++)
254 mList.at(i).selected = 0;
255
256 if (update_size) {
257 char free_space[255];
258 sprintf(free_space, "%llu", Part->Free / 1024 / 1024);
259 mList.at(item_selected).Display_Name = Part->Storage_Name + " (";
260 mList.at(item_selected).Display_Name += free_space;
261 mList.at(item_selected).Display_Name += "MB)";
262 }
263 mList.at(item_selected).selected = 1;
264 mUpdate = 1;
265
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;
epicXb7337372021-02-24 23:12:08 +0530277 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 Yonker0a3a98f2015-02-05 00:48:28 +0100279
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}