mainwindow.cpp 23 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>
Laurent Montel's avatar
Laurent Montel committed
6
    Copyright (C) 2013-2016 Laurent Montel <montel@kde.org>
Mehrdad Momeny's avatar
Mehrdad Momeny committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

    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/
*/
24 25 26 27 28 29 30 31 32 33 34 35

#include "mainwindow.h"
#include "global.h"
#include "dbman.h"
#include "toolbox.h"
#include "postentry.h"
#include "addeditblog.h"
#include "backend.h"
#include "bilbomedia.h"
#include "settings.h"
#include "bilboblog.h"
#include "blogsettings.h"
36
#include "poststabwidget.h"
Laurent Montel's avatar
Laurent Montel committed
37
#include "uploadmediadialog.h"
38
#include "configuredialog.h"
39 40 41 42 43

#include "ui_advancedsettingsbase.h"
#include "ui_settingsbase.h"
#include "ui_editorsettingsbase.h"

Laurent Montel's avatar
Laurent Montel committed
44
#include "Libkdepim/ProgressDialog"
Laurent Montel's avatar
Laurent Montel committed
45 46
#include <Libkdepim/StatusbarProgressWidget>
#include <Libkdepim/ProgressStatusBarWidget>
47

Laurent Montel's avatar
Laurent Montel committed
48
#include <qtabwidget.h>
49
#include <KStatusNotifierItem>
50
#include <qstatusbar.h>
51 52
#include <KToggleAction>
#include <kactioncollection.h>
53
#include <KActionMenu>
Laurent Montel's avatar
Port++  
Laurent Montel committed
54
#include <QAction>
55
#include <kconfigdialog.h>
Laurent Montel's avatar
Laurent Montel committed
56
#include "blogilo_debug.h"
57
#include <kmessagebox.h>
Laurent Montel's avatar
Laurent Montel committed
58
#include <KLocalizedString>
Laurent Montel's avatar
Laurent Montel committed
59 60
#include <KSelectAction>
#include <kimagefilepreview.h>
Laurent Montel's avatar
Port++  
Laurent Montel committed
61
#include <QMenu>
Laurent Montel's avatar
KUrl--  
Laurent Montel committed
62
#include <QUrl>
Laurent Montel's avatar
Laurent Montel committed
63

Laurent Montel's avatar
Laurent Montel committed
64 65
#include <QDockWidget>
#include <QProgressBar>
Mehrdad Momeny's avatar
Mehrdad Momeny committed
66
#include <QTimer>
67
#include <QKeyEvent>
Laurent Montel's avatar
Laurent Montel committed
68
#include <QDesktopServices>
69 70 71 72

#define TIMEOUT 5000

MainWindow::MainWindow()
Laurent Montel's avatar
Laurent Montel committed
73
    : KXmlGuiWindow(),
Laurent Montel's avatar
Laurent Montel committed
74 75
      activePost(Q_NULLPTR),
      systemTray(Q_NULLPTR),
Laurent Montel's avatar
Laurent Montel committed
76 77
      previousActivePostIndex(-1),
      busyNumber(0),
Laurent Montel's avatar
Laurent Montel committed
78
      progress(Q_NULLPTR),
Laurent Montel's avatar
Laurent Montel committed
79
      mCurrentBlogId(__currentBlogId)
