File indexing completed on 2024-04-28 04:55:46
0001 /* 0002 This file is part of Choqok, the KDE micro-blogging client 0003 0004 SPDX-FileCopyrightText: 2013-2014 Andrea Scarpino <scarpino@kde.org> 0005 SPDX-FileCopyrightText: 2008-2012 Mehrdad Momeny <mehrdad.momeny@gmail.com> 0006 0007 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0008 */ 0009 0010 #include "pumpiocomposerwidget.h" 0011 0012 #include <QFileDialog> 0013 #include <QGridLayout> 0014 #include <QLabel> 0015 #include <QPushButton> 0016 #include <QSpacerItem> 0017 #include <QVBoxLayout> 0018 0019 #include <KLocalizedString> 0020 0021 #include "account.h" 0022 #include "choqoktextedit.h" 0023 #include "shortenmanager.h" 0024 0025 #include "pumpiodebug.h" 0026 #include "pumpiomicroblog.h" 0027 #include "pumpiopost.h" 0028 0029 class PumpIOComposerWidget::Private 0030 { 0031 public: 0032 QString mediumToAttach; 0033 QPushButton *btnAttach; 0034 QPointer<QLabel> mediumName; 0035 QPointer<QPushButton> btnCancel; 0036 QGridLayout *editorLayout; 0037 QString replyToObjectType; 0038 }; 0039 0040 PumpIOComposerWidget::PumpIOComposerWidget(Choqok::Account *account, QWidget *parent) 0041 : ComposerWidget(account, parent) 0042 , d(new Private) 0043 { 0044 d->editorLayout = qobject_cast<QGridLayout *>(editorContainer()->layout()); 0045 d->btnAttach = new QPushButton(editorContainer()); 0046 d->btnAttach->setIcon(QIcon::fromTheme(QLatin1String("mail-attachment"))); 0047 d->btnAttach->setToolTip(i18n("Attach a file")); 0048 d->btnAttach->setMaximumWidth(d->btnAttach->height()); 0049 connect(d->btnAttach, &QPushButton::clicked, this, &PumpIOComposerWidget::attachMedia); 0050 QVBoxLayout *vLayout = new QVBoxLayout; 0051 vLayout->addWidget(d->btnAttach); 0052 vLayout->addSpacerItem(new QSpacerItem(1, 1, QSizePolicy::Preferred, QSizePolicy::MinimumExpanding)); 0053 d->editorLayout->addItem(vLayout, 0, 1); 0054 } 0055 0056 PumpIOComposerWidget::~PumpIOComposerWidget() 0057 { 0058 delete d; 0059 } 0060 0061 void PumpIOComposerWidget::submitPost(const QString &text) 0062 { 0063 qCDebug(CHOQOK); 0064 editorContainer()->setEnabled(false); 0065 QString txt = text; 0066 if (currentAccount()->postCharLimit() && 0067 txt.size() > (int) currentAccount()->postCharLimit()) { 0068 txt = Choqok::ShortenManager::self()->parseText(txt); 0069 } 0070 setPostToSubmit(nullptr); 0071 setPostToSubmit(new Choqok::Post); 0072 postToSubmit()->content = txt; 0073 if (!replyToId.isEmpty()) { 0074 postToSubmit()->replyToPostId = replyToId; 0075 } 0076 connect(currentAccount()->microblog(), &Choqok::MicroBlog::postCreated, this, 0077 &PumpIOComposerWidget::slotPostSubmited); 0078 connect(currentAccount()->microblog(), &Choqok::MicroBlog::errorPost, this, 0079 &PumpIOComposerWidget::slotErrorPost); 0080 btnAbort = new QPushButton(QIcon::fromTheme(QLatin1String("dialog-cancel")), i18n("Abort"), this); 0081 layout()->addWidget(btnAbort); 0082 connect(btnAbort, &QPushButton::clicked, this, &PumpIOComposerWidget::abort); 0083 0084 PumpIOMicroBlog *mBlog = qobject_cast<PumpIOMicroBlog * >(currentAccount()->microblog()); 0085 if (d->mediumToAttach.isEmpty()) { 0086 if (replyToId.isEmpty()) { 0087 currentAccount()->microblog()->createPost(currentAccount(), postToSubmit()); 0088 } else { 0089 // WTF? It seems we cannot cast postToSubmit to PumpIOPost and then I'm copying its attributes 0090 PumpIOPost *pumpPost = new PumpIOPost(); 0091 pumpPost->content = postToSubmit()->content; 0092 pumpPost->replyToPostId = postToSubmit()->replyToPostId; 0093 pumpPost->replyToObjectType = d->replyToObjectType; 0094 setPostToSubmit(pumpPost); 0095 0096 mBlog->createReply(currentAccount(), pumpPost); 0097 } 0098 } else { 0099 mBlog->createPostWithMedia(currentAccount(), postToSubmit(), d->mediumToAttach); 0100 } 0101 } 0102 0103 void PumpIOComposerWidget::slotPostSubmited(Choqok::Account *theAccount, Choqok::Post *post) 0104 { 0105 qCDebug(CHOQOK); 0106 if (currentAccount() == theAccount && post == postToSubmit()) { 0107 qCDebug(CHOQOK) << "Accepted"; 0108 disconnect(currentAccount()->microblog(), &Choqok::MicroBlog::postCreated, 0109 this, &PumpIOComposerWidget::slotPostSubmited); 0110 disconnect(currentAccount()->microblog(), &Choqok::MicroBlog::errorPost, 0111 this, &PumpIOComposerWidget::slotErrorPost); 0112 if (btnAbort) { 0113 btnAbort->deleteLater(); 0114 } 0115 editor()->clear(); 0116 editorCleared(); 0117 editorContainer()->setEnabled(true); 0118 setPostToSubmit(nullptr); 0119 cancelAttach(); 0120 currentAccount()->microblog()->updateTimelines(currentAccount()); 0121 } 0122 } 0123 0124 void PumpIOComposerWidget::attachMedia() 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 qCDebug(CHOQOK) << "No file selected"; 0131 return; 0132 } 0133 const QString fileName = QUrl(d->mediumToAttach).fileName(); 0134 if (!d->mediumName) { 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, &PumpIOComposerWidget::cancelAttach); 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 PumpIOComposerWidget::cancelAttach() 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 void PumpIOComposerWidget::slotSetReply(const QString replyToId, const QString replyToUsername, const QString replyToObjectType) 0160 { 0161 qCDebug(CHOQOK); 0162 this->replyToId = replyToId; 0163 this->replyToUsername = replyToUsername; 0164 d->replyToObjectType = replyToObjectType; 0165 0166 if (!replyToUsername.isEmpty()) { 0167 replyToUsernameLabel()->setText(i18n("Replying to <b>%1</b>", replyToUsername)); 0168 btnCancelReply()->show(); 0169 replyToUsernameLabel()->show(); 0170 } 0171 editor()->setFocus(); 0172 } 0173 0174 #include "moc_pumpiocomposerwidget.cpp"