mainwindow.cpp 24.8 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-2015 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 39
#include "configuredialog.h"
#include "storageservice/storageservicemanagersettingsjob.h"
40 41 42 43 44

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

45 46 47
#include "pimcommon/storageservice/storageservicemanager.h"
#include "pimcommon/storageservice/storageservicejobconfig.h"
#include "pimcommon/storageservice/storageserviceabstract.h"
48
#include "pimcommon/storageservice/storageserviceprogressmanager.h"
49

50 51
#include "libkdepim/progresswidget/progressdialog.h"
#include "libkdepim/progresswidget/statusbarprogresswidget.h"
52
#include "libkdepim/progresswidget/progressstatusbarwidget.h"
53

Laurent Montel's avatar
Laurent Montel committed
54
#include <qtabwidget.h>
55
#include <KStatusNotifierItem>
56
#include <qstatusbar.h>
57 58
#include <KToggleAction>
#include <kactioncollection.h>
59
#include <KActionMenu>
Laurent Montel's avatar
Port++  
Laurent Montel committed
60
#include <QAction>
61
#include <kconfigdialog.h>
Laurent Montel's avatar
Laurent Montel committed
62
#include "blogilo_debug.h"
63
#include <kmessagebox.h>
Laurent Montel's avatar
Laurent Montel committed
64
#include <KLocalizedString>
Laurent Montel's avatar
Laurent Montel committed
65 66
#include <KSelectAction>
#include <kimagefilepreview.h>
Laurent Montel's avatar
Port++  
Laurent Montel committed
67
#include <QMenu>
Laurent Montel's avatar
KUrl--  
Laurent Montel committed
68
#include <QUrl>
Laurent Montel's avatar
Laurent Montel committed
69

Laurent Montel's avatar
Laurent Montel committed
70 71
#include <QDockWidget>
#include <QProgressBar>
Mehrdad Momeny's avatar
Mehrdad Momeny committed
72
#include <QTimer>
73
#include <QKeyEvent>
Laurent Montel's avatar
Laurent Montel committed
74
#include <QDesktopServices>
75 76 77 78

#define TIMEOUT 5000

MainWindow::MainWindow()
Laurent Montel's avatar
Laurent Montel committed
79
    : KXmlGuiWindow(),
Laurent Montel's avatar
Laurent Montel committed
80 81
      activePost(Q_NULLPTR),
      systemTray(Q_NULLPTR),
Laurent Montel's avatar
Laurent Montel committed
82 83
      previousActivePostIndex(-1),
      busyNumber(0),
Laurent Montel's avatar
Laurent Montel committed
84
      progress(Q_NULLPTR),
Laurent Montel's avatar
Laurent Montel committed
85
      mCurrentBlogId(__currentBlogId)
86
{
Laurent Montel's avatar
Laurent Montel committed
87
    setWindowTitle(i18n("Blogilo"));
88
    initStorageService();
89

90
    tabPosts = new PostsTabWidget(this);
Laurent Montel's avatar
Laurent Montel committed
91
    setCentralWidget(tabPosts);
Laurent Montel's avatar
Laurent Montel committed
92 93 94 95
    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);
96

Laurent Montel's avatar
Laurent Montel committed
97 98 99 100 101
    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
102

Laurent Montel's avatar
Laurent Montel committed
103 104
    toolboxDock->setObjectName(QStringLiteral("dock_toolbox"));
    toolbox->setObjectName(QStringLiteral("toolbox"));
Laurent Montel's avatar
Laurent Montel committed
105

Laurent Montel's avatar
Laurent Montel committed
106
    this->addDockWidget(Qt::RightDockWidgetArea, toolboxDock);
107 108 109

    // then, setup our actions
    setupActions();
110
    setupStatusBar();
111 112 113 114 115 116 117 118

    // 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
119
    toolbox->setVisible(Settings::showToolboxOnStart());
Laurent Montel's avatar
Laurent Montel committed
120
    actionCollection()->action(QStringLiteral("toggle_toolbox"))->setChecked(Settings::showToolboxOnStart());
121 122 123

    setupSystemTray();

Laurent Montel's avatar
Laurent Montel committed
124
    connect(tabPosts, &PostsTabWidget::currentChanged, this, &MainWindow::slotActivePostChanged);
