Redesign main core classes.

This commit is contained in:
Vladimir Golovnev (Glassez) 2015-04-19 18:17:47 +03:00
parent 60c0939e05
commit d16d1fdb3a
152 changed files with 11366 additions and 8967 deletions

View file

@ -32,22 +32,19 @@
#include <QCryptographicHash>
#include <queue>
#include <vector>
#include <libtorrent/session.hpp>
#ifndef DISABLE_GUI
// TODO: Drop GUI dependency!
#include "iconprovider.h"
#endif
#include "core/iconprovider.h"
#include "core/misc.h"
#include "core/fs_utils.h"
#include "core/preferences.h"
#include "btjson.h"
#include "prefjson.h"
#include "qbtsession.h"
#include "core/bittorrent/session.h"
#include "core/bittorrent/trackerentry.h"
#include "core/bittorrent/torrentinfo.h"
#include "core/bittorrent/torrenthandle.h"
#include "websessiondata.h"
#include "webapplication.h"
using namespace libtorrent;
static const int API_VERSION = 2;
static const int API_VERSION_MIN = 2;
@ -195,11 +192,7 @@ void WebApplication::action_public_theme()
return;
}
#ifdef DISABLE_GUI
QString url = ":/icons/oxygen/" + args_.front() + ".png";
#else
QString url = IconProvider::instance()->getIconPath(args_.front());
#endif
qDebug() << Q_FUNC_INFO << "There icon:" << url;
printFile(url);
@ -312,13 +305,8 @@ void WebApplication::action_command_download()
qDebug("Converting bc link to magnet link");
url = misc::bcLinkToMagnet(url);
}
else if (url.startsWith("magnet:", Qt::CaseInsensitive)) {
QBtSession::instance()->addMagnetSkipAddDlg(url);
}
else {
qDebug("Downloading url: %s", qPrintable(url));
QBtSession::instance()->downloadUrlAndSkipDialog(url);
}
BitTorrent::Session::instance()->addTorrent(url);
}
}
}
@ -332,11 +320,17 @@ void WebApplication::action_command_upload()
QString filePath = saveTmpFile(torrent.data);
if (!filePath.isEmpty()) {
QTorrentHandle h = QBtSession::instance()->addTorrent(filePath);
if (!h.is_valid()) {
status(415, "Internal Server Error");
BitTorrent::TorrentInfo torrentInfo = BitTorrent::TorrentInfo::loadFromFile(filePath);
if (!torrentInfo.isValid()) {
status(415, "Unsupported Media Type");
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)) {
status(500, "Internal Server Error");
print(QObject::tr("Error: Could not add torrent to session."), Http::CONTENT_TYPE_TXT);
}
}
// Clean up
fsutils::forceRemove(filePath);
}
@ -354,37 +348,49 @@ void WebApplication::action_command_addTrackers()
CHECK_PARAMETERS("hash" << "urls");
QString hash = request().posts["hash"];
if (!hash.isEmpty()) {
QString urls = request().posts["urls"];
QStringList list = urls.split('\n');
QBtSession::instance()->addTrackersAndUrlSeeds(hash, list, QStringList());
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
if (torrent) {
QList<BitTorrent::TrackerEntry> trackers;
foreach (const QString &url, request().posts["urls"].split('\n'))
trackers << url;
torrent->addTrackers(trackers);
}
}
void WebApplication::action_command_resumeAll()
{
CHECK_URI(0);
QBtSession::instance()->resumeAllTorrents();
foreach (BitTorrent::TorrentHandle *const torrent, BitTorrent::Session::instance()->torrents())
torrent->resume();
}
void WebApplication::action_command_pauseAll()
{
CHECK_URI(0);
QBtSession::instance()->pauseAllTorrents();
foreach (BitTorrent::TorrentHandle *const torrent, BitTorrent::Session::instance()->torrents())
torrent->pause();
}
void WebApplication::action_command_resume()
{
CHECK_URI(0);
CHECK_PARAMETERS("hash");
QBtSession::instance()->resumeTorrent(request().posts["hash"]);
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(request().posts["hash"]);
if (torrent)
torrent->resume();
}
void WebApplication::action_command_pause()
{
CHECK_URI(0);
CHECK_PARAMETERS("hash");
QBtSession::instance()->pauseTorrent(request().posts["hash"]);
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(request().posts["hash"]);
if (torrent)
torrent->pause();
}
void WebApplication::action_command_setPreferences()
@ -401,22 +407,22 @@ void WebApplication::action_command_setFilePrio()
QString hash = request().posts["hash"];
int file_id = request().posts["id"].toInt();
int priority = request().posts["priority"].toInt();
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
if (h.is_valid() && h.has_metadata())
h.file_priority(file_id, priority);
if (torrent && torrent->hasMetadata())
torrent->setFilePriority(file_id, priority);
}
void WebApplication::action_command_getGlobalUpLimit()
{
CHECK_URI(0);
print(QByteArray::number(QBtSession::instance()->getSession()->settings().upload_rate_limit));
print(QByteArray::number(BitTorrent::Session::instance()->uploadRateLimit()));
}
void WebApplication::action_command_getGlobalDlLimit()
{
CHECK_URI(0);
print(QByteArray::number(QBtSession::instance()->getSession()->settings().download_rate_limit));
print(QByteArray::number(BitTorrent::Session::instance()->downloadRateLimit()));
}
void WebApplication::action_command_setGlobalUpLimit()
@ -426,7 +432,7 @@ void WebApplication::action_command_setGlobalUpLimit()
qlonglong limit = request().posts["limit"].toLongLong();
if (limit == 0) limit = -1;
QBtSession::instance()->setUploadRateLimit(limit);
BitTorrent::Session::instance()->setUploadRateLimit(limit);
if (Preferences::instance()->isAltBandwidthEnabled())
Preferences::instance()->setAltGlobalUploadLimit(limit / 1024.);
else
@ -440,7 +446,7 @@ void WebApplication::action_command_setGlobalDlLimit()
qlonglong limit = request().posts["limit"].toLongLong();
if (limit == 0) limit = -1;
QBtSession::instance()->setDownloadRateLimit(limit);
BitTorrent::Session::instance()->setDownloadRateLimit(limit);
if (Preferences::instance()->isAltBandwidthEnabled())
Preferences::instance()->setAltGlobalDownloadLimit(limit / 1024.);
else
@ -474,9 +480,9 @@ void WebApplication::action_command_setTorrentsUpLimit()
QStringList hashes = request().posts["hashes"].split("|");
foreach (const QString &hash, hashes) {
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
if (h.is_valid())
h.set_upload_limit(limit);
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
if (torrent)
torrent->setUploadLimit(limit);
}
}
@ -491,16 +497,16 @@ void WebApplication::action_command_setTorrentsDlLimit()
QStringList hashes = request().posts["hashes"].split("|");
foreach (const QString &hash, hashes) {
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
if (h.is_valid())
h.set_download_limit(limit);
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
if (torrent)
torrent->setDownloadLimit(limit);
}
}
void WebApplication::action_command_toggleAlternativeSpeedLimits()
{
CHECK_URI(0);
QBtSession::instance()->useAlternativeSpeedsLimit(!Preferences::instance()->isAltBandwidthEnabled());
BitTorrent::Session::instance()->changeSpeedLimitMode(!Preferences::instance()->isAltBandwidthEnabled());
}
void WebApplication::action_command_alternativeSpeedLimitsEnabled()
@ -515,11 +521,9 @@ void WebApplication::action_command_toggleSequentialDownload()
CHECK_PARAMETERS("hashes");
QStringList hashes = request().posts["hashes"].split("|");
foreach (const QString &hash, hashes) {
try {
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
h.toggleSequentialDownload();
}
catch(invalid_handle&) {}
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
if (torrent)
torrent->toggleSequentialDownload();
}
}
@ -529,11 +533,9 @@ void WebApplication::action_command_toggleFirstLastPiecePrio()
CHECK_PARAMETERS("hashes");
QStringList hashes = request().posts["hashes"].split("|");
foreach (const QString &hash, hashes) {
try {
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
h.toggleFirstLastPiecePrio();
}
catch(invalid_handle&) {}
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
if (torrent)
torrent->toggleFirstLastPiecePriority();
}
}
@ -544,11 +546,9 @@ void WebApplication::action_command_setSuperSeeding()
bool value = request().posts["value"] == "true";
QStringList hashes = request().posts["hashes"].split("|");
foreach (const QString &hash, hashes) {
try {
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
h.super_seeding(value);
}
catch(invalid_handle&) {}
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
if (torrent)
torrent->setSuperSeeding(value);
}
}
@ -559,9 +559,9 @@ void WebApplication::action_command_setForceStart()
bool value = request().posts["value"] == "true";
QStringList hashes = request().posts["hashes"].split("|");
foreach (const QString &hash, hashes) {
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
if (h.is_valid())
QBtSession::instance()->resumeTorrent(hash, value);
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
if (torrent)
torrent->resume(value);
}
}
@ -571,7 +571,7 @@ void WebApplication::action_command_delete()
CHECK_PARAMETERS("hashes");
QStringList hashes = request().posts["hashes"].split("|");
foreach (const QString &hash, hashes)
QBtSession::instance()->deleteTorrent(hash, false);
BitTorrent::Session::instance()->deleteTorrent(hash, false);
}
void WebApplication::action_command_deletePerm()
@ -580,7 +580,7 @@ void WebApplication::action_command_deletePerm()
CHECK_PARAMETERS("hashes");
QStringList hashes = request().posts["hashes"].split("|");
foreach (const QString &hash, hashes)
QBtSession::instance()->deleteTorrent(hash, true);
BitTorrent::Session::instance()->deleteTorrent(hash, true);
}
void WebApplication::action_command_increasePrio()
@ -594,32 +594,7 @@ void WebApplication::action_command_increasePrio()
}
QStringList hashes = request().posts["hashes"].split("|");
std::priority_queue<QPair<int, QTorrentHandle>,
std::vector<QPair<int, QTorrentHandle> >,
std::greater<QPair<int, QTorrentHandle> > > torrent_queue;
// Sort torrents by priority
foreach (const QString &hash, hashes) {
try {
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
if (!h.is_seed())
torrent_queue.push(qMakePair(h.queue_position(), h));
}
catch(invalid_handle&) {}
}
// Increase torrents priority (starting with the ones with highest priority)
while(!torrent_queue.empty()) {
QTorrentHandle h = torrent_queue.top().second;
try {
h.queue_position_up();
}
catch(invalid_handle&) {}
torrent_queue.pop();
}
BitTorrent::Session::instance()->increaseTorrentsPriority(hashes);
}
void WebApplication::action_command_decreasePrio()
@ -633,33 +608,7 @@ void WebApplication::action_command_decreasePrio()
}
QStringList hashes = request().posts["hashes"].split("|");
std::priority_queue<QPair<int, QTorrentHandle>,
std::vector<QPair<int, QTorrentHandle> >,
std::less<QPair<int, QTorrentHandle> > > torrent_queue;
// Sort torrents by priority
foreach (const QString &hash, hashes) {
try {
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
if (!h.is_seed())
torrent_queue.push(qMakePair(h.queue_position(), h));
}
catch(invalid_handle&) {}
}
// Decrease torrents priority (starting with the ones with lowest priority)
while(!torrent_queue.empty()) {
QTorrentHandle h = torrent_queue.top().second;
try {
h.queue_position_down();
}
catch(invalid_handle&) {}
torrent_queue.pop();
}
BitTorrent::Session::instance()->decreaseTorrentsPriority(hashes);
}
void WebApplication::action_command_topPrio()
@ -672,10 +621,8 @@ void WebApplication::action_command_topPrio()
return;
}
foreach (const QString &hash, request().posts["hashes"].split("|")) {
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
if (h.is_valid()) h.queue_position_top();
}
QStringList hashes = request().posts["hashes"].split("|");
BitTorrent::Session::instance()->topTorrentsPriority(hashes);
}
void WebApplication::action_command_bottomPrio()
@ -688,17 +635,18 @@ void WebApplication::action_command_bottomPrio()
return;
}
foreach (const QString &hash, request().posts["hashes"].split("|")) {
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
if (h.is_valid()) h.queue_position_bottom();
}
QStringList hashes = request().posts["hashes"].split("|");
BitTorrent::Session::instance()->bottomTorrentsPriority(hashes);
}
void WebApplication::action_command_recheck()
{
CHECK_URI(0);
CHECK_PARAMETERS("hash");
QBtSession::instance()->recheckTorrent(request().posts["hash"]);
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(request().posts["hash"]);
if (torrent)
torrent->forceRecheck();
}
bool WebApplication::isPublicScope()