KCoreAddons

kdelibs4migration.cpp
1 /*
2  This file is part of the KDE Frameworks
3 
4  SPDX-FileCopyrightText: 2014 David Faure <[email protected]>
5  SPDX-FileCopyrightText: 2014 Ivan Cukic <ivan.cukic(at)kde.org>
6 
7  SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9 
10 #include "kdelibs4migration.h"
11 #include "config-kde4home.h"
12 #include "kcoreaddons_debug.h"
13 #include <QDir>
14 #include <QVector>
15 
16 #ifdef Q_OS_WIN
17 #include <shlobj.h>
18 #endif
19 
20 class Kdelibs4MigrationPrivate
21 {
22 public:
23  QString m_kdeHome;
24 };
25 
27  : d(new Kdelibs4MigrationPrivate)
28 {
29  if (qEnvironmentVariableIsSet("KDEHOME")) {
30  qCDebug(KCOREADDONS_DEBUG) << "Using KDEHOME as the location of the old config file";
31  d->m_kdeHome = QString::fromLocal8Bit(qgetenv("KDEHOME"));
32  } else {
33  QDir homeDir = QDir::home();
34  QVector<QString> testSubdirs;
35  testSubdirs << QStringLiteral(KDE4_DEFAULT_HOME) << QStringLiteral(".kde4") << QStringLiteral(".kde");
36 #ifdef Q_OS_WIN
37  WCHAR wPath[MAX_PATH + 1];
38  if (SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, wPath) == S_OK) {
39  testSubdirs << QDir::fromNativeSeparators(QString::fromUtf16((const ushort *)wPath)) + QLatin1String("/" KDE4_DEFAULT_HOME);
40  }
41 #endif
42  for (const QString &testSubdir : qAsConst(testSubdirs)) {
43  if (homeDir.exists(testSubdir)) {
44  qCDebug(KCOREADDONS_DEBUG) << "Using" << testSubdir << "as the location of the old config file";
45  d->m_kdeHome = homeDir.filePath(testSubdir);
46  break;
47  }
48  }
49  if (d->m_kdeHome.isEmpty()) {
50  d->m_kdeHome = homeDir.filePath(QStringLiteral(KDE4_DEFAULT_HOME));
51  }
52  }
53 
54  if (!d->m_kdeHome.isEmpty() && !d->m_kdeHome.endsWith(QLatin1Char('/'))) {
55  d->m_kdeHome.append(QLatin1Char('/'));
56  }
57 }
58 
60 
62 {
63  return !d->m_kdeHome.isEmpty() && QDir(d->m_kdeHome).exists();
64 }
65 
67 {
68  return d->m_kdeHome;
69 }
70 
71 QString Kdelibs4Migration::locateLocal(const char *type, const QString &filename) const
72 {
73  if (d->m_kdeHome.isEmpty()) {
74  return QString();
75  }
76  const QString dir = saveLocation(type);
77  if (dir.isEmpty()) {
78  return QString();
79  }
80  const QString file = dir + filename;
81  if (QFile::exists(file)) {
82  return file;
83  }
84  return QString();
85 }
86 
87 static const struct {
88  const char *type;
89  const char *subdir;
90 } s_subdirs[] = {{"config", "share/config/"},
91  {"data", "share/apps/"},
92  {"services", "share/kde4/services"},
93  {"servicetypes", "share/kde4/servicetypes"},
94  {"wallpaper", "share/wallpapers"},
95  {"emoticons", "share/emoticons"},
96  {"templates", "share/templates"}};
97 
98 QString Kdelibs4Migration::saveLocation(const char *type, const QString &suffix) const
99 {
100  if (d->m_kdeHome.isEmpty()) {
101  return QString();
102  }
103  static const int numResources = sizeof(s_subdirs) / sizeof(*s_subdirs);
104  for (uint i = 0; i < numResources; ++i) {
105  if (qstrcmp(s_subdirs[i].type, type) == 0) {
106  QString dir = d->m_kdeHome + QString::fromLatin1(s_subdirs[i].subdir) + suffix;
107  if (!dir.endsWith(QLatin1Char('/'))) {
108  dir += QLatin1Char('/');
109  }
110  return dir;
111  }
112  }
113  qCWarning(KCOREADDONS_DEBUG) << "No such resource" << type;
114  return QString();
115 }
bool kdeHomeFound() const
Returns true if a "kdehome" was found.
~Kdelibs4Migration()
Destructor.
QString fromNativeSeparators(const QString &pathName)
QString locateLocal(const char *type, const QString &filename) const
Finds a local file in a resource.
QString filePath(const QString &fileName) const const
Kdelibs4Migration()
Constructs a Kdelibs4Migration instance.
bool exists() const const
QString fromLocal8Bit(const char *str, int size)
bool exists() const const
QString fromUtf16(const ushort *unicode, int size)
bool isEmpty() const const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const const
QString kdeHome() const
Returns the kdehome that was found.
QString fromLatin1(const char *str, int size)
QDir home()
QString saveLocation(const char *type, const QString &suffix=QString()) const
Finds a location to save files into for the given type in the user&#39;s home directory.
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.