fix to prevent multiple launch appcontrol.
[apps/osp/Account.git] / src / AccAddForm.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://floralicense.org/license/
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file                AccAddForm.cpp
19  * @brief               This is the implementation file for AddForm class.
20  */
21
22 #include <FApp.h>
23 #include "AccAddForm.h"
24 #include "AccTypes.h"
25
26 using namespace Tizen::App;
27 using namespace Tizen::Base;
28 using namespace Tizen::Graphics;
29 using namespace Tizen::Ui;
30 using namespace Tizen::Ui::Controls;
31 using namespace Tizen::Ui::Scenes;
32
33 AddForm::AddForm(void)
34         : __pPm(null)
35         , __pSectionTableView(null)
36         , __pEmptyPanel(null)
37         , __isRunningAppControl(false)
38 {
39 }
40
41 AddForm::~AddForm(void)
42 {
43 }
44
45 result
46 AddForm::Initialize()
47 {
48         return Construct(IDL_ACCOUNT_ADD_FORM);
49 }
50
51 result
52 AddForm::OnInitializing(void)
53 {
54         AppLogDebug("ENTER");
55         AddFocusEventListener(*this);
56         SetFormBackEventListener(this);
57
58         __pPm = AccountPresentationModel::GetInstance();
59
60         __pSectionTableView = static_cast<SectionTableView*>(GetControl(IDC_SECTIONTABLEVIEW));
61         __pSectionTableView->SetItemProvider(this);
62         __pSectionTableView->AddSectionTableViewItemEventListener(*this);
63
64         return E_SUCCESS;
65 }
66
67 result
68 AddForm::OnTerminating(void)
69 {
70         AppLogDebug("ENTER");
71         return E_SUCCESS;
72 }
73
74 void
75 AddForm::OnAppControlCompleteResponseReceived(const Tizen::App::AppId& appId, const Tizen::Base::String& operationId, Tizen::App::AppCtrlResult appControlResult, const Tizen::Base::Collection::IMap* pExtraData)
76 {
77         AppLogDebug("AppCtrlResult %d", appControlResult);
78         if (appControlResult == APP_CTRL_RESULT_SUCCEEDED)
79         {
80                 result r = SceneManager::GetInstance()->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT));
81                 if (r == E_UNDERFLOW)
82                 {
83                         r = SceneManager::GetInstance()->GoForward(SceneTransitionId(ID_SCNT_MAIN_FORM_BACK));
84                         AppLogDebug("%s", GetErrorMessage(r));
85                 }
86         }
87         else if (appControlResult == APP_CTRL_RESULT_FAILED || appControlResult == APP_CTRL_RESULT_ABORTED)
88         {
89                 String errorMsg;
90                 UiApp::GetInstance()->GetAppResource()->GetString(IDS_EMAIL_POP_UNABLE_TO_ADD_ACCOUNT, errorMsg);
91
92                 int modal = 0;
93                 MessageBox msgBox;
94                 msgBox.Construct(L"", errorMsg, MSGBOX_STYLE_NONE, 2000);
95                 msgBox.ShowAndWait(modal);
96         }
97 }
98
99 void
100 AddForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
101 {
102         AppLogDebug("ENTER");
103         result r = SceneManager::GetInstance()->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT));
104         if(r == E_UNDERFLOW)
105         {
106                 UiApp::GetInstance()->Terminate();
107         }
108 }
109
110 void
111 AddForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId, const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
112 {
113         AppLogDebug("ENTER");
114         __pPm->AddAccountChangedEventListener(*this);
115         __pSectionTableView->UpdateTableView();
116         if (__pSectionTableView->GetSectionCount() == 0)
117         {
118                 SetEmptyPanel();
119         }
120         else
121         {
122                 RemoveEmptyPanel();
123         }
124 }
125
126 void
127 AddForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId, const Tizen::Ui::Scenes::SceneId& nextSceneId)
128 {
129         AppLogDebug("ENTER");
130         __pPm->RemoveAccountChangedEventListener(*this);
131 }
132
133 Tizen::Ui::Controls::TableViewItem*
134 AddForm::CreateItem(int sectionIndex, int itemIndex, int itemWidth)
135 {
136         AppLogDebug("itemWidth: %d, sectionIndex: %d, itemIndex: %d", itemWidth, sectionIndex, itemIndex);
137
138         RelativeLayout layout;
139         layout.Construct();
140
141         TableViewItem* pItem = new (std::nothrow) TableViewItem();
142         pItem->Construct(layout, Dimension(itemWidth, 112));
143
144         Panel* pPanel = new (std::nothrow) Panel();
145         pPanel->Construct(IDL_PANEL_SINGLE_LINE_ITEM);
146
147         Label* pMainLabel = static_cast<Label*>(pPanel->GetControl(IDC_LABEL_MAIN_TEXT));
148         pMainLabel->SetText(__pPm->GetAccountProviderName(itemIndex));
149
150         Bitmap* pIcon = __pPm->GetAccountProviderIconN(itemIndex);
151         if (!pIcon)
152         {
153                 pIcon = UiApp::GetInstance()->GetAppResource()->GetBitmapN(L"A01_2_title_Icon_none.png");
154         }
155
156         Label* pIconLabel = static_cast<Label*>(pPanel->GetControl(IDC_LABEL_ICON));
157         pIconLabel->SetBackgroundBitmap(*pIcon);
158         delete pIcon;
159
160         pItem->AddControl(pPanel);
161
162         layout.SetHorizontalFitPolicy(*pPanel, FIT_POLICY_PARENT);
163         layout.SetVerticalFitPolicy(*pPanel, FIT_POLICY_PARENT);
164         layout.Update();
165
166         return pItem;
167 }
168
169 bool
170 AddForm::DeleteItem(int sectionIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem *pItem)
171 {
172         delete pItem;
173         return true;
174 }
175
176 int
177 AddForm::GetDefaultItemHeight(void)
178 {
179         return 0;
180 }
181
182 int
183 AddForm::GetItemCount(int sectionIndex)
184 {
185         AppLogDebug("sectionIndex: %d", sectionIndex);
186         switch (sectionIndex)
187         {
188         case 0:
189                 return __pPm->GetAccountProviderCount();
190         default:
191                 return 0;
192         }
193 }
194
195 int
196 AddForm::GetSectionCount(void)
197 {
198         if (__pPm->GetAccountProviderCount() == 0)
199         {
200                 return 0;
201         }
202         else
203         {
204                 return 1;
205         }
206 }
207
208 Tizen::Base::String
209 AddForm::GetSectionFooter(int sectionIndex)
210 {
211         return L"";
212 }
213
214 Tizen::Base::String
215 AddForm::GetSectionHeader(int sectionIndex)
216 {
217         return L"";
218 }
219
220 bool
221 AddForm::HasSectionFooter(int sectionIndex)
222 {
223         return false;
224 }
225
226 bool
227 AddForm::HasSectionHeader(int sectionIndex)
228 {
229         return false;
230 }
231
232 void
233 AddForm::UpdateItem(int sectionIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem *pItem)
234 {
235 }
236
237 void
238 AddForm::OnSectionTableViewContextItemActivationStateChanged(Tizen::Ui::Controls::SectionTableView &tableView, int sectionIndex, int itemIndex, Tizen::Ui::Controls::TableViewContextItem *pContextItem, bool activated)
239 {
240 }
241
242 void
243 AddForm::OnSectionTableViewItemStateChanged(Tizen::Ui::Controls::SectionTableView &tableView, int sectionIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem *pItem, Tizen::Ui::Controls::TableViewItemStatus status)
244 {
245         AppLogDebug("sectionIndex: %d, itemIndex: %d", sectionIndex, itemIndex);
246         switch (sectionIndex)
247         {
248         case 0:
249         {
250                 if (!__isRunningAppControl)
251                 {
252                         AppId appId = __pPm->GetAccountProviderAppId(itemIndex);
253                         AppControl* pAppControl = AppManager::GetInstance()->FindAppControlN(appId, L"http://tizen.org/appcontrol/operation/account/add");
254                         if (pAppControl)
255                         {
256                                 result r = pAppControl->Start(null, null, null, this);
257                                 delete pAppControl;
258                                 TryReturnVoid(r == E_SUCCESS, "[%s] Failed to start appcontrol", GetErrorMessage(r));
259
260                                 __isRunningAppControl = true;
261                         }
262                         else
263                         {
264                                 AppLogException("[%s] Failed to find appcontrol", GetErrorMessage(GetLastResult()));
265                         }
266                 }
267                 break;
268         }
269         default:
270                 break;
271         }
272 }
273
274 void
275 AddForm::OnFocusGained(const Tizen::Ui::Control &source)
276 {
277         AppLogDebug("Enter");
278         __isRunningAppControl = false;
279 }
280
281 void
282 AddForm::OnFocusLost(const Tizen::Ui::Control &source)
283 {
284         AppLogDebug("Enter");
285 }
286
287 void
288 AddForm::OnAccountListChanged(void)
289 {
290         AppLogDebug("Enter");
291         __pSectionTableView->UpdateTableView();
292         if (__pSectionTableView->GetSectionCount() == 0)
293         {
294                 SetEmptyPanel();
295         }
296         else
297         {
298                 RemoveEmptyPanel();
299         }
300 }
301
302 void
303 AddForm::SetEmptyPanel(void)
304 {
305         if (!__pEmptyPanel)
306         {
307                 Panel* pEmptyPanel = new (std::nothrow) Panel();
308                 pEmptyPanel->Construct(IDL_PANEL_EMPTY_LIST);
309
310                 String emptyText;
311                 UiApp::GetInstance()->GetAppResource()->GetString(IDS_NO_ACCOUNT_PROVIDERS, emptyText);
312
313                 Label* pLabel = static_cast<Label*>(pEmptyPanel->GetControl(IDC_LABEL_TEXT));
314                 pLabel->SetText(emptyText);
315
316                 __pSectionTableView->AddControl(pEmptyPanel);
317                 __pSectionTableView->Invalidate(true);
318                 __pEmptyPanel = pEmptyPanel;
319         }
320 }
321
322 void
323 AddForm::RemoveEmptyPanel(void)
324 {
325         if (__pEmptyPanel)
326         {
327                 __pSectionTableView->RemoveControl(__pEmptyPanel);
328                 __pEmptyPanel = null;
329         }
330 }