File indexing completed on 2024-04-28 04:55:51

0001 /*
0002     This file is part of Choqok, the KDE micro-blogging client
0003 
0004     SPDX-FileCopyrightText: 2010-2012 Mehrdad Momeny <mehrdad.momeny@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0007 */
0008 
0009 #include "konqchoqok.h"
0010 
0011 #include <QDBusInterface>
0012 #include <QDBusConnection>
0013 #include <QDBusConnectionInterface>
0014 #include <QMenu>
0015 
0016 #include <KActionCollection>
0017 #include <KActionMenu>
0018 #include <KLocalizedString>
0019 #include <KMessageBox>
0020 #include <KPluginFactory>
0021 #include <KToggleAction>
0022 #include <KToolInvocation>
0023 #include <KWebPage>
0024 
0025 K_PLUGIN_CLASS_WITH_JSON(KonqPluginChoqok, "konqchoqok.json")
0026 
0027 KonqPluginChoqok::KonqPluginChoqok(QObject *parent, const QVariantList &)
0028     : Plugin(parent) , m_interface(nullptr)
0029 {
0030     KActionMenu *menu = new KActionMenu(QIcon::fromTheme(QLatin1String("choqok")) , i18n("Choqok"),
0031                                         actionCollection());
0032     actionCollection()->addAction(QLatin1String("action menu"), menu);
0033     menu->setDelayed(false);
0034 
0035     QAction *postaction = actionCollection()->addAction(QLatin1String("post_choqok"));
0036     postaction->setText(i18n("Post Text with Choqok"));
0037     connect(postaction, SIGNAL(triggered(bool)), SLOT(slotpostSelectedText()));
0038     menu->addAction(postaction);
0039 
0040     QAction *shortening = actionCollection()->add<KToggleAction>(QLatin1String("shortening_choqok"));
0041     shortening->setText(i18n("Shorten URL on Paste"));
0042     connect(shortening, SIGNAL(toggled(bool)), SLOT(toggleShortening(bool)));
0043     menu->addAction(shortening);
0044 
0045     connect(menu->menu(), SIGNAL(aboutToShow()), SLOT(updateActions()));
0046 }
0047 
0048 void KonqPluginChoqok::updateActions()
0049 {
0050 
0051     // Is Choqok running?
0052     if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(QLatin1String("org.kde.choqok"))) {
0053         ((KToggleAction *) actionCollection()->action(QLatin1String("shortening_choqok")))->setEnabled(false);
0054         return;
0055     }
0056     // Choqok is running, so I can connect to it, if I haven't done yet.
0057     if (!m_interface) {
0058         m_interface = new  QDBusInterface(QLatin1String("org.kde.choqok"),
0059                                           QLatin1String("/"),
0060                                           QLatin1String("org.kde.choqok"),
0061                                           QDBusConnection::sessionBus());
0062 
0063     }
0064     QDBusReply<bool> reply = m_interface->call(QLatin1String("getShortening"));
0065     if (reply.isValid()) {
0066         ((KToggleAction *) actionCollection()->action(QLatin1String("shortening_choqok")))->setEnabled(true);
0067         ((KToggleAction *) actionCollection()->action(QLatin1String("shortening_choqok")))->setChecked(reply.value());
0068     }
0069 }
0070 
0071 KonqPluginChoqok::~KonqPluginChoqok()
0072 {
0073 
0074 }
0075 
0076 void KonqPluginChoqok::slotpostSelectedText()
0077 {
0078     QWidget *m_parentWidget;
0079     QString text;
0080 
0081     if (parent()->inherits("KWebPage")) {
0082         m_parentWidget = qobject_cast< KWebPage * >(parent())->view();
0083         text = QString(qobject_cast< KWebPage * >(parent())->selectedText());
0084     } else {
0085         return;
0086     }
0087 
0088     if (text.isEmpty()) {
0089         KMessageBox::information(m_parentWidget,
0090                                  i18n("You need to select text to post."),
0091                                  i18n("Post Text with Choqok"));
0092         return;
0093     }
0094 
0095     if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(QLatin1String("org.kde.choqok"))) {
0096         //qDebug() << "Choqok is not running, starting it!..." << endl;
0097         KToolInvocation::startServiceByDesktopName(QLatin1String("choqok"),
0098                 QStringList());
0099     }
0100     if (!m_interface) {
0101         m_interface = new  QDBusInterface(QLatin1String("org.kde.choqok"),
0102                                           QLatin1String("/"),
0103                                           QLatin1String("org.kde.choqok"),
0104                                           QDBusConnection::sessionBus());
0105     }
0106 
0107     m_interface->call(QLatin1String("postText"), text);
0108 }
0109 
0110 void KonqPluginChoqok::toggleShortening(bool value)
0111 {
0112     m_interface->call(QLatin1String("setShortening"), value);
0113     ((KToggleAction *) actionCollection()->action(QLatin1String("shortening_choqok")))->setChecked(value);
0114 }
0115 
0116 #include "konqchoqok.moc"
0117 #include "moc_konqchoqok.cpp"