blob: 102802d7d0207759626c6379cdabd3394f82023a [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 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{
Vojtech Bocekede51c52014-02-07 23:58:09 +010082 if(!isConditionTrue())
83 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) {
102 int listSize = 0;
103
104 // Completely update the list if needed -- Used primarily for
105 // restore as the list for restore will change depending on what
106 // partitions were backed up
107 mList.clear();
108 PartitionManager.Get_Partition_List(ListType, &mList);
109 SetVisibleListLocation(0);
110 updateList = false;
111 mUpdate = 1;
Ethan Yonkercfd65092015-02-14 10:39:21 -0600112 if (ListType == "backup" || ListType == "flashimg")
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100113 MatchList();
114 }
115
116 if (mUpdate) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500117 mUpdate = 0;
118 if (Render() == 0)
119 return 2;
120 }
121
Dees_Troya13d74f2013-03-24 08:54:55 -0500122 return 0;
123}
124
Vojtech Bocek07220562014-02-08 02:05:33 +0100125int GUIPartitionList::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troya13d74f2013-03-24 08:54:55 -0500126{
thatfa30aca2015-02-13 01:22:22 +0100127 GUIScrollList::NotifyVarChange(varName, value);
128
Vojtech Bocekede51c52014-02-07 23:58:09 +0100129 if(!isConditionTrue())
130 return 0;
131
Dees_Troya13d74f2013-03-24 08:54:55 -0500132 if (varName == mVariable && !mUpdate)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200133 {
134 if (ListType == "storage") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500135 currentValue = value;
Ethan Yonkercfd65092015-02-14 10:39:21 -0600136 SetPosition();
Dees_Troya13d74f2013-03-24 08:54:55 -0500137 } else if (ListType == "backup") {
138 MatchList();
139 } else if (ListType == "restore") {
140 updateList = true;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100141 SetVisibleListLocation(0);
Dees_Troya13d74f2013-03-24 08:54:55 -0500142 }
143
144 mUpdate = 1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200145 return 0;
146 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500147 return 0;
148}
149
Dees_Troya13d74f2013-03-24 08:54:55 -0500150void GUIPartitionList::SetPageFocus(int inFocus)
151{
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100152 GUIScrollList::SetPageFocus(inFocus);
Dees_Troya13d74f2013-03-24 08:54:55 -0500153 if (inFocus) {
Ethan Yonkercfd65092015-02-14 10:39:21 -0600154 if (ListType == "storage" || ListType == "flashimg") {
Dees_Troya13d74f2013-03-24 08:54:55 -0500155 DataManager::GetValue(mVariable, currentValue);
Ethan Yonkercfd65092015-02-14 10:39:21 -0600156 SetPosition();
Dees_Troya13d74f2013-03-24 08:54:55 -0500157 }
158 updateList = true;
159 mUpdate = 1;
160 }
161}
162
163void GUIPartitionList::MatchList(void) {
164 int i, listSize = mList.size();
165 string variablelist, searchvalue;
166 size_t pos;
167
168 DataManager::GetValue(mVariable, variablelist);
169
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100170 for (i = 0; i < listSize; i++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500171 searchvalue = mList.at(i).Mount_Point + ";";
172 pos = variablelist.find(searchvalue);
173 if (pos != string::npos) {
174 mList.at(i).selected = 1;
175 } else {
176 mList.at(i).selected = 0;
177 }
178 }
179}
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100180
Ethan Yonkercfd65092015-02-14 10:39:21 -0600181void GUIPartitionList::SetPosition() {
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100182 int listSize = mList.size();
183
Ethan Yonkercfd65092015-02-14 10:39:21 -0600184 SetVisibleListLocation(0);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100185 for (int i = 0; i < listSize; i++) {
186 if (mList.at(i).Mount_Point == currentValue) {
187 mList.at(i).selected = 1;
188 SetVisibleListLocation(i);
189 } else {
190 mList.at(i).selected = 0;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100191 }
192 }
193}
194
195size_t GUIPartitionList::GetItemCount()
196{
197 return mList.size();
198}
199
that0af77952015-02-25 08:52:19 +0100200void GUIPartitionList::RenderItem(size_t itemindex, int yPos, bool selected)
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100201{
that0af77952015-02-25 08:52:19 +0100202 // note: the "selected" parameter above is for the currently touched item
203 // don't confuse it with the more persistent "selected" flag per list item used below
204 ImageResource* icon = mList.at(itemindex).selected ? mIconSelected : mIconUnselected;
205 const std::string& text = mList.at(itemindex).Display_Name;
206
207 RenderStdItem(yPos, selected, icon, text.c_str());
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100208}
209
210void GUIPartitionList::NotifySelect(size_t item_selected)
211{
212 if (item_selected < mList.size()) {
213 int listSize = mList.size();
214 if (ListType == "mount") {
215 if (!mList.at(item_selected).selected) {
216 if (PartitionManager.Mount_By_Path(mList.at(item_selected).Mount_Point, true)) {
217 mList.at(item_selected).selected = 1;
218 PartitionManager.Add_MTP_Storage(mList.at(item_selected).Mount_Point);
219 mUpdate = 1;
220 }
221 } else {
222 if (PartitionManager.UnMount_By_Path(mList.at(item_selected).Mount_Point, true)) {
223 mList.at(item_selected).selected = 0;
224 mUpdate = 1;
225 }
226 }
227 } else if (!mVariable.empty()) {
228 if (ListType == "storage") {
229 int i;
230 std::string str = mList.at(item_selected).Mount_Point;
231 bool update_size = false;
232 TWPartition* Part = PartitionManager.Find_Partition_By_Path(str);
233 if (Part == NULL) {
234 LOGERR("Unable to locate partition for '%s'\n", str.c_str());
235 return;
236 }
237 if (!Part->Is_Mounted() && Part->Removable)
238 update_size = true;
239 if (!Part->Mount(true)) {
240 // Do Nothing
241 } else if (update_size && !Part->Update_Size(true)) {
242 // Do Nothing
243 } else {
244 for (i=0; i<listSize; i++)
245 mList.at(i).selected = 0;
246
247 if (update_size) {
248 char free_space[255];
249 sprintf(free_space, "%llu", Part->Free / 1024 / 1024);
250 mList.at(item_selected).Display_Name = Part->Storage_Name + " (";
251 mList.at(item_selected).Display_Name += free_space;
252 mList.at(item_selected).Display_Name += "MB)";
253 }
254 mList.at(item_selected).selected = 1;
255 mUpdate = 1;
256
257 DataManager::SetValue(mVariable, str);
258 }
259 } else {
260 if (ListType == "flashimg") { // only one item can be selected for flashing images
261 for (int i=0; i<listSize; i++)
262 mList.at(i).selected = 0;
263 }
264 if (mList.at(item_selected).selected)
265 mList.at(item_selected).selected = 0;
266 else
267 mList.at(item_selected).selected = 1;
268
269 int i;
270 string variablelist;
271 for (i=0; i<listSize; i++) {
272 if (mList.at(i).selected) {
273 variablelist += mList.at(i).Mount_Point + ";";
274 }
275 }
276
277 mUpdate = 1;
278 if (selectedList.empty())
279 DataManager::SetValue(mVariable, variablelist);
280 else
281 DataManager::SetValue(selectedList, variablelist);
282 }
283 }
284 }
285}