Skip to content

Commit ef3f9fe

Browse files
committed
refactor: modernize codebase with Qt 6.8 APIs and coding conventions
- Use Qt 6.8 string literals (u""_s) and QStringView for zero-copy operations - Change constants to constexpr static auto& references - Introduce fromStaticRaw() helper for static string handling - Replace QScopedPointer with std::unique_ptr - Add Q_LOGGING_CATEGORY for structured logging - Use qTokenize for efficient string splitting - Format code to match project conventions Signed-off-by: ComixHe <heyuming@deepin.org>
1 parent 3cbad3e commit ef3f9fe

26 files changed

Lines changed: 486 additions & 375 deletions

apps/app-identifier/src/main.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@ int main(int argc, char *argv[])
4848
return;
4949
}
5050

51-
auto msg = QDBusMessage::createMethodCall(
52-
DDEApplicationManager1ServiceName, DDEApplicationManager1ObjectPath, ApplicationManager1Interface, "Identify");
51+
using namespace Qt::StringLiterals;
52+
auto msg = QDBusMessage::createMethodCall(fromStaticRaw(DDEApplicationManager1ServiceName),
53+
fromStaticRaw(DDEApplicationManager1ObjectPath),
54+
fromStaticRaw(ApplicationManager1Interface),
55+
u"Identify"_s);
5356
msg.setArguments({QVariant::fromValue(QDBusUnixFileDescriptor{pidfd})});
5457
// see QDBusUnixFileDescriptor: The original file descriptor is not touched and must be closed by the user.
5558
close(pidfd);

apps/dde-am/src/commandexecutor.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,13 @@ void CommandExecutor::setEnvironmentVariables(const QStringList &envVars)
7878

