Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/dconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@

#include <QLoggingCategory>
#include <QCoreApplication>
#include <QThread>

Check warning on line 20 in src/dconfig.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QThread> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#ifdef Q_OS_LINUX
#include <unistd.h>

Check warning on line 22 in src/dconfig.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <unistd.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#elif defined(Q_OS_WIN)
#include <windows.h>

Check warning on line 24 in src/dconfig.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <windows.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <lmcons.h>

Check warning on line 25 in src/dconfig.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <lmcons.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QCryptographicHash>

Check warning on line 26 in src/dconfig.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QCryptographicHash> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#endif

// https://gitlabwh.uniontech.com/wuhan/se/deepin-specifications/-/issues/3
Expand All @@ -33,6 +37,24 @@
#endif
static QString NoAppId;

#if defined(Q_OS_WIN)
// Generate a unique user ID from Windows username (similar to Unix UID)
static uint getWindowsUserId()
{
WCHAR username[UNLEN + 1];
DWORD size = UNLEN + 1;
if (GetUserNameW(username, &size)) {
QByteArray nameData = QString::fromWCharArray(username).toUtf8();
QByteArray hash = QCryptographicHash::hash(nameData, QCryptographicHash::Md5);
// Use first 4 bytes as uint
uint uid = 0;
memcpy(&uid, hash.constData(), sizeof(uint));
return uid;
}
return 0;
}
#endif

