mainwindow.cpp 24.9 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 67
#include <KSelectAction>
#include <kimagefilepreview.h>
#include <KToolInvocation>
Laurent Montel's avatar
Port++  
Laurent Montel committed
68
#include <QMenu>
Laurent Montel's avatar
KUrl--  
Laurent Montel committed
69
#include <QUrl>
Laurent Montel's avatar
Laurent Montel committed
70

Laurent Montel's avatar
Laurent Montel committed
71 72
#include <QDockWidget>
#include <QProgressBar>
Mehrdad Momeny's avatar
Mehrdad Momeny committed
73
#include <QTimer>
74
#include <QKeyEvent>
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

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

MainWindow::~MainWindow()
{
143 144
}

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

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

    mStorageManager = new PimCommon::StorageServiceManager(this);
Laurent Montel's avatar
Laurent Montel committed
159 160 161 162
    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);
163 164 165 166 167 168 169 170 171 172 173 174 175
}

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"));
176 177
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

void MainWindow::writeConfigs()
{
Laurent Montel's avatar
Laurent Montel committed
573
    qCDebug(BLOGILO_LOG);
Laurent Montel's avatar
Laurent Montel committed
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 604 605
    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;
606 607 608 609
        }
    }
}

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

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

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

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

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

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

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