80
{
Laurent Montel's avatar
Laurent Montel committed
81
    setWindowTitle(i18n("Blogilo"));
82

83
    tabPosts = new PostsTabWidget(this);
Laurent Montel's avatar
Laurent Montel committed
84
    setCentralWidget(tabPosts);
Laurent Montel's avatar
Laurent Montel committed
85 86 87 88
    connect(tabPosts, &PostsTabWidget::createNewPost, this, &MainWindow::slotCreateNewPost);
    connect(tabPosts, &PostsTabWidget::closeTabClicked, this, &MainWindow::slotCloseTabClicked);
    connect(tabPosts, &PostsTabWidget::tabCloseRequested, this, &MainWindow::slotRemovePostEntry);
    connect(tabPosts, &PostsTabWidget::tabRemoveAllExclude, this, &MainWindow::slotRemoveAllExclude);
89

Laurent Montel's avatar
Laurent Montel committed
90 91 92 93 94
    toolbox = new Toolbox(this);
    toolboxDock = new QDockWidget(i18n("Toolbox"), this);
    toolboxDock->setAllowedAreas(Qt::RightDockWidgetArea | Qt::LeftDockWidgetArea);
    toolboxDock->setFeatures(QDockWidget::AllDockWidgetFeatures);
    toolboxDock->setWidget(toolbox);
Laurent Montel's avatar
Laurent Montel committed
95

Laurent Montel's avatar
Laurent Montel committed
96 97
    toolboxDock->setObjectName(QStringLiteral("dock_toolbox"));
    toolbox->setObjectName(QStringLiteral("toolbox"));
Laurent Montel's avatar
Laurent Montel committed
98

Laurent Montel's avatar
Laurent Montel committed
99
    this->addDockWidget(Qt::RightDockWidgetArea, toolboxDock);
100 101 102

    // then, setup our actions
    setupActions();
103
    setupStatusBar();
104 105 106 107 108 109 110 111

    // a call to KXmlGuiWindow::setupGUI() populates the GUI
    // with actions, using KXMLGUI.
    // It also applies the saved mainwindow settings, if any, and ask the
    // mainwindow to automatically save settings if changed: window size,
    // toolbar position, icon size, etc.
    setupGUI();

Laurent Montel's avatar
Laurent Montel committed
112
    toolbox->setVisible(Settings::showToolboxOnStart());
Laurent Montel's avatar
Laurent Montel committed
113
    actionCollection()->action(QStringLiteral("toggle_toolbox"))->setChecked(Settings::showToolboxOnStart());
114 115 116

    setupSystemTray();

Laurent Montel's avatar
Laurent Montel committed
117
    connect(tabPosts, &PostsTabWidget::currentChanged, this, &MainWindow::slotActivePostChanged);
Laurent Montel's avatar
Laurent Montel committed
118 119 120
    connect(toolbox, &Toolbox::sigEntrySelected, this, &MainWindow::slotNewPostOpened);
    connect(toolbox, &Toolbox::sigError, this, &MainWindow::slotError);
    connect(toolbox, &Toolbox::sigBusy, this, &MainWindow::slotBusy);
121

122 123
    foreach (BilboBlog *blog, DBMan::self()->blogList()) {
        QAction *act = new QAction(blog->title(), blogs);
Laurent Montel's avatar
Laurent Montel committed
124
        act->setCheckable(true);
125
        act->setData(blog->id());
Laurent Montel's avatar
Laurent Montel committed
126
        blogs->addAction(act);
127
    }
Laurent Montel's avatar
Laurent Montel committed
128 129
    connect(blogs, static_cast<void (KSelectAction::*)(QAction *)>(&KSelectAction::triggered), this, &MainWindow::currentBlogChanged);
    QTimer::singleShot(0, this, SLOT(loadTempPosts()));
130 131 132 133
}

MainWindow::~MainWindow()
{
134 135
}

136 137
void MainWindow::setupStatusBar()
{
138 139 140 141 142 143 144 145 146 147 148 149 150
}

void MainWindow::slotUploadFileDone(const QString &serviceName, const QString &link)
{
    Q_UNUSED(serviceName);
    KMessageBox::information(this, i18n("File uploaded. You can access to it at this url %1", link), i18n("Upload File"));
}

void MainWindow::slotUploadFileFailed(const QString &serviceName, const QString &filename)
{
    Q_UNUSED(serviceName);
    Q_UNUSED(filename);
    KMessageBox::error(this, i18n("Error during upload."), i18n("Upload File"));
151 152
}

Laurent Montel's avatar
Laurent Montel committed
153 154 155 156 157
void MainWindow::slotActionFailed(const QString &serviceName, const QString &error)
{
    KMessageBox::error(this, i18n("%1 return an error '%2'", serviceName, error), i18n("Error"));
}

158 159 160 161 162 163 164 165
void MainWindow::slotCloseTabClicked()
{
    const int currentIndex = tabPosts->currentIndex();
    if (currentIndex != -1) {
        slotRemovePostEntry(currentIndex);
    }
}

166
void MainWindow::closeEvent(QCloseEvent *event)
167
{
168
    writeConfigs();
Laurent Montel's avatar
Laurent Montel committed
169
    if (!DBMan::self()->clearTempEntries()) {
Laurent Montel's avatar
Laurent Montel committed
170
        qCDebug(BLOGILO_LOG) << "Could not erase temp_post table: " << DBMan::self()->lastErrorText();
Laurent Montel's avatar
Laurent Montel committed
171
    }
Laurent Montel's avatar
Laurent Montel committed
172
    const int count = tabPosts->count();
173
    if (count > 0) {
Mehrdad Momeny's avatar
Mehrdad Momeny committed
174
        toolbox->getFieldsValue(activePost->currentPost());
Laurent Montel's avatar
Laurent Montel committed
175 176
        for (int i = 0; i < count; ++i) {
            PostEntry *pst = qobject_cast<PostEntry *>(tabPosts->widget(i));
Mehrdad Momeny's avatar
Mehrdad Momeny committed
177 178
            DBMan::self()->saveTempEntry(*pst->currentPost(), pst->currentPostBlogId());
        }
179
    }
180
    event->accept();
181 182 183 184
}

