1 /*
2 This file is part of Choqok, the KDE micro-blogging client
3
4 Copyright (C) 2008-2012 Mehrdad Momeny <mehrdad.momeny@gmail.com>
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of
9 the License or (at your option) version 3 or any later version
10 accepted by the membership of KDE e.V. (or its successor approved
11 by the membership of KDE e.V.), which shall act as a proxy
12 defined in Section 14 of version 3 of the license.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, see http://www.gnu.org/licenses/
21
22 */
23
24 #include "gnusocialapicomposerwidget.h"
25
26 #include <QFileDialog>
27 #include <QLabel>
28 #include <QLayout>
29 #include <QPointer>
30 #include <QPushButton>
31
32 #include <KLocalizedString>
33
34 #include "account.h"
35 #include "choqoktextedit.h"
36 #include "microblog.h"
37 #include "notifymanager.h"
38 #include "shortenmanager.h"
39
40 #include "twitterapitextedit.h"
41
42 #include "gnusocialapidebug.h"
43 #include "gnusocialapimicroblog.h"
44
45 class GNUSocialApiComposerWidget::Private
46 {
47 public:
Private()48 Private()
49 : btnAttach(nullptr), mediumName(nullptr), btnCancel(nullptr)
50 {}
51 QString mediumToAttach;
52 QPushButton *btnAttach;
53 QPointer<QLabel> mediumName;
54 QPointer<QPushButton> btnCancel;
55 QGridLayout *editorLayout;
56 };
57
GNUSocialApiComposerWidget(Choqok::Account * account,QWidget * parent)58 GNUSocialApiComposerWidget::GNUSocialApiComposerWidget(Choqok::Account *account, QWidget *parent)
59 : TwitterApiComposerWidget(account, parent), d(new Private)
60 {
61 d->editorLayout = qobject_cast<QGridLayout *>(editorContainer()->layout());
62 d->btnAttach = new QPushButton(editorContainer());
63 d->btnAttach->setIcon(QIcon::fromTheme(QLatin1String("mail-attachment")));
64 d->btnAttach->setToolTip(i18n("Attach a file"));
65 d->btnAttach->setMaximumWidth(d->btnAttach->height());
66 connect(d->btnAttach, &QPushButton::clicked, this, &GNUSocialApiComposerWidget::selectMediumToAttach);
67 QVBoxLayout *vLayout = new QVBoxLayout;
68 vLayout->addWidget(d->btnAttach);
69 vLayout->addSpacerItem(new QSpacerItem(1, 1, QSizePolicy::Preferred, QSizePolicy::MinimumExpanding));
70 d->editorLayout->addItem(vLayout, 0, 1, 1, 1);
71
72 connect(account, &Choqok::Account::modified, this, &GNUSocialApiComposerWidget::slotRebuildEditor);
73 }
74
~GNUSocialApiComposerWidget()75 GNUSocialApiComposerWidget::~GNUSocialApiComposerWidget()
76 {
77 delete d;
78 }
79
submitPost(const QString & txt)80 void GNUSocialApiComposerWidget::submitPost(const QString &txt)
81 {
82 if (d->mediumToAttach.isEmpty()) {
83 Choqok::UI::ComposerWidget::submitPost(txt);
84 } else {
85 qCDebug(CHOQOK);
86 editorContainer()->setEnabled(false);
87 QString text = txt;
88 if (currentAccount()->postCharLimit() &&
89 text.size() > (int)currentAccount()->postCharLimit()) {
90 text = Choqok::ShortenManager::self()->parseText(text);
91 }
92 setPostToSubmit(nullptr);
93 setPostToSubmit(new Choqok::Post);
94 postToSubmit()->content = text;
95 if (!replyToId.isEmpty()) {
96 postToSubmit()->replyToPostId = replyToId;
97 }
98 connect(currentAccount()->microblog(), &Choqok::MicroBlog::postCreated,
99 this, &GNUSocialApiComposerWidget::slotPostMediaSubmitted);
100 connect(currentAccount()->microblog(), &Choqok::MicroBlog::errorPost,
101 this, &GNUSocialApiComposerWidget::slotErrorPost);
102 btnAbort = new QPushButton(QIcon::fromTheme(QLatin1String("dialog-cancel")), i18n("Abort"), this);
103 layout()->addWidget(btnAbort);
104 connect(btnAbort, &QPushButton::clicked, this, &Choqok::UI::ComposerWidget::abort);
105 GNUSocialApiMicroBlog *mBlog = qobject_cast<GNUSocialApiMicroBlog *>(currentAccount()->microblog());
106 mBlog->createPostWithAttachment(currentAccount(), postToSubmit(), d->mediumToAttach);
107 }
108 }
109
slotPostMediaSubmitted(Choqok::Account * theAccount,Choqok::Post * post)110 void GNUSocialApiComposerWidget::slotPostMediaSubmitted(Choqok::Account *theAccount, Choqok::Post *post)
111 {
112 qCDebug(CHOQOK);
113 if (currentAccount() == theAccount && post == postToSubmit()) {
114 qCDebug(CHOQOK) << "Accepted";
115 disconnect(currentAccount()->microblog(), &Choqok::MicroBlog::postCreated,
116 this, &GNUSocialApiComposerWidget::slotPostMediaSubmitted);
117 disconnect(currentAccount()->microblog(), &Choqok::MicroBlog::errorPost,
118 this, &GNUSocialApiComposerWidget::slotErrorPost);
119 if (btnAbort) {
120 btnAbort->deleteLater();
121 }
122 Choqok::NotifyManager::success(i18n("New post for account %1 submitted successfully",
123 theAccount->alias()));
124 editor()->clear();
125 replyToId.clear();
126 editorContainer()->setEnabled(true);
127 setPostToSubmit(nullptr);
128 cancelAttachMedium();
129 currentAccount()->microblog()->updateTimelines(currentAccount());
130 }
131 }
132
selectMediumToAttach()133 void GNUSocialApiComposerWidget::selectMediumToAttach()
134 {
135 qCDebug(CHOQOK);
136 d->mediumToAttach = QFileDialog::getOpenFileName(this, i18n("Select Media to Upload"),
137 QString(), QStringLiteral("Images"));
138 if (d->mediumToAttach.isEmpty()) {
139 return;
140 }
141 QString fileName = QUrl(d->mediumToAttach).fileName();
142 if (!d->mediumName) {
143 qCDebug(CHOQOK) << fileName;
144 d->mediumName = new QLabel(editorContainer());
145 d->btnCancel = new QPushButton(editorContainer());
146 d->btnCancel->setIcon(QIcon::fromTheme(QLatin1String("list-remove")));
147 d->btnCancel->setToolTip(i18n("Discard Attachment"));
148 d->btnCancel->setMaximumWidth(d->btnCancel->height());
149 connect(d->btnCancel, &QPushButton::clicked, this, &GNUSocialApiComposerWidget::cancelAttachMedium);
150
151 d->editorLayout->addWidget(d->mediumName, 1, 0);
152 d->editorLayout->addWidget(d->btnCancel, 1, 1);
153 }
154 d->mediumName->setText(i18n("Attaching <b>%1</b>", fileName));
155 editor()->setFocus();
156 }
157
cancelAttachMedium()158 void GNUSocialApiComposerWidget::cancelAttachMedium()
159 {
160 qCDebug(CHOQOK);
161 delete d->mediumName;
162 d->mediumName = nullptr;
163 delete d->btnCancel;
164 d->btnCancel = nullptr;
165 d->mediumToAttach.clear();
166 }
167
slotRebuildEditor(Choqok::Account * theAccount)168 void GNUSocialApiComposerWidget::slotRebuildEditor(Choqok::Account *theAccount)
169 {
170 setEditor(new TwitterApiTextEdit(theAccount, this));
171 }
172
173