2023-09-20 18:29:42 +08:00
|
|
|
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
|
|
|
|
#include "dbus/mimemanager1adaptor.h"
|
|
|
|
#include "applicationmanager1service.h"
|
|
|
|
#include "applicationservice.h"
|
|
|
|
#include "constant.h"
|
|
|
|
|
|
|
|
MimeManager1Service::MimeManager1Service(ApplicationManager1Service *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
{
|
|
|
|
new MimeManager1Adaptor{this};
|
|
|
|
if (!registerObjectToDBus(this, DDEApplicationManager1MimeManager1ObjectPath, MimeManager1Interface)) {
|
|
|
|
std::terminate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MimeManager1Service::~MimeManager1Service() = default;
|
|
|
|
|
|
|
|
ObjectMap MimeManager1Service::listApplications(const QString &mimeType) const noexcept
|
|
|
|
{
|
2023-10-30 16:21:36 +08:00
|
|
|
auto type = m_database.mimeTypeForName(mimeType).name();
|
|
|
|
if (type.isEmpty()) {
|
|
|
|
qInfo() << "try to query raw type:" << mimeType;
|
|
|
|
type = mimeType;
|
2023-09-20 18:29:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QStringList appIds;
|
|
|
|
|
|
|
|
for (auto it = m_infos.rbegin(); it != m_infos.rend(); ++it) {
|
|
|
|
const auto &info = it->cacheInfo();
|
|
|
|
if (!info) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-10-30 16:21:36 +08:00
|
|
|
auto apps = info->queryApps(type);
|
2023-09-20 18:29:42 +08:00
|
|
|
appIds.append(std::move(apps));
|
|
|
|
}
|
|
|
|
appIds.removeDuplicates();
|
|
|
|
qInfo() << "query" << mimeType << "find:" << appIds;
|
2023-10-30 13:19:40 +08:00
|
|
|
const auto &apps = dynamic_cast<ApplicationManager1Service *>(parent())->findApplicationsByIds(appIds);
|
2023-09-20 18:29:42 +08:00
|
|
|
return dumpDBusObject(apps);
|
|
|
|
}
|
|
|
|
|
2023-10-30 13:19:40 +08:00
|
|
|
QString MimeManager1Service::queryDefaultApplication(const QString &content, QDBusObjectPath &application) const noexcept
|
2023-09-20 18:29:42 +08:00
|
|
|
{
|
2023-10-30 13:19:40 +08:00
|
|
|
QMimeType mime;
|
|
|
|
QFileInfo info{content};
|
2023-10-30 17:43:31 +08:00
|
|
|
application = QDBusObjectPath{"/"};
|
2023-09-20 18:29:42 +08:00
|
|
|
|
2023-10-30 13:19:40 +08:00
|
|
|
if (info.isAbsolute() and info.exists() and info.isFile()) {
|
|
|
|
mime = m_database.mimeTypeForFile(content);
|
|
|
|
} else {
|
|
|
|
mime = m_database.mimeTypeForName(content);
|
|
|
|
}
|
|
|
|
|
2023-10-30 16:21:36 +08:00
|
|
|
auto type = mime.name();
|
|
|
|
|
|
|
|
if (type.isEmpty()) {
|
|
|
|
qInfo() << "try to query raw content:" << content;
|
|
|
|
type = content;
|
2023-09-20 18:29:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QString defaultAppId;
|
|
|
|
for (auto it1 = m_infos.rbegin(); it1 != m_infos.rend(); ++it1) {
|
|
|
|
const auto &list = it1->appsList();
|
|
|
|
for (auto it2 = list.rbegin(); it2 != list.rend(); ++it2) {
|
2023-10-30 16:21:36 +08:00
|
|
|
if (auto app = it2->queryDefaultApp(type); !app.isEmpty()) {
|
2023-09-20 18:29:42 +08:00
|
|
|
defaultAppId = app;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defaultAppId.isEmpty()) {
|
|
|
|
qInfo() << "file's mimeType:" << mime.name() << "but can't find a default application for this type.";
|
2023-10-30 16:21:36 +08:00
|
|
|
return type;
|
2023-09-20 18:29:42 +08:00
|
|
|
}
|
|
|
|
|
2023-10-30 13:19:40 +08:00
|
|
|
const auto &apps = dynamic_cast<ApplicationManager1Service *>(parent())->findApplicationsByIds({defaultAppId});
|
2023-09-20 18:29:42 +08:00
|
|
|
if (apps.isEmpty()) {
|
|
|
|
qWarning() << "default application has been found:" << defaultAppId
|
|
|
|
<< " but we can't find corresponding application in ApplicationManagerService.";
|
|
|
|
} else {
|
|
|
|
application = apps.firstKey();
|
|
|
|
}
|
|
|
|
|
2023-10-30 16:21:36 +08:00
|
|
|
return type;
|
2023-09-20 18:29:42 +08:00
|
|
|
}
|
|
|
|
|
2023-10-12 18:05:00 +08:00
|
|
|
void MimeManager1Service::setDefaultApplication(const QStringMap &defaultApps) noexcept
|
2023-09-20 18:29:42 +08:00
|
|
|
{
|
|
|
|
auto &app = m_infos.front().appsList();
|
2024-01-09 15:16:01 +08:00
|
|
|
|
|
|
|
if (app.empty()) {
|
|
|
|
sendErrorReply(QDBusError::InternalError);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-20 18:29:42 +08:00
|
|
|
auto userConfig = std::find_if(
|
|
|
|
app.begin(), app.end(), [](const MimeApps &config) { return !config.isDesktopSpecific(); }); // always find this
|
|
|
|
|
2024-01-09 15:16:01 +08:00
|
|
|
if (userConfig == app.end()) {
|
|
|
|
qWarning() << "couldn't find user mimeApps";
|
|
|
|
sendErrorReply(QDBusError::InternalError);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-20 18:29:42 +08:00
|
|
|
for (auto it = defaultApps.constKeyValueBegin(); it != defaultApps.constKeyValueEnd(); ++it) {
|
|
|
|
userConfig->setDefaultApplication(it->first, it->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!userConfig->writeToFile()) {
|
|
|
|
sendErrorReply(QDBusError::Failed, "set default app failed, these config will be reset after re-login.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MimeManager1Service::unsetDefaultApplication(const QStringList &mimeTypes) noexcept
|
|
|
|
{
|
|
|
|
auto &app = m_infos.front().appsList();
|
|
|
|
auto userConfig = std::find_if(app.begin(), app.end(), [](const MimeApps &config) { return !config.isDesktopSpecific(); });
|
|
|
|
|
|
|
|
for (const auto &mime : mimeTypes) {
|
|
|
|
userConfig->unsetDefaultApplication(mime);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!userConfig->writeToFile()) {
|
|
|
|
sendErrorReply(QDBusError::Failed, "unset default app failed, these config will be reset after re-login.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MimeManager1Service::appendMimeInfo(MimeInfo &&info)
|
|
|
|
{
|
|
|
|
m_infos.emplace_back(std::move(info));
|
|
|
|
}
|
2024-01-04 14:22:01 +08:00
|
|
|
|
|
|
|
void MimeManager1Service::reset() noexcept
|
|
|
|
{
|
|
|
|
m_infos.clear();
|
|
|
|
}
|