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

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