qBittorrent/src/httpserver.cpp

122 lines
5 KiB
C++
Raw Normal View History

2008-05-17 17:23:37 +04:00
/*
2009-04-05 22:48:45 +04:00
* Bittorrent Client using Qt4 and libtorrent.
* Copyright (C) 2006 Ishan Arora and Christophe Dumez
2008-05-17 17:23:37 +04:00
*
2009-04-05 22:48:45 +04:00
* 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.
2008-05-17 17:23:37 +04:00
*
2009-04-05 22:48:45 +04:00
* 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.
2008-05-17 17:23:37 +04:00
*
2009-04-05 22:48:45 +04:00
* 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.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*
* Contact : chris@qbittorrent.org
2008-05-17 17:23:37 +04:00
*/
#include "httpserver.h"
#include "httpconnection.h"
#include "eventmanager.h"
#include "bittorrent.h"
#include <QTimer>
HttpServer::HttpServer(Bittorrent *_BTSession, int msec, QObject* parent) : QTcpServer(parent)
2008-05-17 17:23:37 +04:00
{
base64 = QByteArray(":").toBase64();
connect(this, SIGNAL(newConnection()), this, SLOT(newHttpConnection()));
2008-12-25 17:33:37 +03:00
BTSession = _BTSession;
manager = new EventManager(this, BTSession);
2008-05-17 17:23:37 +04:00
//add torrents
std::vector<torrent_handle> torrents = BTSession->getTorrents();
std::vector<torrent_handle>::iterator torrentIT;
for(torrentIT = torrents.begin(); torrentIT != torrents.end(); torrentIT++) {
QTorrentHandle h = QTorrentHandle(*torrentIT);
if(h.is_valid())
manager->addedTorrent(h);
}
2008-05-17 17:23:37 +04:00
//connect BTSession to manager
connect(BTSession, SIGNAL(addedTorrent(QTorrentHandle&)), manager, SLOT(addedTorrent(QTorrentHandle&)));
2008-05-17 17:23:37 +04:00
connect(BTSession, SIGNAL(deletedTorrent(QString)), manager, SLOT(deletedTorrent(QString)));
//set timer
2008-09-12 23:58:57 +04:00
timer = new QTimer(this);
2008-05-17 17:23:37 +04:00
connect(timer, SIGNAL(timeout()), this, SLOT(onTimer()));
timer->start(msec);
// Additional translations for Web UI
QString a = tr("File");
a = tr("Edit");
a = tr("Help");
a = tr("Delete from HD");
a = tr("Download Torrents from their URL or Magnet link");
a = tr("Only one link per line");
a = tr("Download local torrent");
a = tr("Torrent files were correctly added to download list.");
a = tr("Point to torrent file");
a = tr("Download");
a = tr("Are you sure you want to delete the selected torrents from the transfer list and hard disk?");
2008-05-17 17:23:37 +04:00
}
HttpServer::~HttpServer()
{
2008-09-12 23:58:57 +04:00
delete timer;
2008-05-17 17:23:37 +04:00
delete manager;
}
void HttpServer::newHttpConnection()
{
QTcpSocket *socket;
while((socket = nextPendingConnection()))
{
HttpConnection *connection = new HttpConnection(socket, BTSession, this);
2008-05-17 17:23:37 +04:00
//connect connection to BTSession
connect(connection, SIGNAL(UrlReadyToBeDownloaded(QString)), BTSession, SLOT(downloadUrlAndSkipDialog(QString)));
2009-08-21 18:05:03 +04:00
connect(connection, SIGNAL(MagnetReadyToBeDownloaded(QString)), BTSession, SLOT(addMagnetSkipAddDlg(QString)));
connect(connection, SIGNAL(torrentReadyToBeDownloaded(QString, bool, QString, bool)), BTSession, SLOT(addTorrent(QString, bool, QString, bool)));
connect(connection, SIGNAL(deleteTorrent(QString, bool)), BTSession, SLOT(deleteTorrent(QString, bool)));
2008-05-17 17:23:37 +04:00
connect(connection, SIGNAL(pauseTorrent(QString)), BTSession, SLOT(pauseTorrent(QString)));
connect(connection, SIGNAL(resumeTorrent(QString)), BTSession, SLOT(resumeTorrent(QString)));
connect(connection, SIGNAL(pauseAllTorrents()), BTSession, SLOT(pauseAllTorrents()));
connect(connection, SIGNAL(resumeAllTorrents()), BTSession, SLOT(resumeAllTorrents()));
}
}
void HttpServer::onTimer() {
std::vector<torrent_handle> torrents = BTSession->getTorrents();
std::vector<torrent_handle>::iterator torrentIT;
for(torrentIT = torrents.begin(); torrentIT != torrents.end(); torrentIT++) {
QTorrentHandle h = QTorrentHandle(*torrentIT);
if(h.is_valid())
manager->modifiedTorrent(h);
}
2008-05-17 17:23:37 +04:00
}
void HttpServer::setAuthorization(QString username, QString password)
{
QString cat = username + ":" + password;
base64 = QByteArray(cat.toLocal8Bit()).toBase64();
2008-05-17 17:23:37 +04:00
}
bool HttpServer::isAuthorized(QByteArray auth) const
{
return (auth == base64);
}
2008-12-25 17:33:37 +03:00
EventManager* HttpServer::eventManager() const
2008-05-17 17:23:37 +04:00
{
return manager;
}