Laurent Montel's avatar
Laurent Montel committed
125 126 127
    connect(toolbox, &Toolbox::sigEntrySelected, this, &MainWindow::slotNewPostOpened);
    connect(toolbox, &Toolbox::sigError, this, &MainWindow::slotError);
    connect(toolbox, &Toolbox::sigBusy, this, &MainWindow::slotBusy);
128

129 130
    foreach (BilboBlog *blog, DBMan::self()->blogList()) {
        QAction *act = new QAction(blog->title(), blogs);
Laurent Montel's avatar
Laurent Montel committed
131
        act->setCheckable(true);
132
        act->setData(blog->id());
Laurent Montel's avatar
Laurent Montel committed
133
        blogs->addAction(act);
134
    }
Laurent Montel's avatar
Laurent Montel committed
135 136
    connect(blogs, static_cast<void (KSelectAction::*)(QAction *)>(&KSelectAction::triggered), this, &MainWindow::currentBlogChanged);
    QTimer::singleShot(0, this, SLOT(loadTempPosts()));
137 138 139 140
}

MainWindow::~MainWindow()
{
141 142
}

143 144
void MainWindow::setupStatusBar()
{
Laurent Montel's avatar
Laurent Montel committed
145 146
    KPIM::ProgressStatusBarWidget *progressStatusBarWidget = new KPIM::ProgressStatusBarWidget(statusBar(), this, PimCommon::StorageServiceProgressManager::progressTypeValue());
    statusBar()->addPermanentWidget(progressStatusBarWidget->littleProgress(), 0);
147 148 149
    statusBar()->show();
}

150 151
void MainWindow::initStorageService()
{
152
    StorageServiceManagerSettingsJob *settingsJob = new StorageServiceManagerSettingsJob;
153 154 155 156
    PimCommon::StorageServiceJobConfig *configJob = PimCommon::StorageServiceJobConfig::self();
    configJob->registerConfigIf(settingsJob);

    mStorageManager = new PimCommon::StorageServiceManager(this);
Laurent Montel's avatar
Laurent Montel committed
157 158 159 160
    connect(mStorageManager, &PimCommon::StorageServiceManager::uploadFileDone, this, &MainWindow::slotUploadFileDone);
    connect(mStorageManager, &PimCommon::StorageServiceManager::shareLinkDone, this, &MainWindow::slotUploadFileDone);
    connect(mStorageManager, &PimCommon::StorageServiceManager::uploadFileFailed, this, &MainWindow::slotUploadFileFailed);
    connect(mStorageManager, &PimCommon::StorageServiceManager::actionFailed, this, &MainWindow::slotActionFailed);
161 162 163 164 165 166 167 168 169 170 171 172 173
}

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"));
174 175
}

Laurent Montel's avatar
Laurent Montel committed
176 177 178 179 180
void MainWindow::slotActionFailed(const QString &serviceName, const QString &error)
{
    KMessageBox::error(this, i18n("%1 return an error '%2'", serviceName, error), i18n("Error"));
}

181 182 183 184 185 186 187 188
void MainWindow::slotCloseTabClicked()
{
    const int currentIndex = tabPosts->currentIndex();
    if (currentIndex != -1) {
        slotRemovePostEntry(currentIndex);
    }
}

189
void MainWindow::closeEvent(QCloseEvent *event)
190
{
191
    writeConfigs();
Laurent Montel's avatar
Laurent Montel committed
192
    if (!DBMan::self()->clearTempEntries()) {
Laurent Montel's avatar
Laurent Montel committed
193
        qCDebug(BLOGILO_LOG) << "Could not erase temp_post table: " << DBMan::self()->lastErrorText();
Laurent Montel's avatar
Laurent Montel committed
194
    }
Laurent Montel's avatar
Laurent Montel committed
195
    const int count = tabPosts->count();
196
    if (count > 0) {
Mehrdad Momeny's avatar
Mehrdad Momeny committed
197
        toolbox->getFieldsValue(activePost->currentPost());
Laurent Montel's avatar
Laurent Montel committed
198 199
        for (int i = 0; i < count; ++i) {
            PostEntry *pst = qobject_cast<PostEntry *>(tabPosts->widget(i));
Mehrdad Momeny's avatar
Mehrdad Momeny committed
200 201
            DBMan::self()->saveTempEntry(*pst->currentPost(), pst->currentPostBlogId());
        }
202
    }
203
    event->accept();
204 205 206 207
}

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

