Initialize Tizen 2.3
[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         return E_SUCCESS;
82 }
83
84 void
85 SettingPresentationModel::CreateInstance(void)
86 {
87         AppLogDebug("ENTER");
88         __pPresentationModelInstance = new (std::nothrow) SettingPresentationModel;
89         result r = __pPresentationModelInstance->Construct();
90
91         if (IsFailed(r) == true)
92         {
93                 delete __pPresentationModelInstance;
94                 __pPresentationModelInstance = null;
95                 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
96
97                 return;
98         }
99
100         std::atexit(DestroyInstance);
101         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
102 }
103
104 void
105 SettingPresentationModel::DestroyInstance(void)
106 {
107         AppLogDebug("ENTER");
108         delete __pPresentationModelInstance;
109         __pPresentationModelInstance = null;
110
111         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
112 }
113
114 result
115 SettingPresentationModel::GetValue(const String& strSectionName, const String& entryName, int& value) const
116 {
117         AppLogDebug("ENTER");
118         result r = __pDefaultGalleryRegistry->GetValue(strSectionName, entryName, value);
119         TryCatch(r == E_SUCCESS,, "Registry::GetValue() failed[%s]", GetErrorMessage(r));
120         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
121
122         return r;
123
124 CATCH:
125         AppLogDebug("EXIT with exception(%s)", GetErrorMessage(GetLastResult()));
126
127         return r;
128 }
129
130 result
131 SettingPresentationModel::GetValue(const String& strSectionName, const String& entryName, String& value) const
132 {
133         AppLogDebug("ENTER");
134         result r = __pDefaultGalleryRegistry->GetValue(strSectionName, entryName, value);
135         TryCatch(r == E_SUCCESS,, "Registry::GetValue() failed[%s]", GetErrorMessage(r));
136         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
137
138         return r;
139
140 CATCH:
141         AppLogDebug("EXIT with exception(%s)", GetErrorMessage(GetLastResult()));
142
143         return r;
144 }
145
146 result
147 SettingPresentationModel::SetValue(const String& strSectionName, const String& entryName, const int value)
148 {
149         AppLogDebug("ENTER");
150         result r= __pDefaultGalleryRegistry->SetValue(strSectionName, entryName, value);
151         TryCatch(r == E_SUCCESS,, "Registry::SetValue() failed[%s]", GetErrorMessage(r));
152         r = __pDefaultGalleryRegistry->Flush();
153         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
154
155         return r;
156
157 CATCH:
158         AppLogDebug("EXIT with exception(%s)", GetErrorMessage(GetLastResult()));
159
160         return r;
161 }
162
163 result
164 SettingPresentationModel::SetValue(const String& strSectionName, const String& entryName, const String& value)
165 {
166         AppLogDebug("ENTER");
167         result r = __pDefaultGalleryRegistry->SetValue(strSectionName, entryName, value);
168         TryCatch(r == E_SUCCESS,, "Registry::SetValue() failed[%s]", GetErrorMessage(r));
169         r = __pDefaultGalleryRegistry->Flush();
170         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
171
172         return r;
173
174 CATCH:
175         AppLogDebug("EXIT with exception(%s)", GetErrorMessage(GetLastResult()));
176
177         return r;
178 }
179
180 result
181 SettingPresentationModel::CreateDefaultRegistry(void)
182 {
183         AppLogDebug("ENTER");
184         result r = E_SUCCESS;
185         AppManager* pAppManager = AppManager::GetInstance();
186         TryReturn(pAppManager != null, E_FAILURE, "Failed to get AppManager");
187         String pathGalleryDefaultSetting =
188                         pAppManager->GetAppSharedPath(GALLERY_APP_ID) + PATH_DATA_DIRECTORY + PATH_GALLERY_DEFAULT_SETTINGS;
189         AppLogDebug("pathGalleryDefaultSetting(%ls)", pathGalleryDefaultSetting.GetPointer());
190
191         __pDefaultGalleryRegistry = new (std::nothrow) Registry();
192         if (File::IsFileExist(pathGalleryDefaultSetting))
193         {
194                 AppLogDebug("Already exist default settings file");
195
196                 __pDefaultGalleryRegistry->Construct(pathGalleryDefaultSetting, "r+");
197         }
198         else
199         {
200                 AppLogDebug("Creating default settings file");
201
202                 __pDefaultGalleryRegistry->Construct(pathGalleryDefaultSetting, "w+");
203                 r = __pDefaultGalleryRegistry->AddSection(SECTION_IMAGE_VIEWER);
204                 TryCatch(r == E_SUCCESS,, "Registry::AddSection() failed[%s]", GetErrorMessage(r));
205
206                 r = __pDefaultGalleryRegistry->AddValue(SECTION_IMAGE_VIEWER, SLIDESHOW_INTERVAL_VALUE,
207                                 DEFAULT_VALUE_INTERVAL);
208                 TryCatch(r == E_SUCCESS,, "Registry::AddValue() failed[%s]", GetErrorMessage(r));
209
210                 r = __pDefaultGalleryRegistry->AddValue(SECTION_IMAGE_VIEWER, TRANSITION_EFFECT_VALUE,
211                                 DEFAULT_VALUE_EFFECT_SLIDE);
212                 TryCatch(r == E_SUCCESS,, "Registry::AddValue() failed[%s]", GetErrorMessage(r));
213
214                 r = __pDefaultGalleryRegistry->AddValue(SECTION_IMAGE_VIEWER, REPEAT_VALUE,
215                                 DEFAULT_VALUE_REPEAT);
216                 TryCatch(r == E_SUCCESS,, "Registry::AddValue() failed[%s]", GetErrorMessage(r));
217
218                 r = __pDefaultGalleryRegistry->AddValue(SECTION_IMAGE_VIEWER, SHUFFLE_VALUE,
219                                 DEFAULT_VALUE_SHUFFLE);
220                 TryCatch(r == E_SUCCESS,, "Registry::AddValue() failed[%s]", GetErrorMessage(r));
221
222                 r = __pDefaultGalleryRegistry->Flush();
223                 TryCatch(r == E_SUCCESS,, "Registry::Flush() failed[%s]", GetErrorMessage(r));
224         }
225         AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
226
227         return r;
228
229 CATCH:
230         delete __pDefaultGalleryRegistry;
231         __pDefaultGalleryRegistry = null;
232         AppLogDebug("EXIT with exception(%s)", GetErrorMessage(GetLastResult()));
233
234         return r;
235 }
236
237 void
238 SettingPresentationModel::DeleteRegistry(void)
239 {
240         if (__pDefaultGalleryRegistry != null )
241         {
242                 delete __pDefaultGalleryRegistry;
243                 __pDefaultGalleryRegistry = null;
244         }
245 }