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

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 "mastodoncomposerwidget.h"
0010 
0011 #include <QFileDialog>
0012 #include <QGridLayout>
0013 #include <QLabel>
0014 #include <QPushButton>
0015 #include <QSpacerItem>
0016 #include <QVBoxLayout>
0017 
0018 #include <KLocalizedString>
0019 
0020 #include "account.h"
0021 #include "choqoktextedit.h"
0022 #include "shortenmanager.h"
0023 
0024 #include "mastodondebug.h"
0025 #include "mastodonmicroblog.h"
0026 #include "mastodonpost.h"
0027 
0028 class MastodonComposerWidget::Private
0029 {
0030 public:
0031     QString mediumToAttach;
0032     QPushButton *btnAttach;
0033     QPointer<QLabel> mediumName;
0034     QPointer<QPushButton> btnCancel;
0035     QGridLayout *editorLayout;
0036 };
0037 
0038 MastodonComposerWidget::MastodonComposerWidget(Choqok::Account *account, QWidget *parent)
0039     : ComposerWidget(account, parent)
0040     , d(new Private)
0041 {
0042     d->editorLayout = qobject_cast<QGridLayout *>(editorContainer()->layout());
0043     d->btnAttach = new QPushButton(editorContainer());
0044     d->btnAttach->setIcon(QIcon::fromTheme(QLatin1String("mail-attachment")));
0045     d->btnAttach->setToolTip(i18n("Attach a file"));
0046     d->btnAttach->setMaximumWidth(d->btnAttach->height());
0047     connect(d->btnAttach, &QPushButton::clicked, this, &MastodonComposerWidget::attachMedia);
0048     QVBoxLayout *vLayout = new QVBoxLayout;
0049     vLayout->addWidget(d->btnAttach);
0050     vLayout->addSpacerItem(new QSpacerItem(1, 1, QSizePolicy::Preferred, QSizePolicy::MinimumExpanding));
0051     d->editorLayout->addItem(vLayout, 0, 1);
0052 }
0053 
0054 MastodonComposerWidget::~MastodonComposerWidget()
0055 {
0056     delete d;
0057 }
0058 
0059 void MastodonComposerWidget::submitPost(const QString &text)
0060 {
0061     qCDebug(CHOQOK);
0062     editorContainer()->setEnabled(false);
0063     QString txt = text;
0064     if (currentAccount()->postCharLimit() &&
0065             txt.size() > (int) currentAccount()->postCharLimit()) {
0066         txt = Choqok::ShortenManager::self()->parseText(txt);
0067     }
0068     setPostToSubmit(nullptr);
0069     setPostToSubmit(new Choqok::Post);
0070     postToSubmit()->content = txt;
0071     if (!replyToId.isEmpty()) {
0072         postToSubmit()->replyToPostId = replyToId;
0073     }
0074     connect(currentAccount()->microblog(), &Choqok::MicroBlog::postCreated, this,
0075             &MastodonComposerWidget::slotPostSubmited);
0076     connect(currentAccount()->microblog(), &Choqok::MicroBlog::errorPost, this,
0077             &MastodonComposerWidget::slotErrorPost);
0078     btnAbort = new QPushButton(QIcon::fromTheme(QLatin1String("dialog-cancel")), i18n("Abort"), this);
0079     layout()->addWidget(btnAbort);
0080     connect(btnAbort, &QPushButton::clicked, this, &MastodonComposerWidget::abort);
0081 
0082     MastodonMicroBlog *mBlog = qobject_cast<MastodonMicroBlog * >(currentAccount()->microblog());
0083     if (d->mediumToAttach.isEmpty()) {
0084         if (replyToId.isEmpty()) {
0085             currentAccount()->microblog()->createPost(currentAccount(), postToSubmit());
0086         } else {
0087             // WTF? It seems we cannot cast postToSubmit to MastodonPost and then I'm copying its attributes
0088             MastodonPost *pumpPost = new MastodonPost();
0089             pumpPost->content = postToSubmit()->content;
0090             pumpPost->replyToPostId = postToSubmit()->replyToPostId;
0091             setPostToSubmit(pumpPost);
0092 
0093             mBlog->createReply(currentAccount(), pumpPost);
0094         }
0095     } else {
0096         // TODO
0097         // mBlog->createPostWithMedia(currentAccount(), postToSubmit(), d->mediumToAttach);
0098     }
0099 }
0100 
0101 void MastodonComposerWidget::slotPostSubmited(Choqok::Account *theAccount, Choqok::Post *post)
0102 {
0103     qCDebug(CHOQOK);
0104     if (currentAccount() == theAccount && post == postToSubmit()) {
0105         qCDebug(CHOQOK) << "Accepted";
0106         disconnect(currentAccount()->microblog(), &Choqok::MicroBlog::postCreated,
0107                    this, &MastodonComposerWidget::slotPostSubmited);
0108         disconnect(currentAccount()->microblog(), &Choqok::MicroBlog::errorPost,
0109                    this, &MastodonComposerWidget::slotErrorPost);
0110         if (btnAbort) {
0111             btnAbort->deleteLater();
0112         }
0113         editor()->clear();
0114         editorCleared();
0115         editorContainer()->setEnabled(true);
0116         setPostToSubmit(nullptr);
0117         cancelAttach();
0118         currentAccount()->microblog()->updateTimelines(currentAccount());
0119     }
0120 }
0121 
0122 void MastodonComposerWidget::attachMedia()
0123 {
0124     qCDebug(CHOQOK);
0125     d->mediumToAttach = QFileDialog::getOpenFileName(this, i18n("Select Media to Upload"),
0126                                                      QString(), QStringLiteral("Images"));
0127     if (d->mediumToAttach.isEmpty()) {
0128         qCDebug(CHOQOK) << "No file selected";
0129         return;
0130     }
0131     const QString fileName = QUrl(d->mediumToAttach).fileName();
0132     if (!d->mediumName) {
0133         d->mediumName = new QLabel(editorContainer());
0134         d->btnCancel = new QPushButton(editorContainer());
0135         d->btnCancel->setIcon(QIcon::fromTheme(QLatin1String("list-remove")));
0136         d->btnCancel->setToolTip(i18n("Discard Attachment"));
0137         d->btnCancel->setMaximumWidth(d->btnCancel->height());
0138         connect(d->btnCancel, &QPushButton::clicked, this, &MastodonComposerWidget::cancelAttach);
0139 
0140         d->editorLayout->addWidget(d->mediumName, 1, 0);
0141         d->editorLayout->addWidget(d->btnCancel, 1, 1);
0142     }
0143     d->mediumName->setText(i18n("Attaching <b>%1</b>", fileName));
0144     editor()->setFocus();
0145 }
0146 
0147 void MastodonComposerWidget::cancelAttach()
0148 {
0149     qCDebug(CHOQOK);
0150     delete d->mediumName;
0151     d->mediumName = nullptr;
0152     delete d->btnCancel;
0153     d->btnCancel = nullptr;
0154     d->mediumToAttach.clear();
0155 }
0156 
0157 #include "moc_mastodoncomposerwidget.cpp"