summaryrefslogtreecommitdiff
path: root/kwebapp/webview.cpp
blob: 7a5ba4c1832ce4ec79aed8acf987779a286f41f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/***************************************************************************
 *   Copyright (C) 2011 by Andrea Diamantini <adjam7@gmail.com>                            *
 *                                                                         *
 *   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) any later version.                                   *
 *                                                                         *
 *   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, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
 ***************************************************************************/


// Self Includes
#include "webview.h"
#include "webview.moc"

// KDE Includes
#include <KIO/Job>
#include <KIO/RenameDialog>
#include <KIO/JobUiDelegate>

#include <KGlobalSettings>
#include <KStandardDirs>
#include <KFileDialog>
#include <KJobUiDelegate>
#include <KLocalizedString>
#include <KMenu>
#include <KAction>
#include <KUrl>
#include <KRun>

// Qt Includes
#include <QUrl>
#include <QDebug>
#include <QWebHitTestResult>
#include <QWebHistory>
#include <QNetworkRequest>
#include <QPointer>


WebView::WebView(QWidget *parent)
    : KWebView(parent)
{
    page()->setForwardUnsupportedContent(true);
    connect(page(), SIGNAL(unsupportedContent(QNetworkReply *)), page(), SLOT(downloadResponse(QNetworkReply *)));
    connect(page(), SIGNAL(downloadRequested(const QNetworkRequest &)), page(), SLOT(downloadRequest(const QNetworkRequest &)));
    connect(this, SIGNAL(linkShiftClicked(const KUrl &)), page(), SLOT(downloadUrl(const KUrl &)));
    
    QWebSettings::setIconDatabasePath( KStandardDirs::locateLocal("cache","kwebapp.favicons") );
    
    setContextMenuPolicy(Qt::CustomContextMenu);

    connect(this, SIGNAL(titleChanged(const QString &)), this, SLOT(setTitle(const QString &)));
    connect(this, SIGNAL(iconChanged()), this, SLOT(setIcon()));
    connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(menuRequested(const QPoint &)));
}


void WebView::setTitle(const QString &t)
{
    setWindowTitle(t);
}


void WebView::setIcon()
{
    setWindowIcon(icon());    
}


void WebView::menuRequested(const QPoint &pos)
{
    QWebHitTestResult result = page()->mainFrame()->hitTestContent(pos);

    KMenu menu(this);
    QAction *a;

    // is a link?
    if (!result.linkUrl().isEmpty())
    {
        a = new KAction(KIcon("window-new"), i18n("Open in default browser"), this);
        a->setData(result.linkUrl());
        connect(a, SIGNAL(triggered(bool)), this, SLOT(openLinkInDefaultBrowser()));
        menu.addAction(a);

        menu.addAction(pageAction(KWebPage::DownloadLinkToDisk));
        menu.addAction(pageAction(KWebPage::CopyLinkToClipboard));
        menu.addSeparator();
    }
    
    if(history()->canGoBack())
    {
        menu.addAction(pageAction(KWebPage::Back));
    }

    if(history()->canGoBack())
    {
        menu.addAction(pageAction(KWebPage::Forward));
    }

    menu.addAction(pageAction(KWebPage::Reload));

    menu.exec(mapToGlobal(pos));
}


void WebView::openLinkInDefaultBrowser()
{
    KAction *a = qobject_cast<KAction*>(sender());
    KUrl u(a->data().toUrl());

    (void)new KRun(u, this, 0);
}