addeditblog.cpp 24.6 KB
Newer Older
Mehrdad Momeny's avatar
Mehrdad Momeny committed
1 2 3
/*
    This file is part of Blogilo, A KDE Blogging Client

4 5
    Copyright (C) 2008-2010 Mehrdad Momeny <mehrdad.momeny@gmail.com>
    Copyright (C) 2008-2010 Golnaz Nilieh <g382nilieh@gmail.com>
Mehrdad Momeny's avatar
Mehrdad Momeny committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of
    the License or (at your option) version 3 or any later version
    accepted by the membership of KDE e.V. (or its successor approved
    by the membership of KDE e.V.), which shall act as a proxy
    defined in Section 14 of version 3 of the license.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, see http://www.gnu.org/licenses/
*/
23

Mehrdad Momeny's avatar
Mehrdad Momeny committed
24
#include "addeditblog.h"
Laurent Montel's avatar
Laurent Montel committed
25 26 27
#include "waitwidget.h"
#include "bilboblog.h"
#include "dbman.h"
28

Laurent Montel's avatar
Laurent Montel committed
29 30 31 32
#include <kblog/blogger1.h>
#include <kblog/metaweblog.h>
#include <kblog/movabletype.h>
#include <kblog/wordpressbuggy.h>
Laurent Montel's avatar
Laurent Montel committed
33
#ifdef HAVE_GAPIBLOGGER_SUPPORT
34
#include "blogger.h"
Laurent Montel's avatar
Laurent Montel committed
35
#endif
36
#include <kmessagebox.h>
Laurent Montel's avatar
Laurent Montel committed
37
#include "blogilo_debug.h"
38 39
#include <kio/jobclasses.h>
#include <kio/job.h>
Laurent Montel's avatar
Laurent Montel committed
40
#include <KLocalizedString>
41 42

#include <QTableWidget>
Mehrdad Momeny's avatar
Mehrdad Momeny committed
43
#include <QTimer>
Laurent Montel's avatar
Laurent Montel committed
44
#include <KConfigGroup>
45 46
static const int TIMEOUT = 45000;

47 48 49 50
class AddEditBlog::Private
{
public:
    Private()
Laurent Montel's avatar
Laurent Montel committed
51
        : mainW(Q_NULLPTR),
Laurent Montel's avatar
Minor  
Laurent Montel committed
52
          isNewBlog(false),
Laurent Montel's avatar
Laurent Montel committed
53 54 55 56 57
          bBlog(Q_NULLPTR),
          mBlog(Q_NULLPTR),
          mFetchProfileIdTimer(Q_NULLPTR),
          mFetchBlogIdTimer(Q_NULLPTR),
          mFetchAPITimer(Q_NULLPTR),
Laurent Montel's avatar
Laurent Montel committed
58 59
          wait(Q_NULLPTR),
          okButton(Q_NULLPTR)
60 61
    {}
    Ui::AddEditBlogBase ui;
Laurent Montel's avatar
Laurent Montel committed
62
    QTabWidget *mainW;
63 64 65
    bool isNewBlog;
    BilboBlog *bBlog;
    KBlog::Blog *mBlog;
Laurent Montel's avatar
Laurent Montel committed
66 67 68
    QTimer *mFetchProfileIdTimer;
    QTimer *mFetchBlogIdTimer;
    QTimer *mFetchAPITimer;
69 70
    WaitWidget *wait;
    QString tmpBlogUrl;
Laurent Montel's avatar
Laurent Montel committed
71
    QPushButton *okButton;
72 73
};

Laurent Montel's avatar
Laurent Montel committed
74 75
AddEditBlog::AddEditBlog(int blog_id, QWidget *parent)
    : QDialog(parent), d(new Private)
76
{
Laurent Montel's avatar
Laurent Montel committed
77
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
Laurent Montel's avatar
Laurent Montel committed
78 79
    d->mainW = new QTabWidget(this);
    d->ui.setupUi(d->mainW);
Laurent Montel's avatar
Laurent Montel committed
80 81 82 83 84 85 86 87 88 89 90
    mainLayout->addWidget(d->mainW);

    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    mainLayout->addWidget(buttonBox);

    d->okButton = buttonBox->button(QDialogButtonBox::Ok);
    d->okButton->setDefault(true);
    d->okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
    connect(buttonBox, &QDialogButtonBox::accepted, this, &AddEditBlog::slotAccepted);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &AddEditBlog::reject);

91
    d->isNewBlog = true;
Laurent Montel's avatar
Laurent Montel committed
92
    d->mFetchAPITimer = d->mFetchBlogIdTimer = d->mFetchProfileIdTimer = Q_NULLPTR;
93

94 95
    connect(d->ui.txtId, &QLineEdit::textChanged, this, &AddEditBlog::enableOkButton);
    connect(d->ui.txtUrl, &QLineEdit::textChanged, this, &AddEditBlog::enableAutoConfBtn);
Laurent Montel's avatar
Laurent Montel committed
96
    connect(d->ui.txtUser, &QLineEdit::textChanged, this, &AddEditBlog::enableAutoConfBtn);
97
    connect(d->ui.txtPass, &QLineEdit::textChanged, this, &AddEditBlog::enableAutoConfBtn);
Laurent Montel's avatar
Laurent Montel committed
98 99
    connect(d->ui.btnAutoConf, &QPushButton::clicked, this, &AddEditBlog::autoConfigure);
    connect(d->ui.btnFetch, &QPushButton::clicked, this, &AddEditBlog::fetchBlogId);
