2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file plugin_container_support.h
18 * @author Grzegorz Krawczyk (g.krawczyk@samsung.com)
22 #include "plugin_container_support.h"
26 #include <dpl/foreach.h>
27 #include <dpl/wrt-dao-ro/global_config.h>
30 const char *STANDARD_FEATURES_LIST_FILE = "standard-features-list";
33 using namespace WrtDB;
35 PluginContainerSupport::PluginContainerSupport()
37 // Retrieve plugin model list
40 // Retrieve features model list
43 // Reading standard features list from file
44 readStandardFeaturesList();
47 PluginContainerSupport::~PluginContainerSupport()
49 // Remove all plugin models
50 m_pluginModels.clear();
52 // Remove all feature models
53 m_featureModels.clear();
55 // Clear standard features list
56 m_standardFeatureList.clear();
59 void PluginContainerSupport::registerPluginModel(DbPluginHandle handle)
61 PluginModelPtr model = getPluginModelById(handle);
64 LogDebug("Model for plugin:" << handle << " already registered");
68 LogDebug("Creating Model for plugin: " << handle);
70 if (PluginDAOReadOnly::INSTALLATION_COMPLETED !=
71 PluginDAOReadOnly::getInstallationStateForHandle(handle))
73 LogWarning("Failed To CreateModel for handle " << handle);
77 model.Reset(new PluginModel(handle));
79 LogInfo("Model Created. Handle: " <<
80 handle << ", name: " << model->LibraryName.Get());
82 m_pluginModels.insert(model);
85 void PluginContainerSupport::readPlugins()
87 LogDebug("Retrieving installed plugin list...");
88 PluginHandleList plugins = PluginDAOReadOnly::getPluginHandleList();
92 registerPluginModel(*it);
96 void PluginContainerSupport::readFeatures()
98 FOREACH(pluginIt, m_pluginModels)
100 PluginModelPtr model = *pluginIt;
101 LogDebug("Analyzing Plugin model: " << model->Handle.Get());
103 FeatureHandleListPtr featureHandles = model->FeatureHandles.Get();
104 FOREACH(featureIt, *featureHandles)
106 LogDebug("Analyzing feature: " << *featureIt);
107 registerFeatureModel(*featureIt, model->Handle.Get());
112 void PluginContainerSupport::readStandardFeaturesList()
114 LogDebug("Reading standard features list from file...");
116 std::string path = GlobalConfig::GetDevicePluginPath();
118 path += STANDARD_FEATURES_LIST_FILE;
120 std::ifstream standardFeatureFile;
121 standardFeatureFile.open(path.c_str(), std::ifstream::in);
123 if (!standardFeatureFile.is_open()) {
124 LogError("Reading standard features list from file FAILED.");
130 while (!standardFeatureFile.eof()) {
131 standardFeatureFile.getline(buffer, sizeof(buffer));
133 if (buffer[0] == '\0') {
137 LogDebug("Standard Feature: <" << buffer << ">");
138 m_standardFeatureList.push_back(std::string(buffer));
141 standardFeatureFile.close();
145 PluginContainerSupport::getFeatureModel(const std::string &name) const
147 FOREACH(iter, m_featureModels)
149 if ((*iter)->Name.Get() == name) {
154 return FeatureModelPtr();
158 PluginContainerSupport::getFeatureModel(const FeatureHandle handle) const
160 FOREACH(iter, m_featureModels)
162 if ((*iter)->FHandle.Get() == handle) {
167 return FeatureModelPtr();
171 PluginContainerSupport::getPluginModelById(DbPluginHandle handle) const
173 FOREACH(pluginModel, m_pluginModels)
175 if ((*pluginModel)->Handle.Get() == handle) {
180 return PluginModelPtr();
183 void PluginContainerSupport::registerFeatureModel(FeatureHandle featureHandle,
184 WrtDB::DbPluginHandle pluginHandle)
186 FeatureModelPtr model = getFeatureModel(featureHandle);
189 LogDebug("Model for feature:" << featureHandle << " already created");
193 LogDebug("Creating Model for feature:" << featureHandle);
196 model.Reset(new FeatureModel(featureHandle));
199 FeatureDAOReadOnly dao(featureHandle);
200 // PluginHandle pluginHandle = dao.GetPluginHandle();
202 model->Name.Set(dao.GetName());
203 model->DeviceCapabilities.Set(dao.GetDeviceCapabilities());
204 model->PHandle.Set(pluginHandle);
206 // Insert new model into map
207 m_featureModels.insert(model);
211 PluginContainerSupport::getPluginModel(const FeatureModelPtr &feature) const
214 Assert(feature && "Null Ptr for feature model");
215 LogDebug("Feature located in plugin: " << feature->PHandle.Get());
217 return getPluginModelById(feature->PHandle.Get());
220 PluginContainerSupport::FeaturesList
221 PluginContainerSupport::getStandardFeatures() const
224 FeaturesList standardFeatures;
226 FOREACH(it, m_standardFeatureList)
228 FeatureModelPtr feature = getFeatureModel(*it);
230 LogWarning("Feature does not exist in DB" << *it);
234 //TODO maybe it should be sorted
235 standardFeatures.push_back(feature);
238 return standardFeatures;
241 PluginContainerSupport::PluginsList
242 PluginContainerSupport::getStandardPlugins() const
246 auto features = getStandardFeatures();
248 FOREACH(it, features)
250 auto plugin = getPluginModel(*it);
253 LogError("PluginModel not found");
257 plugins.push_back(plugin);
264 PluginContainerSupport::getPluginForFeature(const std::string& featureName)
266 return getPluginModel(getFeatureModel(featureName));