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 std::map<FeatureHandle, std::string> featureNames = FeatureDAOReadOnly::GetNames();
99 std::multimap<FeatureHandle, std::string> deviceCapabilities = FeatureDAOReadOnly::GetDevCapWithFeatureHandle();
101 FOREACH(pluginIt, m_pluginModels)
103 PluginModelPtr model = *pluginIt;
104 LogDebug("Analyzing Plugin model: " << model->Handle.Get());
106 WrtDB::DbPluginHandle pluginHandle = model->Handle.Get();
107 FeatureHandleListPtr featureHandles = model->FeatureHandles.Get();
109 FOREACH(featureIt, *featureHandles)
111 LogDebug("Analyzing feature: " << *featureIt);
112 FeatureModelPtr model = getFeatureModel(*featureIt);
114 LogDebug("Model for feature:" << *featureIt << " already created");
118 LogDebug("Creating Model for feature:" << *featureIt);
120 model.Reset(new FeatureModel(*featureIt));
122 std::set<std::string> devCapList;
123 auto ret = deviceCapabilities.equal_range(*featureIt);
124 for (auto devCapIt = ret.first; devCapIt != ret.second; devCapIt++)
126 devCapList.insert((*devCapIt).second);
128 model->SetData(featureNames.find(*featureIt)->second, devCapList, pluginHandle);
129 m_featureModels.insert(model);
134 void PluginContainerSupport::readStandardFeaturesList()
136 LogDebug("Reading standard features list from file...");
138 std::string path = GlobalConfig::GetDevicePluginPath();
140 path += STANDARD_FEATURES_LIST_FILE;
142 std::ifstream standardFeatureFile;
143 standardFeatureFile.open(path.c_str(), std::ifstream::in);
145 if (!standardFeatureFile.is_open()) {
146 LogError("Reading standard features list from file FAILED.");
152 while (!standardFeatureFile.eof()) {
153 standardFeatureFile.getline(buffer, sizeof(buffer));
155 if (buffer[0] == '\0') {
159 LogDebug("Standard Feature: <" << buffer << ">");
160 m_standardFeatureList.push_back(std::string(buffer));
163 standardFeatureFile.close();
167 PluginContainerSupport::getFeatureModel(const std::string &name) const
169 FOREACH(iter, m_featureModels)
171 if ((*iter)->Name.Get() == name) {
176 return FeatureModelPtr();
180 PluginContainerSupport::getFeatureModel(const FeatureHandle handle) const
182 FOREACH(iter, m_featureModels)
184 if ((*iter)->FHandle.Get() == handle) {
189 return FeatureModelPtr();
193 PluginContainerSupport::getPluginModelById(DbPluginHandle handle) const
195 FOREACH(pluginModel, m_pluginModels)
197 if ((*pluginModel)->Handle.Get() == handle) {
202 return PluginModelPtr();
206 PluginContainerSupport::getPluginModel(const FeatureModelPtr &feature) const
209 Assert(feature && "Null Ptr for feature model");
210 LogDebug("Feature located in plugin: " << feature->PHandle.Get());
212 return getPluginModelById(feature->PHandle.Get());
215 PluginContainerSupport::FeaturesList
216 PluginContainerSupport::getStandardFeatures() const
219 FeaturesList standardFeatures;
221 FOREACH(it, m_standardFeatureList)
223 FeatureModelPtr feature = getFeatureModel(*it);
225 LogWarning("Feature does not exist in DB " << *it);
229 //TODO maybe it should be sorted
230 standardFeatures.push_back(feature);
233 return standardFeatures;
236 PluginContainerSupport::PluginsList
237 PluginContainerSupport::getStandardPlugins() const
241 auto features = getStandardFeatures();
243 FOREACH(it, features)
245 auto plugin = getPluginModel(*it);
248 LogError("PluginModel not found");
252 plugins.push_back(plugin);
259 PluginContainerSupport::getPluginForFeature(const std::string& featureName)
261 return getPluginModel(getFeatureModel(featureName));