1 /*
2     SPDX-FileCopyrightText: 2009 Joris Guisson <joris.guisson@gmail.com>
3     SPDX-License-Identifier: GPL-2.0-or-later
4 */
5 
6 #include "webview.h"
7 
8 #include <QAction>
9 #include <QApplication>
10 #include <QFileDialog>
11 #include <QStandardPaths>
12 #include <QTextStream>
13 #include <QUrl>
14 #include <QUrlQuery>
15 #include <QWebEngineProfile>
16 #include <QWebEngineView>
17 
18 #include <KIO/AccessManager>
19 #include <KIO/CopyJob>
20 #include <KIO/Job>
21 #include <KIconLoader>
22 #include <KLocalizedString>
23 #include <KMainWindow>
24 
25 #include "magneturlschemehandler.h"
26 #include <util/log.h>
27 
28 using namespace bt;
29 
30 namespace kt
31 {
WebView(kt::WebViewClient * client,ProxyHelper * proxy,QWidget * parentWidget)32 WebView::WebView(kt::WebViewClient *client, ProxyHelper *proxy, QWidget *parentWidget)
33     : QWebEngineView(parentWidget)
34     , client(client)
35     , m_proxy(proxy)
36 {
37     MagnetUrlSchemeHandler *magneturlschemehandler = new MagnetUrlSchemeHandler(this);
38     page()->profile()->installUrlSchemeHandler("magnet", magneturlschemehandler);
39 
40     connect(magneturlschemehandler, &MagnetUrlSchemeHandler::magnetUrlDetected, this, &WebView::magnetUrlDetected);
41     connect(page()->profile(), &QWebEngineProfile::downloadRequested, this, &WebView::downloadRequested);
42 }
43 
~WebView()44 WebView::~WebView()
45 {
46 }
47 
handleMagnetUrl(const QUrl & magnet_url)48 void WebView::handleMagnetUrl(const QUrl &magnet_url)
49 {
50     if (client)
51         client->magnetUrl(magnet_url);
52 }
53 
openUrl(const QUrl & url)54 void WebView::openUrl(const QUrl &url)
55 {
56     if (url.host() == QStringLiteral("ktorrent.searchplugin"))
57         home();
58     else
59         load(url);
60 }
61 
home()62 void WebView::home()
63 {
64     if (home_page_html.isEmpty())
65         loadHomePage();
66     if (!home_page_html.isEmpty()) {
67         const QString file = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("ktorrent/search/home"));
68         setHtml(home_page_html, QUrl(file));
69     }
70 }
71 
homePageData()72 QString WebView::homePageData()
73 {
74     if (home_page_html.isEmpty())
75         loadHomePage();
76 
77     return home_page_html;
78 }
79 
loadHomePage()80 void WebView::loadHomePage()
81 {
82     QString file = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("ktorrent/search/home/home.html"));
83     QFile fptr(file);
84 
85     if (fptr.open(QIODevice::ReadOnly)) {
86         Out(SYS_SRC | LOG_DEBUG) << "Loading home page from " << file << endl;
87         home_page_base_url = file.left(file.lastIndexOf(QLatin1Char('/')) + 1);
88         home_page_html = QTextStream(&fptr).readAll();
89 
90         // %1
91         home_page_html = home_page_html.arg(QStringLiteral("ktorrent_infopage.css"));
92         // %2
93 
94         if (qApp->layoutDirection() == Qt::RightToLeft) {
95             QString link = QStringLiteral("<link rel=\"stylesheet\" type=\"text/css\" href=\"%1\" />");
96             link = link.arg(QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kdeui/about/kde_infopage_rtl.css")));
97             home_page_html = home_page_html.arg(link);
98         } else
99             home_page_html = home_page_html.arg(QString());
100 
101         KIconLoader *iconloader = KIconLoader::global();
102 
103         int icon_size = iconloader->currentSize(KIconLoader::Desktop);
104 
105         home_page_html = home_page_html
106                              .arg(i18n("Home")) // %3 Title
107                              .arg(i18n("KTorrent")) // %4
108                              .arg(i18nc("KDE 4 tag line, see http://kde.org/img/kde40.png", "Be free.")) // %5
109                              .arg(i18n("Search the web for torrents.")) // %6
110                              .arg(i18n("Search")) // %7
111                              .arg(QStringLiteral("search_text")) // %8
112                              .arg(icon_size)
113                              .arg(icon_size); // %9 and %10
114     } else {
115         Out(SYS_SRC | LOG_IMPORTANT) << "Failed to load " << file << " : " << fptr.errorString() << endl;
116     }
117 }
118 
searchUrl(const QString & search_text)119 QUrl WebView::searchUrl(const QString &search_text)
120 {
121     if (client)
122         return client->searchUrl(search_text);
123     else
124         // client is broken -> browse to home
125         return QUrl(QStringLiteral("http://ktorrent.searchplugin/"));
126 }
127 
createWindow(QWebEnginePage::WebWindowType type)128 QWebEngineView *WebView::createWindow(QWebEnginePage::WebWindowType type)
129 {
130     Q_UNUSED(type)
131     return client->newTab();
132 }
133 
downloadRequested(QWebEngineDownloadItem * download)134 void WebView::downloadRequested(QWebEngineDownloadItem *download)
135 {
136     if (download->mimeType() == QStringLiteral("application/x-bittorrent") || download->url().path().endsWith(QLatin1String(".torrent"))) {
137         Q_EMIT torrentFileDownloadRequested(download);
138     } else {
139         downloadFile(download);
140     }
141 }
142 
downloadFile(QWebEngineDownloadItem * download)143 void WebView::downloadFile(QWebEngineDownloadItem *download)
144 {
145     QString filename = QFileInfo(download->url().path()).fileName();
146     QString path = QFileDialog::getExistingDirectory(this, i18n("Save %1 to"), QStandardPaths::writableLocation(QStandardPaths::DownloadLocation));
147 
148     if (!path.isEmpty()) {
149         download->setDownloadDirectory(path);
150         download->accept();
151     }
152 }
153 
magnetUrlDetected(const QUrl & url)154 void WebView::magnetUrlDetected(const QUrl &url)
155 {
156     client->magnetUrl(url);
157 }
158 
159 }
160