fix to prevent multiple launch appcontrol.
[apps/osp/Account.git] / src / AccMainForm.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                AccMainForm.cpp
19  * @brief               This is the implementation file for MainForm class.
20  */
21
22 #include <FApp.h>
23 #include <FBase.h>
24 #include <FGraphics.h>
25 #include <FSocial.h>
26 #include <FUi.h>
27 #include "AccMainForm.h"
28 #include "AccTypes.h"
29 #include "AccAccountPresentationModel.h"
30
31 using namespace Tizen::App;
32 using namespace Tizen::Base;
33 using namespace Tizen::Base::Collection;
34 using namespace Tizen::Graphics;
35 using namespace Tizen::Social;
36 using namespace Tizen::Ui;
37 using namespace Tizen::Ui::Controls;
38 using namespace Tizen::Ui::Scenes;
39
40 static const int IDA_ADD_ACCOUNT = 20001;
41
42 MainForm::MainForm(void)
43         : __pPm(null)
44         , __pSectionTableView(null)
45         , __pEmptyPanel(null)
46         , __isRunningAppControl(false)
47 {
48 }
49
50 MainForm::~MainForm(void)
51 {
52 }
53
54 result
55 MainForm::Initialize(void)
56 {
57         return Construct(IDL_MAIN_FORM);
58 }
59
60 result
61 MainForm::OnInitializing(void)
62 {
63         AppLogDebug("ENTER");
64         AddFocusEventListener(*this);
65         SetFormBackEventListener(this);
66         GetFooter()->AddActionEventListener(*this);
67
68         __pPm = AccountPresentationModel::GetInstance();
69
70         __pSectionTableView = static_cast<SectionTableView*>(GetControl(IDC_SECTIONTABLEVIEW));
71         __pSectionTableView->SetItemProvider(this);
72         __pSectionTableView->AddSectionTableViewItemEventListener(*this);
73
74         return E_SUCCESS;
75 }
76
77 result
78 MainForm::OnTerminating(void)
79 {
80         AppLogDebug("ENTER");
81         return E_SUCCESS;
82 }
83
84 void
85 MainForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId,const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
86 {
87         AppLogDebug("ENTER");
88         __pPm->AddAccountChangedEventListener(*this);
89         __pSectionTableView->UpdateTableView();
90         if (__pSectionTableView->GetSectionCount() == 0)
91         {
92                 SetEmptyPanel();
93         }
94         else
95         {
96                 RemoveEmptyPanel();
97         }
98 }
99
100 void
101 MainForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId,const Tizen::Ui::Scenes::SceneId& nextSceneId)
102 {
103         AppLogDebug("ENTER");
104         __pPm->RemoveAccountChangedEventListener(*this);
105 }
106
107 void
108 MainForm::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
109 {
110         AppLogDebug("actionId: %d", actionId);
111         if (!__isRunningAppControl)
112         {
113                 switch(actionId)
114                 {
115                 case IDA_ADD_ACCOUNT:
116                                  SceneManager::GetInstance()->GoForward(SceneTransitionId(ID_SCNT_ACCOUNT_ADD));
117                         break;
118                 default:
119                         break;
120                 }
121         }
122 }
123
124 void
125 MainForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
126 {
127         AppLogDebug("ENTER");
128         UiApp::GetInstance()->Terminate();
129 }
130
131 Tizen::Ui::Controls::TableViewItem*
132 MainForm::CreateItem(int sectionIndex, int itemIndex, int itemWidth)
133 {
134         AppLogDebug("itemWidth: %d, sectionIndex: %d, itemIndex: %d", itemWidth, sectionIndex, itemIndex);
135
136         RelativeLayout layout;
137         layout.Construct();
138
139         TableViewItem* pItem = new (std::nothrow) TableViewItem();
140         pItem->Construct(layout, Dimension(itemWidth, 140));
141
142         Panel* pPanel = new (std::nothrow) Panel();
143         pPanel->Construct(IDL_PANEL_MULTI_LINE_ITEM);
144
145         Label* pMainLabel = static_cast<Label*>(pPanel->GetControl(IDC_LABEL_MAIN_TEXT));
146         pMainLabel->SetText(__pPm->GetAccountInfo(ACCOUNT_INFO_TYPE_USER_NAME, itemIndex));
147
148         Label* pSubLabel = static_cast<Label*>(pPanel->GetControl(IDC_LABEL_SUB_TEXT));
149         pSubLabel->SetText(__pPm->GetAccountInfo(ACCOUNT_INFO_TYPE_PROVIDER_NAME, itemIndex));
150
151         Bitmap* pIcon = __pPm->GetAccountIconN(itemIndex);
152         if (!pIcon)
153         {
154                 pIcon = UiApp::GetInstance()->GetAppResource()->GetBitmapN(L"A01_2_title_Icon_none.png");
155         }
156
157         Label* pIconLabel = static_cast<Label*>(pPanel->GetControl(IDC_LABEL_ICON));
158         pIconLabel->SetBackgroundBitmap(*pIcon);
159         delete pIcon;
160
161         pItem->AddControl(pPanel);
162
163         layout.SetHorizontalFitPolicy(*pPanel, FIT_POLICY_PARENT);
164         layout.SetVerticalFitPolicy(*pPanel, FIT_POLICY_PARENT);
165         layout.Update();
166
167         return pItem;
168 }
169
170 bool
171 MainForm::DeleteItem(int sectionIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem *pItem)
172 {
173         delete pItem;
174         return true;
175 }
176
177 int
178 MainForm::GetDefaultItemHeight(void)
179 {
180         return 0;
181 }
182
183 int
184 MainForm::GetItemCount(int sectionIndex)
185 {
186         AppLogDebug("sectionIndex: %d", sectionIndex);
187         switch (sectionIndex)
188         {
189         case 0:
190                 return __pPm->GetAccountCount();
191         default:
192                 return 0;
193         }
194 }
195
196 int
197 MainForm::GetSectionCount(void)
198 {
199         if (__pPm->GetAccountCount() == 0)
200         {
201                 return 0;
202         }
203         else
204         {
205                 return 1;
206         }
207 }
208
209 Tizen::Base::String
210 MainForm::GetSectionFooter(int sectionIndex)
211 {
212         return L"";
213 }
214
215 Tizen::Base::String
216 MainForm::GetSectionHeader(int sectionIndex)
217 {
218         switch (sectionIndex)
219         {
220         case 0:
221         {
222                 String header;
223                 UiApp::GetInstance()->GetAppResource()->GetString(IDS_EMAIL_SK3_ACCOUNT_LIST, header);
224                 return header;
225         }
226         default:
227                 return L"";
228         }
229 }
230
231 bool
232 MainForm::HasSectionFooter(int sectionIndex)
233 {
234         return false;
235 }
236
237 bool
238 MainForm::HasSectionHeader(int sectionIndex)
239 {
240         switch (sectionIndex)
241         {
242         case 0:
243                 return true;
244         default:
245                 return false;
246         }
247 }
248
249 void
250 MainForm::UpdateItem(int sectionIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem *pItem)
251 {
252 }
253
254 void
255 MainForm::OnSectionTableViewContextItemActivationStateChanged(Tizen::Ui::Controls::SectionTableView &tableView, int sectionIndex, int itemIndex, Tizen::Ui::Controls::TableViewContextItem *pContextItem, bool activated)
256 {
257         AppLogDebug("Enter");
258 }
259
260 void
261 MainForm::OnSectionTableViewItemStateChanged(Tizen::Ui::Controls::SectionTableView &tableView, int sectionIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem *pItem, Tizen::Ui::Controls::TableViewItemStatus status)
262 {
263         AppLogDebug("sectionIndex: %d, itemIndex: %d", sectionIndex, itemIndex);
264         switch (sectionIndex)
265         {
266         case 0:
267         {
268                 if (!__isRunningAppControl)
269                 {
270                         AppId appId = __pPm->GetAccountAppId(itemIndex);
271                         AppControl* pAppControl = AppManager::GetInstance()->FindAppControlN(appId, L"http://tizen.org/appcontrol/operation/account/configure");
272                         if (pAppControl)
273                         {
274                                 HashMap argMap(SingleObjectDeleter);
275                                 argMap.Construct();
276                                 argMap.Add(new (std::nothrow) String(L"http://tizen.org/appcontrol/data/account/id"), new (std::nothrow) String(Integer::ToString(__pPm->GetAccountId(itemIndex))));
277
278                                 result r = pAppControl->Start(null, null, &argMap, null);
279                                 delete pAppControl;
280                                 TryReturnVoid(r == E_SUCCESS, "[%s] Failed to start appcontrol", GetErrorMessage(r));
281                                 __isRunningAppControl = true;
282                         }
283                         else
284                         {
285                                 AppLogException("[%s] Failed to find appcontrol", GetErrorMessage(GetLastResult()));
286                         }
287                 }
288                 break;
289         }
290         default:
291                 break;
292         }
293 }
294
295 void
296 MainForm::OnFocusGained(const Tizen::Ui::Control &source)
297 {
298         AppLogDebug("Enter");
299         __isRunningAppControl = false;
300 }
301
302 void
303 MainForm::OnFocusLost(const Tizen::Ui::Control &source)
304 {
305         AppLogDebug("Enter");
306 }
307
308 void
309 MainForm::OnAccountListChanged(void)
310 {
311         AppLogDebug("Enter");
312         __pSectionTableView->UpdateTableView();
313         if (__pSectionTableView->GetSectionCount() == 0)
314         {
315                 SetEmptyPanel();
316         }
317         else
318         {
319                 RemoveEmptyPanel();
320         }
321 }
322
323 void
324 MainForm::SetEmptyPanel(void)
325 {
326         if (!__pEmptyPanel)
327         {
328                 Panel* pEmptyPanel = new (std::nothrow) Panel();
329                 pEmptyPanel->Construct(IDL_PANEL_EMPTY_LIST);
330                 __pSectionTableView->AddControl(pEmptyPanel);
331                 __pSectionTableView->Invalidate(true);
332                 __pEmptyPanel = pEmptyPanel;
333         }
334 }
335
336 void
337 MainForm::RemoveEmptyPanel(void)
338 {
339         if (__pEmptyPanel)
340         {
341                 __pSectionTableView->RemoveControl(__pEmptyPanel);
342                 __pEmptyPanel = null;
343         }
344 }