forked from qbittorrent/qBittorrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
166 lines (159 loc) · 4.89 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Bittorrent Client using Qt4 and libtorrent.
* Copyright (C) 2006 Christophe Dumez
*
* 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.
*
* 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
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Contact : [email protected]
*/
#include <QApplication>
#include <QLocale>
#include <QTranslator>
#include <QFile>
#include <QSplashScreen>
#include <QTcpSocket>
#include <QSettings>
#include <QTcpSocket>
#include <QTcpServer>
#include <QPlastiqueStyle>
#include <QCleanlooksStyle>
#include <QMotifStyle>
#include <QCDEStyle>
#ifdef Q_WS_WIN
#include <QWindowsXPStyle>
#endif
#ifdef Q_WS_MAC
#include <QMacStyle>
#endif
#include <stdlib.h>
#include "GUI.h"
#include "misc.h"
void useStyle(QApplication *app, QString style){
std::cout << "* Style: Using " << style.toStdString() << " style\n";
if(style == "Cleanlooks"){
app->setStyle(new QCleanlooksStyle());
return;
}
if(style == "Motif"){
app->setStyle(new QMotifStyle());
return;
}
if(style == "CDE"){
app->setStyle(new QCDEStyle());
return;
}
#ifdef Q_WS_MAC
if(style == "MacOS"){
app->setStyle(new QMacStyle());
return;
}
#endif
#ifdef Q_WS_WIN
if(style == "WinXP"){
app->setStyle(new QWindowsXPStyle());
return;
}
#endif
app->setStyle(new QPlastiqueStyle());
}
// Main
int main(int argc, char *argv[]){
QFile file;
QString locale;
if(argc > 1){
if(QString(argv[1])=="--version"){
std::cout << "qBittorrent " << VERSION << '\n';
return 0;
}
if(QString(argv[1])=="--help"){
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
if(putenv("QBITTORRENT="VERSION)){
std::cerr << "Couldn't set environment variable...\n";
}
//Check if there is another instance running
QTcpSocket tcpSocket;
tcpSocket.connectToHost(QHostAddress::LocalHost, 1666);
if (tcpSocket.waitForConnected(1000)){
std::cout << "Another qBittorrent instance is already running...\n";
// Send parameters
if(argc > 1){
QStringList params;
for(int i=1;i<argc;++i){
params << QString(argv[i]);
std::cout << QString(argv[i]).toStdString() << '\n';
}
QByteArray block = params.join("\n").toUtf8();
std::cout << "writting: " << block.data() << '\n';
std::cout << "size: " << block.size() << '\n';
uint val = tcpSocket.write(block);
if(tcpSocket.waitForBytesWritten(5000)){
std::cout << "written(" <<val<<"): " << block.data() << '\n';
}else{
std::cerr << "Writing to the socket timed out\n";
}
tcpSocket.disconnectFromHost();
std::cout << "disconnected\n";
}
tcpSocket.close();
return 0;
}
QApplication app(argc, argv);
QSettings settings("qBittorrent", "qBittorrent");
QString style;
#ifdef Q_WS_WIN
style = settings.value("Options/Style", "WinXP").toString();
#endif
#ifdef Q_WS_MAC
style = settings.value("Options/Style", "MacOS").toString();
#endif
#ifndef Q_WS_WIN
#ifndef Q_WS_MAC
style = settings.value("Options/Style", "Plastique").toString();
#endif
#endif
useStyle(&app, style);
QSplashScreen *splash = new QSplashScreen(QPixmap(":/Icons/splash.png"));
splash->show();
// Open options file to read locale
locale = settings.value("Options/Language/Locale", QString()).toString();
QTranslator translator;
if(locale.isEmpty()){
locale = QLocale::system().name();
settings.setValue("Options/Language/Locale", locale);
}
if(translator.load(QString(":/lang/qbittorrent_") + locale)){
qDebug("%s locale recognized, using translation.", (const char*)locale.toUtf8());
}else{
qDebug("%s locale unrecognized, using default (en_GB).", (const char*)locale.toUtf8());
}
app.installTranslator(&translator);
app.setApplicationName("qBittorrent");
app.setQuitOnLastWindowClosed(false);
// Read torrents given on command line
QStringList torrentCmdLine = app.arguments();
// Remove first argument (program name)
torrentCmdLine.removeFirst();
GUI window(0, torrentCmdLine);
splash->finish(&window);
delete splash;
return app.exec();
}