File indexing completed on 2024-05-05 04:57:31

0001 /*
0002     This file is part of Choqok, the KDE micro-blogging client
0003 
0004     SPDX-FileCopyrightText: 2010-2012 Andrey Esin <gmlastik@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 "imstatus.h"
0010 
0011 #include <QTimer>
0012 
0013 #include <KPluginFactory>
0014 
0015 #include "choqokuiglobal.h"
0016 #include "quickpost.h"
0017 
0018 #include "imqdbus.h"
0019 #include "imstatussettings.h"
0020 
0021 K_PLUGIN_CLASS_WITH_JSON(IMStatus, "choqok_imstatus.json")
0022 
0023 class IMStatusPrivate
0024 {
0025 public:
0026     IMStatusPrivate() {}
0027     IMQDBus *im;
0028 };
0029 
0030 IMStatus::IMStatus(QObject *parent, const QList<QVariant> &)
0031     : Choqok::Plugin(QLatin1String("choqok_imstatus"), parent), d(new IMStatusPrivate())
0032 {
0033     QTimer::singleShot(500, this, SLOT(update()));
0034     d->im = new IMQDBus(this);
0035 }
0036 
0037 IMStatus::~IMStatus()
0038 {
0039     delete d;
0040 }
0041 
0042 void IMStatus::update()
0043 {
0044     if (Choqok::UI::Global::quickPostWidget() != nullptr) {
0045         connect(Choqok::UI::Global::quickPostWidget(), &Choqok::UI::QuickPost::newPostSubmitted,
0046                 this, &IMStatus::slotIMStatus);
0047     } else {
0048         QTimer::singleShot(500, this, SLOT(update()));
0049     }
0050 }
0051 
0052 void IMStatus::slotIMStatus(Choqok::JobResult res, Choqok::Post *newPost)
0053 {
0054     if (res == Choqok::Success) {
0055         IMStatusSettings::self()->load();
0056         QString statusMessage = IMStatusSettings::templtate();
0057         statusMessage.replace(QLatin1String("%status%"), newPost->content, Qt::CaseInsensitive);
0058         statusMessage.replace(QLatin1String("%username%"), newPost->author.userName, Qt::CaseInsensitive);
0059         statusMessage.replace(QLatin1String("%fullname%"), newPost->author.realName, Qt::CaseInsensitive);
0060         statusMessage.replace(QLatin1String("%time%"), newPost->creationDateTime.toString(QLatin1String("hh:mm:ss")), Qt::CaseInsensitive);
0061         statusMessage.replace(QLatin1String("%url%"), newPost->link.toDisplayString(), Qt::CaseInsensitive);
0062         statusMessage.replace(QLatin1String("%client%"), QLatin1String("Choqok"), Qt::CaseInsensitive);
0063         if (!IMStatusSettings::repeat() && !newPost->repeatedFromUser.userName.isEmpty()) {
0064             return;
0065         }
0066         if (!IMStatusSettings::reply() && !newPost->replyToUser.userName.isEmpty()) {
0067             return;
0068         }
0069         d->im->updateStatusMessage(IMStatusSettings::imclient(), statusMessage);
0070     }
0071 }
0072 
0073 #include "imstatus.moc"
0074 #include "moc_imstatus.cpp"