Browse Source

- Rewritten most of the code for options

- Fix: Could only listen on the 1st port of given range
- Fix: Option "use torrent addition dialog" wasn't saved correctly
adaptive-webui-19844
Christophe Dumez 18 years ago
parent
commit
0722195cad
  1. 28
      src/main.cpp
  2. 473
      src/options_imp.cpp
  3. 4
      src/options_imp.h

28
src/main.cpp

@ -20,12 +20,12 @@
*/ */
#include <QApplication> #include <QApplication>
#include <QtXml/QDomDocument>
#include <QLocale> #include <QLocale>
#include <QTranslator> #include <QTranslator>
#include <QFile> #include <QFile>
#include <QSplashScreen> #include <QSplashScreen>
#include <QTcpSocket> #include <QTcpSocket>
#include <QSettings>
#include <stdlib.h> #include <stdlib.h>
@ -34,9 +34,6 @@
// Main // Main
int main(int argc, char *argv[]){ int main(int argc, char *argv[]){
QDomDocument doc("options");
QDomElement root;
QDomElement tag;
QFile file; QFile file;
QString locale; QString locale;
if(argc > 1){ if(argc > 1){
@ -121,27 +118,12 @@ int main(int argc, char *argv[]){
QApplication app(argc, argv); QApplication app(argc, argv);
QSplashScreen *splash = new QSplashScreen(QPixmap(":/Icons/splash.jpg")); QSplashScreen *splash = new QSplashScreen(QPixmap(":/Icons/splash.jpg"));
splash->show(); splash->show();
bool isLocalized = false;
// Open options file to read locale // Open options file to read locale
QString optionsPath = misc::qBittorrentPath()+"options.xml"; QSettings settings("qBittorrent", "qBittorrent");
FILE *f = fopen((const char*)optionsPath.toUtf8(), "r"); locale = settings.value("Options/Language/Locale", QString()).toString();
if(f){
if (file.open(f, QIODevice::ReadOnly | QIODevice::Text)){
if (doc.setContent(&file)) {
root = doc.firstChildElement("options");
tag = root.firstChildElement("locale");
if(!tag.isNull()){
locale = tag.text();
isLocalized = true;
}
}
}
file.close();
fclose(f);
}
QTranslator translator; QTranslator translator;
if(!isLocalized){ if(locale.isEmpty()){
locale = QLocale::system().name(); locale = QLocale::system().name();
} }
if(translator.load(QString(":/lang/qbittorrent_") + locale)){ if(translator.load(QString(":/lang/qbittorrent_") + locale)){
@ -156,9 +138,7 @@ int main(int argc, char *argv[]){
torrentCmdLine.removeFirst(); torrentCmdLine.removeFirst();
GUI window(0, torrentCmdLine); GUI window(0, torrentCmdLine);
// Set locale // Set locale
if(!isLocalized){
window.setLocale(locale); window.setLocale(locale);
}
// Show main window // Show main window
window.show(); window.show();
splash->finish(&window); splash->finish(&window);

473
src/options_imp.cpp

@ -22,8 +22,8 @@
#include <QFileDialog> #include <QFileDialog>
#include <QMessageBox> #include <QMessageBox>
#include <QInputDialog> #include <QInputDialog>
#include <QDomDocument>
#include <QSystemTrayIcon> #include <QSystemTrayIcon>
#include <QSettings>
#include "options_imp.h" #include "options_imp.h"
#include "misc.h" #include "misc.h"
@ -100,9 +100,8 @@ options_imp::options_imp(QWidget *parent):QDialog(parent){
if(! QFile::exists(savePath)){ if(! QFile::exists(savePath)){
saveOptions(); saveOptions();
} }
if(!loadOptions()){ // Load options
std::cerr << "Warning: Couldn't load options" << '\n'; loadOptions();
}
// Connect signals / slots // Connect signals / slots
connect(disableUPLimit, SIGNAL(stateChanged(int)), this, SLOT(disableUpload(int))); connect(disableUPLimit, SIGNAL(stateChanged(int)), this, SLOT(disableUpload(int)));
connect(disableDLLimit, SIGNAL(stateChanged(int)), this, SLOT(disableDownload(int))); connect(disableDLLimit, SIGNAL(stateChanged(int)), this, SLOT(disableDownload(int)));
@ -161,373 +160,257 @@ options_imp::options_imp(QWidget *parent):QDialog(parent){
} }
} }
// save options to options.xml file void options_imp::saveOptions(){
bool options_imp::saveOptions(){ QSettings settings("qBittorrent", "qBittorrent");
QString savePath;
QDomDocument doc("options");
QDomElement root;
QDomElement tag, tag2;
QDomText optionValue;
QString xml;
FILE *f;
QFile file;
// Getting HOME evironment variable
savePath = misc::qBittorrentPath() + "options.xml";
// Check if min port < max port // Check if min port < max port
checkPortsLogic(); checkPortsLogic();
// Generate XML settings.beginGroup("Options");
root = doc.createElement("options"); // Main options
doc.appendChild(root); settings.beginGroup("Main");
// Main Options settings.setValue("DLLimit", getLimits().first);
tag = doc.createElement("save_path"); settings.setValue("UPLimit", getLimits().second);
root.appendChild(tag); settings.setValue("MaxConnecs", getMaxConnec());
optionValue = doc.createTextNode(txt_savePath->text()); settings.setValue("PortRangeMin", getPorts().first);
tag.appendChild(optionValue); settings.setValue("PortRangeMax", getPorts().second);
tag = doc.createElement("dl_limit"); settings.setValue("ShareRatio", getRatio());
root.appendChild(tag); settings.setValue("DHTPort", getDHTPort());
if(spin_download->isEnabled()){ settings.setValue("ScanDir", getScanDir());
optionValue = doc.createTextNode(misc::toString(spin_download->value()).c_str()); // End Main options
}else{ settings.endGroup();
optionValue = doc.createTextNode(misc::toString(-1).c_str()); // Language options
} settings.beginGroup("Language");
tag.appendChild(optionValue); settings.setValue("Locale", getLocale());
tag = doc.createElement("up_limit"); // End Language options
root.appendChild(tag); settings.endGroup();
if(spin_upload->isEnabled()){ // IPFilter options
optionValue = doc.createTextNode(misc::toString(spin_upload->value()).c_str()); settings.beginGroup("IPFilter");
}else{ bool enabled = isFilteringEnabled();
optionValue = doc.createTextNode(misc::toString(-1).c_str()); settings.setValue("Enabled", enabled);
} if(enabled){
tag.appendChild(optionValue); settings.setValue("File", filterFile->text());
tag = doc.createElement("max_connec"); }
root.appendChild(tag); // End IPFilter options
if(spin_max_connec->isEnabled()){ settings.endGroup();
optionValue = doc.createTextNode(misc::toString(spin_max_connec->value()).c_str()); // Proxy options
}else{ settings.beginGroup("Proxy");
optionValue = doc.createTextNode(misc::toString(-1).c_str()); enabled = isProxyEnabled();
} settings.setValue("Enabled", enabled);
tag.appendChild(optionValue); if(enabled){
tag = doc.createElement("port_range"); settings.setValue("IP", getProxyIp());
root.appendChild(tag); settings.setValue("Port", getProxyPort());
tag2 = doc.createElement("port_min"); enabled = isProxyAuthEnabled();
tag.appendChild(tag2); settings.beginGroup("Authentication");
optionValue = doc.createTextNode(misc::toString(spin_port_min->value()).c_str()); settings.setValue("Enabled", enabled);
tag2.appendChild(optionValue); if(enabled){
tag2 = doc.createElement("port_max"); settings.setValue("Username", getProxyUsername());
tag.appendChild(tag2); settings.setValue("Password", getProxyPassword());
optionValue = doc.createTextNode(misc::toString(spin_port_max->value()).c_str()); }
tag2.appendChild(optionValue); settings.endGroup();
tag = doc.createElement("ratio"); }
root.appendChild(tag); // End Proxy options
if(spin_ratio->isEnabled()){ settings.endGroup();
optionValue = doc.createTextNode(misc::toString(spin_ratio->value()).c_str()); // Misc options
}else{ settings.beginGroup("Misc");
optionValue = doc.createTextNode("0"); settings.beginGroup("TorrentAdditionDialog");
} enabled = useAdditionDialog();
tag.appendChild(optionValue); settings.setValue("Enabled", enabled);
tag = doc.createElement("DHT"); if(!enabled){
root.appendChild(tag); settings.setValue("SavePath", getSavePath());
if(disableDHT->isChecked()){ }
optionValue = doc.createTextNode("0"); settings.endGroup();
}else{ settings.beginGroup("Behaviour");
optionValue = doc.createTextNode("1"); settings.setValue("ConfirmOnExit", getConfirmOnExit());
} settings.setValue("ClearFinishedDownloads", getClearFinishedOnExit());
tag.appendChild(optionValue); settings.setValue("GoToSystray", getGoToSysTrayOnMinimizingWindow());
tag = doc.createElement("DHTPort"); settings.endGroup();
root.appendChild(tag); settings.setValue("PreviewProgram", getPreviewProgram());
optionValue = doc.createTextNode(misc::toString(getDHTPort()).c_str()); // End Misc options
tag.appendChild(optionValue); settings.endGroup();
if(scanDir->isEnabled()){ if(getUseOSDAlways()){
tag = doc.createElement("scan_dir"); settings.setValue("OSDEnabled", 1);
root.appendChild(tag); }else{
optionValue = doc.createTextNode(scanDir->text()); if(getUseOSDWhenHiddenOnly()){
tag.appendChild(optionValue); settings.setValue("OSDEnabled", 2);
} }else{
// IPFilter Settings settings.setValue("OSDEnabled", 0);
if(filterGroup->isEnabled()){ }
tag = doc.createElement("ipFilter_File"); }
root.appendChild(tag); settings.endGroup();
optionValue = doc.createTextNode(filterFile->text());
tag.appendChild(optionValue);
}
// Proxy Settings
if(groupProxy->isEnabled()){
tag = doc.createElement("proxy_settings");
root.appendChild(tag);
tag2 = doc.createElement("proxy_ip");
tag.appendChild(tag2);
optionValue = doc.createTextNode(proxy_ip->text());
tag2.appendChild(optionValue);
tag2 = doc.createElement("proxy_port");
tag.appendChild(tag2);
optionValue = doc.createTextNode(proxy_port->text());
tag2.appendChild(optionValue);
// Proxy Authentication
if(groupProxyAuth->isEnabled()){
tag2 = doc.createElement("proxy_username");
tag.appendChild(tag2);
optionValue = doc.createTextNode(proxy_username->text());
tag2.appendChild(optionValue);
tag2 = doc.createElement("proxy_password");
tag.appendChild(tag2);
optionValue = doc.createTextNode(proxy_password->text());
tag2.appendChild(optionValue);
}
}
// GUI stuff
tag = doc.createElement("gui_settings");
root.appendChild(tag);
tag2 = doc.createElement("goToSysTrayOnMinimizing");
if(check_goToSysTray->isChecked()){
optionValue = doc.createTextNode("true");
}else{
optionValue = doc.createTextNode("false");
}
tag2.appendChild(optionValue);
tag.appendChild(tag2);
tag2 = doc.createElement("clearFinishedOnExit");
if(clearFinished_checkBox->isChecked()){
optionValue = doc.createTextNode("true");
}else{
optionValue = doc.createTextNode("false");
}
tag2.appendChild(optionValue);
tag.appendChild(tag2);
tag2 = doc.createElement("confirmOnExit");
if(confirmExit_checkBox->isChecked()){
optionValue = doc.createTextNode("true");
}else{
optionValue = doc.createTextNode("false");
}
tag2.appendChild(optionValue);
tag.appendChild(tag2);
tag2 = doc.createElement("previewing");
optionValue = doc.createTextNode(preview_program->text());
tag2.appendChild(optionValue);
tag.appendChild(tag2);
tag2 = doc.createElement("displayOSD");
if(alwaysOSD->isChecked()){
optionValue = doc.createTextNode("true");
}else{
if(someOSD->isChecked()){
optionValue = doc.createTextNode("partial");
}else{
optionValue = doc.createTextNode("false");
}
}
tag2.appendChild(optionValue);
tag.appendChild(tag2);
// Language Settings
tag = doc.createElement("locale");
root.appendChild(tag);
optionValue = doc.createTextNode(getLocale());
tag.appendChild(optionValue);
xml = doc.toString();
// Write XML file to HD
f = fopen((const char*)savePath.toUtf8(), "w");
if (!f){
std::cerr << "Error: Couldn't create file " << (const char*)savePath.toUtf8() << " for saving!" << '\n';
return false;
}
if (!file.open(f, QIODevice::WriteOnly | QIODevice::Text)){
std::cerr << "Error: Couldn't open file " << (const char*)savePath.toUtf8() << " for saving!" << '\n';
return false;
}
file.write((const char*)xml.toUtf8(), xml.length());
file.close();
if(fclose(f) == EOF){
std::cerr << "Error: Couldn't close file " << (const char*)savePath.toUtf8() << " after saving!" << '\n';
return false;
}
// set infobar text // set infobar text
emit status_changed(tr("Options saved successfully!")); emit status_changed(tr("Options saved successfully!"));
// Disable apply Button // Disable apply Button
applyButton->setEnabled(false); applyButton->setEnabled(false);
return true;
} }
bool options_imp::isFilteringEnabled() const{ bool options_imp::isFilteringEnabled() const{
return activateFilter->isChecked(); return activateFilter->isChecked();
} }
// Load options from options.xml file void options_imp::loadOptions(){
bool options_imp::loadOptions(){ int value;
QString savePath; float floatValue;
QDomDocument doc("options"); QString strValue;
QDomElement root; QSettings settings("qBittorrent", "qBittorrent");
QDomElement tag, tag2; // Check if min port < max port
QDomText optionValue; checkPortsLogic();
QString xml; settings.beginGroup("Options");
FILE *f; // Main options
QFile file; settings.beginGroup("Main");
int tmp; value = settings.value("DLLimit", -1).toInt();
float tmpFloat; if(value < 0){
// Getting savepath for options.xml
savePath = misc::qBittorrentPath() + "options.xml";
// Read XML file on HD
f = fopen((const char*)savePath.toUtf8(), "r");
if (!f){
return false;
}
if (!file.open(f, QIODevice::ReadOnly | QIODevice::Text)){
return false;
}
if (!doc.setContent(&file)) {
file.close();
return false;
}
file.close();
if(fclose(f) == EOF){
std::cerr << "Error: Couldn't close file " << (const char*)savePath.toUtf8() << " after reading!" << '\n';
return false;
}
// Loading option from XML
root = doc.firstChildElement("options");
this->txt_savePath->setText(root.firstChildElement("save_path").text());
tmp = root.firstChildElement("dl_limit").text().toInt();
if(tmp == -1){
disableDLLimit->setChecked(true); disableDLLimit->setChecked(true);
spin_download->setEnabled(false); spin_download->setEnabled(false);
}else{ }else{
disableDLLimit->setChecked(false); disableDLLimit->setChecked(false);
spin_download->setEnabled(true); spin_download->setEnabled(true);
this->spin_download->setValue(tmp); spin_download->setValue(value);
} }
tmp = root.firstChildElement("up_limit").text().toInt(); value = settings.value("UPLimit", -1).toInt();
if(tmp == -1){ if(value < 0){
disableUPLimit->setChecked(true); disableUPLimit->setChecked(true);
spin_upload->setEnabled(false); spin_upload->setEnabled(false);
}else{ }else{
disableUPLimit->setChecked(false); disableUPLimit->setChecked(false);
spin_upload->setEnabled(true); spin_upload->setEnabled(true);
this->spin_upload->setValue(tmp); spin_upload->setValue(value);
}
value = settings.value("MaxConnecs", -1).toInt();
if(value < 0){
disableMaxConnec->setChecked(true);
spin_max_connec->setEnabled(false);
}else{
disableMaxConnec->setChecked(false);
spin_max_connec->setEnabled(true);
spin_max_connec->setValue(value);
} }
tmpFloat = root.firstChildElement("ratio").text().toFloat(); spin_port_min->setValue(settings.value("PortRangeMin", 6881).toInt());
if(tmpFloat == 0){ spin_port_max->setValue(settings.value("PortRangeMax", 6889).toInt());
floatValue = settings.value("ShareRatio", 0).toDouble();
if(floatValue == 0){
disableRatio->setChecked(true); disableRatio->setChecked(true);
spin_ratio->setEnabled(false); spin_ratio->setEnabled(false);
}else{ }else{
disableRatio->setChecked(false); disableRatio->setChecked(false);
spin_ratio->setEnabled(true); spin_ratio->setEnabled(true);
this->spin_ratio->setValue(tmpFloat); spin_ratio->setValue(floatValue);
} }
tag = root.firstChildElement("DHT"); value = settings.value("DHTPort", 6881).toInt();
if(!tag.isNull()){ if(value < 0){
if(tag.text().toInt() == 0){
disableDHT->setChecked(true); disableDHT->setChecked(true);
groupDHT->setEnabled(false); groupDHT->setEnabled(false);
}else{ }else{
disableDHT->setChecked(false); disableDHT->setChecked(false);
groupDHT->setEnabled(true); groupDHT->setEnabled(true);
if(value < 1000){
value = 6881;
} }
spin_dht_port->setValue(value);
} }
tag = root.firstChildElement("DHTPort"); strValue = settings.value("ScanDir", QString()).toString();
if(!tag.isNull()){ if(!strValue.isEmpty()){
if(tag.text().toInt() < 1000){
spin_dht_port->setValue(6881);
}else{
spin_dht_port->setValue(tag.text().toInt());
}
}
tmp = root.firstChildElement("max_connec").text().toInt();
if(tmp == -1){
disableMaxConnec->setChecked(true);
spin_max_connec->setEnabled(false);
}else{
disableMaxConnec->setChecked(false);
spin_max_connec->setEnabled(true);
this->spin_max_connec->setValue(tmp);
}
tag = root.firstChildElement("port_range");
this->spin_port_min->setValue(tag.firstChildElement("port_min").text().toInt());
this->spin_port_max->setValue(tag.firstChildElement("port_max").text().toInt());
tag = root.firstChildElement("scan_dir");
if(!tag.isNull()){
enableScan_checkBox->setChecked(true); enableScan_checkBox->setChecked(true);
scanDir->setEnabled(true); scanDir->setEnabled(true);
scanDir->setText(tag.text()); scanDir->setText(strValue);
}else{ }else{
enableScan_checkBox->setChecked(false); enableScan_checkBox->setChecked(false);
scanDir->setEnabled(false); scanDir->setEnabled(false);
} }
// Load IPFilter Settings // End Main options
tag = root.firstChildElement("ipFilter_File"); settings.endGroup();
if(!tag.isNull()){ // Language options
settings.beginGroup("Language");
strValue = settings.value("Locale", "en_GB").toString();
setLocale(strValue);
// End Language options
settings.endGroup();
// IPFilter options
settings.beginGroup("IPFilter");
if(settings.value("Enabled", false).toBool()){
strValue = settings.value("File", QString()).toString();
if(strValue.isEmpty()){
activateFilter->setChecked(false);
filterGroup->setEnabled(false);
}else{
activateFilter->setChecked(true); activateFilter->setChecked(true);
filterGroup->setEnabled(true); filterGroup->setEnabled(true);
filterFile->setText(tag.text()); filterFile->setText(strValue);
processFilterFile(tag.text()); processFilterFile(strValue);
}
}else{ }else{
activateFilter->setChecked(false); activateFilter->setChecked(false);
filterGroup->setEnabled(false); filterGroup->setEnabled(false);
} }
// Load proxy settings // End IPFilter options
tag = root.firstChildElement("proxy_settings"); settings.endGroup();
if(!tag.isNull()){ // Proxy options
settings.beginGroup("Proxy");
if(settings.value("Enabled", false).toBool()){
strValue = settings.value("IP", QString()).toString();
if(strValue.isEmpty()){
enableProxy_checkBox->setChecked(false);
groupProxy->setEnabled(false);
}else{
enableProxy_checkBox->setChecked(true); enableProxy_checkBox->setChecked(true);
groupProxy->setEnabled(true); groupProxy->setEnabled(true);
proxy_ip->setText(tag.firstChildElement("proxy_ip").text()); proxy_ip->setText(strValue);
proxy_port->setValue(tag.firstChildElement("proxy_port").text().toInt()); proxy_port->setValue(settings.value("Port", 8080).toInt());
tag2 = tag.firstChildElement("proxy_username"); settings.beginGroup("Authentication");
if(!tag2.isNull()){ if(settings.value("Enabled", false).toBool()){
enableProxyAuth_checkBox->setChecked(true); enableProxyAuth_checkBox->setChecked(true);
groupProxyAuth->setEnabled(true); groupProxyAuth->setEnabled(true);
proxy_username->setText(tag2.text()); proxy_username->setText(settings.value("Username", QString()).toString());
proxy_password->setText(tag.firstChildElement("proxy_password").text()); proxy_password->setText(settings.value("Password", QString()).toString());
}else{ }else{
enableProxyAuth_checkBox->setChecked(false); enableProxyAuth_checkBox->setChecked(false);
groupProxyAuth->setEnabled(false); groupProxyAuth->setEnabled(false);
} }
settings.endGroup();
}
}else{ }else{
enableProxyAuth_checkBox->setChecked(false);
enableProxy_checkBox->setChecked(false); enableProxy_checkBox->setChecked(false);
groupProxyAuth->setEnabled(false);
groupProxy->setEnabled(false); groupProxy->setEnabled(false);
} }
// Locale Settings // End Proxy options
tag = root.firstChildElement("locale"); settings.endGroup();
if(!tag.isNull()){ // Misc options
setLocale(tag.text()); settings.beginGroup("Misc");
} settings.beginGroup("TorrentAdditionDialog");
// Gui Settings if(settings.value("Enabled", true).toBool()){
tag = root.firstChildElement("gui_settings"); checkAdditionDialog->setChecked(true);
if(!tag.isNull()){ groupSavePath->setEnabled(false);
if(tag.firstChildElement("goToSysTrayOnMinimizing").text() == "false"){
check_goToSysTray->setChecked(false);
}else{
check_goToSysTray->setChecked(true);
}
if(tag.firstChildElement("clearFinishedOnExit").text() == "false"){
clearFinished_checkBox->setChecked(false);
}else{
clearFinished_checkBox->setChecked(true);
}
if(tag.firstChildElement("confirmOnExit").text() == "false"){
confirmExit_checkBox->setChecked(false);
}else{ }else{
confirmExit_checkBox->setChecked(true); checkAdditionDialog->setChecked(false);
} groupSavePath->setEnabled(true);
preview_program->setText(tag.firstChildElement("previewing").text()); txt_savePath->setText(settings.value("SavePath", QString()).toString());
QString OSDValue = tag.firstChildElement("displayOSD").text(); }
if(OSDValue == "false"){ settings.endGroup();
settings.beginGroup("Behaviour");
confirmExit_checkBox->setChecked(settings.value("ConfirmOnExit", true).toBool());
clearFinished_checkBox->setChecked(settings.value("ClearFinishedDownloads", true).toBool());
check_goToSysTray->setChecked(settings.value("GoToSystray", true).toBool());
settings.endGroup();
preview_program->setText(settings.value("PreviewProgram", QString()).toString());
// End Misc options
settings.endGroup();
value = settings.value("OSDEnabled", 1).toInt();
if(value == 0){
neverOSD->setChecked(true); neverOSD->setChecked(true);
}else{ }else{
if(OSDValue == "partial"){ if(value == 2){
someOSD->setChecked(true); someOSD->setChecked(true);
}else{ }else{
alwaysOSD->setChecked(true); alwaysOSD->setChecked(true);
} }
} }
} settings.endGroup();
// Disable apply Button // Disable apply Button
applyButton->setEnabled(false); applyButton->setEnabled(false);
return true;
} }
// return min & max ports // return min & max ports
// [min, max] // [min, max]
std::pair<unsigned short, unsigned short> options_imp::getPorts() const{ std::pair<unsigned short, unsigned short> options_imp::getPorts() const{
return std::make_pair(this->spin_port_min->value(), this->spin_port_min->value()); return std::make_pair(this->spin_port_min->value(), this->spin_port_max->value());
} }
int options_imp::getDHTPort() const{ int options_imp::getDHTPort() const{

4
src/options_imp.h

@ -42,8 +42,8 @@ class options_imp : public QDialog, private Ui::Dialog{
options_imp(QWidget *parent=0); options_imp(QWidget *parent=0);
// Methods // Methods
bool saveOptions(); void saveOptions();
bool loadOptions(); void loadOptions();
// Main options // Main options
std::pair<unsigned short, unsigned short> getPorts() const; std::pair<unsigned short, unsigned short> getPorts() const;
QPair<int,int> getLimits() const; QPair<int,int> getLimits() const;

Loading…
Cancel
Save