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 <QStandardPaths>
12 #include <Kdelibs4Migration>
13 #include <QFile>
14 #include <QFileInfo>
15 #include <QDir>
16 #include <QLoggingCategory>
17 #include <QPluginLoader>
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 Q_DECL_HIDDEN Kdelibs4ConfigMigrator::Private
24 {
25 public:
26  Private(const QString &_appName)
27  : appName(_appName)
28  {
29 
30  }
31 
32  QStringList configFiles;
33  QStringList uiFiles;
34  const QString appName;
35 };
36 
38  : d(new Private(appName))
39 {
40 }
41 
43 {
44  delete d;
45 }
46 
47 void Kdelibs4ConfigMigrator::setConfigFiles(const QStringList &configFileNameList)
48 {
49  d->configFiles = configFileNameList;
50 }
51 
53 {
54  d->uiFiles = uiFileNameList;
55 }
56 
58 {
59  // Testing for kdehome
60  Kdelibs4Migration migration;
61  if (!migration.kdeHomeFound()) {
62  return false;
63  }
64 
65  bool didSomething = false;
66 
67  for (const QString &configFileName : qAsConst(d->configFiles)) {
68  const QString newConfigLocation
70  + QLatin1Char('/') + configFileName;
71 
72  if (QFile(newConfigLocation).exists()) {
73  continue;
74  }
75  //Be safe
76  QFileInfo fileInfo(newConfigLocation);
77  QDir().mkpath(fileInfo.absolutePath());
78 
79  const QString oldConfigFile(migration.locateLocal("config", configFileName));
80  if (!oldConfigFile.isEmpty()) {
81  if (QFile(oldConfigFile).copy(newConfigLocation)) {
82  didSomething = true;
83  qCDebug(MIGRATOR) << "config file" << oldConfigFile << "was migrated to" << newConfigLocation;
84  }
85  }
86  }
87 
88  if (d->appName.isEmpty() && !d->uiFiles.isEmpty()) {
89  qCCritical(MIGRATOR) << " We can not migrate ui file. AppName is missing";
90  } else {
91  for (const QString &uiFileName : qAsConst(d->uiFiles)) {
92  const QString newConfigLocation
94  QLatin1String("/kxmlgui5/") + d->appName + QLatin1Char('/') + uiFileName;
95  if (QFile(newConfigLocation).exists()) {
96  continue;
97  }
98  QFileInfo fileInfo(newConfigLocation);
99  QDir().mkpath(fileInfo.absolutePath());
100 
101  const QString oldConfigFile(migration.locateLocal("data", d->appName + QLatin1Char('/') + uiFileName));
102  if (!oldConfigFile.isEmpty()) {
103  if (QFile(oldConfigFile).copy(newConfigLocation)) {
104  didSomething = true;
105  qCDebug(MIGRATOR) << "ui file" << oldConfigFile << "was migrated to" << newConfigLocation;
106  }
107  }
108  }
109  }
110 
111  // Trigger KSharedConfig::openConfig()->reparseConfiguration() via the framework integration plugin
112  if (didSomething) {
113  QPluginLoader lib(QStringLiteral("kf5/FrameworkIntegrationPlugin"));
114  QObject *rootObj = lib.instance();
115  if (rootObj) {
116  QMetaObject::invokeMethod(rootObj, "reparseConfiguration");
117  }
118  }
119 
120  return true;
121 }
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
QCA_EXPORT QString appName()
Kdelibs4ConfigMigrator migrates selected config files and ui files from the kdelibs 4...
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Sep 20 2020 23:01:43 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.