void MainWindow::setupActions()
{
Laurent Montel's avatar
Laurent Montel committed
185
    KStandardAction::quit(qApp, SLOT(quit()), actionCollection());
186

Laurent Montel's avatar
Laurent Montel committed
187
    KStandardAction::preferences(this, SLOT(optionsPreferences()), actionCollection());
188 189

    // custom menu and menu item
Laurent Montel's avatar
Laurent Montel committed
190 191
    QAction *actNewPost = new QAction(QIcon::fromTheme(QStringLiteral("document-new")), i18n("New Post"), this);
    actionCollection()->addAction(QStringLiteral("new_post"), actNewPost);
Laurent Montel's avatar
Laurent Montel committed
192
    actionCollection()->setDefaultShortcut(actNewPost, QKeySequence(Qt::CTRL + Qt::Key_N));
Laurent Montel's avatar
Laurent Montel committed
193
    connect(actNewPost, &QAction::triggered, this, &MainWindow::slotCreateNewPost);
194

Laurent Montel's avatar
Laurent Montel committed
195 196
    QAction *actAddBlog = new QAction(QIcon::fromTheme(QStringLiteral("list-add")), i18n("Add Blog..."), this);
    actionCollection()->addAction(QStringLiteral("add_blog"), actAddBlog);
Laurent Montel's avatar
Laurent Montel committed
197
    connect(actAddBlog, &QAction::triggered, this, &MainWindow::addBlog);
198

Laurent Montel's avatar
Laurent Montel committed
199 200
    QAction *actPublish = new QAction(QIcon::fromTheme(QStringLiteral("arrow-up")), i18n("Submit..."), this);
    actionCollection()->addAction(QStringLiteral("publish_post"), actPublish);
Laurent Montel's avatar
Laurent Montel committed
201
    connect(actPublish, &QAction::triggered, this, &MainWindow::slotPublishPost);
202

Laurent Montel's avatar
Laurent Montel committed
203 204
    QAction *actUpload = new QAction(QIcon::fromTheme(QStringLiteral("upload-media")), i18n("Upload Media..."), this);
    actionCollection()->addAction(QStringLiteral("upload_media"), actUpload);
Laurent Montel's avatar
Laurent Montel committed
205
    connect(actUpload, &QAction::triggered, this, &MainWindow::uploadMediaObject);
206

Laurent Montel's avatar
Laurent Montel committed
207 208
    QAction *actSaveLocally = new QAction(QIcon::fromTheme(QStringLiteral("document-save")), i18n("Save Locally"), this);
    actionCollection()->addAction(QStringLiteral("save_locally"), actSaveLocally);
Laurent Montel's avatar
Laurent Montel committed
209
    actionCollection()->setDefaultShortcut(actSaveLocally, QKeySequence(Qt::CTRL + Qt::Key_S));
Laurent Montel's avatar
Laurent Montel committed
210
    connect(actSaveLocally, &QAction::triggered, this, &MainWindow::slotSavePostLocally);
211

Laurent Montel's avatar
Laurent Montel committed
212
    KToggleAction *actToggleToolboxVisible = new KToggleAction(i18n("Show Toolbox"), this);
Laurent Montel's avatar
Laurent Montel committed
213
    actionCollection()->addAction(QStringLiteral("toggle_toolbox"), actToggleToolboxVisible);
Laurent Montel's avatar
Laurent Montel committed
214
    actionCollection()->setDefaultShortcut(actToggleToolboxVisible, QKeySequence(Qt::CTRL + Qt::Key_T));
Laurent Montel's avatar
Laurent Montel committed
215 216
    connect(actToggleToolboxVisible, &KToggleAction::toggled, this, &MainWindow::slotToggleToolboxVisible);
    connect(toolboxDock, &QDockWidget::visibilityChanged, this, &MainWindow::slotToolboxVisibilityChanged);
217

Laurent Montel's avatar
Laurent Montel committed
218
    blogs = new KSelectAction(this);
Laurent Montel's avatar
Laurent Montel committed
219
    actionCollection()->addAction(QStringLiteral("blogs_list"), blogs);
220

Laurent Montel's avatar
Laurent Montel committed
221 222
    QAction *actOpenBlog = new QAction(QIcon::fromTheme(QStringLiteral("applications-internet")), i18n("Open in browser"), this);
    actionCollection()->addAction(QStringLiteral("open_blog_in_browser"), actOpenBlog);
223
    actOpenBlog->setToolTip(i18n("Open current blog in browser"));
Laurent Montel's avatar
Laurent Montel committed
224
    connect(actOpenBlog, &QAction::triggered, this, &MainWindow::slotOpenCurrentBlogInBrowser);
225

226 227 228 229
}

void MainWindow::loadTempPosts()
{
Laurent Montel's avatar
Laurent Montel committed
230
    qCDebug(BLOGILO_LOG);
Laurent Montel's avatar
Laurent Montel committed
231
    QMap<BilboPost *, int> tempList = DBMan::self()->listTempPosts();
Laurent Montel's avatar
Laurent Montel committed
232
    const int count = tempList.count();
Laurent Montel's avatar
Laurent Montel committed
233 234 235 236
    if (count > 0) {
        QMap<BilboPost *, int>::ConstIterator it = tempList.constBegin();
        QMap<BilboPost *, int>::ConstIterator endIt = tempList.constEnd();
        for (; it != endIt; ++it) {
237 238 239
            createPostEntry(it.value(), (*it.key()));
        }
    } else {
240
        slotCreateNewPost();
241 242 243
    }
//     activePost = qobject_cast<PostEntry*>( tabPosts->currentWidget() );
    previousActivePostIndex = 0;
Laurent Montel's avatar
Laurent Montel committed
244 245 246
    if (activePost) {
        setCurrentBlog(activePost->currentPostBlogId());
    }
247 248
}

