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