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