Laurent Montel's avatar
Laurent Montel committed
249
void MainWindow::setCurrentBlog(int blog_id)
250
{
Laurent Montel's avatar
Laurent Montel committed
251
    qCDebug(BLOGILO_LOG) << blog_id;
252
    if (blog_id == -1) {
Laurent Montel's avatar
Laurent Montel committed
253 254
        blogs->setCurrentItem(-1);
        toolbox->setCurrentBlogId(blog_id);
255 256 257
//         actionCollection()->action("publish_post")->setEnabled( false );
        return;
    }
Laurent Montel's avatar
Laurent Montel committed
258
    const int count = blogs->items().count();
Laurent Montel's avatar
Laurent Montel committed
259 260 261 262
    for (int i = 0; i < count; ++i) {
        if (blogs->action(i)->data().toInt() == blog_id) {
            blogs->setCurrentItem(i);
            currentBlogChanged(blogs->action(i));
263 264 265 266 267
            break;
        }
    }
}

Laurent Montel's avatar
Laurent Montel committed
268
void MainWindow::currentBlogChanged(QAction *act)
269
{
Laurent Montel's avatar
Laurent Montel committed
270 271
    if (act) {
        if (mCurrentBlogId == act->data().toInt()) {
272
            return;
Laurent Montel's avatar
Laurent Montel committed
273
        }
274 275
        mCurrentBlogId = act->data().toInt();
//         __currentBlogId = mCurrentBlogId;
Laurent Montel's avatar
Laurent Montel committed
276
        if (activePost) {
277
//             actionCollection()->action("publish_post")->setEnabled( true );
Laurent Montel's avatar
Laurent Montel committed
278
            activePost->setCurrentPostBlogId(mCurrentBlogId);
279 280 281
        } else {
//             actionCollection()->action("publish_post")->setEnabled( false );
        }
Laurent Montel's avatar
Laurent Montel committed
282
        blogs->setToolTip(DBMan::self()->blogList().value(mCurrentBlogId)->blogUrl());
283 284
    } else {
        mCurrentBlogId = -1;
Laurent Montel's avatar
Laurent Montel committed
285 286 287
        if (activePost) {
            activePost->setCurrentPostBlogId(mCurrentBlogId);
        }
288
    }
Laurent Montel's avatar
Laurent Montel committed
289
    toolbox->setCurrentBlogId(mCurrentBlogId);
290 291
}

292
void MainWindow::slotCreateNewPost()
293
{
Laurent Montel's avatar
Laurent Montel committed
294
    qCDebug(BLOGILO_LOG);
295

Laurent Montel's avatar
Laurent Montel committed
296 297 298 299 300
    tabPosts->setCurrentWidget(createPostEntry(mCurrentBlogId, BilboPost()));
    if (mCurrentBlogId == -1) {
        if (!blogs->items().isEmpty()) {
            blogs->setCurrentItem(0);
            currentBlogChanged(blogs->action(0));
301 302
        }
    }
Laurent Montel's avatar
Laurent Montel committed
303
    if (this->isVisible() == false) {
304 305 306 307 308 309 310 311 312 313 314
        this->show();
    }
}

void MainWindow::optionsPreferences()
{
    // The preference dialog is derived from prefs_base.ui
    //
    // compare the names of the widgets in the .ui file
    // to the names of the variables in the .kcfg file
    //avoid having 2 dialogs shown
Laurent Montel's avatar
Laurent Montel committed
315
    if (KConfigDialog::showDialog(QStringLiteral("settings")))  {
316 317
        return;
    }
318
    ConfigureDialog *dialog = new ConfigureDialog(this, QStringLiteral("settings"), Settings::self());
Laurent Montel's avatar
Laurent Montel committed
319 320
    connect(dialog, &ConfigureDialog::blogAdded, this, &MainWindow::slotBlogAdded);
    connect(dialog, &ConfigureDialog::blogEdited, this, &MainWindow::slotBlogEdited);
Laurent Montel's avatar
Laurent Montel committed
321
    connect(dialog, &ConfigureDialog::blogRemoved, this, &MainWindow::slotBlogRemoved);
Laurent Montel's avatar
Laurent Montel committed
322 323 324 325
    connect(dialog, &KConfigDialog::settingsChanged, this, &MainWindow::settingsChanged);
    connect(dialog, &KConfigDialog::settingsChanged, this, &MainWindow::slotSettingsChanged);
    connect(dialog, &ConfigureDialog::dialogDestroyed, this, &MainWindow::slotDialogDestroyed);
    connect(dialog, &ConfigureDialog::configurationChanged, this, &MainWindow::slotSettingsChanged);
326 327 328 329 330 331 332 333
    dialog->show();
}

