2006-09-30 20:02:39 +04:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt4 and libtorrent.
|
2007-07-14 18:31:59 +04:00
|
|
|
* Copyright (C) 2006 Christophe Dumez
|
2006-09-30 20:02:39 +04:00
|
|
|
*
|
2007-07-14 18:31:59 +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.
|
2006-09-30 20:02:39 +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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2007-07-14 18:31:59 +04:00
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
2009-04-05 21:00:55 +04:00
|
|
|
* 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.
|
|
|
|
*
|
2007-07-14 18:31:59 +04:00
|
|
|
* Contact : chris@qbittorrent.org
|
2006-09-30 20:02:39 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QLocale>
|
|
|
|
#include <QTranslator>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QSplashScreen>
|
2006-11-14 23:26:32 +03:00
|
|
|
#include <QSettings>
|
2008-07-06 22:39:07 +04:00
|
|
|
#ifdef QT_4_4
|
|
|
|
#include <QLocalSocket>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#else
|
|
|
|
#include <QTcpSocket>
|
|
|
|
#include <QHostAddress>
|
|
|
|
#endif
|
2007-03-28 12:05:37 +04:00
|
|
|
#include <QPlastiqueStyle>
|
2007-10-13 12:32:31 +04:00
|
|
|
#include "qgnomelook.h"
|
2007-03-28 12:05:37 +04:00
|
|
|
#include <QMotifStyle>
|
|
|
|
#include <QCDEStyle>
|
2007-03-19 17:45:17 +03:00
|
|
|
#ifdef Q_WS_WIN
|
|
|
|
#include <QWindowsXPStyle>
|
|
|
|
#endif
|
|
|
|
#ifdef Q_WS_MAC
|
|
|
|
#include <QMacStyle>
|
|
|
|
#endif
|
2008-12-26 19:23:53 +03:00
|
|
|
#ifndef Q_WS_WIN
|
|
|
|
#include <signal.h>
|
2009-01-18 18:12:38 +03:00
|
|
|
#include <execinfo.h>
|
|
|
|
#include "stacktrace.h"
|
2008-12-26 19:23:53 +03:00
|
|
|
#endif
|
2007-03-19 17:45:17 +03:00
|
|
|
|
2006-09-30 20:02:39 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include "GUI.h"
|
|
|
|
#include "misc.h"
|
2008-11-05 01:39:43 +03:00
|
|
|
#include "ico.h"
|
|
|
|
|
2008-12-26 19:23:53 +03:00
|
|
|
QApplication *app;
|
|
|
|
|
|
|
|
#ifndef Q_WS_WIN
|
|
|
|
void sigtermHandler(int) {
|
|
|
|
qDebug("Catching SIGTERM, exiting cleanly");
|
|
|
|
app->exit();
|
|
|
|
}
|
2009-01-18 18:12:38 +03:00
|
|
|
void sigsegvHandler(int) {
|
|
|
|
std::cerr << "\n\n*************************************************************\n";
|
|
|
|
std::cerr << "Catching SIGSEGV, please report a bug at http://bug.qbittorrent.org\nand provide the following backtrace:\n";
|
|
|
|
print_stacktrace();
|
|
|
|
std::raise(SIGINT);
|
|
|
|
std::abort();
|
|
|
|
}
|
2008-12-26 19:23:53 +03:00
|
|
|
#endif
|
2006-09-30 20:02:39 +04:00
|
|
|
|
2007-09-08 21:07:29 +04:00
|
|
|
void useStyle(QApplication *app, int style){
|
|
|
|
switch(style) {
|
2007-10-03 23:18:55 +04:00
|
|
|
case 1:
|
2007-09-08 21:07:29 +04:00
|
|
|
app->setStyle(new QPlastiqueStyle());
|
|
|
|
break;
|
2007-10-03 23:18:55 +04:00
|
|
|
case 2:
|
2007-10-13 12:32:31 +04:00
|
|
|
app->setStyle(new QGnomeLookStyle());
|
2007-09-08 21:07:29 +04:00
|
|
|
break;
|
2007-10-03 23:18:55 +04:00
|
|
|
case 3:
|
2007-09-08 21:07:29 +04:00
|
|
|
app->setStyle(new QMotifStyle());
|
|
|
|
break;
|
2007-10-03 23:18:55 +04:00
|
|
|
case 4:
|
2007-09-08 21:07:29 +04:00
|
|
|
app->setStyle(new QCDEStyle());
|
|
|
|
break;
|
2007-03-28 12:05:37 +04:00
|
|
|
#ifdef Q_WS_MAC
|
2007-10-03 23:18:55 +04:00
|
|
|
case 5:
|
2007-03-28 12:05:37 +04:00
|
|
|
app->setStyle(new QMacStyle());
|
2007-09-08 21:07:29 +04:00
|
|
|
break;
|
2007-03-28 12:05:37 +04:00
|
|
|
#endif
|
|
|
|
#ifdef Q_WS_WIN
|
2007-10-03 23:18:55 +04:00
|
|
|
case 6:
|
2007-03-28 12:05:37 +04:00
|
|
|
app->setStyle(new QWindowsXPStyle());
|
2007-09-08 21:07:29 +04:00
|
|
|
break;
|
2007-03-28 12:05:37 +04:00
|
|
|
#endif
|
2009-07-12 07:35:04 +04:00
|
|
|
default:
|
|
|
|
if(app->style()->objectName() == "cleanlooks") {
|
|
|
|
// Force our own cleanlooks style
|
|
|
|
qDebug("Forcing our own cleanlooks style");
|
|
|
|
app->setStyle(new QGnomeLookStyle());
|
|
|
|
}
|
2007-09-08 21:07:29 +04:00
|
|
|
}
|
2007-03-28 12:05:37 +04:00
|
|
|
}
|
|
|
|
|
2006-09-30 20:02:39 +04:00
|
|
|
// Main
|
|
|
|
int main(int argc, char *argv[]){
|
|
|
|
QFile file;
|
|
|
|
QString locale;
|
|
|
|
if(argc > 1){
|
2007-08-20 11:03:41 +04:00
|
|
|
if(QString::fromUtf8(argv[1]) == QString::fromUtf8("--version")){
|
2006-09-30 20:02:39 +04:00
|
|
|
std::cout << "qBittorrent " << VERSION << '\n';
|
|
|
|
return 0;
|
|
|
|
}
|
2007-08-20 11:03:41 +04:00
|
|
|
if(QString::fromUtf8(argv[1]) == QString::fromUtf8("--help")){
|
2006-09-30 20:02:39 +04:00
|
|
|
std::cout << "Usage: \n";
|
|
|
|
std::cout << '\t' << argv[0] << " --version : displays program version\n";
|
|
|
|
std::cout << '\t' << argv[0] << " --help : displays this help message\n";
|
|
|
|
std::cout << '\t' << argv[0] << " [files or urls] : starts program and download given parameters (optional)\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Set environment variable
|
2008-12-15 22:24:10 +03:00
|
|
|
if(putenv((char*)"QBITTORRENT="VERSION)) {
|
2006-10-16 13:02:53 +04:00
|
|
|
std::cerr << "Couldn't set environment variable...\n";
|
2006-09-30 20:02:39 +04:00
|
|
|
}
|
2008-07-06 22:39:07 +04:00
|
|
|
QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent"));
|
2006-09-30 20:02:39 +04:00
|
|
|
//Check if there is another instance running
|
2008-07-06 22:39:07 +04:00
|
|
|
#ifdef QT_4_4
|
|
|
|
QLocalSocket localSocket;
|
|
|
|
QString uid = QString::number(getuid());
|
|
|
|
#else
|
|
|
|
QTcpSocket localSocket;
|
|
|
|
#endif
|
|
|
|
#ifdef QT_4_4
|
|
|
|
localSocket.connectToServer("qBittorrent-"+uid, QIODevice::WriteOnly);
|
|
|
|
#else
|
|
|
|
int serverPort = settings.value(QString::fromUtf8("uniqueInstancePort"), -1).toInt();
|
|
|
|
if(serverPort != -1) {
|
|
|
|
localSocket.connectToHost(QHostAddress::LocalHost, serverPort, QIODevice::WriteOnly);
|
|
|
|
#endif
|
|
|
|
if (localSocket.waitForConnected(1000)){
|
2006-12-05 03:27:00 +03:00
|
|
|
std::cout << "Another qBittorrent instance is already running...\n";
|
|
|
|
// Send parameters
|
|
|
|
if(argc > 1){
|
|
|
|
QStringList params;
|
|
|
|
for(int i=1;i<argc;++i){
|
2007-08-20 11:03:41 +04:00
|
|
|
params << QString::fromUtf8(argv[i]);
|
|
|
|
std::cout << argv[i] << '\n';
|
2006-12-05 03:27:00 +03:00
|
|
|
}
|
|
|
|
QByteArray block = params.join("\n").toUtf8();
|
|
|
|
std::cout << "writting: " << block.data() << '\n';
|
|
|
|
std::cout << "size: " << block.size() << '\n';
|
2008-07-06 22:39:07 +04:00
|
|
|
uint val = localSocket.write(block);
|
|
|
|
if(localSocket.waitForBytesWritten(5000)){
|
2006-12-05 03:27:00 +03:00
|
|
|
std::cout << "written(" <<val<<"): " << block.data() << '\n';
|
|
|
|
}else{
|
|
|
|
std::cerr << "Writing to the socket timed out\n";
|
|
|
|
}
|
2008-07-06 22:39:07 +04:00
|
|
|
#ifdef QT_4_4
|
|
|
|
localSocket.disconnectFromServer();
|
|
|
|
#else
|
|
|
|
localSocket.disconnectFromHost();
|
|
|
|
#endif
|
2006-12-05 03:27:00 +03:00
|
|
|
std::cout << "disconnected\n";
|
2006-09-30 20:02:39 +04:00
|
|
|
}
|
2008-07-06 22:39:07 +04:00
|
|
|
localSocket.close();
|
2006-09-30 20:02:39 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2008-07-06 22:39:07 +04:00
|
|
|
#ifndef QT_4_4
|
|
|
|
}
|
|
|
|
#endif
|
2008-12-26 19:23:53 +03:00
|
|
|
app = new QApplication(argc, argv);
|
|
|
|
useStyle(app, settings.value("Preferences/General/Style", 0).toInt());
|
|
|
|
app->setStyleSheet("QStatusBar::item { border-width: 0; }");
|
2009-04-26 12:19:38 +04:00
|
|
|
QSplashScreen *splash = new QSplashScreen(QPixmap(QString::fromUtf8(":/Icons/skin/splash.png")));
|
2006-09-30 20:02:39 +04:00
|
|
|
splash->show();
|
|
|
|
// Open options file to read locale
|
2007-09-08 21:07:29 +04:00
|
|
|
locale = settings.value(QString::fromUtf8("Preferences/General/Locale"), QString()).toString();
|
2006-09-30 20:02:39 +04:00
|
|
|
QTranslator translator;
|
2006-11-14 23:26:32 +03:00
|
|
|
if(locale.isEmpty()){
|
2006-09-30 20:02:39 +04:00
|
|
|
locale = QLocale::system().name();
|
2007-09-08 21:07:29 +04:00
|
|
|
settings.setValue(QString::fromUtf8("Preferences/General/Locale"), locale);
|
2006-09-30 20:02:39 +04:00
|
|
|
}
|
2007-08-20 11:03:41 +04:00
|
|
|
if(translator.load(QString::fromUtf8(":/lang/qbittorrent_") + locale)){
|
2006-10-26 00:35:41 +04:00
|
|
|
qDebug("%s locale recognized, using translation.", (const char*)locale.toUtf8());
|
2006-09-30 20:02:39 +04:00
|
|
|
}else{
|
2006-10-26 00:35:41 +04:00
|
|
|
qDebug("%s locale unrecognized, using default (en_GB).", (const char*)locale.toUtf8());
|
2006-09-30 20:02:39 +04:00
|
|
|
}
|
2008-12-26 19:23:53 +03:00
|
|
|
app->installTranslator(&translator);
|
|
|
|
app->setApplicationName(QString::fromUtf8("qBittorrent"));
|
|
|
|
app->setQuitOnLastWindowClosed(false);
|
2009-01-18 18:12:38 +03:00
|
|
|
#ifndef Q_WS_WIN
|
|
|
|
signal(SIGTERM, sigtermHandler);
|
|
|
|
signal(SIGSEGV, sigsegvHandler);
|
|
|
|
#endif
|
2006-09-30 20:02:39 +04:00
|
|
|
// Read torrents given on command line
|
2008-12-26 19:23:53 +03:00
|
|
|
QStringList torrentCmdLine = app->arguments();
|
2006-09-30 20:02:39 +04:00
|
|
|
// Remove first argument (program name)
|
|
|
|
torrentCmdLine.removeFirst();
|
2008-12-26 19:27:52 +03:00
|
|
|
GUI *window = new GUI(0, torrentCmdLine);
|
|
|
|
splash->finish(window);
|
2006-09-30 20:02:39 +04:00
|
|
|
delete splash;
|
2008-12-26 19:27:52 +03:00
|
|
|
int ret = app->exec();
|
|
|
|
delete window;
|
|
|
|
delete app;
|
|
|
|
return ret;
|
2006-09-30 20:02:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|