7979
DExpected<void> CommandExecutor::execute()
8080
{
81+
using namespace Qt::StringLiterals;
8182
registerComplexDbusType();
8283
auto con = QDBusConnection::sessionBus();
83-
auto msg = QDBusMessage::createMethodCall(
84-
DDEApplicationManager1ServiceName, DDEApplicationManager1ObjectPath, ApplicationManager1Interface, "executeCommand");
84+
auto msg = QDBusMessage::createMethodCall(fromStaticRaw(DDEApplicationManager1ServiceName),
85+
fromStaticRaw(DDEApplicationManager1ObjectPath),
86+
fromStaticRaw(ApplicationManager1Interface),
87+
u"executeCommand"_s);
8588

8689
QStringMap envMap;
8790
for (const auto &env : std::as_const(m_environmentVariables)) {

apps/dde-am/src/launcher.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ ObjectMap getManagedObjects()
4040
{
4141
registerComplexDbusType();
4242

43-
auto msg = QDBusMessage::createMethodCall(
44-
DDEApplicationManager1ServiceName, DDEApplicationManager1ObjectPath, ObjectManagerInterface, "GetManagedObjects");
43+
using namespace Qt::StringLiterals;
44+
auto msg = QDBusMessage::createMethodCall(fromStaticRaw(DDEApplicationManager1ServiceName),
45+
fromStaticRaw(DDEApplicationManager1ObjectPath),
46+
fromStaticRaw(ObjectManagerInterface),
47+
u"GetManagedObjects"_s);
4548

4649
auto reply = QDBusConnection::sessionBus().call(msg);
4750

@@ -114,18 +117,20 @@ DExpected<void> Launcher::run()
114117

115118
DExpected<void> Launcher::launch() noexcept
116119
{
120+
using namespace Qt::StringLiterals;
117121
// Build options map
118122
QVariantMap options;
119123
if (!m_environmentVariables.isEmpty()) {
120-
options.insert("env", m_environmentVariables);
124+
options.insert(u"env"_s, m_environmentVariables);
121125
}
122126

123127
// Mark autostart launches so AM can suppress splash
124128
if (m_autostart) {
125-
options.insert("_autostart", true);
129+
options.insert(u"_autostart"_s, true);
126130
}
127131

128-
auto msg = QDBusMessage::createMethodCall(DDEApplicationManager1ServiceName, m_path, ApplicationInterface, "Launch");
132+
auto msg = QDBusMessage::createMethodCall(
133+
fromStaticRaw(DDEApplicationManager1ServiceName), m_path, fromStaticRaw(ApplicationInterface), u"Launch"_s);
129134
msg.setArguments({m_action, QStringList{}, options});
130135
auto reply = QDBusConnection::sessionBus().call(msg);
131136

@@ -140,10 +145,8 @@ DExpected<void> Launcher::launch() noexcept
140145

141146
DExpected<void> Launcher::updateLaunchedTimes() noexcept
142147
{
143-
std::unique_ptr<DConfig> config(DConfig::create(
144-
// use QString::fromRawData(const char16_t*, size) if Qt version >= 6.10
145-
QString::fromRawData(reinterpret_cast<const QChar *>(ApplicationServiceID), std::size(ApplicationServiceID) - 1),
146-
ApplicationManagerToolsConfig));
148+
std::unique_ptr<DConfig> config(
149+
DConfig::create(fromStaticRaw(ApplicationServiceID), fromStaticRaw(ApplicationManagerToolsConfig)));
147150
if (!config->isValid()) {
148151
return DUnexpected{emplace_tag::USE_EMPLACE, -1, "DConfig is invalid when updating launched times."};
149152
}
@@ -173,9 +176,11 @@ QString Launcher::appId() const noexcept
173176
return {};
174177
}
175178

176-
const auto startIndex = QString(DDEApplicationManager1ObjectPath).size();
177-
auto endIndex = m_path.indexOf("/", startIndex + 1);
178-
const auto id = endIndex <= -1 ? m_path.mid(startIndex + 1) : m_path.sliced(startIndex + 1, endIndex - (startIndex + 1));
179+
const QStringView pathView{m_path};
180+
const qsizetype startIndex = std::size(DDEApplicationManager1ObjectPath) - 1;
181+
const auto endIndex = pathView.indexOf(u'/', startIndex + 1);
182+
const auto id =
183+
endIndex <= -1 ? pathView.sliced(startIndex + 1) : pathView.sliced(startIndex + 1, endIndex - (startIndex + 1));
179184
return unescapeFromObjectPath(id);
180185
}
181186

apps/dde-am/src/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ int handleLaunchApp(const QCommandLineParser &parser,
144144
QString appPath;
145145
if (!appId.isEmpty()) {
146146
// 解析出 appId,说明输入是 appId 或 desktop 文件
147-
appPath = QString("%1/%2").arg(DDEApplicationManager1ObjectPath, escapeToObjectPath(appId));
148-
} else if (!inputArg.isEmpty() && !inputArg.startsWith("/")) {
147+
appPath = fromStaticRaw(DDEApplicationManager1ObjectPath) % u'/' % escapeToObjectPath(appId);
148+
} else if (!inputArg.isEmpty() && !inputArg.startsWith(u'/')) {
149149
// 既不是绝对路径,也不是 desktop 文件等,视为 appId
150-
appPath = QString("%1/%2").arg(DDEApplicationManager1ObjectPath, escapeToObjectPath(inputArg));
150+
appPath = fromStaticRaw(DDEApplicationManager1ObjectPath) % escapeToObjectPath(inputArg);
151151
} else {
152152
// 绝对路径或特殊用法
153153
qWarning() << "unknown input: " << inputArg << ", just use it as path.";

apps/dde-application-manager/src/main.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,23 @@ void registerComplexDbusType()
3434

3535
int main(int argc, char *argv[])
3636
{
37-
setenv("DSG_APP_ID", ApplicationManagerConfig, 0);
37+
using namespace Qt::StringLiterals;
38+
setenv("DSG_APP_ID", fromStaticRaw(ApplicationManagerConfig).toUtf8().constData(), 0);
3839
#ifdef PROFILING_MODE
3940
auto start = std::chrono::high_resolution_clock::now();
4041
#endif
41-
QGuiApplication app{argc, argv};
42+
const QGuiApplication app{argc, argv};
4243

4344
auto &bus = ApplicationManager1DBus::instance();
4445
bus.initGlobalServerBus(DBusType::Session);
4546
bus.setDestBus();
46-
auto &AMBus = bus.globalServerBus();
4747

4848
registerComplexDbusType();
49-
auto storageDir = getXDGDataHome() + QDir::separator() + "deepin" + QDir::separator() + "ApplicationManager";
49+
auto storageDir = getXDGDataHome() % u"/deepin/ApplicationManager"_s;
5050
auto storage = ApplicationManager1Storage::createApplicationManager1Storage(storageDir);
5151

5252
ApplicationManager1Service AMService{std::make_unique<CGroupsIdentifier>(), storage};
53-
AMService.initService(AMBus);
53+
AMService.initService(bus.globalServerBus());
5454

5555
#ifdef PROFILING_MODE
5656
auto end = std::chrono::high_resolution_clock::now();

src/applicationchecker.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ bool hasDesktopIntersection(QStringView rawValue, const QStringList &currentDesk
4545
bool ApplicationFilter::hiddenCheck(const DesktopEntry &entry) noexcept
4646
{
4747
bool hidden{false};
48-
auto hiddenVal = entry.value(DesktopFileEntryKey, DesktopEntryHidden);
48+
auto hiddenVal = entry.value(fromStaticRaw(DesktopFileEntryKey), fromStaticRaw(DesktopEntryHidden));
4949

5050
if (hiddenVal.has_value()) {
5151
bool ok{false};
@@ -60,7 +60,7 @@ bool ApplicationFilter::hiddenCheck(const DesktopEntry &entry) noexcept
6060

6161
bool ApplicationFilter::tryExecCheck(const DesktopEntry &entry) noexcept
6262
{
63-
auto tryExecVal = entry.value(DesktopFileEntryKey, u"TryExec"_s);
63+
auto tryExecVal = entry.value(fromStaticRaw(DesktopFileEntryKey), u"TryExec"_s);
6464
if (tryExecVal.has_value()) {
6565
auto executable = toString(tryExecVal.value());
6666
if (executable.isEmpty()) {
@@ -86,12 +86,12 @@ bool ApplicationFilter::showInCheck(const DesktopEntry &entry) noexcept
8686
}
8787

8888
bool showInCurrentDE{true};
89-
if (auto val = entry.value(DesktopFileEntryKey, u"OnlyShowIn"_s); val.has_value()) {
89+
if (auto val = entry.value(fromStaticRaw(DesktopFileEntryKey), u"OnlyShowIn"_s); val.has_value()) {
9090
showInCurrentDE = hasDesktopIntersection(toString(val.value()), desktops);
9191
}
9292

9393
bool notShowInCurrentDE{false};
94-
if (auto val = entry.value(DesktopFileEntryKey, u"NotShowIn"_s); val.has_value()) {
94+
if (auto val = entry.value(fromStaticRaw(DesktopFileEntryKey), u"NotShowIn"_s); val.has_value()) {
9595
notShowInCurrentDE = hasDesktopIntersection(toString(val.value()), desktops);
9696
}
9797

src/applicationmanagerstorage.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@
1212
std::shared_ptr<ApplicationManager1Storage>
1313
ApplicationManager1Storage::createApplicationManager1Storage(const QString &storageDir) noexcept
1414
{
15-
QDir dir;
16-
auto dirPath = QDir::cleanPath(storageDir);
17-
if (!dir.mkpath(dirPath)) {
15+
const QDir dir{QDir::cleanPath(storageDir)};
16+
if (!dir.mkpath(".")) {
1817
qCritical() << "can't create directory";
1918
return nullptr;
2019
}
2120

22-
dir.setPath(dirPath);
2321
auto storagePath = dir.filePath("storage.json");
2422
auto obj = std::shared_ptr<ApplicationManager1Storage>(new (std::nothrow) ApplicationManager1Storage{storagePath});
2523

src/applicationmanagerstorage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <QJsonObject>
1010
#include <QFile>
1111

12-
enum class ModifyMode { Create, Update };
12+
enum class ModifyMode : uint8_t { Create, Update };
1313

1414
class ApplicationManager1Storage
1515
{

src/compatibilitymanager.cpp

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
#include "compatibilitymanager.h"
66
#include "constant.h"
7+
#include "global.h"
78
#include <QJsonDocument>
89
#include <QJsonObject>
910
#include <QJsonArray>
1011

11-
std::optional<CompatibilityDesktopEntry::Entry> CompatibilityDesktopEntry::group(const QString &key) const noexcept
12+
std::optional<CompatibilityDesktopEntry::Entry> CompatibilityDesktopEntry::group(const QString &key) const noexcept
1213
{
1314
auto iter = m_entrys.find(key);
14-
if(iter == m_entrys.end()){
15+
if (iter == m_entrys.end()) {
1516
return std::nullopt;
1617
}
1718

@@ -21,15 +22,15 @@ std::optional<CompatibilityDesktopEntry::Entry> CompatibilityDesktopEntry::group
2122
std::any CompatibilityDesktopEntry::value(const QString &key, const CompatibilityDesktopEntry::ValueKey &valueKey) const noexcept
2223
{
2324
auto iter = m_entrys.find(key);
24-
if(iter == m_entrys.end()){
25+
if (iter == m_entrys.end()) {
2526
return std::nullopt;
2627
}
2728

28-
if(valueKey == CompatibilityDesktopEntry::Exec){
29+
if (valueKey == CompatibilityDesktopEntry::Exec) {
2930
return get<0>(*iter);
3031
}
3132

32-
if(valueKey == CompatibilityDesktopEntry::Env){
33+
if (valueKey == CompatibilityDesktopEntry::Env) {
3334
return get<1>(*iter);
3435
}
3536

@@ -39,36 +40,36 @@ std::any CompatibilityDesktopEntry::value(const QString &key, const Compatibilit
3940
void CompatibilityDesktopEntry::insert(const QString &groupKey, const Entry &entry) noexcept
4041
{
4142
auto iter = m_entrys.find(groupKey);
42-
if(iter == m_entrys.end()){
43-
m_entrys.insert(groupKey,entry);
44-
}else{
45-
qWarning()<<"insert : "<<groupKey<<"fail";
43+
if (iter == m_entrys.end()) {
44+
m_entrys.insert(groupKey, entry);
45+
} else {
46+
qWarning() << "insert : " << groupKey << "fail";
4647
}
4748
}
4849

4950
CompatibilityManager::CompatibilityManager()
5051
{
5152
connect(&m_watcher, &QFileSystemWatcher::fileChanged, this, &CompatibilityManager::loadCompatibilityConfig);
5253

53-
if(!m_watcher.addPath(CompatibilityConfigFilePath)){
54-
qWarning() << "add path : "<< CompatibilityConfigFilePath << "to watch fail";
54+
if (!m_watcher.addPath(fromStaticRaw(CompatibilityConfigFilePath))) {
55+
qWarning() << "add path : " << QStringView{CompatibilityConfigFilePath} << "to watch fail";
5556
}
5657
loadCompatibilityConfig();
5758
}
5859

5960
std::optional<QString> CompatibilityManager::getExec(const QString &desktopId, const QString &groupId)
6061
{
6162
auto iter = m_compatibilityConfig.find(desktopId);
62-
if(iter == m_compatibilityConfig.end()){
63+
if (iter == m_compatibilityConfig.end()) {
6364
return std::nullopt;
6465
}
6566

6667
auto value = iter->value(groupId, CompatibilityDesktopEntry::Exec);
67-
if(!value.has_value()){
68+
if (!value.has_value()) {
6869
return std::nullopt;
6970
}
7071

71-
if (value.type() != typeid(QString)){
72+
if (value.type() != typeid(QString)) {
7273
return std::nullopt;
7374
}
7475

@@ -78,34 +79,36 @@ std::optional<QString> CompatibilityManager::getExec(const QString &desktopId, c
7879
QStringList CompatibilityManager::getEnv(const QString &desktopId, const QString &groupId)
7980
{
8081
auto iter = m_compatibilityConfig.find(desktopId);
81-
if(iter == m_compatibilityConfig.end()){
82+
if (iter == m_compatibilityConfig.end()) {
8283
return QStringList();
8384
}
8485

8586
auto value = iter->value(groupId, CompatibilityDesktopEntry::Env);
86-
if(!value.has_value()){
87+
if (!value.has_value()) {
8788
return QStringList();
8889
}
8990

90-
if (value.type() != typeid(QStringList)){
91+
if (value.type() != typeid(QStringList)) {
9192
return QStringList();
9293
}
9394

9495
return std::any_cast<QStringList>(value);
9596
}
9697

97-
void CompatibilityManager::loadCompatibilityConfig(){
98+
void CompatibilityManager::loadCompatibilityConfig()
99+
{
98100
qInfo() << "loadCompatibilityConfig";
99101
m_compatibilityConfig.clear();
100-
QFile file{CompatibilityConfigFilePath};
102+
QFile file{fromStaticRaw(CompatibilityConfigFilePath)};
101103

102104
auto err = parse(file);
103105
if (err != ParserError::NoError) {
104106
qWarning() << "parse file :" << file.fileName() << ", err";
105107
}
106108
}
107109

108-
ParserError CompatibilityManager::parse(QFile &file) noexcept{
110+
ParserError CompatibilityManager::parse(QFile &file) noexcept
111+
{
109112
if (!file.open(QFile::Text | QFile::ReadOnly | QFile::ExistingOnly)) {
110113
qWarning() << "open file:" << file.fileName() << "failed:" << file.errorString() << ", skip.";
111114
return ParserError::NotFound;
@@ -119,26 +122,26 @@ ParserError CompatibilityManager::parse(QFile &file) noexcept{
119122
return ParserError::MissingInfo;
120123
}
121124
if (json.isEmpty()) {
122-
qWarning() << "file : "<<file.fileName()<<" empty";
125+
qWarning() << "file : " << file.fileName() << " empty";
123126
return ParserError::MissingInfo;
124127
}
125128

126129
auto obj = json.object();
127130
for (auto it = obj.constBegin(); it != obj.constEnd(); ++it) {
128131
const auto &desktopId = it.key();
129132
auto existDesktopId = m_compatibilityConfig.constFind(desktopId);
130-
if(existDesktopId != m_compatibilityConfig.cend()){
131-
qWarning() << "the desktop id : "<< it.key() <<"exist skip";
133+
if (existDesktopId != m_compatibilityConfig.cend()) {
134+
qWarning() << "the desktop id : " << it.key() << "exist skip";
132135
continue;
133136
}
134137
auto desktopEntry = obj[desktopId].toObject();
135138

136139
// 处理 Desktop Entry
137140
CompatibilityDesktopEntry compatibilityEntry;
138-
const auto& keys = desktopEntry.keys();
139-
for(const auto &entryId : std::as_const(keys)){
140-
if(!entryId.startsWith(DesktopFileEntryKey) && !entryId.startsWith(DesktopFileActionKey)){
141-
qWarning() << "parse entry : "<<entryId<<" fail";
141+
const auto &keys = desktopEntry.keys();
142+
for (const auto &entryId : std::as_const(keys)) {
143+
if (!entryId.startsWith(DesktopFileEntryKey) && !entryId.startsWith(DesktopFileActionKey)) {
144+
qWarning() << "parse entry : " << entryId << " fail";
142145
return ParserError::MissingInfo;
143146
}
144147

@@ -151,13 +154,13 @@ ParserError CompatibilityManager::parse(QFile &file) noexcept{
151154
env.push_back(envJson.toString());
152155
}
153156

154-
//auto tub = std::make_tuple(exec,std::move(env.join("; ") + ";"));
155-
auto tub = std::make_tuple(exec,env);
156-
compatibilityEntry.insert(entryId,tub);
157-
qInfo()<<"insert desktop id : " << entryId <<", with Exec : "<< exec << "and Env : "<<env;
157+
// auto tub = std::make_tuple(exec,std::move(env.join("; ") + ";"));
158+
auto tub = std::make_tuple(exec, env);
159+
compatibilityEntry.insert(entryId, tub);
160+
qInfo() << "insert desktop id : " << entryId << ", with Exec : " << exec << "and Env : " << env;
158161
}
159162

160-
m_compatibilityConfig.insert(desktopId,compatibilityEntry);
163+
m_compatibilityConfig.insert(desktopId, compatibilityEntry);
161164
}
162165

163166
return ParserError::NoError;

0 commit comments

Comments
 (0)