Laurent Montel's avatar
Laurent Montel committed
210
    KStandardAction::preferences(this, SLOT(optionsPreferences()), actionCollection());
211 212

    // custom menu and menu item
Laurent Montel's avatar
Laurent Montel committed
213 214
    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
215
    actionCollection()->setDefaultShortcut(actNewPost, QKeySequence(Qt::CTRL + Qt::Key_N));
Laurent Montel's avatar
Laurent Montel committed
216
    connect(actNewPost, &QAction::triggered, this, &MainWindow::slotCreateNewPost);
217

Laurent Montel's avatar
Laurent Montel committed
218 219
    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
220
    connect(actAddBlog, &QAction::triggered, this, &MainWindow::addBlog);
221

Laurent Montel's avatar
Laurent Montel committed
222 223
    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
224
    connect(actPublish, &QAction::triggered, this, &MainWindow::slotPublishPost);
225

Laurent Montel's avatar
Laurent Montel committed
226 227
    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
228
    connect(actUpload, &QAction::triggered, this, &MainWindow::uploadMediaObject);
229

Laurent Montel's avatar
Laurent Montel committed
230 231
    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
232
    actionCollection()->setDefaultShortcut(actSaveLocally, QKeySequence(Qt::CTRL + Qt::Key_S));
Laurent Montel's avatar
Laurent Montel committed
233
    connect(actSaveLocally, &QAction::triggered, this, &MainWindow::slotSavePostLocally);
234

Laurent Montel's avatar
Laurent Montel committed
235
    KToggleAction *actToggleToolboxVisible = new KToggleAction(i18n("Show Toolbox"), this);
Laurent Montel's avatar
Laurent Montel committed
236
    actionCollection()->addAction(QStringLiteral("toggle_toolbox"), actToggleToolboxVisible);
Laurent Montel's avatar
Laurent Montel committed
237
    actionCollection()->setDefaultShortcut(actToggleToolboxVisible, QKeySequence(Qt::CTRL + Qt::Key_T));
Laurent Montel's avatar
Laurent Montel committed
238 239
    connect(actToggleToolboxVisible, &KToggleAction::toggled, this, &MainWindow::slotToggleToolboxVisible);
    connect(toolboxDock, &QDockWidget::visibilityChanged, this, &MainWindow::slotToolboxVisibilityChanged);
240

Laurent Montel's avatar
Laurent Montel committed
241
    blogs = new KSelectAction(this);
Laurent Montel's avatar
Laurent Montel committed
242
    actionCollection()->addAction(QStringLiteral("blogs_list"), blogs);
243

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

Laurent Montel's avatar
Laurent Montel committed
249 250
    actionCollection()->addAction(QStringLiteral("upload_file"), mStorageManager->menuUploadServices(this));
    actionCollection()->addAction(QStringLiteral("download_file"), mStorageManager->menuDownloadServices(this));
Laurent Montel's avatar
Laurent Montel committed
251
    mStorageManager->setDefaultUploadFolder(Settings::self()->downloadDirectory());
252 253 254 255
}

void MainWindow::loadTempPosts()
{
Laurent Montel's avatar
Laurent Montel committed
256
    qCDebug(BLOGILO_LOG);
Laurent Montel's avatar
Laurent Montel committed
257
    QMap<BilboPost *, int> tempList = DBMan::self()->listTempPosts();
Laurent Montel's avatar
Laurent Montel committed
258
    const int count = tempList.count();
Laurent Montel's avatar
Laurent Montel committed
259 260 261 262
    if (count > 0) {
        QMap<BilboPost *, int>::ConstIterator it = tempList.constBegin();
        QMap<BilboPost *, int>::ConstIterator endIt = tempList.constEnd();
        for (; it != endIt; ++it) {
263 264 265
            createPostEntry(it.value(), (*it.key()));
        }
    } else {
266
        slotCreateNewPost();
267 268 269
    }
//     activePost = qobject_cast<PostEntry*>( tabPosts->currentWidget() );
    previousActivePostIndex = 0;
Laurent Montel's avatar
Laurent Montel committed
270 271 272
    if (activePost) {
        setCurrentBlog(activePost->currentPostBlogId());
    }
273 274
}