void MainWindow::slotSettingsChanged()
{
    setupSystemTray();
}

Laurent Montel's avatar
Laurent Montel committed
334
void MainWindow::slotDialogDestroyed(QObject *win)
335
{
Laurent Montel's avatar
Cleanup  
Laurent Montel committed
336 337
    const QSize size = qobject_cast<QWidget *>(win)->size();
    const QString name = win->objectName();
338
    if (name == QLatin1String("settings")) {
Laurent Montel's avatar
Laurent Montel committed
339
        Settings::setConfigWindowSize(size);
340 341 342 343 344
    }
}

void MainWindow::addBlog()
{
Laurent Montel's avatar
Laurent Montel committed
345 346 347
    AddEditBlog *addEditBlogWindow = new AddEditBlog(-1, this);
    addEditBlogWindow->setWindowModality(Qt::ApplicationModal);
    addEditBlogWindow->setAttribute(Qt::WA_DeleteOnClose);
Laurent Montel's avatar
Laurent Montel committed
348
    connect(addEditBlogWindow, &AddEditBlog::sigBlogAdded, this, &MainWindow::slotBlogAdded);
349 350 351
    addEditBlogWindow->show();
}

Laurent Montel's avatar
Laurent Montel committed
352
void MainWindow::slotBlogAdded(const BilboBlog &blog)
353
{
Laurent Montel's avatar
Laurent Montel committed
354 355 356 357 358 359
    QAction *act = new QAction(blog.title(), blogs);
    act->setCheckable(true);
    act->setData(blog.id());
    blogs->addAction(act);
    blogs->setCurrentAction(act);
    currentBlogChanged(act);
360
    toolbox->slotReloadCategoryList();
Laurent Montel's avatar
Laurent Montel committed
361
    toolbox->slotUpdateEntries(20);
362 363
}

Laurent Montel's avatar
Laurent Montel committed
364
void MainWindow::slotBlogEdited(const BilboBlog &blog)
365
{
Laurent Montel's avatar
Cleanup  
Laurent Montel committed
366
    const int count = blogs->actions().count();
Laurent Montel's avatar
Laurent Montel committed
367 368 369
    for (int i = 0; i < count; ++i) {
        if (blogs->action(i)->data().toInt() == blog.id()) {
            blogs->action(i)->setText(blog.title());
370 371 372 373 374
            break;
        }
    }
}

Laurent Montel's avatar
Laurent Montel committed
375
void MainWindow::slotBlogRemoved(int blog_id)
376
{
Laurent Montel's avatar
Laurent Montel committed
377
    const int count = blogs->actions().count();
Laurent Montel's avatar
Laurent Montel committed
378 379 380 381 382
    for (int i = 0; i < count; ++i) {
        if (blogs->action(i)->data().toInt() == blog_id) {
            if (blogs->currentItem() == i) {
                blogs->setCurrentItem(i - 1);
                currentBlogChanged(blogs->action(i - 1));
383
            }
Laurent Montel's avatar
Laurent Montel committed
384 385
            blogs->removeAction(blogs->action(i));
            if (blogs->currentItem() == -1) {
386
                toolbox->clearFields();
Laurent Montel's avatar
Laurent Montel committed
387
            }
388 389 390 391 392 393 394
            break;
        }
    }
}

void MainWindow::setupSystemTray()
{
Laurent Montel's avatar
Laurent Montel committed
395 396 397
    if (Settings::enableSysTrayIcon()) {
        if (!systemTray) {
            systemTray = new KStatusNotifierItem(this);
Laurent Montel's avatar
Laurent Montel committed
398 399 400
            systemTray->setIconByName(QStringLiteral("blogilo"));
            systemTray->setToolTip(QStringLiteral("blogilo"), i18n("Blogilo"), i18n("A KDE Blogging Client"));
            systemTray->contextMenu()->addAction(actionCollection()->action(QStringLiteral("new_post")));
401 402
            systemTray->setCategory(KStatusNotifierItem::ApplicationStatus);
            systemTray->setStatus(KStatusNotifierItem::Active);
403
        }
Laurent Montel's avatar
Laurent Montel committed
404
    } else if (systemTray) {
405
        systemTray->deleteLater();
Laurent Montel's avatar
Laurent Montel committed
406
        systemTray = Q_NULLPTR;
407 408 409
    }
}

Laurent Montel's avatar
Laurent Montel committed
410
void MainWindow::slotPostTitleChanged(const QString &title)
411
{
Laurent Montel's avatar
Laurent Montel committed
412
//     qCDebug(BLOGILO_LOG);
Laurent Montel's avatar
Laurent Montel committed
413
    tabPosts->setTabText(tabPosts->currentIndex(), title);
414 415
}

