bilbobrowser.cpp 5.17 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>
Mehrdad Momeny's avatar
Mehrdad Momeny committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

    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/
*/
23 24 25

#include "bilbobrowser.h"

26
#include <QWebEngineView>
27
#include <QProgressBar>
Laurent Montel's avatar
Laurent Montel committed
28
#include <qstatusbar.h>
29 30
#include <kmessagebox.h>
#include <kseparator.h>
Laurent Montel's avatar
Laurent Montel committed
31
#include <QPushButton>
Laurent Montel's avatar
Laurent Montel committed
32
#include "blogilo_debug.h"
33

34 35 36 37 38 39 40
#include "stylegetter.h"
#include "global.h"
#include "settings.h"
#include <klocalizedstring.h>
#include <dbman.h>
#include <bilboblog.h>
#include <qcheckbox.h>
Mehrdad Momeny's avatar
Mehrdad Momeny committed
41
#include <QVBoxLayout>
Laurent Montel's avatar
Laurent Montel committed
42 43
#include <QTimer>

Laurent Montel's avatar
Laurent Montel committed
44 45
BilboBrowser::BilboBrowser(QWidget *parent)
    : QWidget(parent)
46
{
47
    mWebView = new QWebEngineView(this);
48

Laurent Montel's avatar
Laurent Montel committed
49
    createUi(parent);
Laurent Montel's avatar
Laurent Montel committed
50 51 52
    connect(mWebView, &QWebEngineView::loadProgress,
            browserProgress, &QProgressBar::setValue);
    connect(mWebView, &QWebEngineView::loadFinished, this, &BilboBrowser::slotCompleted);
53 54 55 56 57 58
}

BilboBrowser::~BilboBrowser()
{
}

Laurent Montel's avatar
Laurent Montel committed
59
void BilboBrowser::createUi(QWidget *parent)
60
{
Laurent Montel's avatar
Laurent Montel committed
61 62
    btnGetStyle = new QPushButton(this);
    btnGetStyle->setText(i18n("Get blog style"));
Laurent Montel's avatar
Laurent Montel committed
63
    connect(btnGetStyle, &QPushButton::clicked, this, &BilboBrowser::slotGetBlogStyle);
64

Laurent Montel's avatar
Laurent Montel committed
65 66
    viewInBlogStyle = new QCheckBox(i18n("View post in the blog style"), this);
    viewInBlogStyle->setChecked(Settings::previewInBlogStyle());
67
    connect(viewInBlogStyle, &QAbstractButton::toggled, this, &BilboBrowser::slotViewModeChanged);
68

Laurent Montel's avatar
Laurent Montel committed
69 70 71
    QSpacerItem *horizontalSpacer = new QSpacerItem(40, 20,
            QSizePolicy::Expanding, QSizePolicy::Minimum);
    KSeparator *separator = new KSeparator(this);
72

Laurent Montel's avatar
Laurent Montel committed
73
    QVBoxLayout *vlayout = new QVBoxLayout(parent);
74 75
    QHBoxLayout *hlayout = new QHBoxLayout();

Laurent Montel's avatar
Laurent Montel committed
76 77 78
    hlayout->addWidget(viewInBlogStyle);
    hlayout->addItem(horizontalSpacer);
    hlayout->addWidget(btnGetStyle);
79

Laurent Montel's avatar
Laurent Montel committed
80 81 82
    vlayout->addLayout(hlayout);
    vlayout->addWidget(separator);
    vlayout->addWidget(mWebView);
83

Laurent Montel's avatar
Laurent Montel committed
84
    browserProgress = new QProgressBar(this);
85 86
    browserProgress->setFixedSize(120, 17);

Laurent Montel's avatar
Laurent Montel committed
87
    browserStatus = new QStatusBar(this);
Laurent Montel's avatar
Laurent Montel committed
88 89 90
    browserStatus->setFixedHeight(20);
    browserStatus->addPermanentWidget(browserProgress);
    vlayout->addWidget(browserStatus);
91 92
}

Laurent Montel's avatar
Laurent Montel committed
93
void BilboBrowser::setHtml(const QString &title, const QString &content)
94 95 96 97
{
    currentTitle = title;
    currentContent = content;

Laurent Montel's avatar
Laurent Montel committed
98
    if (browserProgress->isHidden()) {
99 100 101
        browserProgress->show();
    }
    browserProgress->reset();
Laurent Montel's avatar
Laurent Montel committed
102
    browserStatus->showMessage(i18n("loading page items..."));
Laurent Montel's avatar
Laurent Montel committed
103
    if (__currentBlogId > -1 && viewInBlogStyle->isChecked()) {
Laurent Montel's avatar
Laurent Montel committed
104 105
        mWebView->setHtml(StyleGetter::styledHtml(__currentBlogId, title, content),
                          DBMan::self()->blog(__currentBlogId)->url());
106
    } else {
Laurent Montel's avatar
Laurent Montel committed
107
        mWebView->setHtml(QLatin1String("<html><body><h2 align='center'>") + title + QLatin1String("</h2>") + content + QLatin1String("</html>"));
108 109 110 111 112
    }
}

void BilboBrowser::stop()
{
113
    mWebView->stop();
114 115
}

116
void BilboBrowser::slotGetBlogStyle()
117
{
118
    stop();
Laurent Montel's avatar
Laurent Montel committed
119 120 121 122
    if (__currentBlogId < 0) {
        KMessageBox::information(this,
                                 i18n("Please select a blog, then try again."),
                                 i18n("Select a blog"));
123 124 125
        return;
    }

Laurent Montel's avatar
Laurent Montel committed
126 127
    browserStatus->showMessage(i18n("Fetching blog style from the web..."));
    if (browserProgress->isHidden()) {
128 129 130 131
        browserProgress->show();
    }
    browserProgress->reset();

Laurent Montel's avatar
Laurent Montel committed
132
    StyleGetter *styleGetter = new StyleGetter(__currentBlogId, this);
133 134
    connect(styleGetter, &StyleGetter::sigGetStyleProgress, browserProgress,
            &QProgressBar::setValue);
Laurent Montel's avatar
Laurent Montel committed
135
    connect(styleGetter, &StyleGetter::sigStyleFetched, this, &BilboBrowser::slotSetBlogStyle);
136 137
}

138
void BilboBrowser::slotSetBlogStyle()
139
{
Laurent Montel's avatar
Laurent Montel committed
140
    browserStatus->showMessage(i18n("Blog style fetched."), 2000);
141 142
    Q_EMIT sigSetBlogStyle();

Laurent Montel's avatar
Laurent Montel committed
143
    if (qobject_cast< StyleGetter * >(sender())) {
144 145 146 147
        sender()->deleteLater();
    }
}

148
void BilboBrowser::slotCompleted(bool ok)
149
{
150
    QTimer::singleShot(1500, browserProgress, &QWidget::hide);
Laurent Montel's avatar
Laurent Montel committed
151 152
    if (!ok) {
        browserStatus->showMessage(i18n("An error occurred in the latest transaction."), 5000);
153 154 155
    }
}

Laurent Montel's avatar
Laurent Montel committed
156
void BilboBrowser::slotSetStatusBarText(const QString &text)
157 158
{
    QString statusText = text;
159
    statusText.remove(QStringLiteral("<qt>"));
Laurent Montel's avatar
Laurent Montel committed
160
    browserStatus->showMessage(statusText);
161 162
}

163
void BilboBrowser::slotViewModeChanged()
164
{
165
    stop();
Laurent Montel's avatar
Laurent Montel committed
166
    setHtml(currentTitle, currentContent);
167 168
}