100 101
    connect(d->ui.comboApi, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &AddEditBlog::slotComboApiChanged);
    connect(d->ui.txtUrl, &QLineEdit::returnPressed, this, &AddEditBlog::slotReturnPressed);
Laurent Montel's avatar
Laurent Montel committed
102
    connect(d->ui.txtUser, &QLineEdit::returnPressed, this, &AddEditBlog::slotReturnPressed);
103 104
    connect(d->ui.txtPass, &QLineEdit::returnPressed, this, &AddEditBlog::slotReturnPressed);
    connect(d->ui.txtId, &QLineEdit::returnPressed, this, &AddEditBlog::slotReturnPressed);
105

Laurent Montel's avatar
Laurent Montel committed
106 107
    if (blog_id > -1) {
        setWindowTitle(i18n("Edit blog settings"));
Laurent Montel's avatar
Laurent Montel committed
108
        d->okButton->setEnabled(true);
Laurent Montel's avatar
Laurent Montel committed
109 110
        d->ui.btnFetch->setEnabled(true);
        d->ui.btnAutoConf->setEnabled(true);
111
        d->isNewBlog = false;
Laurent Montel's avatar
Laurent Montel committed
112 113 114 115 116
        d->bBlog =  DBMan::self()->blog(blog_id);
        d->ui.txtUrl->setText(d->bBlog->url().url());
        d->ui.txtUser->setText(d->bBlog->username());
        d->ui.txtPass->setText(d->bBlog->password());
        d->ui.txtId->setText(d->bBlog->blogid());
117
        QString title = d->bBlog->title();
Laurent Montel's avatar
Laurent Montel committed
118
        title = title.replace(QLatin1String("&amp;"), QStringLiteral("&"));
Laurent Montel's avatar
Laurent Montel committed
119 120 121
        d->ui.txtTitle->setText(title);
        d->ui.comboApi->setCurrentIndex(d->bBlog->api());
        d->ui.comboDir->setCurrentIndex(d->bBlog->direction());
122
        d->ui.txtTitle->setEnabled(true);
123
    } else {
Laurent Montel's avatar
Laurent Montel committed
124 125 126
        setWindowTitle(i18n("Add a new blog"));
        d->bBlog = new BilboBlog(this);
        d->bBlog->setBlogId(QString());
Laurent Montel's avatar
Laurent Montel committed
127
        d->okButton->setEnabled(false);
128
        d->ui.txtTitle->setEnabled(false);
129 130
    }

Laurent Montel's avatar
Laurent Montel committed
131
    slotComboApiChanged(d->ui.comboApi->currentIndex());
132
    d->ui.txtUrl->setFocus();
133 134 135 136
}

void AddEditBlog::enableAutoConfBtn()
{
Laurent Montel's avatar
Laurent Montel committed
137 138 139
    if (d->ui.txtUrl->text().isEmpty() || d->ui.txtUser->text().isEmpty() || d->ui.txtPass->text().isEmpty()) {
        d->ui.btnAutoConf->setEnabled(false);
        d->ui.btnFetch->setEnabled(false);
140
    } else {
Laurent Montel's avatar
Laurent Montel committed
141 142
        d->ui.btnAutoConf->setEnabled(true);
        d->ui.btnFetch->setEnabled(true);
143 144 145 146 147
    }
}

void AddEditBlog::autoConfigure()
{
Laurent Montel's avatar
Laurent Montel committed
148
    if (d->ui.txtUrl->text().isEmpty() || d->ui.txtUser->text().isEmpty() || d->ui.txtPass->text().isEmpty()) {
149
        qCDebug(BLOGILO_LOG) << "Username, Password or URL not set!";
Laurent Montel's avatar
Laurent Montel committed
150 151
        KMessageBox::sorry(this, i18n("You have to set the username, password and URL of your blog or website."),
                           i18n("Incomplete fields"));
152 153
        return;
    }
Laurent Montel's avatar
Laurent Montel committed
154
    showWaitWidget(i18n("Trying to guess blog and API type..."));
155
    QString textUrl;
156
    ///Guess API with Url:
Laurent Montel's avatar
Laurent Montel committed
157
    if (d->ui.txtUrl->text().contains(QStringLiteral("xmlrpc.php"), Qt::CaseInsensitive)) {
Laurent Montel's avatar
Laurent Montel committed
158
        d->ui.comboApi->setCurrentIndex(3);
159 160 161
        fetchBlogId();
        return;
    }
Laurent Montel's avatar
Laurent Montel committed
162
    if (d->ui.txtUrl->text().contains(QStringLiteral("blogspot"), Qt::CaseInsensitive)) {
Laurent Montel's avatar
Laurent Montel committed
163
        d->ui.comboApi->setCurrentIndex(4);
164 165 166
        fetchBlogId();
        return;
    }
Laurent Montel's avatar
Laurent Montel committed
167
    if (d->ui.txtUrl->text().contains(QStringLiteral("wordpress"), Qt::CaseInsensitive)) {
Laurent Montel's avatar
Laurent Montel committed
168
        d->ui.comboApi->setCurrentIndex(3);
Laurent Montel's avatar
Cleanup  
Laurent Montel committed
169

Laurent Montel's avatar
Laurent Montel committed
170
        textUrl = d->ui.txtUrl->text();
Laurent Montel's avatar
Laurent Montel committed
171
        while (textUrl.endsWith(QLatin1Char('/'))) {
Laurent Montel's avatar
Laurent Montel committed
172
            textUrl.remove(textUrl.length() - 1, 1);
Laurent Montel's avatar
Laurent Montel committed
173
        }
Laurent Montel's avatar
Laurent Montel committed
174
        d->ui.txtUrl->setText(textUrl + QStringLiteral("/xmlrpc.php"));
175 176 177
        fetchBlogId();
        return;
    }
Laurent Montel's avatar
Laurent Montel committed
178
    if (d->ui.txtUrl->text().contains(QStringLiteral("livejournal"), Qt::CaseInsensitive)) {
Laurent Montel's avatar
Laurent Montel committed
179
        d->ui.comboApi->setCurrentIndex(0);
180
        d->tmpBlogUrl = d->ui.txtUrl->text();
Laurent Montel's avatar
Laurent Montel committed
181
        d->ui.txtUrl->setText(QStringLiteral("http://www.livejournal.com/interface/blogger/"));
Laurent Montel's avatar
Laurent Montel committed
182 183
        d->ui.txtId->setText(d->ui.txtUser->text());
        d->ui.txtTitle->setText(d->ui.txtUser->text());
184 185 186
        hideWaitWidget();
        return;
    }
Laurent Montel's avatar
Laurent Montel committed
187
    qCDebug(BLOGILO_LOG) << "Trying to guess API type by Homepage contents";
Laurent Montel's avatar
Laurent Montel committed
188
    KIO::StoredTransferJob *httpGetJob = KIO::storedGet(d->ui.txtUrl->text(), KIO::NoReload, KIO::HideProgressInfo);
Laurent Montel's avatar
Laurent Montel committed
189
    connect(httpGetJob, &KIO::StoredTransferJob::result, this, &AddEditBlog::gotHtml);
Laurent Montel's avatar
Laurent Montel committed
190 191
    d->mFetchAPITimer = new QTimer(this);
    d->mFetchAPITimer->setSingleShot(true);
Laurent Montel's avatar
Laurent Montel committed
192
    connect(d->mFetchAPITimer, &QTimer::timeout, this, &AddEditBlog::handleFetchAPITimeout);
Laurent Montel's avatar
Laurent Montel committed
193
    d->mFetchAPITimer->start(TIMEOUT);
194 195
}

