blob: 48bd295d6464a6e28c77b2c6ef77792fdaa08e5c [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#include "../minuitwrp/minui.h"
Dees_Troya13d74f2013-03-24 08:54:55 -050026}
27
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 int mIconWidth = 0, mIconHeight = 0, mSelectedIconHeight = 0, mSelectedIconWidth = 0, mUnselectedIconHeight = 0, mUnselectedIconWidth = 0;
39 mIconSelected = mIconUnselected = NULL;
Dees_Troya13d74f2013-03-24 08:54:55 -050040 mUpdate = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -050041 updateList = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050042
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_Troya13d74f2013-03-24 08:54:55 -050053
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_Troya13d74f2013-03-24 08:54:55 -050066 if (mIconSelected && mIconSelected->GetResource())
67 {
68 mSelectedIconWidth = gr_get_width(mIconSelected->GetResource());
69 mSelectedIconHeight = gr_get_height(mIconSelected->GetResource());
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010070 if (mSelectedIconHeight > mIconHeight)
71 mIconHeight = mSelectedIconHeight;
Dees_Troya13d74f2013-03-24 08:54:55 -050072 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 Yonker0a3a98f2015-02-05 00:48:28 +010079 if (mUnselectedIconHeight > mIconHeight)
80 mIconHeight = mUnselectedIconHeight;
Dees_Troya13d74f2013-03-24 08:54:55 -050081 if (mUnselectedIconWidth > mIconWidth)
82 mIconWidth = mUnselectedIconWidth;
83 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010084 SetMaxIconSize(mIconWidth, mIconHeight);
Dees_Troya13d74f2013-03-24 08:54:55 -050085
86 child = node->first_node("listtype");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010087 if (child && (attr = child->first_attribute("name"))) {
88 ListType = attr->value();
89 updateList = true;
Dees_Troya13d74f2013-03-24 08:54:55 -050090 } else {
91 mList.clear();
Dees_Troy2673cec2013-04-02 20:22:16 +000092 LOGERR("No partition listtype specified for partitionlist GUI element\n");
Dees_Troya13d74f2013-03-24 08:54:55 -050093 return;
94 }
95}
96
97GUIPartitionList::~GUIPartitionList()
98{
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010099 delete mIconSelected;
100 delete mIconUnselected;
Dees_Troya13d74f2013-03-24 08:54:55 -0500101}
102
103int GUIPartitionList::Update(void)
104{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100105 if(!isConditionTrue())
106 return 0;
107
Dees_Troya13d74f2013-03-24 08:54:55 -0500108 // Check for changes in mount points if the list type is mount and update the list and render if needed
109 if (ListType == "mount") {
110 int listSize = mList.size();
111 for (int i = 0; i < listSize; i++) {
112 if (PartitionManager.Is_Mounted_By_Path(mList.at(i).Mount_Point) && !mList.at(i).selected) {
113 mList.at(i).selected = 1;
114 mUpdate = 1;
115 } else if (!PartitionManager.Is_Mounted_By_Path(mList.at(i).Mount_Point) && mList.at(i).selected) {
116 mList.at(i).selected = 0;
117 mUpdate = 1;
118 }
119 }
120 }
121
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100122 GUIScrollList::Update();
123
124 if (updateList) {
125 int listSize = 0;
126
127 // Completely update the list if needed -- Used primarily for
128 // restore as the list for restore will change depending on what
129 // partitions were backed up
130 mList.clear();
131 PartitionManager.Get_Partition_List(ListType, &mList);
132 SetVisibleListLocation(0);
133 updateList = false;
134 mUpdate = 1;
135 if (ListType == "backup")
136 MatchList();
137 }
138
139 if (mUpdate) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500140 mUpdate = 0;
141 if (Render() == 0)
142 return 2;
143 }
144
Dees_Troya13d74f2013-03-24 08:54:55 -0500145 return 0;
146}
147
Vojtech Bocek07220562014-02-08 02:05:33 +0100148int GUIPartitionList::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troya13d74f2013-03-24 08:54:55 -0500149{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100150 if(!isConditionTrue())
151 return 0;
152
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100153 GUIScrollList::NotifyVarChange(varName, value);
154
Dees_Troya13d74f2013-03-24 08:54:55 -0500155 if (varName == mVariable && !mUpdate)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200156 {
157 if (ListType == "storage") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500158 currentValue = value;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100159 SetStoragePosition();
Dees_Troya13d74f2013-03-24 08:54:55 -0500160 } else if (ListType == "backup") {
161 MatchList();
162 } else if (ListType == "restore") {
163 updateList = true;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100164 SetVisibleListLocation(0);
Dees_Troya13d74f2013-03-24 08:54:55 -0500165 }
166
167 mUpdate = 1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200168 return 0;
169 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500170 return 0;
171}
172
Dees_Troya13d74f2013-03-24 08:54:55 -0500173void GUIPartitionList::SetPageFocus(int inFocus)
174{
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100175 GUIScrollList::SetPageFocus(inFocus);
Dees_Troya13d74f2013-03-24 08:54:55 -0500176 if (inFocus) {
177 if (ListType == "storage") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500178 DataManager::GetValue(mVariable, currentValue);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100179 SetStoragePosition();
Dees_Troya13d74f2013-03-24 08:54:55 -0500180 }
181 updateList = true;
182 mUpdate = 1;
183 }
184}
185
186void GUIPartitionList::MatchList(void) {
187 int i, listSize = mList.size();
188 string variablelist, searchvalue;
189 size_t pos;
190
191 DataManager::GetValue(mVariable, variablelist);
192
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100193 for (i = 0; i < listSize; i++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500194 searchvalue = mList.at(i).Mount_Point + ";";
195 pos = variablelist.find(searchvalue);
196 if (pos != string::npos) {
197 mList.at(i).selected = 1;
198 } else {
199 mList.at(i).selected = 0;
200 }
201 }
202}
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100203
204void GUIPartitionList::SetStoragePosition() {
205 int listSize = mList.size();
206
207 for (int i = 0; i < listSize; i++) {
208 if (mList.at(i).Mount_Point == currentValue) {
209 mList.at(i).selected = 1;
210 SetVisibleListLocation(i);
211 } else {
212 mList.at(i).selected = 0;
213 SetVisibleListLocation(0);
214 }
215 }
216}
217
218size_t GUIPartitionList::GetItemCount()
219{
220 return mList.size();
221}
222
223int GUIPartitionList::GetListItem(size_t item_index, Resource*& icon, std::string &text)
224{
225 text = mList.at(item_index).Display_Name;
226 if (mList.at(item_index).selected)
227 icon = mIconSelected;
228 else
229 icon = mIconUnselected;
230 return 0;
231}
232
233void GUIPartitionList::NotifySelect(size_t item_selected)
234{
235 if (item_selected < mList.size()) {
236 int listSize = mList.size();
237 if (ListType == "mount") {
238 if (!mList.at(item_selected).selected) {
239 if (PartitionManager.Mount_By_Path(mList.at(item_selected).Mount_Point, true)) {
240 mList.at(item_selected).selected = 1;
241 PartitionManager.Add_MTP_Storage(mList.at(item_selected).Mount_Point);
242 mUpdate = 1;
243 }
244 } else {
245 if (PartitionManager.UnMount_By_Path(mList.at(item_selected).Mount_Point, true)) {
246 mList.at(item_selected).selected = 0;
247 mUpdate = 1;
248 }
249 }
250 } else if (!mVariable.empty()) {
251 if (ListType == "storage") {
252 int i;
253 std::string str = mList.at(item_selected).Mount_Point;
254 bool update_size = false;
255 TWPartition* Part = PartitionManager.Find_Partition_By_Path(str);
256 if (Part == NULL) {
257 LOGERR("Unable to locate partition for '%s'\n", str.c_str());
258 return;
259 }
260 if (!Part->Is_Mounted() && Part->Removable)
261 update_size = true;
262 if (!Part->Mount(true)) {
263 // Do Nothing
264 } else if (update_size && !Part->Update_Size(true)) {
265 // Do Nothing
266 } else {
267 for (i=0; i<listSize; i++)
268 mList.at(i).selected = 0;
269
270 if (update_size) {
271 char free_space[255];
272 sprintf(free_space, "%llu", Part->Free / 1024 / 1024);
273 mList.at(item_selected).Display_Name = Part->Storage_Name + " (";
274 mList.at(item_selected).Display_Name += free_space;
275 mList.at(item_selected).Display_Name += "MB)";
276 }
277 mList.at(item_selected).selected = 1;
278 mUpdate = 1;
279
280 DataManager::SetValue(mVariable, str);
281 }
282 } else {
283 if (ListType == "flashimg") { // only one item can be selected for flashing images
284 for (int i=0; i<listSize; i++)
285 mList.at(i).selected = 0;
286 }
287 if (mList.at(item_selected).selected)
288 mList.at(item_selected).selected = 0;
289 else
290 mList.at(item_selected).selected = 1;
291
292 int i;
293 string variablelist;
294 for (i=0; i<listSize; i++) {
295 if (mList.at(i).selected) {
296 variablelist += mList.at(i).Mount_Point + ";";
297 }
298 }
299
300 mUpdate = 1;
301 if (selectedList.empty())
302 DataManager::SetValue(mVariable, variablelist);
303 else
304 DataManager::SetValue(selectedList, variablelist);
305 }
306 }
307 }
308}