KCoreAddons

kdelibs4configmigrator.cpp
1 /*
2  This file is part of the KDE Frameworks
3 
4  SPDX-FileCopyrightText: 2014 Montel Laurent <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #include "kdelibs4configmigrator.h"
10 
11 #include <Kdelibs4Migration>
12 #include <QDir>
13 #include <QFile>
14 #include <QFileInfo>
15 #include <QLoggingCategory>
16 #include <QPluginLoader>
17 #include <QStandardPaths>
18 
19 Q_DECLARE_LOGGING_CATEGORY(MIGRATOR)
20 // logging category for this framework, default: log stuff >= warning
21 Q_LOGGING_CATEGORY(MIGRATOR, "kf.coreaddons.kdelibs4configmigrator", QtWarningMsg)
22 
23 class Kdelibs4ConfigMigratorPrivate
24 {
25 public:
26  Kdelibs4ConfigMigratorPrivate(const QString &_appName)
27  : appName(_appName)
28  {
29  }
30 
31  QStringList configFiles;
32  QStringList uiFiles;
33  const QString appName;
34 };
35 
37  : d(new Kdelibs4ConfigMigratorPrivate(appName))
38 {
39 }
40 
42 
43 void Kdelibs4ConfigMigrator::setConfigFiles(const QStringList &configFileNameList)
44 {
45  d->configFiles = configFileNameList;
46 }
47 
49 {
50  d->uiFiles = uiFileNameList;
51 }
52 
54 {
55  // Testing for kdehome
56  Kdelibs4Migration migration;
57  if (!migration.kdeHomeFound()) {
58  return false;
59  }
60 
61  bool didSomething = false;
62 
63  for (const QString &configFileName : qAsConst(d->configFiles)) {
64  const QString newConfigLocation = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + QLatin1Char('/') + configFileName;
65 
66  if (QFile(newConfigLocation).exists()) {
67  continue;
68  }
69  // Be safe
70  QFileInfo fileInfo(newConfigLocation);
71  QDir().mkpath(fileInfo.absolutePath());
72 
73  const QString oldConfigFile(migration.locateLocal("config", configFileName));
74  if (!oldConfigFile.isEmpty()) {
75  if (QFile(oldConfigFile).copy(newConfigLocation)) {
76  didSomething = true;
77  qCDebug(MIGRATOR) << "config file" << oldConfigFile << "was migrated to" << newConfigLocation;
78  }
79  }
80  }
81 
82  if (d->appName.isEmpty() && !d->uiFiles.isEmpty()) {
83  qCCritical(MIGRATOR) << " We can not migrate ui file. AppName is missing";
84  } else {
85  for (const QString &uiFileName : qAsConst(d->uiFiles)) {
86  const QString newConfigLocation = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/kxmlgui5/") + d->appName
87  + QLatin1Char('/') + uiFileName;
88  if (QFile(newConfigLocation).exists()) {
89  continue;
90  }
91  QFileInfo fileInfo(newConfigLocation);
92  QDir().mkpath(fileInfo.absolutePath());
93 
94  const QString oldConfigFile(migration.locateLocal("data", d->appName + QLatin1Char('/') + uiFileName));
95  if (!oldConfigFile.isEmpty()) {
96  if (QFile(oldConfigFile).copy(newConfigLocation)) {
97  didSomething = true;
98  qCDebug(MIGRATOR) << "ui file" << oldConfigFile << "was migrated to" << newConfigLocation;
99  }
100  }
101  }
102  }
103 
104  // Trigger KSharedConfig::openConfig()->reparseConfiguration() via the framework integration plugin
105  if (didSomething) {
106  QPluginLoader lib(QStringLiteral("kf5/FrameworkIntegrationPlugin"));
107  QObject *rootObj = lib.instance();
108  if (rootObj) {
109  QMetaObject::invokeMethod(rootObj, "reparseConfiguration");
110  }
111  }
112 
113  return true;
114 }
bool kdeHomeFound() const
Returns true if a "kdehome" was found.
QString writableLocation(QStandardPaths::StandardLocation type)
bool migrate()
Migrate the files, if any.
QString locateLocal(const char *type, const QString &filename) const
Finds a local file in a resource.
bool copy(const QString &newName)
void setConfigFiles(const QStringList &configFileNameList)
Set the list of config files that need to be migrated.
QObject * instance()
Kdelibs4ConfigMigrator(const QString &appName)
Constructs a Kdelibs4ConfigMigrator.
bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0, QGenericArgument val1, QGenericArgument val2, QGenericArgument val3, QGenericArgument val4, QGenericArgument val5, QGenericArgument val6, QGenericArgument val7, QGenericArgument val8, QGenericArgument val9)
void setUiFiles(const QStringList &uiFileNameList)
Set the list of ui files to migrate.
QString absolutePath() const const
Kdelibs4Migration provides support for locating config files and application data files saved by kdel...
bool mkpath(const QString &dirPath) const const
~Kdelibs4ConfigMigrator()
Destructor.
QCA_EXPORT QString appName()
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Apr 18 2021 23:02:02 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.