Laurent Montel's avatar
Laurent Montel committed
275
void MainWindow::setCurrentBlog(int blog_id)
276
{
Laurent Montel's avatar
Laurent Montel committed
277
    qCDebug(BLOGILO_LOG) << blog_id;
278
    if (blog_id == -1) {
Laurent Montel's avatar
Laurent Montel committed
279 280
        blogs->setCurrentItem(-1);
        toolbox->setCurrentBlogId(blog_id);
281 282 283
//         actionCollection()->action("publish_post")->setEnabled( false );
        return;
    }
Laurent Montel's avatar
Laurent Montel committed
284
    const int count = blogs->items().count();
Laurent Montel's avatar
Laurent Montel committed
285 286 287 288
    for (int i = 0; i < count; ++i) {
        if (blogs->action(i)->data().toInt() == blog_id) {
            blogs->setCurrentItem(i);
            currentBlogChanged(blogs->action(i));
289 290 291 292 293
            break;
        }
    }
}

Laurent Montel's avatar
Laurent Montel committed
294
void MainWindow::currentBlogChanged(QAction *act)
295
{
Laurent Montel's avatar
Laurent Montel committed
296 297
    if (act) {
        if (mCurrentBlogId == act->data().toInt()) {
298
            return;
Laurent Montel's avatar
Laurent Montel committed
299
        }
300 301
        mCurrentBlogId = act->data().toInt();
//         __currentBlogId = mCurrentBlogId;
Laurent Montel's avatar
Laurent Montel committed
302
        if (activePost) {
303
//             actionCollection()->action("publish_post")->setEnabled( true );
Laurent Montel's avatar
Laurent Montel committed
304
            activePost->setCurrentPostBlogId(mCurrentBlogId);
305 306 307
        } else {
//             actionCollection()->action("publish_post")->setEnabled( false );
        }
Laurent Montel's avatar
Laurent Montel committed
308
        blogs->setToolTip(DBMan::self()->blogList().value(mCurrentBlogId)->blogUrl());
309 310
    } else {
        mCurrentBlogId = -1;
Laurent Montel's avatar
Laurent Montel committed
311 312 313
        if (activePost) {
            activePost->setCurrentPostBlogId(mCurrentBlogId);
        }
314
    }
Laurent Montel's avatar
Laurent Montel committed
315
    toolbox->setCurrentBlogId(mCurrentBlogId);
316 317
}

318
void MainWindow::slotCreateNewPost()
319
{
Laurent Montel's avatar
Laurent Montel committed
320
    qCDebug(BLOGILO_LOG);
321

Laurent Montel's avatar
Laurent Montel committed
322 323 324 325 326
    tabPosts->setCurrentWidget(createPostEntry(mCurrentBlogId, BilboPost()));
    if (mCurrentBlogId == -1) {
        if (!blogs->items().isEmpty()) {
            blogs->setCurrentItem(0);
            currentBlogChanged(blogs->action(0));
327 328
        }
    }
Laurent Montel's avatar
Laurent Montel committed
329
    if (this->isVisible() == false) {
330 331 332 333 334 335 336 337 338 339 340
        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
341
    if (KConfigDialog::showDialog(QStringLiteral("settings")))  {
342 343
        return;
    }
Laurent Montel's avatar
Laurent Montel committed
344
    ConfigureDialog *dialog = new ConfigureDialog(mStorageManager, this, QStringLiteral("settings"), Settings::self());
Laurent Montel's avatar
Laurent Montel committed
345 346
    connect(dialog, &ConfigureDialog::blogAdded, this, &MainWindow::slotBlogAdded);
    connect(dialog, &ConfigureDialog::blogEdited, this, &MainWindow::slotBlogEdited);
Laurent Montel's avatar
Laurent Montel committed
347
    connect(dialog, &ConfigureDialog::blogRemoved, this, &MainWindow::slotBlogRemoved);
Laurent Montel's avatar
Laurent Montel committed
348 349 350 351
    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);
352 353 354 355 356 357
    dialog->show();
}

void MainWindow::slotSettingsChanged()
{
    setupSystemTray();
Laurent Montel's avatar
Laurent Montel committed
358
    mStorageManager->setDefaultUploadFolder(Settings::self()->downloadDirectory());
359 360
}

