Fix memory leaks
[apps/osp/Gallery.git] / src / GlSettingPresentationModel.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                GlSettingPresentationModel.cpp
19  * @brief               This is the implementation file for SettingPresentationModel class.
20  */
21
22 #include <cstdlib>
23 #include "GlSettingPresentationModel.h"
24
25 using namespace Tizen::App;
26 using namespace Tizen::Base;
27 using namespace Tizen::Io;
28
29 static const int DEFAULT_VALUE_EFFECT_SLIDE = 0;
30 static const int DEFAULT_VALUE_INTERVAL = 3;
31 static const int DEFAULT_VALUE_REPEAT = 0;
32 static const int DEFAULT_VALUE_SHUFFLE = 0;
33
34 const wchar_t* GALLERY_APP_ID = L"ijudt7w61q.Gallery";
35
36 const wchar_t* PATH_DATA_DIRECTORY = L"data/";
37 const wchar_t* PATH_GALLERY_DEFAULT_SETTINGS = L"GalleryDefaultSettings.ini";
38
39 const wchar_t* SECTION_GALLERY = L"GALLRY_SETTING";
40 const wchar_t* SECTION_IMAGE_VIEWER = L"IMAGE_VIEWER_SETTING";
41 const wchar_t* SECTION_VIDEO_PLAYER = L"VIDEO_PLAYER_SETTING";
42
43 const wchar_t* SLIDESHOW_INTERVAL_VALUE = L"SLIDESHOW_INTERVAL_VALUE";
44 const wchar_t* TRANSITION_EFFECT_VALUE = L"TRANSITION_EFFECT_VALUE";
45 const wchar_t* REPEAT_VALUE = L"REPEAT_VALUE";
46 const wchar_t* SHUFFLE_VALUE = L"SHUFFLE_VALUE";
47
48 SettingPresentationModel* SettingPresentationModel::__pPresentationModelInstance = null;
49
50 SettingPresentationModel::SettingPresentationModel(void)
51         : __pDefaultGalleryRegistry(null)
52 {
53         AppLogDebug("ENTER");
54         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
55 }
56
57 SettingPresentationModel::~SettingPresentationModel(void)
58 {
59         AppLogDebug("ENTER");
60         delete __pDefaultGalleryRegistry;
61         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
62 }
63
64 SettingPresentationModel*
65 SettingPresentationModel::GetInstance(void)
66 {
67         AppLogDebug("ENTER");
68         if (__pPresentationModelInstance == null)
69         {
70                 CreateInstance();
71         }
72         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
73
74         return __pPresentationModelInstance;
75 }
76
77 result
78 SettingPresentationModel::Construct(void)
79 {
80         AppLogDebug("ENTER");
81         result r = CreateDefaultRegistry();
82         TryCatch(r == E_SUCCESS, , "CreateDefaultRegistry() failed[%s]", GetErrorMessage(r));
83
84         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
85
86         return r;
87
88 CATCH:
89         AppLogDebug("EXIT with exception(%s)", GetErrorMessage(GetLastResult()));
90
91         return r;
92 }
93
94 void
95 SettingPresentationModel::CreateInstance(void)
96 {
97         AppLogDebug("ENTER");
98         __pPresentationModelInstance = new (std::nothrow) SettingPresentationModel;
99         result r = __pPresentationModelInstance->Construct();
100
101         if (IsFailed(r) == true)
102         {
103                 delete __pPresentationModelInstance;
104                 __pPresentationModelInstance = null;
105                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
106
107                 return;
108         }
109
110         std::atexit(DestroyInstance);
111         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
112 }
113
114 void
115 SettingPresentationModel::DestroyInstance(void)
116 {
117         AppLogDebug("ENTER");
118         delete __pPresentationModelInstance;
119         __pPresentationModelInstance = null;
120
121         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
122 }
123
124 result
125 SettingPresentationModel::GetValue(const String& strSectionName, const String& entryName, int& value) const
126 {
127         AppLogDebug("ENTER");
128         result r = __pDefaultGalleryRegistry->GetValue(strSectionName, entryName, value);
129         TryCatch(r == E_SUCCESS, , "Registry::GetValue() failed[%s]", GetErrorMessage(r));
130         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
131
132         return r;
133
134 CATCH:
135         AppLogDebug("EXIT with exception(%s)", GetErrorMessage(GetLastResult()));
136
137         return r;
138 }
139
140 result
141 SettingPresentationModel::GetValue(const String& strSectionName, const String& entryName, String& value) const
142 {
143         AppLogDebug("ENTER");
144         result r = __pDefaultGalleryRegistry->GetValue(strSectionName, entryName, value);
145         TryCatch(r == E_SUCCESS, , "Registry::GetValue() failed[%s]", GetErrorMessage(r));
146         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
147
148         return r;
149
150 CATCH:
151         AppLogDebug("EXIT with exception(%s)", GetErrorMessage(GetLastResult()));
152
153         return r;
154 }
155
156 result
157 SettingPresentationModel::SetValue(const String& strSectionName, const String& entryName, const int value)
158 {
159         AppLogDebug("ENTER");
160         result r= __pDefaultGalleryRegistry->SetValue(strSectionName, entryName, value);
161         TryCatch(r == E_SUCCESS, , "Registry::SetValue() failed[%s]", GetErrorMessage(r));
162         r = __pDefaultGalleryRegistry->Flush();
163         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
164
165         return r;
166
167 CATCH:
168         AppLogDebug("EXIT with exception(%s)", GetErrorMessage(GetLastResult()));
169
170         return r;
171 }
172
173 result
174 SettingPresentationModel::SetValue(const String& strSectionName, const String& entryName, const String& value)
175 {
176         AppLogDebug("ENTER");
177         result r = __pDefaultGalleryRegistry->SetValue(strSectionName, entryName, value);
178         TryCatch(r == E_SUCCESS, , "Registry::SetValue() failed[%s]", GetErrorMessage(r));
179         r = __pDefaultGalleryRegistry->Flush();
180         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
181
182         return r;
183
184 CATCH:
185         AppLogDebug("EXIT with exception(%s)", GetErrorMessage(GetLastResult()));
186
187         return r;
188 }
189
190 result
191 SettingPresentationModel::CreateDefaultRegistry(void)
192 {
193         AppLogDebug("ENTER");
194         result r = E_SUCCESS;
195         AppManager* pAppManager = AppManager::GetInstance();
196         TryReturn(pAppManager != null, E_FAILURE, "Failed to get AppManager");
197         String pathGalleryDefaultSetting =
198                         pAppManager->GetAppSharedPath(GALLERY_APP_ID) + PATH_DATA_DIRECTORY + PATH_GALLERY_DEFAULT_SETTINGS;
199         AppLogDebug("pathGalleryDefaultSetting(%ls)", pathGalleryDefaultSetting.GetPointer());
200
201         __pDefaultGalleryRegistry = new (std::nothrow) Registry();
202         if (File::IsFileExist(pathGalleryDefaultSetting))
203         {
204                 AppLogDebug("Already exist default settings file");
205
206                 __pDefaultGalleryRegistry->Construct(pathGalleryDefaultSetting, "r+");
207         }
208         else
209         {
210                 AppLogDebug("Creating default settings file");
211
212                 __pDefaultGalleryRegistry->Construct(pathGalleryDefaultSetting, "w+");
213                 r = __pDefaultGalleryRegistry->AddSection(SECTION_IMAGE_VIEWER);
214                 TryCatch(r == E_SUCCESS, , "Registry::AddSection() failed[%s]", GetErrorMessage(r));
215
216                 r = __pDefaultGalleryRegistry->AddValue(SECTION_IMAGE_VIEWER, SLIDESHOW_INTERVAL_VALUE,
217                                 DEFAULT_VALUE_INTERVAL);
218                 TryCatch(r == E_SUCCESS, , "Registry::AddValue() failed[%s]", GetErrorMessage(r));
219
220                 r = __pDefaultGalleryRegistry->AddValue(SECTION_IMAGE_VIEWER, TRANSITION_EFFECT_VALUE,
221                                 DEFAULT_VALUE_EFFECT_SLIDE);
222                 TryCatch(r == E_SUCCESS, , "Registry::AddValue() failed[%s]", GetErrorMessage(r));
223
224                 r = __pDefaultGalleryRegistry->AddValue(SECTION_IMAGE_VIEWER, REPEAT_VALUE,
225                                 DEFAULT_VALUE_REPEAT);
226                 TryCatch(r == E_SUCCESS, , "Registry::AddValue() failed[%s]", GetErrorMessage(r));
227
228                 r = __pDefaultGalleryRegistry->AddValue(SECTION_IMAGE_VIEWER, SHUFFLE_VALUE,
229                                 DEFAULT_VALUE_SHUFFLE);
230                 TryCatch(r == E_SUCCESS, , "Registry::AddValue() failed[%s]", GetErrorMessage(r));
231
232                 r = __pDefaultGalleryRegistry->Flush();
233                 TryCatch(r == E_SUCCESS, , "Registry::Flush() failed[%s]", GetErrorMessage(r));
234         }
235         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
236
237         return r;
238
239 CATCH:
240         delete __pDefaultGalleryRegistry;
241         __pDefaultGalleryRegistry = null;
242         AppLogDebug("EXIT with exception(%s)", GetErrorMessage(GetLastResult()));
243
244         return r;
245 }