File indexing completed on 2024-05-05 04:47:06
0001 #include "server.h" 0002 0003 #include <QGuiApplication> 0004 #include <QQuickWindow> 0005 #include <QQmlApplicationEngine> 0006 0007 #include <MauiKit3/FileBrowsing/fmstatic.h> 0008 0009 #include <QDBusConnection> 0010 #include <QDebug> 0011 0012 #if (defined Q_OS_LINUX || defined Q_OS_FREEBSD) && !defined Q_OS_ANDROID 0013 #include "serverinterface.h" 0014 #include "settingsadaptor.h" 0015 0016 QVector<QPair<QSharedPointer<OrgMauiSettingsActionsInterface>, QStringList>> AppInstance::appInstances(const QString& preferredService) 0017 { 0018 QVector<QPair<QSharedPointer<OrgMauiSettingsActionsInterface>, QStringList>> dolphinInterfaces; 0019 0020 if (!preferredService.isEmpty()) 0021 { 0022 QSharedPointer<OrgMauiSettingsActionsInterface> preferredInterface( 0023 new OrgMauiSettingsActionsInterface(preferredService, 0024 QStringLiteral("/Actions"), 0025 QDBusConnection::sessionBus())); 0026 0027 qDebug() << "IS PREFRFRED INTERFACE VALID?" << preferredInterface->isValid() << preferredInterface->lastError().message(); 0028 if (preferredInterface->isValid() && !preferredInterface->lastError().isValid()) { 0029 dolphinInterfaces.append(qMakePair(preferredInterface, QStringList())); 0030 } 0031 } 0032 0033 // Look for dolphin instances among all available dbus services. 0034 QDBusConnectionInterface *sessionInterface = QDBusConnection::sessionBus().interface(); 0035 const QStringList dbusServices = sessionInterface ? sessionInterface->registeredServiceNames().value() : QStringList(); 0036 // Don't match the service without trailing "-" (unique instance) 0037 const QString pattern = QStringLiteral("org.maui.settings-"); 0038 0039 // Don't match the pid without leading "-" 0040 const QString myPid = QLatin1Char('-') + QString::number(QCoreApplication::applicationPid()); 0041 0042 for (const QString& service : dbusServices) 0043 { 0044 if (service.startsWith(pattern) && !service.endsWith(myPid)) 0045 { 0046 qDebug() << "EXISTING INTANCES" << service; 0047 0048 // Check if instance can handle our URLs 0049 QSharedPointer<OrgMauiSettingsActionsInterface> interface( 0050 new OrgMauiSettingsActionsInterface(service, 0051 QStringLiteral("/Actions"), 0052 QDBusConnection::sessionBus())); 0053 if (interface->isValid() && !interface->lastError().isValid()) 0054 { 0055 dolphinInterfaces.append(qMakePair(interface, QStringList())); 0056 } 0057 } 0058 } 0059 0060 return dolphinInterfaces; 0061 } 0062 0063 bool AppInstance::attachToExistingInstance(const QString &module, const QString& preferredService) 0064 { 0065 bool attached = false; 0066 0067 auto dolphinInterfaces = appInstances(preferredService); 0068 if (dolphinInterfaces.isEmpty()) 0069 { 0070 return attached; 0071 } 0072 0073 for (const auto& interface: qAsConst(dolphinInterfaces)) 0074 { 0075 auto reply = interface.first->openModule(module); 0076 reply.waitForFinished(); 0077 0078 if (!reply.isError()) 0079 { 0080 interface.first->activateWindow(); 0081 attached = true; 0082 break; 0083 } 0084 } 0085 0086 return attached; 0087 } 0088 0089 bool AppInstance::registerService() 0090 { 0091 QDBusConnectionInterface *iface = QDBusConnection::sessionBus().interface(); 0092 0093 auto registration = iface->registerService(QStringLiteral("org.maui.settings-%1").arg(QCoreApplication::applicationPid()), 0094 QDBusConnectionInterface::ReplaceExistingService, 0095 QDBusConnectionInterface::DontAllowReplacement); 0096 0097 if (!registration.isValid()) 0098 { 0099 qWarning("2 Failed to register D-Bus service \"%s\" on session bus: \"%s\"", 0100 qPrintable("org.maui.settings"), 0101 qPrintable(registration.error().message())); 0102 return false; 0103 } 0104 0105 return true; 0106 } 0107 0108 #endif 0109 0110 0111 Server::Server(QObject *parent) : QObject(parent) 0112 , m_qmlObject(nullptr) 0113 { 0114 #if (defined Q_OS_LINUX || defined Q_OS_FREEBSD) && !defined Q_OS_ANDROID 0115 new ActionsAdaptor(this); 0116 if(!QDBusConnection::sessionBus().registerObject(QStringLiteral("/Actions"), this)) 0117 { 0118 qDebug() << "FAILED TO REGISTER BACKGROUND DBUS OBJECT"; 0119 return; 0120 } 0121 #endif 0122 } 0123 0124 void Server::setQmlObject(QObject *object) 0125 { 0126 if(!m_qmlObject) 0127 { 0128 m_qmlObject = object; 0129 } 0130 } 0131 0132 void Server::activateWindow() 0133 { 0134 if(m_qmlObject) 0135 { 0136 qDebug() << "ACTIVET WINDOW FROM C++"; 0137 auto window = qobject_cast<QQuickWindow *>(m_qmlObject); 0138 if (window) 0139 { 0140 qDebug() << "Trying to raise wndow"; 0141 window->raise(); 0142 window->requestActivate(); 0143 } 0144 } 0145 } 0146 0147 void Server::quit() 0148 { 0149 QCoreApplication::quit(); 0150 } 0151 0152 void Server::openModule(const QString &module) 0153 { 0154 if(m_qmlObject) 0155 { 0156 if(!module.isEmpty()) 0157 { 0158 QMetaObject::invokeMethod(m_qmlObject, "openModule", 0159 Q_ARG(QVariant, module)); 0160 } 0161 0162 } 0163 } 0164 0165 bool Server::isModuleOpen(const QString &url) 0166 { 0167 bool value = false; 0168 0169 if(m_qmlObject) 0170 { 0171 QMetaObject::invokeMethod(m_qmlObject, "isModuleOpen", 0172 Q_RETURN_ARG(bool, value), 0173 Q_ARG(QString, url)); 0174 } 0175 return value; 0176 }