Laurent Montel's avatar
Laurent Montel committed
361
void MainWindow::slotDialogDestroyed(QObject *win)
362
{
Laurent Montel's avatar
Cleanup  
Laurent Montel committed
363 364
    const QSize size = qobject_cast<QWidget *>(win)->size();
    const QString name = win->objectName();
365
    if (name == QLatin1String("settings")) {
Laurent Montel's avatar
Laurent Montel committed
366
        Settings::setConfigWindowSize(size);
367 368 369 370 371
    }
}

void MainWindow::addBlog()
{
Laurent Montel's avatar
Laurent Montel committed
372 373 374
    AddEditBlog *addEditBlogWindow = new AddEditBlog(-1, this);
    addEditBlogWindow->setWindowModality(Qt::ApplicationModal);
    addEditBlogWindow->setAttribute(Qt::WA_DeleteOnClose);
Laurent Montel's avatar
Laurent Montel committed
375
    connect(addEditBlogWindow, &AddEditBlog::sigBlogAdded, this, &MainWindow::slotBlogAdded);
376 377 378
    addEditBlogWindow->show();
}

Laurent Montel's avatar
Laurent Montel committed
379
void MainWindow::slotBlogAdded(const BilboBlog &blog)
380
{
Laurent Montel's avatar
Laurent Montel committed
381 382 383 384 385 386
    QAction *act = new QAction(blog.title(), blogs);
    act->setCheckable(true);
    act->setData(blog.id());
    blogs->addAction(act);
    blogs->setCurrentAction(act);
    currentBlogChanged(act);
387
    toolbox->slotReloadCategoryList();
Laurent Montel's avatar
Laurent Montel committed
388
    toolbox->slotUpdateEntries(20);
389 390
}

Laurent Montel's avatar
Laurent Montel committed
391
void MainWindow::slotBlogEdited(const BilboBlog &blog)
392
{
Laurent Montel's avatar
Cleanup  
Laurent Montel committed
393
    const int count = blogs->actions().count();
Laurent Montel's avatar
Laurent Montel committed
394 395 396
    for (int i = 0; i < count; ++i) {
        if (blogs->action(i)->data().toInt() == blog.id()) {
            blogs->action(i)->setText(blog.title());
397 398 399 400 401
            break;
        }
    }
}

Laurent Montel's avatar
Laurent Montel committed
402
void MainWindow::slotBlogRemoved(int blog_id)
403
{
Laurent Montel's avatar
Laurent Montel committed
404
    const int count = blogs->actions().count();
Laurent Montel's avatar
Laurent Montel committed
405 406 407 408 409
    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));
410
            }
Laurent Montel's avatar
Laurent Montel committed
411 412
            blogs->removeAction(blogs->action(i));
            if (blogs->currentItem() == -1) {
413
                toolbox->clearFields();
Laurent Montel's avatar
Laurent Montel committed
414
            }
415 416 417 418 419 420 421
            break;
        }
    }
}

void MainWindow::setupSystemTray()
{
Laurent Montel's avatar
Laurent Montel committed
422 423 424
    if (Settings::enableSysTrayIcon()) {
        if (!systemTray) {
            systemTray = new KStatusNotifierItem(this);
Laurent Montel's avatar
Laurent Montel committed
425 426 427
            systemTray->setIconByName(QStringLiteral("blogilo"));
            systemTray->setToolTip(QStringLiteral("blogilo"), i18n("Blogilo"), i18n("A KDE Blogging Client"));
            systemTray->contextMenu()->addAction(actionCollection()->action(QStringLiteral("new_post")));
428 429
            systemTray->setCategory(KStatusNotifierItem::ApplicationStatus);
            systemTray->setStatus(KStatusNotifierItem::Active);
430
        }
Laurent Montel's avatar
Laurent Montel committed
431
    } else if (systemTray) {
432
        systemTray->deleteLater();
Laurent Montel's avatar
Laurent Montel committed
433
        systemTray = Q_NULLPTR;
434 435 436
    }
}

Laurent Montel's avatar
Laurent Montel committed
437
void MainWindow::slotPostTitleChanged(const QString &title)
438
{
Laurent Montel's avatar
Laurent Montel committed
439
//     qCDebug(BLOGILO_LOG);
Laurent Montel's avatar
Laurent Montel committed
440
    tabPosts->setTabText(tabPosts->currentIndex(), title);
441 442
}