Laurent Montel's avatar
Laurent Montel committed
416
void MainWindow::slotToggleToolboxVisible(bool isVisible)
417
{
Laurent Montel's avatar
Laurent Montel committed
418
    toolboxDock->setVisible(isVisible);
419 420 421 422
}

void MainWindow::slotToolboxVisibilityChanged(bool)
{
Laurent Montel's avatar
Laurent Montel committed
423
    actionCollection()->action(QStringLiteral("toggle_toolbox"))->setChecked(toolboxDock->isVisibleTo(this));
424 425
}

Laurent Montel's avatar
Laurent Montel committed
426
void MainWindow::slotActivePostChanged(int index)
427
{
Laurent Montel's avatar
Laurent Montel committed
428
    qCDebug(BLOGILO_LOG) << "new post index: " << index << "\tPrev Index: " << previousActivePostIndex;
429

Laurent Montel's avatar
Laurent Montel committed
430 431
    activePost = qobject_cast<PostEntry *>(tabPosts->widget(index));
    PostEntry *prevActivePost = qobject_cast<PostEntry *>(tabPosts->widget(previousActivePostIndex));
432 433 434
    int activePostBlogId = -1;
    int prevPostBlogId = -1;

Laurent Montel's avatar
Laurent Montel committed
435
    if ((prevActivePost != Q_NULLPTR) && (index != previousActivePostIndex)) {
436
        prevPostBlogId = prevActivePost->currentPostBlogId();
Laurent Montel's avatar
Laurent Montel committed
437 438
        toolbox->getFieldsValue(prevActivePost->currentPost());
        prevActivePost->setCurrentPostBlogId(mCurrentBlogId);
439 440
    }

Laurent Montel's avatar
Laurent Montel committed
441
    if (index >= 0) {
442
        activePostBlogId = activePost->currentPostBlogId();
Laurent Montel's avatar
Laurent Montel committed
443 444
        if (activePostBlogId != -1 && activePostBlogId != prevPostBlogId) {
            setCurrentBlog(activePostBlogId);
445
        }
Laurent Montel's avatar
Laurent Montel committed
446
        toolbox->setFieldsValue(activePost->currentPost());
447
    } else {
Laurent Montel's avatar
Laurent Montel committed
448
        qCCritical(BLOGILO_LOG) << "ActivePost is NULL! tabPosts Current index is: " << tabPosts->currentIndex();
449 450 451 452
    }
    previousActivePostIndex = index;
}

453
void MainWindow::slotPublishPost()
454
{
Laurent Montel's avatar
Laurent Montel committed
455
    qCDebug(BLOGILO_LOG);
Laurent Montel's avatar
Laurent Montel committed
456 457
    if (mCurrentBlogId == -1) {
        KMessageBox::sorry(this, i18n("You have to select a blog to publish this post to."));
458 459
        return;
    }
Laurent Montel's avatar
Laurent Montel committed
460 461
    if (!activePost || tabPosts->currentIndex() == -1) {
        KMessageBox::sorry(this, i18n("There is no open post to submit."));
462 463
        return;
    }
Laurent Montel's avatar
Laurent Montel committed
464 465
    toolbox->getFieldsValue(activePost->currentPost());
    activePost->submitPost(mCurrentBlogId, *activePost->currentPost());
466 467
}

468 469
void MainWindow::slotRemoveAllExclude(int pos)
{
Laurent Montel's avatar
Laurent Montel committed
470
    for (int i = tabPosts->count() - 1; i >= 0; --i) {
471 472 473
        if (i == pos) {
            continue;
        }
Laurent Montel's avatar
Laurent Montel committed
474 475 476
        PostEntry *widget = qobject_cast<PostEntry *>(tabPosts->widget(i));
        if (!widget) {
            if (activePost) {
477
                widget = activePost;
Laurent Montel's avatar
Laurent Montel committed
478
            } else {
479
                return;
Laurent Montel's avatar
Laurent Montel committed
480
            }
481
        }
Laurent Montel's avatar
Laurent Montel committed
482
        DBMan::self()->removeTempEntry(*widget->currentPost());
Laurent Montel's avatar
Laurent Montel committed
483
        tabPosts->removeTab(tabPosts->indexOf(widget));
484 485
        widget->close();
    }
Laurent Montel's avatar
Laurent Montel committed
486
    if (tabPosts->count() < 1) {
Laurent Montel's avatar
Laurent Montel committed
487
        activePost = Q_NULLPTR;
488 489 490 491 492
        toolbox->resetFields();
//         actionCollection()->action("publish_post")->setEnabled( false );
    }
}