Laurent Montel's avatar
Laurent Montel committed
196
void AddEditBlog::gotHtml(KJob *job)
197
{
Laurent Montel's avatar
Laurent Montel committed
198
    if (!job) {
Laurent Montel's avatar
Laurent Montel committed
199
        return;
Laurent Montel's avatar
Laurent Montel committed
200 201
    }
    if (job->error()) {
Laurent Montel's avatar
Laurent Montel committed
202
        qCDebug(BLOGILO_LOG) << "Auto configuration failed! Error: " << job->errorString();
203 204 205 206
        hideWaitWidget();
        KMessageBox::sorry(this, i18n("Auto configuration failed. You have to set Blog API on Advanced tab manually."));
        return;
    }
Laurent Montel's avatar
Laurent Montel committed
207
    QString httpData(QString::fromUtf8(static_cast<KIO::StoredTransferJob *>(job)->data()));
208 209
    job->deleteLater();

Laurent Montel's avatar
Laurent Montel committed
210
    QRegExp rxGData(QStringLiteral("content='blogger' name='generator'"));
Laurent Montel's avatar
Laurent Montel committed
211
    if (rxGData.indexIn(httpData) != -1) {
Laurent Montel's avatar
Laurent Montel committed
212
        qCDebug(BLOGILO_LOG) << "content='blogger' name='generator' matched";
213
        d->mFetchAPITimer->deleteLater();
Laurent Montel's avatar
Laurent Montel committed
214
        d->ui.comboApi->setCurrentIndex(4);
Laurent Montel's avatar
Laurent Montel committed
215
        QRegExp rxBlogId(QStringLiteral("BlogID=(\\d+)"));
Laurent Montel's avatar
Laurent Montel committed
216
        d->ui.txtId->setText(rxBlogId.cap(1));
217 218 219 220
        hideWaitWidget();
        return;
    }

Laurent Montel's avatar
Laurent Montel committed
221
    QRegExp rxLiveJournal(QStringLiteral("rel=\"openid.server\" href=\"http://www.livejournal.com/openid/server.bml\""));
Laurent Montel's avatar
Laurent Montel committed
222
    if (rxLiveJournal.indexIn(httpData) != -1) {
Laurent Montel's avatar
Laurent Montel committed
223
        qCDebug(BLOGILO_LOG) << " rel=\"openid.server\" href=\"http://www.livejournal.com/openid/server.bml\" matched";
224
        d->mFetchAPITimer->deleteLater();
Laurent Montel's avatar
Laurent Montel committed
225
        d->ui.comboApi->setCurrentIndex(0);
226
        d->ui.txtUrl->setText(QStringLiteral("http://www.liverjournal.com/interface/blogger/"));
Laurent Montel's avatar
Laurent Montel committed
227
        d->ui.txtId->setText(d->ui.txtUser->text());
228 229 230 231
        hideWaitWidget();
        return;
    }

Laurent Montel's avatar
Minor  
Laurent Montel committed
232
    QString textUrl;
Laurent Montel's avatar
Laurent Montel committed
233
    QRegExp rxWordpress(QStringLiteral("name=\"generator\" content=\"WordPress"));
Laurent Montel's avatar
Laurent Montel committed
234
    if (rxWordpress.indexIn(httpData) != -1) {
Laurent Montel's avatar
Laurent Montel committed
235
        qCDebug(BLOGILO_LOG) << "name=\"generator\" content=\"WordPress matched";
236
        d->mFetchAPITimer->deleteLater();
Laurent Montel's avatar
Laurent Montel committed
237
        d->ui.comboApi->setCurrentIndex(3);
Laurent Montel's avatar
Laurent Montel committed
238

Laurent Montel's avatar
Laurent Montel committed
239
        textUrl = d->ui.txtUrl->text();
Laurent Montel's avatar
Laurent Montel committed
240
        while (textUrl.endsWith(QLatin1Char('/'))) {
Laurent Montel's avatar
Laurent Montel committed
241
            textUrl.remove(textUrl.length() - 1, 1);
Laurent Montel's avatar
Laurent Montel committed
242
        }
Laurent Montel's avatar
Laurent Montel committed
243
        d->ui.txtUrl->setText(textUrl + QLatin1String("/xmlrpc.php"));
Laurent Montel's avatar
Laurent Montel committed
244 245
        fetchBlogId();
        return;
246 247 248
    }

    // add MT for WordpressBuggy -> URL/xmlrpc.php exists
249
    textUrl = d->ui.txtUrl->text();
Laurent Montel's avatar
Laurent Montel committed
250
    while (textUrl.endsWith(QLatin1Char('/'))) {
Laurent Montel's avatar
Laurent Montel committed
251
        textUrl.remove(textUrl.length() - 1, 1);
252
    }
Laurent Montel's avatar
Laurent Montel committed
253
    KIO::StoredTransferJob *testXmlRpcJob = KIO::storedGet(QStringLiteral("%1/xmlrpc.php").arg(textUrl),
Laurent Montel's avatar
Laurent Montel committed
254
                                            KIO::NoReload, KIO::HideProgressInfo);
255

Laurent Montel's avatar
Laurent Montel committed
256
    connect(testXmlRpcJob, &KIO::StoredTransferJob::result, this, &AddEditBlog::gotXmlRpcTest);
257 258
}

