diff --git a/src/pimcommon/migration/autotests/migrateapplicationfilestest.cpp b/src/pimcommon/migration/autotests/migrateapplicationfilestest.cpp index 415b7b0..d4380f5 100644 --- a/src/pimcommon/migration/autotests/migrateapplicationfilestest.cpp +++ b/src/pimcommon/migration/autotests/migrateapplicationfilestest.cpp @@ -1,402 +1,402 @@ /* Copyright (c) 2015-2019 Montel Laurent This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "migrateapplicationfilestest.h" #include "../migrateapplicationfiles.h" #include #include #include #include #include using namespace PimCommon; MigrateApplicationFilesTest::MigrateApplicationFilesTest(QObject *parent) : QObject(parent) { } MigrateApplicationFilesTest::~MigrateApplicationFilesTest() { } void MigrateApplicationFilesTest::initTestCase() { //qDebug() << " QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)" < don't migrate old info. Put after checkIfNecessary to override value migrate.setCurrentConfigVersion(3); MigrateFileInfo info; info.setFolder(true); info.setPath(folderName); info.setType(QStringLiteral("data")); info.setVersion(2); migrate.insertMigrateInfo(info); QVERIFY(migrate.start()); - Q_FOREACH (const QString &file, files) { + for (const QString &file : qAsConst(files)) { QVERIFY(!QFile(xdgDatahome + file).exists()); } } void MigrateApplicationFilesTest::shouldMigrateFoldersWithSubFolders() { QTemporaryDir kdehomeDir; QVERIFY(kdehomeDir.isValid()); const QString kdehome = kdehomeDir.path(); qputenv("KDEHOME", QFile::encodeName(kdehome)); //Generate kde4 apps dir const QString folderName = QStringLiteral("foo/folder1/"); const QString appsPath = kdehome + QLatin1Char('/') + QLatin1String("share/apps/") + folderName; QDir().mkpath(appsPath); QVERIFY(QDir(appsPath).exists()); QStringList files; files << QStringLiteral("file1") << QStringLiteral("file2"); const QString xdgDatahome = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + folderName; - Q_FOREACH (const QString &file, files) { + for (const QString &file : qAsConst(files)) { QFile fooFile(QLatin1String(MIGRATION_DATA_DIR) + QLatin1Char('/') + file); QVERIFY(fooFile.exists()); const QString storedConfigFilePath = appsPath + QLatin1Char('/') + file; QVERIFY(QFile::copy(fooFile.fileName(), storedConfigFilePath)); const QString xdgFile = xdgDatahome + file; QVERIFY(!QFile::exists(xdgFile)); } //SubFolder. const QString subFolder = QStringLiteral("foo/folder1/subfolder/"); const QString appsPathSubfolder = kdehome + QLatin1Char('/') + QLatin1String("share/apps/") + subFolder; QDir().mkpath(appsPathSubfolder); QVERIFY(QDir(appsPathSubfolder).exists()); const QString xdgDatahomeSubfolder = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + subFolder; - Q_FOREACH (const QString &file, files) { + for (const QString &file : qAsConst(files)) { QFile fooFile(QLatin1String(MIGRATION_DATA_DIR) + QLatin1Char('/') + file); QVERIFY(fooFile.exists()); const QString storedConfigFilePath = appsPathSubfolder + QLatin1Char('/') + file; QVERIFY(QFile::copy(fooFile.fileName(), storedConfigFilePath)); const QString xdgFile = xdgDatahomeSubfolder + file; QVERIFY(!QFile::exists(xdgFile)); } MigrateApplicationFiles migrate; migrate.setConfigFileName(QStringLiteral("foorc")); MigrateFileInfo info; info.setFolder(true); info.setPath(folderName); info.setType(QStringLiteral("data")); info.setVersion(2); migrate.insertMigrateInfo(info); QVERIFY(migrate.checkIfNecessary()); migrate.setCurrentConfigVersion(1); QVERIFY(migrate.start()); - Q_FOREACH (const QString &file, files) { + for (const QString &file : qAsConst(files)) { QVERIFY(QFile(xdgDatahome + file).exists()); QVERIFY(QFile(xdgDatahomeSubfolder + file).exists()); } } void MigrateApplicationFilesTest::shouldNotMigrateIfAlreadyDone() { QTemporaryDir kdehomeDir; QVERIFY(kdehomeDir.isValid()); const QString kdehome = kdehomeDir.path(); qputenv("KDEHOME", QFile::encodeName(kdehome)); //Generate kde4 apps dir const QString folderName = QStringLiteral("foo/"); const QString appsPath = kdehome + QLatin1Char('/') + QLatin1String("share/apps/") + folderName; QDir().mkpath(appsPath); QVERIFY(QDir(appsPath).exists()); const QString xdgDatahome = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + folderName; QStringList files; files << QStringLiteral("file1") << QStringLiteral("file2"); - Q_FOREACH (const QString &file, files) { + for (const QString &file : qAsConst(files)) { QFile fooFile(QLatin1String(MIGRATION_DATA_DIR) + QLatin1Char('/') + file); QVERIFY(fooFile.exists()); const QString storedConfigFilePath = appsPath + QLatin1Char('/') + file; QVERIFY(QFile::copy(fooFile.fileName(), storedConfigFilePath)); const QString xdgFile = xdgDatahome + file; QVERIFY(!QFile::exists(xdgFile)); } MigrateApplicationFiles migrate; migrate.setConfigFileName(QStringLiteral("foorc")); MigrateFileInfo info; info.setPath(folderName + QStringLiteral("file1")); info.setType(QStringLiteral("data")); info.setVersion(2); migrate.insertMigrateInfo(info); MigrateFileInfo info2; info2.setPath(folderName + QStringLiteral("file2")); info2.setType(QStringLiteral("data")); info2.setVersion(2); migrate.insertMigrateInfo(info2); QVERIFY(migrate.checkIfNecessary()); //We have a current version == 3 => don't migrate old info. migrate.setCurrentConfigVersion(3); QVERIFY(migrate.start()); - Q_FOREACH (const QString &file, files) { + for (const QString &file : qAsConst(files)) { QVERIFY(!QFile(xdgDatahome + file).exists()); } } void MigrateApplicationFilesTest::shouldMigrateFiles() { QTemporaryDir kdehomeDir; QVERIFY(kdehomeDir.isValid()); const QString kdehome = kdehomeDir.path(); qputenv("KDEHOME", QFile::encodeName(kdehome)); //Generate kde4 apps dir const QString folderName = QStringLiteral("foo/"); const QString appsPath = kdehome + QLatin1Char('/') + QLatin1String("share/apps/") + folderName; QDir().mkpath(appsPath); QVERIFY(QDir(appsPath).exists()); const QString xdgDatahome = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + folderName; QStringList files; files << QStringLiteral("file1") << QStringLiteral("file2"); - Q_FOREACH (const QString &file, files) { + for (const QString &file : qAsConst(files)) { QFile fooFile(QLatin1String(MIGRATION_DATA_DIR) + QLatin1Char('/') + file); QVERIFY(fooFile.exists()); const QString storedConfigFilePath = appsPath + QLatin1Char('/') + file; //qDebug()<<" storedConfigFilePath"< This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "plugininterface.h" #include #include "pimcommonakonadi_debug.h" #include #include #include #include using namespace PimCommon; class PimCommon::PluginInterfacePrivate { public: PluginInterfacePrivate() : mParentWidget(nullptr) , mActionCollection(nullptr) , mGenericPluginManager(nullptr) { } QString mPluginName; QString mServiceTypeName; QWidget *mParentWidget = nullptr; KActionCollection *mActionCollection = nullptr; QVector mListGenericInterface; GenericPluginManager *mGenericPluginManager = nullptr; }; PluginInterface::PluginInterface(QObject *parent) : QObject(parent) , d(new PimCommon::PluginInterfacePrivate) { d->mGenericPluginManager = new GenericPluginManager(this); } PluginInterface::~PluginInterface() { delete d; } void PimCommon::PluginInterface::setActionCollection(KActionCollection *ac) { d->mActionCollection = ac; } void PluginInterface::initializePlugins() { d->mGenericPluginManager->setPluginName(d->mPluginName); d->mGenericPluginManager->setServiceTypeName(d->mServiceTypeName); if (!d->mGenericPluginManager->initializePlugins()) { qCDebug(PIMCOMMONAKONADI_LOG) << " Impossible to initialize plugins"; } } void PluginInterface::setPluginName(const QString &name) { d->mPluginName = name; } void PluginInterface::setServiceTypeName(const QString &name) { d->mServiceTypeName = name; } void PluginInterface::createPluginInterface() { if (!d->mActionCollection) { qCWarning(PIMCOMMONAKONADI_LOG) << "Missing action collection"; return; } - Q_FOREACH (PimCommon::GenericPlugin *plugin, d->mGenericPluginManager->pluginsList()) { + for (PimCommon::GenericPlugin *plugin : d->mGenericPluginManager->pluginsList()) { if (plugin->isEnabled()) { PimCommon::GenericPluginInterface *interface = static_cast(plugin->createInterface(this)); interface->setParentWidget(d->mParentWidget); //Add parent before creating action interface->createAction(d->mActionCollection); interface->setPlugin(plugin); connect(interface, &PimCommon::GenericPluginInterface::emitPluginActivated, this, &PluginInterface::slotPluginActivated); d->mListGenericInterface.append(interface); } } } void PluginInterface::initializeInterfaceRequires(PimCommon::AbstractGenericPluginInterface *interface) { Q_UNUSED(interface) } QString PluginInterface::actionXmlExtension(ActionType::Type type) { switch (type) { case PimCommon::ActionType::Tools: return QStringLiteral("_plugins_tools"); case PimCommon::ActionType::Edit: return QStringLiteral("_plugins_edit"); case PimCommon::ActionType::File: return QStringLiteral("_plugins_file"); case PimCommon::ActionType::Action: return QStringLiteral("_plugins_actions"); case PimCommon::ActionType::PopupMenu: return QStringLiteral("_popupmenu_actions"); case PimCommon::ActionType::ToolBar: return QStringLiteral("_toolbar_actions"); case PimCommon::ActionType::Message: return QStringLiteral("_plugins_message"); case PimCommon::ActionType::Folder: return QStringLiteral("_plugins_folder"); } return {}; } void PluginInterface::slotPluginActivated(PimCommon::AbstractGenericPluginInterface *interface) { initializeInterfaceRequires(interface); if (interface) { interface->exec(); } } void PluginInterface::setParentWidget(QWidget *widget) { d->mParentWidget = widget; } void PluginInterface::initializePluginActions(const QString &prefix, KXMLGUIClient *guiClient) { if (guiClient->factory()) { QHashIterator > localActionsType(actionsType()); while (localActionsType.hasNext()) { localActionsType.next(); QList lst = localActionsType.value(); if (!lst.isEmpty()) { const QString actionlistname = prefix + PimCommon::PluginInterface::actionXmlExtension(localActionsType.key()); guiClient->unplugActionList(actionlistname); guiClient->plugActionList(actionlistname, lst); } } } } void PluginInterface::updateActions(int numberOfSelectedItems, int numberOfSelectedCollections) { for (PimCommon::GenericPluginInterface *interface : qAsConst(d->mListGenericInterface)) { interface->updateActions(numberOfSelectedItems, numberOfSelectedCollections); } } QHash > PluginInterface::actionsType() { QHash > listType; for (PimCommon::GenericPluginInterface *interface : qAsConst(d->mListGenericInterface)) { for (const PimCommon::ActionType &actionType : interface->actionTypes()) { PimCommon::ActionType::Type type = actionType.type(); QList lst = listType.value(type); if (!lst.isEmpty()) { QAction *act = new QAction(this); act->setSeparator(true); lst << act << actionType.action(); listType.insert(type, lst); } else { listType.insert(type, QList() << actionType.action()); } if (interface->plugin()->hasPopupMenuSupport()) { type = PimCommon::ActionType::PopupMenu; QList lst = listType.value(type); if (!lst.isEmpty()) { QAction *act = new QAction(this); act->setSeparator(true); lst << act << actionType.action(); listType.insert(type, lst); } else { listType.insert(type, QList() << actionType.action()); } } if (interface->plugin()->hasToolBarSupport()) { type = PimCommon::ActionType::ToolBar; QList lst = listType.value(type); if (!lst.isEmpty()) { QAction *act = new QAction(this); act->setSeparator(true); lst << act << actionType.action(); listType.insert(type, lst); } else { listType.insert(type, QList() << actionType.action()); } } } } return listType; } QVector PluginInterface::pluginsDataList() const { return d->mGenericPluginManager->pluginsDataList(); } QString PluginInterface::configGroupName() const { return d->mGenericPluginManager->configGroupName(); } QString PluginInterface::configPrefixSettingKey() const { return d->mGenericPluginManager->configPrefixSettingKey(); } PimCommon::GenericPlugin *PluginInterface::pluginFromIdentifier(const QString &id) { return d->mGenericPluginManager->pluginFromIdentifier(id); }