add_definitions(-DQT_NO_URL_CAST_FROM_STRING) + fix compilation
[kdepim.git] / blogilo / src / addeditblog.cpp
blobbc602abf9d55c204815018e3234c7e3779d58802
1 /*
2 This file is part of Blogilo, A KDE Blogging Client
4 Copyright (C) 2008-2010 Mehrdad Momeny <mehrdad.momeny@gmail.com>
5 Copyright (C) 2008-2010 Golnaz Nilieh <g382nilieh@gmail.com>
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of
10 the License or (at your option) version 3 or any later version
11 accepted by the membership of KDE e.V. (or its successor approved
12 by the membership of KDE e.V.), which shall act as a proxy
13 defined in Section 14 of version 3 of the license.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, see http://www.gnu.org/licenses/
24 #include "addeditblog.h"
25 #include "waitwidget.h"
26 #include "bilboblog.h"
27 #include "dbman.h"
29 #include <kblog/blogger1.h>
30 #include <kblog/metaweblog.h>
31 #include <kblog/movabletype.h>
32 #include <kblog/wordpressbuggy.h>
33 #ifdef HAVE_GAPIBLOGGER_SUPPORT
34 #include "blogger.h"
35 #endif
36 #include <kmessagebox.h>
37 #include "blogilo_debug.h"
38 #include <kio/jobclasses.h>
39 #include <kio/job.h>
40 #include <KLocalizedString>
42 #include <QTableWidget>
43 #include <QTimer>
44 #include <KConfigGroup>
45 static const int TIMEOUT = 45000;
47 class AddEditBlog::Private
49 public:
50 Private()
51 : mainW(Q_NULLPTR),
52 isNewBlog(false),
53 bBlog(Q_NULLPTR),
54 mBlog(Q_NULLPTR),
55 mFetchProfileIdTimer(Q_NULLPTR),
56 mFetchBlogIdTimer(Q_NULLPTR),
57 mFetchAPITimer(Q_NULLPTR),
58 wait(Q_NULLPTR),
59 okButton(Q_NULLPTR)
61 Ui::AddEditBlogBase ui;
62 QTabWidget *mainW;
63 bool isNewBlog;
64 BilboBlog *bBlog;
65 KBlog::Blog *mBlog;
66 QTimer *mFetchProfileIdTimer;
67 QTimer *mFetchBlogIdTimer;
68 QTimer *mFetchAPITimer;
69 WaitWidget *wait;
70 QString tmpBlogUrl;
71 QPushButton *okButton;
74 AddEditBlog::AddEditBlog(int blog_id, QWidget *parent)
75 : QDialog(parent), d(new Private)
77 QVBoxLayout *mainLayout = new QVBoxLayout(this);
78 d->mainW = new QTabWidget(this);
79 d->ui.setupUi(d->mainW);
80 mainLayout->addWidget(d->mainW);
82 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
83 mainLayout->addWidget(buttonBox);
85 d->okButton = buttonBox->button(QDialogButtonBox::Ok);
86 d->okButton->setDefault(true);
87 d->okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
88 connect(buttonBox, &QDialogButtonBox::accepted, this, &AddEditBlog::slotAccepted);
89 connect(buttonBox, &QDialogButtonBox::rejected, this, &AddEditBlog::reject);
91 d->isNewBlog = true;
92 d->mFetchAPITimer = d->mFetchBlogIdTimer = d->mFetchProfileIdTimer = Q_NULLPTR;
94 connect(d->ui.txtId, &QLineEdit::textChanged, this, &AddEditBlog::enableOkButton);
95 connect(d->ui.txtUrl, &QLineEdit::textChanged, this, &AddEditBlog::enableAutoConfBtn);
96 connect(d->ui.txtUser, &QLineEdit::textChanged, this, &AddEditBlog::enableAutoConfBtn);
97 connect(d->ui.txtPass, &QLineEdit::textChanged, this, &AddEditBlog::enableAutoConfBtn);
98 connect(d->ui.btnAutoConf, &QPushButton::clicked, this, &AddEditBlog::autoConfigure);
99 connect(d->ui.btnFetch, &QPushButton::clicked, this, &AddEditBlog::fetchBlogId);
100 connect(d->ui.comboApi, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &AddEditBlog::slotComboApiChanged);
101 connect(d->ui.txtUrl, &QLineEdit::returnPressed, this, &AddEditBlog::slotReturnPressed);
102 connect(d->ui.txtUser, &QLineEdit::returnPressed, this, &AddEditBlog::slotReturnPressed);
103 connect(d->ui.txtPass, &QLineEdit::returnPressed, this, &AddEditBlog::slotReturnPressed);
104 connect(d->ui.txtId, &QLineEdit::returnPressed, this, &AddEditBlog::slotReturnPressed);
106 if (blog_id > -1) {
107 setWindowTitle(i18n("Edit blog settings"));
108 d->okButton->setEnabled(true);
109 d->ui.btnFetch->setEnabled(true);
110 d->ui.btnAutoConf->setEnabled(true);
111 d->isNewBlog = false;
112 d->bBlog = DBMan::self()->blog(blog_id);
113 d->ui.txtUrl->setText(d->bBlog->url().url());
114 d->ui.txtUser->setText(d->bBlog->username());
115 d->ui.txtPass->setText(d->bBlog->password());
116 d->ui.txtId->setText(d->bBlog->blogid());
117 QString title = d->bBlog->title();
118 title = title.replace(QLatin1String("&amp;"), QStringLiteral("&"));
119 d->ui.txtTitle->setText(title);
120 d->ui.comboApi->setCurrentIndex(d->bBlog->api());
121 d->ui.comboDir->setCurrentIndex(d->bBlog->direction());
122 d->ui.txtTitle->setEnabled(true);
123 } else {
124 setWindowTitle(i18n("Add a new blog"));
125 d->bBlog = new BilboBlog(this);
126 d->bBlog->setBlogId(QString());
127 d->okButton->setEnabled(false);
128 d->ui.txtTitle->setEnabled(false);
131 slotComboApiChanged(d->ui.comboApi->currentIndex());
132 d->ui.txtUrl->setFocus();
135 void AddEditBlog::enableAutoConfBtn()
137 if (d->ui.txtUrl->text().isEmpty() || d->ui.txtUser->text().isEmpty() || d->ui.txtPass->text().isEmpty()) {
138 d->ui.btnAutoConf->setEnabled(false);
139 d->ui.btnFetch->setEnabled(false);
140 } else {
141 d->ui.btnAutoConf->setEnabled(true);
142 d->ui.btnFetch->setEnabled(true);
146 void AddEditBlog::autoConfigure()
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!";
150 KMessageBox::sorry(this, i18n("You have to set the username, password and URL of your blog or website."),
151 i18n("Incomplete fields"));
152 return;
154 showWaitWidget(i18n("Trying to guess blog and API type..."));
155 QString textUrl;
156 ///Guess API with Url:
157 if (d->ui.txtUrl->text().contains(QStringLiteral("xmlrpc.php"), Qt::CaseInsensitive)) {
158 d->ui.comboApi->setCurrentIndex(3);
159 fetchBlogId();
160 return;
162 if (d->ui.txtUrl->text().contains(QStringLiteral("blogspot"), Qt::CaseInsensitive)) {
163 d->ui.comboApi->setCurrentIndex(4);
164 fetchBlogId();
165 return;
167 if (d->ui.txtUrl->text().contains(QStringLiteral("wordpress"), Qt::CaseInsensitive)) {
168 d->ui.comboApi->setCurrentIndex(3);
170 textUrl = d->ui.txtUrl->text();
171 while (textUrl.endsWith(QLatin1Char('/'))) {
172 textUrl.remove(textUrl.length() - 1, 1);
174 d->ui.txtUrl->setText(textUrl + QStringLiteral("/xmlrpc.php"));
175 fetchBlogId();
176 return;
178 if (d->ui.txtUrl->text().contains(QStringLiteral("livejournal"), Qt::CaseInsensitive)) {
179 d->ui.comboApi->setCurrentIndex(0);
180 d->tmpBlogUrl = d->ui.txtUrl->text();
181 d->ui.txtUrl->setText(QStringLiteral("http://www.livejournal.com/interface/blogger/"));
182 d->ui.txtId->setText(d->ui.txtUser->text());
183 d->ui.txtTitle->setText(d->ui.txtUser->text());
184 hideWaitWidget();
185 return;
187 qCDebug(BLOGILO_LOG) << "Trying to guess API type by Homepage contents";
188 KIO::StoredTransferJob *httpGetJob = KIO::storedGet(QUrl(d->ui.txtUrl->text()), KIO::NoReload, KIO::HideProgressInfo);
189 connect(httpGetJob, &KIO::StoredTransferJob::result, this, &AddEditBlog::gotHtml);
190 d->mFetchAPITimer = new QTimer(this);
191 d->mFetchAPITimer->setSingleShot(true);
192 connect(d->mFetchAPITimer, &QTimer::timeout, this, &AddEditBlog::handleFetchAPITimeout);
193 d->mFetchAPITimer->start(TIMEOUT);
196 void AddEditBlog::gotHtml(KJob *job)
198 if (!job) {
199 return;
201 if (job->error()) {
202 qCDebug(BLOGILO_LOG) << "Auto configuration failed! Error: " << job->errorString();
203 hideWaitWidget();
204 KMessageBox::sorry(this, i18n("Auto configuration failed. You have to set Blog API on Advanced tab manually."));
205 return;
207 QString httpData(QString::fromUtf8(static_cast<KIO::StoredTransferJob *>(job)->data()));
208 job->deleteLater();
210 QRegExp rxGData(QStringLiteral("content='blogger' name='generator'"));
211 if (rxGData.indexIn(httpData) != -1) {
212 qCDebug(BLOGILO_LOG) << "content='blogger' name='generator' matched";
213 d->mFetchAPITimer->deleteLater();
214 d->ui.comboApi->setCurrentIndex(4);
215 QRegExp rxBlogId(QStringLiteral("BlogID=(\\d+)"));
216 d->ui.txtId->setText(rxBlogId.cap(1));
217 hideWaitWidget();
218 return;
221 QRegExp rxLiveJournal(QStringLiteral("rel=\"openid.server\" href=\"http://www.livejournal.com/openid/server.bml\""));
222 if (rxLiveJournal.indexIn(httpData) != -1) {
223 qCDebug(BLOGILO_LOG) << " rel=\"openid.server\" href=\"http://www.livejournal.com/openid/server.bml\" matched";
224 d->mFetchAPITimer->deleteLater();
225 d->ui.comboApi->setCurrentIndex(0);
226 d->ui.txtUrl->setText(QStringLiteral("http://www.liverjournal.com/interface/blogger/"));
227 d->ui.txtId->setText(d->ui.txtUser->text());
228 hideWaitWidget();
229 return;
232 QString textUrl;
233 QRegExp rxWordpress(QStringLiteral("name=\"generator\" content=\"WordPress"));
234 if (rxWordpress.indexIn(httpData) != -1) {
235 qCDebug(BLOGILO_LOG) << "name=\"generator\" content=\"WordPress matched";
236 d->mFetchAPITimer->deleteLater();
237 d->ui.comboApi->setCurrentIndex(3);
239 textUrl = d->ui.txtUrl->text();
240 while (textUrl.endsWith(QLatin1Char('/'))) {
241 textUrl.remove(textUrl.length() - 1, 1);
243 d->ui.txtUrl->setText(textUrl + QLatin1String("/xmlrpc.php"));
244 fetchBlogId();
245 return;
248 // add MT for WordpressBuggy -> URL/xmlrpc.php exists
249 textUrl = d->ui.txtUrl->text();
250 while (textUrl.endsWith(QLatin1Char('/'))) {
251 textUrl.remove(textUrl.length() - 1, 1);
253 KIO::StoredTransferJob *testXmlRpcJob = KIO::storedGet(QUrl(QStringLiteral("%1/xmlrpc.php").arg(textUrl)),
254 KIO::NoReload, KIO::HideProgressInfo);
256 connect(testXmlRpcJob, &KIO::StoredTransferJob::result, this, &AddEditBlog::gotXmlRpcTest);
259 void AddEditBlog::gotXmlRpcTest(KJob *job)
261 d->mFetchAPITimer->deleteLater();
262 if (!job) {
263 return;
265 if (job->error()) {
266 qCDebug(BLOGILO_LOG) << "Auto configuration failed! Error: " << job->errorString();
267 hideWaitWidget();
268 KMessageBox::sorry(this, i18n("Auto configuration failed. You have to set Blog API on Advanced tab manually."));
269 return;
271 KMessageBox::information(this, i18n("The program could not guess the API of your blog, "
272 "but has found an XMLRPC interface and is trying to use it.\n"
273 "The MovableType API is assumed for now; choose another API if you know the server supports it."));
274 d->ui.comboApi->setCurrentIndex(2);
275 QString textUrl = d->ui.txtUrl->text();
276 while (textUrl.endsWith(QLatin1Char('/'))) {
277 textUrl.remove(textUrl.length() - 1, 1);
279 d->ui.txtUrl->setText(textUrl + QLatin1String("/xmlrpc.php"));
280 fetchBlogId();
283 void AddEditBlog::fetchBlogId()
285 switch (d->ui.comboApi->currentIndex()) {
286 case 0:
287 case 1:
288 case 2:
289 case 3: {
290 KBlog::Blogger1 *blog = new KBlog::Blogger1(QUrl(d->ui.txtUrl->text()), this);
291 d->mBlog = blog;
292 blog->setUsername(d->ui.txtUser->text());
293 blog->setPassword(d->ui.txtPass->text());
294 connect(blog, &KBlog::Blogger1::listedBlogs, this, &AddEditBlog::fetchedBlogId);
295 d->mFetchBlogIdTimer = new QTimer(this);
296 d->mFetchBlogIdTimer->setSingleShot(true);
297 connect(d->mFetchBlogIdTimer, &QTimer::timeout, this, &AddEditBlog::handleFetchIDTimeout);
298 d->mFetchBlogIdTimer->start(TIMEOUT);
299 blog->listBlogs();
300 break;
302 #ifdef HAVE_GAPIBLOGGER_SUPPORT
303 case 4: {
304 KBlog::Blogger *blog = new KBlog::Blogger(QUrl(d->ui.txtUrl->text()), this);
305 d->mBlog = blog;
306 blog->setUsername(d->ui.txtUser->text());
307 blog->setPassword(d->ui.txtPass->text());
308 blog->setApiKey(QStringLiteral("500754804903-g6n1rfjjcmhct64p3qgj6ma3oo8l8s6a.apps.googleusercontent.com"));
309 blog->setSecretKey(QStringLiteral("jzSzkrD7ert2z0v5VEq6CcSs"));
310 connect(blog, &KBlog::Blogger::authenticated, this, &AddEditBlog::bloggerAuthenticated);
311 blog->authenticate();
312 break;
314 #endif
315 default:
316 qCDebug(BLOGILO_LOG) << "Unknown API";
317 return;
320 connect(d->mBlog, &KBlog::Blog::error, this, &AddEditBlog::handleFetchError);
321 d->ui.txtId->setText(i18n("Please wait..."));
322 d->ui.txtId->setEnabled(false);
323 showWaitWidget(i18n("Fetching Blog Id..."));
326 void AddEditBlog::handleFetchIDTimeout()
328 if (d->mFetchBlogIdTimer) {
329 d->mFetchBlogIdTimer->stop();
331 if (d->mFetchProfileIdTimer) {
332 d->mFetchProfileIdTimer->stop();
334 d->ui.txtId->clear();
335 d->ui.txtId->setEnabled(true);
336 hideWaitWidget();
337 KMessageBox::error(this, i18n("Fetching the blog id timed out. Check your Internet connection,"
338 "and your homepage URL, username or password.\nNote that the URL has to contain \"http://\"\n"
339 "If you are using a self-hosted Wordpress blog, you have to enable Remote Publishing in its configuration."));
342 void AddEditBlog::handleFetchAPITimeout()
344 d->mFetchAPITimer->deleteLater();
345 d->mFetchAPITimer = Q_NULLPTR;
346 hideWaitWidget();
347 d->ui.txtId->setEnabled(true);
348 d->ui.txtId->clear();
349 KMessageBox::sorry(this, i18n("The API guess function has failed, "
350 "please check your Internet connection. Otherwise, you have to set the API type manually on the Advanced tab."),
351 i18n("Auto Configuration Failed"));
354 void AddEditBlog::handleFetchError(KBlog::Blog::ErrorType type, const QString &errorMsg)
356 qCDebug(BLOGILO_LOG) << " ErrorType: " << type;
357 d->ui.txtId->setEnabled(true);
358 d->ui.txtId->clear();
359 hideWaitWidget();
360 KMessageBox::detailedError(this, i18n("Fetching BlogID Failed.\nPlease check your Internet connection."), errorMsg);
363 void AddEditBlog::fetchedBlogId(const QList< QMap < QString, QString > > &list)
365 if (d->mFetchBlogIdTimer) {
366 d->mFetchBlogIdTimer->deleteLater();
367 d->mFetchBlogIdTimer = Q_NULLPTR;
369 hideWaitWidget();
370 QString blogId, blogName, blogUrl, apiUrl;
371 const int listCount(list.count());
372 if (listCount > 1) {
373 qCDebug(BLOGILO_LOG) << "User has more than ONE blog!";
374 QDialog *blogsDialog = new QDialog(this);
375 QTableWidget *blogsList = new QTableWidget(blogsDialog);
376 blogsList->setSelectionBehavior(QAbstractItemView::SelectRows);
377 QList< QMap<QString, QString> >::const_iterator it = list.constBegin();
378 QList< QMap<QString, QString> >::const_iterator endIt = list.constEnd();
379 int i = 0;
380 blogsList->setColumnCount(4);
381 QStringList headers;
382 headers << i18n("Title") << i18n("URL");
384 blogsList->setHorizontalHeaderLabels(headers);
385 blogsList->setColumnHidden(2, true);
386 blogsList->setColumnHidden(3, true);
387 for (; it != endIt; ++it) {
388 qCDebug(BLOGILO_LOG) << it->value(QStringLiteral("title"));
389 blogsList->insertRow(i);
390 blogsList->setCellWidget(i, 0, new QLabel(it->value(QStringLiteral("title"))));
391 blogsList->setCellWidget(i, 1, new QLabel(it->value(QStringLiteral("url"))));
392 blogsList->setCellWidget(i, 2, new QLabel(it->value(QStringLiteral("id"))));
393 blogsList->setCellWidget(i, 3, new QLabel(it->value(QStringLiteral("apiUrl"))));
394 ++i;
396 QVBoxLayout *mainLayout = new QVBoxLayout;
397 blogsDialog->setLayout(mainLayout);
398 mainLayout->addWidget(blogsList);
399 blogsDialog->setWindowTitle(i18n("Which blog?"));
400 if (blogsDialog->exec()) {
401 int row = blogsList->currentRow();
402 if (row == -1) {
403 delete blogsDialog;
404 return;
406 blogId = qobject_cast<QLabel *>(blogsList->cellWidget(row, 2))->text();
407 blogName = qobject_cast<QLabel *>(blogsList->cellWidget(row, 0))->text();
408 blogUrl = qobject_cast<QLabel *>(blogsList->cellWidget(row, 1))->text();
409 apiUrl = qobject_cast<QLabel *>(blogsList->cellWidget(row, 3))->text();
410 } else {
411 delete blogsDialog;
412 return;
414 delete blogsDialog;
415 } else if (listCount > 0) {
416 blogId = list.constBegin()->value(QStringLiteral("id"));
417 blogName = list.constBegin()->value(QStringLiteral("title"));
418 blogUrl = list.constBegin()->value(QStringLiteral("url"));
419 apiUrl = list.constBegin()->value(QStringLiteral("apiUrl"));
420 } else {
421 KMessageBox::sorry(this, i18n("Sorry, No blog found with the specified account info."));
422 return;
424 d->ui.txtId->setText(blogId);
425 d->ui.txtTitle->setText(blogName);
426 d->ui.txtId->setEnabled(true);
427 d->ui.btnFetch->setEnabled(true);
428 d->ui.btnAutoConf->setEnabled(true);
430 if (!apiUrl.isEmpty()) {
431 d->ui.txtUrl->setText(apiUrl);
432 } else {
433 apiUrl = d->ui.txtUrl->text();
435 if (!blogUrl.isEmpty()) {
436 d->bBlog->setBlogUrl(blogUrl);
437 } else {
438 if (d->tmpBlogUrl.isEmpty()) {
439 d->bBlog->setBlogUrl(apiUrl);
440 } else {
441 d->bBlog->setBlogUrl(d->tmpBlogUrl);
445 d->bBlog->setUrl(QUrl(apiUrl));
446 d->bBlog->setUsername(d->ui.txtUser->text());
447 d->bBlog->setPassword(d->ui.txtPass->text());
448 d->bBlog->setBlogId(blogId);
449 d->bBlog->setTitle(blogName);
452 void AddEditBlog::enableOkButton(const QString &txt)
454 const bool check = !txt.isEmpty();
455 d->okButton->setEnabled(check);
456 d->ui.txtTitle->setEnabled(check);
459 void AddEditBlog::slotReturnPressed()
461 ///FIXME This function commented temporarilly! check its functionality! and uncomment it!
462 if (d->okButton->isEnabled()) {
463 d->okButton->setFocus();
464 } else {
465 if (d->mainW->currentIndex() == 0) {
466 if (d->ui.btnAutoConf->isEnabled()) {
467 autoConfigure();
469 } else {
470 fetchBlogId();
475 AddEditBlog::~AddEditBlog()
477 delete d;
480 void AddEditBlog::setSupportedFeatures(BilboBlog::ApiType api)
482 const QString yesStyle = QStringLiteral("QLabel{color: green;}");
483 const QString yesText = i18nc("Supported feature or Not", "Yes");
484 const QString noStyle = QStringLiteral("QLabel{color: red;}");
485 const QString noText = i18nc("Supported feature or Not", "No, API does not support it");
486 const QString notYetText = i18nc("Supported feature or Not", "No, Blogilo does not yet support it");
488 d->ui.featureCreatePost->setText(yesText);
489 d->ui.featureCreatePost->setStyleSheet(yesStyle);
490 d->ui.featureRemovePost->setText(yesText);
491 d->ui.featureRemovePost->setStyleSheet(yesStyle);
492 d->ui.featurRecentPosts->setText(yesText);
493 d->ui.featurRecentPosts->setStyleSheet(yesStyle);
495 d->ui.featureCreateCategory->setStyleSheet(noStyle);
497 switch (api) {
498 case BilboBlog::BLOGGER1_API:
499 d->ui.featureUploadMedia->setText(noText);
500 d->ui.featureUploadMedia->setStyleSheet(noStyle);
501 d->ui.featureCategories->setText(noText);
502 d->ui.featureCategories->setStyleSheet(noStyle);
503 d->ui.featureMultipagedPosts->setText(noText);
504 d->ui.featureMultipagedPosts->setStyleSheet(noStyle);
505 d->ui.featureCreateCategory->setText(noText);
506 d->ui.featureTags->setText(noText);
507 d->ui.featureTags->setStyleSheet(noStyle);
508 break;
509 case BilboBlog::METAWEBLOG_API:
510 d->ui.featureUploadMedia->setText(yesText);
511 d->ui.featureUploadMedia->setStyleSheet(yesStyle);
512 d->ui.featureCategories->setText(noText);
513 d->ui.featureCategories->setStyleSheet(noStyle);
514 d->ui.featureMultipagedPosts->setText(noText);
515 d->ui.featureMultipagedPosts->setStyleSheet(noStyle);
516 d->ui.featureCreateCategory->setText(noText);
517 d->ui.featureTags->setText(noText);
518 d->ui.featureTags->setStyleSheet(noStyle);
519 break;
520 case BilboBlog::MOVABLETYPE_API:
521 d->ui.featureUploadMedia->setText(yesText);
522 d->ui.featureUploadMedia->setStyleSheet(yesStyle);
523 d->ui.featureCategories->setText(yesText);
524 d->ui.featureCategories->setStyleSheet(yesStyle);
525 d->ui.featureMultipagedPosts->setText(yesText);
526 d->ui.featureMultipagedPosts->setStyleSheet(yesStyle);
527 d->ui.featureCreateCategory->setText(noText);
528 d->ui.featureTags->setText(yesText);
529 d->ui.featureTags->setStyleSheet(yesStyle);
530 break;
531 case BilboBlog::WORDPRESSBUGGY_API:
532 d->ui.featureUploadMedia->setText(yesText);
533 d->ui.featureUploadMedia->setStyleSheet(yesStyle);
534 d->ui.featureCategories->setText(yesText);
535 d->ui.featureCategories->setStyleSheet(yesStyle);
536 d->ui.featureMultipagedPosts->setText(yesText);
537 d->ui.featureMultipagedPosts->setStyleSheet(yesStyle);
538 d->ui.featureCreateCategory->setText(notYetText);
539 d->ui.featureTags->setText(yesText);
540 d->ui.featureTags->setStyleSheet(yesStyle);
541 break;
542 case BilboBlog::BLOGGER_API:
543 d->ui.featureUploadMedia->setText(noText);
544 d->ui.featureUploadMedia->setStyleSheet(noStyle);
545 d->ui.featureCategories->setText(noText);
546 d->ui.featureCategories->setStyleSheet(noStyle);
547 d->ui.featureMultipagedPosts->setText(noText);
548 d->ui.featureMultipagedPosts->setStyleSheet(noStyle);
549 d->ui.featureCreateCategory->setText(noText);
550 d->ui.featureTags->setText(yesText);
551 d->ui.featureTags->setStyleSheet(yesStyle);
552 break;
556 void AddEditBlog::slotComboApiChanged(int index)
558 ///This wrapper is to change api if needed!
559 setSupportedFeatures((BilboBlog::ApiType) index);
562 void AddEditBlog::slotAccepted()
564 if (d->bBlog->blogid().isEmpty() && d->ui.txtId->text().isEmpty()) {
565 KMessageBox::sorry(this, i18n("Blog ID has not yet been retrieved.\n"
566 "You can fetch the blog ID by clicking on \"Auto Configure\" or the \"Fetch ID\" button; otherwise, you have "
567 "to insert your blog ID manually.")
569 return;
571 d->bBlog->setApi((BilboBlog::ApiType)d->ui.comboApi->currentIndex());
572 d->bBlog->setDirection((Qt::LayoutDirection)d->ui.comboDir->currentIndex());
573 d->bBlog->setTitle(d->ui.txtTitle->text());
574 d->bBlog->setPassword(d->ui.txtPass->text());
575 d->bBlog->setUsername(d->ui.txtUser->text());
576 d->bBlog->setBlogId(d->ui.txtId->text());
577 d->bBlog->setUrl(QUrl(d->ui.txtUrl->text()));
578 if (d->bBlog->blogUrl().isEmpty()) {
579 d->bBlog->setBlogUrl(d->ui.txtUrl->text());
582 if (d->isNewBlog) {
583 int blog_id = DBMan::self()->addBlog(*d->bBlog);
584 d->bBlog->setId(blog_id);
585 if (blog_id != -1) {
586 qCDebug(BLOGILO_LOG) << "Emitting sigBlogAdded() ...";
587 Q_EMIT sigBlogAdded(*d->bBlog);
588 } else {
589 qCDebug(BLOGILO_LOG) << "Cannot add blog";
591 } else {
592 if (DBMan::self()->editBlog(*d->bBlog)) {
593 qCDebug(BLOGILO_LOG) << "Emitting sigBlogEdited() ...";
594 Q_EMIT sigBlogEdited(*d->bBlog);
595 } else {
596 qCDebug(BLOGILO_LOG) << "Cannot edit blog with id " << d->bBlog->id();
599 accept();
602 void AddEditBlog::showWaitWidget(const QString &text)
604 d->ui.btnAutoConf->setEnabled(false);
605 d->ui.btnFetch->setEnabled(false);
606 if (!d->wait) {
607 d->wait = new WaitWidget(this);
608 d->wait->setWindowModality(Qt::WindowModal);
609 d->wait->setBusyState();
611 d->wait->setText(text);
612 d->wait->show();
615 void AddEditBlog::hideWaitWidget()
617 d->ui.btnAutoConf->setEnabled(true);
618 d->ui.btnFetch->setEnabled(true);
619 if (d->wait) {
620 d->wait->deleteLater();
622 d->wait = Q_NULLPTR;
625 void AddEditBlog::bloggerAuthenticated(const QMap< QString, QString > &authData)
627 #ifdef HAVE_GAPIBLOGGER_SUPPORT
628 d->bBlog->setAuthData(authData);
629 KBlog::Blogger *blogger = qobject_cast<KBlog::Blogger *>(d->mBlog);
630 connect(blogger, &KBlog::Blogger::listedBlogs, this, &AddEditBlog::fetchedBlogId);
631 d->mFetchBlogIdTimer = new QTimer(this);
632 d->mFetchBlogIdTimer->setSingleShot(true);
633 connect(d->mFetchBlogIdTimer, &QTimer::timeout, this, &AddEditBlog::handleFetchIDTimeout);
634 d->mFetchBlogIdTimer->start(TIMEOUT);
635 blogger->listBlogs();
636 #endif