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

0001 /*
0002     This file is part of Choqok, the KDE micro-blogging client
0003 
0004     SPDX-FileCopyrightText: 2017 Andrea Scarpino <scarpino@kde.org>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0007 */
0008 
0009 #include "twittertextedit.h"
0010 
0011 #include <QJsonDocument>
0012 #include <QLabel>
0013 
0014 #include <KIO/Job>
0015 
0016 #include "urlutils.h"
0017 
0018 #include "twitterapiaccount.h"
0019 #include "twitterapimicroblog.h"
0020 
0021 #include "twitterdebug.h"
0022 
0023 class TwitterTextEdit::Private
0024 {
0025 public:
0026     Private(Choqok::Account *theAccount)
0027         : acc(theAccount), tCoMaximumLength(0), tCoMaximumLengthHttps(0)
0028     {}
0029     Choqok::Account *acc;
0030     int tCoMaximumLength;
0031     int tCoMaximumLengthHttps;
0032 };
0033 
0034 TwitterTextEdit::TwitterTextEdit(Choqok::Account *theAccount, QWidget *parent)
0035     : TwitterApiTextEdit(theAccount, parent), d(new Private(theAccount))
0036 {
0037     qCDebug(CHOQOK);
0038     fetchTCoMaximumLength();
0039 }
0040 
0041 TwitterTextEdit::~TwitterTextEdit()
0042 {
0043     delete d;
0044 }
0045 
0046 void TwitterTextEdit::updateRemainingCharsCount()
0047 {
0048     QString txt = this->toPlainText();
0049     int count = txt.count();
0050     if (count) {
0051         lblRemainChar->show();
0052         if (charLimit()) {
0053             int remain = charLimit() - count;
0054 
0055             for (const QString &url: UrlUtils::detectUrls(txt)) {
0056                 // Twitter does not wrapps urls with login information
0057                 if (!url.contains(QLatin1Char('@'))) {
0058                     int diff = -1;
0059                     if (url.startsWith(QLatin1String("http://"))) {
0060                         diff = url.length() - d->tCoMaximumLength;
0061                     } else if (url.startsWith(QLatin1String("https://"))) {
0062                         diff = url.length() - d->tCoMaximumLengthHttps;
0063                     }
0064 
0065                     if (diff > 0) {
0066                         remain += diff;
0067                     }
0068                 }
0069             }
0070 
0071             if (remain < 0) {
0072                 lblRemainChar->setStyleSheet(QLatin1String("QLabel {color: red;}"));
0073             } else if (remain < 30) {
0074                 lblRemainChar->setStyleSheet(QLatin1String("QLabel {color: rgb(242, 179, 19);}"));
0075             } else {
0076                 lblRemainChar->setStyleSheet(QLatin1String("QLabel {color: green;}"));
0077             }
0078             lblRemainChar->setText(QString::number(remain));
0079         } else {
0080             lblRemainChar->setText(QString::number(count));
0081             lblRemainChar->setStyleSheet(QLatin1String(QLatin1String("QLabel {color: blue;}")));
0082         }
0083         txt.remove(QRegExp(QLatin1String("@([^\\s\\W]+)")));
0084         txt = txt.trimmed();
0085         if (firstChar() != txt[0]) {
0086             setFirstChar(txt[0]);
0087             txt.prepend(QLatin1Char(' '));
0088             QTextBlockFormat f;
0089             f.setLayoutDirection((Qt::LayoutDirection) txt.isRightToLeft());
0090             textCursor().mergeBlockFormat(f);
0091         }
0092     } else {
0093         lblRemainChar->hide();
0094     }
0095 }
0096 
0097 void TwitterTextEdit::fetchTCoMaximumLength()
0098 {
0099     TwitterApiAccount *acc = qobject_cast<TwitterApiAccount *>(d->acc);
0100     if (acc) {
0101         QUrl url = acc->apiUrl();
0102         url.setPath(url.path() + QLatin1String("/help/configuration.json"));
0103 
0104         KIO::StoredTransferJob *job = KIO::storedGet(url, KIO::Reload, KIO::HideProgressInfo);
0105         if (!job) {
0106             qCDebug(CHOQOK) << "Cannot create an http GET request!";
0107             return;
0108         }
0109         TwitterApiMicroBlog *mBlog = qobject_cast<TwitterApiMicroBlog *>(acc->microblog());
0110         job->addMetaData(QStringLiteral("customHTTPHeader"),
0111                          QStringLiteral("Authorization: ") +
0112                          QLatin1String(mBlog->authorizationHeader(acc, url, QNetworkAccessManager::GetOperation)));
0113         connect(job, &KIO::StoredTransferJob::result, this, &TwitterTextEdit::slotTCoMaximumLength);
0114         job->start();
0115     } else {
0116         qCDebug(CHOQOK) << "the account is not a TwitterAPIAccount!";
0117     }
0118 }
0119 
0120 void TwitterTextEdit::slotTCoMaximumLength(KJob *job)
0121 {
0122     if (job->error()) {
0123         qCDebug(CHOQOK) << "Job Error:" << job->errorString();
0124     } else {
0125         KIO::StoredTransferJob *j = qobject_cast<KIO::StoredTransferJob * >(job);
0126         const QJsonDocument json = QJsonDocument::fromJson(j->data());
0127         if (!json.isNull()) {
0128             const QVariantMap reply = json.toVariant().toMap();
0129             d->tCoMaximumLength = reply[QLatin1String("short_url_length")].toInt();
0130             d->tCoMaximumLengthHttps = reply[QLatin1String("short_url_length_https")].toInt();
0131         } else {
0132             qCDebug(CHOQOK) << "Cannot parse JSON reply";
0133         }
0134     }
0135 }
0136 
0137 #include "moc_twittertextedit.cpp"