Applied latest source code
[apps/native/preloaded/Settings.git] / src / StAppSettingManager.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                StAppSettingManager.cpp
19  * @brief               This is the implementation file for AppSetting Manager class.
20  */
21
22 #include <cstdlib>
23 #include <libxml/xmlreader.h>
24 #include "StAppSettingManager.h"
25
26 using namespace Tizen::App;
27 using namespace Tizen::Base;
28 using namespace Tizen::Base::Collection;
29 using namespace Tizen::Base::Utility;
30
31 static const int ID_APPSETTING_MANAGER_CHANGED_VALUE_KEY_DELIMIT_MAX_COUNT = 2;
32 static const int ID_APPSETTING_MANAGER_ELEMENT_NODE_DELIMIT_COUNT = 5;
33 static const int ID_APPSETTING_MANAGER_NODE_ATTRIUBTE_DELIMIT_COUNT = 8;
34
35 static const wchar_t* ID_APPSETTING_CHANGED_VALUE_KEY_DELIMIT = L":";
36 static const wchar_t* ID_APPSETTING_CHANGED_VALUE_BOOL_TYPE = L"bool";
37 static const wchar_t* ID_APPSETTING_CHANGED_VALUE_INTEGER_TYPE = L"integer";
38
39 static const int APP_SETTING_DEFAULT_TOKEN_COUNT = 1;
40 static const int XML_READER_FOR_FILE_DEFAULT_OPTION = 524288;
41
42 int separatorCount = 1;
43 static const wchar_t* appSettingElementNodeDelimit[] =
44 {
45                 L"settings",
46                 L"setting",
47                 L"navigationbar",
48                 L"group",
49                 L"expandlist"
50 };
51
52 static const wchar_t* appSettingNodeAttributeDelimit[] =
53 {
54                 L"main",
55                 L"title",
56                 L"type",
57                 L"min",
58                 L"max",
59                 L"minlength",
60                 L"maxlength",
61                 L"value"
62 };
63
64 AppSettingManager* AppSettingManager::__pAppSettingManagerInstance = null;
65
66 Node::Node(Tizen::Base::String id, Tizen::Base::String type, Node* pParent, Tizen::Base::String parentId)
67         : __pParent(pParent)
68         , __pRegisteredObserverList(null)
69         , __pNodeAttribute(null)
70         , __parentId(parentId)
71         , __id(id)
72         , __type(type)
73 {
74 }
75
76 Node::~Node(void)
77 {
78         RemoveAllAttribute();
79         RemoveAllObserverListener();
80
81         __pParent = null;
82 }
83
84 Tizen::Base::String
85 Node::GetId(void)
86 {
87         return __id;
88 }
89
90 Tizen::Base::String
91 Node::GetType(void)
92 {
93         return __type;
94 }
95
96 Tizen::Base::String
97 Node::GetParentId(void)
98 {
99         return __parentId;
100 }
101
102 Node*
103 Node::GetParent(void)
104 {
105         return __pParent;
106 }
107
108 result
109 Node::AddAttribute(Tizen::Base::String key, Tizen::Base::String value)
110 {
111         result r = E_SUCCESS;
112
113         if (__pNodeAttribute == null)
114         {
115                 __pNodeAttribute = new (std::nothrow) HashMap();
116                 r = __pNodeAttribute->Construct();
117
118                 if (IsFailed(r))
119                 {
120                         delete __pNodeAttribute;
121                         __pNodeAttribute = null;
122
123                         AppLogDebug("Construct Fail [%s]", GetErrorMessage(r));
124                         return r;
125                 }
126         }
127
128         r = __pNodeAttribute->Add(new (std::nothrow) String(key), new (std::nothrow) String(value));
129
130         return r;
131 }
132
133 result
134 Node::SetAttribute(Tizen::Base::String key, Tizen::Base::String value)
135 {
136         if (__pNodeAttribute == null)
137         {
138                 return E_INVALID_OPERATION;
139         }
140
141         bool isFind = false;
142         result r = __pNodeAttribute->ContainsKey(key, isFind);
143         if ((IsFailed(r))
144                 || (isFind == false))
145         {
146                 AppLogDebug("ContainsKey fail[result : %s][isFind : %d]", GetErrorMessage(r), isFind);
147
148                 if (isFind == false)
149                 {
150                         r = E_INVALID_KEY;
151                 }
152                 return r;
153         }
154
155         r = __pNodeAttribute->SetValue(*(new (std::nothrow) String(key)), *(new (std::nothrow) String(value)));
156         if (!IsFailed(r))
157         {
158                 ChangedAttributeValue(value);
159         }
160
161         return r;
162 }
163
164 Tizen::Base::String*
165 Node::GetAttributeN(Tizen::Base::String key)
166 {
167         String* pValue = null;
168         bool isFind = false;
169
170         if (__pNodeAttribute == null)
171         {
172                 AppLogDebug("__pNodeAttribute is null");
173                 return pValue;
174         }
175
176         result r = __pNodeAttribute->ContainsKey(key, isFind);
177         if ((IsFailed(r))
178                 || (isFind == false))
179         {
180                 AppLogDebug("ContainsKey fail[result : %s][isFind : %d]", GetErrorMessage(r), isFind);
181                 return pValue;
182         }
183
184         pValue = static_cast<String*>(__pNodeAttribute->GetValue(key));
185         return (new (std::nothrow) String(*pValue));
186 }
187
188 Tizen::Base::Collection::IMapEnumerator*
189 Node::GetAttributeIEnumeratorN(void)
190 {
191         if (__pNodeAttribute == null)
192         {
193                 AppLogDebug("__pNodeAttribute is null");
194                 return null;
195         }
196
197         IMapEnumerator* pMapEnum = __pNodeAttribute->GetMapEnumeratorN();
198         return pMapEnum;
199 }
200
201 result
202 Node::RemoveAttriube(Tizen::Base::String key)
203 {
204         if (__pNodeAttribute == null)
205         {
206                 return E_INVALID_OPERATION;
207         }
208
209         bool isFind = false;
210         result r = __pNodeAttribute->ContainsKey(key, isFind);
211         if ((IsFailed(r))
212                 || (isFind == false))
213         {
214                 AppLogDebug("ContainsKey fail[result : %s][isFind : %d]", GetErrorMessage(r), isFind);
215
216                 if (isFind == false)
217                 {
218                         r = E_INVALID_KEY;
219                 }
220                 return r;
221         }
222
223         r = __pNodeAttribute->Remove(key, true);
224         return r;
225 }
226
227 void
228 Node::RemoveAllAttribute(void)
229 {
230         if (__pNodeAttribute != null)
231         {
232                 __pNodeAttribute->RemoveAll();
233                 delete __pNodeAttribute;
234                 __pNodeAttribute = null;
235         }
236 }
237
238 void
239 Node::RemoveAllObserverListener(void)
240 {
241         if (__pRegisteredObserverList != null)
242         {
243                 __pRegisteredObserverList->RemoveAll();
244                 delete __pRegisteredObserverList;
245                 __pRegisteredObserverList = null;
246         }
247 }
248
249 int
250 Node::GetAttriubteCount(void)
251 {
252         int attributeCount = 0;
253
254         if (__pNodeAttribute == null)
255         {
256                 AppLogDebug("__pNodeAttribute is null");
257                 return attributeCount;
258         }
259
260         attributeCount = __pNodeAttribute->GetCount();
261         return attributeCount;
262 }
263
264 void
265 Node::RegisterObserverlistener(ObserverAttribute* pObserver)
266 {
267         if (__pRegisteredObserverList == null)
268         {
269                 __pRegisteredObserverList = new (std::nothrow) LinkedListT<ObserverAttribute*>;
270                 if (__pRegisteredObserverList == null)
271                 {
272                         AppLogDebug("__pRegisteredObserverList is null");
273                         return;
274                 }
275         }
276
277         if (pObserver == null)
278         {
279                 AppLogDebug("pObserver is null");
280                 return;
281         }
282
283         if (!__pRegisteredObserverList->Contains(pObserver))
284         {
285                 __pRegisteredObserverList->Add(pObserver);
286                 AppLogDebug("ObserverAttribute Add");
287         }
288         else
289         {
290                 AppLogDebug("ObserverAttribute fail");
291         }
292 }
293
294 void
295 Node::UnRegisterObserverlistener(ObserverAttribute* pObserver)
296 {
297         if (__pRegisteredObserverList == null)
298         {
299                 AppLogDebug("__pNodeAttribute is null");
300                 return;
301         }
302
303         ObserverAttribute* pObser = null;
304         IEnumeratorT<ObserverAttribute*>* pEnum = __pRegisteredObserverList->GetEnumeratorN();
305         while (pEnum->MoveNext() == E_SUCCESS)
306         {
307                 result r = pEnum->GetCurrent(pObser);
308                 if (IsFailed(r))
309                 {
310                         AppLogDebug("GetCurrent Fail [%s]", GetErrorMessage(r));
311                         break;
312                 }
313
314                 if (pObser == pObserver)
315                 {
316                         __pRegisteredObserverList->Remove(pObserver);
317                         break;
318                 }
319         }
320         delete pEnum;
321 }
322
323 void
324 Node::ChangedAttributeValue(Tizen::Base::String value)
325 {
326         if ((__pNodeAttribute == null)
327                 || (__pRegisteredObserverList == null))
328         {
329                 AppLogDebug("__pNodeAttribute or __pRegisteredObserverList is null");
330                 return;
331         }
332
333         ObserverAttribute* pObser = null;
334         IEnumeratorT<ObserverAttribute*>* pEnum = __pRegisteredObserverList->GetEnumeratorN();
335         while (pEnum->MoveNext() == E_SUCCESS)
336         {
337                 result r = pEnum->GetCurrent(pObser);
338                 if (IsFailed(r))
339                 {
340                         AppLogDebug("GetCurrent Fail [%s]", GetErrorMessage(r));
341                         break;
342                 }
343
344                 pObser->OnChangedAttributeValue(this->__id, value, this->__type);
345         }
346         delete pEnum;
347 }
348
349 void
350 ElementLeaf::DebugLog(void)
351 {
352         if (__pNodeAttribute == null)
353         {
354                 AppLogDebug("__pNodeAttribute is null");
355                 return;
356         }
357
358         String* pKey = null;
359         String* pValue = null;
360         IMapEnumerator* pMapEnum = __pNodeAttribute->GetMapEnumeratorN();
361
362         while (pMapEnum->MoveNext() == E_SUCCESS)
363         {
364                 pKey = static_cast<String*>(pMapEnum->GetKey());
365                 pValue = static_cast<String*>(pMapEnum->GetValue());
366                 AppLogDebug("pKey [%ls], pValue [%ls]", pKey->GetPointer(), pValue->GetPointer());
367         }
368         delete pMapEnum;
369
370         AppLogDebug("__pCurrent Id [%ls], _pParentId [%ls]", (this->__id).GetPointer(), this->__parentId.GetPointer());
371 }
372
373 Tizen::Base::Collection::IEnumerator*
374 ElementLeaf::GetCurrentIEnumerator(void)
375 {
376         return null;
377 }
378
379 Node::NodeType
380 ElementLeaf::GetNodeType(void)
381 {
382         return NODE_TYPE_LEAF;
383 }
384
385 Enumerator*
386 ElementLeaf::CreateEnumerator(void)
387 {
388         return dynamic_cast<Enumerator*>((new (std::nothrow) NullEnumerator()));
389 }
390
391 ElementNode::ElementNode(Tizen::Base::String id, Tizen::Base::String type, Node* pParent, Tizen::Base::String parentId)
392         : Node(id, type, pParent, parentId), __pCurrentEnum(null), __pChild(null)
393 {
394 }
395
396 ElementNode::~ElementNode(void)
397 {
398         if (__pCurrentEnum != null)
399         {
400                 delete __pCurrentEnum;
401                 __pCurrentEnum = null;
402         }
403         RemoveAllChild();
404 }
405
406 result
407 ElementNode::Add(Node& node)
408 {
409         if (__pChild == null)
410         {
411                 __pChild = new (std::nothrow) LinkedList();
412                 if (__pChild == null)
413                 {
414                         AppLogDebug("__pChild is null");
415                         return E_FAILURE;
416                 }
417         }
418
419         result r = __pChild->Add(node);
420         if (IsFailed(r))
421         {
422                 AppLogDebug(" Add fail [%s]", GetErrorMessage(r));
423                 return r;
424         }
425
426         return E_SUCCESS;
427 }
428
429 Node*
430 ElementNode::AddSilbing(Tizen::Base::String id, Tizen::Base::String type, NodeType nodetype)
431 {
432         Node* pSibling = null;
433
434         if ((this->GetParent())->GetNodeType() != NODE_TYPE_ELEMENT)
435         {
436                 AppLogDebug("GetNodeType() is [%d]", (this->GetParent())->GetNodeType());
437                 return null;
438         }
439
440         if (nodetype == NODE_TYPE_ELEMENT)
441         {
442                 pSibling = dynamic_cast<Node*>(new (std::nothrow) ElementNode(id, type, this->GetParent(), this->GetParentId()));
443         }
444         else
445         {
446                 pSibling = dynamic_cast<Node*>(new (std::nothrow) ElementLeaf(id, type, this->GetParent(), this->GetParentId()));
447         }
448
449         if (pSibling == null)
450         {
451                 AppLogDebug(" Add fail [E_FAILURE]]");
452                 return null;
453         }
454
455         ElementNode* pElementNode = dynamic_cast<ElementNode*>(this->GetParent());
456         if (pElementNode == null)
457         {
458                 AppLogDebug(" dynamic_cast<ElementNode*>(this->GetParent()) is null");
459                 delete pSibling;
460                 return null;
461         }
462
463         result r = pElementNode->Add(*pSibling);
464         if (IsFailed(r))
465         {
466                 AppLogDebug(" Add fail [%s]", GetErrorMessage(r));
467                 delete pSibling;
468                 return null;
469         }
470
471         return pSibling;
472 }
473
474 Node*
475 ElementNode::AddChild(Tizen::Base::String id, Tizen::Base::String type, NodeType nodetype)
476 {
477         Node* pChild = null;
478
479         if (GetNodeType() != NODE_TYPE_ELEMENT)
480         {
481                 AppLogDebug("GetNodeType() is [%d]", GetNodeType());
482                 return null;
483         }
484
485         if (nodetype == NODE_TYPE_ELEMENT)
486         {
487                 pChild = dynamic_cast<Node*>(new (std::nothrow) ElementNode(id, type, this, this->__id));
488         }
489         else
490         {
491                 pChild = dynamic_cast<Node*>(new (std::nothrow) ElementLeaf(id, type, this, this->__id));
492         }
493
494         if (pChild == null)
495         {
496                 AppLogDebug(" Add fail [E_FAILURE]]");
497                 return null;
498         }
499
500         result r =Add(*pChild);
501         if (IsFailed(r))
502         {
503                 AppLogDebug(" Add fail [%s]", GetErrorMessage(r));
504                 delete pChild;
505                 return null;
506         }
507
508         return pChild;
509 }
510
511 result
512 ElementNode::Remove(Tizen::Base::String id, Node* pParent)
513 {
514         if (this->__id.Equals(id, false))
515         {
516                 AppLogDebug("can not delete yourself");
517                 return E_FAILURE;
518         }
519
520         IEnumerator* pEnum = null;
521         if (pParent->GetNodeType() == NODE_TYPE_ELEMENT)
522         {
523                 ElementNode* pElementNode = (dynamic_cast<ElementNode*>(pParent));
524                 if (pElementNode == null)
525                 {
526                         AppLogDebug("(dynamic_cast<ElementNode*>(pParent) is null");
527                         return E_FAILURE;
528                 }
529                 if (pElementNode->__pChild == null)
530                 {
531                         AppLogDebug(" pElementNode->__pChild is null");
532                         return E_FAILURE;
533                 }
534
535                 pEnum = pElementNode->__pChild->GetEnumeratorN();
536         }
537         else
538         {
539                 AppLogDebug(" pParent is not NODE_TYPE_ELEMENT");
540                 return E_INVALID_OPERATION;
541         }
542
543         Node* pObj = null;
544         while (pEnum->MoveNext() == E_SUCCESS)
545         {
546                 pObj = static_cast<Node*>(pEnum->GetCurrent());
547                 if ((pObj->GetId()).Equals(id, false))
548                 {
549                         ElementNode* pElementNode = dynamic_cast<ElementNode*>(pParent);
550                         if (pElementNode == null)
551                         {
552                                 delete pEnum;
553                                 return E_FAILURE;
554                         }
555
556                         delete pEnum;
557                         return pElementNode->__pChild->Remove(*pObj, true);
558                 }
559         }
560
561         delete pEnum;
562
563         AppLogDebug(" Node is not found");
564         return E_FAILURE;
565 }
566
567 void
568 ElementNode::RemoveAllChild(void)
569 {
570         if (__pChild != null)
571         {
572                 __pChild->RemoveAll();
573                 delete __pChild;
574                 __pChild = null;
575         }
576 }
577
578 Node*
579 ElementNode::GetChild(Tizen::Base::String id)
580 {
581         if (__pChild == null)
582         {
583                 AppLogDebug("__pChild is null");
584                 return null;
585         }
586
587         Node* pObj = null;
588         IEnumerator* pEnum = __pChild->GetEnumeratorN();
589         while (pEnum->MoveNext())
590         {
591                 pObj = static_cast<Node*>(pEnum->GetCurrent());
592                 if ((pObj->GetId()).Equals(id, false))
593                 {
594                         delete pEnum;
595                         return pObj;
596                 }
597         }
598
599         delete pEnum;
600         return null;
601 }
602
603 Node*
604 ElementNode::GetChild(int index)
605 {
606         if (__pChild == null)
607         {
608                 AppLogDebug("__pChild is null");
609                 return null;
610         }
611
612         Node* pNode = static_cast<Node*>(__pChild->GetAt(index));
613         return pNode;
614 }
615
616 int
617 ElementNode::GetChildCount(void)
618 {
619         if (__pChild == null)
620         {
621                 AppLogDebug("__pChild is null");
622                 return 0;
623         }
624
625         return __pChild->GetCount();
626 }
627
628 result
629 ElementNode::RemoveChildAt(Tizen::Base::String id)
630 {
631         return Remove(id, dynamic_cast<Node*>(this));
632 }
633
634 result
635 ElementNode::RemoveSilbingAt(Tizen::Base::String id)
636 {
637         return Remove(id, dynamic_cast<Node*>(this->GetParent()));
638 }
639
640 Enumerator*
641 ElementNode::CreateEnumerator(void)
642 {
643         return dynamic_cast<Enumerator*>((new (std::nothrow) ElementEnumerator(this)));
644 }
645
646 Tizen::Base::Collection::IEnumerator*
647 ElementNode::GetCurrentIEnumerator(void)
648 {
649         if (__pChild == null)
650         {
651                 AppLogDebug("__pChild is null");
652                 return null;
653         }
654
655         if (__pCurrentEnum ==  null)
656         {
657                 __pCurrentEnum = __pChild->GetEnumeratorN();
658         }
659
660         return __pCurrentEnum;
661 }
662
663 void
664 ElementNode::ResetCurrentIEnumerator(void)
665 {
666         if (__pCurrentEnum != null)
667         {
668                 __pCurrentEnum->Reset();
669         }
670 }
671
672 Node::NodeType
673 ElementNode::GetNodeType(void)
674 {
675         return NODE_TYPE_ELEMENT;
676 }
677
678 void
679 ElementNode::DebugLog(void)
680 {
681         if (__pChild != null)
682         {
683                 IEnumerator* pEnum = __pChild->GetEnumeratorN();
684                 ElementNode* pObj = null;
685                 while (pEnum->MoveNext() == E_SUCCESS)
686                 {
687                         pObj = static_cast<ElementNode*>(pEnum->GetCurrent());
688                         AppLogDebug("__pCurrent Id [%ls], _pParentId [%ls] __type [%ls]", pObj->__id.GetPointer(), pObj->__parentId.GetPointer(), pObj->__type.GetPointer());
689                 }
690                 delete pEnum;
691         }
692         else
693         {
694                 AppLogDebug("__pChild is null");
695         }
696
697         if (__pNodeAttribute != null)
698         {
699                 String* pKey = null;
700                 String* pValue = null;
701                 IMapEnumerator* pMapEnum = __pNodeAttribute->GetMapEnumeratorN();
702
703                 while (pMapEnum->MoveNext() == E_SUCCESS)
704                 {
705                         pKey = static_cast<String*>(pMapEnum->GetKey());
706                         pValue = static_cast<String*>(pMapEnum->GetValue());
707                         AppLogDebug("pKey [%ls], pValue [%ls]", pKey->GetPointer(), pValue->GetPointer());
708                 }
709                 delete pMapEnum;
710                 AppLogDebug("__pCurrent Id [%ls], _pParentId [%ls]", (this->__id).GetPointer(), this->__parentId.GetPointer());
711         }
712         else
713         {
714                 AppLogDebug("__pNodeAttribute is null");
715         }
716 }
717
718 ElementEnumerator::ElementEnumerator(Node* pRootNode )
719         : __pRootNode(pRootNode)
720 {
721         Reset();
722         if (__pRootNode != null)
723         {
724                 __enumeratorStack.Push(__pRootNode);
725         }
726 }
727
728 ElementEnumerator::~ElementEnumerator(void)
729 {
730         Reset();
731         __pCurrentIEnumerator = null;
732         __pRootNode = null;
733 }
734
735 void
736 ElementEnumerator::Reset(void)
737 {
738         Node* pNode = null;
739         while (IsEmpty() == false)
740         {
741                 __enumeratorStack.Pop(pNode);
742                 if (pNode->GetNodeType() == Node::NODE_TYPE_ELEMENT)
743                 {
744                         ElementNode* pElementNode = dynamic_cast<ElementNode*>(pNode);
745                         if (pElementNode != null)
746                         {
747                                 pElementNode->ResetCurrentIEnumerator();
748                         }
749                 }
750         }
751 }
752
753 bool
754 ElementEnumerator::IsEmpty(void)
755 {
756         if (__enumeratorStack.GetCount() == 0)
757         {
758                 return true;
759         }
760         return false;
761 }
762
763 result
764 ElementEnumerator::MoveNext(void)
765 {
766         if (IsEmpty() == true)
767         {
768                 return E_FAILURE;
769         }
770
771         Node* pNode = null;
772         result r = __enumeratorStack.Peek(pNode);
773         if (IsFailed(r))
774         {
775                 AppLogDebug("Peek is fail [%s]", GetErrorMessage(r));
776                 return E_FAILURE;
777         }
778         __pCurrentIEnumerator = pNode->GetCurrentIEnumerator();
779
780         if ((__pCurrentIEnumerator != null)
781                 && (__pCurrentIEnumerator->MoveNext() == E_SUCCESS))
782         {
783                 pNode = static_cast<Node*>(__pCurrentIEnumerator->GetCurrent());
784                 r = __enumeratorStack.Push(pNode);
785                 if (IsFailed(r))
786                 {
787                         AppLogDebug("Push is fail [%s]", GetErrorMessage(r));
788                         return E_FAILURE;
789                 }
790                 return E_SUCCESS;
791         }
792         else
793         {
794                 while (IsEmpty() == false)
795                 {
796                         r = __enumeratorStack.Pop(pNode);
797                         if (IsFailed(r))
798                         {
799                                 AppLogDebug("Pop is fail [%s]", GetErrorMessage(r));
800                                 return E_FAILURE;
801                         }
802
803                         if (pNode->GetNodeType() == Node::NODE_TYPE_ELEMENT)
804                         {
805                                 ElementNode* pElementNode = dynamic_cast<ElementNode*>(pNode);
806                                 if (pElementNode != null)
807                                 {
808                                         pElementNode->ResetCurrentIEnumerator();
809                                 }
810                         }
811
812                         if (IsEmpty() == false)
813                         {
814                                 r = __enumeratorStack.Peek(pNode);
815                                 if (IsFailed(r))
816                                 {
817                                         AppLogDebug("Peek is fail [%s]", GetErrorMessage(r));
818                                         return E_FAILURE;
819                                 }
820
821                                 __pCurrentIEnumerator = pNode->GetCurrentIEnumerator();
822                                 if ((__pCurrentIEnumerator != null)
823                                         && (__pCurrentIEnumerator->MoveNext() == E_SUCCESS))
824                                 {
825                                         pNode = static_cast<Node*>(__pCurrentIEnumerator->GetCurrent());
826                                         r = __enumeratorStack.Push(pNode);
827                                         if (IsFailed(r))
828                                         {
829                                                 AppLogDebug("Peek is fail [%s]", GetErrorMessage(r));
830                                                 return E_FAILURE;
831                                         }
832
833                                         return E_SUCCESS;
834                                 }
835                         }
836                 }
837                 return E_FAILURE;
838         }
839 }
840
841 Tizen::Base::Object*
842 ElementEnumerator::GetCurrentObject(void)
843 {
844         if (__pCurrentIEnumerator != null)
845         {
846                 return __pCurrentIEnumerator->GetCurrent();
847         }
848         return null;
849 }
850
851 result
852 NullEnumerator::MoveNext(void)
853 {
854         return E_FAILURE;
855 }
856
857 Tizen::Base::Object*
858 NullEnumerator::GetCurrentObject(void)
859 {
860         return __pRootNode;
861 }
862
863 IOAppSetting::IOAppSetting(void)
864         : __appId(L"")
865         , __pAppSettingInstance(null)
866         , __pChangedAttributeHashMap(null)
867 {
868 }
869
870 IOAppSetting::~IOAppSetting(void)
871 {
872         if (__pChangedAttributeHashMap != null)
873         {
874                 __pChangedAttributeHashMap->RemoveAll();
875                 delete __pChangedAttributeHashMap;
876                 __pChangedAttributeHashMap = null;
877         }
878
879         if (__pAppSettingInstance != null)
880         {
881                 __pAppSettingInstance->ReleaseInstanceByAppId(__appId);
882                 __pAppSettingInstance = null;
883         }
884 }
885
886 result
887 IOAppSetting::Constructor(Tizen::Base::String appId)
888 {
889         result r = E_SUCCESS;
890
891         __appId.Append(appId);
892
893         __pAppSettingInstance = AppSetting::GetInstanceByAppId(__appId);
894         if (__pAppSettingInstance == null)
895         {
896                 AppLogDebug("GetInstanceByAppId is null");
897                 return E_FAILURE;
898         }
899
900         if (__pChangedAttributeHashMap == null)
901         {
902                 __pChangedAttributeHashMap = new (std::nothrow) HashMap();
903                 r = __pChangedAttributeHashMap->Construct();
904
905                 if (IsFailed(r))
906                 {
907                         delete __pChangedAttributeHashMap;
908                         __pChangedAttributeHashMap = null;
909
910                         __pAppSettingInstance->ReleaseInstanceByAppId(__appId);
911                         __pAppSettingInstance = null;
912
913                         AppLogDebug("Construct Fail [%s]", GetErrorMessage(r));
914                         return r;
915                 }
916         }
917
918         return r;
919 }
920
921 result
922 IOAppSetting::OnChangedAttributeValue(Tizen::Base::String id, Tizen::Base::String value, Tizen::Base::String type)
923 {
924         result r = E_FAILURE;
925
926         if (__pChangedAttributeHashMap == null)
927         {
928                 AppLogDebug("__pChangedAttributeHashMap is null");
929                 return r;
930         }
931
932         id.Append(ID_APPSETTING_CHANGED_VALUE_KEY_DELIMIT);
933         id.Append(type);
934
935         bool isFind = false;
936         r = __pChangedAttributeHashMap->ContainsKey(id, isFind);
937         if (IsFailed(r))
938         {
939                 AppLogDebug("ContainsKey fail[%s]", GetErrorMessage(r));
940                 return r;
941         }
942
943         if (isFind == true)
944         {
945                 r = __pChangedAttributeHashMap->SetValue(*(new (std::nothrow) String(id)), *(new (std::nothrow) String(value)));
946         }
947         else
948         {
949                 r = __pChangedAttributeHashMap->Add(new (std::nothrow) String(id), new (std::nothrow) String(value));
950         }
951
952         return r;
953 }
954
955 void
956 IOAppSetting::UpdateChangedValue(void)
957 {
958         if ((__pChangedAttributeHashMap == null)
959                 || (__pAppSettingInstance == null))
960         {
961                 AppLogDebug("__pChangedAttributeHashMap or __pAppSettingInstance is null");
962                 return;
963         }
964
965         String* pKey = null;
966         String* pValue = null;
967         String tokenValue[ID_APPSETTING_MANAGER_CHANGED_VALUE_KEY_DELIMIT_MAX_COUNT];
968         IMapEnumerator* pMapEnum = __pChangedAttributeHashMap->GetMapEnumeratorN();
969
970         while (pMapEnum->MoveNext() == E_SUCCESS)
971         {
972                 pKey = static_cast<String*>(pMapEnum->GetKey());
973                 pValue = static_cast<String*>(pMapEnum->GetValue());
974
975                 StringTokenizer stringTokenizer(*pKey, ID_APPSETTING_CHANGED_VALUE_KEY_DELIMIT);
976                 if (stringTokenizer.GetTokenCount() != static_cast<int>(ID_APPSETTING_MANAGER_CHANGED_VALUE_KEY_DELIMIT_MAX_COUNT))
977                 {
978                         AppLogDebug("failed to Get id and type [%d]", stringTokenizer.GetTokenCount());
979                         continue;
980                 }
981
982                 bool isConntinueRoutine = true;
983                 for (int i = 0; i <= stringTokenizer.GetTokenCount(); i++)
984                 {
985                         result r = stringTokenizer.GetNextToken(tokenValue[i]);
986                         if (IsFailed(r))
987                         {
988                                 AppLogDebug("GetNextToken failed[%s]", GetErrorMessage(r));
989                                 isConntinueRoutine = false;
990                                 break;
991                         }
992                 }
993
994                 if (isConntinueRoutine == false)
995                 {
996                         continue;
997                 }
998
999                 if (tokenValue[APP_SETTING_DEFAULT_TOKEN_COUNT].Equals(ID_APPSETTING_CHANGED_VALUE_BOOL_TYPE, false))
1000                 {
1001                         bool retValue = GetStringToBoolValue(*pValue);
1002                         result r = __pAppSettingInstance->SetValue(tokenValue[0], retValue);
1003                         if (IsFailed(r))
1004                         {
1005                                 AppLogDebug("SetValue fail [id:%ls][type:%ls][value:%d]", tokenValue[0].GetPointer(), tokenValue[APP_SETTING_DEFAULT_TOKEN_COUNT].GetPointer(), retValue);
1006                         }
1007                 }
1008                 else if (tokenValue[APP_SETTING_DEFAULT_TOKEN_COUNT].Equals(ID_APPSETTING_CHANGED_VALUE_INTEGER_TYPE, false))
1009                 {
1010                         int retValue = GetStringToIntegerValue(*pValue);
1011                         result r = __pAppSettingInstance->SetValue(tokenValue[0], retValue);
1012                         if (IsFailed(r))
1013                         {
1014                                 AppLogDebug("SetValue fail [id:%ls][type:%ls][value:%d]", tokenValue[0].GetPointer(), tokenValue[APP_SETTING_DEFAULT_TOKEN_COUNT].GetPointer(), retValue);
1015                         }
1016                 }
1017                 else
1018                 {
1019                         String retValue = pValue->GetPointer();
1020                         result r = __pAppSettingInstance->SetValue(tokenValue[0], retValue);
1021                         if (IsFailed(r))
1022                         {
1023                                 AppLogDebug("SetValue fail [id:%ls][type:%ls][value:%ls]", tokenValue[0].GetPointer(), tokenValue[APP_SETTING_DEFAULT_TOKEN_COUNT].GetPointer(), pValue->GetPointer());
1024                         }
1025                 }
1026         }
1027         delete pMapEnum;
1028         __pChangedAttributeHashMap->RemoveAll();
1029 }
1030
1031 bool
1032 IOAppSetting::GetStringToBoolValue(Tizen::Base::String value)
1033 {
1034         if (Boolean::Parse(value) == true)
1035         {
1036                 return true;
1037         }
1038         return false;
1039 }
1040
1041 int
1042 IOAppSetting::GetStringToIntegerValue(Tizen::Base::String value)
1043 {
1044         int intgerValue = 0;
1045         result r = Integer::Parse(value, intgerValue);
1046         if (IsFailed(r))
1047         {
1048                 AppLogDebug("Integer::Parse fail");
1049                 return 0;
1050         }
1051
1052         return intgerValue;
1053 }
1054
1055 AppSettingManager::AppSettingManager(void)
1056         : __pRootListHashMap(null)
1057 {
1058 }
1059
1060 AppSettingManager::~AppSettingManager(void)
1061 {
1062         if (__pRootListHashMap != null)
1063         {
1064                 __pRootListHashMap->RemoveAll();
1065                 delete __pRootListHashMap;
1066                 __pRootListHashMap = null;
1067         }
1068 }
1069
1070 AppSettingManager*
1071 AppSettingManager::GetInstance(void)
1072 {
1073         if (__pAppSettingManagerInstance == null)
1074         {
1075                 CreateInstance();
1076         }
1077
1078         return __pAppSettingManagerInstance;
1079 }
1080
1081 result
1082 AppSettingManager::Construct(void)
1083 {
1084         __pRootListHashMap = new (std::nothrow) HashMap();
1085         result r = __pRootListHashMap->Construct();
1086
1087         if (IsFailed(r))
1088         {
1089                 delete __pRootListHashMap;
1090                 __pRootListHashMap = null;
1091
1092                 AppLogDebug("Construct Fail [%s]", GetErrorMessage(r));
1093                 return r;
1094         }
1095
1096         return r;
1097 }
1098
1099 void
1100 AppSettingManager::CreateInstance(void)
1101 {
1102         __pAppSettingManagerInstance = new (std::nothrow) AppSettingManager();
1103         result r = __pAppSettingManagerInstance->Construct();
1104         if (IsFailed(r))
1105         {
1106                 delete __pAppSettingManagerInstance;
1107                 __pAppSettingManagerInstance = null;
1108                 return;
1109         }
1110
1111         std::atexit(DestroyInstance);
1112 }
1113
1114 void
1115 AppSettingManager::DestroyInstance(void)
1116 {
1117         delete __pAppSettingManagerInstance;
1118         __pAppSettingManagerInstance = null;
1119 }
1120
1121 result
1122 AppSettingManager::XmlAppSettingFileRead(Tizen::Base::String filePath, ElementNode* pRootNode)
1123 {
1124         ByteBuffer* pBuf = StringUtil::StringToUtf8N(filePath);
1125         if ((pBuf == null)
1126                 || (pRootNode == null))
1127         {
1128                 AppLogDebug("pBuf or pRootNode is null");
1129                 return E_FAILURE;
1130         }
1131
1132         xmlTextReaderPtr reader = xmlReaderForFile((const char*) pBuf->GetPointer(), NULL, XML_READER_FOR_FILE_DEFAULT_OPTION);
1133         if (reader == null)
1134         {
1135                 AppLogDebug("reader is null");
1136                 delete pBuf;
1137                 return E_FAILURE;
1138         }
1139         ArrayList* idList = new (std::nothrow) ArrayList(SingleObjectDeleter);
1140         idList->Construct();
1141         idList->Add(new (std::nothrow) String(L"Start"));
1142
1143         int xmlreadType = 0;
1144         Node* pParentOfNewNode = dynamic_cast<Node*>(pRootNode);
1145         while (xmlTextReaderRead(reader))
1146         {
1147                 const xmlChar* name = xmlTextReaderConstName(reader);
1148                 xmlreadType = xmlTextReaderNodeType(reader);
1149                 if ((xmlreadType != XML_READER_TYPE_ELEMENT)
1150                         && (xmlreadType != XML_READER_TYPE_END_ELEMENT))
1151                 {
1152                         if (xmlStrcasecmp(name, (const xmlChar*) null) == 0)
1153                         {
1154                                 AppLogDebug("value field must be not empty");
1155                                 break;
1156                         }
1157                         continue;
1158                 }
1159
1160                 if (xmlreadType == XML_READER_TYPE_END_ELEMENT)
1161                 {
1162                         if (pParentOfNewNode != null)
1163                         {
1164                                 pParentOfNewNode = pParentOfNewNode->GetParent();
1165                         }
1166                         continue;
1167                 }
1168
1169                 if (xmlTextReaderAttributeCount(reader) == 0)
1170                 {
1171                         if (xmlStrcasecmp(name, (const xmlChar*) "separator") != 0)
1172                         {
1173                                 continue;
1174                         }
1175                 }
1176
1177                 String elementIdType;
1178                 String elementId;
1179                 Node::NodeType newNodeType = Node::NODE_TYPE_LEAF;
1180
1181                 StringUtil::Utf8ToString((char*)(xmlTextReaderConstName(reader)), elementIdType);
1182                 StringUtil::Utf8ToString((char*)(xmlTextReaderGetAttribute(reader, (xmlChar*) "id")), elementId);
1183
1184                 for (int i = 0; i < ID_APPSETTING_MANAGER_ELEMENT_NODE_DELIMIT_COUNT; i++)
1185                 {
1186                         if (elementIdType.Equals(appSettingElementNodeDelimit[i], false))
1187                         {
1188                                 newNodeType = Node::NODE_TYPE_ELEMENT;
1189                                 break;
1190                         }
1191                 }
1192
1193                 Node* pCurrentNode = null;
1194                 if (newNodeType == Node::NODE_TYPE_ELEMENT)
1195                 {
1196                         ElementNode* pElementNode = dynamic_cast<ElementNode*>(pParentOfNewNode);
1197                         if (pElementNode != null)
1198                         {
1199                                 if (idList->Contains(String(elementId)))
1200                                 {
1201                                         AppLogDebug("continue elementId[%ls]", elementId.GetPointer());
1202                                         continue;
1203                                 }
1204                                 pParentOfNewNode = pElementNode->AddChild(elementId, elementIdType, newNodeType);
1205                                 pCurrentNode = pParentOfNewNode;
1206
1207                                 idList->Add(new (std::nothrow) String(elementId));
1208                         }
1209                 }
1210                 else
1211                 {
1212                         ElementNode* pElementNode = dynamic_cast<ElementNode*>(pParentOfNewNode);
1213                         if (pElementNode != null)
1214                         {
1215                                 if (xmlStrcasecmp(name, (const xmlChar*) "separator") == 0)
1216                                 {
1217                                         String separatorId;
1218                                         separatorId.Format(20, L"IDSP_%d", separatorCount);
1219
1220                                         if (idList->Contains(String(separatorId)))
1221                                         {
1222                                                 continue;
1223                                         }
1224                                         pCurrentNode = pElementNode->AddChild(separatorId, elementIdType, newNodeType);
1225                                         idList->Add(new (std::nothrow) String(separatorId));
1226                                         separatorCount++;
1227                                 }
1228                                 else
1229                                 {
1230                                         if (idList->Contains(String(elementId)))
1231                                         {
1232                                                 AppLogDebug("continue elementId[%ls]", elementId.GetPointer());
1233                                                 continue;
1234                                         }
1235                                         AppLogDebug("continue elementId[%ls]", elementId.GetPointer());
1236                                         pCurrentNode = pElementNode->AddChild(elementId, elementIdType, newNodeType);
1237                                         idList->Add(new (std::nothrow) String(elementId));
1238                                 }
1239                         }
1240                 }
1241
1242                 ByteBuffer* pBuf = null;
1243                 xmlChar* xmlAttriuteText = null;
1244                 for (int i = 0; i < ID_APPSETTING_MANAGER_NODE_ATTRIUBTE_DELIMIT_COUNT; i++)
1245                 {
1246                         pBuf = StringUtil::StringToUtf8N(appSettingNodeAttributeDelimit[i]);
1247                         if ((xmlAttriuteText = xmlTextReaderGetAttribute(reader, pBuf->GetPointer())) != null)
1248                         {
1249                                 String value;
1250                                 StringUtil::Utf8ToString((char*)xmlAttriuteText, value);
1251
1252                                 if (pCurrentNode != null)
1253                                 {
1254                                         pCurrentNode->AddAttribute(appSettingNodeAttributeDelimit[i], value);
1255                                 }
1256                         }
1257                         delete pBuf;
1258                 }
1259                 if (xmlStrcasecmp(name, (const xmlChar*) "separator") == 0)
1260                 {
1261                         if (pCurrentNode != null)
1262                         {
1263                                 pCurrentNode->AddAttribute(appSettingNodeAttributeDelimit[1], L"separator");
1264                                 pCurrentNode->AddAttribute(appSettingNodeAttributeDelimit[2], L"true");
1265                         }
1266                 }
1267         }
1268
1269         if (reader)
1270         {
1271                 xmlFreeTextReader(reader);
1272         }
1273         separatorCount = 0;
1274         delete pBuf;
1275         delete  idList;
1276         return E_SUCCESS;
1277 }
1278
1279 ElementNode*
1280 AppSettingManager::AddAppSettingRootNode(Tizen::Base::String rootId)
1281 {
1282         ElementNode* pRoot = new (std::nothrow) ElementNode(rootId, L"Root");
1283         if (pRoot == null)
1284         {
1285                 AppLogDebug("Root Create failed");
1286                 return null;
1287         }
1288
1289         result r = __pRootListHashMap->Add(*(new (std::nothrow) String(rootId)), *pRoot);
1290         if (IsFailed(r))
1291         {
1292                 AppLogDebug("Add failed = %s", GetErrorMessage(r));
1293                 delete pRoot;
1294                 pRoot = null;
1295         }
1296
1297         return pRoot;
1298 }
1299
1300 ElementNode*
1301 AppSettingManager::GetAppSettingRootNode(Tizen::Base::String rootId)
1302 {
1303         ElementNode* pValue = null;
1304         bool isFind = false;
1305
1306         if (__pRootListHashMap == null)
1307         {
1308                 AppLogDebug("__pRootListHashMap is null");
1309                 return pValue;
1310         }
1311
1312         result r = __pRootListHashMap->ContainsKey(rootId, isFind);
1313         if ((IsFailed(r))
1314                 || (isFind == false))
1315         {
1316                 AppLogDebug("ContainsKey fail[result : %s][isFind : %d]", GetErrorMessage(r), isFind);
1317                 return pValue;
1318         }
1319
1320         pValue = static_cast<ElementNode*>(__pRootListHashMap->GetValue(rootId));
1321         return pValue;
1322 }
1323
1324 result
1325 AppSettingManager::RemoveAppSettingRootNode(Tizen::Base::String rootId)
1326 {
1327         if (__pRootListHashMap == null)
1328         {
1329                 return E_INVALID_OPERATION;
1330         }
1331
1332         bool isFind = false;
1333         result r = __pRootListHashMap->ContainsKey(rootId, isFind);
1334         if ((IsFailed(r))
1335                 || (isFind == false))
1336         {
1337                 AppLogDebug("ContainsKey fail[result : %s][isFind : %d]", GetErrorMessage(r), isFind);
1338
1339                 if (isFind == false)
1340                 {
1341                         r = E_INVALID_KEY;
1342                 }
1343                 return r;
1344         }
1345
1346         r = __pRootListHashMap->Remove(rootId, false);
1347         return r;
1348 }
1349
1350 Tizen::Base::Collection::IMapEnumerator*
1351 AppSettingManager::GetRottNodeIEnumeratorN(void)
1352 {
1353         if (__pRootListHashMap == null)
1354         {
1355                 AppLogDebug("__pRootListHashMap is null");
1356                 return null;
1357         }
1358
1359         IMapEnumerator* pMapEnum = __pRootListHashMap->GetMapEnumeratorN();
1360         return pMapEnum;
1361 }
1362
1363 int
1364 AppSettingManager::GetRootNodeCount(void)
1365 {
1366         int rootNodeCount = 0;
1367
1368         if (__pRootListHashMap == null)
1369         {
1370                 AppLogDebug("__pRootListHashMap is null");
1371                 return rootNodeCount;
1372         }
1373
1374         rootNodeCount = __pRootListHashMap->GetCount();
1375         return rootNodeCount;
1376 }