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