Fix for N_SE-54326
[apps/osp/Calendar.git] / src / ClCalendarApp.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  * @file        ClCalendarApp.cpp
19  * @brief       This is the implementation file for the CalendarApp class.
20  */
21
22 #include <FIo.h>
23 #include <FSocial.h>
24 #include "ClCalendarApp.h"
25 #include "ClMainFrame.h"
26 #include "ClResourceManager.h"
27
28 using namespace Tizen::App;
29 using namespace Tizen::Base;
30 using namespace Tizen::Base::Collection;
31 using namespace Tizen::Io;
32 using namespace Tizen::Social;
33 using namespace Tizen::System;
34 using namespace Tizen::Ui;
35 using namespace Tizen::Ui::Controls;
36 using namespace Tizen::Ui::Scenes;
37
38
39 const int MIN_MEMORY_NEEDED = 1048576;
40
41 CalendarApp::CalendarApp(void)
42         : __type(OPERATION_TYPE_MAIN)
43         , __requestId(INVALID_REQUEST_ID)
44         , __initialScene(IDSCN_MONTH)
45         , __pArgs(null)
46         , __pInitialSceneArgument(null)
47 {
48 }
49
50 CalendarApp::~CalendarApp(void)
51 {
52         delete __pArgs;
53         delete __pInitialSceneArgument;
54 }
55
56 UiApp*
57 CalendarApp::CreateInstance(void)
58 {
59         // Create the instance through the constructor.
60         return new (std::nothrow) CalendarApp();
61 }
62
63 const String
64 CalendarApp::GetExportVcsFilePath(void)
65 {
66         DateTime currentTime;
67         SystemTime::GetCurrentTime(TIME_MODE_WALL, currentTime);
68
69         String path;
70         path.Format(30, VCS_EXPORT_PATH, currentTime.GetYear(), currentTime.GetMonth(), currentTime.GetDay(),
71                                                                          currentTime.GetHour(), currentTime.GetMinute(), currentTime.GetSecond());
72
73         if (File::IsFileExist(path) == true)
74         {
75                 int count = 0;
76                 String nextPath;
77                 do
78                 {
79                         String countString;
80                         countString.Format(5, L"-%3d", ++count);
81                         nextPath = path;
82                         nextPath.Insert(countString, path.GetLength() - 4);
83                 } while (File::IsFileExist(nextPath) == true);
84                 path = nextPath;
85         }
86
87         path = Application::GetInstance()->GetAppSharedPath() + path;
88
89         return path;
90 }
91
92 const String*
93 CalendarApp::GetArgument(const String& key) const
94 {
95         if (__pArgs == null)
96         {
97                 return null;
98         }
99         return static_cast<const String*>(__pArgs->GetValue(key));
100 }
101
102 const String&
103 CalendarApp::GetInitialScene(void) const
104 {
105         return __initialScene;
106 }
107
108 const IList*
109 CalendarApp::GetInitialSceneArgumentN(void) const
110 {
111         ArrayList* pArgs = null;
112         if (__pInitialSceneArgument != null)
113         {
114                 pArgs = new (std::nothrow) ArrayList(SingleObjectDeleter);
115                 pArgs->Construct(__pInitialSceneArgument->GetCount());
116                 IEnumerator* pEnum = __pInitialSceneArgument->GetEnumeratorN();
117                 while (pEnum->MoveNext() == E_SUCCESS)
118                 {
119                         if (dynamic_cast<const CalEvent*>(pEnum->GetCurrent()) != null)
120                         {
121                                 pArgs->Add(new (std::nothrow) CalEvent(static_cast<const CalEvent&>(*pEnum->GetCurrent())));
122                         }
123                         else if (dynamic_cast<const CalTodo*>(pEnum->GetCurrent()) != null)
124                         {
125                                 pArgs->Add(new (std::nothrow) CalTodo(static_cast<const CalTodo&>(*pEnum->GetCurrent())));
126                         }
127                 }
128                 delete pEnum;
129         }
130         return pArgs;
131 }
132
133 const String&
134 CalendarApp::GetMimeType(void) const
135 {
136         return __mimeType;
137 }
138
139 OperationType
140 CalendarApp::GetOperationType(void) const
141 {
142         return __type;
143 }
144
145 RequestId
146 CalendarApp::GetRequestId(void) const
147 {
148         return __requestId;
149 }
150
151 const String&
152 CalendarApp::GetUri(void) const
153 {
154         return __uriScheme;
155 }
156
157 void
158 CalendarApp::AddCalendarAppStatusChangedEventListener(ICalendarAppStatusChangedEventListener& listener)
159 {
160         __listeners.Add(&listener);
161 }
162
163 void
164 CalendarApp::RemoveCalendarAppStatusChangedEventListener(ICalendarAppStatusChangedEventListener& listener)
165 {
166         __listeners.Remove(listener);
167 }
168
169 bool
170 CalendarApp::OnAppInitializing(AppRegistry& appRegistry)
171 {
172         // Set AppControl Provider
173         AppControlProviderManager::GetInstance()->SetAppControlProviderEventListener(this);
174
175         return true;
176 }
177
178 bool
179 CalendarApp::OnAppInitialized(void)
180 {
181         // Create a Frame
182         MainFrame* pMainFrame = new (std::nothrow) MainFrame();
183
184         pMainFrame->Construct();
185         AddFrame(*pMainFrame);
186
187         if (__type == OPERATION_TYPE_MAIN)
188         {
189                 // Set SettingInfo event listener
190                 SettingInfo::AddSettingEventListener(*this);
191         }
192
193         //Get Current Language Reigon Info
194         SettingInfo::GetValue(KEY_SYSTEM_LANGUAGE, __currentDisplayLang);
195         SettingInfo::GetValue(KEY_SYSTEM_COUNTRY, __currentRegion);
196         AppManager::GetInstance()->AddActiveAppEventListener(*this);
197
198         return true;
199 }
200
201 bool
202 CalendarApp::OnAppTerminating(AppRegistry& appRegistry, bool forcedTermination)
203 {
204         AppManager::GetInstance()->RemoveActiveAppEventListener(*this);
205         return true;
206 }
207
208 void
209 CalendarApp::OnForeground(void)
210 {
211         IEnumerator* pEnum = __listeners.GetEnumeratorN();
212         while (pEnum->MoveNext() == E_SUCCESS)
213         {
214                 static_cast<ICalendarAppStatusChangedEventListener*>(pEnum->GetCurrent())->OnForeground();
215         }
216         delete pEnum;
217
218         AppAssertf(GetAppFrame() != null && GetAppFrame()->GetFrame() != null, "[E_FAILURE] Unable to get frame.");
219
220         // Todo: temporary solution for appcontrol.
221         if (!GetAppFrame()->GetFrame()->IsEnabled())
222         {
223                 GetAppFrame()->GetFrame()->SetEnabled(true);
224                 GetAppFrame()->GetFrame()->Invalidate(true);
225         }
226 }
227
228 void
229 CalendarApp::OnBackground(void)
230 {
231         IEnumerator* pEnum = __listeners.GetEnumeratorN();
232         while (pEnum->MoveNext() == E_SUCCESS)
233         {
234                 static_cast<ICalendarAppStatusChangedEventListener*>(pEnum->GetCurrent())->OnBackground();
235         }
236         GetAppFrame()->GetFrame()->SetEnabled(true);
237         delete pEnum;
238 }
239
240 void
241 CalendarApp::OnAppControlRequestReceived(RequestId reqId, const String& operationId, const String* pUriData,
242                                                                                         const String* pMimeType, const IMap* pExtraData)
243 {
244         AppLogDebug("%ls", operationId.GetPointer());
245         if (pMimeType != null)
246         {
247                 __mimeType = *pMimeType;
248                 AppLogDebug("MimeType : %ls", __mimeType.GetPointer());
249         }
250         if (pUriData != null)
251         {
252                 __uriScheme = *pUriData;
253                 AppLogDebug("Uri : %ls", __uriScheme.GetPointer());
254         }
255         __requestId = reqId;
256
257         if (pExtraData != null)
258         {
259                 HashMap* pArgs = new (std::nothrow) HashMap(SingleObjectDeleter);
260                 pArgs->Construct(pExtraData->GetCount());
261                 IMapEnumerator* pEnum = pExtraData->GetMapEnumeratorN();
262                 while (pEnum->MoveNext() == E_SUCCESS)
263                 {
264                         const String* pKey = dynamic_cast<const String*>(pEnum->GetKey());
265                         const String* pValue = dynamic_cast<const String*>(pEnum->GetValue());
266                         if (pKey != null && pValue != null)
267                         {
268                                 AppLogDebug("Add key:%ls, value:%ls", pKey->GetPointer(), pValue->GetPointer());
269                                 pArgs->Add(new (std::nothrow) String(*pKey), new (std::nothrow) String (*pValue));
270                         }
271                 }
272                 delete pEnum;
273                 __pArgs = pArgs;
274         }
275
276         bool requestFailed = true;
277         if (operationId == ID_OPERATION_MAIN)
278         {
279                 requestFailed = false;
280         }
281         else if (operationId == ID_OPERATION_VIEW)
282         {
283                 if (__uriScheme.StartsWith(FILE_URL_HEADER, 0) == true)
284                 {
285                         String path = __uriScheme;
286                         path.Replace(FILE_URL_HEADER, L"");
287
288                         __pInitialSceneArgument = ResourceManager::ParseVcsFileN(path);
289                         __initialScene = IDSCN_VCS_SELECTOR;
290                         __mimeType = MIME_TYPE_VCALENDAR;
291                         __type = OPERATION_TYPE_VIEW;
292                         requestFailed = false;
293
294                 }
295         }
296         else if (operationId == ID_OPERATION_SOCIAL_PICK)
297         {
298                 const String* pItemType = GetArgument(KEY_SOCIAL_ITEM_TYPE);
299                 if (pItemType != null)
300                 {
301                         if (pItemType->Equals(VALUE_CALENDAR_EVENT_TYPE, false) == true)
302                         {
303                                 __initialScene = IDSCN_EVENT_SELECTOR;
304                                 __type = OPERATION_TYPE_PICK;
305                                 requestFailed = false;
306                         }
307                         else if (pItemType->Equals(VALUE_CALENDAR_TODO_TYPE, false) == true)
308                         {
309                                 __initialScene = IDSCN_TASK_SELECTOR;
310                                 __type = OPERATION_TYPE_PICK;
311                                 requestFailed = false;
312                         }
313                 }
314         }
315         else if (operationId == ID_OPERATION_SOCIAL_VIEW)
316         {
317                 const String* pItemType = GetArgument(KEY_SOCIAL_ITEM_TYPE);
318                 if (pItemType != null)
319                 {
320                         if (pItemType->Equals(VALUE_CALENDAR_EVENT_TYPE, false) == true)
321                         {
322                                 __initialScene = IDSCN_EVENT_DETAILS;
323                                 __type = OPERATION_TYPE_VIEW;
324                                 requestFailed = false;
325                         }
326                         else if (pItemType->Equals(VALUE_CALENDAR_TODO_TYPE, false) == true)
327                         {
328                                 __initialScene = IDSCN_TASK_DETAILS;
329                                 __type = OPERATION_TYPE_VIEW;
330                                 requestFailed = false;
331                         }
332                 }
333         }
334         else if (operationId == ID_OPERATION_SOCIAL_EDIT)
335         {
336                 const String* pItemType = GetArgument(KEY_SOCIAL_ITEM_TYPE);
337                 if (pItemType != null)
338                 {
339                         if (pItemType->Equals(VALUE_CALENDAR_EVENT_TYPE, false) == true)
340                         {
341                                 __initialScene = IDSCN_EVENT_EDITOR;
342                                 __type = OPERATION_TYPE_EDIT;
343                                 requestFailed = false;
344                         }
345                 }
346         }
347
348         if (requestFailed)
349         {
350                 result r = AppControlProviderManager::GetInstance()->SendAppControlResult(__requestId, APP_CTRL_RESULT_FAILED, null);
351                 AppLogDebugIf(r != E_SUCCESS, "[%s] Unable to return result.", GetErrorMessage(r));
352                 Terminate();
353         }
354 }
355
356 void
357 CalendarApp::OnSettingChanged(String& key)
358 {
359         AppLogDebug("key: %ls", key.GetPointer());
360         String changedLanguage;
361         String changedRegion;
362         SettingInfo::GetValue(KEY_SYSTEM_LANGUAGE, changedLanguage);
363         SettingInfo::GetValue(KEY_SYSTEM_COUNTRY, changedRegion);
364         if (key == KEY_SYSTEM_LANGUAGE
365                 || key == KEY_SYSTEM_COUNTRY
366                 || key == KEY_SYSTEM_TIME_ZONE
367                 || key == L"http://tizen.org/setting/font.size")
368         {
369                 if (!(key == KEY_SYSTEM_LANGUAGE && __currentDisplayLang.Equals(changedLanguage)) && !(key == KEY_SYSTEM_COUNTRY &&
370                                 __currentRegion.Equals(changedRegion)))
371                 {
372                         if (__type != OPERATION_TYPE_MAIN)
373                         {
374                                 result r = AppControlProviderManager::GetInstance()->SendAppControlResult(__requestId, APP_CTRL_RESULT_TERMINATED, null);
375                                 AppLogDebugIf(r != E_SUCCESS, "[%s] Unable to return result.", GetErrorMessage(r));
376                         }
377                         Terminate();
378                 }
379         }
380 }
381
382 void
383 CalendarApp::OnActiveAppChanged(const Tizen::Base::String& appId)
384 {
385         if(GetAppId().Equals(appId) == true)
386         {
387                 AppLogDebug("OnActiveAppChanged %ls",appId.GetPointer());
388                 long long mem = GetAvailableMemory();
389                 AppLogDebug("%lld",mem);
390                 if(mem < MIN_MEMORY_NEEDED)
391                 {
392                         MessageBox LowMemory;
393                         String msg = L"";
394                         LowMemory.Construct(L"",ResourceManager::GetString(IDS_COM_POP_NOT_ENOUGH_MEMORY_DELETE_SOME_ITEMS),  MSGBOX_STYLE_OK);
395                         int modalResult = 0;
396                         // Calls ShowAndWait() : Draws and Shows itself and processes events
397                         LowMemory.ShowAndWait(modalResult);
398                         Terminate();
399                 }
400         }
401
402 }
403
404 long long
405 CalendarApp::GetAvailableMemory(void)
406 {
407     result r = E_SUCCESS;
408
409     String key(L"http://tizen.org/runtime/storage.available.internal");
410     long long allocatedMemory = 0;
411
412     r = RuntimeInfo::GetValue(key, allocatedMemory);
413     TryCatch(r == E_SUCCESS, , "MyRuntimeInfo: Failed to get value");
414
415     return allocatedMemory;
416
417    CATCH:
418     return 0;
419 }