File indexing completed on 2024-11-24 05:00:24

0001 /*
0002     SPDX-FileCopyrightText: 2017 Weng Xuetian <wengxt@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "config-kimpanel.h"
0008 #include <QCoreApplication>
0009 #include <QDBusConnection>
0010 #include <QDBusConnectionInterface>
0011 #include <QDBusServiceWatcher>
0012 #include <QProcess>
0013 
0014 class IBusPanelLauncher : public QCoreApplication
0015 {
0016     Q_OBJECT
0017 public:
0018     IBusPanelLauncher(int &argc, char *argv[])
0019         : QCoreApplication(argc, argv)
0020         , m_watcher(new QDBusServiceWatcher(this))
0021     {
0022         m_watcher->setConnection(QDBusConnection::sessionBus());
0023         QMetaObject::invokeMethod(this, "init", Qt::QueuedConnection);
0024     }
0025 
0026 public Q_SLOTS:
0027     void init()
0028     {
0029         // already a launcher running
0030         if (!QDBusConnection::sessionBus().registerService(QStringLiteral("org.kde.impanel.IBusPanelLauncher"))) {
0031             quit();
0032             return;
0033         }
0034         // Check if applet is still there.
0035         connect(m_watcher, &QDBusServiceWatcher::serviceUnregistered, this, &IBusPanelLauncher::serviceUnregistered);
0036         connect(m_watcher, &QDBusServiceWatcher::serviceRegistered, this, &IBusPanelLauncher::serviceRegistered);
0037         m_watcher->addWatchedService(QStringLiteral("org.kde.impanel"));
0038         m_watcher->addWatchedService(QStringLiteral("org.freedesktop.IBus"));
0039 
0040         // check if panel is already created
0041         QDBusConnection::sessionBus().connect(QStringLiteral("org.kde.impanel"),
0042                                               QStringLiteral("/org/kde/impanel"),
0043                                               QStringLiteral("org.kde.impanel2"),
0044                                               QStringLiteral("PanelRegistered"),
0045                                               this,
0046                                               SLOT(quit()));
0047         if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(QStringLiteral("org.kde.impanel"))) {
0048             quit();
0049             return;
0050         }
0051         if (QDBusConnection::sessionBus().interface()->isServiceRegistered(QStringLiteral("org.freedesktop.IBus"))) {
0052             serviceRegistered(QStringLiteral("org.freedesktop.IBus"));
0053         }
0054     }
0055 
0056     void serviceRegistered(const QString &service)
0057     {
0058         if (service == QLatin1String{"org.freedesktop.IBus"}) {
0059             launchIBusPanel();
0060         }
0061     }
0062 
0063     void serviceUnregistered(const QString &service)
0064     {
0065         if (service == QLatin1String{"org.kde.impanel"}) {
0066             quit();
0067         }
0068     }
0069 
0070 private:
0071     void launchIBusPanel()
0072     {
0073         const QString panelPath = QStringLiteral(KIMPANEL_LIBEXEC_DIR "/kimpanel-ibus-panel");
0074         QProcess::startDetached(panelPath, QStringList());
0075         quit();
0076     }
0077 
0078 private:
0079     QDBusServiceWatcher *m_watcher;
0080 };
0081 
0082 int main(int argc, char *argv[])
0083 {
0084     IBusPanelLauncher app(argc, argv);
0085 
0086     return app.exec();
0087 }
0088 
0089 #include "launcher.moc"