File indexing completed on 2024-04-28 05:49:27

0001 /* This file is part of the KDE project
0002    SPDX-FileCopyrightText: 2001 Christoph Cullmann <cullmann@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #include "kateappadaptor.h"
0008 
0009 #include "kateapp.h"
0010 #include "katedocmanager.h"
0011 #include "katemainwindow.h"
0012 #include "katesessionmanager.h"
0013 
0014 #include "katedebug.h"
0015 
0016 // X11 startup handling
0017 #define HAVE_X11 __has_include(<KStartupInfo>)
0018 #if HAVE_X11
0019 #include <KStartupInfo>
0020 #endif
0021 
0022 #include <KWindowSystem>
0023 
0024 #include <QApplication>
0025 
0026 /**
0027  * add the adapter to the global application instance to have
0028  * it auto-register with KDBusService, see bug 410742
0029  */
0030 KateAppAdaptor::KateAppAdaptor(KateApp *app)
0031     : QDBusAbstractAdaptor(qApp)
0032     , m_app(app)
0033 {
0034 }
0035 
0036 void KateAppAdaptor::activate(const QString &token)
0037 {
0038     KateMainWindow *win = m_app->activeKateMainWindow();
0039     if (!win) {
0040         return;
0041     }
0042 
0043     // like QtSingleApplication
0044     win->setWindowState(win->windowState() & ~Qt::WindowMinimized);
0045     win->raise();
0046     win->activateWindow();
0047 
0048     // try to raise window, see bug 407288
0049     if (KWindowSystem::isPlatformX11()) {
0050 #if HAVE_X11
0051         KStartupInfo::setNewStartupId(win->windowHandle(), token.toUtf8());
0052 #endif
0053     } else if (KWindowSystem::isPlatformWayland()) {
0054         KWindowSystem::setCurrentXdgActivationToken(token);
0055     }
0056 
0057     KWindowSystem::activateWindow(win->windowHandle());
0058 }
0059 
0060 bool KateAppAdaptor::openUrl(const QString &url, const QString &encoding)
0061 {
0062     return m_app->openDocUrl(QUrl(url), encoding, false);
0063 }
0064 
0065 bool KateAppAdaptor::openUrl(const QString &url, const QString &encoding, bool isTempFile)
0066 {
0067     qCDebug(LOG_KATE) << "openURL";
0068 
0069     return m_app->openDocUrl(QUrl(url), encoding, isTempFile);
0070 }
0071 
0072 //-----------
0073 QString KateAppAdaptor::tokenOpenUrl(const QString &url, const QString &encoding)
0074 {
0075     KTextEditor::Document *doc = m_app->openDocUrl(QUrl(url), encoding, false);
0076     if (!doc) {
0077         return QStringLiteral("ERROR");
0078     }
0079     return QStringLiteral("%1").arg(reinterpret_cast<qptrdiff>(doc));
0080 }
0081 
0082 QString KateAppAdaptor::tokenOpenUrl(const QString &url, const QString &encoding, bool isTempFile)
0083 {
0084     qCDebug(LOG_KATE) << "openURL";
0085     KTextEditor::Document *doc = m_app->openDocUrl(QUrl(url), encoding, isTempFile);
0086     if (!doc) {
0087         return QStringLiteral("ERROR");
0088     }
0089     return QStringLiteral("%1").arg(reinterpret_cast<qptrdiff>(doc));
0090 }
0091 
0092 QString KateAppAdaptor::tokenOpenUrlAt(const QString &url, int line, int column, const QString &encoding, bool isTempFile)
0093 {
0094     qCDebug(LOG_KATE) << "openURLAt";
0095     KTextEditor::Document *doc = m_app->openDocUrl(QUrl(url), encoding, isTempFile);
0096     if (!doc) {
0097         return QStringLiteral("ERROR");
0098     }
0099     m_app->setCursor(line, column);
0100     return QStringLiteral("%1").arg(reinterpret_cast<qptrdiff>(doc));
0101 }
0102 //--------
0103 
0104 bool KateAppAdaptor::setCursor(int line, int column)
0105 {
0106     return m_app->setCursor(line, column);
0107 }
0108 
0109 bool KateAppAdaptor::openInput(const QString &text, const QString &encoding)
0110 {
0111     return m_app->openInput(text, encoding);
0112 }
0113 
0114 bool KateAppAdaptor::activateSession(const QString &session)
0115 {
0116     return m_app->sessionManager()->activateSession(session);
0117 }
0118 
0119 qint64 KateAppAdaptor::lastActivationChange() const
0120 {
0121     return m_app->lastActivationChange();
0122 }
0123 
0124 QString KateAppAdaptor::activeSession() const
0125 {
0126     return m_app->sessionManager()->activeSession()->name();
0127 }
0128 
0129 void KateAppAdaptor::emitExiting()
0130 {
0131     Q_EMIT exiting();
0132 }
0133 
0134 void KateAppAdaptor::emitDocumentClosed(const QString &token)
0135 {
0136     documentClosed(token);
0137 }
0138 
0139 #include "moc_kateappadaptor.cpp"