Fixed Issue: N_SE-50944
[apps/native/sample/ImageFeatureManager.git] / project / src / UpdatePanel.cpp
1 //
2 // Tizen C++ SDK
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.1 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include "AppResourceId.h"
19 #include "UpdatePanel.h"
20 #include "FileManagerForm.h"
21 #include "MainForm.h"
22
23 using namespace Tizen::Base;
24 using namespace Tizen::Graphics;
25 using namespace Tizen::Ui;
26 using namespace Tizen::Ui::Controls;
27 using namespace Tizen::Ui::Scenes;
28
29
30 UpdatePanel::UpdatePanel(void)
31     : __currentFeaturePath("")
32     , __currentImagePath("")
33         , __prevFeaturesCount(0)
34 {
35 }
36
37 UpdatePanel::~UpdatePanel(void)
38 {
39 }
40
41 bool
42 UpdatePanel::Initialize()
43 {
44     Panel::Construct(IDC_UPDATE_MODE_PANEL);
45     return true;
46 }
47
48 result
49 UpdatePanel::OnInitializing(void)
50 {
51     result r = E_SUCCESS;
52
53     Button *pButton = null;
54     Tizen::Ui::Controls::Frame *pFrame = Tizen::App::Application::GetInstance()->GetAppFrame()->GetFrame();
55     MainForm* pForm = static_cast<MainForm*>(pFrame->GetControl(IDF_FORM, true));
56
57     Rectangle clienRect = pForm->GetClientAreaBounds();
58     this->SetBounds(clienRect);
59
60     pButton = static_cast<Button*>(GetControl(IDC_BUTTON_LOAD_FEATURE_SET, true));
61     if (pButton)
62     {
63         pButton->SetActionId(ID_BUTTON_LOAD_FEATURE_SET);
64         pButton->AddActionEventListener(*this);
65     }
66
67     return r;
68 }
69
70 result
71 UpdatePanel::OnTerminating(void)
72 {
73     result r = E_SUCCESS;
74     return r;
75 }
76
77 void
78 UpdatePanel::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
79 {
80     SceneManager* pSceneManager = SceneManager::GetInstance();
81     AppAssert(pSceneManager);
82
83     switch (actionId)
84     {
85     case ID_BUTTON_LOAD_FEATURE_SET:
86     {
87         pSceneManager->GoForward(
88                 ForwardSceneTransition("ChooseExistingFeatureSetScene",
89                         SCENE_TRANSITION_ANIMATION_TYPE_ZOOM_IN,
90                         SCENE_HISTORY_OPTION_ADD_HISTORY,
91                         SCENE_DESTROY_OPTION_KEEP), 0);
92     }
93     break;
94     }
95 }
96
97
98 void
99 UpdatePanel::OnSceneActivatedN(
100         const Tizen::Ui::Scenes::SceneId& previousSceneId,
101         const Tizen::Ui::Scenes::SceneId& currentSceneId,
102         Tizen::Base::Collection::IList* pArgs)
103 {
104
105     Tizen::Ui::Controls::Frame *pFrame = Tizen::App::Application::GetInstance()->GetAppFrame()->GetFrame();
106     MainForm* pForm = static_cast<MainForm*>(pFrame->GetControl(IDF_FORM, true));
107     Footer* pFooter = pForm->GetFooter();
108
109     if ((previousSceneId == "ChooseImageScene") && pArgs)
110     {
111         if (pArgs->GetCount() > 0)
112         {
113             __currentImagePath   = *(dynamic_cast<String*>(pArgs->GetAt(0)));
114             pForm->AppendImageFolder(__currentImagePath, this);
115
116                 if (pForm->GetFeatureManager()->GetTotalNumberOfFeatures() != __prevFeaturesCount)
117                 {
118                                 Footer* pFooter = pForm->GetFooter();
119                                 pFooter->SetItemEnabled(0, true);
120                 }
121         }
122     }
123     else if ((previousSceneId == "ChooseExistingFeatureSetScene") && pArgs)
124     {
125         if (pArgs->GetCount() > 0)
126         {
127             __currentFeaturePath   = *(dynamic_cast<String*>(pArgs->GetAt(0)));
128
129             result res = pForm->GetFeatureManager()->Load(__currentFeaturePath);
130             AppSecureLog("Result:: %s %ls", GetErrorMessage(res), __currentFeaturePath.GetPointer());
131
132             if (IsFailed(res))
133             {
134                 __currentFeaturePath = "";
135                 SendUserEvent(REQUEST_MESSAGE_LOAD_FAILED, null);
136             }
137             else
138             {
139                 __prevFeaturesCount = pForm->GetFeatureManager()->GetTotalNumberOfFeatures();
140                 pFooter->SetItemEnabled(0, false);
141                 pForm->SetMenuVisibility(true);
142             }
143         }
144     }
145     else if (previousSceneId == "GenerationModeScene")
146     {
147         result res = pForm->GetFeatureManager()->Load(__currentFeaturePath);
148         AppSecureLog("Result:: %s %ls", GetErrorMessage(res), __currentFeaturePath.GetPointer());
149         __prevFeaturesCount = pForm->GetFeatureManager()->GetTotalNumberOfFeatures();
150         pFooter->SetItemEnabled(0, false);
151
152         if(!IsFailed(res))
153         {
154                 pForm->SetMenuVisibility(true);
155         }
156
157     }
158     else if (previousSceneId == "ManageScene")
159     {
160         if (pForm->GetFeatureManager()->GetTotalNumberOfFeatures() != __prevFeaturesCount)
161         {
162             Footer* pFooter = pForm->GetFooter();
163                         pFooter->SetItemEnabled(0, true);
164                 SendUserEvent(REQUEST_MESSAGE_AFTER_DELETE, null);
165         }
166     }
167
168     if (pArgs)
169     {
170         pArgs->RemoveAll(true);
171         delete pArgs;
172     }
173
174
175     Label* pLabel = null;
176     pLabel = static_cast<Label*>(GetControl(IDC_FEATURE_SET_PATH_LABEL, true));
177     if (pLabel)
178     {
179         pLabel->SetText(__currentFeaturePath);
180     }
181
182
183     if (__currentFeaturePath.IsEmpty())
184     {
185         pFooter->SetItemEnabled(0, false);
186         pFooter->SetButtonEnabled(BUTTON_POSITION_LEFT, false);
187     }
188     else
189     {
190         pFooter->SetButtonEnabled(BUTTON_POSITION_LEFT, true);
191     }
192 }
193
194 void
195 UpdatePanel::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId,
196         const Tizen::Ui::Scenes::SceneId& nextSceneId)
197 {
198 }
199
200 void
201 UpdatePanel::OnImageProcessed(int imNo, int total)
202 {
203     Tizen::Ui::Controls::Frame *pFrame = Tizen::App::Application::GetInstance()->GetAppFrame()->GetFrame();
204     MainForm* pForm = static_cast<MainForm*>(pFrame->GetControl(IDF_FORM, true));
205     pForm->OnImageProcessed(imNo, total);
206 }
207
208
209 void
210 UpdatePanel::OnFinish(void)
211 {
212     Tizen::Ui::Controls::Frame *pFrame = Tizen::App::Application::GetInstance()->GetAppFrame()->GetFrame();
213     MainForm* pForm = static_cast<MainForm*>(pFrame->GetControl(IDF_FORM, true));
214
215     __currentImagePath = "";
216     Button* pButtonPath = static_cast<Button*>(GetControl(L"IDC_BUTTON_UPDATE_IMAGE_FOLDER", true));
217     if (pButtonPath)
218     {
219         pButtonPath->SetText(__currentImagePath);
220     }
221
222     if (pForm->GetFeatureManager()->GetTotalNumberOfFeatures() != __prevFeaturesCount)
223     {
224         Footer* pFooter = pForm->GetFooter();
225         pFooter->SetItemEnabled(0, true);
226         __prevFeaturesCount = pForm->GetFeatureManager()->GetTotalNumberOfFeatures();
227     }
228
229     pForm->OnFinish();
230 }
231
232 void
233 UpdatePanel::OnUserEventReceivedN(RequestId requestId, Tizen::Base::Collection::IList* pArgs)
234 {
235     SceneManager* pSceneManager = SceneManager::GetInstance();
236     AppAssert(pSceneManager);
237
238         switch (requestId)
239         {
240
241         case UpdatePanel::REQUEST_MESSAGE_LOAD_FAILED:
242         {
243         MessageBox msgBox;
244         int res = 0;
245
246         msgBox.Construct("Error", "Can't load feature set.", MSGBOX_STYLE_OK, 5000);
247         msgBox.ShowAndWait(res);
248         }
249         break;
250         case UpdatePanel::REQUEST_MESSAGE_AFTER_DELETE:
251         {
252                 Tizen::Ui::Controls::Frame *pFrame = Tizen::App::Application::GetInstance()->GetAppFrame()->GetFrame();
253                 MainForm* pForm = static_cast<MainForm*>(pFrame->GetControl(IDF_FORM, true));
254
255                 Tizen::Base::String message;
256                 message.Format(80, L"Deleted %i features.", __prevFeaturesCount - pForm->GetFeatureManager()->GetTotalNumberOfFeatures());
257
258         MessageBox msgBox;
259         int res = 0;
260
261         msgBox.Construct("Update", message, MSGBOX_STYLE_OK, 5000);
262         msgBox.ShowAndWait(res);
263
264         __prevFeaturesCount = pForm->GetFeatureManager()->GetTotalNumberOfFeatures();
265         }
266         break;
267     case UpdatePanel::REQUEST_ADD:
268     {
269         pSceneManager->GoForward(
270                 ForwardSceneTransition("ChooseImageScene",
271                         SCENE_TRANSITION_ANIMATION_TYPE_ZOOM_IN,
272                         SCENE_HISTORY_OPTION_ADD_HISTORY,
273                         SCENE_DESTROY_OPTION_KEEP), /*pList*/null);
274     }
275     break;
276     case UpdatePanel::REQUEST_DELETE:
277         {
278         pSceneManager->GoForward(
279                 ForwardSceneTransition("ManageScene",
280                         SCENE_TRANSITION_ANIMATION_TYPE_ZOOM_IN,
281                         SCENE_HISTORY_OPTION_ADD_HISTORY,
282                         SCENE_DESTROY_OPTION_KEEP), null);
283         }
284         break;
285     case UpdatePanel::REQUEST_UPDATE:
286         {
287                 Tizen::Ui::Controls::Frame *pFrame = Tizen::App::Application::GetInstance()->GetAppFrame()->GetFrame();
288                 MainForm* pForm = static_cast<MainForm*>(pFrame->GetControl(IDF_FORM, true));
289
290                 //Disabling update button
291                 Footer* pFooter = pForm->GetFooter();
292                 pFooter->SetItemEnabled(0, false);
293
294                 pFooter->Draw();
295                 pFooter->Show();
296
297         result res = pForm->GetFeatureManager()->Flush(0);
298         AppSecureLog("Result:: on Flush %s", GetErrorMessage(res));
299
300                 if (!IsFailed(res))
301                 {
302                         pForm->GetFeatureManager()->Load(__currentFeaturePath);
303
304                         MessageBox msgBox;
305                         int res = 0;
306
307                         String message;
308                         int currentFeaturesCount = pForm->GetFeatureManager()->GetTotalNumberOfFeatures();
309
310                         message.Format(80, L"Feature set successfully updated.\nTotal features count: %i", currentFeaturesCount);
311
312                         __prevFeaturesCount = currentFeaturesCount;
313
314                         msgBox.Construct("Update", message, MSGBOX_STYLE_OK, 5000);
315                         msgBox.ShowAndWait(res);
316
317                 }
318                 else
319                 {
320                         MessageBox msgBox;
321                         int res = 0;
322
323                         msgBox.Construct("Update", "Feature set failed to update.", MSGBOX_STYLE_OK, 5000);
324                         msgBox.ShowAndWait(res);
325
326                         pForm->GetFeatureManager()->Load(__currentFeaturePath);
327                 }
328         }
329         break;
330     default:
331         break;
332         }
333 }