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