2183e8b16243cb2d84bc152289ab22c295bac448
[apps/osp/Internet.git] / src / IntSharePopup.cpp
1 //
2
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 /*
19  * IntSharePopUp.cpp
20  *@file:        IntSharePopup.cpp
21  *@brief:       Share Popup for sharing bookmark via message, email etc
22  */
23
24 #include <FAppUiApp.h>
25 #include <FUi.h>
26 #include <FBase.h>
27 #include <FIo.h>
28
29 #include "IntCommonLib.h"
30 #include "IntSharePopup.h"
31
32 using namespace Tizen::App;
33 using namespace Tizen::Io;
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Collection;
36 using namespace Tizen::Base::Runtime;
37 using namespace Tizen::Graphics;
38 using namespace Tizen::Ui;
39 using namespace Tizen::Ui::Controls;
40 using namespace Tizen::Ui::Scenes;
41
42
43 const int SharePopup::IDA_CANCEL_BUTTON = 101;
44 const int SharePopup::ID_FORMAT_MESSAGE_STRING = 500;
45 const int SharePopup::ID_FORMAT_EMAIL_STRING = 501;
46 const int SharePopup::ID_FORMAT_FACEBOOK_STRING = 502;
47 const int SharePopup::ID_FORMAT_TWITTER_STRING = 503;
48
49 ShareInfo::     ShareInfo(void)
50 {
51         isImageAttached = false;
52 }
53
54 ShareInfo::     ~ShareInfo(void)
55 {
56 }
57
58 void
59 ShareInfo::SetPageTitle(Tizen::Base::String aPageTitle)
60 {
61         __pageTitle.Clear();
62         __pageTitle.Append(aPageTitle);
63 }
64
65 void
66 ShareInfo::SetPageUrl(Tizen::Base::String aPageURL)
67 {
68         __pageURL.Clear();
69         __pageURL.Append(aPageURL);
70 }
71
72 Tizen::Base::String
73 ShareInfo::GetPageTitle(void)
74 {
75         return __pageTitle;
76 }
77
78 Tizen::Base::String
79 ShareInfo::GetPageURL(void)
80 {
81         return __pageURL;
82 }
83
84 void
85 ShareInfo::SetImageAttached(bool imageAttached)
86 {
87         isImageAttached = imageAttached;
88 }
89
90 bool
91 ShareInfo::GetImageAttached()
92 {
93         return isImageAttached;
94 }
95
96 void
97 ShareInfo::SetImagePath(Tizen::Base::String aImagePath)
98 {
99         __imagePath.Clear();
100         __imagePath.Append(aImagePath);
101 }
102
103 Tizen::Base::String
104 ShareInfo::GetImagePath(void)
105 {
106         return __imagePath;
107 }
108
109 SharePopup::SharePopup(void)
110 :__pList(null),__pShareList(null)
111 {
112         __appControlOngoing = false;
113         __pTimer = null;
114 }
115
116 SharePopup::~SharePopup(void)
117 {
118         ShareInfo* pShareInfo = null;
119         result r = E_FAILURE;
120
121         if (__pShareList != null)
122         {
123                 pShareInfo = dynamic_cast<ShareInfo*>(__pShareList->GetAt(0));
124         }
125
126         String attachVal;
127         bool imageAttachment = false;
128         if (pShareInfo != null)
129         {
130                 imageAttachment = pShareInfo->GetImageAttached();
131                 if (imageAttachment)
132                 {
133                         attachVal = pShareInfo->GetImagePath();
134                         AppLog("SharePopup:: imagePath is %S",attachVal.GetPointer());
135                         r = File::Remove(attachVal);
136                         if(r == E_SUCCESS)
137                         {
138                                 AppLog("removed success");
139                         }
140                         else
141                         {
142                                 AppLog("removed failure");
143                         }
144                 }
145                 __pShareList->RemoveAll(true);
146         }
147
148 }
149
150 bool
151 SharePopup::Initialize(void)
152 {
153         Button* pCancelButton = null;
154
155         Popup::Construct(L"IDL_SHARE_POPUP");
156         SetName(L"CommonPopup");
157
158         __pShareList = new(std::nothrow) ArrayList();
159         __pShareList->Construct();
160
161         __pList = static_cast<ListView*>(GetControl(L"IDC_LISTVIEW"));
162         if (__pList == null)
163         {
164                 return false;
165         }
166
167         __pList->SetItemProvider(*this);
168         __pList->AddListViewItemEventListener(*this);
169
170         pCancelButton = static_cast< Button* >(GetControl(L"IDC_BUTTON1", true));
171         if (pCancelButton)
172         {
173                 pCancelButton->AddActionEventListener(*this);
174                 pCancelButton->SetActionId(IDA_CANCEL_BUTTON);
175         }
176
177         __pTimer = new(std::nothrow) Timer();
178         __pTimer->Construct(*this);
179         SetPropagatedKeyEventListener(this);
180         return true;
181 }
182
183 result
184 SharePopup::OnTerminating(void)
185 {
186         result r = E_SUCCESS;
187         return r;
188 }
189
190 void
191 SharePopup::OnTimerExpired(Timer& timer)
192 {
193         Popup::SetShowState(false);
194         Popup::Show();
195 }
196
197 void
198 SharePopup::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
199 {
200         switch(actionId)
201         {
202         case IDA_CANCEL_BUTTON:
203         {
204                 Popup::SetShowState(false);
205                 Popup::Show();
206         }
207         break;
208         default:
209                 break;
210         }
211         return;
212 }
213
214 void
215 SharePopup::OnListViewContextItemStateChanged(Tizen::Ui::Controls::ListView& listView, int index, int elementId, Tizen::Ui::Controls::ListContextItemStatus state)
216 {
217         return;
218 }
219
220 void
221 SharePopup::OnListViewItemStateChanged(Tizen::Ui::Controls::ListView& listView, int index, int elementId, Tizen::Ui::Controls::ListItemStatus status)
222 {
223         switch (index)
224         {
225         case 0:
226         {
227                 if(__appControlOngoing == false)
228                 {
229                         __appControlOngoing = true;
230                         // share via message
231                         StartMessageAppControl();
232                         if(__pTimer == null)
233                         {
234                                 __pTimer = new(std::nothrow) Timer();
235                                 __pTimer->Construct(*this);
236                         }
237                         __pTimer->Start(2500);
238                 }
239         }
240         break;
241         case 1:
242         {
243                 if(__appControlOngoing == false)
244                 {
245                         __appControlOngoing = true;
246                         // share via email
247                         StartEmailAppControl();
248                         if(__pTimer == null)
249                         {
250                                 __pTimer = new(std::nothrow) Timer();
251                                 __pTimer->Construct(*this);
252                         }
253                         __pTimer->Start(2500);
254                 }
255
256         }
257         break;
258         default:
259                 break;
260         }
261         Frame* pCurrentFrame = null;
262         pCurrentFrame = Application::GetInstance()->GetAppFrame()->GetFrame();
263
264         if (pCurrentFrame != null)
265         { pCurrentFrame->SetEnabled(false); }
266 }
267
268 void
269 SharePopup::OnListViewItemSwept(Tizen::Ui::Controls::ListView& listView, int index, Tizen::Ui::Controls::SweepDirection direction)
270 {
271         return;
272 }
273
274 void
275 SharePopup::OnListViewItemLongPressed(Tizen::Ui::Controls::ListView& listView, int index, int elementId, bool& invokeListViewItemCallback)
276 {
277         return;
278 }
279
280 Tizen::Ui::Controls::ListItemBase*
281 SharePopup::CreateItem(int index, int itemWidth)
282 {
283         result r = E_FAILURE;
284         ListAnnexStyle style = LIST_ANNEX_STYLE_NORMAL;
285         int textSize = 35;
286         int elementHeight = 50;
287         int listItemHeight = 75;
288
289         CustomItem* pItem = new(std::nothrow) CustomItem();
290         r = pItem->Construct(Dimension(GetClientAreaBounds().width, listItemHeight), style);
291         if (IsFailed(r))
292         {
293                 AppLogDebug("Create Item Failed with error %s", GetErrorMessage(r));
294                 delete pItem;
295                 return null;
296         }
297
298         switch(index)
299         {
300         case 0:
301         {
302                 AppLogDebug("SharePopUp CreateItem 0");
303                 // IDS_SHARE_VIA_MESSAGE
304                 pItem->AddElement(Rectangle(45,0, GetClientAreaBounds().width, listItemHeight), ID_FORMAT_MESSAGE_STRING, CommonUtil::GetString(L"IDS_BR_OPT_MESSAGES"), true);
305         }
306         break;
307         case 1:
308         {
309                 AppLogDebug("SharePopUp CreateItem 1");
310                 // IDS_SHARE_VIA_EMAIL
311                 pItem->AddElement(Rectangle(45, 0, GetClientAreaBounds().width, listItemHeight), ID_FORMAT_EMAIL_STRING, CommonUtil::GetString(L"IDS_BR_OPT_SENDURLVIA_EMAIL"), true);
312         }
313         break;
314         default:
315                 break;
316         }
317         return pItem;
318 }
319
320 bool
321 SharePopup::DeleteItem(int index, Tizen::Ui::Controls::ListItemBase* pItem, int itemWidth)
322 {
323         return true;
324 }
325
326 int
327 SharePopup::GetItemCount(void)
328 {
329         return 2;
330 }
331
332 result
333 SharePopup::AddShareInfo(ShareInfo* pShareInfo)
334 {
335         result r = E_FAILURE;
336
337         if (__pShareList != NULL)
338         {
339                 r = __pShareList->Add(*pShareInfo);
340         }
341         return r;
342 }
343
344 void SharePopup::RemoveAllShareInfo()
345 {
346         __pShareList->RemoveAll();
347 }
348
349 void
350 SharePopup::StartEmailAppControl(void)
351 {
352    HashMap extraData;
353    ShareInfo* pShareInfo = null;
354    result r = E_FAILURE;
355    bool imageAttachment = false;
356
357    extraData.Construct();
358
359    if (__pShareList != null)
360         {
361                 pShareInfo = dynamic_cast<ShareInfo*>(__pShareList->GetAt(0));
362                 if (pShareInfo != null)
363                 {
364                         String testURL = pShareInfo->GetPageURL();
365                         AppLogDebug("getpageURL getpageURL is %ls", testURL.GetPointer());
366                 }
367         }
368
369    String textVal;
370
371         if (pShareInfo != null)
372         {
373                 textVal.Append(pShareInfo->GetPageTitle());
374                 textVal.Append(L" <");
375                 AppLog("share info url is %ls",pShareInfo->GetPageURL().GetPointer());
376                 textVal.Append(pShareInfo->GetPageURL().GetPointer());
377                 textVal.Append(L">");
378         }
379
380    String subjectKey = L"http://tizen.org/appcontrol/data/subject";
381    String subjectVal = L"";
382    String textKey = L"http://tizen.org/appcontrol/data/text";
383
384    String toKey = L"http://tizen.org/appcontrol/data/to";
385    String toVal = L"";
386    String ccKey = L"http://tizen.org/appcontrol/data/cc";
387    String ccVal = L"";
388    String bccKey = L"http://tizen.org/appcontrol/data/bcc";
389    String bccVal = L"";
390    String resultKey = L"http://tizen.org/appcontrol/data/return_result";
391    String resultVal = "true";
392
393
394    //String attachKey = L"attachments";
395    String attachKey = L"http://tizen.org/appcontrol/data/path";
396    String attachVal;
397         if (pShareInfo != null)
398         {
399                 imageAttachment = pShareInfo->GetImageAttached();
400                 if (imageAttachment)
401                 {
402                         attachVal = pShareInfo->GetImagePath();
403                         AppLog("SharePopup::StartEmailAppControl imagePath is %S",attachVal.GetPointer());
404                 }
405                 else
406                 {
407                          attachVal = L"";
408                 }
409         }
410
411         ArrayList* pDataList = null;
412         pDataList = new (std::nothrow) ArrayList();
413         pDataList->Construct();
414         pDataList->Add(&attachVal);
415
416    extraData.Add(&subjectKey, &subjectVal);
417    extraData.Add(&textKey, &textVal);
418    extraData.Add(&toKey, &toVal);
419    extraData.Add(&ccKey, &ccVal);
420    extraData.Add(&bccKey, &bccVal);
421    extraData.Add(&resultKey, &resultVal);
422    if(attachVal.GetLength() > 0)
423    {
424            extraData.Add(&attachKey, pDataList);
425    }
426
427    AppControl* pAc = AppManager::FindAppControlN(L"tizen.email", L"http://tizen.org/appcontrol/operation/compose");
428    if (pAc)
429    {
430           r = pAc->Start(null, null, &extraData, this);
431            delete pAc;
432    }
433
434    delete pDataList;
435 }
436
437 void
438 SharePopup::StartMessageAppControl(void)
439 {
440         HashMap extraData;
441         ShareInfo* pShareInfo = null;
442         result r = E_FAILURE;
443         bool imageAttachment = false;
444
445         extraData.Construct();
446
447         String typeKey = L"http://tizen.org/appcontrol/data/message/type";
448         String typeVal;
449         String textKey = L"http://tizen.org/appcontrol/data/text";
450         String textVal;
451         String attachKey = L"http://tizen.org/appcontrol/data/path";
452         String attachVal;
453         String resultKey = L"http://tizen.org/appcontrol/data/return_result";
454         String resultVal = "true";
455         ArrayList* pDataList = null;
456
457         if (__pShareList != null)
458         {
459                 pShareInfo = dynamic_cast<ShareInfo*>(__pShareList->GetAt(0));
460                 if (pShareInfo != null)
461                 {
462                         String testURL = pShareInfo->GetPageURL();
463                         AppLogDebug("getpageURL getpageURL is %ls", testURL.GetPointer());
464                 }
465         }
466
467         if (pShareInfo != null)
468         {
469                 textVal.Append(pShareInfo->GetPageTitle());
470                 if(pShareInfo->GetPageTitle().GetLength() > 0)
471                 {
472                         textVal.Append(L" <");
473                 }
474                 else
475                 {
476                         textVal.Append(L"<");
477                 }
478                 AppLog("share info url is %ls",pShareInfo->GetPageURL().GetPointer());
479                 textVal.Append(pShareInfo->GetPageURL().GetPointer());
480                 textVal.Append(L">");
481         }
482         if (pShareInfo != null)
483         {
484                 imageAttachment = pShareInfo->GetImageAttached();
485                 if (imageAttachment)
486                 {
487                         // type is mms
488                         typeVal = L"mms";
489                         attachVal = pShareInfo->GetImagePath();
490
491                         pDataList = new (std::nothrow) ArrayList();
492                         pDataList->Construct();
493                         pDataList->Add(&attachVal);
494                 }
495                 else
496                 {
497                         typeVal = L"sms";
498                         // type is sms
499                 }
500         }
501
502         extraData.Add(&typeKey, &typeVal);
503
504         if (imageAttachment)
505         {
506                 // type is MMMS, attach the image
507                 extraData.Add(&attachKey, pDataList);
508         }
509         else
510         {
511                 // type is SMS , attach the text
512                 extraData.Add(&textKey, &textVal);
513         }
514         extraData.Add(&resultKey, &resultVal);
515         AppControl* pAc = AppManager::FindAppControlN(L"tizen.messages", L"http://tizen.org/appcontrol/operation/compose");
516         if (pAc)
517         {
518                 r = pAc->Start(null, null, &extraData, this);
519                 delete pAc;
520         }
521
522         if(pDataList)
523         {
524                 delete pDataList;
525         }
526 }
527
528 void
529 SharePopup::OnAppControlCompleted(const Tizen::Base::String& providerId, const Tizen::Base::String& operationId, const Tizen::Base::Collection::IList* pResultList)
530 {
531         AppLog("InternetApp::OnForeground called");
532
533 }
534
535 void SharePopup::OnAppControlCompleteResponseReceived(const Tizen::App::AppId& appId, const Tizen::Base::String& operationId, Tizen::App::AppCtrlResult appControlResult, const Tizen::Base::Collection::IMap* pExtraData)
536 {
537         AppLog("SharePopup::OnAppControlCompleteResponseReceived");
538
539         ShareInfo* pShareInfo = null;
540         result r = E_FAILURE;
541         if(__pTimer)
542         {
543                 __pTimer->Cancel();
544                 delete __pTimer;
545                 __pTimer = null;
546         }
547         if (__pShareList != null)
548         {
549                 pShareInfo = dynamic_cast<ShareInfo*>(__pShareList->GetAt(0));
550         }
551
552         String attachVal;
553         bool imageAttachment = false;
554         if (pShareInfo != null)
555         {
556                 imageAttachment = pShareInfo->GetImageAttached();
557                 if (imageAttachment)
558                 {
559                         attachVal = pShareInfo->GetImagePath();
560                         AppLog("SharePopup:: imagePath is %S",attachVal.GetPointer());
561                         r = File::Remove(attachVal);
562                         if(r == E_SUCCESS)
563                         {
564                                 AppLog("removed success");
565                         }
566                         else
567                         {
568                                 AppLog("removed failure");
569                         }
570                 }
571                 __pShareList->RemoveAll(true);
572         }
573         Popup::SetShowState(false);
574         Popup::Show();
575         Frame* pCurrentFrame = null;
576         pCurrentFrame = Application::GetInstance()->GetAppFrame()->GetFrame();
577
578         if (pCurrentFrame != null)
579         { pCurrentFrame->SetEnabled(true); }
580         __appControlOngoing = false;
581 }
582
583 bool
584 SharePopup::OnKeyReleased (Control &source, const KeyEventInfo &keyEventInfo)
585 {
586         AppLog("ConfirmationPopup::OnKeyReleased %d",keyEventInfo.GetKeyCode());
587         if(keyEventInfo.GetKeyCode() == KEY_BACK || keyEventInfo.GetKeyCode() == KEY_ESC)
588         {
589                 if(GetShowState() == true)
590                 {
591                         if(__pTimer)
592                         {
593                                 __pTimer->Cancel();
594                                 delete __pTimer;
595                                 __pTimer = null;
596                         }
597                         SetShowState(false);
598                 }
599         }
600         return false;
601 }