Applied latest source code
[apps/native/preloaded/MusicPlayer.git] / src / MpSetAsPopup.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.1 (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                MpSetAsPopup.cpp
19  * @brief               This is the implementation file for SetAsPopup class.
20  */
21
22 #include <FBase.h>
23 #include <FSclAddressbook.h>
24 #include <FSclAddressbookManager.h>
25 #include <FSclContact.h>
26 #include <FSysSettingInfo.h>
27 #include "MpMusicPlayerApp.h"
28 #include "MpResourceManager.h"
29 #include "MpSetAsPopup.h"
30 #include "MpTypes.h"
31
32 using namespace Tizen::App;
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Collection;
35 using namespace Tizen::Base::Runtime;
36 using namespace Tizen::Graphics;
37 using namespace Tizen::Social;
38 using namespace Tizen::System;
39 using namespace Tizen::Ui;
40 using namespace Tizen::Ui::Controls;
41 using namespace Tizen::Ui::Scenes;
42
43 enum SetAsId
44 {
45         SET_AS_CALL_RINGTONE = 0,
46         SET_AS_CALLER_RINGTONE,
47         SET_AS_END
48 };
49
50 SetAsPopup::SetAsPopup(void)
51         : __pSetAsItemTableView(null)
52         , __pCancelButton(null)
53         , __pControl(null)
54 {
55         AppLogDebug("ENTER");
56         AppLogDebug("EXIT");
57 }
58
59 SetAsPopup::~SetAsPopup(void)
60 {
61         AppLogDebug("ENTER");
62         AppLogDebug("EXIT");
63 }
64
65 result
66 SetAsPopup::Initialize(Tizen::Ui::Control* pControl)
67 {
68         AppLogDebug("ENTER");
69         result r = E_SUCCESS;
70         r = Construct(IDL_SET_AS_POPUP);
71         TryCatch(r == E_SUCCESS, r = E_OUT_OF_MEMORY, "Unable to construct popup.\n");
72         SetTitleText(ResourceManager::GetString("IDS_COM_SK_SET_AS"));
73         AddTouchEventListener(*this);
74         SetPropagatedKeyEventListener(this);
75         __pSetAsItemTableView = static_cast<TableView*>(GetControl(IDC_TABLEVIEW));
76         TryCatch(__pSetAsItemTableView != null, r = E_OUT_OF_MEMORY, "Unable to construct table view.\n");
77         __pSetAsItemTableView->AddTableViewItemEventListener(*this);
78         __pSetAsItemTableView->SetItemProvider(this);
79
80         __pCancelButton = static_cast<Button*>(GetControl(IDC_CANCEL_BUTTON));
81         TryCatch(__pCancelButton != null, r = E_OUT_OF_MEMORY, "Unable to construct cancel button.\n");
82         __pCancelButton->SetActionId(IDA_BUTTON_CANCEL);
83         __pCancelButton->AddActionEventListener(*this);
84
85         __pControl = pControl;
86
87         AppLogDebug("EXIT");
88         return r;
89
90 CATCH:
91         //AppLogException("[%ls]", GetErrorMessage(r));
92         if (__pCancelButton != null)
93         {
94                 delete __pCancelButton;
95                 __pCancelButton = null;
96         }
97         if ( __pSetAsItemTableView != null)
98         {
99                 delete __pSetAsItemTableView;
100                 __pSetAsItemTableView = null;
101         }
102         return r;
103 }
104
105 void
106 SetAsPopup::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
107 {
108         AppLogDebug("ENTER");
109         if (actionId == IDA_BUTTON_CANCEL)
110         {
111                 SetShowState(false);
112                 Invalidate(true);
113         }
114         AppLogDebug("EXIT");
115 }
116
117 void
118 SetAsPopup::OnTouchDoublePressed(const Tizen::Ui::Control& source,
119                                         const Tizen::Graphics::Point& currentPosition,
120                                         const Tizen::Ui::TouchEventInfo& touchInfo)
121 {
122         AppLogDebug("ENTER");
123         AppLogDebug("EXIT");
124 }
125
126 void
127 SetAsPopup::OnTouchLongPressed(const Tizen::Ui::Control& source,
128                                         const Tizen::Graphics::Point& currentPosition,
129                                         const Tizen::Ui::TouchEventInfo& touchInfo)
130 {
131         AppLogDebug("ENTER");
132         AppLogDebug("EXIT");
133 }
134
135 void
136 SetAsPopup::OnTouchFocusIn(const Tizen::Ui::Control& source,
137                                         const Tizen::Graphics::Point& currentPosition,
138                                         const Tizen::Ui::TouchEventInfo& touchInfo)
139 {
140         AppLogDebug("ENTER");
141         AppLogDebug("EXIT");
142 }
143
144 void
145 SetAsPopup::OnTouchFocusOut(const Tizen::Ui::Control& source,
146                                         const Tizen::Graphics::Point& currentPosition,
147                                         const Tizen::Ui::TouchEventInfo& touchInfo)
148 {
149         AppLogDebug("ENTER");
150         AppLogDebug("EXIT");
151 }
152
153 void
154 SetAsPopup::OnTouchMoved(const Tizen::Ui::Control& source,
155                                         const Tizen::Graphics::Point& currentPosition,
156                                         const Tizen::Ui::TouchEventInfo& touchInfo)
157 {
158         AppLogDebug("ENTER");
159         AppLogDebug("EXIT");
160 }
161
162 void
163 SetAsPopup::OnTouchPressed(const Tizen::Ui::Control& source,
164                                         const Tizen::Graphics::Point& currentPosition,
165                                         const Tizen::Ui::TouchEventInfo& touchInfo)
166 {
167         AppLogDebug("ENTER");
168         if (GetShowState()
169                 && (currentPosition.x < 0
170                 || currentPosition.y < 0
171                 || this->GetBounds().width
172                 || this->GetBounds().height))
173         {
174 //              SetShowState(false);
175 //              Invalidate(true);
176         }
177         AppLogDebug("EXIT");
178 }
179
180 void
181 SetAsPopup::OnTouchReleased(const Tizen::Ui::Control& source,
182                                         const Tizen::Graphics::Point& currentPosition,
183                                         const Tizen::Ui::TouchEventInfo& touchInfo)
184 {
185         AppLogDebug("ENTER");
186         AppLogDebug("EXIT");
187 }
188
189 bool
190 SetAsPopup::OnKeyPressed(Control& source, const KeyEventInfo& keyEventInfo)
191 {
192         AppLogDebug("ENTER");
193         if (KEY_BACK == keyEventInfo.GetKeyCode() || KEY_ESC == keyEventInfo.GetKeyCode())
194         {
195                 return true;
196         }
197         AppLogDebug("EXIT");
198         return false;
199 }
200
201 bool
202 SetAsPopup::OnKeyReleased(Control& source, const KeyEventInfo& keyEventInfo)
203 {
204         AppLogDebug("ENTER");
205         if (KEY_BACK == keyEventInfo.GetKeyCode() || KEY_ESC == keyEventInfo.GetKeyCode())
206         {
207                 SetShowState(false);
208                 Show();
209                 return true;
210         }
211
212         AppLogDebug("EXIT");
213         return false;
214
215 }
216
217 int
218 SetAsPopup::GetItemCount(void)
219 {
220         AppLogDebug("ENTER");
221         AppLogDebug("EXIT");
222         return SET_AS_END;
223 }
224
225 Tizen::Ui::Controls::TableViewItem*
226 SetAsPopup::CreateItem(const int itemIndex, int itemWidth)
227 {
228         AppLogDebug("ENTER");
229         RelativeLayout layout;
230         layout.Construct();
231
232         TableViewItem* pItem = new (std::nothrow) TableViewItem();
233
234         result r = pItem->Construct(Dimension(itemWidth, ITEM_HEIGHT), TABLE_VIEW_ANNEX_STYLE_NORMAL);
235         TryCatch(r == E_SUCCESS, delete pItem, "pItem->Construct(%s)", GetErrorMessage(r));
236
237         if (itemIndex < SET_AS_END)
238         {
239                 String labelName = L"";
240                 switch (itemIndex)
241                 {
242                 case SET_AS_CALL_RINGTONE:
243                         {
244                                 labelName = ResourceManager::GetString(L"IDS_MUSIC_OPT_CALL_RINGTONE");
245                         }
246                         break;
247
248                 case SET_AS_CALLER_RINGTONE:
249                         {
250                                 labelName = ResourceManager::GetString(L"IDS_MUSIC_OPT_SETAS_CALLER_RINGTONE");
251                         }
252                         break;
253
254                 default:
255                         break;
256                 }
257
258                 r = CreateTableViewItem(*pItem, labelName);
259                 TryCatch(r == E_SUCCESS, delete pItem, "CreateTableViewItem failed(%s)", GetErrorMessage(r));
260         }
261
262         AppLogDebug("EXIT");
263         return pItem;
264
265 CATCH:
266         AppLogDebug("EXIT(%ls)", GetErrorMessage(GetLastResult()));
267         return null;
268 }
269
270 bool
271 SetAsPopup::DeleteItem(const int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem)
272 {
273         AppLogDebug("ENTER");
274         AppLogDebug("EXIT");
275         return false;
276 }
277
278 void
279 SetAsPopup::UpdateItem(int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem)
280 {
281         AppLogDebug("ENTER");
282         AppLogDebug("EXIT");
283 }
284
285 int
286 SetAsPopup::GetDefaultItemHeight(void)
287 {
288         AppLogDebug("ENTER");
289         AppLogDebug("EXIT");
290         return ITEM_HEIGHT;
291 }
292
293 void
294 SetAsPopup::OnTableViewItemStateChanged(Tizen::Ui::Controls::TableView& tableView,
295                                                 int itemIndex,
296                                                 Tizen::Ui::Controls::TableViewItem* pItem,
297                                                 Tizen::Ui::Controls::TableViewItemStatus status)
298 {
299         AppLogDebug("ENTER");
300         result r = E_FAILURE;
301         if (itemIndex == 0)
302         {
303                 result r = Tizen::System::SettingInfo::SetValue(CALL_RINGTONE_KEY, *__pCurrentContentPath);
304                 if (r == E_SUCCESS)
305                 {
306                         SetShowState(false);
307                         Invalidate(true);
308
309                         MessageBox messageBox;
310                         messageBox.Construct(L"", ResourceManager::GetString(L"IDS_COM_POP_SUCCESS"),
311                                         MSGBOX_STYLE_OK,
312                                         3000);
313                         int modalResult = 0;
314                         messageBox.ShowAndWait(modalResult);
315                 }
316         }
317         else
318         {
319                 AppControl* pAppControl = AppManager::FindAppControlN(PROVIDER_ID_CONTACT, OPERATION_ID_SOCIAL_PICK);
320                 if (pAppControl == null)
321                 {
322                         return;
323                 }
324
325                 HashMap* pDataList = new (std::nothrow) HashMap(SingleObjectDeleter);
326                 pDataList->Construct();
327                 pDataList->Add(new (std::nothrow) String(APPCONTROL_KEY_SELECTION_MODE), new (std::nothrow) String(APPCONTROL_OPTION_SINGLE));
328                 pDataList->Add(new (std::nothrow) String(APPCONTROL_KEY_SOCIAL_ITEM_TYPE), new (std::nothrow) String(APPCONTROL_OPTION_PERSON));
329                 pDataList->Add(new (std::nothrow) String(APPCONTROL_KEY_SOCIAL_RESULT_TYPE), new (std::nothrow) String(APPCONTROL_OPTION_ITEM_ID));
330
331 //              Tizen::Base::Collection::ArrayList* pData = new (std::nothrow) Tizen::Base::Collection::ArrayList();
332 //              pData->Construct();
333 //              pData->Add(*(new (std::nothrow) String(SELECTION_MODE)));
334 //              pData->Add(*(new (std::nothrow) String(RETURN_TYPE)));
335
336                 r = pAppControl->Start(null, null, pDataList, this);
337                 if (r == E_SUCCESS)
338                 {
339                         Frame* pFrame = MusicPlayerApp::GetInstance()->GetFrameAt(0);
340                         AppAssert(pFrame);
341                         pFrame->SetEnabled(false);
342                 }
343                 else
344                 {
345                         AppLogDebug("AppControl Start fail");
346                 }
347                 SetShowState(false);
348                 Invalidate(true);
349
350                 pDataList->RemoveAll(true);
351                 delete pDataList;
352                 pDataList = null;
353                 delete pAppControl;
354         }
355         AppLogDebug("EXIT");
356 }
357
358 void
359 SetAsPopup::OnTableViewContextItemActivationStateChanged(Tizen::Ui::Controls::TableView& tableView,
360                                                                                 int itemIndex,
361                                                                                 Tizen::Ui::Controls::TableViewContextItem* pContextItem,
362                                                                                 bool activated)
363 {
364         AppLogDebug("ENTER");
365         AppLogDebug("EXIT");
366 }
367
368 void
369 SetAsPopup::OnTableViewItemReordered(Tizen::Ui::Controls::TableView& tableView,
370                                                         int itemIndexFrom,
371                                                         int itemIndexTo)
372 {
373         AppLogDebug("ENTER");
374         AppLogDebug("EXIT");
375 }
376
377 result
378 SetAsPopup::CreateTableViewItem(Tizen::Ui::Container& parent,
379                                 Tizen::Base::String& labelName)
380 {
381         AppLogDebug("ENTER");
382         result r = E_SUCCESS;
383
384         Label* pStrLabel = new (std::nothrow) Label();
385         if (pStrLabel == null)
386         {
387                 return E_INIT_FAILED;
388         }
389
390         r = pStrLabel->Construct(Rectangle(0, 0, POPUP_ITEM_WIDTH, ITEM_HEIGHT), L"");
391         if (r != E_SUCCESS)
392         {
393                 return E_INIT_FAILED;
394         }
395
396         pStrLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
397         pStrLabel->SetTextConfig(32, LABEL_TEXT_STYLE_NORMAL);
398         pStrLabel->SetText(labelName);
399
400         parent.AddControl(pStrLabel);
401
402         AppLogDebug("EXIT");
403         return r;
404 }
405
406 void
407 SetAsPopup::OnAppControlCompleteResponseReceived(const AppId& appId,
408                 const String& operationId, AppCtrlResult appControlResult,
409                 const IMap* pExtraData)
410 {
411         AppLogDebug("ENTER");
412         if (pExtraData == null)
413         {
414                 AppLogDebug("End");
415                 return;
416         }
417
418         AppLogDebug("%ls, %ls", appId.GetPointer(), operationId.GetPointer());
419         if (appId.Equals(String(PROVIDER_ID_CONTACT))/* && operationId.Equals(String(OPERATION_ID_PICK))*/)
420         {
421                 if (appControlResult == APP_CTRL_RESULT_SUCCEEDED)
422                 {
423
424                         int contactId = -1;
425                         String* pKey = null;
426                         ArrayList* pArrayListValue = null;
427                         String* pResultString = null;
428                         result r = E_SUCCESS;
429                         if (pExtraData != null)
430                         {
431                                 IMapEnumerator* pEnum = pExtraData->GetMapEnumeratorN();
432                                 while (pEnum->MoveNext() == E_SUCCESS)
433                                 {
434                                         pKey = static_cast<String*>(pEnum->GetKey());
435                                         if (pKey->Equals(APPCONTROL_KEY_SOCIAL_ITEM_ID, true))
436                                         {
437                                                 pArrayListValue = static_cast<ArrayList*>(pEnum->GetValue());
438                                         }
439                                 }
440                                 delete pEnum;
441
442                                 if (pArrayListValue != null && pArrayListValue->GetCount() > 0)
443                                 {
444                                         for (int iCount = 0; iCount < pArrayListValue->GetCount(); iCount++)
445                                         {
446                                                 pResultString = static_cast<String*>(pArrayListValue->GetAt(iCount));
447                                                 AppLogDebug("extraData : %ls:%ls total contacts %d", pKey->GetPointer(), pResultString->GetPointer(),pArrayListValue->GetCount());
448                                                 Integer::Parse(*pResultString, contactId);
449                                                 pResultString->Clear();
450                                                 if (contactId < 0)
451                                                 {
452                                                         AppLogDebug("pKey is invaild");
453                                                         break;
454                                                 }
455
456                                                 AddressbookManager* pAddressbookManager = AddressbookManager::GetInstance();
457                                                 Addressbook* pAddressbook = pAddressbookManager->GetAddressbookN(DEFAULT_ADDRESSBOOK_ID);
458
459                                                 Contact* pContact = pAddressbook->GetContactN(contactId);
460                                                 pContact->SetValue(CONTACT_PROPERTY_ID_RINGTONE, *__pCurrentContentPath);
461                                                 r = pAddressbook->UpdateContact(*pContact);
462                                                 delete pContact;
463                                                 pContact = null;
464
465                                                 delete pAddressbook;
466                                                 pAddressbook = null;
467                                         }
468                                 }
469
470                         }
471
472                         else
473                         {
474                                 AppLogDebug("pExtraData is null");
475                                 return;
476                         }
477
478                         if (r == E_SUCCESS)
479                         {
480                                 SceneManager* pSceneManager = SceneManager::GetInstance();
481                                 if (pSceneManager != null)
482                                 {
483                                         pSceneManager->GetCurrentScene()->GetForm()->SendUserEvent(ID_SHOW_SUCCESS_MESSAGE,null);
484                                 }
485                         }
486
487                 }
488         }
489         AppLogDebug("EXIT");
490 }
491
492 void
493 SetAsPopup::SetCurrentContentPath(Tizen::Base::String* contentPath)
494 {
495         AppLogDebug("ENTER");
496         __pCurrentContentPath = contentPath;
497         AppLogDebug("EXIT");
498 }