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