refact: suppress warnings and standardize project

Signed-off-by: ComixHe <heyuming@deepin.org>
This commit is contained in:
ComixHe
2024-02-18 13:11:57 +08:00
committed by Comix
parent aca0531e85
commit 9d2cee79fe
14 changed files with 71 additions and 46 deletions

View File

@ -7,12 +7,17 @@ project(dde-application-manager
) )
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON) set(CMAKE_AUTORCC ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(THREADS_PREFER_PTHREAD_FLAG ON) set(THREADS_PREFER_PTHREAD_FLAG ON)
add_definitions(-Wall -Wextra -Wpedantic -Wformat)
set(BUILD_EXAMPLES OFF CACHE BOOL "Whether to build examples or not.") set(BUILD_EXAMPLES OFF CACHE BOOL "Whether to build examples or not.")
set(DDE_AM_USE_DEBUG_DBUS_NAME OFF CACHE BOOL "build a dbus service using a different bus name for debug.") set(DDE_AM_USE_DEBUG_DBUS_NAME OFF CACHE BOOL "build a dbus service using a different bus name for debug.")
set(PROFILING_MODE OFF CACHE BOOL "run a valgrind performance profiling.") set(PROFILING_MODE OFF CACHE BOOL "run a valgrind performance profiling.")

View File

@ -73,9 +73,7 @@ int processExecStart(msg_ptr &msg, const std::deque<std::string_view> &execArgs)
if (ret = sd_bus_message_open_container(msg, SD_BUS_TYPE_VARIANT, "a(sasb)"); ret < 0) { if (ret = sd_bus_message_open_container(msg, SD_BUS_TYPE_VARIANT, "a(sasb)"); ret < 0) {
sd_journal_perror("open variant of execStart failed."); sd_journal_perror("open variant of execStart failed.");
if (auto tmp = sd_bus_message_close_container(msg)) { return ret;
return ret;
}
} }
if (ret = sd_bus_message_open_container(msg, SD_BUS_TYPE_ARRAY, "(sasb)"); ret < 0) { if (ret = sd_bus_message_open_container(msg, SD_BUS_TYPE_ARRAY, "(sasb)"); ret < 0) {
@ -229,7 +227,7 @@ std::string cmdParse(msg_ptr &msg, std::deque<std::string_view> cmdLines)
} }
auto kvStr = str.substr(2); auto kvStr = str.substr(2);
if (!kvStr.empty()) [[likely]] { if (!kvStr.empty()) {
const auto *it = kvStr.cbegin(); const auto *it = kvStr.cbegin();
if (it = std::find(it, kvStr.cend(), '='); it == kvStr.cend()) { if (it = std::find(it, kvStr.cend(), '='); it == kvStr.cend()) {
sd_journal_print(LOG_WARNING, "invalid k-v pair: %s", kvStr.data()); sd_journal_print(LOG_WARNING, "invalid k-v pair: %s", kvStr.data());

View File

@ -82,7 +82,7 @@ QString CGroupsIdentifier::parseCGroupsPath(QFile &cgroupFile) noexcept
return {}; return {};
} }
if (auto processUID = processUIDStr.toInt(); processUID != getCurrentUID()) { if (auto processUID = processUIDStr.toInt(); static_cast<uid_t>(processUID) != getCurrentUID()) {
qWarning() << "process is not in CGroups of current user, ignore...."; qWarning() << "process is not in CGroups of current user, ignore....";
return {}; return {};
} }

View File

@ -32,7 +32,10 @@ void ApplicationManager1Service::initService(QDBusConnection &connection) noexce
qFatal("%s", connection.lastError().message().toLocal8Bit().data()); qFatal("%s", connection.lastError().message().toLocal8Bit().data());
} }
new (std::nothrow) ProcessGuesser1Service{connection, this}; if (auto *tmp = new (std::nothrow) ProcessGuesser1Service{connection, this}; tmp == nullptr) {
qCritical() << "new ProcessGuesser1Service failed.";
std::terminate();
}
if (auto *tmp = new (std::nothrow) ApplicationManager1Adaptor{this}; tmp == nullptr) { if (auto *tmp = new (std::nothrow) ApplicationManager1Adaptor{this}; tmp == nullptr) {
qCritical() << "new Application Manager Adaptor failed."; qCritical() << "new Application Manager Adaptor failed.";
@ -154,7 +157,9 @@ void ApplicationManager1Service::initService(QDBusConnection &connection) noexce
watcher->deleteLater(); watcher->deleteLater();
}; };
*sigCon = connect(watcher, &QDBusServiceWatcher::serviceRegistered, singleSlot); if (watcher != nullptr) {
*sigCon = connect(watcher, &QDBusServiceWatcher::serviceRegistered, singleSlot);
}
auto msg = auto msg =
QDBusMessage::createMethodCall("org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "NameHasOwner"); QDBusMessage::createMethodCall("org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "NameHasOwner");
@ -192,7 +197,7 @@ void ApplicationManager1Service::addInstanceToApplication(const QString &unitNam
m_applicationList.cend(), m_applicationList.cend(),
[&appId](const QSharedPointer<ApplicationService> &app) { return app->id() == appId; }); [&appId](const QSharedPointer<ApplicationService> &app) { return app->id() == appId; });
if (appIt == m_applicationList.cend()) [[unlikely]] { if (appIt == m_applicationList.cend()) {
qWarning() << "couldn't find app" << appId << "in application manager."; qWarning() << "couldn't find app" << appId << "in application manager.";
return; return;
} }
@ -201,7 +206,7 @@ void ApplicationManager1Service::addInstanceToApplication(const QString &unitNam
const auto &applicationPath = (*appIt)->applicationPath().path(); const auto &applicationPath = (*appIt)->applicationPath().path();
if (!(*appIt)->addOneInstance(instanceId, applicationPath, systemdUnitPath.path(), launcher)) [[likely]] { if (!(*appIt)->addOneInstance(instanceId, applicationPath, systemdUnitPath.path(), launcher)) {
qCritical() << "add Instance failed:" << applicationPath << unitName << systemdUnitPath.path(); qCritical() << "add Instance failed:" << applicationPath << unitName << systemdUnitPath.path();
} }
} }
@ -225,7 +230,7 @@ void ApplicationManager1Service::removeInstanceFromApplication(const QString &un
m_applicationList.cend(), m_applicationList.cend(),
[&appId](const QSharedPointer<ApplicationService> &app) { return app->id() == appId; }); [&appId](const QSharedPointer<ApplicationService> &app) { return app->id() == appId; });
if (appIt == m_applicationList.cend()) [[unlikely]] { if (appIt == m_applicationList.cend()) {
qWarning() << "couldn't find app" << appId << "in application manager."; qWarning() << "couldn't find app" << appId << "in application manager.";
return; return;
} }
@ -237,12 +242,12 @@ void ApplicationManager1Service::removeInstanceFromApplication(const QString &un
return value->property("SystemdUnitPath") == systemdUnitPath; return value->property("SystemdUnitPath") == systemdUnitPath;
}); });
if (instanceIt != appIns.cend()) [[likely]] { if (instanceIt != appIns.cend()) {
(*appIt)->removeOneInstance(instanceIt.key()); (*appIt)->removeOneInstance(instanceIt.key());
return; return;
} }
orphanedInstances->removeIf([&systemdUnitPath](const QSharedPointer<InstanceService> &ptr) { orphanedInstances.removeIf([&systemdUnitPath](const QSharedPointer<InstanceService> &ptr) {
return (ptr->property("SystemdUnitPath").value<QDBusObjectPath>() == systemdUnitPath); return (ptr->property("SystemdUnitPath").value<QDBusObjectPath>() == systemdUnitPath);
}); });
} }
@ -426,7 +431,7 @@ QHash<QSharedPointer<ApplicationService>, QString> ApplicationManager1Service::s
} }
qInfo() << "launch new autostart application " << newApp->id(); qInfo() << "launch new autostart application " << newApp->id();
newApp->setAutostartSource({desktopFile.sourcePath()}); newApp->setAutostartSource({desktopFile.sourcePath(), {}});
ret.insert(newApp, {}); ret.insert(newApp, {});
} }

