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

0001 /*
0002     This file is part of Choqok, the KDE micro-blogging client
0003 
0004     SPDX-FileCopyrightText: 2008-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 "gnusocialapicomposerwidget.h"
0010 
0011 #include <QFileDialog>
0012 #include <QLabel>
0013 #include <QLayout>
0014 #include <QPointer>
0015 #include <QPushButton>
0016 
0017 #include <KLocalizedString>
0018 
0019 #include "account.h"
0020 #include "choqoktextedit.h"
0021 #include "microblog.h"
0022 #include "notifymanager.h"
0023 #include "shortenmanager.h"
0024 
0025 #include "twitterapitextedit.h"
0026 
0027 #include "gnusocialapidebug.h"
0028 #include "gnusocialapimicroblog.h"
0029 
0030 class GNUSocialApiComposerWidget::Private
0031 {
0032 public:
0033     Private()
0034         : btnAttach(nullptr), mediumName(nullptr), btnCancel(nullptr)
0035     {}
0036     QString mediumToAttach;
0037     QPushButton *btnAttach;
0038     QPointer<QLabel> mediumName;
0039     QPointer<QPushButton> btnCancel;
0040     QGridLayout *editorLayout;
0041 };
0042 
0043 GNUSocialApiComposerWidget::GNUSocialApiComposerWidget(Choqok::Account *account, QWidget *parent)
0044     : TwitterApiComposerWidget(account, parent), d(new Private)
0045 {
0046     d->editorLayout = qobject_cast<QGridLayout *>(editorContainer()->layout());
0047     d->btnAttach = new QPushButton(editorContainer());
0048     d->btnAttach->setIcon(QIcon::fromTheme(QLatin1String("mail-attachment")));
0049     d->btnAttach->setToolTip(i18n("Attach a file"));
0050     d->btnAttach->setMaximumWidth(d->btnAttach->height());
0051     connect(d->btnAttach, &QPushButton::clicked, this, &GNUSocialApiComposerWidget::selectMediumToAttach);
0052     QVBoxLayout *vLayout = new QVBoxLayout;
0053     vLayout->addWidget(d->btnAttach);
0054     vLayout->addSpacerItem(new QSpacerItem(1, 1, QSizePolicy::Preferred, QSizePolicy::MinimumExpanding));
0055     d->editorLayout->addItem(vLayout, 0, 1, 1, 1);
0056 
0057     connect(account, &Choqok::Account::modified, this, &GNUSocialApiComposerWidget::slotRebuildEditor);
0058 }
0059 
0060 GNUSocialApiComposerWidget::~GNUSocialApiComposerWidget()
0061 {
0062     delete d;
0063 }
0064 
0065 void GNUSocialApiComposerWidget::submitPost(const QString &txt)
0066 {
0067     if (d->mediumToAttach.isEmpty()) {
0068         Choqok::UI::ComposerWidget::submitPost(txt);
0069     } else {
0070         qCDebug(CHOQOK);
0071         editorContainer()->setEnabled(false);
0072         QString text = txt;
0073         if (currentAccount()->postCharLimit() &&
0074                 text.size() > (int)currentAccount()->postCharLimit()) {
0075             text = Choqok::ShortenManager::self()->parseText(text);
0076         }
0077         setPostToSubmit(nullptr);
0078         setPostToSubmit(new Choqok::Post);
0079         postToSubmit()->content = text;
0080         if (!replyToId.isEmpty()) {
0081             postToSubmit()->replyToPostId = replyToId;
0082         }
0083         connect(currentAccount()->microblog(), &Choqok::MicroBlog::postCreated,
0084                 this, &GNUSocialApiComposerWidget::slotPostMediaSubmitted);
0085         connect(currentAccount()->microblog(), &Choqok::MicroBlog::errorPost,
0086                 this, &GNUSocialApiComposerWidget::slotErrorPost);
0087         btnAbort = new QPushButton(QIcon::fromTheme(QLatin1String("dialog-cancel")), i18n("Abort"), this);
0088         layout()->addWidget(btnAbort);
0089         connect(btnAbort, &QPushButton::clicked, this, &Choqok::UI::ComposerWidget::abort);
0090         GNUSocialApiMicroBlog *mBlog = qobject_cast<GNUSocialApiMicroBlog *>(currentAccount()->microblog());
0091         mBlog->createPostWithAttachment(currentAccount(), postToSubmit(), d->mediumToAttach);
0092     }
0093 }
0094 
0095 void GNUSocialApiComposerWidget::slotPostMediaSubmitted(Choqok::Account *theAccount, Choqok::Post *post)
0096 {
0097     qCDebug(CHOQOK);
0098     if (currentAccount() == theAccount && post == postToSubmit()) {
0099         qCDebug(CHOQOK) << "Accepted";
0100         disconnect(currentAccount()->microblog(), &Choqok::MicroBlog::postCreated,
0101                    this, &GNUSocialApiComposerWidget::slotPostMediaSubmitted);
0102         disconnect(currentAccount()->microblog(), &Choqok::MicroBlog::errorPost,
0103                    this, &GNUSocialApiComposerWidget::slotErrorPost);
0104         if (btnAbort) {
0105             btnAbort->deleteLater();
0106         }
0107         Choqok::NotifyManager::success(i18n("New post for account %1 submitted successfully",
0108                                             theAccount->alias()));
0109         editor()->clear();
0110         replyToId.clear();
0111         editorContainer()->setEnabled(true);
0112         setPostToSubmit(nullptr);
0113         cancelAttachMedium();
0114         currentAccount()->microblog()->updateTimelines(currentAccount());
0115     }
0116 }
0117 
0118 void GNUSocialApiComposerWidget::selectMediumToAttach()
0119 {
0120     qCDebug(CHOQOK);
0121     d->mediumToAttach = QFileDialog::getOpenFileName(this, i18n("Select Media to Upload"),
0122                                                      QString(), QStringLiteral("Images"));
0123     if (d->mediumToAttach.isEmpty()) {
0124         return;
0125     }
0126     QString fileName = QUrl(d->mediumToAttach).fileName();
0127     if (!d->mediumName) {
0128         qCDebug(CHOQOK) << fileName;
0129         d->mediumName = new QLabel(editorContainer());
0130         d->btnCancel = new QPushButton(editorContainer());
0131         d->btnCancel->setIcon(QIcon::fromTheme(QLatin1String("list-remove")));
0132         d->btnCancel->setToolTip(i18n("Discard Attachment"));
0133         d->btnCancel->setMaximumWidth(d->btnCancel->height());
0134         connect(d->btnCancel, &QPushButton::clicked, this, &GNUSocialApiComposerWidget::cancelAttachMedium);
0135 
0136         d->editorLayout->addWidget(d->mediumName, 1, 0);
0137         d->editorLayout->addWidget(d->btnCancel, 1, 1);
0138     }
0139     d->mediumName->setText(i18n("Attaching <b>%1</b>", fileName));
0140     editor()->setFocus();
0141 }
0142 
0143 void GNUSocialApiComposerWidget::cancelAttachMedium()
0144 {
0145     qCDebug(CHOQOK);
0146     delete d->mediumName;
0147     d->mediumName = nullptr;
0148     delete d->btnCancel;
0149     d->btnCancel = nullptr;
0150     d->mediumToAttach.clear();
0151 }
0152 
0153 void GNUSocialApiComposerWidget::slotRebuildEditor(Choqok::Account *theAccount)
0154 {
0155     setEditor(new TwitterApiTextEdit(theAccount, this));
0156 }
0157 
0158 #include "moc_gnusocialapicomposerwidget.cpp"