Laurent Montel's avatar
Laurent Montel committed
259
void AddEditBlog::gotXmlRpcTest(KJob *job)
260
{
261
    d->mFetchAPITimer->deleteLater();
Laurent Montel's avatar
Laurent Montel committed
262
    if (!job) {
Laurent Montel's avatar
Cleanup  
Laurent Montel committed
263
        return;
Laurent Montel's avatar
Laurent Montel committed
264 265
    }
    if (job->error()) {
Laurent Montel's avatar
Laurent Montel committed
266
        qCDebug(BLOGILO_LOG) << "Auto configuration failed! Error: " << job->errorString();
267 268 269 270
        hideWaitWidget();
        KMessageBox::sorry(this, i18n("Auto configuration failed. You have to set Blog API on Advanced tab manually."));
        return;
    }
Yuri Chornoivan's avatar
Yuri Chornoivan committed
271 272 273
    KMessageBox::information(this, i18n("The program could not guess the API of your blog, "
                                        "but has found an XMLRPC interface and is trying to use it.\n"
                                        "The MovableType API is assumed for now; choose another API if you know the server supports it."));
Laurent Montel's avatar
Laurent Montel committed
274
    d->ui.comboApi->setCurrentIndex(2);
Laurent Montel's avatar
Minor  
Laurent Montel committed
275
    QString textUrl = d->ui.txtUrl->text();
Laurent Montel's avatar
Laurent Montel committed
276
    while (textUrl.endsWith(QLatin1Char('/'))) {
Laurent Montel's avatar
Laurent Montel committed
277
        textUrl.remove(textUrl.length() - 1, 1);
278
    }
Laurent Montel's avatar
Laurent Montel committed
279
    d->ui.txtUrl->setText(textUrl + QLatin1String("/xmlrpc.php"));
280 281 282 283 284
    fetchBlogId();
}

void AddEditBlog::fetchBlogId()
{
Laurent Montel's avatar
Laurent Montel committed
285
    switch (d->ui.comboApi->currentIndex()) {
Laurent Montel's avatar
Laurent Montel committed
286 287 288
    case 0:
    case 1:
    case 2:
Laurent Montel's avatar
Laurent Montel committed
289
    case 3: {
Laurent Montel's avatar
Laurent Montel committed
290
        KBlog::Blogger1 *blog = new KBlog::Blogger1(QUrl(d->ui.txtUrl->text()), this);
Laurent Montel's avatar
Laurent Montel committed
291
        d->mBlog = blog;
Laurent Montel's avatar
Laurent Montel committed
292 293
        blog->setUsername(d->ui.txtUser->text());
        blog->setPassword(d->ui.txtPass->text());
Laurent Montel's avatar
Laurent Montel committed
294
        connect(blog, &KBlog::Blogger1::listedBlogs, this, &AddEditBlog::fetchedBlogId);
Laurent Montel's avatar
Laurent Montel committed
295 296
        d->mFetchBlogIdTimer = new QTimer(this);
        d->mFetchBlogIdTimer->setSingleShot(true);
Laurent Montel's avatar
Laurent Montel committed
297
        connect(d->mFetchBlogIdTimer, &QTimer::timeout, this, &AddEditBlog::handleFetchIDTimeout);
Laurent Montel's avatar
Laurent Montel committed
298
        d->mFetchBlogIdTimer->start(TIMEOUT);
Laurent Montel's avatar
Laurent Montel committed
299 300 301
        blog->listBlogs();
        break;
    }
Laurent Montel's avatar
Laurent Montel committed
302
#ifdef HAVE_GAPIBLOGGER_SUPPORT
Laurent Montel's avatar
Laurent Montel committed
303
    case 4: {
Laurent Montel's avatar
Laurent Montel committed
304
        KBlog::Blogger *blog = new KBlog::Blogger(QUrl(d->ui.txtUrl->text()), this);
Laurent Montel's avatar
Laurent Montel committed
305
        d->mBlog = blog;
Laurent Montel's avatar
Laurent Montel committed
306 307
        blog->setUsername(d->ui.txtUser->text());
        blog->setPassword(d->ui.txtPass->text());
Laurent Montel's avatar
Laurent Montel committed
308 309
        blog->setApiKey(QStringLiteral("500754804903-g6n1rfjjcmhct64p3qgj6ma3oo8l8s6a.apps.googleusercontent.com"));
        blog->setSecretKey(QStringLiteral("jzSzkrD7ert2z0v5VEq6CcSs"));
Laurent Montel's avatar
Laurent Montel committed
310
        connect(blog, &KBlog::Blogger::authenticated, this, &AddEditBlog::bloggerAuthenticated);
311
        blog->authenticate();
Laurent Montel's avatar
Laurent Montel committed
312 313
        break;
    }
Laurent Montel's avatar
Laurent Montel committed
314
#endif
Laurent Montel's avatar
Laurent Montel committed
315
    default:
Laurent Montel's avatar
Laurent Montel committed
316
        qCDebug(BLOGILO_LOG) << "Unknown API";
Laurent Montel's avatar
Laurent Montel committed
317 318 319
        return;
    }

Laurent Montel's avatar
Laurent Montel committed
320
    connect(d->mBlog, &KBlog::Blog::error, this, &AddEditBlog::handleFetchError);
Laurent Montel's avatar
Laurent Montel committed
321 322 323
    d->ui.txtId->setText(i18n("Please wait..."));
    d->ui.txtId->setEnabled(false);
    showWaitWidget(i18n("Fetching Blog Id..."));
324 325 326 327
}

