31c30d6507bb5b379fd1a8c1e4c06a7b52f6a2b5
[apps/native/preloaded/Email.git] / src / EmEmailApp.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.1 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://floralicense.org/license/
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * Name        : Email
19  * Version     :
20  * Vendor      :
21  * Description :
22  */
23
24 #include "EmNativeMailboxManager.h"
25 #include "EmEmailApp.h"
26 #include "EmMainFrame.h"
27 #include "EmFormFactory.h"
28 #include "EmTypes.h"
29 #include "EmMailSyncThread.h"
30 #include "EmEmailSettingPresentationModel.h"
31 #include "EmInboxPresentationModel.h"
32 #include "EmManualSetUpForm.h"
33 #include "EmStateInfo.h"
34
35 using namespace Tizen::App;
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Utility;
38 using namespace Tizen::Io;
39 using namespace Tizen::System;
40 using namespace Tizen::Ui;
41 using namespace Tizen::Ui::Controls;
42 using namespace Tizen::Ui::Scenes;
43 using namespace Tizen::Graphics;
44 using namespace Tizen::Base::Collection;
45
46 EmailApp* EmailApp::__application = null;
47
48 EmailApp::EmailApp(void)
49         : __downloadBodyHandle(0)
50         , __downloadBodyAccountId(-1)
51         , __returnAppControlResult(false)
52         , __frameDisabled(false)
53         , __showDeletePopup(false)
54         , __showDownloadAttachmentPopup(false)
55         , __showDownloadFailedPopup(false)
56         , __isMoveFailed(false)
57         , __showMoveOrDeleteFailedPopup(false)
58         , __mailId(-1)
59         , __downloadMailId(-1)
60         , __appControlRequestId(0)
61         , __opId(EMAIL_OPERATION_NONE)
62         , __appControlResult(APP_CTRL_RESULT_FAILED)
63         , __pArgs(null)
64 {
65         // Empty Implementation
66 }
67
68 EmailApp::~EmailApp(void)
69 {
70         // Empty Implementation
71 }
72
73 UiApp*
74 EmailApp::CreateInstance(void)
75 {
76         __application = new (std::nothrow) EmailApp();
77         // Create the instance through the constructor.
78         return __application;
79 }
80
81 EmailApp*
82 EmailApp::GetInstance(void)
83 {
84         return __application;
85 }
86
87 bool
88 EmailApp::OnAppInitializing(AppRegistry& appRegistry)
89 {
90         // TODO:
91         // Initialize Frame and App specific data.
92         // The App's permanent data and context can be obtained from the appRegistry.
93         //
94         // If this method is successful, return true; otherwise, return false.
95         // If this method returns false, the App will be terminated.
96
97         // Uncomment the following statement to listen to the screen on/off events.
98         // PowerManager::SetScreenEventListener(*this);
99
100         AppLogDebug("ENTER");
101
102         result r = E_SUCCESS;
103         r = AppControlProviderManager::GetInstance()->SetAppControlProviderEventListener(this);
104         AppLogDebug("SetAppControlProviderEventListener result:(%s)", GetErrorMessage(r));
105
106         AppLogDebug("EXIT");
107         return true;
108 }
109
110 bool
111 EmailApp::OnAppInitialized(void)
112 {
113         AppLogDebug("ENTER");
114
115         int res;
116         int modRes;
117         long long mem;
118
119         result r = E_SUCCESS;
120         MessageBox msgBox;
121         String languageKey = L"http://tizen.org/setting/locale.language";
122         String timeFormatKey = L"http://tizen.org/setting/locale.time.format.24hour";
123
124         String backButtonKey = L"http://tizen.org/feature/input.keys.back";
125         String menuButtonKey = L"http://tizen.org/feature/input.keys.menu";
126
127         // Create a Frame
128         EmailFrame* pEmailFrame = new (std::nothrow) EmailFrame();
129         pEmailFrame->Construct();
130         pEmailFrame->SetName(IDS_EMAIL_TEXT);
131         AddFrame(*pEmailFrame);
132
133         mem = GetAvailableMemory();
134         AppLogDebug("%lld",mem);
135
136         if(mem < MIN_MEMORY_NEEDED)
137         {
138                 MessageBox LowMemory;
139                 String msg = L"";
140                 //LowMemory.Construct(Utils::GetResourceString(L"IDS_COM_BODY_WARNING"), L"Memory Low, Delete some data", MSGBOX_STYLE_NONE,1000);
141                 LowMemory.Construct(Utils::GetResourceString(L"IDS_COM_BODY_WARNING"), Utils::GetResourceString(L"IDS_COM_POP_NOT_ENOUGH_MEMORY"), MSGBOX_STYLE_NONE,1000);
142                 int modalResult = 0;
143                 // Calls ShowAndWait() : Draws and Shows itself and processes events
144                 LowMemory.ShowAndWait(modalResult);
145
146                 return false;
147         }
148
149         if (email_service_begin() != EMAIL_ERROR_NONE)
150         {
151                 AppLogDebug("Email service begin failed!");
152                 msgBox.Construct("Error!", "Launch failed due to Email Service error!", MSGBOX_STYLE_OK, 300000);
153                 msgBox.ShowAndWait(modRes);
154                 return false;
155         }
156
157         if ((res=email_open_db()) != EMAIL_ERROR_NONE)
158         {
159                 AppLogDebug("Open db failed with error:(%d)", res);
160                 msgBox.Construct("Error!", "Launch failed due to DB error!", MSGBOX_STYLE_OK, 300000);
161                 msgBox.ShowAndWait(modRes);
162                 return false;
163         }
164
165         MailNotificationThread* pNotiThread = MailNotificationThread::GetInstance();
166         pNotiThread->Start();
167
168         SceneManager* pSceneManager = SceneManager::GetInstance();
169         RegisterScenes();
170
171         if (__opId == EMAIL_OPERATION_COMPOSE)
172         {
173                 if (EmailSettingsProvider::GetInstance()->GetDefaultAccountId() > 0)
174                 {
175                         AppLogDebug("Args count:(%d)", __pArgs->GetCount());
176                         r = pSceneManager->GoForward(ForwardSceneTransition(IDSCN_EMAIL_COMPOSER), __pArgs);
177                         AppLogDebug("Result of goforward:(%s)", GetErrorMessage(r));
178                 }
179                 else
180                 {
181                         r = pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ADD_ACCOUNT), __pArgs);
182                         AppLogDebug("Result of goforward:(%s)", GetErrorMessage(r));
183                 }
184         }
185         else if (__opId == EMAIL_OPERATION_SETTINGS)
186         {
187                 //              Create Email settings manager...
188                 EmailSettingsProvider* pMailSettingsProvider = EmailSettingsProvider::GetInstance();
189
190                 if (pMailSettingsProvider == null)
191                 {
192                         AppLogDebug("Manager initialization failed!");
193                         AppLogDebug("EXIT, returning false");
194                         return false;
195                 }
196
197                 r = pSceneManager->GoForward(ForwardSceneTransition(IDSCN_EMAIL_SETTING));
198         }
199         else if (__opId == EMAIL_OPERATION_ADD_ACCOUNT)
200         {
201                 //              Create Email settings manager...
202                 EmailSettingsProvider* pMailSettingsProvider = EmailSettingsProvider::GetInstance();
203
204                 if (pMailSettingsProvider == null)
205                 {
206                         AppLogDebug("Manager initialization failed!");
207                         AppLogDebug("EXIT, returning false");
208                         return false;
209                 }
210
211                 r = pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ADD_ACCOUNT));
212         }
213         else if (__opId == EMAIL_OPERATION_ACCOUNT_CONFIG)
214         {
215                 EmailAccount* pTmpAccount = EmailSettingsProvider::GetInstance()->GetAccountById(__accountId);
216
217                 ArrayList* pArgs = new ArrayList();
218                 pArgs->Construct();
219
220                 pArgs->Add(pTmpAccount);
221
222                 r = pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ACCOUNT_CONFIG), pArgs);
223         }
224         else if (__opId == EMAIL_OPERATION_NONE)
225         {
226                 int accountId;
227                 Orientation frameOrientation;
228
229                 // Create Email settings provider...
230                 EmailSettingsProvider* pMailSettingsProvider = EmailSettingsProvider::GetInstance();
231
232                 // Create Email Mailbox manager...
233                 EmailMailboxManager* pMailboxManager = EmailMailboxManager::GetInstance();
234
235                 if (pMailSettingsProvider == null || pMailboxManager == null)
236                 {
237                         AppLogDebug("Manager initialization failed!");
238                         AppLogDebug("EXIT, returning false");
239                         return false;
240                 }
241
242                 accountId = pMailSettingsProvider->GetDefaultAccountId();
243                 if (accountId > 0)
244                 {
245                         Frame* pFrame = GetInstance()->GetFrame(IDS_EMAIL_TEXT);
246
247                         if (pFrame != null)
248                         {
249                                 frameOrientation = pFrame->GetOrientation();
250
251                                 AppLogDebug("check orientation:(%d)", frameOrientation);
252                                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_INBOX));
253                         }
254                 }
255                 else
256                 {
257                         pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ADD_ACCOUNT));
258                 }
259         }
260
261         SettingInfo::GetValue(languageKey, __currentDisplayLanguage);
262         SettingInfo::GetValue(timeFormatKey, __is24HourFormat);
263         SettingInfo::AddSettingEventListener(*this);
264
265         AppLogDebug("EXIT, returning true");
266         return true;
267 }
268
269 bool
270 EmailApp::OnAppWillTerminate(void)
271 {
272         // TODO:
273         // Comment.
274         return true;
275 }
276
277 bool
278 EmailApp::OnAppTerminating(AppRegistry& appRegistry, bool forcedTermination)
279 {
280         AppLogDebug("ENTER");
281
282         if (__returnAppControlResult == true)
283         {
284                 AppControlProviderManager::GetInstance()->SendAppControlResult(__appControlRequestId, __appControlResult, null);
285         }
286
287         if(email_close_db() != EMAIL_ERROR_NONE)
288         {
289                 AppLogDebug("Error closing DB!");
290         }
291
292         if(email_service_end() != EMAIL_ERROR_NONE)
293         {
294                 AppLogDebug("Error ending email service");
295         }
296
297         Utils::CleanUp();
298
299         AppLogDebug("EXIT");
300         return true;
301 }
302
303 void
304 EmailApp::OnForeground(void)
305 {
306         // TODO:
307         // Start or resume drawing when the application is moved to the foreground.
308         AppLogDebug("ENTER");
309
310         Frame* pFrame = GetFrame(IDS_EMAIL_TEXT);
311
312         if (pFrame != null)
313         {
314                 Form* pForm = pFrame->GetCurrentForm();
315
316                 if (pForm != null)
317                 {
318                         AppLogDebug("Sending user event to form:(%ls)", pForm->GetName().GetPointer());
319                         pForm->SendUserEvent(IDA_EMAIL_APP_FOREGROUND, null);
320                 }
321         }
322
323         AppLogDebug("EXIT");
324 }
325
326 void
327 EmailApp::OnBackground(void)
328 {
329         AppLogDebug("ENTER");
330
331         EnableFrame();
332
333         AppLogDebug("EXIT");
334 }
335
336 void
337 EmailApp::OnLowMemory(void)
338 {
339         // TODO:
340         // Free unused resources or close the application.
341 }
342
343 void
344 EmailApp::OnBatteryLevelChanged(BatteryLevel batteryLevel)
345 {
346         // TODO:
347         // Handle any changes in battery level here.
348         // Stop using multimedia features(camera, mp3 etc.) if the battery level is CRITICAL.
349 }
350
351 void
352 EmailApp::OnScreenOn(void)
353 {
354         // TODO:
355         // Get the released resources or resume the operations that were paused or stopped in OnScreenOff().
356 }
357
358 void
359 EmailApp::OnScreenOff(void)
360 {
361         // TODO:
362         // Unless there is a strong reason to do otherwise, release resources (such as 3D, media, and sensors) to allow the device
363         // to enter the sleep mode to save the battery.
364         // Invoking a lengthy asynchronous method within this listener method can be risky, because it is not guaranteed to invoke a
365         // callback before the device enters the sleep mode.
366         // Similarly, do not perform lengthy operations in this listener method. Any operation must be a quick one.
367 }
368
369 void
370 EmailApp::OnUserEventReceivedN(RequestId requestId, Tizen::Base::Collection::IList* pArgs)
371 {
372         AppLogDebug("ENTER, requestId:(%d)", requestId);
373
374         if (requestId == IDA_NOTI_DELETE_FINISHED)
375         {
376                 if (pArgs != null && pArgs->GetCount() == 2)
377                 {
378                         int accountId = static_cast<Integer*>(pArgs->GetAt(0))->ToInt();
379                         String* pData = static_cast<String*>(pArgs->GetAt(1));
380
381                         if (pData != null)
382                         {
383                                 EmailMailboxManager::GetInstance()->RemoveData(accountId, pData);
384                         }
385
386                         Frame* pFrame = GetFrame(IDS_EMAIL_TEXT);
387                         if(pFrame != null)
388                         {
389                                 Form* pForm = pFrame->GetCurrentForm();
390                                 if(pForm != null)
391                                 {
392                                         AppLogDebug("Sending user event to form:(%ls)", pForm->GetName().GetPointer());
393                                         pForm->SendUserEvent(requestId, null);
394                                 }
395                         }
396                 }
397         }
398         else if (requestId == IDA_NOTI_MAIL_MOVE_FINISHED)
399         {
400                 AppLogDebug("EmailApp::mail data modified");
401
402                 if (requestId == IDA_NOTI_MAIL_MOVE_FINISHED && pArgs != null)
403                 {
404                         int fromMailboxId = static_cast<Integer*>(pArgs->GetAt(1))->ToInt();
405                         int toMailboxId = static_cast<Integer*>(pArgs->GetAt(2))->ToInt();
406                         HashMap* pMailIdMap = static_cast<HashMap*>(pArgs->GetAt(3));
407
408                         EmailMailboxManager::GetInstance()->MoveData(fromMailboxId, toMailboxId, pMailIdMap);
409
410                         pMailIdMap->RemoveAll(true);
411                         pArgs->RemoveAll(true);
412                         delete pArgs;
413                 }
414
415                 Frame* pFrame = GetFrame(IDS_EMAIL_TEXT);
416                 if(pFrame != null)
417                 {
418                         Form* pForm = pFrame->GetCurrentForm();
419                         if(pForm != null)
420                         {
421                                 AppLogDebug("Sending user event to form:(%ls)", pForm->GetName().GetPointer());
422                                 pForm->SendUserEvent(requestId, null);
423                         }
424                 }
425         }
426         else if (requestId == IDA_NOTI_MAIL_MOVE_FAIL)
427         {
428                 Frame* pFrame = GetFrame(IDS_EMAIL_TEXT);
429                 if(pFrame != null)
430                 {
431                         Form* pForm = pFrame->GetCurrentForm();
432                         if(pForm != null)
433                         {
434                                 AppLogDebug("Sending user event to form:(%ls)", pForm->GetName().GetPointer());
435                                 pForm->SendUserEvent(requestId, null);
436                         }
437                 }
438         }
439         else if (requestId == IDA_VALIDATE_AND_CREATE_ACCOUNT_FINISH && pArgs != null)
440         {
441                 Frame* pFrame = GetFrame(IDS_EMAIL_TEXT);
442                 if(pFrame != null)
443                 {
444                         Form* pForm = static_cast<Form *>(pFrame->GetCurrentForm());
445                         if(pForm != null)
446                         {
447                                 if (pForm->GetName().Equals(IDL_LOGIN_FORM, true) == false &&
448                                         pForm->GetName().Equals(IDL_MANUAL_SETUP_FORM, true) == false)
449                                 {
450                                         pArgs->RemoveAll(true);
451                                         delete pArgs;
452                                         pArgs = null;
453                                 }
454                                 AppLogDebug("Sending user event to form:(%ls)", pForm->GetName().GetPointer());
455                                 pForm->SendUserEvent(IDA_VALIDATE_AND_CREATE_ACCOUNT_FINISH, pArgs);
456                         }
457                 }
458         }
459         else if (requestId == IDA_VALIDATE_AND_CREATE_ACCOUNT_FAIL)
460         {
461                 Frame* pFrame = GetFrame(IDS_EMAIL_TEXT);
462                 if(pFrame != null)
463                 {
464                         Form* pForm = static_cast<Form *>(pFrame->GetCurrentForm());
465                         if(pForm != null)
466                         {
467                                 AppLogDebug("Sending user event to form:(%ls)", pForm->GetName().GetPointer());
468                                 pForm->SendUserEvent(IDA_VALIDATE_AND_CREATE_ACCOUNT_FAIL, null);
469                         }
470                 }
471         }
472         else if (requestId == IDA_VALIDATE_ACCOUNT_FINISH)
473         {
474                 Frame* pFrame = GetFrame(IDS_EMAIL_TEXT);
475                 if(pFrame != null)
476                 {
477                         Form* pForm = static_cast<Form *>(pFrame->GetCurrentForm());
478                         if(pForm != null)
479                         {
480                                 AppLogDebug("Sending user event to form:(%ls)", pForm->GetName().GetPointer());
481                                 pForm->SendUserEvent(IDA_VALIDATE_ACCOUNT_FINISH, pArgs);
482                         }
483                 }
484         }
485         else if (requestId == IDA_VALIDATE_ACCOUNT_FAIL)
486         {
487                 Frame* pFrame = GetFrame(IDS_EMAIL_TEXT);
488                 if(pFrame != null)
489                 {
490                         Form* pForm = static_cast<Form *>(pFrame->GetCurrentForm());
491                         if(pForm != null)
492                         {
493                                 AppLogDebug("Sending user event to form:(%ls)", pForm->GetName().GetPointer());
494                                 pForm->SendUserEvent(IDA_VALIDATE_ACCOUNT_FAIL, null);
495                         }
496                 }
497         }
498         else if (requestId == IDA_NOTI_MAIL_ADD && pArgs != null)
499         {
500                 Frame* pFrame = GetFrame(IDS_EMAIL_TEXT);
501                 if(pFrame != null)
502                 {
503                         Form* pForm = pFrame->GetCurrentForm();
504                         if(pForm != null)
505                         {
506                                 AppLogDebug("Sending user event to form:(%ls)", pForm->GetName().GetPointer());
507                                 pForm->SendUserEvent(IDA_NOTI_MAIL_ADD, pArgs);
508                         }
509                 }
510         }
511         else if (requestId == IDA_NOTI_DOWNLOAD_BODY_FAIL)
512         {
513                 Frame* pFrame = GetFrame(IDS_EMAIL_TEXT);
514                 if(pFrame != null)
515                 {
516                         Form* pForm = pFrame->GetCurrentForm();
517                         if(pForm != null)
518                         {
519                                 AppLogDebug("Sending user event to form:(%ls)", pForm->GetName().GetPointer());
520                                 pForm->SendUserEvent(IDA_NOTI_DOWNLOAD_BODY_FAIL, null);
521                         }
522                 }
523         }
524         else if (requestId == IDA_NOTI_DOWNLOAD_BODY_FINISH)
525         {
526                 Frame* pFrame = GetFrame(IDS_EMAIL_TEXT);
527                 if(pFrame != null)
528                 {
529                         Form* pForm = pFrame->GetCurrentForm();
530                         if(pForm != null)
531                         {
532                                 AppLogDebug("Sending user event to form:(%ls)", pForm->GetName().GetPointer());
533                                 pForm->SendUserEvent(IDA_NOTI_DOWNLOAD_BODY_FINISH, null);
534                         }
535                 }
536         }
537         else if (requestId == IDA_NOTI_DOWNLOAD_ATTACH_START)
538         {
539                 Frame* pFrame = GetFrame(IDS_EMAIL_TEXT);
540                 if(pFrame != null)
541                 {
542                         Form* pForm = pFrame->GetCurrentForm();
543                         if(pForm != null)
544                         {
545                                 AppLogDebug("Sending user event to form:(%ls)", pForm->GetName().GetPointer());
546                                 pForm->SendUserEvent(IDA_NOTI_DOWNLOAD_ATTACH_START, pArgs);
547                         }
548                 }
549         }
550         else if (requestId == IDA_NOTI_DOWNLOAD_ATTACH_FINISH || requestId == IDA_NOTI_DOWNLOAD_ATTACH_FAIL)
551         {
552                 Frame* pFrame = GetFrame(IDS_EMAIL_TEXT);
553                 if(pFrame != null)
554                 {
555                         Form* pForm = pFrame->GetCurrentForm();
556                         if(pForm != null)
557                         {
558                                 AppLogDebug("Sending user event to form:(%ls)", pForm->GetName().GetPointer());
559                                 pForm->SendUserEvent(requestId, pArgs);
560                         }
561                 }
562         }
563         else if (requestId == IDA_NOTI_DOWNLOAD_FINISH && pArgs != null)
564         {
565                 Frame* pFrame = GetFrame(IDS_EMAIL_TEXT);
566
567                 if(pFrame != null)
568                 {
569                         Form* pForm = pFrame->GetCurrentForm();
570                         if(pForm != null)
571                         {
572                                 AppLogDebug("Sending user event to form:(%ls)", pForm->GetName().GetPointer());
573                                 pForm->SendUserEvent(IDA_NOTI_DOWNLOAD_FINISH, pArgs);
574                         }
575                 }
576         }
577         else if (requestId == IDA_NOTI_MAIL_UPDATE || requestId == IDA_NOTI_MAIL_FIELD_UPDATE)
578         {
579                 Frame* pFrame = GetFrame(IDS_EMAIL_TEXT);
580
581                 if(pFrame != null)
582                 {
583                         Form* pForm = pFrame->GetCurrentForm();
584                         if(pForm != null)
585                         {
586                                 AppLogDebug("Sending user event to form:(%ls)", pForm->GetName().GetPointer());
587                                 pForm->SendUserEvent(requestId, pArgs);
588                         }
589                 }
590         }
591         else if (requestId == IDA_SYNC_IMAP_MAILBOX_LIST_FINISH || requestId == IDA_SYNC_IMAP_MAILBOX_LIST_FAIL)
592         {
593                 Frame* pFrame = GetFrame(IDS_EMAIL_TEXT);
594
595                 if(pFrame != null)
596                 {
597                         Form* pForm = pFrame->GetCurrentForm();
598                         if(pForm != null)
599                         {
600                                 AppLogDebug("Sending user event to form:(%ls)", pForm->GetName().GetPointer());
601                                 pForm->SendUserEvent(requestId, pArgs);
602                         }
603                 }
604         }
605
606         AppLogDebug("EXIT");
607 }
608
609 void
610 ParseURI(const String* pBaseUri, ArrayList* pArgs)
611 {
612         AppLogDebug("ENTER");
613
614         int index;
615         Uri baseUri;
616
617         String strTo;
618         String strCc;
619         String strBcc;
620         String strSubject;
621         String strBody;
622         String hname, hvalue, dHValue;
623         String token;
624         String encodedURI;
625         StateInfo* pStateInfo = new (std::nothrow) StateInfo();
626
627         result r = E_SUCCESS;
628
629         if (pBaseUri != null)
630         {
631                 baseUri.SetUri(*pBaseUri);
632                 encodedURI = baseUri.GetEncodedSchemeSpecificPart();
633
634                 //set the text after 'mailto:' as part of 'to' email address
635                 encodedURI.Insert(L"to=",0);
636
637                 // Replace only the very first '?' with an '&'
638                 r = encodedURI.IndexOf(L'?',0,index);
639                 if (r == E_SUCCESS)
640                 {
641                         encodedURI.SetCharAt(L'&',index);
642                 }
643
644                 StringTokenizer strTok(encodedURI, L"&");
645                 while (strTok.HasMoreTokens())
646                 {
647                         strTok.GetNextToken(token);
648
649                         AppLogDebug("Got token:(%ls)", token.GetPointer());
650
651                         // extract pairs of the form 'hname=hvalue'
652                         r = token.IndexOf(L'=', 0, index);
653                         if (r == E_SUCCESS)
654                         {
655                                 r = token.SubString(0,index, hname);
656                                 r |= token.SubString(index+1, hvalue);
657
658                                 AppLogDebug("Got hname:(%ls), hvalue:(%ls)", hname.GetPointer(), hvalue.GetPointer());
659
660                                 if (r == E_SUCCESS && hname.IsEmpty() == false)
661                                 {
662                                         if (hname.Equals(L"to", false) == true && hvalue.IsEmpty() == false)
663                                         {
664                                                 UrlDecoder::Decode(hvalue, L"UTF-8", dHValue);
665                                                 strTo.Append(dHValue);
666                                                 strTo.Append(", ");
667                                         }
668                                         else if (hname.Equals(L"cc", false) == true && hvalue.IsEmpty() == false)
669                                         {
670                                                 UrlDecoder::Decode(hvalue, L"UTF-8", dHValue);
671                                                 strCc.Append(dHValue);
672                                                 strCc.Append(", ");
673                                         }
674                                         else if (hname.Equals(L"bcc", false) == true && hvalue.IsEmpty() == false)
675                                         {
676                                                 UrlDecoder::Decode(hvalue, L"UTF-8", dHValue);
677                                                 strBcc.Append(dHValue);
678                                                 strBcc.Append(", ");
679                                         }
680                                         else if (hname.Equals(L"subject", false) == true)
681                                         {
682                                                 UrlDecoder::Decode(hvalue, L"UTF-8", dHValue);
683                                                 strSubject.Clear();
684                                                 strSubject.Append(dHValue);
685                                         }
686                                         else if (hname.Equals(L"body", false) == true && hvalue.IsEmpty() == false)
687                                         {
688                                                 if (strBody.IsEmpty() == true && hvalue.IsEmpty() == false)
689                                                 {
690                                                         strBody.Append(hvalue);
691                                                 }
692                                                 else if (strBody.IsEmpty() == false && hvalue.IsEmpty() == false)
693                                                 {
694                                                         strBody.Append(L"%0D%0A");
695                                                         strBody.Append(hvalue);
696                                                 }
697                                                 else if (strBody.IsEmpty() == false && hvalue.IsEmpty() == true)
698                                                 {
699                                                         strBody.Append(L"%0D%0A");
700                                                 }
701                                         }
702                                 }
703                         }
704                 }
705
706                 if (strBody.IsEmpty() == false)
707                 {
708                         UrlDecoder::Decode(strBody, L"UTF-8", dHValue);
709                         strBody.Clear(); strBody.Append(dHValue);
710                 }
711         }
712
713         Utils::BuildEmailIdStringFromCSV(strTo, token);
714 //      pArgs->Add(new (std::nothrow) String(token));       // To
715         pStateInfo->toFieldValue = token;
716
717         Utils::BuildEmailIdStringFromCSV(strCc, token);
718 //      pArgs->Add(new (std::nothrow) String(token));       // Cc
719         pStateInfo->ccFieldValue = token;
720
721         Utils::BuildEmailIdStringFromCSV(strBcc, token);
722 //      pArgs->Add(new (std::nothrow) String(token));       // Bcc
723         pStateInfo->bccFieldValue = token;
724
725 //      pArgs->Add(new (std::nothrow) String(strSubject));  // Subject
726 //      pArgs->Add(new (std::nothrow) String(strBody));     // Body
727 //      pArgs->Add(new (std::nothrow) String());            // Attachments
728
729         pStateInfo->subjectFieldValue = strSubject;
730         pStateInfo->bodyFieldValue = strBody;
731         pStateInfo->attachmentsPath = L"";
732
733         pArgs->Add(pStateInfo);
734
735         AppLogDebug("to:(%ls)", strTo.GetPointer());
736         AppLogDebug("cc:(%ls)", strCc.GetPointer());
737         AppLogDebug("bcc:(%ls)", strBcc.GetPointer());
738         AppLogDebug("subj:(%ls)", strSubject.GetPointer());
739         AppLogDebug("body:(%ls)", strBody.GetPointer());
740
741         AppLogDebug("EXIT");
742         return;
743 }
744
745 void
746 EmailApp::OnAppControlRequestReceived(RequestId reqId, const String& operationId, const String* pUriData, const String* pMimeType, const IMap* pExtraData)
747 {
748         AppLogDebug("ENTER, reqID:(%ld), operationID:(%ls)", reqId, operationId.GetPointer());
749         AppLogDebug("===", Utils::ParseExtraData(pExtraData));
750
751         __appControlRequestId = reqId;
752
753         if (operationId.Equals(IDS_TIZEN_OPERATIONID_COMPOSE, true))
754         {
755                 __opId = EMAIL_OPERATION_COMPOSE;
756
757                 AppLogDebug("Going to compose scene");
758
759                 const String* pTmpVal = null;
760                 const ArrayList* pTmpArr = null;
761                 StateInfo* pStateInfo = new (std::nothrow) StateInfo();
762
763                 __pArgs = new (std::nothrow) ArrayList();
764                 __pArgs->Construct();
765
766                 if (pExtraData)
767                 {
768                         pTmpVal = static_cast<const String*>(pExtraData->GetValue(String(IDS_APPCONTROL_REQUEST_KEY_TO)));
769                         if (pTmpVal != null)
770                         {
771                                 String tmpStr(*pTmpVal);
772                                 tmpStr.Replace(L",", IDS_DELIM_SEMICOLON);
773 //                              __pArgs->Add(new (std::nothrow) String(tmpStr));
774                                 pStateInfo->toFieldValue = tmpStr;
775                         }
776                         else
777                         {
778 //                              __pArgs->Add(new (std::nothrow) String());
779                                 pStateInfo->toFieldValue = L"";
780                         }
781
782                         pTmpVal = static_cast<const String*>(pExtraData->GetValue(String(IDS_APPCONTROL_REQUEST_KEY_CC)));
783                         if (pTmpVal != null)
784                         {
785                                 String tmpStr(*pTmpVal);
786                                 tmpStr.Replace(L",", IDS_DELIM_SEMICOLON);
787 //                              __pArgs->Add(new (std::nothrow) String(tmpStr));
788                                 pStateInfo->ccFieldValue = tmpStr;
789                         }
790                         else
791                         {
792 //                              __pArgs->Add(new (std::nothrow) String());
793                                 pStateInfo->ccFieldValue = L"";
794                         }
795
796                         pTmpVal = static_cast<const String*>(pExtraData->GetValue(String(IDS_APPCONTROL_REQUEST_KEY_BCC)));
797                         if (pTmpVal != null)
798                         {
799                                 String tmpStr(*pTmpVal);
800                                 tmpStr.Replace(L",", IDS_DELIM_SEMICOLON);
801 //                              __pArgs->Add(new (std::nothrow) String(tmpStr));
802                                 pStateInfo->bccFieldValue = tmpStr;
803                         }
804                         else
805                         {
806 //                              __pArgs->Add(new (std::nothrow) String());
807                                 pStateInfo->bccFieldValue = L"";
808                         }
809
810                         pTmpVal = static_cast<const String*>(pExtraData->GetValue(String(IDS_APPCONTROL_REQUEST_KEY_SUBJECT)));
811                         if (pTmpVal != null)
812                         {
813 //                              __pArgs->Add(new (std::nothrow) String(*pTmpVal));
814                                 pStateInfo->subjectFieldValue = *pTmpVal;
815                         }
816                         else
817                         {
818 //                              __pArgs->Add(new (std::nothrow) String());
819                                 pStateInfo->subjectFieldValue = L"";
820                         }
821
822                         pTmpVal = static_cast<const String*>(pExtraData->GetValue(String(IDS_APPCONTROL_REQUEST_KEY_TEXT)));
823                         if (pTmpVal != null)
824                         {
825 //                              __pArgs->Add(new (std::nothrow) String(*pTmpVal));
826                                 pStateInfo->bodyFieldValue = *pTmpVal;
827                         }
828                         else
829                         {
830 //                              __pArgs->Add(new (std::nothrow) String());
831                                 pStateInfo->bodyFieldValue = L"";
832                         }
833
834                         pTmpArr = static_cast<const ArrayList*>(pExtraData->GetValue(String(IDS_APPCONTROL_REQUEST_KEY_ATTACH)));
835                         if (pTmpArr != null)
836                         {
837                                 String val;
838                                 String tmpVal;
839
840                                 for (int i=0; i<pTmpArr->GetCount(); i++)
841                                 {
842                                         tmpVal.Clear();
843                                         tmpVal.Append(*static_cast<const String*>(pTmpArr->GetAt(i)));
844
845                                         val.Append(tmpVal);
846                                         val.Append(IDS_DELIM_SEMICOLON);
847                                 }
848
849 //                              __pArgs->Add(new (std::nothrow) String(val));
850                                 pStateInfo->attachmentsPath = val;
851                         }
852                         else
853                         {
854 //                              __pArgs->Add(new (std::nothrow) String());
855                                 pStateInfo->attachmentsPath = L"";
856                         }
857
858                         pTmpVal = static_cast<const String*>(pExtraData->GetValue(String(IDS_APPCONTROL_REQUEST_KEY_RETURN_RESULT)));
859                         if (pTmpVal != null && pTmpVal->Equals(L"true", false) == true)
860                         {
861                                 SetAppControlResult(APP_CTRL_RESULT_SUCCEEDED);
862                         }
863                         __pArgs->Add(pStateInfo);
864                 }
865                 else if (pUriData != null)
866                 {
867                         AppLogDebug("Parsing uridata");
868
869                         //handle mailto uri scheme...
870                         const String* pBaseUri = pUriData;
871                         ParseURI(pBaseUri, __pArgs);
872                 }
873                 else
874                 {
875                         AppLogDebug("ExtraData and pUriData were null");
876 //                      __pArgs->Add(new (std::nothrow) String());
877 //                      __pArgs->Add(new (std::nothrow) String());
878 //                      __pArgs->Add(new (std::nothrow) String());
879 //                      __pArgs->Add(new (std::nothrow) String());
880 //                      __pArgs->Add(new (std::nothrow) String());
881 //                      __pArgs->Add(new (std::nothrow) String());
882                         __pArgs->Add(pStateInfo);
883                 }
884         }
885         else if (operationId.Equals(IDS_TIZEN_OPERATIONID_ACCOUNT_ADD, true))
886         {
887                 AppLogDebug("Going to add account scene");
888                 __opId = EMAIL_OPERATION_ADD_ACCOUNT;
889         }
890         else if (operationId.Equals(IDS_TIZEN_OPERATIONID_ACCOUNT_CONFIGURE, true))
891         {
892                 AppLogDebug("Going to account config scene");
893                 __opId = EMAIL_OPERATION_ACCOUNT_CONFIG;
894
895                 if (pExtraData != null)
896                 {
897                         Integer::Parse(*static_cast<const String*>(pExtraData->GetValue(String(IDS_TIZEN_APPCONTROL_KEY_ACCOUNT_ID))), __accountId);
898                         AppLogDebug("Got Account DB account id:(%d)", __accountId);
899
900                         IMapEnumerator* pMapEnum = EmailSettingsProvider::GetInstance()->GetAccountListN();
901
902                         if (pMapEnum != null)
903                         {
904                                 while (!IsFailed(pMapEnum->MoveNext()))
905                                 {
906                                         EmailAccount* pAccount = dynamic_cast<EmailAccount*>(pMapEnum->GetValue());
907
908                                         if (pAccount != null && pAccount->GetAccountSvcId() == __accountId)
909                                         {
910                                                 __accountId = pAccount->GetAccountId();
911                                                 break;
912                                         }
913                                 }
914                                 delete pMapEnum;
915                         }
916                 }
917                 else
918                 {
919                         AppLogDebug("ExtraData was null");
920                 }
921         }
922         else if (operationId.Equals(IDS_TIZEN_OPERATIONID_CONFIGURE, true))
923         {
924                 AppLogDebug("Going to configure scene");
925                 __opId = EMAIL_OPERATION_SETTINGS;
926         }
927         else if (operationId.Equals(IDS_TIZEN_OPERATIONID_SHARE_TEXT, true))
928         {
929                 __opId = EMAIL_OPERATION_COMPOSE;
930
931                 const String* pTmpVal = null;
932                 StateInfo* pStateInfo = new (std::nothrow) StateInfo();
933
934                 __pArgs = new (std::nothrow) ArrayList();
935                 __pArgs->Construct();
936
937                 if (pExtraData)
938                 {
939                         pTmpVal = static_cast<const String*>(pExtraData->GetValue(String(IDS_APPCONTROL_REQUEST_KEY_SHARE_TEXT)));
940
941                         if (pTmpVal != null)
942                         {
943                                 pStateInfo->bodyFieldValue = *pTmpVal;
944                         }
945                 }
946
947                 SetAppControlResult(APP_CTRL_RESULT_SUCCEEDED);
948                 __pArgs->Add(pStateInfo);
949         }
950
951         AppLogDebug("EXIT");
952         return;
953 }
954
955 OperationType
956 EmailApp::GetOperationType(void)
957 {
958         return __opId;
959 }
960
961 void
962 EmailApp::SetAppControlResult(AppCtrlResult appCtrlResult)
963 {
964         __returnAppControlResult = true;
965         __appControlResult = appCtrlResult;
966         return;
967 }
968
969 bool
970 EmailApp::IsReturnResultSet(void)
971 {
972         return __returnAppControlResult;
973 }
974
975 void
976 EmailApp::OnSettingChanged(String& key)
977 {
978         AppLogDebug("ENTER, key:(%ls)", key.GetPointer());
979
980         String languageSettingKey = L"http://tizen.org/setting/locale.language";
981         String timeFormatKey = L"http://tizen.org/setting/locale.time.format.24hour";
982         String fontSettingKey = L"http://tizen.org/setting/font.size";
983         String languageSelected;
984         bool is24HourFormat;
985
986         if (key.Equals(languageSettingKey, false) == true)
987         {
988                 SettingInfo::GetValue(languageSettingKey, languageSelected);
989
990                 if (__currentDisplayLanguage.Equals(languageSelected, false) == false)
991                 {
992                         Application::Terminate();
993                 }
994         }
995
996         if (key.Equals(timeFormatKey, false) == true)
997         {
998                 SettingInfo::GetValue(timeFormatKey, is24HourFormat);
999
1000                 if (__is24HourFormat != is24HourFormat)
1001                 {
1002                         Application::Terminate();
1003                 }
1004         }
1005
1006         if (key.Equals(fontSettingKey, false) == true)
1007         {
1008                 Application::Terminate();
1009         }
1010
1011         AppLogDebug("EXIT");
1012 }
1013
1014 void
1015 EmailApp::DisableFrame(void)
1016 {
1017         AppLogDebug("ENTER");
1018
1019         __frameDisabled = true;
1020         GetAppFrame()->GetFrame()->SetEnabled(false);
1021
1022         AppLogDebug("EXIT");
1023 }
1024
1025 void
1026 EmailApp::EnableFrame(void)
1027 {
1028         AppLogDebug("ENTER");
1029
1030         if (__frameDisabled)
1031         {
1032                 AppLogDebug("Enabling frame");
1033                 GetAppFrame()->GetFrame()->SetEnabled(true);
1034                 GetAppFrame()->GetFrame()->Invalidate(true);
1035                 __frameDisabled = false;
1036         }
1037
1038         AppLogDebug("EXIT");
1039 }
1040
1041 bool
1042 EmailApp::GetShowDeletePopupStatus(int& mailId, DeletionMode& delMode)
1043 {
1044         AppLogDebug("Returning bool:(%d), mailId:(%d)", __showDeletePopup, __mailId);
1045
1046         delMode = __delMode;
1047         mailId  = __mailId;
1048
1049         return __showDeletePopup;
1050 }
1051
1052 void
1053 EmailApp::SetShowDeletePopupStatus(DeletionMode delMode, const int mailId)
1054 {
1055         AppLogDebug("ENTER, delMode:(%d), mailId:(%d)", delMode, mailId);
1056
1057         __delMode = delMode;
1058         __mailId  = mailId;
1059         __showDeletePopup = true;
1060
1061         AppLogDebug("EXIT");
1062 }
1063
1064 void
1065 EmailApp::ClearShowDeletePopupStatus(void)
1066 {
1067         AppLogDebug("Cleared show delete popup status");
1068
1069         __mailId = -1;
1070         __showDeletePopup = false;
1071 }
1072
1073 bool
1074 EmailApp::GetDownloadBodyPopupStatus(int& accountId, int& handle)
1075 {
1076         accountId = __downloadBodyAccountId;
1077         handle = __downloadBodyHandle;
1078
1079         return __downloadBodyHandle;
1080 }
1081
1082 void
1083 EmailApp::SetDownloadBodyPopupStatus(int accountId, int handle)
1084 {
1085         __downloadBodyAccountId = accountId;
1086         __downloadBodyHandle = handle;
1087
1088         return;
1089 }
1090
1091 void
1092 EmailApp::ClearDownloadBodyPopupStatus(void)
1093 {
1094         __downloadBodyAccountId = 0;
1095         __downloadBodyHandle = 0;
1096
1097         return;
1098 }
1099
1100 bool
1101 EmailApp::GetDownloadAttachmentPopupStatus(int& mailId, DownloadInitiatorMode& forwardMode)
1102 {
1103         AppLogDebug("ENTER");
1104
1105         mailId = __downloadMailId;
1106         forwardMode = __downloadMode;
1107
1108         AppLogDebug("EXIT maildId:(%d), forwardMode:(%d)", mailId, forwardMode);
1109         return __showDownloadAttachmentPopup;
1110 }
1111
1112 void
1113 EmailApp::SetDownloadAttachmentPopupStatus(const int mailId, DownloadInitiatorMode downloadMode)
1114 {
1115         AppLogDebug("ENTER");
1116
1117         __downloadMailId = mailId;
1118         __downloadMode = downloadMode;
1119         __showDownloadAttachmentPopup = true;
1120
1121         AppLogDebug("EXIT");
1122 }
1123
1124 void
1125 EmailApp::ClearDownloadAttachmentPopupStatus(void)
1126 {
1127         AppLogDebug("ENTER");
1128
1129         __downloadMailId = -1;
1130         __showDownloadAttachmentPopup = false;
1131
1132         AppLogDebug("EXIT");
1133 }
1134
1135 void
1136 EmailApp::SetDownloadFailedPopupStatus(bool showPopup)
1137 {
1138         AppLogDebug("ENTER");
1139
1140         __showDownloadFailedPopup = showPopup;
1141
1142         AppLogDebug("EXIT");
1143 }
1144
1145 bool
1146 EmailApp::GetDownloadFailedPopupStatus(void)
1147 {
1148         AppLogDebug("ENTER & EXIT");
1149         return __showDownloadFailedPopup;
1150 }
1151
1152 void
1153 EmailApp::SetMoveOrDeleteFailedPopupStatus(bool isMoveFailed)
1154 {
1155         AppLogDebug("ENTER");
1156
1157         __showMoveOrDeleteFailedPopup = true;
1158         __isMoveFailed = isMoveFailed;
1159
1160         AppLogDebug("EXIT");
1161 }
1162
1163 bool
1164 EmailApp::GetMoveOrDeleteFailedPopupStatus(bool& isMoveFailed)
1165 {
1166         AppLogDebug("ENTER");
1167
1168         isMoveFailed = __isMoveFailed;
1169
1170         AppLogDebug("EXIT");
1171         return __showMoveOrDeleteFailedPopup;
1172 }
1173
1174 void
1175 EmailApp::ClearMoveOrDeleteFailedPopupStatus(void)
1176 {
1177         AppLogDebug("ENTER");
1178
1179         __showMoveOrDeleteFailedPopup = false;
1180         __isMoveFailed = false;
1181
1182         AppLogDebug("EXIT");
1183 }
1184
1185 long long
1186 EmailApp::GetAvailableMemory(void)
1187 {
1188         AppLogDebug("ENTER");
1189
1190         result r = E_SUCCESS;
1191
1192         String key(L"http://tizen.org/runtime/storage.available.internal");
1193         long long allocatedMemory = 0;
1194
1195         r = RuntimeInfo::GetValue(key, allocatedMemory);
1196         TryCatch(r == E_SUCCESS, , "MyRuntimeInfo: Failed to get value");
1197
1198         AppLogDebug("EXIT, allocatedMemory:(%ll)", allocatedMemory);
1199         return allocatedMemory;
1200
1201 CATCH:
1202         AppLogDebug("EXIT, allocatedMemory:(0)");
1203         return 0;
1204 }
1205
1206 void
1207 EmailApp::RegisterScenes(void)
1208 {
1209         AppLogDebug("ENTER");
1210
1211         result r = E_SUCCESS;
1212         SceneManager* pSceneManager = SceneManager::GetInstance();
1213
1214         static EmailFormFactory formFactory;
1215         r = pSceneManager->RegisterFormFactory(formFactory);
1216         AppLogDebugIf(r != E_SUCCESS, "Failed to register form factory");
1217
1218         r |= pSceneManager->RegisterScene(IDSCN_ADD_ACCOUNT,              ADD_ACCOUNT_FORM,              L"");
1219         r |= pSceneManager->RegisterScene(IDSCN_INBOX,                    INBOX_FORM,                    L"");
1220         r |= pSceneManager->RegisterScene(IDSCN_EMAIL_COMPOSER,           EMAIL_COMPOSER_FORM,           L"");
1221         r |= pSceneManager->RegisterScene(IDSCN_EMAIL_DETAIL,             EMAIL_DETAIL_FORM,             L"");
1222         r |= pSceneManager->RegisterScene(IDSCN_MOVEMAIL,                 MOVEMAIL_FORM,                 L"");
1223         r |= pSceneManager->RegisterScene(IDSCN_MOVEMAIL_SELECTED,        MOVEMAIL_SELECTED_FORM,        L"");
1224         r |= pSceneManager->RegisterScene(IDSCN_EMAIL_SETTING,            EMAIL_SETTING_FORM,            L"");
1225         r |= pSceneManager->RegisterScene(IDSCN_ADD_ADDRESS,              ADD_ADDRESS_FORM,              L"");
1226         r |= pSceneManager->RegisterScene(IDSCN_EMAIL_BLOCK,              BLOCK_EMAIL_FORM,              L"");
1227         r |= pSceneManager->RegisterScene(IDSCN_ACCOUNT_SETTING,          ACCOUNT_SETTING_FORM,          L"");
1228         r |= pSceneManager->RegisterScene(IDSCN_ADD_SUBJECT,              ADD_SUBJECT_FORM,              L"");
1229         r |= pSceneManager->RegisterScene(IDSCN_DELETE_ADDRESS_SUBJECT,   DELETE_ADDRESS_SUBJECT_FORM,   L"");
1230         r |= pSceneManager->RegisterScene(IDSCN_SETTING_SETUP_MAILBOX,    SETTING_SETUP_MAILBOX_FORM,    L"");
1231         r |= pSceneManager->RegisterScene(IDSCN_ADD_ACCOUNT_MANUAL_SETUP, ADD_ACCOUNT_MANUAL_SETUP_FORM, L"");
1232         r |= pSceneManager->RegisterScene(IDSCN_SEARCH,                   SEARCH_FORM,                   L"");
1233         r |= pSceneManager->RegisterScene(IDSCN_SPLIT_VIEW,               SPLIT_VIEW_FORM,               L"");
1234         r |= pSceneManager->RegisterScene(IDSCN_FOLDERS_VIEW,             FOLDERS_VIEW_FORM,             L"");
1235         r |= pSceneManager->RegisterScene(IDSCN_ACCOUNT_CONFIG,           ACCOUNT_CONFIG_FORM,           L"");
1236
1237         AppLogDebugIf(r != E_SUCCESS, "Failed to register scene");
1238
1239         AppLogDebug("EXIT");
1240 }