Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src / 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/string.h>
27 #include <wrt-commons/widget-interface-dao/widget_interface_dao.h>
28 #include <Commons/Exception.h>
29
30 namespace WrtDeviceApis {
31 using namespace WidgetInterfaceDB;
32
33 WidgetInterfaceObject::WidgetInterfaceObject(WidgetHandle widgetHandle)
34 {
35     Try
36     {
37         m_dao.reset(new WidgetInterfaceDAO(widgetHandle));
38     }
39     Catch(WidgetInterfaceDAO::Exception::DatabaseError)
40     {
41         ReThrow(Commons::PlatformException);
42     }
43 }
44
45 WidgetInterfaceObject::~WidgetInterfaceObject()
46 {
47     m_dao.reset();
48 }
49
50 void WidgetInterfaceObject::setItem(const std::string& key,
51                                     const std::string& value,
52                                     bool readOnly)
53 {
54     Try
55     {
56         m_dao->setItem(key, value, readOnly);
57     }
58     Catch(WidgetInterfaceDAO::Exception::DatabaseError)
59     {
60         ReThrow(Commons::PlatformException);
61     }
62     Catch(WidgetInterfaceDAO::Exception::LocalStorageValueNoModifableException)
63     {
64         LogError("Cannot delete item. Item is readonly");
65         ReThrow(Commons::LocalStorageValueNoModifableException);
66     }
67 }
68
69 void WidgetInterfaceObject::removeItem(const std::string& key)
70 {
71     Try
72     {
73         m_dao->removeItem(key);
74     }
75     Catch(WidgetInterfaceDAO::Exception::DatabaseError)
76     {
77         ReThrow(Commons::PlatformException);
78     }
79     Catch(WidgetInterfaceDAO::Exception::LocalStorageValueNoModifableException)
80     {
81         LogError("Cannot delete item. Item is readonly");
82         ReThrow(Commons::LocalStorageValueNoModifableException);
83     }
84 }
85
86 DPL::OptionalStdString WidgetInterfaceObject::getValue(
87     const std::string& key) const
88 {
89     Try
90     {
91         return m_dao->getValue(key);
92     }
93     Catch(WidgetInterfaceDAO::Exception::DatabaseError)
94     {
95         ReThrow(Commons::PlatformException);
96     }
97 }
98
99 void WidgetInterfaceObject::clear(bool removeReadOnly)
100 {
101     Try
102     {
103         m_dao->clear(removeReadOnly);
104     }
105     Catch(WidgetInterfaceDAO::Exception::DatabaseError)
106     {
107         ReThrow(Commons::PlatformException);
108     }
109 }
110
111 size_t WidgetInterfaceObject::getStorageSize() const
112 {
113     Try
114     {
115         return m_dao->getStorageSize();
116     }
117     Catch(WidgetInterfaceDAO::Exception::DatabaseError)
118     {
119         ReThrow(Commons::PlatformException);
120     }
121 }
122
123 std::string WidgetInterfaceObject::getKeyByIndex(size_t index) const
124 {
125     Try
126     {
127         return m_dao->getKeyByIndex(index);
128     }
129     Catch(WidgetInterfaceDAO::Exception::DatabaseError)
130     {
131         ReThrow(Commons::PlatformException);
132     }
133     Catch(WidgetInterfaceDAO::Exception::InvalidArgumentException)
134     {
135         ReThrow(Commons::InvalidArgumentException);
136     }
137 }
138 }