refact: ApplicationService ctor and some member functions
Signed-off-by: ComixHe <heyuming@deepin.org>
This commit is contained in:
@ -31,39 +31,11 @@ public:
|
||||
Q_PROPERTY(QList<QDBusObjectPath> List READ list)
|
||||
[[nodiscard]] QList<QDBusObjectPath> list() const;
|
||||
|
||||
template <typename T>
|
||||
bool addApplication(T &&desktopFileSource)
|
||||
{
|
||||
QSharedPointer<ApplicationService> application = makeApplication(std::forward<T>(desktopFileSource), this);
|
||||
if (!application) {
|
||||
return false;
|
||||
}
|
||||
bool addApplication(DesktopFile desktopFileSource) noexcept;
|
||||
void removeOneApplication(const QDBusObjectPath &application) noexcept;
|
||||
void removeAllApplication() noexcept;
|
||||
|
||||
if (m_applicationList.constFind(application->m_applicationPath) != m_applicationList.cend()) {
|
||||
auto info = qInfo();
|
||||
info << "this application already exists.";
|
||||
if (application->m_isPersistence) {
|
||||
info << "desktop source:" << application->m_desktopSource.m_file.filePath();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
auto *ptr = application.data();
|
||||
new ApplicationAdaptor{ptr};
|
||||
|
||||
if (!registerObjectToDBus(
|
||||
ptr, application->m_applicationPath.path(), getDBusInterface(QMetaType::fromType<ApplicationAdaptor>()))) {
|
||||
return false;
|
||||
}
|
||||
m_applicationList.insert(application->m_applicationPath, application);
|
||||
emit InterfacesAdded(application->m_applicationPath, getInterfacesListFromObject(ptr));
|
||||
|
||||
return true;
|
||||
}
|
||||
void removeOneApplication(const QDBusObjectPath &application);
|
||||
void removeAllApplication();
|
||||
|
||||
void updateApplication(const QSharedPointer<ApplicationService> &deskApp, const DesktopFile &desktopFile) noexcept;
|
||||
void updateApplication(const QSharedPointer<ApplicationService> &destApp, const DesktopFile &desktopFile) noexcept;
|
||||
|
||||
JobManager1Service &jobManager() noexcept { return *m_jobManager; }
|
||||
|
||||
@ -82,59 +54,4 @@ private:
|
||||
QMap<QDBusObjectPath, QSharedPointer<ApplicationService>> m_applicationList;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
QSharedPointer<ApplicationService> makeApplication(T &&source,
|
||||
ApplicationManager1Service *parent) // NOTE: maybe we should refactor
|
||||
{
|
||||
static_assert(std::is_same_v<T, DesktopFile> or std::is_same_v<T, QString>, "param type must be QString or DesktopFile.");
|
||||
QString objectPath;
|
||||
QTextStream sourceStream;
|
||||
QFile sourceFile;
|
||||
QSharedPointer<ApplicationService> app{nullptr};
|
||||
|
||||
if constexpr (std::is_same_v<T, DesktopFile>) {
|
||||
DesktopFile in{std::forward<T>(source)};
|
||||
objectPath = QString{DDEApplicationManager1ObjectPath} + "/" + escapeToObjectPath(in.desktopId());
|
||||
sourceFile.setFileName(in.filePath());
|
||||
|
||||
if (!sourceFile.open(QFile::ExistingOnly | QFile::ReadOnly | QFile::Text)) {
|
||||
qCritical() << "desktop file can't open:" << in.filePath() << sourceFile.errorString();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
app.reset(new ApplicationService{std::move(in)});
|
||||
sourceStream.setDevice(&sourceFile);
|
||||
} else if (std::is_same_v<T, QString>) {
|
||||
QString in{std::forward<T>(source)};
|
||||
objectPath = QString{DDEApplicationManager1ObjectPath} + "/" + QUuid::createUuid().toString(QUuid::Id128);
|
||||
|
||||
app.reset(new ApplicationService{std::move(in)});
|
||||
sourceStream.setString(&in, QTextStream::ReadOnly | QTextStream::Text);
|
||||
}
|
||||
|
||||
std::unique_ptr<DesktopEntry> entry{std::make_unique<DesktopEntry>()};
|
||||
auto error = entry->parse(sourceStream);
|
||||
|
||||
if (error != DesktopErrorCode::NoError) {
|
||||
if (error != DesktopErrorCode::EntryKeyInvalid) {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
if (auto val = entry->value(DesktopFileEntryKey, "Hidden"); val.has_value()) {
|
||||
bool ok{false};
|
||||
if (auto hidden = val.value().toBoolean(ok); ok and hidden) {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
app->m_parent = parent;
|
||||
app->m_entry.reset(entry.release());
|
||||
app->m_applicationPath = QDBusObjectPath{std::move(objectPath)};
|
||||
|
||||
// TODO: icon lookup
|
||||
new APPObjectManagerAdaptor{app.data()};
|
||||
return app;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user