Fixed Nabi Issues N_SE-56908,56903,56917,56940
[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         if(__pAppControlList)
149                 __pAppControlList->RemoveAll(true);
150         delete __pAppControlList;
151 }
152
153 bool
154 SharePopup::Initialize(void)
155 {
156         Button* pCancelButton = null;
157
158         Popup::Construct(L"IDL_SHARE_POPUP");
159         SetName(L"CommonPopup");
160
161         __pShareList = new(std::nothrow) ArrayList();
162         __pShareList->Construct();
163
164         __pList = static_cast<ListView*>(GetControl(L"IDC_LISTVIEW"));
165         if (__pList == null)
166         {
167                 return false;
168         }
169
170         __pList->SetItemProvider(*this);
171         __pList->AddListViewItemEventListener(*this);
172 //      __pList->SetTextOfEmptyList(L"No Sharing Options");//hardcoded string
173
174         pCancelButton = static_cast< Button* >(GetControl(L"IDC_BUTTON1", true));
175         if (pCancelButton)
176         {
177                 pCancelButton->AddActionEventListener(*this);
178                 pCancelButton->SetActionId(IDA_CANCEL_BUTTON);
179         }
180
181         __pTimer = new(std::nothrow) Timer();
182         __pTimer->Construct(*this);
183         SetPropagatedKeyEventListener(this);
184         return true;
185 }
186
187 result
188 SharePopup::OnTerminating(void)
189 {
190         result r = E_SUCCESS;
191         return r;
192 }
193
194 void
195 SharePopup::OnTimerExpired(Timer& timer)
196 {
197         Popup::SetShowState(false);
198         Popup::Show();
199         Frame* pCurrentFrame = null;
200         pCurrentFrame = Application::GetInstance()->GetAppFrame()->GetFrame();
201
202         if (pCurrentFrame != null)
203         {
204                 pCurrentFrame->SetEnabled(true);
205                 pCurrentFrame->Invalidate(true);
206         }
207 }
208
209 void
210 SharePopup::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
211 {
212         switch(actionId)
213         {
214         case IDA_CANCEL_BUTTON:
215         {
216                 Popup::SetShowState(false);
217                 Popup::Show();
218         }
219         break;
220         default:
221                 break;
222         }
223         return;
224 }
225
226 void
227 SharePopup::OnListViewContextItemStateChanged(Tizen::Ui::Controls::ListView& listView, int index, int elementId, Tizen::Ui::Controls::ListContextItemStatus state)
228 {
229         return;
230 }
231
232 void
233 SharePopup::OnListViewItemStateChanged(Tizen::Ui::Controls::ListView& listView, int index, int elementId, Tizen::Ui::Controls::ListItemStatus status)
234 {
235         switch (index)
236         {
237         case 0:
238         {
239                 if(__appControlOngoing == false)
240                 {
241                         __appControlOngoing = true;
242                         // share via message
243                         StartMessageAppControl();
244                         if(__pTimer == null)
245                         {
246                                 __pTimer = new(std::nothrow) Timer();
247                                 __pTimer->Construct(*this);
248                         }
249                         __pTimer->Start(2500);
250                 }
251         }
252         break;
253         case 1:
254         {
255                 if(__appControlOngoing == false)
256                 {
257                         __appControlOngoing = true;
258                         // share via email
259                         StartEmailAppControl();
260                         if(__pTimer == null)
261                         {
262                                 __pTimer = new(std::nothrow) Timer();
263                                 __pTimer->Construct(*this);
264                         }
265                         __pTimer->Start(2500);
266                 }
267
268         }
269         break;
270         default:
271                 if(__appControlOngoing == false)
272                 {
273                         __appControlOngoing = true;
274                         //share via third party app
275                         StartAppControl(index);
276                         if(__pTimer == null)
277                         {
278                                 __pTimer = new(std::nothrow) Timer();
279                                 __pTimer->Construct(*this);
280                         }
281                         __pTimer->Start(2500);
282                 }
283                 break;
284         }
285         Frame* pCurrentFrame = null;
286         pCurrentFrame = Application::GetInstance()->GetAppFrame()->GetFrame();
287
288         if (pCurrentFrame != null)
289         {
290                 pCurrentFrame->SetEnabled(false);
291                 pCurrentFrame->Invalidate(true);
292         }
293 }
294
295 void
296 SharePopup::OnListViewItemSwept(Tizen::Ui::Controls::ListView& listView, int index, Tizen::Ui::Controls::SweepDirection direction)
297 {
298         return;
299 }
300
301 void
302 SharePopup::OnListViewItemLongPressed(Tizen::Ui::Controls::ListView& listView, int index, int elementId, bool& invokeListViewItemCallback)
303 {
304         return;
305 }
306
307 Tizen::Ui::Controls::ListItemBase*
308 SharePopup::CreateItem(int index, int itemWidth)
309 {
310         AppLog("SharePopup::CreateItem index %d",index);
311         result r = E_FAILURE;
312         ListAnnexStyle style = LIST_ANNEX_STYLE_NORMAL;
313         int textSize = 35;
314         int elementHeight = 50;
315         int listItemHeight = 75;
316
317         CustomItem* pItem = new(std::nothrow) CustomItem();
318         r = pItem->Construct(Dimension(GetClientAreaBounds().width, listItemHeight), style);
319         if (IsFailed(r))
320         {
321                 AppLogDebug("Create Item Failed with error %s", GetErrorMessage(r));
322                 delete pItem;
323                 return null;
324         }
325
326
327
328         switch(index)
329         {
330         case 0:
331         {
332                 AppLogDebug("SharePopUp CreateItem 0");
333                 // IDS_SHARE_VIA_MESSAGE
334                 pItem->AddElement(Rectangle(45,0, GetClientAreaBounds().width, listItemHeight), ID_FORMAT_MESSAGE_STRING, CommonUtil::GetString(L"IDS_BR_OPT_SENDURLVIA_MESSAGE"), true);
335         }
336         break;
337         case 1:
338         {
339                 AppLogDebug("SharePopUp CreateItem 1");
340                 // IDS_SHARE_VIA_EMAIL
341                 pItem->AddElement(Rectangle(45, 0, GetClientAreaBounds().width, listItemHeight), ID_FORMAT_EMAIL_STRING, CommonUtil::GetString(L"IDS_BR_OPT_SENDURLVIA_EMAIL"), true);
342         }
343         break;
344         default:
345                 if(__pAppControlList)
346                 {
347                         AppControl * pControl = dynamic_cast<AppControl*>(__pAppControlList->GetAt(index -2));
348                         AppLog("SharePopup::CreateItem appcontrol count %d",__pAppControlList->GetCount());
349                         if(pControl)
350                                 pItem->AddElement(Rectangle(45,0, GetClientAreaBounds().width, listItemHeight), ID_FORMAT_MESSAGE_STRING+index, pControl->GetAppName(), true);
351                 }
352                 break;
353         }
354         return pItem;
355 }
356
357 bool
358 SharePopup::DeleteItem(int index, Tizen::Ui::Controls::ListItemBase* pItem, int itemWidth)
359 {
360         return true;
361 }
362
363 int
364 SharePopup::GetItemCount(void)
365 {
366         String* pStrId = new String(L"http://tizen.org/appcontrol/operation/share_text");
367         __pAppControlList = AppManager::FindAppControlsN(pStrId,null,null,null);
368         if(__pAppControlList == null)
369         {
370                 return 2;
371         }
372         int count = __pAppControlList->GetCount();
373         AppLog("SharePopup::GetItemCount appcontrol count %d",count);
374         int removeCount = 0;
375         for(int pos = 0; pos < count; pos++)
376         {
377                 AppControl * pControl = dynamic_cast<AppControl*>(__pAppControlList->GetAt(pos));
378                 AppLog("pControl->GetAppName() %ls",pControl->GetAppName().GetPointer());
379                 if( pControl->GetAppId().Equals(L"8r4r5ddzzn.Messages",false) == true || pControl->GetAppId().Equals(L"vxqbrefica.Email",false) == true)
380                 {
381                         AppLog("Removed pControl->GetAppName %ls pControl->GetAppId() %ls",pControl->GetAppName().GetPointer(),pControl->GetAppId().GetPointer());
382                         __pAppControlList->RemoveAt(pos,true);
383                         pos--;
384                         count --;
385                         removeCount++;
386                 }
387         }
388
389         AppLog("removeCount %d",removeCount);
390         if(count+removeCount >=2)
391                 return count+removeCount;
392         return 2;
393
394 }
395
396 result
397 SharePopup::AddShareInfo(ShareInfo* pShareInfo)
398 {
399         result r = E_FAILURE;
400
401         if (__pShareList != NULL)
402         {
403                 r = __pShareList->Add(*pShareInfo);
404         }
405         return r;
406 }
407
408 void SharePopup::RemoveAllShareInfo()
409 {
410         __pShareList->RemoveAll();
411 }
412
413 void
414 SharePopup::StartAppControl(int index)
415 {
416    HashMap extraData;
417    ShareInfo* pShareInfo = null;
418    result r = E_FAILURE;
419    bool imageAttachment = false;
420
421    extraData.Construct();
422
423    if (__pShareList != null)
424         {
425                 pShareInfo = dynamic_cast<ShareInfo*>(__pShareList->GetAt(0));
426                 if (pShareInfo != null)
427                 {
428                         String testURL = pShareInfo->GetPageURL();
429                         AppLogDebug("getpageURL getpageURL is %ls", testURL.GetPointer());
430                 }
431         }
432
433    String textVal;
434
435         if (pShareInfo != null)
436         {
437                 textVal.Append(pShareInfo->GetPageTitle());
438                 textVal.Append(L" <");
439                 AppLog("share info url is %ls",pShareInfo->GetPageURL().GetPointer());
440                 textVal.Append(pShareInfo->GetPageURL().GetPointer());
441                 textVal.Append(L">");
442         }
443
444
445    String textKey = L"http://tizen.org/appcontrol/data/text";
446
447    //String attachKey = L"attachments";
448    String attachKey = L"http://tizen.org/appcontrol/data/path";
449    String attachVal;
450         if (pShareInfo != null)
451         {
452                 imageAttachment = pShareInfo->GetImageAttached();
453                 if (imageAttachment)
454                 {
455                         attachVal = pShareInfo->GetImagePath();
456                         AppLog("SharePopup::StartEmailAppControl imagePath is %S",attachVal.GetPointer());
457                 }
458                 else
459                 {
460                          attachVal = L"";
461                 }
462         }
463
464         ArrayList* pDataList = null;
465         pDataList = new (std::nothrow) ArrayList();
466         pDataList->Construct();
467         pDataList->Add(&attachVal);
468
469
470    extraData.Add(&textKey, &textVal);
471
472    if(attachVal.GetLength() > 0)
473    {
474            extraData.Add(&attachKey, pDataList);
475    }
476
477    AppControl * pControl = dynamic_cast<AppControl*>(__pAppControlList->GetAt(index - 2));
478    AppLog("pControl AppControl Name %ls",pControl->GetAppName().GetPointer());
479    if (pControl)
480    {
481           r = pControl->Start(null, null, &extraData, this);
482           AppLog("AppControl Start %s",GetErrorMessage(r));
483 //         delete pControl;
484    }
485
486    delete pDataList;
487 }
488
489 void
490 SharePopup::StartEmailAppControl(void)
491 {
492    HashMap extraData;
493    ShareInfo* pShareInfo = null;
494    result r = E_FAILURE;
495    bool imageAttachment = false;
496
497    extraData.Construct();
498
499    if (__pShareList != null)
500         {
501                 pShareInfo = dynamic_cast<ShareInfo*>(__pShareList->GetAt(0));
502                 if (pShareInfo != null)
503                 {
504                         String testURL = pShareInfo->GetPageURL();
505                         AppLogDebug("getpageURL getpageURL is %ls", testURL.GetPointer());
506                 }
507         }
508
509    String textVal;
510
511         if (pShareInfo != null)
512         {
513                 textVal.Append(pShareInfo->GetPageTitle());
514                 textVal.Append(L" <");
515                 AppLog("share info url is %ls",pShareInfo->GetPageURL().GetPointer());
516                 textVal.Append(pShareInfo->GetPageURL().GetPointer());
517                 textVal.Append(L">");
518         }
519
520    String subjectKey = L"http://tizen.org/appcontrol/data/subject";
521    String subjectVal = L"";
522    String textKey = L"http://tizen.org/appcontrol/data/text";
523
524    String toKey = L"http://tizen.org/appcontrol/data/to";
525    String toVal = L"";
526    String ccKey = L"http://tizen.org/appcontrol/data/cc";
527    String ccVal = L"";
528    String bccKey = L"http://tizen.org/appcontrol/data/bcc";
529    String bccVal = L"";
530    String resultKey = L"http://tizen.org/appcontrol/data/return_result";
531    String resultVal = "true";
532
533
534    //String attachKey = L"attachments";
535    String attachKey = L"http://tizen.org/appcontrol/data/path";
536    String attachVal;
537         if (pShareInfo != null)
538         {
539                 imageAttachment = pShareInfo->GetImageAttached();
540                 if (imageAttachment)
541                 {
542                         attachVal = pShareInfo->GetImagePath();
543                         AppLog("SharePopup::StartEmailAppControl imagePath is %S",attachVal.GetPointer());
544                 }
545                 else
546                 {
547                          attachVal = L"";
548                 }
549         }
550
551         ArrayList* pDataList = null;
552         pDataList = new (std::nothrow) ArrayList();
553         pDataList->Construct();
554         pDataList->Add(&attachVal);
555
556    extraData.Add(&subjectKey, &subjectVal);
557    extraData.Add(&textKey, &textVal);
558    extraData.Add(&toKey, &toVal);
559    extraData.Add(&ccKey, &ccVal);
560    extraData.Add(&bccKey, &bccVal);
561    extraData.Add(&resultKey, &resultVal);
562    if(attachVal.GetLength() > 0)
563    {
564            extraData.Add(&attachKey, pDataList);
565    }
566
567    AppControl* pAc = AppManager::FindAppControlN(L"tizen.email", L"http://tizen.org/appcontrol/operation/compose");
568    if (pAc)
569    {
570           r = pAc->Start(null, null, &extraData, this);
571            delete pAc;
572    }
573
574    delete pDataList;
575 }
576
577 void
578 SharePopup::StartMessageAppControl(void)
579 {
580         HashMap extraData;
581         ShareInfo* pShareInfo = null;
582         result r = E_FAILURE;
583         bool imageAttachment = false;
584
585         extraData.Construct();
586
587         String typeKey = L"http://tizen.org/appcontrol/data/message/type";
588         String typeVal;
589         String textKey = L"http://tizen.org/appcontrol/data/text";
590         String textVal;
591         String attachKey = L"http://tizen.org/appcontrol/data/path";
592         String attachVal;
593         String resultKey = L"http://tizen.org/appcontrol/data/return_result";
594         String resultVal = "true";
595         ArrayList* pDataList = null;
596
597         if (__pShareList != null)
598         {
599                 pShareInfo = dynamic_cast<ShareInfo*>(__pShareList->GetAt(0));
600                 if (pShareInfo != null)
601                 {
602                         String testURL = pShareInfo->GetPageURL();
603                         AppLogDebug("getpageURL getpageURL is %ls", testURL.GetPointer());
604                 }
605         }
606
607         if (pShareInfo != null)
608         {
609                 textVal.Append(pShareInfo->GetPageTitle());
610                 if(pShareInfo->GetPageTitle().GetLength() > 0)
611                 {
612                         textVal.Append(L" <");
613                 }
614                 else
615                 {
616                         textVal.Append(L"<");
617                 }
618                 AppLog("share info url is %ls",pShareInfo->GetPageURL().GetPointer());
619                 textVal.Append(pShareInfo->GetPageURL().GetPointer());
620                 textVal.Append(L">");
621         }
622         if (pShareInfo != null)
623         {
624                 imageAttachment = pShareInfo->GetImageAttached();
625                 if (imageAttachment)
626                 {
627                         // type is mms
628                         typeVal = L"mms";
629                         attachVal = pShareInfo->GetImagePath();
630
631                         pDataList = new (std::nothrow) ArrayList();
632                         pDataList->Construct();
633                         pDataList->Add(&attachVal);
634                 }
635                 else
636                 {
637                         typeVal = L"sms";
638                         // type is sms
639                 }
640         }
641
642         extraData.Add(&typeKey, &typeVal);
643
644         if (imageAttachment)
645         {
646                 // type is MMMS, attach the image
647                 extraData.Add(&attachKey, pDataList);
648         }
649         else
650         {
651                 // type is SMS , attach the text
652                 extraData.Add(&textKey, &textVal);
653         }
654         extraData.Add(&resultKey, &resultVal);
655         AppControl* pAc = AppManager::FindAppControlN(L"tizen.messages", L"http://tizen.org/appcontrol/operation/compose");
656         if (pAc)
657         {
658                 r = pAc->Start(null, null, &extraData, this);
659                 delete pAc;
660         }
661
662         if(pDataList)
663         {
664                 delete pDataList;
665         }
666 }
667
668 void
669 SharePopup::OnAppControlCompleted(const Tizen::Base::String& providerId, const Tizen::Base::String& operationId, const Tizen::Base::Collection::IList* pResultList)
670 {
671         AppLog("InternetApp::OnForeground called");
672
673 }
674
675 void SharePopup::OnAppControlCompleteResponseReceived(const Tizen::App::AppId& appId, const Tizen::Base::String& operationId, Tizen::App::AppCtrlResult appControlResult, const Tizen::Base::Collection::IMap* pExtraData)
676 {
677         AppLog("SharePopup::OnAppControlCompleteResponseReceived");
678
679         ShareInfo* pShareInfo = null;
680         result r = E_FAILURE;
681         if(__pTimer)
682         {
683                 __pTimer->Cancel();
684                 delete __pTimer;
685                 __pTimer = null;
686         }
687         if (__pShareList != null)
688         {
689                 pShareInfo = dynamic_cast<ShareInfo*>(__pShareList->GetAt(0));
690         }
691
692         String attachVal;
693         bool imageAttachment = false;
694         if (pShareInfo != null)
695         {
696                 imageAttachment = pShareInfo->GetImageAttached();
697                 if (imageAttachment)
698                 {
699                         attachVal = pShareInfo->GetImagePath();
700                         AppLog("SharePopup:: imagePath is %S",attachVal.GetPointer());
701                         r = File::Remove(attachVal);
702                         if(r == E_SUCCESS)
703                         {
704                                 AppLog("removed success");
705                         }
706                         else
707                         {
708                                 AppLog("removed failure");
709                         }
710                 }
711                 __pShareList->RemoveAll(true);
712         }
713         Popup::SetShowState(false);
714         Popup::Show();
715         Frame* pCurrentFrame = null;
716         pCurrentFrame = Application::GetInstance()->GetAppFrame()->GetFrame();
717
718         if (pCurrentFrame != null)
719         {
720                 pCurrentFrame->SetEnabled(true);
721                 pCurrentFrame->Invalidate(true);
722         }
723         __appControlOngoing = false;
724 }
725
726 bool
727 SharePopup::OnKeyReleased (Control &source, const KeyEventInfo &keyEventInfo)
728 {
729         AppLog("ConfirmationPopup::OnKeyReleased %d",keyEventInfo.GetKeyCode());
730         if(keyEventInfo.GetKeyCode() == KEY_BACK || keyEventInfo.GetKeyCode() == KEY_ESC)
731         {
732                 if(GetShowState() == true)
733                 {
734                         if(__pTimer)
735                         {
736                                 __pTimer->Cancel();
737                                 delete __pTimer;
738                                 __pTimer = null;
739                         }
740                         SetShowState(false);
741                 }
742         }
743         return false;
744 }