File indexing completed on 2024-05-12 04:47:03

0001 #include "server.h"
0002 
0003 #include <QGuiApplication>
0004 #include <QQuickWindow>
0005 #include <QQmlApplicationEngine>
0006 
0007 #include <MauiKit3/FileBrowsing/fmstatic.h>
0008 
0009 #include "stationinterface.h"
0010 #include "stationadaptor.h"
0011 
0012 QVector<QPair<QSharedPointer<OrgKdeStationActionsInterface>, QStringList>> AppInstance::appInstances(const QString& preferredService)
0013 {
0014     QVector<QPair<QSharedPointer<OrgKdeStationActionsInterface>, QStringList>> dolphinInterfaces;
0015 
0016     if (!preferredService.isEmpty())
0017     {
0018         QSharedPointer<OrgKdeStationActionsInterface> preferredInterface(
0019                     new OrgKdeStationActionsInterface(preferredService,
0020                                                       QStringLiteral("/Actions"),
0021                                                       QDBusConnection::sessionBus()));
0022 
0023         qDebug() << "IS PREFRFRED INTERFACE VALID?" << preferredInterface->isValid() << preferredInterface->lastError().message();
0024         if (preferredInterface->isValid() && !preferredInterface->lastError().isValid()) {
0025             dolphinInterfaces.append(qMakePair(preferredInterface, QStringList()));
0026         }
0027     }
0028 
0029     // Look for dolphin instances among all available dbus services.
0030     QDBusConnectionInterface *sessionInterface = QDBusConnection::sessionBus().interface();
0031     const QStringList dbusServices = sessionInterface ? sessionInterface->registeredServiceNames().value() : QStringList();
0032     // Don't match the service without trailing "-" (unique instance)
0033     const QString pattern = QStringLiteral("org.kde.station-");
0034 
0035     // Don't match the pid without leading "-"
0036     const QString myPid = QLatin1Char('-') + QString::number(QCoreApplication::applicationPid());
0037 
0038     for (const QString& service : dbusServices)
0039     {
0040         if (service.startsWith(pattern) && !service.endsWith(myPid))
0041         {
0042             qDebug() << "EXISTING INTANCES" << service;
0043 
0044             // Check if instance can handle our URLs
0045             QSharedPointer<OrgKdeStationActionsInterface> interface(
0046                         new OrgKdeStationActionsInterface(service,
0047                                                           QStringLiteral("/Actions"),
0048                                                           QDBusConnection::sessionBus()));
0049             if (interface->isValid() && !interface->lastError().isValid())
0050             {
0051                 dolphinInterfaces.append(qMakePair(interface, QStringList()));
0052             }
0053         }
0054     }
0055 
0056     return dolphinInterfaces;
0057 }
0058 
0059 bool AppInstance::attachToExistingInstance(const QList<QUrl>& inputUrls, bool splitView, const QString& preferredService)
0060 {
0061     bool attached = false;
0062 
0063     auto dolphinInterfaces = appInstances(preferredService);
0064     if (dolphinInterfaces.isEmpty())
0065     {
0066         return false;
0067     }
0068 
0069     if(inputUrls.isEmpty())
0070     {
0071         auto interface = dolphinInterfaces.first();
0072         auto reply = interface.first->openNewTab("$PWD");
0073         reply.waitForFinished();
0074 
0075         if (!reply.isError())
0076         {
0077             interface.first->activateWindow();
0078         }
0079 
0080         return true;
0081     }
0082 
0083     for (const auto& interface: qAsConst(dolphinInterfaces))
0084     {
0085         auto reply = interface.first->openTabs(QUrl::toStringList(inputUrls, QUrl::PreferLocalFile), splitView);
0086         reply.waitForFinished();
0087 
0088         if (!reply.isError())
0089         {
0090             interface.first->activateWindow();
0091             attached = true;
0092             break;
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.station-%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.station"),
0111                  qPrintable(registration.error().message()));
0112         return false;
0113     }
0114 
0115     return true;
0116 }
0117 
0118 Server::Server(QObject *parent) : QObject(parent)
0119   , m_qmlObject(nullptr)
0120 {
0121     new ActionsAdaptor(this);
0122     if(!QDBusConnection::sessionBus().registerObject(QStringLiteral("/Actions"), this))
0123     {
0124         qDebug() << "FAILED TO REGISTER BACKGROUND DBUS OBJECT";
0125         return;
0126     }
0127 }
0128 
0129 void Server::setQmlObject(QObject *object)
0130 {
0131     if(!m_qmlObject)
0132     {
0133         m_qmlObject = object;
0134     }
0135 }
0136 
0137 void Server::activateWindow()
0138 {
0139     if(m_qmlObject)
0140     {
0141         qDebug() << "ACTIVET WINDOW FROM C++";
0142         auto window = qobject_cast<QQuickWindow *>(m_qmlObject);
0143         if (window)
0144         {
0145             qDebug() << "Trying to raise window";
0146             window->raise();
0147             window->requestActivate();
0148         }
0149     }
0150 }
0151 
0152 void Server::quit()
0153 {
0154     QCoreApplication::quit();
0155 }
0156 
0157 void Server::openTabs(const QStringList &urls, bool splitView)
0158 {
0159     Q_UNUSED(splitView)
0160 
0161     for(const auto &url : urls)
0162     {
0163         qDebug() << "REQUEST TO OPEN TAB AT LOCATION" << url;
0164         this->openNewTab(url);
0165     }
0166 }
0167 
0168 void Server::openNewTab(const QString &url)
0169 {
0170     if(m_qmlObject)
0171     {
0172         QMetaObject::invokeMethod(m_qmlObject, "openTab",
0173                                   Q_ARG(QString, url));
0174     }
0175 }
0176 
0177 void Server::openNewWindow(const QString &url)
0178 {
0179 
0180 }