blob: c85339152b64cbed7caafdce268b1525212d3028 [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
thatf6ed8fc2015-02-14 20:23:16 +010061 int iconWidth = std::max(mIconSelected->GetWidth(), mIconUnselected->GetWidth());
62 int iconHeight = std::max(mIconSelected->GetHeight(), mIconUnselected->GetHeight());
63 SetMaxIconSize(iconWidth, iconHeight);
Dees_Troya13d74f2013-03-24 08:54:55 -050064
Ethan Yonker21ff02a2015-02-18 14:35:00 -060065 child = FindNode(node, "listtype");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010066 if (child && (attr = child->first_attribute("name"))) {
67 ListType = attr->value();
68 updateList = true;
Dees_Troya13d74f2013-03-24 08:54:55 -050069 } else {
70 mList.clear();
Dees_Troy2673cec2013-04-02 20:22:16 +000071 LOGERR("No partition listtype specified for partitionlist GUI element\n");
Dees_Troya13d74f2013-03-24 08:54:55 -050072 return;
73 }
74}
75
76GUIPartitionList::~GUIPartitionList()
77{
Dees_Troya13d74f2013-03-24 08:54:55 -050078}
79
80int GUIPartitionList::Update(void)
81{
Matt Mowera8a89d12016-12-30 18:10:37 -060082 if (!isConditionTrue())
Vojtech Bocekede51c52014-02-07 23:58:09 +010083 return 0;
84
Dees_Troya13d74f2013-03-24 08:54:55 -050085 // 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 Yonker0a3a98f2015-02-05 00:48:28 +010099 GUIScrollList::Update();
100
101 if (updateList) {
Matt Mowera8a89d12016-12-30 18:10:37 -0600102 // Completely update the list if needed -- Used primarily for
103 // restore as the list for restore will change depending on what
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100104 // partitions were backed up
105 mList.clear();
106 PartitionManager.Get_Partition_List(ListType, &mList);
107 SetVisibleListLocation(0);
108 updateList = false;
109 mUpdate = 1;
Ethan Yonkercfd65092015-02-14 10:39:21 -0600110 if (ListType == "backup" || ListType == "flashimg")
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100111 MatchList();
112 }
113
114 if (mUpdate) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500115 mUpdate = 0;
116 if (Render() == 0)
117 return 2;
118 }
119
Dees_Troya13d74f2013-03-24 08:54:55 -0500120 return 0;
121}
122
Vojtech Bocek07220562014-02-08 02:05:33 +0100123int GUIPartitionList::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troya13d74f2013-03-24 08:54:55 -0500124{
thatfa30aca2015-02-13 01:22:22 +0100125 GUIScrollList::NotifyVarChange(varName, value);
126
Matt Mowera8a89d12016-12-30 18:10:37 -0600127 if (!isConditionTrue())
Vojtech Bocekede51c52014-02-07 23:58:09 +0100128 return 0;
129
Dees_Troya13d74f2013-03-24 08:54:55 -0500130 if (varName == mVariable && !mUpdate)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200131 {
132 if (ListType == "storage") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500133 currentValue = value;
Ethan Yonkercfd65092015-02-14 10:39:21 -0600134 SetPosition();
Dees_Troya13d74f2013-03-24 08:54:55 -0500135 } else if (ListType == "backup") {
136 MatchList();
137 } else if (ListType == "restore") {
138 updateList = true;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100139 SetVisibleListLocation(0);
Dees_Troya13d74f2013-03-24 08:54:55 -0500140 }
141
142 mUpdate = 1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200143 return 0;
144 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500145 return 0;
146}
147
Dees_Troya13d74f2013-03-24 08:54:55 -0500148void GUIPartitionList::SetPageFocus(int inFocus)
149{
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100150 GUIScrollList::SetPageFocus(inFocus);
Dees_Troya13d74f2013-03-24 08:54:55 -0500151 if (inFocus) {
Ethan Yonkercfd65092015-02-14 10:39:21 -0600152 if (ListType == "storage" || ListType == "flashimg") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500153 DataManager::GetValue(mVariable, currentValue);
Ethan Yonkercfd65092015-02-14 10:39:21 -0600154 SetPosition();
Dees_Troya13d74f2013-03-24 08:54:55 -0500155 }
156 updateList = true;
157 mUpdate = 1;
158 }
159}
160
161void 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 Yonker0a3a98f2015-02-05 00:48:28 +0100168 for (i = 0; i < listSize; i++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500169 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 Yonker0a3a98f2015-02-05 00:48:28 +0100178
Ethan Yonkercfd65092015-02-14 10:39:21 -0600179void GUIPartitionList::SetPosition() {
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100180 int listSize = mList.size();
181
Ethan Yonkercfd65092015-02-14 10:39:21 -0600182 SetVisibleListLocation(0);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100183 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 Yonker0a3a98f2015-02-05 00:48:28 +0100189 }
190 }
191}
192
193size_t GUIPartitionList::GetItemCount()
194{
195 return mList.size();
196}
197
that0af77952015-02-25 08:52:19 +0100198void GUIPartitionList::RenderItem(size_t itemindex, int yPos, bool selected)
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100199{
that0af77952015-02-25 08:52:19 +0100200 // 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 Yonker0a3a98f2015-02-05 00:48:28 +0100206}
207
208void 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}