/*!
@~english
@class Dtk::Core::DConfigBackend
Expand Down Expand Up @@ -177,7 +199,11 @@
return true;

configFile.reset(new DConfigFile(owner->appId,owner->name, owner->subpath));
#ifdef Q_OS_LINUX
configCache.reset(configFile->createUserCache(getuid()));
#else
configCache.reset(configFile->createUserCache(getWindowsUserId()));
#endif
const QString &prefix = localPrefix();

if (!configFile->load(prefix) || !configCache->load(prefix))
Expand All @@ -190,7 +216,11 @@
std::unique_ptr<DConfigFile> file(new DConfigFile(NoAppId, owner->name, owner->subpath));
const bool canFallbackToGeneric = !file->meta()->metaPath(prefix).isEmpty();
if (canFallbackToGeneric) {
#ifdef Q_OS_LINUX
std::unique_ptr<DConfigCache> cache(file->createUserCache(getuid()));
#else
std::unique_ptr<DConfigCache> cache(file->createUserCache(getWindowsUserId()));
#endif
if (file->load(prefix) && cache->load(prefix)) {
genericConfigFile.reset(file.release());
genericConfigCache.reset(cache.release());
Expand Down
39 changes: 37 additions & 2 deletions src/dconfigfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@
#include <QDir>
#include <QDirIterator>
#include <QCollator>
#include <QDateTime>

Check warning on line 22 in src/dconfigfile.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDateTime> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QRegularExpression>

Check warning on line 23 in src/dconfigfile.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QRegularExpression> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#ifdef Q_OS_LINUX
#include <unistd.h>

Check warning on line 26 in src/dconfigfile.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <unistd.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <pwd.h>

Check warning on line 27 in src/dconfigfile.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <pwd.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#elif defined(Q_OS_WIN)
#include <windows.h>

Check warning on line 29 in src/dconfigfile.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <windows.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <lmcons.h>
#endif

// https://gitlabwh.uniontech.com/wuhan/se/deepin-specifications/-/issues/3

Expand Down Expand Up @@ -238,8 +243,21 @@
}

inline static QString getUserName(const uint uid) {
#ifdef Q_OS_LINUX
passwd *pw = getpwuid(uid);
return pw ? QString::fromLocal8Bit(pw->pw_name) : QString();
#elif defined(Q_OS_WIN)
Q_UNUSED(uid)
WCHAR username[UNLEN + 1];
DWORD size = UNLEN + 1;
if (GetUserNameW(username, &size)) {
return QString::fromWCharArray(username);
}
return QString();
#else
Q_UNUSED(uid)
return QString();
#endif
}

/*!
Expand Down Expand Up @@ -304,7 +322,11 @@
QStringList paths;
// lower priority is higher.
for (auto item: DStandardPaths::paths(DStandardPaths::DSG::DataDir)) {
paths.prepend(QDir::cleanPath(QString("%1/%2/configs").arg(localPrefix, item)));
if (localPrefix.isEmpty()) {
Comment thread
BLumia marked this conversation as resolved.
paths.prepend(QDir::cleanPath(QString("%1/configs").arg(item)));
} else {
paths.prepend(QDir::cleanPath(QString("%1/%2/configs").arg(localPrefix, item)));
}
}
return paths;
}
Expand Down Expand Up @@ -1112,19 +1134,28 @@
if (prefix.isEmpty()) {
// If target user is current user or system user, then get the home path by environment variable first.
QString homePath;
#ifdef Q_OS_LINUX
if (userid == InvalidUID || (getuid() == userid)) {
homePath = DStandardPaths::homePath();
} else {
homePath = DStandardPaths::homePath(getuid());
}
#else
Q_UNUSED(userid)
homePath = DStandardPaths::homePath();
#endif

if (homePath.isEmpty())
return QString();

// fallback to default application cache directory.
prefix = homePath + QStringLiteral("/.config/dsg/configs");
}
return QDir::cleanPath(QString("%1/%2/%3").arg(localPrefix, prefix + suffix, configKey.appId));
if (localPrefix.isEmpty()) {
return QDir::cleanPath(QString("%1/%2").arg(prefix + suffix, configKey.appId));
} else {
return QDir::cleanPath(QString("%1/%2/%3").arg(localPrefix, prefix + suffix, configKey.appId));
}
}

inline QString applicationCacheDir(const QString &localPrefix) const
Expand Down Expand Up @@ -1286,8 +1317,12 @@
cacheChanged = false;
const QString &dir = getCacheDir(localPrefix);
if (dir.isEmpty()) {
#ifdef Q_OS_LINUX
qCWarning(cfLog, "Falied on saveing, the config cache directory is empty for the user[%d], "
"the current user[%d].", userid, getuid());
#else
qCWarning(cfLog, "Falied on saveing, the config cache directory is empty for the user[%d].", userid);
#endif
return false;
}
QString path = cacheDir(dir);
Expand Down
13 changes: 8 additions & 5 deletions src/filesystem/dstandardpaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,7 @@ QString DStandardPaths::path(DStandardPaths::XDG type)
const QByteArray &path = qgetenv("XDG_STATE_HOME");
if (!path.isEmpty())
return QString::fromLocal8Bit(path);
#ifdef Q_OS_LINUX
return homePath() + QStringLiteral("/.local/state");
#else
// TODO: handle it on mac
return QString();
#endif
}
}
return QString();
Expand All @@ -187,9 +182,17 @@ QStringList DStandardPaths::paths(DSG type)
if (type == DSG::DataDir) {
const QByteArray &path = qgetenv("DSG_DATA_DIRS");
if (path.isEmpty()) {
#ifdef Q_OS_LINUX
return {QLatin1String(PREFIX"/share/dsg")};
#else
return {QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/dsg")};
#endif
}
#ifdef Q_OS_WIN
const auto list = path.split(';');
#else
const auto list = path.split(':');
#endif
paths.reserve(list.size());
for (const auto &i : list)
paths.push_back(QString::fromLocal8Bit(i));
Expand Down
21 changes: 11 additions & 10 deletions src/glob.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ set(OUTER_HEADER
${CMAKE_CURRENT_LIST_DIR}/../include/global/ddesktopentry.h
)

if(DEFINED D_DSG_APP_DATA_FALLBACK)
add_definitions(-DD_DSG_APP_DATA_FALLBACK="${D_DSG_APP_DATA_FALLBACK}")
endif()
list(APPEND OUTER_SOURCE
${CMAKE_CURRENT_LIST_DIR}/dconfigfile.cpp
)
list(APPEND OUTER_HEADER
${CMAKE_CURRENT_LIST_DIR}/../include/global/dconfigfile.h
)

if(LINUX)
if(DEFINED D_DSG_APP_DATA_FALLBACK)
add_definitions(-DD_DSG_APP_DATA_FALLBACK="${D_DSG_APP_DATA_FALLBACK}")
endif()
list(APPEND OUTER_SOURCE
${CMAKE_CURRENT_LIST_DIR}/dconfigfile.cpp
)
list(APPEND OUTER_HEADER
${CMAKE_CURRENT_LIST_DIR}/../include/global/dconfigfile.h
)
# generic dbus interfaces
if(NOT DEFINED DTK_DISABLE_DBUS_CONFIG)
include(${CMAKE_CURRENT_LIST_DIR}/dbus/dbus.cmake)
Expand All @@ -39,7 +40,7 @@ if(LINUX)
add_definitions(-DD_DISABLE_DBUS_CONFIG)
endif()
else()
add_definitions(-DD_DISABLE_DCONFIG)
add_definitions(-DD_DISABLE_DBUS_CONFIG)
endif()

list(APPEND glob_SRC
Expand Down
Loading