void AddEditBlog::handleFetchIDTimeout()
{
Laurent Montel's avatar
Laurent Montel committed
328
    if (d->mFetchBlogIdTimer) {
Mehrdad Momeny's avatar
Mehrdad Momeny committed
329
        d->mFetchBlogIdTimer->stop();
330
    }
Laurent Montel's avatar
Laurent Montel committed
331
    if (d->mFetchProfileIdTimer) {
Mehrdad Momeny's avatar
Mehrdad Momeny committed
332
        d->mFetchProfileIdTimer->stop();
333
    }
Laurent Montel's avatar
Laurent Montel committed
334
    d->ui.txtId->clear();
Laurent Montel's avatar
Laurent Montel committed
335
    d->ui.txtId->setEnabled(true);
336
    hideWaitWidget();
Laurent Montel's avatar
Laurent Montel committed
337 338 339
    KMessageBox::error(this, i18n("Fetching the blog id timed out. Check your Internet connection,"
                                  "and your homepage URL, username or password.\nNote that the URL has to contain \"http://\"\n"
                                  "If you are using a self-hosted Wordpress blog, you have to enable Remote Publishing in its configuration."));
340 341 342 343
}

void AddEditBlog::handleFetchAPITimeout()
{
344
    d->mFetchAPITimer->deleteLater();
Laurent Montel's avatar
Laurent Montel committed
345
    d->mFetchAPITimer = Q_NULLPTR;
346
    hideWaitWidget();
Laurent Montel's avatar
Laurent Montel committed
347
    d->ui.txtId->setEnabled(true);
Laurent Montel's avatar
Laurent Montel committed
348
    d->ui.txtId->clear();
Laurent Montel's avatar
Laurent Montel committed
349 350 351
    KMessageBox::sorry(this, i18n("The API guess function has failed, "
                                  "please check your Internet connection. Otherwise, you have to set the API type manually on the Advanced tab."),
                       i18n("Auto Configuration Failed"));
352 353
}

Laurent Montel's avatar
Laurent Montel committed
354
void AddEditBlog::handleFetchError(KBlog::Blog::ErrorType type, const QString &errorMsg)
355
{
Laurent Montel's avatar
Laurent Montel committed
356
    qCDebug(BLOGILO_LOG) << " ErrorType: " << type;
Laurent Montel's avatar
Laurent Montel committed
357
    d->ui.txtId->setEnabled(true);
Laurent Montel's avatar
Laurent Montel committed
358
    d->ui.txtId->clear();
359
    hideWaitWidget();
Laurent Montel's avatar
Laurent Montel committed
360
    KMessageBox::detailedError(this, i18n("Fetching BlogID Failed.\nPlease check your Internet connection."), errorMsg);
361 362
}