Laurent Montel's avatar
Laurent Montel committed
493
void MainWindow::slotRemovePostEntry(int pos)
494
{
Laurent Montel's avatar
Laurent Montel committed
495
    qCDebug(BLOGILO_LOG);
496

Laurent Montel's avatar
Laurent Montel committed
497 498 499 500
    PostEntry *widget = qobject_cast<PostEntry *>(tabPosts->widget(pos));

    if (!widget) {
        if (activePost) {
501
            widget = activePost;
Laurent Montel's avatar
Laurent Montel committed
502
        } else {
503
            return;
Laurent Montel's avatar
Laurent Montel committed
504
        }
505
    }
Laurent Montel's avatar
Laurent Montel committed
506
    DBMan::self()->removeTempEntry(*widget->currentPost());
Laurent Montel's avatar
Laurent Montel committed
507
    tabPosts->removeTab(tabPosts->indexOf(widget));
508 509
    widget->close();

Laurent Montel's avatar
Laurent Montel committed
510
    if (tabPosts->count() < 1) {
Laurent Montel's avatar
Laurent Montel committed
511
        activePost = Q_NULLPTR;
512 513 514 515 516
        toolbox->resetFields();
//         actionCollection()->action("publish_post")->setEnabled( false );
    }
}

Laurent Montel's avatar
Laurent Montel committed
517
void MainWindow::slotNewPostOpened(BilboPost &newPost, int blog_id)
518
{
Laurent Montel's avatar
Laurent Montel committed
519
    qCDebug(BLOGILO_LOG);
Laurent Montel's avatar
Laurent Montel committed
520 521
    QWidget *w = createPostEntry(blog_id, newPost);
    tabPosts->setCurrentWidget(w);
522 523
}

524
void MainWindow::slotSavePostLocally()
525
{
Laurent Montel's avatar
Laurent Montel committed
526
    qCDebug(BLOGILO_LOG);
Laurent Montel's avatar
Laurent Montel committed
527
    if (activePost && (tabPosts->count() > 0)) {
528
        toolbox->getFieldsValue(activePost->currentPost());
529 530 531 532 533
        activePost->saveLocally();
        toolbox->reloadLocalPosts();
    }
}

Laurent Montel's avatar
Laurent Montel committed
534
void MainWindow::slotError(const QString &errorMessage)
535
{
Laurent Montel's avatar
Laurent Montel committed
536
    qCDebug(BLOGILO_LOG) << "Error message: " << errorMessage;
Laurent Montel's avatar
Laurent Montel committed
537
    KMessageBox::detailedError(this, i18n("An error occurred in the last transaction."), errorMessage);
538 539 540 541 542 543
    statusBar()->clearMessage();
    slotBusy(false);
}

void MainWindow::writeConfigs()
{
Laurent Montel's avatar
Laurent Montel committed
544
    qCDebug(BLOGILO_LOG);
Laurent Montel's avatar
Laurent Montel committed
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
    if (toolboxDock->isVisible()) {
        Settings::setShowToolboxOnStart(true);
    } else {
        Settings::setShowToolboxOnStart(false);
    }
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    if (event->modifiers() == Qt::CTRL) {
        switch (event->key()) {
        case  Qt::Key_1:
            toolbox->setCurrentPage(0);
            break;
        case Qt::Key_2:
            toolbox->setCurrentPage(1);
            break;
        case Qt::Key_3:
            toolbox->setCurrentPage(2);
            break;
        case Qt::Key_4:
            toolbox->setCurrentPage(3);
            break;
        case Qt::Key_5:
            toolbox->setCurrentPage(4);
            break;
        case Qt::Key_W:
            slotRemovePostEntry(tabPosts->currentIndex());
            break;
        default:
            KXmlGuiWindow::keyPressEvent(event);
            break;
577 578 579 580
        }
    }
}

Laurent Montel's avatar
Laurent Montel committed
581
void MainWindow::postManipulationDone(bool isError, const QString &customMessage)
582
{
Laurent Montel's avatar
Laurent Montel committed
583
    qCDebug(BLOGILO_LOG);
Laurent Montel's avatar
Laurent Montel committed
584
    if (isError) {
Yuri Chornoivan's avatar
Yuri Chornoivan committed
585
        KMessageBox::detailedError(this, i18n("Submitting post failed"), customMessage);
586
    } else {
Laurent Montel's avatar
Laurent Montel committed
587 588 589
        PostEntry *entry = qobject_cast<PostEntry *>(sender());
        if (entry) {
            if (KMessageBox::questionYesNo(this, i18n("%1\nDo you want to keep the post open?", customMessage),
Laurent Montel's avatar
Laurent Montel committed
590
                                           QString(), KStandardGuiItem::yes(), KStandardGuiItem::no(), QStringLiteral("KeepPostOpen")) == KMessageBox::No) {
Laurent Montel's avatar
Laurent Montel committed
591 592 593 594
                slotRemovePostEntry(tabPosts->indexOf(entry));
            } else {
                toolbox->setFieldsValue(entry->currentPost());
            }
595
        }
Laurent Montel's avatar
Laurent Montel committed
596
        toolbox->slotLoadEntriesFromDB(mCurrentBlogId);
597 598 599 600 601 602 603
    }
    this->unsetCursor();
    toolbox->unsetCursor();
}

