From 753c9556fd3c02da287107305ea063a2101931b2 Mon Sep 17 00:00:00 2001 From: Naikel Aparicio Date: Thu, 22 Oct 2015 00:06:36 -0430 Subject: [PATCH] Cookies support on WebUI when downloading torrent from a URL. Modified download and upload windows to allow autocompletion of browsers. Fixed the spinner in the WebUI upload page. Modified height of the WebUI download page. Fixed all the JavaScript functions for download and upload pages. --- src/webui/extra_translations.h | 5 +- src/webui/webapplication.cpp | 48 +++++++++++- src/webui/webapplication.h | 1 + src/webui/www/public/download.html | 40 +++++++++- src/webui/www/public/scripts/download.js | 34 +++++---- src/webui/www/public/scripts/mocha-init.js | 8 +- src/webui/www/public/upload.html | 89 ++++++++-------------- 7 files changed, 142 insertions(+), 83 deletions(-) diff --git a/src/webui/extra_translations.h b/src/webui/extra_translations.h index 23b50a3da..1cab1be69 100644 --- a/src/webui/extra_translations.h +++ b/src/webui/extra_translations.h @@ -39,7 +39,7 @@ static const char *__TRANSLATIONS__[] = { QT_TRANSLATE_NOOP("HttpServer", "Logout"), QT_TRANSLATE_NOOP("HttpServer", "Download Torrents from their URL or Magnet link"), QT_TRANSLATE_NOOP("HttpServer", "Only one link per line"), - QT_TRANSLATE_NOOP("HttpServer", "Download local torrent"), + QT_TRANSLATE_NOOP("HttpServer", "Upload local torrent"), QT_TRANSLATE_NOOP("HttpServer", "Download"), QT_TRANSLATE_NOOP("HttpServer", "Are you sure you want to delete the selected torrents from the transfer list?"), QT_TRANSLATE_NOOP("HttpServer", "Global upload rate limit must be greater than 0 or disabled."), @@ -84,6 +84,9 @@ static const char *__TRANSLATIONS__[] = { QT_TRANSLATE_NOOP("HttpServer", "Paused"), QT_TRANSLATE_NOOP("HttpServer", "Active"), QT_TRANSLATE_NOOP("HttpServer", "Inactive") + QT_TRANSLATE_NOOP("HttpServer", "Save files to location:") + QT_TRANSLATE_NOOP("HttpServer", "Label:") + QT_TRANSLATE_NOOP("HttpServer", "Cookie:") }; static const struct { const char *source; const char *comment; } __COMMENTED_TRANSLATIONS__[] = { diff --git a/src/webui/webapplication.cpp b/src/webui/webapplication.cpp index 5519005ee..5881724db 100644 --- a/src/webui/webapplication.cpp +++ b/src/webui/webapplication.cpp @@ -44,6 +44,7 @@ #include "core/bittorrent/trackerentry.h" #include "core/bittorrent/torrentinfo.h" #include "core/bittorrent/torrenthandle.h" +#include "core/net/downloadmanager.h" #include "websessiondata.h" #include "webapplication.h" @@ -112,6 +113,7 @@ QMap > WebApplication::initialize ADD_ACTION(command, bottomPrio); ADD_ACTION(command, recheck); ADD_ACTION(command, setLabel); + ADD_ACTION(command, getSavePath); ADD_ACTION(version, api); ADD_ACTION(version, api_min); ADD_ACTION(version, qbittorrent); @@ -306,9 +308,33 @@ void WebApplication::action_command_shutdown() void WebApplication::action_command_download() { CHECK_URI(0); - CHECK_PARAMETERS("urls"); QString urls = request().posts["urls"]; QStringList list = urls.split('\n'); + QString savepath = request().posts["savepath"]; + QString label = request().posts["label"]; + QString cookie = request().posts["cookie"]; + QList cookies; + if (!cookie.isEmpty()) { + + QStringList cookiesStr = cookie.split("; "); + foreach (QString cookieStr, cookiesStr) { + cookieStr = cookieStr.trimmed(); + int index = cookieStr.indexOf('='); + if (index > 1) { + QByteArray name = cookieStr.left(index).toLatin1(); + QByteArray value = cookieStr.right(cookieStr.length() - index - 1).toLatin1(); + QNetworkCookie c(name, value); + cookies << c; + } + } + } + + savepath = savepath.trimmed(); + label = label.trimmed(); + + BitTorrent::AddTorrentParams params; + params.savePath = savepath; + params.label = label; foreach (QString url, list) { url = url.trimmed(); @@ -320,7 +346,9 @@ void WebApplication::action_command_download() if ((url.size() == 40 && !url.contains(QRegExp("[^0-9A-Fa-f]"))) || (url.size() == 32 && !url.contains(QRegExp("[^2-7A-Za-z]")))) url = "magnet:?xt=urn:btih:" + url; - BitTorrent::Session::instance()->addTorrent(url); + + Net::DownloadManager::instance()->setCookiesFromUrl(cookies, QUrl::fromEncoded(url.toUtf8())); + BitTorrent::Session::instance()->addTorrent(url, params); } } } @@ -329,6 +357,11 @@ void WebApplication::action_command_upload() { qDebug() << Q_FUNC_INFO; CHECK_URI(0); + QString savepath = request().posts["savepath"]; + QString label = request().posts["label"]; + + savepath = savepath.trimmed(); + label = label.trimmed(); foreach(const Http::UploadedFile& torrent, request().files) { QString filePath = saveTmpFile(torrent.data); @@ -340,7 +373,10 @@ void WebApplication::action_command_upload() print(QObject::tr("Error: '%1' is not a valid torrent file.\n").arg(torrent.filename), Http::CONTENT_TYPE_TXT); } else { - if (!BitTorrent::Session::instance()->addTorrent(torrentInfo)) { + BitTorrent::AddTorrentParams params; + params.savePath = savepath; + params.label = label; + if (!BitTorrent::Session::instance()->addTorrent(torrentInfo, params)) { status(500, "Internal Server Error"); print(QObject::tr("Error: Could not add torrent to session."), Http::CONTENT_TYPE_TXT); } @@ -685,6 +721,12 @@ void WebApplication::action_command_setLabel() } } +void WebApplication::action_command_getSavePath() +{ + CHECK_URI(0); + print(Preferences::instance()->getSavePath()); +} + bool WebApplication::isPublicScope() { return (scope_ == DEFAULT_SCOPE || scope_ == VERSION_INFO); diff --git a/src/webui/webapplication.h b/src/webui/webapplication.h index 5424ce2c5..9696c8e09 100644 --- a/src/webui/webapplication.h +++ b/src/webui/webapplication.h @@ -87,6 +87,7 @@ private: void action_command_bottomPrio(); void action_command_recheck(); void action_command_setLabel(); + void action_command_getSavePath(); void action_version_api(); void action_version_api_min(); void action_version_qbittorrent(); diff --git a/src/webui/www/public/download.html b/src/webui/www/public/download.html index bfa24e255..0a2cce4ae 100644 --- a/src/webui/www/public/download.html +++ b/src/webui/www/public/download.html @@ -4,16 +4,50 @@ QBT_TR(Add Torrent Link)QBT_TR + + +

QBT_TR(Download Torrents from their URLs or Magnet links)QBT_TR

- +

QBT_TR(Only one link per line)QBT_TR

- -
+
+
+ + +
+
+ + +
+
+ + +
+
+ +
+ + + +
diff --git a/src/webui/www/public/scripts/download.js b/src/webui/www/public/scripts/download.js index 8088dc068..a01c8eedf 100644 --- a/src/webui/www/public/scripts/download.js +++ b/src/webui/www/public/scripts/download.js @@ -20,19 +20,23 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -window.addEvent('domready', function() { - $('urls').focus(); - $('downButton').addEvent('click', function(e) { - new Event(e).stop(); - new Request({ - url: 'command/download', - method: 'post', - data: { - urls: $('urls').value - }, - onComplete: function() { - window.parent.document.getElementById('downloadPage').parentNode.removeChild(window.parent.document.getElementById('downloadPage')); - } + +getSavePath = function() { + var req = new Request({ + url: 'command/getSavePath', + method: 'get', + noCache: true, + onFailure: function() { + alert("Could not contact qBittorrent"); + }, + onSuccess: function(data) { + if (data) { + $('savepath').setProperty('value', data); + } + } }).send(); - }); -}); \ No newline at end of file +} + +$(window).addEventListener("load", function() { + getSavePath(); +}); diff --git a/src/webui/www/public/scripts/mocha-init.js b/src/webui/www/public/scripts/mocha-init.js index 23a8f4bf2..b3732f2de 100644 --- a/src/webui/www/public/scripts/mocha-init.js +++ b/src/webui/www/public/scripts/mocha-init.js @@ -57,7 +57,7 @@ initializeWindows = function() { paddingVertical: 0, paddingHorizontal: 0, width: 500, - height: 300 + height: 360 }); updateMainData(); }); @@ -88,7 +88,7 @@ initializeWindows = function() { new Event(e).stop(); new MochaUI.Window({ id: 'uploadPage', - title: "QBT_TR(Download local torrent)QBT_TR", + title: "QBT_TR(Upload local torrent)QBT_TR", loadMethod: 'iframe', contentURL: 'upload.html', scrollbars: true, @@ -96,8 +96,8 @@ initializeWindows = function() { maximizable: false, paddingVertical: 0, paddingHorizontal: 0, - width: 600, - height: 130 + width: 500, + height: 200 }); updateMainData(); }); diff --git a/src/webui/www/public/upload.html b/src/webui/www/public/upload.html index 2f2a3b6a4..3e49301f3 100644 --- a/src/webui/www/public/upload.html +++ b/src/webui/www/public/upload.html @@ -2,73 +2,48 @@ - QBT_TR(Download local torrent)QBT_TR + QBT_TR(Upload local torrent)QBT_TR - + -
-
- + +

+

+ +
+

+
+
+ +
-
+
+ + +
+
+
+