Laurent Montel's avatar
Laurent Montel committed
363
void AddEditBlog::fetchedBlogId(const QList< QMap < QString, QString > > &list)
364
{
Laurent Montel's avatar
Laurent Montel committed
365
    if (d->mFetchBlogIdTimer) {
366
        d->mFetchBlogIdTimer->deleteLater();
Laurent Montel's avatar
Laurent Montel committed
367
        d->mFetchBlogIdTimer = Q_NULLPTR;
368 369 370
    }
    hideWaitWidget();
    QString blogId, blogName, blogUrl, apiUrl;
371
    const int listCount(list.count());
Laurent Montel's avatar
Laurent Montel committed
372
    if (listCount > 1) {
Laurent Montel's avatar
Laurent Montel committed
373
        qCDebug(BLOGILO_LOG) << "User has more than ONE blog!";
Laurent Montel's avatar
Laurent Montel committed
374
        QDialog *blogsDialog = new QDialog(this);
375 376
        QTableWidget *blogsList = new QTableWidget(blogsDialog);
        blogsList->setSelectionBehavior(QAbstractItemView::SelectRows);
Laurent Montel's avatar
Laurent Montel committed
377 378 379
        QList< QMap<QString, QString> >::const_iterator it = list.constBegin();
        QList< QMap<QString, QString> >::const_iterator endIt = list.constEnd();
        int i = 0;
380 381
        blogsList->setColumnCount(4);
        QStringList headers;
382
        headers << i18n("Title") << i18n("URL");
Laurent Montel's avatar
Laurent Montel committed
383

384 385 386
        blogsList->setHorizontalHeaderLabels(headers);
        blogsList->setColumnHidden(2, true);
        blogsList->setColumnHidden(3, true);
Laurent Montel's avatar
Laurent Montel committed
387
        for (; it != endIt; ++it) {
Laurent Montel's avatar
Laurent Montel committed
388
            qCDebug(BLOGILO_LOG) << it->value(QStringLiteral("title"));
389
            blogsList->insertRow(i);
Laurent Montel's avatar
Laurent Montel committed
390 391 392 393
            blogsList->setCellWidget(i, 0, new QLabel(it->value(QStringLiteral("title"))));
            blogsList->setCellWidget(i, 1, new QLabel(it->value(QStringLiteral("url"))));
            blogsList->setCellWidget(i, 2, new QLabel(it->value(QStringLiteral("id"))));
            blogsList->setCellWidget(i, 3, new QLabel(it->value(QStringLiteral("apiUrl"))));
394 395
            ++i;
        }
Laurent Montel's avatar
Laurent Montel committed
396 397 398
        QVBoxLayout *mainLayout = new QVBoxLayout;
        blogsDialog->setLayout(mainLayout);
        mainLayout->addWidget(blogsList);
Laurent Montel's avatar
Laurent Montel committed
399 400
        blogsDialog->setWindowTitle(i18n("Which blog?"));
        if (blogsDialog->exec()) {
401
            int row = blogsList->currentRow();
Laurent Montel's avatar
Laurent Montel committed
402
            if (row == -1) {
Laurent Montel's avatar
Laurent Montel committed
403
                delete blogsDialog;
404
                return;
Laurent Montel's avatar
Laurent Montel committed
405
            }
Laurent Montel's avatar
Laurent Montel committed
406 407 408 409
            blogId = qobject_cast<QLabel *>(blogsList->cellWidget(row, 2))->text();
            blogName = qobject_cast<QLabel *>(blogsList->cellWidget(row, 0))->text();
            blogUrl = qobject_cast<QLabel *>(blogsList->cellWidget(row, 1))->text();
            apiUrl = qobject_cast<QLabel *>(blogsList->cellWidget(row, 3))->text();
Laurent Montel's avatar
Laurent Montel committed
410 411
        } else {
            delete blogsDialog;
412
            return;
Laurent Montel's avatar
Laurent Montel committed
413 414
        }
        delete blogsDialog;
415
    } else if (listCount > 0) {
Laurent Montel's avatar
Laurent Montel committed
416 417 418 419
        blogId = list.constBegin()->value(QStringLiteral("id"));
        blogName = list.constBegin()->value(QStringLiteral("title"));
        blogUrl = list.constBegin()->value(QStringLiteral("url"));
        apiUrl = list.constBegin()->value(QStringLiteral("apiUrl"));
420
    } else {
421 422
        KMessageBox::sorry(this, i18n("Sorry, No blog found with the specified account info."));
        return;
423
    }
Laurent Montel's avatar
Laurent Montel committed
424 425 426 427 428 429 430 431
    d->ui.txtId->setText(blogId);
    d->ui.txtTitle->setText(blogName);
    d->ui.txtId->setEnabled(true);
    d->ui.btnFetch->setEnabled(true);
    d->ui.btnAutoConf->setEnabled(true);

    if (!apiUrl.isEmpty()) {
        d->ui.txtUrl->setText(apiUrl);
432
    } else {
433
        apiUrl = d->ui.txtUrl->text();
434
    }
Laurent Montel's avatar
Laurent Montel committed
435 436
    if (!blogUrl.isEmpty()) {
        d->bBlog->setBlogUrl(blogUrl);
437
    } else {
Laurent Montel's avatar
Laurent Montel committed
438 439 440 441 442
        if (d->tmpBlogUrl.isEmpty()) {
            d->bBlog->setBlogUrl(apiUrl);
        } else {
            d->bBlog->setBlogUrl(d->tmpBlogUrl);
        }
443 444
    }

Laurent Montel's avatar
Laurent Montel committed
445 446 447 448 449
    d->bBlog->setUrl(QUrl(apiUrl));
    d->bBlog->setUsername(d->ui.txtUser->text());
    d->bBlog->setPassword(d->ui.txtPass->text());
    d->bBlog->setBlogId(blogId);
    d->bBlog->setTitle(blogName);
450 451
}

Laurent Montel's avatar
Laurent Montel committed
452
void AddEditBlog::enableOkButton(const QString &txt)
453
{
454
    const bool check = !txt.isEmpty();
Laurent Montel's avatar
Laurent Montel committed
455
    d->okButton->setEnabled(check);
Laurent Montel's avatar
Laurent Montel committed
456
    d->ui.txtTitle->setEnabled(check);
457 458
}

459
void AddEditBlog::slotReturnPressed()
460 461
{
    ///FIXME This function commented temporarilly! check its functionality! and uncomment it!
Laurent Montel's avatar
Laurent Montel committed
462 463
    if (d->okButton->isEnabled()) {
        d->okButton->setFocus();
464
    } else {
Laurent Montel's avatar
Laurent Montel committed
465 466
        if (d->mainW->currentIndex() == 0) {
            if (d->ui.btnAutoConf->isEnabled()) {
467 468 469 470 471 472 473 474 475 476
                autoConfigure();
            }
        } else {
            fetchBlogId();
        }
    }
}