Laurent Montel's avatar
Laurent Montel committed
443
void MainWindow::slotToggleToolboxVisible(bool isVisible)
444
{
Laurent Montel's avatar
Laurent Montel committed
445
    toolboxDock->setVisible(isVisible);
446 447 448 449
}

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

Laurent Montel's avatar
Laurent Montel committed
453
void MainWindow::slotActivePostChanged(int index)
454
{
Laurent Montel's avatar
Laurent Montel committed
455
    qCDebug(BLOGILO_LOG) << "new post index: " << index << "\tPrev Index: " << previousActivePostIndex;
456

Laurent Montel's avatar
Laurent Montel committed
457 458
    activePost = qobject_cast<PostEntry *>(tabPosts->widget(index));
    PostEntry *prevActivePost = qobject_cast<PostEntry *>(tabPosts->widget(previousActivePostIndex));
459 460 461
    int activePostBlogId = -1;
    int prevPostBlogId = -1;

Laurent Montel's avatar
Laurent Montel committed
462
    if ((prevActivePost != Q_NULLPTR) && (index != previousActivePostIndex)) {
463
        prevPostBlogId = prevActivePost->currentPostBlogId();
Laurent Montel's avatar
Laurent Montel committed
464 465
        toolbox->getFieldsValue(prevActivePost->currentPost());
        prevActivePost->setCurrentPostBlogId(mCurrentBlogId);
466 467
    }

Laurent Montel's avatar
Laurent Montel committed
468
    if (index >= 0) {
469
        activePostBlogId = activePost->currentPostBlogId();
Laurent Montel's avatar
Laurent Montel committed
470 471
        if (activePostBlogId != -1 && activePostBlogId != prevPostBlogId) {
            setCurrentBlog(activePostBlogId);
472
        }
Laurent Montel's avatar
Laurent Montel committed
473
        toolbox->setFieldsValue(activePost->currentPost());
474
    } else {
Laurent Montel's avatar
Laurent Montel committed
475
        qCCritical(BLOGILO_LOG) << "ActivePost is NULL! tabPosts Current index is: " << tabPosts->currentIndex() ;
476 477 478 479
    }
    previousActivePostIndex = index;
}

480
void MainWindow::slotPublishPost()
481
{
Laurent Montel's avatar
Laurent Montel committed
482
    qCDebug(BLOGILO_LOG);
Laurent Montel's avatar
Laurent Montel committed
483 484
    if (mCurrentBlogId == -1) {
        KMessageBox::sorry(this, i18n("You have to select a blog to publish this post to."));
485 486
        return;
    }
Laurent Montel's avatar
Laurent Montel committed
487 488
    if (!activePost || tabPosts->currentIndex() == -1) {
        KMessageBox::sorry(this, i18n("There is no open post to submit."));
489 490
        return;
    }
Laurent Montel's avatar
Laurent Montel committed
491 492
    toolbox->getFieldsValue(activePost->currentPost());
    activePost->submitPost(mCurrentBlogId, *activePost->currentPost());
493 494
}

495 496
void MainWindow::slotRemoveAllExclude(int pos)
{
Laurent Montel's avatar
Laurent Montel committed
497
    for (int i = tabPosts->count() - 1; i >= 0; --i) {
498 499 500
        if (i == pos) {
            continue;
        }
Laurent Montel's avatar
Laurent Montel committed
501 502 503
        PostEntry *widget = qobject_cast<PostEntry *>(tabPosts->widget(i));
        if (!widget) {
            if (activePost) {
504
                widget = activePost;
Laurent Montel's avatar
Laurent Montel committed
505
            } else {
506
                return;
Laurent Montel's avatar
Laurent Montel committed
507
            }
508
        }
Laurent Montel's avatar
Laurent Montel committed
509
        DBMan::self()->removeTempEntry(*widget->currentPost());
Laurent Montel's avatar
Laurent Montel committed
510
        tabPosts->removeTab(tabPosts->indexOf(widget));
511 512
        widget->close();
    }
Laurent Montel's avatar
Laurent Montel committed
513
    if (tabPosts->count() < 1) {
Laurent Montel's avatar
Laurent Montel committed
514
        activePost = Q_NULLPTR;
515 516 517 518 519
        toolbox->resetFields();
//         actionCollection()->action("publish_post")->setEnabled( false );
    }
}

