/** \file PluginsManager.cpp \brief Define the class to manage and load the plugins \author alpha_one_x86 \licence GPL3, see the file COPYING */ #include #include #include #include #include #include "PluginsManager.h" /// \brief Create the manager and load the defaults variables PluginsManager::PluginsManager() { ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QStringLiteral("start")); //load the overall instance pluginLoaded = false; language = QStringLiteral("en"); stopIt = false; pluginInformation = NULL; #ifndef ULTRACOPIER_PLUGIN_ALL_IN_ONE importingPlugin = false; #endif editionSemList.release(); englishPluginType << QStringLiteral("CopyEngine") << QStringLiteral("Languages") << QStringLiteral("Listener") << QStringLiteral("PluginLoader") << QStringLiteral("SessionLoader") << QStringLiteral("Themes"); //catPlugin << tr("CopyEngine") << tr("Languages") << tr("Listener") << tr("PluginLoader") << tr("SessionLoader") << tr("Themes"); #ifdef ULTRACOPIER_PLUGIN_IMPORT_SUPPORT connect(&decodeThread, &QXzDecodeThread::decodedIsFinish, this, &PluginsManager::decodingFinished,Qt::QueuedConnection); #endif connect(this, &PluginsManager::finished, this, &PluginsManager::post_operation,Qt::QueuedConnection); // connect(this, &PluginsManager::pluginListingIsfinish, options,&OptionEngine::setInterfaceValue); //load the plugins list /// \bug bug when I put here: moveToThread(this);, due to the direction connection to remove the plugin start(); } /// \brief Destroy the manager PluginsManager::~PluginsManager() { stopIt=true; if(pluginInformation!=NULL) delete pluginInformation; if(this->isRunning()) this->wait(0); } /// \brief set current language void PluginsManager::setLanguage(const QString &language) { this->language=language; } void PluginsManager::post_operation() { pluginLoaded=true; emit pluginListingIsfinish(); } bool PluginsManager::allPluginHaveBeenLoaded() const { return pluginLoaded; } void PluginsManager::lockPluginListEdition() { editionSemList.acquire(); } void PluginsManager::unlockPluginListEdition() { editionSemList.release(); } void PluginsManager::run() { regexp_to_clean_1=QRegularExpression(QStringLiteral("[\n\r]+")); regexp_to_clean_2=QRegularExpression(QStringLiteral("[ \t]+")); regexp_to_clean_3=QRegularExpression(QStringLiteral("(&&)+")); regexp_to_clean_4=QRegularExpression(QStringLiteral("^&&")); regexp_to_clean_5=QRegularExpression(QStringLiteral("&&$")); regexp_to_dep_1=QRegularExpression(QStringLiteral("(&&|\\|\\||\\(|\\))")); regexp_to_dep_2=QRegularExpression(QStringLiteral("^(<=|<|=|>|>=)[a-zA-Z0-9\\-]+-([0-9]+\\.)*[0-9]+$")); regexp_to_dep_3=QRegularExpression(QStringLiteral("(<=|<|=|>|>=)")); regexp_to_dep_4=QRegularExpression(QStringLiteral("-([0-9]+\\.)*[0-9]+")); regexp_to_dep_5=QRegularExpression(QStringLiteral("[a-zA-Z0-9\\-]+-")); regexp_to_dep_6=QRegularExpression(QStringLiteral("[a-zA-Z0-9\\-]+-([0-9]+\\.)*[0-9]+")); //load the path and plugins into the path QStringList readPath; readPath << ResourcesManager::resourcesManager->getReadPath(); pluginsList.clear(); ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QStringLiteral("pluginsList.size(): ")+QString::number(pluginsList.size())); foreach(QString basePath,readPath) { foreach(QString dirSub,englishPluginType) { QString pluginComposed=basePath+dirSub+QDir::separator(); QDir dir(pluginComposed); if(stopIt) return; ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QStringLiteral("search plugin into: ")+pluginComposed); if(dir.exists()) { foreach(QString dirName, dir.entryList(QDir::Dirs|QDir::NoDotAndDotDot)) { if(stopIt) return; loadPluginInformation(pluginComposed+dirName+QDir::separator()); } } } } #ifdef ULTRACOPIER_DEBUG int index_debug=0; const int &loop_size=pluginsList.size(); while(index_debug list; int index=0; while(indexcategoryToTranslation(category); } bool PluginsManager::isSamePlugin(const PluginsAvailable &pluginA,const PluginsAvailable &pluginB) { /*if(pluginA.category!=pluginB.category) return false;*/ //only this test should be suffisent if(pluginA.path!=pluginB.path) return false; /*if(pluginA.name!=pluginB.name) return false; if(pluginA.writablePath!=pluginB.writablePath) return false; if(pluginA.categorySpecific!=pluginB.categorySpecific) return false; if(pluginA.version!=pluginB.version) return false; if(pluginA.informations!=pluginB.informations) return false;*/ return true; } bool PluginsManager::loadPluginInformation(const QString &path) { PluginsAvailable tempPlugin; tempPlugin.isAuth = false; tempPlugin.path = path; tempPlugin.category = PluginType_Unknow; QDir pluginPath(path); if(pluginPath.cdUp() && pluginPath.cdUp() && ResourcesManager::resourcesManager->getWritablePath()!="" && pluginPath==QDir(ResourcesManager::resourcesManager->getWritablePath())) tempPlugin.isWritable=true; else tempPlugin.isWritable=false; QFile xmlMetaData(path+QStringLiteral("informations.xml")); if(xmlMetaData.exists()) { if(xmlMetaData.open(QIODevice::ReadOnly)) { loadPluginXml(&tempPlugin,xmlMetaData.readAll()); xmlMetaData.close(); } else { tempPlugin.errorString=tr("informations.xml is not accessible"); ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,QStringLiteral("informations.xml is not accessible into the plugin: ")+path); } } else { tempPlugin.errorString=tr("informations.xml not found for the plugin"); ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,QStringLiteral("informations.xml not found for the plugin: ")+path); } editionSemList.acquire(); pluginsList << tempPlugin; if(tempPlugin.errorString==QStringLiteral("")) pluginsListIndexed.insert(tempPlugin.category,tempPlugin); editionSemList.release(); if(tempPlugin.errorString==QStringLiteral("")) return true; else { emit onePluginInErrorAdded(tempPlugin); ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,QStringLiteral("Error detected, the not loaded: ")+tempPlugin.errorString+QStringLiteral(", for path: ")+tempPlugin.path); return false; } } void PluginsManager::loadPluginXml(PluginsAvailable * thePlugin,const QByteArray &xml) { QString errorStr; int errorLine; int errorColumn; QDomDocument domDocument; if (!domDocument.setContent(xml, false, &errorStr,&errorLine,&errorColumn)) { thePlugin->errorString=tr("%1, parse error at line %2, column %3: %4").arg(QStringLiteral("informations.xml")).arg(errorLine).arg(errorColumn).arg(errorStr); ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QStringLiteral("%1, Parse error at line %2, column %3: %4").arg(QStringLiteral("informations.xml")).arg(errorLine).arg(errorColumn).arg(errorStr)); } else { QDomElement root = domDocument.documentElement(); if (root.tagName() != QStringLiteral("package")) { thePlugin->errorString=tr("\"package\" root tag not found for the xml file"); ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QStringLiteral("\"package\" root balise not found for the xml file")); } //load the variable if(thePlugin->errorString.isEmpty()) loadBalise(root,QStringLiteral("title"),&(thePlugin->informations),&(thePlugin->errorString),true,true,true); if(thePlugin->errorString.isEmpty()) loadBalise(root,QStringLiteral("website"),&(thePlugin->informations),&(thePlugin->errorString),false,true); if(thePlugin->errorString.isEmpty()) loadBalise(root,QStringLiteral("description"),&(thePlugin->informations),&(thePlugin->errorString),true,true,true); if(thePlugin->errorString.isEmpty()) loadBalise(root,QStringLiteral("author"),&(thePlugin->informations),&(thePlugin->errorString),true,false); if(thePlugin->errorString.isEmpty()) loadBalise(root,QStringLiteral("pubDate"),&(thePlugin->informations),&(thePlugin->errorString),true,false); if(thePlugin->errorString.isEmpty()) { loadBalise(root,QStringLiteral("version"),&(thePlugin->informations),&(thePlugin->errorString),true,false); if(thePlugin->errorString.isEmpty()) thePlugin->version=thePlugin->informations.last().last(); } if(thePlugin->errorString.isEmpty()) { loadBalise(root,QStringLiteral("category"),&(thePlugin->informations),&(thePlugin->errorString),true,false); if(thePlugin->errorString.isEmpty()) { QString tempCat=thePlugin->informations.last().last(); if(tempCat==QStringLiteral("Languages")) thePlugin->category=PluginType_Languages; else if(tempCat==QStringLiteral("CopyEngine")) thePlugin->category=PluginType_CopyEngine; else if(tempCat==QStringLiteral("Listener")) thePlugin->category=PluginType_Listener; else if(tempCat==QStringLiteral("PluginLoader")) thePlugin->category=PluginType_PluginLoader; else if(tempCat==QStringLiteral("SessionLoader")) thePlugin->category=PluginType_SessionLoader; else if(tempCat==QStringLiteral("Themes")) thePlugin->category=PluginType_Themes; else thePlugin->errorString=QStringLiteral("Unknow category: ")+QString::number((int)thePlugin->category); if(thePlugin->errorString.isEmpty()) { if(thePlugin->category!=PluginType_Languages) { #ifndef ULTRACOPIER_PLUGIN_ALL_IN_ONE loadBalise(root,QStringLiteral("architecture"),&(thePlugin->informations),&(thePlugin->errorString),true,false); if(thePlugin->errorString.isEmpty()) { if(thePlugin->informations.last().last()!=ULTRACOPIER_PLATFORM_CODE) thePlugin->errorString=QStringLiteral("Wrong platform code: ")+thePlugin->informations.last().last(); } #endif } } } } if(thePlugin->errorString.isEmpty()) { loadBalise(root,QStringLiteral("name"),&(thePlugin->informations),&(thePlugin->errorString),true,false); if(thePlugin->errorString.isEmpty()) { thePlugin->name=thePlugin->informations.last().last(); int index=0; const int &loop_size=pluginsList.size(); int sub_index,loop_sub_size; while(indexname && pluginsList.at(index).category==thePlugin->category) { ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Plugin duplicate found ("+QString::number((int)thePlugin->category)+"/"+pluginsList.at(index).informations.at(sub_index).last()+"), already loaded, actual version skipped: "+thePlugin->version); thePlugin->errorString=tr("Duplicated plugin found, already loaded!"); break; break; } sub_index++; } index++; } } } if(thePlugin->errorString.isEmpty()) loadBalise(root,QStringLiteral("dependencies"),&(thePlugin->informations),&(thePlugin->errorString),true,false); if(thePlugin->errorString.isEmpty()) { QDomElement child = root.firstChildElement(QStringLiteral("categorySpecific")); if(!child.isNull() && child.isElement()) thePlugin->categorySpecific=child; } } } /// \brief to load the multi-language balise void PluginsManager::loadBalise(const QDomElement &root,const QString &name,QList *informations,QString *errorString,bool needHaveOneEntryMinimum,bool multiLanguage,bool englishNeedBeFound) { int foundElement=0; bool englishTextIsFoundForThisChild=false; QDomElement child = root.firstChildElement(name); while(!child.isNull()) { if(child.isElement()) { QStringList newInformations; if(multiLanguage) { if(child.hasAttribute(QStringLiteral("xml:lang"))) { if(child.attribute(QStringLiteral("xml:lang"))==QStringLiteral("en")) englishTextIsFoundForThisChild=true; foundElement++; newInformations << child.tagName() << child.attribute(QStringLiteral("xml:lang")) << child.text(); } else ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,QStringLiteral("Have not the attribute xml:lang: child.tagName(): %1, child.text(): %2").arg(child.tagName()).arg(child.text())); } else { foundElement++; newInformations << child.tagName() << child.text(); } *informations << newInformations; } else ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,QStringLiteral("Is not Element: child.tagName(): %1").arg(child.tagName())); child = child.nextSiblingElement(name); } if(multiLanguage && englishTextIsFoundForThisChild==false && englishNeedBeFound) { informations->clear(); *errorString=tr("English text missing in the informations.xml for the tag: %1").arg(name); ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,QStringLiteral("English text missing into the informations.xml for the tag: %1").arg(name)); return; } if(needHaveOneEntryMinimum && foundElement==0) { informations->clear(); ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,QStringLiteral("Tag not found: %1").arg(name)); *errorString=tr("Tag not found: %1").arg(name); } } /// \brief to load the get dom specific QString PluginsManager::getDomSpecific(const QDomElement &root,const QString &name,const QList > &listChildAttribute) const { QDomElement child = root.firstChildElement(name); int index,loop_size; bool allIsFound; while(!child.isNull()) { if(child.isElement()) { allIsFound=true; index=0; loop_size=listChildAttribute.size(); while(index")) defaultReturnValue=false; while(indexreaNumberB) return false; if(reaNumberA")) { if(reaNumberAreaNumberB) return true; } if(sign==QStringLiteral("<=")) { if(reaNumberA>reaNumberB) return false; if(reaNumberA=")) { if(reaNumberAreaNumberB) return true; } index++; } return defaultReturnValue; } QList PluginsManager::getPluginsByCategory(const PluginType &category) const { return pluginsListIndexed.values(category); } QList PluginsManager::getPlugins(bool withError) const { QList list; int index=0; while(indexsetLanguage(mainShortName); pluginInformation->setPlugin(pluginsList.at(index)); pluginInformation->show(); return; } index++; } ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,QStringLiteral("item not selected")); } void PluginsManager::showInformationDoubleClick() { // showInformation(false); } #ifdef ULTRACOPIER_PLUGIN_IMPORT_SUPPORT void PluginsManager::removeThePluginSelected(const QString &path) { ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QStringLiteral("start")); int index=0; while(index cppdata; cppdata.resize(data.size()); memcpy(cppdata.data(),data.data(),data.size()); if(!tarFile.decodeData(cppdata)) { ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"tarFile.errorString(): "+QString::fromStdString(tarFile.errorString())); QMessageBox::critical(NULL,tr("Plugin loader"),tr("Unable to load the plugin content, please check it: %1").arg(QString::fromStdString(tarFile.errorString()))); } else { std::vector fileList = tarFile.getFileList(); std::vector > dataList = tarFile.getDataList(); if(fileList.size()>1) { QString basePluginArchive=QStringLiteral(""); /* block use less for tar? if(fileList.at(0).contains(QRegularExpression("[\\/]"))) { bool folderFoundEveryWhere=true; basePluginArchive=fileList.at(0); basePluginArchive.remove(QRegularExpression("[\\/].*$")); for (int i = 0; i < list.size(); ++i) { if(!fileList.at(i).startsWith(basePluginArchive)) { folderFoundEveryWhere=false; break; } } if(folderFoundEveryWhere) { for (int i = 0; i < fileList.size(); ++i) fileList[i].remove(0,basePluginArchive.size()); } else basePluginArchive=""; }*/ PluginsAvailable tempPlugin; QString categoryFinal=QStringLiteral(""); for (unsigned int i = 0; i < fileList.size(); ++i) if(fileList.at(i)=="informations.xml") { loadPluginXml(&tempPlugin,QByteArray(dataList.at(i).data(),dataList.at(i).size())); break; } if(tempPlugin.errorString=="") { categoryFinal=categoryToString(tempPlugin.category); if(categoryFinal!="") { QString writablePath=ResourcesManager::resourcesManager->getWritablePath(); if(writablePath!="") { QDir dir; QString finalPluginPath=writablePath+categoryFinal+QDir::separator()+tempPlugin.name+QDir::separator(); ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"writablePath: \""+writablePath+"\""); ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"basePluginArchive: \""+basePluginArchive+"\""); ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"categoryFinal: \""+categoryFinal+"\""); ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Information,"finalPluginPath: \""+finalPluginPath+"\""); if(!dir.exists(finalPluginPath)) { bool errorFound=false; for (unsigned int i = 0; i < fileList.size(); ++i) { ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Information,"file "+QString::number(i)+": "+finalPluginPath+QString::fromStdString(fileList.at(i))); QString fileListEntry=QString::fromStdString(fileList[i]); fileListEntry.remove(QRegularExpression("^(..?[\\/])+")); QFile currentFile(finalPluginPath+fileListEntry); QFileInfo info(currentFile); if(!dir.exists(info.absolutePath())) if(!dir.mkpath(info.absolutePath())) { ResourcesManager::resourcesManager->disableWritablePath(); ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Critical,"Unable to make the path: "+info.absolutePath()); QMessageBox::critical(NULL,tr("Plugin loader"),tr("Unable to create a folder to install the plugin:\n%1").arg(info.absolutePath())); errorFound=true; break; } if(currentFile.open(QIODevice::ReadWrite)) { currentFile.write(QByteArray(dataList.at(i).data(),dataList.at(i).size())); currentFile.close(); } else { ResourcesManager::resourcesManager->disableWritablePath(); ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Critical,"Unable to make the file: "+info.absolutePath()+", error:"+currentFile.errorString()); QMessageBox::critical(NULL,tr("Plugin loader"),tr("Unable to create a file to install the plugin:\n%1\nsince:%2").arg(info.absolutePath()).arg(currentFile.errorString())); errorFound=true; break; } } if(!errorFound) { if(loadPluginInformation(finalPluginPath)) { ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Information,"push the new plugin into the real list"); while(checkDependencies()!=0){}; emit needLangToRefreshPluginList(); emit manuallyAdded(tempPlugin); } } } else { ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Critical,"Folder with same name is present, skip the plugin installation: "+finalPluginPath); QMessageBox::critical(NULL,tr("Plugin loader"),tr("Folder with same name is present, skip the plugin installation:\n%1").arg(finalPluginPath)); } } else { ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Critical,"Have not writable path, then how add you plugin?"); QMessageBox::critical(NULL,tr("Plugin loader"),tr("Unable to load the plugin content, please check it")); } } else { ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"category into informations.xml not found!"); QMessageBox::critical(NULL,tr("Plugin loader"),tr("Unable to load the plugin content, please check it")); } } else { ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Error in the xml: "+tempPlugin.errorString); QMessageBox::critical(NULL,tr("Plugin loader"),tr("Unable to load the plugin content, please check it: %1").arg(tempPlugin.errorString)); } } else { ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"No file found into the plugin"); QMessageBox::critical(NULL,tr("Plugin loader"),tr("Unable to load the plugin content, please check it")); } } } else { ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"decodeThread.errorFound(), error: "+decodeThread.errorString()); QMessageBox::critical(NULL,tr("Plugin loader"),tr("Unable to load the plugin content, please check it: %1").arg(decodeThread.errorString())); } importingPlugin=false; } #endif #ifndef ULTRACOPIER_PLUGIN_ALL_IN_ONE void PluginsManager::newAuthPath(const QString &path) { ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QStringLiteral("start")); int index=0; while(index