AddEditBlog::~AddEditBlog()
{
Laurent Montel's avatar
Laurent Montel committed
477
    delete d;
478 479
}

Laurent Montel's avatar
Laurent Montel committed
480
void AddEditBlog::setSupportedFeatures(BilboBlog::ApiType api)
481
{
Laurent Montel's avatar
Laurent Montel committed
482
    const QString yesStyle = QStringLiteral("QLabel{color: green;}");
Laurent Montel's avatar
Laurent Montel committed
483
    const QString yesText = i18nc("Supported feature or Not", "Yes");
Laurent Montel's avatar
Laurent Montel committed
484
    const QString noStyle = QStringLiteral("QLabel{color: red;}");
Laurent Montel's avatar
Laurent Montel committed
485 486
    const QString noText = i18nc("Supported feature or Not", "No, API does not support it");
    const QString notYetText = i18nc("Supported feature or Not", "No, Blogilo does not yet support it");
487

Laurent Montel's avatar
Laurent Montel committed
488 489 490 491 492 493
    d->ui.featureCreatePost->setText(yesText);
    d->ui.featureCreatePost->setStyleSheet(yesStyle);
    d->ui.featureRemovePost->setText(yesText);
    d->ui.featureRemovePost->setStyleSheet(yesStyle);
    d->ui.featurRecentPosts->setText(yesText);
    d->ui.featurRecentPosts->setStyleSheet(yesStyle);
494

Laurent Montel's avatar
Laurent Montel committed
495
    d->ui.featureCreateCategory->setStyleSheet(noStyle);
496

Laurent Montel's avatar
Laurent Montel committed
497
    switch (api) {
Laurent Montel's avatar
Laurent Montel committed
498
    case BilboBlog::BLOGGER1_API:
Laurent Montel's avatar
Laurent Montel committed
499 500 501 502 503 504 505 506 507
        d->ui.featureUploadMedia->setText(noText);
        d->ui.featureUploadMedia->setStyleSheet(noStyle);
        d->ui.featureCategories->setText(noText);
        d->ui.featureCategories->setStyleSheet(noStyle);
        d->ui.featureMultipagedPosts->setText(noText);
        d->ui.featureMultipagedPosts->setStyleSheet(noStyle);
        d->ui.featureCreateCategory->setText(noText);
        d->ui.featureTags->setText(noText);
        d->ui.featureTags->setStyleSheet(noStyle);
Laurent Montel's avatar
Laurent Montel committed
508 509
        break;
    case BilboBlog::METAWEBLOG_API:
Laurent Montel's avatar
Laurent Montel committed
510 511 512 513 514 515 516 517 518
        d->ui.featureUploadMedia->setText(yesText);
        d->ui.featureUploadMedia->setStyleSheet(yesStyle);
        d->ui.featureCategories->setText(noText);
        d->ui.featureCategories->setStyleSheet(noStyle);
        d->ui.featureMultipagedPosts->setText(noText);
        d->ui.featureMultipagedPosts->setStyleSheet(noStyle);
        d->ui.featureCreateCategory->setText(noText);
        d->ui.featureTags->setText(noText);
        d->ui.featureTags->setStyleSheet(noStyle);
Laurent Montel's avatar
Laurent Montel committed
519 520
        break;
    case BilboBlog::MOVABLETYPE_API:
Laurent Montel's avatar
Laurent Montel committed
521 522 523 524 525 526 527 528 529
        d->ui.featureUploadMedia->setText(yesText);
        d->ui.featureUploadMedia->setStyleSheet(yesStyle);
        d->ui.featureCategories->setText(yesText);
        d->ui.featureCategories->setStyleSheet(yesStyle);
        d->ui.featureMultipagedPosts->setText(yesText);
        d->ui.featureMultipagedPosts->setStyleSheet(yesStyle);
        d->ui.featureCreateCategory->setText(noText);
        d->ui.featureTags->setText(yesText);
        d->ui.featureTags->setStyleSheet(yesStyle);
Laurent Montel's avatar
Laurent Montel committed
530 531
        break;
    case BilboBlog::WORDPRESSBUGGY_API:
Laurent Montel's avatar
Laurent Montel committed
532 533 534 535 536 537 538 539 540
        d->ui.featureUploadMedia->setText(yesText);
        d->ui.featureUploadMedia->setStyleSheet(yesStyle);
        d->ui.featureCategories->setText(yesText);
        d->ui.featureCategories->setStyleSheet(yesStyle);
        d->ui.featureMultipagedPosts->setText(yesText);
        d->ui.featureMultipagedPosts->setStyleSheet(yesStyle);
        d->ui.featureCreateCategory->setText(notYetText);
        d->ui.featureTags->setText(yesText);
        d->ui.featureTags->setStyleSheet(yesStyle);
Laurent Montel's avatar
Laurent Montel committed
541
        break;
542
    case BilboBlog::BLOGGER_API:
Laurent Montel's avatar
Laurent Montel committed
543 544 545 546 547 548 549 550 551
        d->ui.featureUploadMedia->setText(noText);
        d->ui.featureUploadMedia->setStyleSheet(noStyle);
        d->ui.featureCategories->setText(noText);
        d->ui.featureCategories->setStyleSheet(noStyle);
        d->ui.featureMultipagedPosts->setText(noText);
        d->ui.featureMultipagedPosts->setStyleSheet(noStyle);
        d->ui.featureCreateCategory->setText(noText);
        d->ui.featureTags->setText(yesText);
        d->ui.featureTags->setStyleSheet(yesStyle);
Laurent Montel's avatar
Laurent Montel committed
552
        break;
553 554 555
    };
}

