Tizen 2.1 base
[sdk/ide/native-sample.git] / samples / native / cpp / Sample / Tizen C++ / MediaApp / MediaApp / project / src / AppConfig.cpp
1 //
2 // Tizen C++ SDK
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.tizenopensource.org/license
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <stdio.h>
19 #include <stdarg.h>
20
21 #include <FIo.h>
22 #include <FAppApp.h>
23 #include <FSystem.h>
24 #include "AppConfig.h"
25 #include "SafeMacros.h"
26
27 #define SECT_MEDIA L"sectMedia"
28 #define ENTRY_HTTP_AUDIO0 L"entryHttpAudio0"
29 #define ENTRY_HTTP_VIDEO0 L"entryHttpVideo0"
30 #define ENTRY_RTSP_AUDIO0 L"entryRtspAudio0"
31 #define ENTRY_RTSP_VIDEO0 L"entryRtspVideo0"
32
33 using namespace Osp::Base;
34 using namespace Osp::Io;
35 using namespace Osp::App;
36
37 AppConfig* AppConfig::__pInst = null;
38 AutoDeleteT< AppConfig > AppConfig::__autoDelete(&AppConfig::__pInst);
39
40
41 AppConfig::AppConfig()
42 {
43 }
44
45 AppConfig::~AppConfig()
46 {
47 }
48
49 result
50 AppConfig::Construct(const Osp::Base::String& configFile)
51 {
52         result r = E_SUCCESS;
53         r = __reg.Construct(configFile, false);
54         TryReturn(r == E_SUCCESS, r , "Constructor failed: %s", GetErrorMessage(r));
55         __contentRoot = App::GetInstance()->GetAppRootPath() + GetString(L"global", L"ContentRoot");
56         return r;
57 }
58
59 AppConfig*
60 AppConfig::GetInstance(void)
61 {
62         result r = E_SUCCESS;
63         if (__pInst == null)
64         {
65                 __pInst = new (std::nothrow) AppConfig();
66                 r = __pInst->Construct(App::GetInstance()->GetAppRootPath() + L"data/config.ini");
67                 TryCatch(r == E_SUCCESS, , "pInst->Construct failed:%s", GetErrorMessage(r));
68         }
69
70         AppLog("Get Instance done");
71         return __pInst;
72
73 CATCH:
74         AppLog("Get Instance failed");
75         delete __pInst;
76         __pInst = null;
77         return null;
78 }
79
80 bool
81 AppConfig::ShowAllTc(void)
82 {
83         result r = E_SUCCESS;
84         int showAll = 0;
85
86         r = __reg.GetValue(L"global", L"ShowAll", showAll);
87         TryReturn(r == E_SUCCESS, false, "GetValue failed: %s", GetErrorMessage(r));
88         if (showAll)
89         {
90                 return true;
91         }
92         else
93         {
94                 return false;
95         }
96 }
97
98 bool
99 AppConfig::GetTcEnabled(const Osp::Base::String& name) const
100 {
101         result r = E_SUCCESS;
102         int ret;
103         r = __reg.GetValue(L"tclist", name, ret);
104         if (IsFailed(r))
105         {
106                 return false;
107         }
108         if (ret)
109         {
110                 return true;
111         }
112         else
113         {
114                 return false;
115         }
116 }
117
118
119 result
120 AppConfig::GetValue(const Osp::Base::String& section, const Osp::Base::String& key, int& val) const
121 {
122         return __reg.GetValue(section, key, val);
123 }
124
125 result
126 AppConfig::SetValue(const Osp::Base::String& section, const Osp::Base::String& key, int val)
127 {
128         result r = E_SUCCESS;
129
130         r = __reg.SetValue(section, key, val);
131         if (r == E_KEY_NOT_FOUND)
132         {
133                 r = __reg.AddValue(section, key, val);
134         }
135         __reg.Flush();
136         return r;
137 }
138
139 result
140 AppConfig::SetValue(const Osp::Base::String& section, const Osp::Base::String& key, const Osp::Base::String val)
141 {
142         result r = E_SUCCESS;
143
144         r = __reg.SetValue(section, key, val);
145         if (r == E_KEY_NOT_FOUND)
146         {
147                 r = __reg.AddValue(section, key, val);
148         }
149         __reg.Flush();
150         return r;
151 }
152
153 const Osp::Base::Collection::IList*
154 AppConfig::GetTsListN(void) const
155 {
156         String tsList = GetString("tclist", "tslist");
157         Osp::Base::Utility::StringTokenizer strTok(tsList, "|");
158         Osp::Base::Collection::ArrayList* pList = new (std::nothrow) Osp::Base::Collection::ArrayList();
159         String token;
160
161         TryCatch(pList, , "ArrayList alloc failed:%s", GetErrorMessage(GetLastResult()));
162         TryCatch(strTok.GetTokenCount() != 0, , "token count is 0");
163
164         while (strTok.HasMoreTokens())
165         {
166                 strTok.GetNextToken(token);         // Osp, StringTokenizer, Sample, code
167                 if (!token.IsEmpty())
168                 {
169                         pList->Add(*(new String(token)));
170                 }
171         }
172         return pList;
173
174 CATCH:
175         if (pList)
176         {
177                 delete pList;
178         }
179         return null;
180 }
181
182 Osp::Base::String
183 AppConfig::GetString(const Osp::Base::String& sectString, const Osp::Base::String& entryString) const
184 {
185         result r = E_SUCCESS;
186         String retValue;
187         ClearLastResult();
188         AppLog("before sectString is %ls, entryString is %ls",sectString.GetPointer(),entryString.GetPointer());
189
190         r = __reg.GetValue(sectString, entryString, retValue);
191         if (IsFailed(r))
192         {
193                 AppLog("reg.GetValue failed:%s", GetErrorMessage(r));
194                 goto CATCH;
195         }
196
197         SetLastResult(r);
198         return retValue;
199
200 CATCH:
201         SetLastResult(r);
202         return L"";
203 }
204
205 Osp::Base::String
206 AppConfig::GetMediaStremingUri(const AppConfig::MediaStreamingType streamingType) const
207 {
208         result r = E_SUCCESS;
209         String retValue;
210         ClearLastResult();
211
212         switch (streamingType)
213         {
214         case HTTP_AUDIO0:
215                 retValue = GetString(SECT_MEDIA, ENTRY_HTTP_AUDIO0);
216                 break;
217
218         case HTTP_VIDEO0:
219                 retValue = GetString(SECT_MEDIA, ENTRY_HTTP_VIDEO0);
220                 break;
221
222         case RTSP_AUDIO0:
223                 retValue = GetString(SECT_MEDIA, ENTRY_RTSP_AUDIO0);
224                 break;
225
226         case RTSP_VIDEO0:
227                 retValue = GetString(SECT_MEDIA, ENTRY_RTSP_VIDEO0);
228                 break;
229
230         default:
231                 break;
232         }
233
234         AppLog("Return Value : %ls", retValue.GetPointer());
235
236         SetLastResult(r);
237         return retValue;
238 }
239
240 Osp::Base::String
241 AppConfig::GetContentPath(const Osp::Base::String& sectString, const Osp::Base::String& entryString) const
242 {
243         String str;
244         String path;
245         String tmp;
246
247         path = GetString(sectString, entryString);
248
249         if (path.StartsWith("http:", 0) || path.StartsWith("rtsp:", 0))
250         {
251                 return path;
252         }
253         else if (path.StartsWith("buffer:", 0))
254         {
255                 path.SubString(sizeof("buffer:") - 1, tmp);
256                 str = "buffer:";
257                 str += __contentRoot + tmp;
258         }
259         else
260                 str = __contentRoot + path;
261
262         return str;
263 }
264
265 Osp::Base::String
266 AppConfig::GetContentPath(const Osp::Base::String& sectString, const char* format, ...) const
267 {
268         va_list ap;
269         char text[1024];
270         String keyStr;
271
272         va_start(ap, format);
273         vsnprintf(text, sizeof(text), format, ap);
274         va_end(ap);
275         keyStr = text;
276         return GetContentPath(sectString, keyStr);
277 }
278
279 Osp::Base::String
280 AppConfig::GetString(const Osp::Base::String& sectString, const char* pFormat, ...) const
281 {
282         va_list ap;
283         char text[1024];
284         String keyStr;
285         AppLog("sectString is %ls",sectString.GetPointer());
286
287         va_start(ap, pFormat);
288         vsnprintf(text, sizeof(text), pFormat, ap);
289         va_end(ap);
290         keyStr = text;
291         return GetString(sectString, keyStr);
292 }
293
294
295 int
296 AppConfig::GetInt(int defaultVal, const Osp::Base::String& sec, const Osp::Base::String& key) const
297 {
298         int val = 0;
299         result r = E_SUCCESS;
300         r = GetValue(sec, key, val);
301         if (IsFailed(r))
302         {
303                 return defaultVal;
304         }
305         return val;
306 }
307
308 int
309 AppConfig::GetInt(int defaultVal, const Osp::Base::String& sec, const char* format, ...) const
310 {
311         va_list ap;
312         char text[32];
313         String keyStr;
314
315         va_start(ap, format);
316         vsnprintf(text, sizeof(text), format, ap);
317         va_end(ap);
318         keyStr = text;
319         return GetInt(defaultVal, sec, keyStr);
320 }
321
322 result
323 AppConfig::GetValue(const Osp::Base::String& section, const Osp::Base::String& key, Osp::Base::ByteBuffer& retVal) const
324 {
325         result r = __reg.GetValue(section, key, retVal);
326
327         if (r != E_SUCCESS)
328         {
329                 int size = retVal.GetCapacity();
330                 memset((byte*) retVal.GetPointer(), 0, size);
331         }
332
333         return r;
334 }