Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_mobile / modules / tizen / WidgetInterface / WidgetInterface.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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  * @author      Lukasz Marek (l.marek@samsung.com)
18  * @version     0.1
19  * @brief
20  */
21
22 #include "WidgetInterface.h"
23 #include <string>
24 #include <dpl/exception.h>
25 #include <dpl/log/log.h>
26 #include <dpl/optional.h>
27 #include <dpl/string.h>
28 #include <wrt-commons/widget-interface-dao/widget_interface_dao.h>
29 #include <Commons/Exception.h>
30
31 namespace WrtDeviceApis {
32 using namespace WidgetInterfaceDB;
33
34 WidgetInterfaceObject::WidgetInterfaceObject(int widgetHandle)
35 {
36     Try
37     {
38         m_dao.reset(new WidgetInterfaceDAO(widgetHandle));
39     }
40     Catch(WidgetInterfaceDAO::Exception::DatabaseError)
41     {
42         ReThrow(Commons::PlatformException);
43     }
44 }
45
46 WidgetInterfaceObject::~WidgetInterfaceObject()
47 {
48     m_dao.reset();
49 }
50
51 void WidgetInterfaceObject::setItem(const std::string& key,
52                                     const std::string& value,
53                                     bool readOnly)
54 {
55     Try
56     {
57         m_dao->setItem(key, value, readOnly);
58     }
59     Catch(WidgetInterfaceDAO::Exception::DatabaseError)
60     {
61         ReThrow(Commons::PlatformException);
62     }
63     Catch(WidgetInterfaceDAO::Exception::LocalStorageValueNoModifableException)
64     {
65         LogError("Cannot delete item. Item is readonly");
66         ReThrow(Commons::LocalStorageValueNoModifableException);
67     }
68 }
69
70 void WidgetInterfaceObject::removeItem(const std::string& key)
71 {
72     Try
73     {
74         m_dao->removeItem(key);
75     }
76     Catch(WidgetInterfaceDAO::Exception::DatabaseError)
77     {
78         ReThrow(Commons::PlatformException);
79     }
80     Catch(WidgetInterfaceDAO::Exception::LocalStorageValueNoModifableException)
81     {
82         LogError("Cannot delete item. Item is readonly");
83         ReThrow(Commons::LocalStorageValueNoModifableException);
84     }
85 }
86
87 DPL::Optional<std::string> WidgetInterfaceObject::getValue(
88     const std::string& key) const
89 {
90     Try
91     {
92         return m_dao->getValue(key);
93     }
94     Catch(WidgetInterfaceDAO::Exception::DatabaseError)
95     {
96         ReThrow(Commons::PlatformException);
97     }
98 }
99
100 void WidgetInterfaceObject::clear(bool removeReadOnly)
101 {
102     Try
103     {
104         m_dao->clear(removeReadOnly);
105     }
106     Catch(WidgetInterfaceDAO::Exception::DatabaseError)
107     {
108         ReThrow(Commons::PlatformException);
109     }
110 }
111
112 size_t WidgetInterfaceObject::getStorageSize() const
113 {
114     Try
115     {
116         return m_dao->getStorageSize();
117     }
118     Catch(WidgetInterfaceDAO::Exception::DatabaseError)
119     {
120         ReThrow(Commons::PlatformException);
121     }
122 }
123
124 std::string WidgetInterfaceObject::getKeyByIndex(size_t index) const
125 {
126     Try
127     {
128         return m_dao->getKeyByIndex(index);
129     }
130     Catch(WidgetInterfaceDAO::Exception::DatabaseError)
131     {
132         ReThrow(Commons::PlatformException);
133     }
134     Catch(WidgetInterfaceDAO::Exception::InvalidArgumentException)
135     {
136         ReThrow(Commons::InvalidArgumentException);
137     }
138 }
139 }