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 <QDir>
13 #include "kcoreaddons_debug.h"
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)) +
40  QLatin1String("/" KDE4_DEFAULT_HOME);
41  }
42 #endif
43  for (const QString &testSubdir : qAsConst(testSubdirs)) {
44  if (homeDir.exists(testSubdir)) {
45  //qCDebug(KCOREADDONS_DEBUG) << "Using" << testSubdir << "as the location of the old config file";
46  d->m_kdeHome = homeDir.filePath(testSubdir);
47  break;
48  }
49  }
50  if (d->m_kdeHome.isEmpty()) {
51  d->m_kdeHome = homeDir.filePath(QStringLiteral(KDE4_DEFAULT_HOME));
52  }
53  }
54 
55  if (!d->m_kdeHome.isEmpty() && !d->m_kdeHome.endsWith(QLatin1Char('/'))) {
56  d->m_kdeHome.append(QLatin1Char('/'));
57  }
58 }
59 
61 {
62  delete d;
63 }
64 
66 {
67  return !d->m_kdeHome.isEmpty() && QDir(d->m_kdeHome).exists();
68 }
69 
71 {
72  return d->m_kdeHome;
73 }
74 
75 QString Kdelibs4Migration::locateLocal(const char *type, const QString &filename) const
76 {
77  if (d->m_kdeHome.isEmpty()) {
78  return QString();
79  }
80  const QString dir = saveLocation(type);
81  if (dir.isEmpty()) {
82  return QString();
83  }
84  const QString file = dir + filename;
85  if (QFile::exists(file)) {
86  return file;
87  }
88  return QString();
89 }
90 
91 static const struct {
92  const char *type;
93  const char *subdir;
94 } s_subdirs[] = {
95  { "config", "share/config/" },
96  { "data", "share/apps/" },
97  { "services", "share/kde4/services" },
98  { "servicetypes", "share/kde4/servicetypes" },
99  { "wallpaper", "share/wallpapers" },
100  { "emoticons", "share/emoticons" },
101  { "templates", "share/templates" }
102 };
103 
104 QString Kdelibs4Migration::saveLocation(const char *type, const QString &suffix) const
105 {
106  if (d->m_kdeHome.isEmpty()) {
107  return QString();
108  }
109  static const int numResources = sizeof(s_subdirs) / sizeof(*s_subdirs);
110  for (uint i = 0; i < numResources; ++i) {
111  if (qstrcmp(s_subdirs[i].type, type) == 0) {
112  QString dir = d->m_kdeHome + QString::fromLatin1(s_subdirs[i].subdir) + suffix;
113  if (!dir.endsWith(QLatin1Char('/'))) {
114  dir += QLatin1Char('/');
115  }
116  return dir;
117  }
118  }
119  qCWarning(KCOREADDONS_DEBUG) << "No such resource" << type;
120  return QString();
121 }
122 
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-2020 The KDE developers.
Generated on Wed Aug 12 2020 23:01:55 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.