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

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

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

291
void MainWindow::slotCreateNewPost()
292
{
Laurent Montel's avatar
Laurent Montel committed
293 294 295 296 297
    tabPosts->setCurrentWidget(createPostEntry(mCurrentBlogId, BilboPost()));
    if (mCurrentBlogId == -1) {
        if (!blogs->items().isEmpty()) {
            blogs->setCurrentItem(0);
            currentBlogChanged(blogs->action(0));
298 299
        }
    }
Laurent Montel's avatar
Laurent Montel committed
300
    if (this->isVisible() == false) {
301 302 303 304 305 306 307 308 309 310 311
        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
312
    if (KConfigDialog::showDialog(QStringLiteral("settings")))  {
313 314
        return;
    }
315
    ConfigureDialog *dialog = new ConfigureDialog(this, QStringLiteral("settings"), Settings::self());
Laurent Montel's avatar
Laurent Montel committed
316 317
    connect(dialog, &ConfigureDialog::blogAdded, this, &MainWindow::slotBlogAdded);
    connect(dialog, &ConfigureDialog::blogEdited, this, &MainWindow::slotBlogEdited);
Laurent Montel's avatar
Laurent Montel committed
318
    connect(dialog, &ConfigureDialog::blogRemoved, this, &MainWindow::slotBlogRemoved);
Laurent Montel's avatar
Laurent Montel committed
319 320 321 322
    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);
323 324 325 326 327 328 329 330
    dialog->show();
}

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

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

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

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

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

Laurent Montel's avatar
Laurent Montel committed
372
void MainWindow::slotBlogRemoved(int blog_id)
373
{
Laurent Montel's avatar
Laurent Montel committed
374
    const int count = blogs->actions().count();
Laurent Montel's avatar
Laurent Montel committed
375 376 377 378 379
    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));
380
            }
Laurent Montel's avatar
Laurent Montel committed
381 382
            blogs->removeAction(blogs->action(i));
            if (blogs->currentItem() == -1) {
383
                toolbox->clearFields();
Laurent Montel's avatar
Laurent Montel committed
384
            }
385 386 387 388 389 390 391
            break;
        }
    }
}

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

Laurent Montel's avatar
Laurent Montel committed
407
void MainWindow::slotPostTitleChanged(const QString &title)
408
{
Laurent Montel's avatar
Laurent Montel committed
409
    tabPosts->setTabText(tabPosts->currentIndex(), title);
410 411
}

Laurent Montel's avatar
Laurent Montel committed
412
void MainWindow::slotToggleToolboxVisible(bool isVisible)
413
{
Laurent Montel's avatar
Laurent Montel committed
414
    toolboxDock->setVisible(isVisible);
415 416 417 418
}

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

Laurent Montel's avatar
Laurent Montel committed
422
void MainWindow::slotActivePostChanged(int index)
423
{
Laurent Montel's avatar
Laurent Montel committed
424
    qCDebug(BLOGILO_LOG) << "new post index: " << index << "\tPrev Index: " << previousActivePostIndex;
425

Laurent Montel's avatar
Laurent Montel committed
426 427
    activePost = qobject_cast<PostEntry *>(tabPosts->widget(index));
    PostEntry *prevActivePost = qobject_cast<PostEntry *>(tabPosts->widget(previousActivePostIndex));
428 429 430
    int activePostBlogId = -1;
    int prevPostBlogId = -1;

Laurent Montel's avatar
Laurent Montel committed
431
    if ((prevActivePost != Q_NULLPTR) && (index != previousActivePostIndex)) {
432
        prevPostBlogId = prevActivePost->currentPostBlogId();
Laurent Montel's avatar
Laurent Montel committed
433 434
        toolbox->getFieldsValue(prevActivePost->currentPost());
        prevActivePost->setCurrentPostBlogId(mCurrentBlogId);
435 436
    }

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

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

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

Laurent Montel's avatar
Laurent Montel committed
488
void MainWindow::slotRemovePostEntry(int pos)
489
{
Laurent Montel's avatar
Laurent Montel committed
490

491

Laurent Montel's avatar
Laurent Montel committed
492 493 494 495
    PostEntry *widget = qobject_cast<PostEntry *>(tabPosts->widget(pos));

    if (!widget) {
        if (activePost) {
496
            widget = activePost;
Laurent Montel's avatar
Laurent Montel committed
497
        } else {
498
            return;
Laurent Montel's avatar
Laurent Montel committed
499
        }
500
    }
Laurent Montel's avatar
Laurent Montel committed
501
    DBMan::self()->removeTempEntry(*widget->currentPost());
Laurent Montel's avatar
Laurent Montel committed
502
    tabPosts->removeTab(tabPosts->indexOf(widget));
503 504
    widget->close();

Laurent Montel's avatar
Laurent Montel committed
505
    if (tabPosts->count() < 1) {
Laurent Montel's avatar
Laurent Montel committed
506
        activePost = Q_NULLPTR;
507 508 509 510 511
        toolbox->resetFields();
//         actionCollection()->action("publish_post")->setEnabled( false );
    }
}

Laurent Montel's avatar
Laurent Montel committed
512
void MainWindow::slotNewPostOpened(BilboPost &newPost, int blog_id)
513
{
Laurent Montel's avatar
Laurent Montel committed
514

Laurent Montel's avatar
Laurent Montel committed
515 516
    QWidget *w = createPostEntry(blog_id, newPost);
    tabPosts->setCurrentWidget(w);
517 518
}

519
void MainWindow::slotSavePostLocally()
520
{
Laurent Montel's avatar
Laurent Montel committed
521

Laurent Montel's avatar
Laurent Montel committed
522
    if (activePost && (tabPosts->count() > 0)) {
523
        toolbox->getFieldsValue(activePost->currentPost());
524 525 526 527 528
        activePost->saveLocally();
        toolbox->reloadLocalPosts();
    }
}

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

void MainWindow::writeConfigs()
{
Laurent Montel's avatar
Laurent Montel committed
539

Laurent Montel's avatar
Laurent Montel committed
540 541 542 543 544 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
    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;
572 573 574 575
        }
    }
}