Laurent Montel's avatar
Laurent Montel committed
556
void AddEditBlog::slotComboApiChanged(int index)
557 558
{
    ///This wrapper is to change api if needed!
Laurent Montel's avatar
Laurent Montel committed
559
    setSupportedFeatures((BilboBlog::ApiType) index);
560 561
}

Laurent Montel's avatar
Laurent Montel committed
562
void AddEditBlog::slotAccepted()
563
{
Laurent Montel's avatar
Laurent Montel committed
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
    if (d->bBlog->blogid().isEmpty() && d->ui.txtId->text().isEmpty()) {
        KMessageBox::sorry(this, i18n("Blog ID has not yet been retrieved.\n"
                                      "You can fetch the blog ID by clicking on \"Auto Configure\" or the \"Fetch ID\" button; otherwise, you have "
                                      "to insert your blog ID manually.")
                          );
        return;
    }
    d->bBlog->setApi((BilboBlog::ApiType)d->ui.comboApi->currentIndex());
    d->bBlog->setDirection((Qt::LayoutDirection)d->ui.comboDir->currentIndex());
    d->bBlog->setTitle(d->ui.txtTitle->text());
    d->bBlog->setPassword(d->ui.txtPass->text());
    d->bBlog->setUsername(d->ui.txtUser->text());
    d->bBlog->setBlogId(d->ui.txtId->text());
    d->bBlog->setUrl(QUrl(d->ui.txtUrl->text()));
    if (d->bBlog->blogUrl().isEmpty()) {
        d->bBlog->setBlogUrl(d->ui.txtUrl->text());
    }
581

Laurent Montel's avatar
Laurent Montel committed
582 583 584 585 586 587
    if (d->isNewBlog) {
        int blog_id = DBMan::self()->addBlog(*d->bBlog);
        d->bBlog->setId(blog_id);
        if (blog_id != -1) {
            qCDebug(BLOGILO_LOG) << "Emitting sigBlogAdded() ...";
            Q_EMIT sigBlogAdded(*d->bBlog);
588
        } else {
Laurent Montel's avatar
Laurent Montel committed
589
            qCDebug(BLOGILO_LOG) << "Cannot add blog";
590
        }
Laurent Montel's avatar
Laurent Montel committed
591
    } else {
Laurent Montel's avatar
Laurent Montel committed
592 593 594 595 596 597
        if (DBMan::self()->editBlog(*d->bBlog)) {
            qCDebug(BLOGILO_LOG) << "Emitting sigBlogEdited() ...";
            Q_EMIT sigBlogEdited(*d->bBlog);
        } else {
            qCDebug(BLOGILO_LOG) << "Cannot edit blog with id " << d->bBlog->id();
        }
Laurent Montel's avatar
Laurent Montel committed
598
    }
Laurent Montel's avatar
Laurent Montel committed
599
    accept();
600 601
}

Laurent Montel's avatar
Laurent Montel committed
602
void AddEditBlog::showWaitWidget(const QString &text)
603
{
Laurent Montel's avatar
Laurent Montel committed
604 605 606
    d->ui.btnAutoConf->setEnabled(false);
    d->ui.btnFetch->setEnabled(false);
    if (!d->wait) {
607
        d->wait = new WaitWidget(this);
Laurent Montel's avatar
Laurent Montel committed
608
        d->wait->setWindowModality(Qt::WindowModal);
609
        d->wait->setBusyState();
610
    }
Laurent Montel's avatar
Laurent Montel committed
611
    d->wait->setText(text);
612
    d->wait->show();
613 614 615 616
}

void AddEditBlog::hideWaitWidget()
{
Laurent Montel's avatar
Laurent Montel committed
617 618 619
    d->ui.btnAutoConf->setEnabled(true);
    d->ui.btnFetch->setEnabled(true);
    if (d->wait) {
620
        d->wait->deleteLater();
Laurent Montel's avatar
Laurent Montel committed
621
    }
Laurent Montel's avatar
Laurent Montel committed
622
    d->wait = Q_NULLPTR;
623 624
}

625 626
void AddEditBlog::bloggerAuthenticated(const QMap< QString, QString > &authData)
{
Laurent Montel's avatar
Laurent Montel committed
627
#ifdef HAVE_GAPIBLOGGER_SUPPORT
628
    d->bBlog->setAuthData(authData);
Laurent Montel's avatar
Laurent Montel committed
629
    KBlog::Blogger *blogger = qobject_cast<KBlog::Blogger *>(d->mBlog);
Laurent Montel's avatar
Laurent Montel committed
630
    connect(blogger, &KBlog::Blogger::listedBlogs, this, &AddEditBlog::fetchedBlogId);
Laurent Montel's avatar
Laurent Montel committed
631 632
    d->mFetchBlogIdTimer = new QTimer(this);
    d->mFetchBlogIdTimer->setSingleShot(true);
Laurent Montel's avatar
Laurent Montel committed
633
    connect(d->mFetchBlogIdTimer, &QTimer::timeout, this, &AddEditBlog::handleFetchIDTimeout);
Laurent Montel's avatar
Laurent Montel committed
634
    d->mFetchBlogIdTimer->start(TIMEOUT);
635
    blogger->listBlogs();
Laurent Montel's avatar
Laurent Montel committed
636
#endif
637
}