Laurent Montel's avatar
Laurent Montel committed
520
void MainWindow::slotRemovePostEntry(int pos)
521
{
Laurent Montel's avatar
Laurent Montel committed
522
    qCDebug(BLOGILO_LOG);
523

Laurent Montel's avatar
Laurent Montel committed
524 525 526 527
    PostEntry *widget = qobject_cast<PostEntry *>(tabPosts->widget(pos));

    if (!widget) {
        if (activePost) {
528
            widget = activePost;
Laurent Montel's avatar
Laurent Montel committed
529
        } else {
530
            return;
Laurent Montel's avatar
Laurent Montel committed
531
        }
532
    }
Laurent Montel's avatar
Laurent Montel committed
533
    DBMan::self()->removeTempEntry(*widget->currentPost());
Laurent Montel's avatar
Laurent Montel committed
534
    tabPosts->removeTab(tabPosts->indexOf(widget));
535 536
    widget->close();

Laurent Montel's avatar
Laurent Montel committed
537
    if (tabPosts->count() < 1) {
Laurent Montel's avatar
Laurent Montel committed
538
        activePost = Q_NULLPTR;
539 540 541 542 543
        toolbox->resetFields();
//         actionCollection()->action("publish_post")->setEnabled( false );
    }
}

Laurent Montel's avatar
Laurent Montel committed
544
void MainWindow::slotNewPostOpened(BilboPost &newPost, int blog_id)
545
{
Laurent Montel's avatar
Laurent Montel committed
546
    qCDebug(BLOGILO_LOG);
Laurent Montel's avatar
Laurent Montel committed
547 548
    QWidget *w = createPostEntry(blog_id, newPost);
    tabPosts->setCurrentWidget(w);
549 550
}

551
void MainWindow::slotSavePostLocally()
552
{
Laurent Montel's avatar
Laurent Montel committed
553
    qCDebug(BLOGILO_LOG);
Laurent Montel's avatar
Laurent Montel committed
554
    if (activePost && (tabPosts->count() > 0)) {
555
        toolbox->getFieldsValue(activePost->currentPost());
556 557 558 559 560
        activePost->saveLocally();
        toolbox->reloadLocalPosts();
    }
}

Laurent Montel's avatar
Laurent Montel committed
561
void MainWindow::slotError(const QString &errorMessage)
562
{
Laurent Montel's avatar
Laurent Montel committed
563
    qCDebug(BLOGILO_LOG) << "Error message: " << errorMessage;
Laurent Montel's avatar
Laurent Montel committed
564
    KMessageBox::detailedError(this, i18n("An error occurred in the last transaction."), errorMessage);
565 566 567 568 569 570
    statusBar()->clearMessage();
    slotBusy(false);
}

void MainWindow::writeConfigs()
{
Laurent Montel's avatar
Laurent Montel committed
571
    qCDebug(BLOGILO_LOG);
Laurent Montel's avatar
Laurent Montel committed
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
    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;
604 605 606 607
        }
    }
}

Laurent Montel's avatar
Laurent Montel committed
608
void MainWindow::postManipulationDone(bool isError, const QString &customMessage)
609
{
Laurent Montel's avatar
Laurent Montel committed
610
    qCDebug(BLOGILO_LOG);
Laurent Montel's avatar
Laurent Montel committed
611
    if (isError) {
Yuri Chornoivan's avatar
Yuri Chornoivan committed
612
        KMessageBox::detailedError(this, i18n("Submitting post failed"), customMessage);
613
    } else {
Laurent Montel's avatar
Laurent Montel committed
614 615 616
        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
617
                                           QString(), KStandardGuiItem::yes(), KStandardGuiItem::no(), QStringLiteral("KeepPostOpen")) == KMessageBox::No) {
Laurent Montel's avatar
Laurent Montel committed
618 619 620 621
                slotRemovePostEntry(tabPosts->indexOf(entry));
            } else {
                toolbox->setFieldsValue(entry->currentPost());
            }
622
        }
Laurent Montel's avatar
Laurent Montel committed
623
        toolbox->slotLoadEntriesFromDB(mCurrentBlogId);
624 625 626 627 628 629 630
    }
    this->unsetCursor();
    toolbox->unsetCursor();
}