Laurent Montel's avatar
Laurent Montel committed
576
void MainWindow::postManipulationDone(bool isError, const QString &customMessage)
577
{
Laurent Montel's avatar
Laurent Montel committed
578

Laurent Montel's avatar
Laurent Montel committed
579
    if (isError) {
Yuri Chornoivan's avatar
Yuri Chornoivan committed
580
        KMessageBox::detailedError(this, i18n("Submitting post failed"), customMessage);
581
    } else {
Laurent Montel's avatar
Laurent Montel committed
582 583 584
        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
585
                                           QString(), KStandardGuiItem::yes(), KStandardGuiItem::no(), QStringLiteral("KeepPostOpen")) == KMessageBox::No) {
Laurent Montel's avatar
Laurent Montel committed
586 587 588 589
                slotRemovePostEntry(tabPosts->indexOf(entry));
            } else {
                toolbox->setFieldsValue(entry->currentPost());
            }
590
        }
Laurent Montel's avatar
Laurent Montel committed
591
        toolbox->slotLoadEntriesFromDB(mCurrentBlogId);
592 593 594 595 596 597 598
    }
    this->unsetCursor();
    toolbox->unsetCursor();
}

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

Laurent Montel's avatar
Laurent Montel committed
626
QWidget *MainWindow::createPostEntry(int blog_id, const BilboPost &post)
627
{
Laurent Montel's avatar
Laurent Montel committed
628

Laurent Montel's avatar
Laurent Montel committed
629 630
    PostEntry *temp = new PostEntry(this);
    temp->setAttribute(Qt::WA_DeleteOnClose);
631
    temp->setCurrentPost(post);
Laurent Montel's avatar
Laurent Montel committed
632
    temp->setCurrentPostBlogId(blog_id);
633

Laurent Montel's avatar
Laurent Montel committed
634 635
    connect(temp, &PostEntry::postTitleChanged, this, &MainWindow::slotPostTitleChanged);
    connect(temp, &PostEntry::postPublishingDone, this, &MainWindow::postManipulationDone);
Laurent Montel's avatar
Laurent Montel committed
636
    connect(this, &MainWindow::settingsChanged, temp, &PostEntry::settingsChanged);
Laurent Montel's avatar
Laurent Montel committed
637
    connect(temp, &PostEntry::showStatusMessage, this, &MainWindow::slotShowStatusMessage);
638

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

Laurent Montel's avatar
Laurent Montel committed
641
    tabPosts->addTab(temp, post.title());
642 643 644 645 646 647 648 649 650 651 652
    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
653
    connect(uploadDlg, &UploadMediaDialog::sigBusy, this, &MainWindow::slotBusy);
Laurent Montel's avatar
Laurent Montel committed
654
    if (mCurrentBlogId == -1) {
Laurent Montel's avatar
Laurent Montel committed
655
        uploadDlg->init(Q_NULLPTR);
Laurent Montel's avatar
Laurent Montel committed
656 657 658
    } else {
        uploadDlg->init(DBMan::self()->blog(mCurrentBlogId));
    }
659 660
}

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