File indexing completed on 2024-04-21 04:55:26

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     SPDX-FileCopyrightText: 2010-2011 Emanuele Bigiarini <pulmro@gmail.com>
0006 
0007     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0008 */
0009 
0010 #include "dbushandler.h"
0011 
0012 #include <QDBusConnection>
0013 #include <QPointer>
0014 #include <QTextDocument>
0015 
0016 #include <KIO/StoredTransferJob>
0017 
0018 #include "ChoqokAdaptor.h"
0019 #include "choqokbehaviorsettings.h"
0020 #include "libchoqokdebug.h"
0021 #include "quickpost.h"
0022 #include "shortenmanager.h"
0023 #include "uploadmediadialog.h"
0024 
0025 namespace Choqok
0026 {
0027 
0028 DbusHandler *DbusHandler::m_self = nullptr;
0029 
0030 DbusHandler::DbusHandler()
0031 {
0032     m_self = this;
0033     new ChoqokAdaptor(this);
0034     QDBusConnection::sessionBus().registerService(QLatin1String("org.kde.choqok"));
0035     QDBusConnection::sessionBus().registerObject(QLatin1String("/"), this);
0036 }
0037 
0038 DbusHandler::~DbusHandler()
0039 {
0040 
0041 }
0042 
0043 QString DbusHandler::prepareUrl(const QString &url)
0044 {
0045     if (Choqok::BehaviorSettings::shortenOnPaste() && url.count() > 30) {
0046         return ShortenManager::self()->shortenUrl(url);
0047     } else {
0048         return url;
0049     }
0050 }
0051 
0052 void DbusHandler::shareUrl(const QString &url, bool title)
0053 {
0054     if (title) {
0055         QByteArray data;
0056         KIO::StoredTransferJob *job = KIO::storedGet(QUrl(url), KIO::NoReload, KIO::HideProgressInfo) ;
0057         if (!job) {
0058             qCDebug(CHOQOK) << "Cannot create an http GET request!";
0059         } else {
0060             connect(job, &KIO::StoredTransferJob::result, this, &DbusHandler::slotTitleUrl);
0061             job->start();
0062         }
0063     }
0064     postText(prepareUrl(url));
0065 }
0066 
0067 void DbusHandler::slotTitleUrl(KJob *job)
0068 {
0069     QString text;
0070     if (!job) {
0071         qCWarning(CHOQOK) << "NULL Job returned";
0072         return;
0073     }
0074     KIO::StoredTransferJob *stj = qobject_cast<KIO::StoredTransferJob *> (job);
0075     if (job->error()) {
0076         qCDebug(CHOQOK) << "Job Error:" << job->errorString();
0077     } else {
0078         QByteArray data = stj->data();
0079         QTextCodec *codec = QTextCodec::codecForHtml(data);
0080         m_doc.setHtml(codec->toUnicode(data));
0081         text.append(m_doc.metaInformation(QTextDocument::DocumentTitle));
0082     }
0083     QString url = stj->url().toDisplayString();
0084     text.append(QLatin1Char(' ') + prepareUrl(url));
0085     postText(text);
0086 }
0087 
0088 void DbusHandler::uploadFile(const QString &filename)
0089 {
0090     QPointer<Choqok::UI::UploadMediaDialog> dlg = new Choqok::UI::UploadMediaDialog(nullptr, filename);
0091     dlg->show();
0092 }
0093 
0094 void DbusHandler::postText(const QString &text)
0095 {
0096     // Before posting text ensure QuickPost widget has been created otherwise wait for it.
0097     // This is necessary when choqok is launched by a D-Bus call, because it can happen
0098     //  that DBusHandler is ready, but QuickPost widget not yet.
0099     if (Choqok::UI::Global::quickPostWidget() == nullptr) {
0100         m_textToPost = QString(text);
0101         connect(Choqok::UI::Global::mainWindow(), &UI::MainWindow::quickPostCreated,
0102                 this, &DbusHandler::slotcreatedQuickPost);
0103         return;
0104     }
0105     if (Choqok::UI::Global::quickPostWidget()->isVisible()) {
0106         Choqok::UI::Global::quickPostWidget()->appendText(text);
0107     } else {
0108         Choqok::UI::Global::quickPostWidget()->setText(text);
0109     }
0110 }
0111 
0112 void DbusHandler::slotcreatedQuickPost()
0113 {
0114     if (Choqok::UI::Global::quickPostWidget()->isVisible()) {
0115         Choqok::UI::Global::quickPostWidget()->appendText(m_textToPost);
0116     } else {
0117         Choqok::UI::Global::quickPostWidget()->setText(m_textToPost);
0118     }
0119 }
0120 
0121 void DbusHandler::updateTimelines()
0122 {
0123     Choqok::UI::Global::mainWindow()->action("update_timeline")->trigger();
0124 }
0125 
0126 void DbusHandler::setShortening(bool flag)
0127 {
0128     Choqok::BehaviorSettings::setShortenOnPaste(flag);
0129 }
0130 
0131 bool DbusHandler::getShortening()
0132 {
0133     return Choqok::BehaviorSettings::shortenOnPaste();
0134 }
0135 
0136 DbusHandler *ChoqokDbus()
0137 {
0138     if (DbusHandler::m_self == nullptr) {
0139         DbusHandler::m_self = new DbusHandler();
0140     }
0141     return DbusHandler::m_self;
0142 }
0143 
0144 }
0145 
0146 #include "moc_dbushandler.cpp"