7c0254dddb19f58f74fc424374132e6361d22ae2
[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
405
406
407 void
408 SharePopup::StartAppControl(int index)
409 {
410    HashMap extraData;
411    ShareInfo* pShareInfo = null;
412    result r = E_FAILURE;
413    bool imageAttachment = false;
414
415    extraData.Construct();
416
417    if (__pShareList != null)
418         {
419                 pShareInfo = dynamic_cast<ShareInfo*>(__pShareList->GetAt(0));
420                 if (pShareInfo != null)
421                 {
422                         String testURL = pShareInfo->GetPageURL();
423                         AppLogDebug("getpageURL getpageURL is %ls", testURL.GetPointer());
424                 }
425         }
426
427    String textVal;
428
429         if (pShareInfo != null)
430         {
431                 textVal.Append(pShareInfo->GetPageTitle());
432                 textVal.Append(L" <");
433                 AppLog("share info url is %ls",pShareInfo->GetPageURL().GetPointer());
434                 textVal.Append(pShareInfo->GetPageURL().GetPointer());
435                 textVal.Append(L">");
436         }
437
438
439    String textKey = L"http://tizen.org/appcontrol/data/text";
440
441    //String attachKey = L"attachments";
442    String attachKey = L"http://tizen.org/appcontrol/data/path";
443    String attachVal;
444         if (pShareInfo != null)
445         {
446                 imageAttachment = pShareInfo->GetImageAttached();
447                 if (imageAttachment)
448                 {
449                         attachVal = pShareInfo->GetImagePath();
450                         AppLog("SharePopup::StartEmailAppControl imagePath is %S",attachVal.GetPointer());
451                 }
452                 else
453                 {
454                          attachVal = L"";
455                 }
456         }
457
458         ArrayList* pDataList = null;
459         pDataList = new (std::nothrow) ArrayList();
460         pDataList->Construct();
461         pDataList->Add(&attachVal);
462
463
464    extraData.Add(&textKey, &textVal);
465
466    if(attachVal.GetLength() > 0)
467    {
468            extraData.Add(&attachKey, pDataList);
469    }
470
471    AppControl * pControl = dynamic_cast<AppControl*>(__pAppControlList->GetAt(index - 2));
472    if (pControl)
473    {
474           r = pControl->Start(null, null, &extraData, this);
475 //         delete pControl;
476    }
477
478    delete pDataList;
479 }
480
481 void
482 SharePopup::StartEmailAppControl(void)
483 {
484    HashMap extraData;
485    ShareInfo* pShareInfo = null;
486    result r = E_FAILURE;
487    bool imageAttachment = false;
488
489    extraData.Construct();
490
491    if (__pShareList != null)
492         {
493                 pShareInfo = dynamic_cast<ShareInfo*>(__pShareList->GetAt(0));
494                 if (pShareInfo != null)
495                 {
496                         String testURL = pShareInfo->GetPageURL();
497                         AppLogDebug("getpageURL getpageURL is %ls", testURL.GetPointer());
498                 }
499         }
500
501    String textVal;
502
503         if (pShareInfo != null)
504         {
505                 textVal.Append(pShareInfo->GetPageTitle());
506                 textVal.Append(L" <");
507                 AppLog("share info url is %ls",pShareInfo->GetPageURL().GetPointer());
508                 textVal.Append(pShareInfo->GetPageURL().GetPointer());
509                 textVal.Append(L">");
510         }
511
512    String subjectKey = L"http://tizen.org/appcontrol/data/subject";
513    String subjectVal = L"";
514    String textKey = L"http://tizen.org/appcontrol/data/text";
515
516    String toKey = L"http://tizen.org/appcontrol/data/to";
517    String toVal = L"";
518    String ccKey = L"http://tizen.org/appcontrol/data/cc";
519    String ccVal = L"";
520    String bccKey = L"http://tizen.org/appcontrol/data/bcc";
521    String bccVal = L"";
522    String resultKey = L"http://tizen.org/appcontrol/data/return_result";
523    String resultVal = "true";
524
525
526    //String attachKey = L"attachments";
527    String attachKey = L"http://tizen.org/appcontrol/data/path";
528    String attachVal;
529         if (pShareInfo != null)
530         {
531                 imageAttachment = pShareInfo->GetImageAttached();
532                 if (imageAttachment)
533                 {
534                         attachVal = pShareInfo->GetImagePath();
535                         AppLog("SharePopup::StartEmailAppControl imagePath is %S",attachVal.GetPointer());
536                 }
537                 else
538                 {
539                          attachVal = L"";
540                 }
541         }
542
543         ArrayList* pDataList = null;
544         pDataList = new (std::nothrow) ArrayList();
545         pDataList->Construct();
546         pDataList->Add(&attachVal);
547
548    extraData.Add(&subjectKey, &subjectVal);
549    extraData.Add(&textKey, &textVal);
550    extraData.Add(&toKey, &toVal);
551    extraData.Add(&ccKey, &ccVal);
552    extraData.Add(&bccKey, &bccVal);
553    extraData.Add(&resultKey, &resultVal);
554    if(attachVal.GetLength() > 0)
555    {
556            extraData.Add(&attachKey, pDataList);
557    }
558
559    AppControl* pAc = AppManager::FindAppControlN(L"tizen.email", L"http://tizen.org/appcontrol/operation/compose");
560    if (pAc)
561    {
562           r = pAc->Start(null, null, &extraData, this);
563            delete pAc;
564    }
565
566    delete pDataList;
567 }
568
569 void
570 SharePopup::StartMessageAppControl(void)
571 {
572         HashMap extraData;
573         ShareInfo* pShareInfo = null;
574         result r = E_FAILURE;
575         bool imageAttachment = false;
576
577         extraData.Construct();
578
579         String typeKey = L"http://tizen.org/appcontrol/data/message/type";
580         String typeVal;
581         String textKey = L"http://tizen.org/appcontrol/data/text";
582         String textVal;
583         String attachKey = L"http://tizen.org/appcontrol/data/path";
584         String attachVal;
585         String resultKey = L"http://tizen.org/appcontrol/data/return_result";
586         String resultVal = "true";
587         ArrayList* pDataList = null;
588
589         if (__pShareList != null)
590         {
591                 pShareInfo = dynamic_cast<ShareInfo*>(__pShareList->GetAt(0));
592                 if (pShareInfo != null)
593                 {
594                         String testURL = pShareInfo->GetPageURL();
595                         AppLogDebug("getpageURL getpageURL is %ls", testURL.GetPointer());
596                 }
597         }
598
599         if (pShareInfo != null)
600         {
601                 textVal.Append(pShareInfo->GetPageTitle());
602                 if(pShareInfo->GetPageTitle().GetLength() > 0)
603                 {
604                         textVal.Append(L" <");
605                 }
606                 else
607                 {
608                         textVal.Append(L"<");
609                 }
610                 AppLog("share info url is %ls",pShareInfo->GetPageURL().GetPointer());
611                 textVal.Append(pShareInfo->GetPageURL().GetPointer());
612                 textVal.Append(L">");
613         }
614         if (pShareInfo != null)
615         {
616                 imageAttachment = pShareInfo->GetImageAttached();
617                 if (imageAttachment)
618                 {
619                         // type is mms
620                         typeVal = L"mms";
621                         attachVal = pShareInfo->GetImagePath();
622
623                         pDataList = new (std::nothrow) ArrayList();
624                         pDataList->Construct();
625                         pDataList->Add(&attachVal);
626                 }
627                 else
628                 {
629                         typeVal = L"sms";
630                         // type is sms
631                 }
632         }
633
634         extraData.Add(&typeKey, &typeVal);
635
636         if (imageAttachment)
637         {
638                 // type is MMMS, attach the image
639                 extraData.Add(&attachKey, pDataList);
640         }
641         else
642         {
643                 // type is SMS , attach the text
644                 extraData.Add(&textKey, &textVal);
645         }
646         extraData.Add(&resultKey, &resultVal);
647         AppControl* pAc = AppManager::FindAppControlN(L"tizen.messages", L"http://tizen.org/appcontrol/operation/compose");
648         if (pAc)
649         {
650                 r = pAc->Start(null, null, &extraData, this);
651                 delete pAc;
652         }
653
654         if(pDataList)
655         {
656                 delete pDataList;
657         }
658 }
659
660 void
661 SharePopup::OnAppControlCompleted(const Tizen::Base::String& providerId, const Tizen::Base::String& operationId, const Tizen::Base::Collection::IList* pResultList)
662 {
663         AppLog("InternetApp::OnForeground called");
664
665 }
666
667 void SharePopup::OnAppControlCompleteResponseReceived(const Tizen::App::AppId& appId, const Tizen::Base::String& operationId, Tizen::App::AppCtrlResult appControlResult, const Tizen::Base::Collection::IMap* pExtraData)
668 {
669         AppLog("SharePopup::OnAppControlCompleteResponseReceived");
670
671         ShareInfo* pShareInfo = null;
672         result r = E_FAILURE;
673         if(__pTimer)
674         {
675                 __pTimer->Cancel();
676                 delete __pTimer;
677                 __pTimer = null;
678         }
679         if (__pShareList != null)
680         {
681                 pShareInfo = dynamic_cast<ShareInfo*>(__pShareList->GetAt(0));
682         }
683
684         String attachVal;
685         bool imageAttachment = false;
686         if (pShareInfo != null)
687         {
688                 imageAttachment = pShareInfo->GetImageAttached();
689                 if (imageAttachment)
690                 {
691                         attachVal = pShareInfo->GetImagePath();
692                         AppLog("SharePopup:: imagePath is %S",attachVal.GetPointer());
693                         r = File::Remove(attachVal);
694                         if(r == E_SUCCESS)
695                         {
696                                 AppLog("removed success");
697                         }
698                         else
699                         {
700                                 AppLog("removed failure");
701                         }
702                 }
703                 __pShareList->RemoveAll(true);
704         }
705         Popup::SetShowState(false);
706         Popup::Show();
707         Frame* pCurrentFrame = null;
708         pCurrentFrame = Application::GetInstance()->GetAppFrame()->GetFrame();
709
710         if (pCurrentFrame != null)
711         {
712                 pCurrentFrame->SetEnabled(true);
713                 pCurrentFrame->Invalidate(true);
714         }
715         __appControlOngoing = false;
716 }
717
718 bool
719 SharePopup::OnKeyReleased (Control &source, const KeyEventInfo &keyEventInfo)
720 {
721         AppLog("ConfirmationPopup::OnKeyReleased %d",keyEventInfo.GetKeyCode());
722         if(keyEventInfo.GetKeyCode() == KEY_BACK || keyEventInfo.GetKeyCode() == KEY_ESC)
723         {
724                 if(GetShowState() == true)
725                 {
726                         if(__pTimer)
727                         {
728                                 __pTimer->Cancel();
729                                 delete __pTimer;
730                                 __pTimer = null;
731                         }
732                         SetShowState(false);
733                 }
734         }
735         return false;
736 }