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