View File

@ -779,23 +779,29 @@ QList<QDBusObjectPath> ApplicationService::instances() const noexcept
bool ApplicationService::addOneInstance(const QString &instanceId, bool ApplicationService::addOneInstance(const QString &instanceId,
const QString &application, const QString &application,
const QString &systemdUnitPath, const QString &systemdUnitPath,
const QString &launcher) const QString &launcher) noexcept
{ {
auto *service = new InstanceService{instanceId, application, systemdUnitPath, launcher}; auto *service = new (std::nothrow) InstanceService{instanceId, application, systemdUnitPath, launcher};
auto *adaptor = new InstanceAdaptor(service); if (service == nullptr) {
QString objectPath{m_applicationPath.path() + "/" + instanceId}; qCritical() << "couldn't new InstanceService.";
return false;
if (registerObjectToDBus(service, objectPath, InstanceInterface)) {
m_Instances.insert(QDBusObjectPath{objectPath}, QSharedPointer<InstanceService>{service});
service->moveToThread(this->thread());
adaptor->moveToThread(this->thread());
emit InterfacesAdded(QDBusObjectPath{objectPath}, getChildInterfacesAndPropertiesFromObject(service));
return true;
} }
adaptor->deleteLater(); auto *adaptor = new (std::nothrow) InstanceAdaptor{service};
service->deleteLater(); QString objectPath{m_applicationPath.path() + "/" + instanceId};
return false;
if (adaptor == nullptr or !registerObjectToDBus(service, objectPath, InstanceInterface)) {
adaptor->deleteLater();
service->deleteLater();
return false;
}
m_Instances.insert(QDBusObjectPath{objectPath}, QSharedPointer<InstanceService>{service});
service->moveToThread(this->thread());
adaptor->moveToThread(this->thread());
emit InterfacesAdded(QDBusObjectPath{objectPath}, getChildInterfacesAndPropertiesFromObject(service));
return true;
} }
void ApplicationService::removeOneInstance(const QDBusObjectPath &instance) noexcept void ApplicationService::removeOneInstance(const QDBusObjectPath &instance) noexcept
@ -817,7 +823,7 @@ void ApplicationService::removeAllInstance() noexcept
void ApplicationService::detachAllInstance() noexcept void ApplicationService::detachAllInstance() noexcept
{ {
for (auto &instance : m_Instances.values()) { for (auto &instance : m_Instances.values()) {
orphanedInstances->append(instance); orphanedInstances.append(instance);
instance->setProperty("Orphaned", true); instance->setProperty("Orphaned", true);
} }
@ -869,7 +875,12 @@ std::optional<QStringList> ApplicationService::unescapeExecArgs(const QString &s
wordfree(word); wordfree(word);
delete word; delete word;
}; };
std::unique_ptr<wordexp_t, decltype(deleter)> words{new (std::nothrow) wordexp_t{0, nullptr, 0}, deleter}; std::unique_ptr<wordexp_t, decltype(deleter)> words{new (std::nothrow) wordexp_t{0, nullptr, 0}, deleter};
if (words == nullptr) {
qCritical() << "couldn't new wordexp_t";
return std::nullopt;
}
if (auto ret = wordexp(unescapedStr.toLocal8Bit(), words.get(), WRDE_SHOWERR); ret != 0) { if (auto ret = wordexp(unescapedStr.toLocal8Bit(), words.get(), WRDE_SHOWERR); ret != 0) {
if (ret != 0) { if (ret != 0) {
@ -899,7 +910,7 @@ std::optional<QStringList> ApplicationService::unescapeExecArgs(const QString &s
} }
QStringList execList; QStringList execList;
for (int i = 0; i < words->we_wordc; ++i) { for (std::size_t i = 0; i < words->we_wordc; ++i) {
execList.emplace_back(words->we_wordv[i]); execList.emplace_back(words->we_wordv[i]);
} }

View File

@ -111,7 +111,7 @@ public:
bool addOneInstance(const QString &instanceId, bool addOneInstance(const QString &instanceId,
const QString &application, const QString &application,
const QString &systemdUnitPath, const QString &systemdUnitPath,
const QString &launcher); const QString &launcher) noexcept;
void recoverInstances(const QList<QDBusObjectPath> &instanceList) noexcept; void recoverInstances(const QList<QDBusObjectPath> &instanceList) noexcept;
void removeOneInstance(const QDBusObjectPath &instance) noexcept; void removeOneInstance(const QDBusObjectPath &instance) noexcept;
void removeAllInstance() noexcept; void removeAllInstance() noexcept;

View File

@ -14,7 +14,10 @@ InstanceService::InstanceService(QString instanceId, QString application, QStrin
, m_Application(std::move(application)) , m_Application(std::move(application))
, m_SystemdUnitPath(std::move(systemdUnitPath)) , m_SystemdUnitPath(std::move(systemdUnitPath))
{ {
new PropertiesForwarder{application + "/" + instanceId, this}; if (auto *tmp = new (std::nothrow) PropertiesForwarder{application + "/" + instanceId, this}; tmp == nullptr) {
qCritical() << "couldn't new PropertiesForwarder for instanceService.";
return;
}
} }
InstanceService::~InstanceService() = default; InstanceService::~InstanceService() = default;

View File

@ -42,6 +42,6 @@ private:
QDBusObjectPath m_SystemdUnitPath; QDBusObjectPath m_SystemdUnitPath;
}; };
Q_GLOBAL_STATIC(QList<QSharedPointer<InstanceService>>, orphanedInstances) static inline QList<QSharedPointer<InstanceService>> orphanedInstances{};
#endif #endif

View File

@ -8,10 +8,11 @@
JobManager1Service::JobManager1Service(ApplicationManager1Service *parent) JobManager1Service::JobManager1Service(ApplicationManager1Service *parent)
: m_parent(parent) : m_parent(parent)
{ {
new JobManager1Adaptor{this}; auto *adaptor = new (std::nothrow) JobManager1Adaptor{this};
if (!registerObjectToDBus(this, DDEApplicationManager1JobManager1ObjectPath, JobManager1Interface)) { if (adaptor == nullptr or !registerObjectToDBus(this, DDEApplicationManager1JobManager1ObjectPath, JobManager1Interface)) {
std::terminate(); std::terminate();
} }
qRegisterMetaType<LaunchTask>(); qRegisterMetaType<LaunchTask>();
} }

View File

@ -58,11 +58,16 @@ public:
qOverload<QVariantList::parameter_type>(&QVariantList::append), qOverload<QVariantList::parameter_type>(&QVariantList::append),
QVariantList{}, QVariantList{},
QtConcurrent::ReduceOption::OrderedReduce); QtConcurrent::ReduceOption::OrderedReduce);
QSharedPointer<JobService> job{new JobService{future}}; QSharedPointer<JobService> job{new (std::nothrow) JobService{future}};
if (job == nullptr) {
qCritical() << "couldn't new JobService.";
future.cancel();
return {};
}
auto *ptr = job.data(); auto *ptr = job.data();
new JobAdaptor(ptr); auto *adaptor = new (std::nothrow) JobAdaptor(ptr);
if (!registerObjectToDBus(ptr, objectPath, JobInterface)) { if (adaptor == nullptr or !registerObjectToDBus(ptr, objectPath, JobInterface)) {
qCritical() << "can't register job to dbus."; qCritical() << "can't register job to dbus.";
future.cancel(); future.cancel();
return {}; return {};

View File

@ -10,8 +10,8 @@
MimeManager1Service::MimeManager1Service(ApplicationManager1Service *parent) MimeManager1Service::MimeManager1Service(ApplicationManager1Service *parent)
: QObject(parent) : QObject(parent)
{ {
new MimeManager1Adaptor{this}; auto *adaptor = new (std::nothrow) MimeManager1Adaptor{this};
if (!registerObjectToDBus(this, DDEApplicationManager1MimeManager1ObjectPath, MimeManager1Interface)) { if (adaptor == nullptr or !registerObjectToDBus(this, DDEApplicationManager1MimeManager1ObjectPath, MimeManager1Interface)) {
std::terminate(); std::terminate();
} }
} }

View File

@ -13,11 +13,9 @@ ProcessGuesser1Service::ProcessGuesser1Service(QDBusConnection &connection, Appl
qFatal("%s", connection.lastError().message().toLocal8Bit().data()); qFatal("%s", connection.lastError().message().toLocal8Bit().data());
} }
if (auto *tmp = new (std::nothrow) ProcessGuesser1Adaptor{this}; tmp == nullptr) { auto *adaptor = new (std::nothrow) ProcessGuesser1Adaptor{this};
qFatal("new Process Guesser Adaptor failed."); if (adaptor == nullptr or
} !registerObjectToDBus(this, "/org/desktopspec/ProcessGuesser1", "org.desktopspec.ProcessGuesser1")) {
if (!registerObjectToDBus(this, "/org/desktopspec/ProcessGuesser1", "org.desktopspec.ProcessGuesser1")) {
std::terminate(); std::terminate();
} }
} }

View File

@ -165,7 +165,7 @@ ParserError DesktopFileParser::addEntry(typename Groups::iterator &group) noexce
} }
auto localeMap = keyIt->value<QStringMap>(); auto localeMap = keyIt->value<QStringMap>();
if (auto valueIt = localeMap.find(localeStr) != localeMap.end()) { if (localeMap.find(localeStr) != localeMap.end()) {
qWarning() << "duplicate locale key:" << key << "skip."; qWarning() << "duplicate locale key:" << key << "skip.";
return ParserError::NoError; return ParserError::NoError;
} }

View File

@ -86,7 +86,6 @@ QStringList setUserLaunchOption::generateCommandLine() const noexcept
QString userName = destUser->pw_name; QString userName = destUser->pw_name;
ret.append(userName); ret.append(userName);
struct passwd *curUser = getpwuid(curUID);
ret.append("env"); ret.append("env");
ret.append("DISPLAY=:0"); ret.append("DISPLAY=:0");