blob: 7e5a74fa5a9e81885b95b9525f8f226e8acffa83 [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{
Dees_Troya13d74f2013-03-24 08:54:55 -050099}
100
101int GUIPartitionList::Update(void)
102{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100103 if(!isConditionTrue())
104 return 0;
105
Dees_Troya13d74f2013-03-24 08:54:55 -0500106 // 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 Yonker0a3a98f2015-02-05 00:48:28 +0100120 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_Troya13d74f2013-03-24 08:54:55 -0500138 mUpdate = 0;
139 if (Render() == 0)
140 return 2;
141 }
142
Dees_Troya13d74f2013-03-24 08:54:55 -0500143 return 0;
144}
145
Vojtech Bocek07220562014-02-08 02:05:33 +0100146int GUIPartitionList::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troya13d74f2013-03-24 08:54:55 -0500147{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100148 if(!isConditionTrue())
149 return 0;
150
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100151 GUIScrollList::NotifyVarChange(varName, value);
152
Dees_Troya13d74f2013-03-24 08:54:55 -0500153 if (varName == mVariable && !mUpdate)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200154 {
155 if (ListType == "storage") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500156 currentValue = value;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100157 SetStoragePosition();
Dees_Troya13d74f2013-03-24 08:54:55 -0500158 } else if (ListType == "backup") {
159 MatchList();
160 } else if (ListType == "restore") {
161 updateList = true;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100162 SetVisibleListLocation(0);
Dees_Troya13d74f2013-03-24 08:54:55 -0500163 }
164
165 mUpdate = 1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200166 return 0;
167 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500168 return 0;
169}
170
Dees_Troya13d74f2013-03-24 08:54:55 -0500171void GUIPartitionList::SetPageFocus(int inFocus)
172{
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100173 GUIScrollList::SetPageFocus(inFocus);
Dees_Troya13d74f2013-03-24 08:54:55 -0500174 if (inFocus) {
175 if (ListType == "storage") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500176 DataManager::GetValue(mVariable, currentValue);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100177 SetStoragePosition();
Dees_Troya13d74f2013-03-24 08:54:55 -0500178 }
179 updateList = true;
180 mUpdate = 1;
181 }
182}
183
184void 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 Yonker0a3a98f2015-02-05 00:48:28 +0100191 for (i = 0; i < listSize; i++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500192 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 Yonker0a3a98f2015-02-05 00:48:28 +0100201
202void 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
216size_t GUIPartitionList::GetItemCount()
217{
218 return mList.size();
219}
220
221int 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
231void 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}