| 168 | void PluginManager::setMetadata(QJsonObject metadata) { m_metadata = metadata; } |
| 169 | |
| 170 | Plugin *PluginManager::loadPlugin(QString file) |
| 171 | { |
| 172 | bool ret; |
| 173 | Plugin *original = nullptr; |
| 174 | Plugin *clone = nullptr; |
| 175 | QObject *inst = nullptr; |
| 176 | QPluginLoader qp; |
| 177 | QString cloneName; |
| 178 | QString errorMsg; |
| 179 | PluginInfo::PluginState state = PluginInfo::PLUGIN_UNLOADED; |
| 180 | if(!QFile::exists(file)) { |
| 181 | state = PluginInfo::PLUGIN_FILE_NOT_FOUND; |
| 182 | goto finish; |
| 183 | } |
| 184 | |
| 185 | if(!QLibrary::isLibrary(file)) { |
| 186 | state = PluginInfo::PLUGIN_INVALID_LIBRARY; |
| 187 | goto finish; |
| 188 | } |
| 189 | |
| 190 | qp.setFileName(file); |
| 191 | ret = qp.load(); |
| 192 | if(!ret) { |
| 193 | errorMsg = "Cannot load library " + qp.fileName() + "- err: " + qp.errorString(); |
| 194 | state = PluginInfo::PLUGIN_LOAD_FAILED; |
| 195 | goto finish; |
| 196 | } |
| 197 | |
| 198 | inst = qp.instance(); |
| 199 | if(!inst) { |
| 200 | state = PluginInfo::PLUGIN_INSTANCE_FAILED; |
| 201 | goto finish; |
| 202 | } |
| 203 | |
| 204 | original = qobject_cast<Plugin *>(qp.instance()); |
| 205 | if(!original) { |
| 206 | state = PluginInfo::PLUGIN_INVALID_PLUGIN; |
| 207 | goto finish; |
| 208 | } |
| 209 | |
| 210 | clone = original->clone(this); |
| 211 | if(!clone) { |
| 212 | state = PluginInfo::PLUGIN_CLONE_FAILED; |
| 213 | goto finish; |
| 214 | } |
| 215 | |
| 216 | cloneName = clone->name(); |
| 217 | |
| 218 | if(cloneName == "") { |
| 219 | state = PluginInfo::PLUGIN_CLONE_FAILED; |
| 220 | goto finish; |
| 221 | } |
| 222 | state = PluginInfo::PLUGIN_LOADED; |
| 223 | finish: |
| 224 | PluginInfo pluginInfo(file, state, errorMsg, clone); |
| 225 | m_plugins.append(pluginInfo); |
| 226 | if(!pluginInfo.isLoaded()) { |
| 227 | qWarning(CAT_PLUGINMANAGER) << pluginInfo.getErrorMessage(); |