From 6d65ef067db86550e22c6c830982e25f60b133af Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Sat, 10 Mar 2007 18:14:53 +0000 Subject: [PATCH] Create Options object only when necessary (to save memory) --- Changelog | 1 + TODO | 5 +-- src/GUI.cpp | 81 +++++++++++++++++++++++++++++---------------- src/GUI.h | 7 ++-- src/main.cpp | 6 +--- src/options_imp.cpp | 19 ++++++++--- src/options_imp.h | 7 ++-- 7 files changed, 78 insertions(+), 48 deletions(-) diff --git a/Changelog b/Changelog index 5cf6147c1..83b8f0523 100644 --- a/Changelog +++ b/Changelog @@ -21,6 +21,7 @@ - BUGFIX: Fixed directory scanning (stop trying to download the same files several times) - BUGFIX: Fixed bad loading of scan dir in option (widgets still disabled) - BUGFIX: Threads are now stopped cleanly before their destruction + - BUGFIX: Create Options object only when necessary (to save memory) - I18N: Better internationalization thanks to dynamic text support - COSMETIC: Replaced OSD messages by Qt4.2 systray messages diff --git a/TODO b/TODO index 55da82b89..cb066c2c9 100644 --- a/TODO +++ b/TODO @@ -42,7 +42,4 @@ // In v0.9.0 - Update translations (FR, SV, NB, PL done) - Splitting torrent part from GUI (bug squashing + cleanup) -- Create options object only when necessary to save up some memory -- Wait for libtorrent v0.12 official release -Info: current TOP output: - 25461 chris 15 0 106m 23m 14m S 0.7 2.4 0:01.60 qbittorrent +- Wait for libtorrent v0.12 official release \ No newline at end of file diff --git a/src/GUI.cpp b/src/GUI.cpp index e702ec3f0..66b45cb0f 100644 --- a/src/GUI.cpp +++ b/src/GUI.cpp @@ -133,9 +133,9 @@ GUI::GUI(QWidget *parent, QStringList torrentCmdLine) : QMainWindow(parent){ connect(&BTSession, SIGNAL(aboutToDownloadFromUrl(const QString&)), this, SLOT(displayDownloadingUrlInfos(const QString&))); // creating options options = new options_imp(this); - connect(options, SIGNAL(status_changed(const QString&)), this, SLOT(OptionsSaved(const QString&))); + connect(options, SIGNAL(status_changed(const QString&, bool)), this, SLOT(OptionsSaved(const QString&, bool))); // Configure BT session according to options - configureSession(); + configureSession(true); // Resume unfinished torrents BTSession.resumeUnfinishedTorrents(); // Add torrent given on command line @@ -255,6 +255,7 @@ GUI::GUI(QWidget *parent, QStringList torrentCmdLine) : QMainWindow(parent){ setAcceptDrops(true); // Set info Bar infos setInfoBar(tr("qBittorrent %1 started.", "e.g: qBittorrent v0.x started.").arg(QString(VERSION))); + show(); qDebug("GUI Built"); } @@ -264,7 +265,6 @@ GUI::~GUI(){ searchProcess->kill(); searchProcess->waitForFinished(); delete searchProcess; - delete options; delete checkConnect; delete refresher; delete myTrayIcon; @@ -375,6 +375,8 @@ void GUI::displayDLListMenu(const QPoint& pos){ QModelIndex index; // Enable/disable pause/start action given the DL state QModelIndexList selectedIndexes = downloadList->selectionModel()->selectedIndexes(); + QSettings settings("qBittorrent", "qBittorrent"); + QString previewProgram = settings.value("Options/Misc/PreviewProgram", QString()).toString(); foreach(index, selectedIndexes){ if(index.column() == NAME){ // Get the file name @@ -389,7 +391,7 @@ void GUI::displayDLListMenu(const QPoint& pos){ myDLLlistMenu.addAction(actionDelete); myDLLlistMenu.addAction(actionDelete_Permanently); myDLLlistMenu.addAction(actionTorrent_Properties); - if(!options->getPreviewProgram().isEmpty() && BTSession.isFilePreviewPossible(fileHash) && selectedIndexes.size()<=DLListModel->columnCount()){ + if(!previewProgram.isEmpty() && BTSession.isFilePreviewPossible(fileHash) && selectedIndexes.size()<=DLListModel->columnCount()){ myDLLlistMenu.addAction(actionPreview_file); } break; @@ -427,7 +429,9 @@ void GUI::previewFile(const QString& filePath){ // Launch program preview QStringList params; params << tmpPath; - previewProcess->start(options->getPreviewProgram(), params, QIODevice::ReadOnly); + QSettings settings("qBittorrent", "qBittorrent"); + QString previewProgram = settings.value("Options/Misc/PreviewProgram", QString()).toString(); + previewProcess->start(previewProgram, params, QIODevice::ReadOnly); }else{ QMessageBox::critical(0, tr("Preview process already running"), tr("There is already another preview process running.\nPlease close the other one first.")); } @@ -806,12 +810,14 @@ void GUI::showAbout(){ // Called when we close the program void GUI::closeEvent(QCloseEvent *e){ - if(options->getGoToSysTrayOnExitingWindow() && !this->isHidden()){ + QSettings settings("qBittorrent", "qBittorrent"); + bool goToSystrayOnExit = settings.value("Options/Misc/GoToSystrayOnExit", false).toBool(); + if(goToSystrayOnExit && !this->isHidden()){ hide(); e->ignore(); return; } - if(options->getConfirmOnExit()){ + if(settings.value("Options/Misc/ConfirmOnExit", true).toBool()){ if(QMessageBox::question(this, tr("Are you sure you want to quit?")+" -- "+tr("qBittorrent"), tr("Are you sure you want to quit qBittorrent?"), @@ -822,7 +828,7 @@ void GUI::closeEvent(QCloseEvent *e){ } } // Clean finished torrents on exit if asked for - if(options->getClearFinishedOnExit()){ + if(settings.value("Options/Misc/ClearFinishedDownloads", true).toBool()){ torrent_handle h; // XXX: Probably move this to the bittorrent part QDir torrentBackup(misc::qBittorrentPath() + "BT_backup"); @@ -859,7 +865,9 @@ void GUI::showCreateWindow(){ // Called when we minimize the program void GUI::hideEvent(QHideEvent *e){ - if(options->getGoToSysTrayOnMinimizingWindow()){ + qDebug("* Receiced hideEvent()"); + QSettings settings("qBittorrent", "qBittorrent"); + if(settings.value("Options/Misc/GoToSystray", true).toBool()){ // Hide window hide(); } @@ -873,8 +881,10 @@ void GUI::dropEvent(QDropEvent *event){ QStringList files=event->mimeData()->text().split('\n'); // Add file to download list QString file; + QSettings settings("qBittorrent", "qBittorrent"); + bool useTorrentAdditionDialog = settings.value("Options/Misc/TorrentAdditionDialog/Enabled", true).toBool(); foreach(file, files){ - if(options->useAdditionDialog()){ + if(useTorrentAdditionDialog){ torrentAdditionDialog *dialog = new torrentAdditionDialog(this); connect(dialog, SIGNAL(torrentAddition(const QString&, bool, const QString&)), &BTSession, SLOT(addTorrent(const QString&, bool, const QString&))); connect(dialog, SIGNAL(setInfoBarGUI(const QString&, const QString&)), this, SLOT(setInfoBar(const QString&, const QString&))); @@ -909,8 +919,10 @@ void GUI::askForTorrents(){ tr("Open Torrent Files"), settings.value("MainWindowLastDir", QDir::homePath()).toString(), tr("Torrent Files")+" (*.torrent)"); if(!pathsList.empty()){ + QSettings settings("qBittorrent", "qBittorrent"); + bool useTorrentAdditionDialog = settings.value("Options/Misc/TorrentAdditionDialog/Enabled", true).toBool(); for(int i=0; iuseAdditionDialog()){ + if(useTorrentAdditionDialog){ torrentAdditionDialog *dialog = new torrentAdditionDialog(this); connect(dialog, SIGNAL(torrentAddition(const QString&, bool, const QString&)), &BTSession, SLOT(addTorrent(const QString&, bool, const QString&))); connect(dialog, SIGNAL(setInfoBarGUI(const QString&, const QString&)), this, SLOT(setInfoBar(const QString&, const QString&))); @@ -1060,7 +1072,8 @@ QString GUI::getSavePath(QString hash){ qDebug("Save path: %s", line.data()); savePath = QString::fromUtf8(line.data()); }else{ - savePath = options->getSavePath(); + QSettings settings("qBittorrent", "qBittorrent"); + savePath = settings.value("Options/Main/ScanDir", QString()).toString(); } // Checking if savePath Dir exists // create it if it is not @@ -1081,12 +1094,14 @@ QString GUI::getSavePath(QString hash){ // the parameter type. void GUI::processParams(const QStringList& params){ QString param; + QSettings settings("qBittorrent", "qBittorrent"); + bool useTorrentAdditionDialog = settings.value("Options/Misc/TorrentAdditionDialog/Enabled", true).toBool(); foreach(param, params){ param = param.trimmed(); if(param.startsWith("http://", Qt::CaseInsensitive) || param.startsWith("ftp://", Qt::CaseInsensitive) || param.startsWith("https://", Qt::CaseInsensitive)){ BTSession.downloadFromUrl(param); }else{ - if(options->useAdditionDialog()){ + if(useTorrentAdditionDialog){ torrentAdditionDialog *dialog = new torrentAdditionDialog(this); connect(dialog, SIGNAL(torrentAddition(const QString&, bool, const QString&)), &BTSession, SLOT(addTorrent(const QString&, bool, const QString&))); connect(dialog, SIGNAL(setInfoBarGUI(const QString&, const QString&)), this, SLOT(setInfoBar(const QString&, const QString&))); @@ -1100,8 +1115,10 @@ void GUI::processParams(const QStringList& params){ void GUI::processScannedFiles(const QStringList& params){ QString param; + QSettings settings("qBittorrent", "qBittorrent"); + bool useTorrentAdditionDialog = settings.value("Options/Misc/TorrentAdditionDialog/Enabled", true).toBool(); foreach(param, params){ - if(options->useAdditionDialog()){ + if(useTorrentAdditionDialog){ torrentAdditionDialog *dialog = new torrentAdditionDialog(this); connect(dialog, SIGNAL(torrentAddition(const QString&, bool, const QString&)), &BTSession, SLOT(addTorrent(const QString&, bool, const QString&))); connect(dialog, SIGNAL(setInfoBarGUI(const QString&, const QString&)), this, SLOT(setInfoBar(const QString&, const QString&))); @@ -1113,7 +1130,9 @@ void GUI::processScannedFiles(const QStringList& params){ } void GUI::processDownloadedFiles(const QString& path, const QString& url){ - if(options->useAdditionDialog()){ + QSettings settings("qBittorrent", "qBittorrent"); + bool useTorrentAdditionDialog = settings.value("Options/Misc/TorrentAdditionDialog/Enabled", true).toBool(); + if(useTorrentAdditionDialog){ torrentAdditionDialog *dialog = new torrentAdditionDialog(this); connect(dialog, SIGNAL(torrentAddition(const QString&, bool, const QString&)), &BTSession, SLOT(addTorrent(const QString&, bool, const QString&))); connect(dialog, SIGNAL(setInfoBarGUI(const QString&, const QString&)), this, SLOT(setInfoBar(const QString&, const QString&))); @@ -1135,7 +1154,7 @@ void GUI::showProperties(const QModelIndex &index){ } // Set BT session configuration -void GUI::configureSession(){ +void GUI::configureSession(bool deleteOptions){ qDebug("Configuring session"); QPair limits; unsigned short old_listenPort, new_listenPort; @@ -1206,6 +1225,9 @@ void GUI::configureSession(){ }else{ BTSession.enableDirectoryScanning(options->getScanDir()); } + if(deleteOptions){ + delete options; + } qDebug("Session configured"); } @@ -1319,16 +1341,20 @@ void GUI::propertiesSelection(){ // called when a torrent has finished void GUI::finishedTorrent(torrent_handle& h){ + QSettings settings("qBittorrent", "qBittorrent"); QString fileName = QString(h.name().c_str()); setInfoBar(tr("%1 has finished downloading.", "e.g: xxx.avi has finished downloading.").arg(fileName)); - if(options->getUseOSDAlways() || (options->getUseOSDWhenHiddenOnly() && (isMinimized() || isHidden()))) { + bool useOSD = (settings.value("Options/Misc/OSDEnabled", 1).toInt() > 0); + if(useOSD && (isMinimized() || isHidden())) { myTrayIcon->showMessage(tr("Download finished"), tr("%1 has finished downloading.", "e.g: xxx.avi has finished downloading.").arg(fileName), QSystemTrayIcon::Information, TIME_TRAY_BALLOON); } } // Notification when disk is full void GUI::fullDiskError(torrent_handle& h){ - if(options->getUseOSDAlways() || (options->getUseOSDWhenHiddenOnly() && (isMinimized() || isHidden()))) { + QSettings settings("qBittorrent", "qBittorrent"); + bool useOSD = (settings.value("Options/Misc/OSDEnabled", 1).toInt() > 0); + if(useOSD && (isMinimized() || isHidden())) { myTrayIcon->showMessage(tr("I/O Error", "i.e: Input/Output Error"), tr("An error occured when trying to read or write %1. The disk is probably full, download has been paused", "e.g: An error occured when trying to read or write xxx.avi. The disk is probably full, download has been paused").arg(QString(h.name().c_str())), QSystemTrayIcon::Critical, TIME_TRAY_BALLOON); } // Download will be paused by libtorrent. Updating GUI information accordingly @@ -1703,7 +1729,9 @@ void GUI::on_update_nova_button_clicked(){ // Search can be finished for 3 reasons : // Error | Stopped by user | Finished normally void GUI::searchFinished(int exitcode,QProcess::ExitStatus){ - if(options->getUseOSDAlways() || (options->getUseOSDWhenHiddenOnly() && (isMinimized() || isHidden()))) { + QSettings settings("qBittorrent", "qBittorrent"); + bool useOSD = (settings.value("Options/Misc/OSDEnabled", 1).toInt() > 0); + if(useOSD && (isMinimized() || isHidden())) { myTrayIcon->showMessage(tr("Search Engine"), tr("Search has finished"), QSystemTrayIcon::Information, TIME_TRAY_BALLOON); } if(exitcode){ @@ -1829,24 +1857,19 @@ void GUI::displayDownloadingUrlInfos(const QString& url){ * * *****************************************************/ -// Set locale in options, this is required -// because main() can set the locale and it -// can't access options object directly. -void GUI::setLocale(QString locale){ - options->setLocale(locale); -} - // Display Program Options -void GUI::showOptions() const{ +void GUI::showOptions(){ + options = new options_imp(this); + connect(options, SIGNAL(status_changed(const QString&, bool)), this, SLOT(OptionsSaved(const QString&, bool))); options->show(); } // Is executed each time options are saved -void GUI::OptionsSaved(const QString& info){ +void GUI::OptionsSaved(const QString& info, bool deleteOptions){ // Update info bar setInfoBar(info); // Update session - configureSession(); + configureSession(deleteOptions); } /***************************************************** diff --git a/src/GUI.h b/src/GUI.h index 7a873b81f..9081d289b 100644 --- a/src/GUI.h +++ b/src/GUI.h @@ -146,7 +146,7 @@ class GUI : public QMainWindow, private Ui::MainWindow{ void deletePermanently(); void deleteSelection(); void checkConnectionStatus(); - void configureSession(); + void configureSession(bool deleteOptions); void processParams(const QStringList& params); void addUnauthenticatedTracker(QPair tracker); void processScannedFiles(const QStringList& params); @@ -169,13 +169,12 @@ class GUI : public QMainWindow, private Ui::MainWindow{ // Utils slots void setRowColor(int row, const QString& color, bool inDLList=true); // Options slots - void showOptions() const; - void OptionsSaved(const QString& info); + void showOptions(); + void OptionsSaved(const QString& info, bool deleteOptions); // HTTP slots void askForTorrentUrl(); public slots: - void setLocale(QString locale); void torrentAdded(const QString& path, torrent_handle& h, bool fastResume); void torrentDuplicate(const QString& path); void torrentCorrupted(const QString& path); diff --git a/src/main.cpp b/src/main.cpp index 27914926b..94c95bb34 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -86,10 +86,10 @@ int main(int argc, char *argv[]){ // Open options file to read locale QSettings settings("qBittorrent", "qBittorrent"); 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()); @@ -103,10 +103,6 @@ int main(int argc, char *argv[]){ // Remove first argument (program name) torrentCmdLine.removeFirst(); GUI window(0, torrentCmdLine); - // Set locale - window.setLocale(locale); - // Show main window - window.show(); splash->finish(&window); delete splash; return app.exec(); diff --git a/src/options_imp.cpp b/src/options_imp.cpp index 66a599144..1cb36d0a7 100644 --- a/src/options_imp.cpp +++ b/src/options_imp.cpp @@ -30,6 +30,7 @@ // Constructor options_imp::options_imp(QWidget *parent):QDialog(parent){ + qDebug("-> Constructing Options"); QString savePath; setupUi(this); // Setting icons @@ -157,6 +158,11 @@ options_imp::options_imp(QWidget *parent):QDialog(parent){ } } +// Main destructor +options_imp::~options_imp(){ + qDebug("-> destructing Options"); +} + void options_imp::saveOptions(){ QSettings settings("qBittorrent", "qBittorrent"); // Check if min port < max port @@ -235,8 +241,6 @@ void options_imp::saveOptions(){ } } settings.endGroup(); - // set infobar text - emit status_changed(tr("Options were saved successfully.")); // Disable apply Button applyButton->setEnabled(false); } @@ -516,8 +520,13 @@ void options_imp::on_okButton_clicked(){ if(applyButton->isEnabled()){ saveOptions(); applyButton->setEnabled(false); + // set infobar text + emit status_changed(tr("Options were saved successfully."), true); + this->hide(); + }else{ + setAttribute(Qt::WA_DeleteOnClose); + accept(); } - this->hide(); } bool options_imp::getClearFinishedOnExit() const{ @@ -526,10 +535,12 @@ bool options_imp::getClearFinishedOnExit() const{ void options_imp::on_applyButton_clicked(){ saveOptions(); + emit optionsApplied(tr("Options were saved successfully."), false); } void options_imp::on_cancelButton_clicked(){ - this->hide(); + setAttribute(Qt::WA_DeleteOnClose); + reject(); } void options_imp::disableDownload(int checkBoxValue){ diff --git a/src/options_imp.h b/src/options_imp.h index dc3fab77c..080ff2594 100644 --- a/src/options_imp.h +++ b/src/options_imp.h @@ -38,8 +38,9 @@ class options_imp : public QDialog, private Ui::Dialog{ QStringList locales; public: - // Contructor + // Contructor / Destructor options_imp(QWidget *parent=0); + ~options_imp(); // Methods void saveOptions(); @@ -106,7 +107,9 @@ class options_imp : public QDialog, private Ui::Dialog{ void setLocale(QString locale); signals: - void status_changed(const QString&) const; + void status_changed(const QString&, bool) const; + void optionsApplied(const QString&, bool); + void exitWithCancel(); }; #endif