void MainWindow::slotBusy(bool isBusy)
{
Laurent Montel's avatar
Laurent Montel committed
631
    qCDebug(BLOGILO_LOG) << "isBusy=" << isBusy << "\tbusyNumber=" << busyNumber;
Laurent Montel's avatar
Laurent Montel committed
632
    if (isBusy) {
633
        this->setCursor(Qt::BusyCursor);
Laurent Montel's avatar
Laurent Montel committed
634
        toolbox->setCursor(Qt::BusyCursor);
635
        ++busyNumber;
Laurent Montel's avatar
Laurent Montel committed
636
        if (!progress) {
637
            progress = new QProgressBar(statusBar());
Laurent Montel's avatar
Laurent Montel committed
638 639
            progress->setMinimum(0);
            progress->setMaximum(0);
640 641 642 643 644
            progress->setFixedSize(250, 17);
            statusBar()->addPermanentWidget(progress);
        }
    } else {
        --busyNumber;
Laurent Montel's avatar
Laurent Montel committed
645
        if (busyNumber < 1) {
646 647
            this->unsetCursor();
            toolbox->unsetCursor();
Laurent Montel's avatar
Laurent Montel committed
648
            if (progress) {
649 650
                statusBar()->removeWidget(progress);
                progress->deleteLater();
Laurent Montel's avatar
Laurent Montel committed
651
                progress = Q_NULLPTR;
652 653 654 655 656 657
            }
//             busyNumber = 0;
        }
    }
}

Laurent Montel's avatar
Laurent Montel committed
658
QWidget *MainWindow::createPostEntry(int blog_id, const BilboPost &post)
659
{
Laurent Montel's avatar
Laurent Montel committed
660
    qCDebug(BLOGILO_LOG);
Laurent Montel's avatar
Laurent Montel committed
661 662
    PostEntry *temp = new PostEntry(this);
    temp->setAttribute(Qt::WA_DeleteOnClose);
663
    temp->setCurrentPost(post);
Laurent Montel's avatar
Laurent Montel committed
664
    temp->setCurrentPostBlogId(blog_id);
665

Laurent Montel's avatar
Laurent Montel committed
666 667
    connect(temp, &PostEntry::postTitleChanged, this, &MainWindow::slotPostTitleChanged);
    connect(temp, &PostEntry::postPublishingDone, this, &MainWindow::postManipulationDone);
Laurent Montel's avatar
Laurent Montel committed
668
    connect(this, &MainWindow::settingsChanged, temp, &PostEntry::settingsChanged);
Laurent Montel's avatar
Laurent Montel committed
669
    connect(temp, &PostEntry::showStatusMessage, this, &MainWindow::slotShowStatusMessage);
670

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

Laurent Montel's avatar
Laurent Montel committed
673
    tabPosts->addTab(temp, post.title());
674 675 676 677 678 679 680 681 682 683 684
    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
685
    connect(uploadDlg, &UploadMediaDialog::sigBusy, this, &MainWindow::slotBusy);
Laurent Montel's avatar
Laurent Montel committed
686
    if (mCurrentBlogId == -1) {
Laurent Montel's avatar
Laurent Montel committed
687
        uploadDlg->init(Q_NULLPTR);
Laurent Montel's avatar
Laurent Montel committed
688 689 690
    } else {
        uploadDlg->init(DBMan::self()->blog(mCurrentBlogId));
    }
691 692
}

693 694
void MainWindow::slotOpenCurrentBlogInBrowser()
{
Mehrdad Momeny's avatar
Mehrdad Momeny committed
695
    if (mCurrentBlogId > -1) {
Laurent Montel's avatar
KUrl--  
Laurent Montel committed
696
        QUrl url(DBMan::self()->blog(mCurrentBlogId)->blogUrl());
Laurent Montel's avatar
Laurent Montel committed
697
        if (url.isValid()) {
Laurent Montel's avatar
Laurent Montel committed
698
            QDesktopServices::openUrl(url);
Laurent Montel's avatar
Laurent Montel committed
699
        } else {
Mehrdad Momeny's avatar
Mehrdad Momeny committed
700
            KMessageBox::sorry(this, i18n("Cannot find current blog URL."));
Laurent Montel's avatar
Laurent Montel committed
701
        }
702 703
    }
    ///TODO
Laurent Montel's avatar
Cleanup  
Laurent Montel committed
704
    ///else show a message to the user saying that a blog should be selected before.
705 706
}