Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_wearable / plugins-installer / plugin_objects.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  * @file    plugin_objects.h
18  * @author  Grzegorz Krawczyk (g.krawczyk@samgsung.com)
19  * @version
20  * @brief
21  */
22 #include <string>
23 #include <dpl/log/log.h>
24 #include "plugin_objects.h"
25 #include <IObject.h>
26
27 namespace {
28 const char* SEPARATOR = ".";
29 const std::string GLOBAL_OBJECT_NAME = WrtPluginsApi::IObject::WINDOW_OBJECT();
30 const std::string OLD_GLOBAL_OBJECT_NAME = "GLOBAL_OBJECT";
31
32 std::string normalizeName(const std::string& objectName)
33 {
34     if (objectName.empty()) {
35         LogError("Normalize name, name size is 0");
36         return objectName;
37     }
38
39     if (!objectName.compare(0, GLOBAL_OBJECT_NAME.size(),
40                             GLOBAL_OBJECT_NAME))
41     {
42         return objectName;
43     }
44     if (!objectName.compare(0, OLD_GLOBAL_OBJECT_NAME.size(),
45                             OLD_GLOBAL_OBJECT_NAME))
46     {
47         return GLOBAL_OBJECT_NAME;
48     }
49
50     //each object in storage has name started from $GLOBAL_OBJECT_NAME$
51     return GLOBAL_OBJECT_NAME + std::string(SEPARATOR) + objectName;
52 }
53
54 std::string normalizeName(const std::string& objectName,
55                           const std::string& parentName)
56 {
57     if (objectName.empty() || parentName.empty()) {
58         LogError("Normalize name, name size or parent name size is 0");
59         return std::string();
60     }
61
62     std::string normalizedName;
63     normalizedName = normalizeName(parentName) +
64         std::string(SEPARATOR) + objectName;
65
66     return normalizedName;
67 }
68 }
69
70 PluginObjects::PluginObjects()
71 {
72     m_implemented = ObjectsPtr(new Objects());
73     m_dependent = ObjectsPtr(new Objects());
74 }
75
76 PluginObjects::ObjectsPtr PluginObjects::getImplementedObject() const
77 {
78     return m_implemented;
79 }
80
81 PluginObjects::ObjectsPtr PluginObjects::getDependentObjects() const
82 {
83     return m_dependent;
84 }
85
86 void PluginObjects::addObjects(const std::string& parentName,
87                                const std::string& name)
88 {
89     LogDebug("\n Parent: " << parentName
90                            << "\n Name: " << name
91                            << "\n After: Implemented: " <<
92              normalizeName(name, parentName)
93                            << "\n After Dependent: " <<
94              normalizeName(parentName)
95              );
96
97     addImplementedObject(normalizeName(name, parentName));
98     addDependentObject(normalizeName(parentName));
99 }
100
101 void PluginObjects::addDependentObject(const std::string& value)
102 {
103     if (!value.compare(GLOBAL_OBJECT_NAME)) {
104         //dont add dependency to GLOBAL_OBJECT
105         return;
106     }
107     m_dependent->insert(value);
108 }
109
110 bool PluginObjects::hasObject(const std::string& name) const
111 {
112     return m_implemented->find(name) != m_implemented->end();
113 }
114
115 void PluginObjects::addImplementedObject(const std::string& value)
116 {
117     m_implemented->insert(value);
118 }