void MainWindow::slotBusy(bool isBusy)
{
Laurent Montel's avatar
Laurent Montel committed
604
    qCDebug(BLOGILO_LOG) << "isBusy=" << isBusy << "\tbusyNumber=" << busyNumber;
Laurent Montel's avatar
Laurent Montel committed
605
    if (isBusy) {
606
        this->setCursor(Qt::BusyCursor);
Laurent Montel's avatar
Laurent Montel committed
607
        toolbox->setCursor(Qt::BusyCursor);
608
        ++busyNumber;
Laurent Montel's avatar
Laurent Montel committed
609
        if (!progress) {
610
            progress = new QProgressBar(statusBar());
Laurent Montel's avatar
Laurent Montel committed
611 612
            progress->setMinimum(0);
            progress->setMaximum(0);
613 614 615 616 617
            progress->setFixedSize(250, 17);
            statusBar()->addPermanentWidget(progress);
        }
    } else {
        --busyNumber;
Laurent Montel's avatar
Laurent Montel committed
618
        if (busyNumber < 1) {
619 620
            this->unsetCursor();
            toolbox->unsetCursor();
Laurent Montel's avatar
Laurent Montel committed
621
            if (progress) {
622 623
                statusBar()->removeWidget(progress);
                progress->deleteLater();
Laurent Montel's avatar
Laurent Montel committed
624
                progress = Q_NULLPTR;
625 626 627 628 629 630
            }
//             busyNumber = 0;
        }
    }
}

Laurent Montel's avatar
Laurent Montel committed
631
QWidget *MainWindow::createPostEntry(int blog_id, const BilboPost &post)
632
{
Laurent Montel's avatar
Laurent Montel committed
633
    qCDebug(BLOGILO_LOG);
Laurent Montel's avatar
Laurent Montel committed
634 635
    PostEntry *temp = new PostEntry(this);
    temp->setAttribute(Qt::WA_DeleteOnClose);
636
    temp->setCurrentPost(post);
Laurent Montel's avatar
Laurent Montel committed
637
    temp->setCurrentPostBlogId(blog_id);
638

Laurent Montel's avatar
Laurent Montel committed
639 640
    connect(temp, &PostEntry::postTitleChanged, this, &MainWindow::slotPostTitleChanged);
    connect(temp, &PostEntry::postPublishingDone, this, &MainWindow::postManipulationDone);
Laurent Montel's avatar
Laurent Montel committed
641
    connect(this, &MainWindow::settingsChanged, temp, &PostEntry::settingsChanged);
Laurent Montel's avatar
Laurent Montel committed
642
    connect(temp, &PostEntry::showStatusMessage, this, &MainWindow::slotShowStatusMessage);
643

Laurent Montel's avatar
Laurent Montel committed
644
    connect(temp, &PostEntry::sigBusy, this, &MainWindow::slotBusy);
645

Laurent Montel's avatar
Laurent Montel committed
646
    tabPosts->addTab(temp, post.title());
647 648 649 650 651 652 653 654 655 656 657
    return temp;
}

void MainWindow::slotShowStatusMessage(const QString &message, bool isPermanent)
{
    statusBar()->showMessage(message, (isPermanent ? 0 : TIMEOUT));
}

void MainWindow::uploadMediaObject()
{
    UploadMediaDialog *uploadDlg = new UploadMediaDialog(this);
Laurent Montel's avatar
Laurent Montel committed
658
    connect(uploadDlg, &UploadMediaDialog::sigBusy, this, &MainWindow::slotBusy);
Laurent Montel's avatar
Laurent Montel committed
659
    if (mCurrentBlogId == -1) {
Laurent Montel's avatar
Laurent Montel committed
660
        uploadDlg->init(Q_NULLPTR);
Laurent Montel's avatar
Laurent Montel committed
661 662 663
    } else {
        uploadDlg->init(DBMan::self()->blog(mCurrentBlogId));
    }
664 665
}

666 667
void MainWindow::slotOpenCurrentBlogInBrowser()
{
Mehrdad Momeny's avatar
Mehrdad Momeny committed
668
    if (mCurrentBlogId > -1) {
Laurent Montel's avatar
KUrl--  
Laurent Montel committed
669
        QUrl url(DBMan::self()->blog(mCurrentBlogId)->blogUrl());
Laurent Montel's avatar
Laurent Montel committed
670
        if (url.isValid()) {
Laurent Montel's avatar
Laurent Montel committed
671
            QDesktopServices::openUrl(url);
Laurent Montel's avatar
Laurent Montel committed
672
        } else {
Mehrdad Momeny's avatar
Mehrdad Momeny committed
673
            KMessageBox::sorry(this, i18n("Cannot find current blog URL."));
Laurent Montel's avatar
Laurent Montel committed
674
        }
675 676
    }
    ///TODO
Laurent Montel's avatar
Cleanup  
Laurent Montel committed
677
    ///else show a message to the user saying that a blog should be selected before.
678 679
}