qBittorrent/src/mainwindow.cpp

1432 lines
51 KiB
C++
Raw Normal View History

/*
* 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.
*
* 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
*/
#include <QtGlobal>
#if defined(Q_WS_X11) && defined(QT_DBUS_LIB)
#include <QDBusConnection>
#include "notifications.h"
#endif
#include <QFileDialog>
#include <QFileSystemWatcher>
#include <QMessageBox>
#include <QTimer>
#include <QDesktopServices>
#include <QStatusBar>
#include <QClipboard>
#include <QCloseEvent>
#include <QShortcut>
2010-10-08 21:47:34 +04:00
#include <QScrollBar>
2010-11-14 00:15:52 +03:00
#include "mainwindow.h"
#include "transferlistwidget.h"
#include "misc.h"
2010-10-23 00:13:22 +04:00
#include "torrentcreatordlg.h"
#include "downloadfromurldlg.h"
2012-05-15 20:57:31 +04:00
#include "addnewtorrentdialog.h"
#include "searchengine.h"
#include "rss_imp.h"
#include "qbtsession.h"
2007-07-23 16:46:36 +04:00
#include "about_imp.h"
#include "trackerlogin.h"
2007-07-23 16:46:36 +04:00
#include "options_imp.h"
#include "speedlimitdlg.h"
#include "preferences.h"
#include "trackerlist.h"
#include "peerlistwidget.h"
#include "torrentpersistentdata.h"
#include "transferlistfilterswidget.h"
2009-11-08 17:20:58 +03:00
#include "propertieswidget.h"
#include "statusbar.h"
2010-06-22 18:20:21 +04:00
#include "hidabletabwidget.h"
#include "qinisettings.h"
#include "torrentimportdlg.h"
2010-10-31 15:03:24 +03:00
#include "rsssettings.h"
#include "torrentmodel.h"
#include "executionlog.h"
#include "iconprovider.h"
2010-06-08 19:17:15 +04:00
#ifdef Q_WS_MAC
#include "qmacapplication.h"
2010-06-09 19:43:29 +04:00
void qt_mac_set_dock_menu(QMenu *menu);
2010-06-08 19:17:15 +04:00
#endif
#include "lineedit.h"
#include "sessionapplication.h"
#if defined(Q_WS_WIN) || defined(Q_WS_MAC)
#include "programupdater.h"
#endif
#include "powermanagement.h"
2007-03-04 20:13:29 +03:00
using namespace libtorrent;
#define TIME_TRAY_BALLOON 5000
#define PREVENT_SUSPEND_INTERVAL 60000
/*****************************************************
* *
* GUI *
* *
*****************************************************/
// Constructor
2012-06-10 21:49:50 +04:00
MainWindow::MainWindow(QWidget *parent, const QStringList& torrentCmdLine) : QMainWindow(parent), m_posInitialized(false), force_exit(false) {
setupUi(this);
Preferences pref;
ui_locked = pref.isUILocked();
2010-06-21 22:32:01 +04:00
setWindowTitle(tr("qBittorrent %1", "e.g: qBittorrent v0.x").arg(QString::fromUtf8(VERSION)));
displaySpeedInTitle = pref.speedInTitleBar();
// Clean exit on log out
connect(static_cast<SessionApplication*>(qApp), SIGNAL(sessionIsShuttingDown()), this, SLOT(deleteBTSession()), Qt::DirectConnection);
// Setting icons
#if defined(Q_WS_X11)
if (Preferences().useSystemIconTheme())
2012-08-25 17:34:00 +04:00
setWindowIcon(QIcon::fromTheme("qbittorrent", QIcon(QString::fromUtf8(":/Icons/skin/qbittorrent32.png"))));
#else
setWindowIcon(QIcon(QString::fromUtf8(":/Icons/skin/qbittorrent32.png")));
#endif
2013-03-11 19:33:57 +04:00
actionOpen->setIcon(IconProvider::instance()->getIcon("list-add"));
actionDownload_from_URL->setIcon(IconProvider::instance()->getIcon("insert-link"));
actionSet_upload_limit->setIcon(QIcon(QString::fromUtf8(":/Icons/skin/seeding.png")));
2009-11-24 21:15:08 +03:00
actionSet_download_limit->setIcon(QIcon(QString::fromUtf8(":/Icons/skin/download.png")));
actionSet_global_upload_limit->setIcon(QIcon(QString::fromUtf8(":/Icons/skin/seeding.png")));
actionSet_global_download_limit->setIcon(QIcon(QString::fromUtf8(":/Icons/skin/download.png")));
actionCreate_torrent->setIcon(IconProvider::instance()->getIcon("document-edit"));
actionAbout->setIcon(IconProvider::instance()->getIcon("help-about"));
actionBugReport->setIcon(IconProvider::instance()->getIcon("tools-report-bug"));
actionDecreasePriority->setIcon(IconProvider::instance()->getIcon("go-down"));
actionDelete->setIcon(IconProvider::instance()->getIcon("list-remove"));
actionDocumentation->setIcon(IconProvider::instance()->getIcon("help-contents"));
actionDonate_money->setIcon(IconProvider::instance()->getIcon("wallet-open"));
actionExit->setIcon(IconProvider::instance()->getIcon("application-exit"));
actionIncreasePriority->setIcon(IconProvider::instance()->getIcon("go-up"));
actionLock_qBittorrent->setIcon(IconProvider::instance()->getIcon("object-locked"));
actionOptions->setIcon(IconProvider::instance()->getIcon("preferences-system"));
actionPause->setIcon(IconProvider::instance()->getIcon("media-playback-pause"));
actionPause_All->setIcon(IconProvider::instance()->getIcon("media-playback-pause"));
actionStart->setIcon(IconProvider::instance()->getIcon("media-playback-start"));
actionStart_All->setIcon(IconProvider::instance()->getIcon("media-playback-start"));
action_Import_Torrent->setIcon(IconProvider::instance()->getIcon("document-import"));
menuAuto_Shutdown_on_downloads_completion->setIcon(IconProvider::instance()->getIcon("application-exit"));
QMenu *startAllMenu = new QMenu(this);
startAllMenu->addAction(actionStart_All);
actionStart->setMenu(startAllMenu);
QMenu *pauseAllMenu = new QMenu(this);
pauseAllMenu->addAction(actionPause_All);
actionPause->setMenu(pauseAllMenu);
QMenu *lockMenu = new QMenu(this);
QAction *defineUiLockPasswdAct = lockMenu->addAction(tr("Set the password..."));
connect(defineUiLockPasswdAct, SIGNAL(triggered()), this, SLOT(defineUILockPassword()));
actionLock_qBittorrent->setMenu(lockMenu);
// Creating Bittorrent session
2010-12-04 17:09:41 +03:00
connect(QBtSession::instance(), SIGNAL(fullDiskError(QTorrentHandle, QString)), this, SLOT(fullDiskError(QTorrentHandle, QString)));
connect(QBtSession::instance(), SIGNAL(finishedTorrent(QTorrentHandle)), this, SLOT(finishedTorrent(QTorrentHandle)));
connect(QBtSession::instance(), SIGNAL(trackerAuthenticationRequired(QTorrentHandle)), this, SLOT(trackerAuthenticationRequired(QTorrentHandle)));
connect(QBtSession::instance(), SIGNAL(newDownloadedTorrent(QString, QString)), this, SLOT(processDownloadedFiles(QString, QString)));
connect(QBtSession::instance(), SIGNAL(newMagnetLink(QString)), this, SLOT(processNewMagnetLink(QString)));
2010-12-04 17:09:41 +03:00
connect(QBtSession::instance(), SIGNAL(downloadFromUrlFailure(QString, QString)), this, SLOT(handleDownloadFromUrlFailure(QString, QString)));
connect(QBtSession::instance(), SIGNAL(alternativeSpeedsModeChanged(bool)), this, SLOT(updateAltSpeedsBtn(bool)));
connect(QBtSession::instance(), SIGNAL(recursiveTorrentDownloadPossible(QTorrentHandle)), this, SLOT(askRecursiveTorrentDownloadConfirmation(QTorrentHandle)));
2010-06-08 19:17:15 +04:00
#ifdef Q_WS_MAC
connect(static_cast<QMacApplication*>(qApp), SIGNAL(newFileOpenMacEvent(QString)), this, SLOT(processParams(QString)));
2010-06-08 19:17:15 +04:00
#endif
qDebug("create tabWidget");
2010-06-22 18:18:43 +04:00
tabs = new HidableTabWidget();
connect(tabs, SIGNAL(currentChanged(int)), this, SLOT(tab_changed(int)));
vSplitter = new QSplitter(Qt::Horizontal);
//vSplitter->setChildrenCollapsible(false);
hSplitter = new QSplitter(Qt::Vertical);
hSplitter->setChildrenCollapsible(false);
2011-02-06 00:27:13 +03:00
hSplitter->setContentsMargins(0, 4, 0, 0);
// Transfer List tab
2010-12-04 17:09:41 +03:00
transferList = new TransferListWidget(hSplitter, this, QBtSession::instance());
2010-11-14 00:15:52 +03:00
properties = new PropertiesWidget(hSplitter, this, transferList);
transferListFilters = new TransferListFiltersWidget(vSplitter, transferList);
2009-11-08 17:20:58 +03:00
hSplitter->addWidget(transferList);
hSplitter->addWidget(properties);
vSplitter->addWidget(transferListFilters);
vSplitter->addWidget(hSplitter);
vSplitter->setCollapsible(0, true);
vSplitter->setCollapsible(1, false);
tabs->addTab(vSplitter, IconProvider::instance()->getIcon("folder-remote"), tr("Transfers"));
2009-03-28 01:25:34 +03:00
vboxLayout->addWidget(tabs);
// Name filter
search_filter = new LineEdit();
connect(search_filter, SIGNAL(textChanged(QString)), transferList, SLOT(applyNameFilter(QString)));
2011-01-10 20:53:33 +03:00
QAction *searchFilterAct = toolBar->insertWidget(actionLock_qBittorrent, search_filter);
search_filter->setFixedWidth(200);
2010-12-14 22:54:34 +03:00
QWidget *spacer = new QWidget(this);
spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
toolBar->insertWidget(searchFilterAct, spacer);
2010-12-14 22:54:34 +03:00
2010-12-20 21:22:06 +03:00
prioSeparator = toolBar->insertSeparator(actionDecreasePriority);
prioSeparatorMenu = menu_Edit->insertSeparator(actionDecreasePriority);
// Transfer list slots
connect(actionStart, SIGNAL(triggered()), transferList, SLOT(startSelectedTorrents()));
2010-12-04 17:09:41 +03:00
connect(actionStart_All, SIGNAL(triggered()), QBtSession::instance(), SLOT(resumeAllTorrents()));
connect(actionPause, SIGNAL(triggered()), transferList, SLOT(pauseSelectedTorrents()));
2010-12-04 17:09:41 +03:00
connect(actionPause_All, SIGNAL(triggered()), QBtSession::instance(), SLOT(pauseAllTorrents()));
connect(actionDelete, SIGNAL(triggered()), transferList, SLOT(deleteSelectedTorrents()));
connect(actionIncreasePriority, SIGNAL(triggered()), transferList, SLOT(increasePrioSelectedTorrents()));
connect(actionDecreasePriority, SIGNAL(triggered()), transferList, SLOT(decreasePrioSelectedTorrents()));
connect(actionToggleVisibility, SIGNAL(triggered()), this, SLOT(toggleVisibility()));
connect(actionMinimize, SIGNAL(triggered()), SLOT(minimizeWindow()));
m_pwr = new PowerManagement(this);
preventTimer = new QTimer(this);
connect(preventTimer, SIGNAL(timeout()), SLOT(checkForActiveTorrents()));
// Configure BT session according to options
loadPreferences(false);
// Start connection checking timer
guiUpdater = new QTimer(this);
connect(guiUpdater, SIGNAL(timeout()), this, SLOT(updateGUI()));
guiUpdater->start(2000);
// Accept drag 'n drops
setAcceptDrops(true);
createKeyboardShortcuts();
// Create status bar
2010-11-14 00:15:52 +03:00
status_bar = new StatusBar(QMainWindow::statusBar());
connect(status_bar->connectionStatusButton(), SIGNAL(clicked()), SLOT(showConnectionSettings()));
connect(actionUse_alternative_speed_limits, SIGNAL(triggered()), status_bar, SLOT(toggleAlternativeSpeeds()));
2010-06-09 11:37:05 +04:00
#ifdef Q_WS_MAC
setUnifiedTitleAndToolBarOnMac(true);
#endif
2010-06-21 22:32:01 +04:00
// View settings
actionTop_tool_bar->setChecked(pref.isToolbarDisplayed());
actionSpeed_in_title_bar->setChecked(pref.speedInTitleBar());
actionRSS_Reader->setChecked(RssSettings().isRSSEnabled());
actionSearch_engine->setChecked(pref.isSearchEnabled());
actionExecution_Logs->setChecked(pref.isExecutionLogEnabled());
2010-06-22 18:18:43 +04:00
displaySearchTab(actionSearch_engine->isChecked());
displayRSSTab(actionRSS_Reader->isChecked());
on_actionExecution_Logs_triggered(actionExecution_Logs->isChecked());
// Auto shutdown actions
QActionGroup * autoShutdownGroup = new QActionGroup(this);
autoShutdownGroup->setExclusive(true);
autoShutdownGroup->addAction(actionAutoShutdown_Disabled);
autoShutdownGroup->addAction(actionAutoExit_qBittorrent);
autoShutdownGroup->addAction(actionAutoShutdown_system);
autoShutdownGroup->addAction(actionAutoSuspend_system);
2011-03-07 23:25:20 +03:00
#if !defined(Q_WS_X11) || defined(QT_DBUS_LIB)
actionAutoShutdown_system->setChecked(pref.shutdownWhenDownloadsComplete());
actionAutoSuspend_system->setChecked(pref.suspendWhenDownloadsComplete());
2011-03-07 23:25:20 +03:00
#else
actionAutoShutdown_system->setDisabled(true);
actionAutoSuspend_system->setDisabled(true);
2011-03-07 23:22:35 +03:00
#endif
actionAutoExit_qBittorrent->setChecked(pref.shutdownqBTWhenDownloadsComplete());
2012-02-20 21:30:53 +04:00
if (!autoShutdownGroup->checkedAction())
actionAutoShutdown_Disabled->setChecked(true);
2010-06-21 22:32:01 +04:00
// Load Window state and sizes
readSettings();
2012-02-20 21:30:53 +04:00
if (!ui_locked) {
if (pref.startMinimized() && systrayIcon) {
show();
minimizeWindow();
// XXX: Using showMinimized() makes it impossible to restore
// the window if "Minimize to systray" is enabled.
//showMinimized();
} else {
2011-01-17 20:29:25 +03:00
show();
activateWindow();
raise();
}
}
2011-02-06 00:27:13 +03:00
properties->readSettings();
// Start watching the executable for updates
executable_watcher = new QFileSystemWatcher();
connect(executable_watcher, SIGNAL(fileChanged(QString)), this, SLOT(notifyOfUpdate(QString)));
executable_watcher->addPath(qApp->applicationFilePath());
// Resume unfinished torrents
2010-12-04 17:09:41 +03:00
QBtSession::instance()->startUpTorrents();
// Add torrent given on command line
processParams(torrentCmdLine);
// Populate the transfer list
transferList->getSourceModel()->populate();
2010-12-25 12:23:04 +03:00
// Update the number of torrents (tab)
updateNbTorrents();
connect(transferList->getSourceModel(), SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(updateNbTorrents()));
connect(transferList->getSourceModel(), SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(updateNbTorrents()));
qDebug("GUI Built");
2010-05-31 01:45:55 +04:00
#ifdef Q_WS_WIN
2012-02-20 21:30:53 +04:00
if (!pref.neverCheckFileAssoc() && (!Preferences::isTorrentFileAssocSet() || !Preferences::isMagnetLinkAssocSet())) {
if (QMessageBox::question(0, tr("Torrent file association"),
2010-06-02 17:08:26 +04:00
tr("qBittorrent is not the default application to open torrent files or Magnet links.\nDo you want to associate qBittorrent to torrent files and Magnet links?"),
QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes) == QMessageBox::Yes) {
Preferences::setTorrentFileAssoc(true);
Preferences::setMagnetLinkAssoc(true);
2010-06-02 17:08:26 +04:00
} else {
pref.setNeverCheckFileAssoc();
2010-06-02 17:08:26 +04:00
}
2010-05-31 01:45:55 +04:00
}
#endif
#ifdef Q_WS_MAC
qt_mac_set_dock_menu(getTrayIconMenu());
#endif
#if defined(Q_WS_WIN) || defined(Q_WS_MAC)
// Check for update
2012-02-20 21:30:53 +04:00
if (pref.isUpdateCheckEnabled()) {
ProgramUpdater *updater = new ProgramUpdater(this);
connect(updater, SIGNAL(updateCheckFinished(bool, QString)), SLOT(handleUpdateCheckFinished(bool, QString)));
updater->checkForUpdates();
}
#endif
// Make sure the Window is visible if we don't have a tray icon
if (!systrayIcon && isHidden()) {
show();
activateWindow();
raise();
}
}
2010-11-14 00:15:52 +03:00
void MainWindow::deleteBTSession() {
guiUpdater->stop();
status_bar->stopTimer();
2010-12-04 16:58:36 +03:00
QBtSession::drop();
m_pwr->setActivityState(false);
// Save window size, columns size
writeSettings();
}
// Destructor
2010-11-14 00:15:52 +03:00
MainWindow::~MainWindow() {
qDebug("GUI destruction");
hide();
#ifdef Q_WS_MAC
// Workaround to avoid bug http://bugreports.qt.nokia.com/browse/QTBUG-7305
2010-06-09 19:19:43 +04:00
setUnifiedTitleAndToolBarOnMac(false);
#endif
disconnect(tabs, SIGNAL(currentChanged(int)), this, SLOT(tab_changed(int)));
// Delete other GUI objects
2012-02-20 21:30:53 +04:00
if (executable_watcher)
delete executable_watcher;
delete status_bar;
delete search_filter;
delete transferList;
delete guiUpdater;
2012-02-20 21:30:53 +04:00
if (createTorrentDlg)
2010-03-03 17:42:57 +03:00
delete createTorrentDlg;
2012-02-20 21:30:53 +04:00
if (m_executionLog)
delete m_executionLog;
2012-02-20 21:30:53 +04:00
if (aboutDlg)
2010-03-03 17:42:57 +03:00
delete aboutDlg;
2012-02-20 21:30:53 +04:00
if (options)
delete options;
2012-02-20 21:30:53 +04:00
if (downloadFromURLDialog)
delete downloadFromURLDialog;
2012-02-20 21:30:53 +04:00
if (rssWidget)
delete rssWidget;
2012-02-20 21:30:53 +04:00
if (searchEngine)
2010-06-22 17:58:22 +04:00
delete searchEngine;
delete transferListFilters;
2009-11-08 17:20:58 +03:00
delete properties;
delete hSplitter;
delete vSplitter;
2012-02-20 21:30:53 +04:00
if (systrayCreator) {
delete systrayCreator;
}
2012-02-20 21:30:53 +04:00
if (systrayIcon) {
2009-11-18 21:45:06 +03:00
delete systrayIcon;
2010-06-09 19:45:59 +04:00
}
2012-02-20 21:30:53 +04:00
if (myTrayIconMenu) {
delete myTrayIconMenu;
}
delete tabs;
// Keyboard shortcuts
delete switchSearchShortcut;
delete switchSearchShortcut2;
delete switchTransferShortcut;
delete switchRSSShortcut;
IconProvider::drop();
2010-12-04 17:09:41 +03:00
// Delete QBtSession::instance() object
m_pwr->setActivityState(false);
2010-12-04 17:09:41 +03:00
qDebug("Deleting QBtSession::instance()");
QBtSession::drop();
qDebug("Exiting GUI destructor...");
}
2010-11-14 00:15:52 +03:00
void MainWindow::defineUILockPassword() {
QString old_pass_md5 = Preferences().getUILockPasswordMD5();
2012-02-20 21:30:53 +04:00
if (old_pass_md5.isNull()) old_pass_md5 = "";
bool ok = false;
2010-08-19 23:37:29 +04:00
QString new_clear_password = QInputDialog::getText(this, tr("UI lock password"), tr("Please type the UI lock password:"), QLineEdit::Password, old_pass_md5, &ok);
2012-02-20 21:30:53 +04:00
if (ok) {
new_clear_password = new_clear_password.trimmed();
2012-02-20 21:30:53 +04:00
if (new_clear_password.size() < 3) {
QMessageBox::warning(this, tr("Invalid password"), tr("The password should contain at least 3 characters"));
return;
}
2012-02-20 21:30:53 +04:00
if (new_clear_password != old_pass_md5) {
Preferences().setUILockPassword(new_clear_password);
}
QMessageBox::information(this, tr("Password update"), tr("The UI lock password has been successfully updated"));
}
}
2010-11-14 00:15:52 +03:00
void MainWindow::on_actionLock_qBittorrent_triggered() {
Preferences pref;
// Check if there is a password
2012-02-20 21:30:53 +04:00
if (pref.getUILockPasswordMD5().isEmpty()) {
// Ask for a password
bool ok = false;
2010-08-19 23:37:29 +04:00
QString clear_password = QInputDialog::getText(this, tr("UI lock password"), tr("Please type the UI lock password:"), QLineEdit::Password, "", &ok);
2012-02-20 21:30:53 +04:00
if (!ok) return;
pref.setUILockPassword(clear_password);
}
// Lock the interface
ui_locked = true;
pref.setUILocked(true);
2010-08-22 23:08:15 +04:00
myTrayIconMenu->setEnabled(false);
hide();
}
2010-11-14 00:15:52 +03:00
void MainWindow::displayRSSTab(bool enable) {
2012-02-20 21:30:53 +04:00
if (enable) {
// RSS tab
2012-02-20 21:30:53 +04:00
if (!rssWidget) {
rssWidget = new RSSImp(tabs);
int index_tab = tabs->addTab(rssWidget, tr("RSS"));
tabs->setTabIcon(index_tab, IconProvider::instance()->getIcon("application-rss+xml"));
}
} else {
2012-02-20 21:30:53 +04:00
if (rssWidget) {
delete rssWidget;
}
}
}
2010-11-14 00:15:52 +03:00
void MainWindow::displaySearchTab(bool enable) {
2012-02-20 21:30:53 +04:00
if (enable) {
2010-06-22 17:58:22 +04:00
// RSS tab
2012-02-20 21:30:53 +04:00
if (!searchEngine) {
2010-11-14 00:15:52 +03:00
searchEngine = new SearchEngine(this);
tabs->insertTab(1, searchEngine, IconProvider::instance()->getIcon("edit-find"), tr("Search"));
2010-06-22 17:58:22 +04:00
}
} else {
2012-02-20 21:30:53 +04:00
if (searchEngine) {
2010-06-22 17:58:22 +04:00
delete searchEngine;
}
}
}
void MainWindow::updateNbTorrents() {
tabs->setTabText(0, tr("Transfers (%1)").arg(transferList->getSourceModel()->rowCount()));
}
2010-11-14 00:15:52 +03:00
void MainWindow::on_actionWebsite_triggered() const {
QDesktopServices::openUrl(QUrl(QString::fromUtf8("http://www.qbittorrent.org")));
}
2010-11-14 00:15:52 +03:00
void MainWindow::on_actionDocumentation_triggered() const {
QDesktopServices::openUrl(QUrl(QString::fromUtf8("http://doc.qbittorrent.org")));
}
2010-11-14 00:15:52 +03:00
void MainWindow::on_actionBugReport_triggered() const {
QDesktopServices::openUrl(QUrl(QString::fromUtf8("http://bugs.qbittorrent.org")));
}
2010-11-14 00:15:52 +03:00
void MainWindow::tab_changed(int new_tab) {
Q_UNUSED(new_tab);
// We cannot rely on the index new_tab
// because the tab order is undetermined now
2012-02-20 21:30:53 +04:00
if (tabs->currentWidget() == vSplitter) {
qDebug("Changed tab to transfer list, refreshing the list");
properties->loadDynamicData();
return;
}
2012-02-20 21:30:53 +04:00
if (tabs->currentWidget() == searchEngine) {
qDebug("Changed tab to search engine, giving focus to search input");
searchEngine->giveFocusToSearchInput();
}
}
2010-11-14 00:15:52 +03:00
void MainWindow::writeSettings() {
QIniSettings settings;
settings.beginGroup(QString::fromUtf8("MainWindow"));
settings.setValue("geometry", saveGeometry());
// Splitter size
2010-12-04 12:37:13 +03:00
settings.setValue(QString::fromUtf8("vsplitterState"), vSplitter->saveState());
settings.endGroup();
properties->saveSettings();
}
2010-12-04 12:37:13 +03:00
void MainWindow::readSettings() {
QIniSettings settings;
2010-12-04 12:37:13 +03:00
settings.beginGroup(QString::fromUtf8("MainWindow"));
2012-02-20 21:30:53 +04:00
if (settings.contains("geometry")) {
if (restoreGeometry(settings.value("geometry").toByteArray()))
2011-03-07 20:41:32 +03:00
m_posInitialized = true;
}
2010-12-04 12:37:13 +03:00
const QByteArray splitterState = settings.value("vsplitterState").toByteArray();
2012-02-20 21:30:53 +04:00
if (splitterState.isEmpty()) {
2010-12-04 12:37:13 +03:00
// Default sizes
vSplitter->setSizes(QList<int>() << 120 << vSplitter->width()-120);
} else {
vSplitter->restoreState(splitterState);
}
settings.endGroup();
}
void MainWindow::balloonClicked() {
2012-02-20 21:30:53 +04:00
if (isHidden()) {
2010-12-04 12:37:13 +03:00
show();
2012-02-20 21:30:53 +04:00
if (isMinimized()) {
2010-12-04 12:37:13 +03:00
showNormal();
}
raise();
activateWindow();
}
}
// called when a torrent has finished
2010-11-24 23:31:14 +03:00
void MainWindow::finishedTorrent(const QTorrentHandle& h) const {
2012-02-20 21:30:53 +04:00
if (!TorrentPersistentData::isSeed(h.hash()))
showNotificationBaloon(tr("Download completion"), tr("%1 has finished downloading.", "e.g: xxx.avi has finished downloading.").arg(h.name()));
}
// Notification when disk is full
2010-11-24 23:31:14 +03:00
void MainWindow::fullDiskError(const QTorrentHandle& h, QString msg) const {
2012-02-20 21:30:53 +04:00
if (!h.is_valid()) return;
showNotificationBaloon(tr("I/O Error", "i.e: Input/Output Error"), tr("An I/O error occurred for torrent %1.\n Reason: %2", "e.g: An error occurred for torrent xxx.avi.\n Reason: disk is full.").arg(h.name()).arg(msg));
}
2010-11-14 00:15:52 +03:00
void MainWindow::createKeyboardShortcuts() {
actionCreate_torrent->setShortcut(QKeySequence(QString::fromUtf8("Ctrl+N")));
actionOpen->setShortcut(QKeySequence(QString::fromUtf8("Ctrl+O")));
actionExit->setShortcut(QKeySequence(QString::fromUtf8("Ctrl+Q")));
switchTransferShortcut = new QShortcut(QKeySequence(tr("Alt+1", "shortcut to switch to first tab")), this);
connect(switchTransferShortcut, SIGNAL(activated()), this, SLOT(displayTransferTab()));
switchSearchShortcut = new QShortcut(QKeySequence(tr("Alt+2", "shortcut to switch to third tab")), this);
connect(switchSearchShortcut, SIGNAL(activated()), this, SLOT(displaySearchTab()));
switchSearchShortcut2 = new QShortcut(QKeySequence(tr("Ctrl+F", "shortcut to switch to search tab")), this);
connect(switchSearchShortcut2, SIGNAL(activated()), this, SLOT(displaySearchTab()));
switchRSSShortcut = new QShortcut(QKeySequence(tr("Alt+3", "shortcut to switch to fourth tab")), this);
connect(switchRSSShortcut, SIGNAL(activated()), this, SLOT(displayRSSTab()));
actionDocumentation->setShortcut(QKeySequence("F1"));
actionOptions->setShortcut(QKeySequence(QString::fromUtf8("Alt+O")));
#ifdef Q_WS_MAC
actionDelete->setShortcut(QKeySequence("Ctrl+Backspace"));
#else
actionDelete->setShortcut(QKeySequence(QString::fromUtf8("Del")));
#endif
actionStart->setShortcut(QKeySequence(QString::fromUtf8("Ctrl+S")));
actionStart_All->setShortcut(QKeySequence(QString::fromUtf8("Ctrl+Shift+S")));
actionPause->setShortcut(QKeySequence(QString::fromUtf8("Ctrl+P")));
actionPause_All->setShortcut(QKeySequence(QString::fromUtf8("Ctrl+Shift+P")));
actionDecreasePriority->setShortcut(QKeySequence(QString::fromUtf8("Ctrl+-")));
actionIncreasePriority->setShortcut(QKeySequence(QString::fromUtf8("Ctrl++")));
#ifdef Q_WS_MAC
actionMinimize->setShortcut(QKeySequence(QString::fromUtf8("Ctrl+M")));
addAction(actionMinimize);
#endif
}
// Keyboard shortcuts slots
2010-11-14 00:15:52 +03:00
void MainWindow::displayTransferTab() const {
tabs->setCurrentWidget(transferList);
}
2010-11-14 00:15:52 +03:00
void MainWindow::displaySearchTab() const {
2012-02-20 21:30:53 +04:00
if (searchEngine)
tabs->setCurrentWidget(searchEngine);
}
2010-11-14 00:15:52 +03:00
void MainWindow::displayRSSTab() const {
2012-02-20 21:30:53 +04:00
if (rssWidget)
tabs->setCurrentWidget(rssWidget);
}
// End of keyboard shortcuts slots
2010-11-24 23:31:14 +03:00
void MainWindow::askRecursiveTorrentDownloadConfirmation(const QTorrentHandle &h) {
Preferences pref;
2012-02-20 21:30:53 +04:00
if (pref.recursiveDownloadDisabled()) return;
// Get Torrent name
QString torrent_name;
try {
torrent_name = h.name();
2012-02-20 21:56:07 +04:00
} catch(invalid_handle&) {
return;
}
QMessageBox confirmBox(QMessageBox::Question, tr("Recursive download confirmation"), tr("The torrent %1 contains torrent files, do you want to proceed with their download?").arg(torrent_name));
QPushButton *yes = confirmBox.addButton(tr("Yes"), QMessageBox::YesRole);
/*QPushButton *no = */confirmBox.addButton(tr("No"), QMessageBox::NoRole);
QPushButton *never = confirmBox.addButton(tr("Never"), QMessageBox::NoRole);
confirmBox.exec();
2012-02-20 21:30:53 +04:00
if (confirmBox.clickedButton() == 0) return;
if (confirmBox.clickedButton() == yes) {
2010-12-04 17:09:41 +03:00
QBtSession::instance()->recursiveTorrentDownload(h);
return;
}
2012-02-20 21:30:53 +04:00
if (confirmBox.clickedButton() == never) {
pref.disableRecursiveDownload();
}
}
2012-02-20 23:32:58 +04:00
void MainWindow::handleDownloadFromUrlFailure(QString url, QString reason) const {
// Display a message box
showNotificationBaloon(tr("Url download error"), tr("Couldn't download file at url: %1, reason: %2.").arg(url).arg(reason));
}
2010-11-14 00:15:52 +03:00
void MainWindow::on_actionSet_global_upload_limit_triggered() {
qDebug("actionSet_global_upload_limit_triggered");
bool ok;
2011-04-17 14:29:44 +04:00
#if LIBTORRENT_VERSION_MINOR > 15
int cur_limit = QBtSession::instance()->getSession()->settings().upload_rate_limit;
#else
int cur_limit = QBtSession::instance()->getSession()->upload_rate_limit();
#endif
const long new_limit = SpeedLimitDialog::askSpeedLimit(&ok, tr("Global Upload Speed Limit"), cur_limit);
2012-02-20 21:30:53 +04:00
if (ok) {
qDebug("Setting global upload rate limit to %.1fKb/s", new_limit/1024.);
2011-04-17 14:29:44 +04:00
QBtSession::instance()->setUploadRateLimit(new_limit);
2012-02-20 21:30:53 +04:00
if (new_limit <= 0)
Preferences().setGlobalUploadLimit(-1);
else
Preferences().setGlobalUploadLimit(new_limit/1024.);
}
}
2010-11-14 00:15:52 +03:00
void MainWindow::on_actionSet_global_download_limit_triggered() {
qDebug("actionSet_global_download_limit_triggered");
bool ok;
2011-04-17 14:29:44 +04:00
#if LIBTORRENT_VERSION_MINOR > 15
int cur_limit = QBtSession::instance()->getSession()->settings().download_rate_limit;
#else
int cur_limit = QBtSession::instance()->getSession()->download_rate_limit();
#endif
const long new_limit = SpeedLimitDialog::askSpeedLimit(&ok, tr("Global Download Speed Limit"), cur_limit);
2012-02-20 21:30:53 +04:00
if (ok) {
qDebug("Setting global download rate limit to %.1fKb/s", new_limit/1024.);
2011-04-17 14:29:44 +04:00
QBtSession::instance()->setDownloadRateLimit(new_limit);
2012-02-20 21:30:53 +04:00
if (new_limit <= 0)
Preferences().setGlobalDownloadLimit(-1);
else
Preferences().setGlobalDownloadLimit(new_limit/1024.);
}
}
// Necessary if we want to close the window
// in one time if "close to systray" is enabled
2010-11-14 00:15:52 +03:00
void MainWindow::on_actionExit_triggered() {
// UI locking enforcement.
if (isHidden() && ui_locked) {
// Ask for UI lock password
if (!unlockUI())
return;
}
force_exit = true;
close();
}
2010-11-14 00:15:52 +03:00
QWidget* MainWindow::getCurrentTabWidget() const {
2012-02-20 21:30:53 +04:00
if (isMinimized() || !isVisible())
return 0;
2012-02-20 21:30:53 +04:00
if (tabs->currentIndex() == 0)
return transferList;
return tabs->currentWidget();
}
2010-11-14 00:15:52 +03:00
void MainWindow::setTabText(int index, QString text) const {
tabs->setTabText(index, text);
}
2010-11-14 00:15:52 +03:00
bool MainWindow::unlockUI() {
bool ok = false;
QString clear_password = QInputDialog::getText(this, tr("UI lock password"), tr("Please type the UI lock password:"), QLineEdit::Password, "", &ok);
2012-02-20 21:30:53 +04:00
if (!ok) return false;
Preferences pref;
QString real_pass_md5 = pref.getUILockPasswordMD5();
QCryptographicHash md5(QCryptographicHash::Md5);
md5.addData(clear_password.toLocal8Bit());
QString password_md5 = md5.result().toHex();
2012-02-20 21:30:53 +04:00
if (real_pass_md5 == password_md5) {
ui_locked = false;
pref.setUILocked(false);
2010-08-22 23:08:15 +04:00
myTrayIconMenu->setEnabled(true);
return true;
}
QMessageBox::warning(this, tr("Invalid password"), tr("The password is invalid"));
return false;
}
2010-11-14 00:15:52 +03:00
void MainWindow::notifyOfUpdate(QString) {
// Show restart message
status_bar->showRestartRequired();
// Delete the executable watcher
delete executable_watcher;
executable_watcher = 0;
}
// Toggle Main window visibility
2010-11-14 00:15:52 +03:00
void MainWindow::toggleVisibility(QSystemTrayIcon::ActivationReason e) {
2012-02-20 21:30:53 +04:00
if (e == QSystemTrayIcon::Trigger || e == QSystemTrayIcon::DoubleClick) {
if (isHidden()) {
if (ui_locked) {
// Ask for UI lock password
2012-02-20 21:30:53 +04:00
if (!unlockUI())
return;
}
// Make sure the window is not minimized
2012-08-25 17:34:00 +04:00
setWindowState(windowState() & (~Qt::WindowMinimized | Qt::WindowActive));
// Then show it
show();
raise();
activateWindow();
}else{
hide();
}
}
}
// Display About Dialog
2010-11-14 00:15:52 +03:00
void MainWindow::on_actionAbout_triggered() {
//About dialog
2012-02-20 21:30:53 +04:00
if (aboutDlg) {
2010-03-03 17:42:57 +03:00
aboutDlg->setFocus();
} else {
aboutDlg = new about(this);
}
}
2010-11-14 00:15:52 +03:00
void MainWindow::showEvent(QShowEvent *e) {
qDebug("** Show Event **");
2012-02-20 21:30:53 +04:00
if (getCurrentTabWidget() == transferList) {
properties->loadDynamicData();
}
e->accept();
// Make sure the window is initially centered
2012-02-20 21:30:53 +04:00
if (!m_posInitialized) {
move(misc::screenCenter(this));
m_posInitialized = true;
}
}
// Called when we close the program
2010-11-14 00:15:52 +03:00
void MainWindow::closeEvent(QCloseEvent *e) {
Preferences pref;
const bool goToSystrayOnExit = pref.closeToTray();
2012-02-20 21:30:53 +04:00
if (!force_exit && systrayIcon && goToSystrayOnExit && !this->isHidden()) {
hide();
e->accept();
return;
}
2012-02-20 21:30:53 +04:00
if (pref.confirmOnExit() && QBtSession::instance()->hasActiveTorrents()) {
if (e->spontaneous() || force_exit) {
if (!isVisible())
2010-06-21 19:53:42 +04:00
show();
2010-06-22 21:51:59 +04:00
QMessageBox confirmBox(QMessageBox::Question, tr("Exiting qBittorrent"),
2010-06-21 20:10:09 +04:00
tr("Some files are currently transferring.\nAre you sure you want to quit qBittorrent?"),
QMessageBox::NoButton, this);
2010-06-21 19:53:42 +04:00
QPushButton *noBtn = confirmBox.addButton(tr("No"), QMessageBox::NoRole);
QPushButton *yesBtn = confirmBox.addButton(tr("Yes"), QMessageBox::YesRole);
QPushButton *alwaysBtn = confirmBox.addButton(tr("Always"), QMessageBox::YesRole);
confirmBox.setDefaultButton(yesBtn);
confirmBox.exec();
2012-02-20 21:30:53 +04:00
if (!confirmBox.clickedButton() || confirmBox.clickedButton() == noBtn) {
2010-06-21 19:53:42 +04:00
// Cancel exit
e->ignore();
force_exit = false;
return;
}
2012-02-20 21:30:53 +04:00
if (confirmBox.clickedButton() == alwaysBtn) {
2010-06-21 19:53:42 +04:00
// Remember choice
Preferences().setConfirmOnExit(false);
2010-06-21 19:53:42 +04:00
}
}
}
hide();
2012-02-20 21:30:53 +04:00
if (systrayIcon) {
// Hide tray icon
2009-11-18 21:45:06 +03:00
systrayIcon->hide();
}
// Save window size, columns size
writeSettings();
// Accept exit
e->accept();
qApp->exit();
}
// Display window to create a torrent
2010-11-14 00:15:52 +03:00
void MainWindow::on_actionCreate_torrent_triggered() {
2012-02-20 21:30:53 +04:00
if (createTorrentDlg) {
2010-03-03 17:42:57 +03:00
createTorrentDlg->setFocus();
} else {
2010-10-23 00:13:22 +04:00
createTorrentDlg = new TorrentCreatorDlg(this);
2010-03-03 17:42:57 +03:00
connect(createTorrentDlg, SIGNAL(torrent_to_seed(QString)), this, SLOT(addTorrent(QString)));
}
}
2010-11-14 00:15:52 +03:00
bool MainWindow::event(QEvent * e) {
2010-07-21 22:02:29 +04:00
switch(e->type()) {
case QEvent::WindowStateChange: {
qDebug("Window change event");
//Now check to see if the window is minimised
2012-02-20 21:30:53 +04:00
if (isMinimized()) {
qDebug("minimisation");
2012-02-20 21:30:53 +04:00
if (systrayIcon && Preferences().minimizeToTray()) {
qDebug("Has active window: %d", (int)(qApp->activeWindow() != 0));
// Check if there is a modal window
bool has_modal_window = false;
foreach (QWidget *widget, QApplication::allWidgets()) {
2012-02-20 21:30:53 +04:00
if (widget->isModal()) {
has_modal_window = true;
break;
2010-07-21 22:02:29 +04:00
}
}
// Iconify if there is no modal window
2012-02-20 21:30:53 +04:00
if (!has_modal_window) {
qDebug("Minimize to Tray enabled, hiding!");
e->ignore();
QTimer::singleShot(0, this, SLOT(hide()));
return true;
}
}
}
break;
}
2010-07-21 22:02:29 +04:00
#ifdef Q_WS_MAC
case QEvent::ToolBarChange: {
qDebug("MAC: Received a toolbar change event!");
bool ret = QMainWindow::event(e);
qDebug("MAC: new toolbar visibility is %d", !actionTop_tool_bar->isChecked());
actionTop_tool_bar->toggle();
Preferences().setToolbarDisplayed(actionTop_tool_bar->isChecked());
return ret;
}
2010-07-21 22:02:29 +04:00
#endif
default:
break;
}
return QMainWindow::event(e);
}
// Action executed when a file is dropped
2010-11-14 00:15:52 +03:00
void MainWindow::dropEvent(QDropEvent *event) {
event->acceptProposedAction();
QStringList files;
2012-02-20 21:30:53 +04:00
if (event->mimeData()->hasUrls()) {
2010-07-23 18:05:53 +04:00
const QList<QUrl> urls = event->mimeData()->urls();
2012-02-20 21:30:53 +04:00
foreach (const QUrl &url, urls) {
if (!url.isEmpty()) {
if (url.scheme().compare("file", Qt::CaseInsensitive) == 0)
2010-12-30 22:12:03 +03:00
files << url.toLocalFile();
else
files << url.toString();
}
}
} else {
files = event->mimeData()->text().split(QString::fromUtf8("\n"));
}
// Add file to download list
Preferences pref;
const bool useTorrentAdditionDialog = pref.useAdditionDialog();
2012-02-20 21:30:53 +04:00
foreach (QString file, files) {
2010-12-30 22:12:03 +03:00
qDebug("Dropped file %s on download list", qPrintable(file));
2012-02-20 21:30:53 +04:00
if (misc::isUrl(file)) {
2010-12-04 17:09:41 +03:00
QBtSession::instance()->downloadFromUrl(file);
continue;
}
2010-12-31 11:47:13 +03:00
// Bitcomet or Magnet link
2012-02-20 21:30:53 +04:00
if (file.startsWith("bc://bt/", Qt::CaseInsensitive)) {
qDebug("Converting bc link to magnet link");
file = misc::bcLinkToMagnet(file);
}
2012-02-20 21:30:53 +04:00
if (file.startsWith("magnet:", Qt::CaseInsensitive)) {
2012-05-15 20:57:31 +04:00
if (useTorrentAdditionDialog)
AddNewTorrentDialog::showMagnet(file);
else
2010-12-31 11:47:13 +03:00
QBtSession::instance()->addMagnetUri(file);
2012-05-15 20:57:31 +04:00
} else {
// Local file
if (useTorrentAdditionDialog)
AddNewTorrentDialog::showTorrent(file);
else
QBtSession::instance()->addTorrent(file);
}
}
}
// Decode if we accept drag 'n drop or not
2010-11-14 00:15:52 +03:00
void MainWindow::dragEnterEvent(QDragEnterEvent *event) {
2012-02-20 21:56:07 +04:00
foreach (const QString &mime, event->mimeData()->formats()) {
qDebug("mimeData: %s", mime.toLocal8Bit().data());
}
2009-12-16 18:06:02 +03:00
if (event->mimeData()->hasFormat(QString::fromUtf8("text/plain")) || event->mimeData()->hasFormat(QString::fromUtf8("text/uri-list"))) {
event->acceptProposedAction();
}
}
/*****************************************************
* *
* Torrent *
* *
*****************************************************/
// Display a dialog to allow user to add
// torrents to download list
2010-11-14 00:15:52 +03:00
void MainWindow::on_actionOpen_triggered() {
Preferences pref;
QIniSettings settings;
// Open File Open Dialog
// Note: it is possible to select more than one file
2010-07-23 18:05:53 +04:00
const QStringList pathsList = QFileDialog::getOpenFileNames(0,
tr("Open Torrent Files"), settings.value(QString::fromUtf8("MainWindowLastDir"), QDir::homePath()).toString(),
tr("Torrent Files")+QString::fromUtf8(" (*.torrent)"));
2012-02-20 21:30:53 +04:00
if (!pathsList.empty()) {
const bool useTorrentAdditionDialog = pref.useAdditionDialog();
2010-03-03 17:42:57 +03:00
const uint listSize = pathsList.size();
2012-02-20 21:30:53 +04:00
for (uint i=0; i<listSize; ++i) {
2012-05-15 20:57:31 +04:00
if (useTorrentAdditionDialog)
AddNewTorrentDialog::showTorrent(pathsList.at(i));
else
2010-12-04 17:09:41 +03:00
QBtSession::instance()->addTorrent(pathsList.at(i));
}
// Save last dir to remember it
QStringList top_dir = pathsList.at(0).split(QDir::separator());
top_dir.removeLast();
settings.setValue(QString::fromUtf8("MainWindowLastDir"), top_dir.join(QDir::separator()));
}
}
// As program parameters, we can get paths or urls.
// This function parse the parameters and call
// the right addTorrent function, considering
// the parameter type.
2010-11-14 00:15:52 +03:00
void MainWindow::processParams(const QString& params_str) {
processParams(params_str.split("|", QString::SkipEmptyParts));
}
2010-11-14 00:15:52 +03:00
void MainWindow::processParams(const QStringList& params) {
2011-03-14 22:18:52 +03:00
Preferences pref;
const bool useTorrentAdditionDialog = pref.useAdditionDialog();
2012-02-20 21:30:53 +04:00
foreach (QString param, params) {
param = param.trimmed();
2012-02-20 21:30:53 +04:00
if (misc::isUrl(param)) {
2010-12-04 17:09:41 +03:00
QBtSession::instance()->downloadFromUrl(param);
}else{
if(param.startsWith("qbt://show")) {
if(ui_locked) {
if(!unlockUI())
return;
}
show();
activateWindow();
raise();
return; // Do not process more params
}
2012-02-20 21:30:53 +04:00
if (param.startsWith("bc://bt/", Qt::CaseInsensitive)) {
qDebug("Converting bc link to magnet link");
param = misc::bcLinkToMagnet(param);
}
2012-02-20 21:30:53 +04:00
if (param.startsWith("magnet:", Qt::CaseInsensitive)) {
2012-05-15 20:57:31 +04:00
if (useTorrentAdditionDialog)
AddNewTorrentDialog::showMagnet(param);
else
2010-12-04 17:09:41 +03:00
QBtSession::instance()->addMagnetUri(param);
} else {
2012-05-15 20:57:31 +04:00
if (useTorrentAdditionDialog)
AddNewTorrentDialog::showTorrent(param);
else
2010-12-04 17:09:41 +03:00
QBtSession::instance()->addTorrent(param);
}
}
}
}
2010-11-14 00:15:52 +03:00
void MainWindow::addTorrent(QString path) {
2010-12-04 17:09:41 +03:00
QBtSession::instance()->addTorrent(path);
}
2010-11-14 00:15:52 +03:00
void MainWindow::processDownloadedFiles(QString path, QString url) {
Preferences pref;
if (pref.useAdditionDialog())
2012-05-15 20:57:31 +04:00
AddNewTorrentDialog::showTorrent(path, url);
else
2010-12-04 17:09:41 +03:00
QBtSession::instance()->addTorrent(path, false, url);
}
void MainWindow::processNewMagnetLink(const QString& link) {
Preferences pref;
if (pref.useAdditionDialog())
AddNewTorrentDialog::showMagnet(link);
else
QBtSession::instance()->addMagnetUri(link);
}
2010-11-14 00:15:52 +03:00
void MainWindow::optionsSaved() {
loadPreferences();
}
// Load program preferences
2010-11-14 00:15:52 +03:00
void MainWindow::loadPreferences(bool configure_session) {
2010-12-04 17:09:41 +03:00
QBtSession::instance()->addConsoleMessage(tr("Options were saved successfully."));
const Preferences pref;
const bool newSystrayIntegration = pref.systrayIntegration();
2012-02-22 22:06:11 +04:00
actionLock_qBittorrent->setVisible(newSystrayIntegration);
2012-02-20 21:30:53 +04:00
if (newSystrayIntegration != (systrayIcon!=0)) {
if (newSystrayIntegration) {
// create the trayicon
2012-02-20 21:30:53 +04:00
if (!QSystemTrayIcon::isSystemTrayAvailable()) {
if (!configure_session) { // Program startup
2009-11-18 21:45:06 +03:00
systrayCreator = new QTimer(this);
connect(systrayCreator, SIGNAL(timeout()), this, SLOT(createSystrayDelayed()));
systrayCreator->setSingleShot(true);
systrayCreator->start(2000);
qDebug("Info: System tray is unavailable, trying again later.");
} else {
qDebug("Warning: System tray is unavailable.");
}
} else {
createTrayIcon();
}
} else {
// Destroy trayicon
2009-11-18 21:45:06 +03:00
delete systrayIcon;
delete myTrayIconMenu;
}
}
// Reload systray icon
2012-02-20 21:30:53 +04:00
if (newSystrayIntegration && systrayIcon) {
systrayIcon->setIcon(getSystrayIcon());
}
// General
2012-02-20 21:30:53 +04:00
if (pref.isToolbarDisplayed()) {
toolBar->setVisible(true);
} else {
// Clear search filter before hiding the top toolbar
search_filter->clear();
toolBar->setVisible(false);
}
2012-02-20 21:30:53 +04:00
if (pref.preventFromSuspend())
{
2011-03-07 23:22:35 +03:00
preventTimer->start(PREVENT_SUSPEND_INTERVAL);
}
else
{
2011-03-07 23:22:35 +03:00
preventTimer->stop();
m_pwr->setActivityState(false);
}
const uint new_refreshInterval = pref.getRefreshInterval();
transferList->setRefreshInterval(new_refreshInterval);
transferList->setAlternatingRowColors(pref.useAlternatingRowColors());
properties->getFilesList()->setAlternatingRowColors(pref.useAlternatingRowColors());
properties->getTrackerList()->setAlternatingRowColors(pref.useAlternatingRowColors());
properties->getPeerList()->setAlternatingRowColors(pref.useAlternatingRowColors());
// Queueing System
2012-02-20 21:30:53 +04:00
if (pref.isQueueingSystemEnabled()) {
if (!actionDecreasePriority->isVisible()) {
transferList->hidePriorityColumn(false);
actionDecreasePriority->setVisible(true);
actionIncreasePriority->setVisible(true);
prioSeparator->setVisible(true);
2010-12-20 21:22:06 +03:00
prioSeparatorMenu->setVisible(true);
}
} else {
2012-02-20 21:30:53 +04:00
if (actionDecreasePriority->isVisible()) {
transferList->hidePriorityColumn(true);
actionDecreasePriority->setVisible(false);
actionIncreasePriority->setVisible(false);
prioSeparator->setVisible(false);
2010-12-20 21:22:06 +03:00
prioSeparatorMenu->setVisible(false);
}
}
// Torrent properties
properties->reloadPreferences();
// Icon provider
2012-02-25 22:20:11 +04:00
#if defined(Q_WS_X11)
IconProvider::instance()->useSystemIconTheme(pref.useSystemIconTheme());
#endif
2012-02-20 21:30:53 +04:00
if (configure_session)
2010-12-04 17:09:41 +03:00
QBtSession::instance()->configureSession();
qDebug("GUI settings loaded");
}
2010-11-14 00:15:52 +03:00
void MainWindow::addUnauthenticatedTracker(const QPair<QTorrentHandle,QString> &tracker) {
// Trackers whose authentication was cancelled
2012-02-20 21:30:53 +04:00
if (unauthenticated_trackers.indexOf(tracker) < 0) {
unauthenticated_trackers << tracker;
}
}
// Called when a tracker requires authentication
2010-11-24 23:31:14 +03:00
void MainWindow::trackerAuthenticationRequired(const QTorrentHandle& h) {
2012-02-20 21:30:53 +04:00
if (unauthenticated_trackers.indexOf(QPair<QTorrentHandle,QString>(h, h.current_tracker())) < 0) {
// Tracker login
new trackerLogin(this, h);
}
}
// Check connection status and display right icon
2010-11-14 00:15:52 +03:00
void MainWindow::updateGUI() {
// update global informations
2012-02-20 21:30:53 +04:00
if (systrayIcon) {
#if defined(Q_WS_X11) || defined(Q_WS_MAC)
QString html = "<div style='background-color: #678db2; color: #fff;height: 18px; font-weight: bold; margin-bottom: 5px;'>";
html += tr("qBittorrent");
html += "</div>";
html += "<div style='vertical-align: baseline; height: 18px;'>";
2010-12-04 17:09:41 +03:00
html += "<img src=':/Icons/skin/download.png'/>&nbsp;"+tr("DL speed: %1 KiB/s", "e.g: Download speed: 10 KiB/s").arg(QString::number(QBtSession::instance()->getPayloadDownloadRate()/1024., 'f', 1));
html += "</div>";
html += "<div style='vertical-align: baseline; height: 18px;'>";
2010-12-04 17:09:41 +03:00
html += "<img src=':/Icons/skin/seeding.png'/>&nbsp;"+tr("UP speed: %1 KiB/s", "e.g: Upload speed: 10 KiB/s").arg(QString::number(QBtSession::instance()->getPayloadUploadRate()/1024., 'f', 1));
html += "</div>";
#else
// OSes such as Windows do not support html here
2010-12-04 17:09:41 +03:00
QString html =tr("DL speed: %1 KiB/s", "e.g: Download speed: 10 KiB/s").arg(QString::number(QBtSession::instance()->getPayloadDownloadRate()/1024., 'f', 1));
html += "\n";
2010-12-04 17:09:41 +03:00
html += tr("UP speed: %1 KiB/s", "e.g: Upload speed: 10 KiB/s").arg(QString::number(QBtSession::instance()->getPayloadUploadRate()/1024., 'f', 1));
#endif
2009-11-18 21:45:06 +03:00
systrayIcon->setToolTip(html); // tray icon
}
2012-02-20 21:30:53 +04:00
if (displaySpeedInTitle) {
setWindowTitle(tr("[D: %1/s, U: %2/s] qBittorrent %3", "D = Download; U = Upload; %3 is qBittorrent version").arg(misc::friendlyUnit(QBtSession::instance()->getSessionStatus().payload_download_rate)).arg(misc::friendlyUnit(QBtSession::instance()->getSessionStatus().payload_upload_rate)).arg(QString::fromUtf8(VERSION)));
}
}
2010-11-14 00:15:52 +03:00
void MainWindow::showNotificationBaloon(QString title, QString msg) const {
2012-02-20 21:30:53 +04:00
if (!Preferences().useProgramNotification()) return;
#if defined(Q_WS_X11) && defined(QT_DBUS_LIB)
org::freedesktop::Notifications notifications("org.freedesktop.Notifications",
"/org/freedesktop/Notifications",
QDBusConnection::sessionBus());
2012-02-20 21:30:53 +04:00
if (notifications.isValid()) {
QVariantMap hints;
hints["desktop-entry"] = "qBittorrent";
QDBusPendingReply<uint> reply = notifications.Notify("qBittorrent", 0, "qbittorrent", title,
msg, QStringList(), hints, -1);
reply.waitForFinished();
2012-02-20 21:30:53 +04:00
if (!reply.isError())
2010-06-22 18:18:43 +04:00
return;
}
#endif
2012-02-20 21:30:53 +04:00
if (systrayIcon && QSystemTrayIcon::supportsMessages())
2010-06-22 18:18:43 +04:00
systrayIcon->showMessage(title, msg, QSystemTrayIcon::Information, TIME_TRAY_BALLOON);
}
/*****************************************************
* *
* Utils *
* *
*****************************************************/
2010-11-14 00:15:52 +03:00
void MainWindow::downloadFromURLList(const QStringList& url_list) {
QIniSettings settings;
2010-03-03 17:42:57 +03:00
const bool useTorrentAdditionDialog = settings.value(QString::fromUtf8("Preferences/Downloads/AdditionDialog"), true).toBool();
2012-02-20 21:30:53 +04:00
foreach (QString url, url_list) {
if (url.startsWith("bc://bt/", Qt::CaseInsensitive)) {
qDebug("Converting bc link to magnet link");
url = misc::bcLinkToMagnet(url);
}
2012-02-20 21:30:53 +04:00
if (url.startsWith("magnet:", Qt::CaseInsensitive)) {
2012-05-15 20:57:31 +04:00
if (useTorrentAdditionDialog)
AddNewTorrentDialog::showMagnet(url);
else
2010-12-04 17:09:41 +03:00
QBtSession::instance()->addMagnetUri(url);
2012-05-15 20:57:31 +04:00
} else
2010-12-04 17:09:41 +03:00
QBtSession::instance()->downloadFromUrl(url);
}
}
/*****************************************************
* *
* Options *
* *
*****************************************************/
2010-11-14 00:15:52 +03:00
void MainWindow::createSystrayDelayed() {
2009-11-18 21:45:06 +03:00
static int timeout = 20;
2012-02-20 21:30:53 +04:00
if (QSystemTrayIcon::isSystemTrayAvailable()) {
// Ok, systray integration is now supported
// Create systray icon
createTrayIcon();
delete systrayCreator;
} else {
2012-02-20 21:30:53 +04:00
if (timeout) {
// Retry a bit later
2009-11-18 21:45:06 +03:00
systrayCreator->start(2000);
--timeout;
} else {
// Timed out, apparently system really does not
// support systray icon
delete systrayCreator;
2009-11-18 21:45:06 +03:00
// Disable it in program preferences to
// avoid trying at earch startup
Preferences().setSystrayIntegration(false);
}
}
}
2010-11-14 00:15:52 +03:00
void MainWindow::updateAltSpeedsBtn(bool alternative) {
2010-06-22 18:18:43 +04:00
actionUse_alternative_speed_limits->setChecked(alternative);
}
void MainWindow::updateTrayIconMenu()
{
actionToggleVisibility->setText(isVisible() ? tr("Hide") : tr("Show"));
}
2010-11-14 00:15:52 +03:00
QMenu* MainWindow::getTrayIconMenu() {
2012-02-20 21:30:53 +04:00
if (myTrayIconMenu)
return myTrayIconMenu;
// Tray icon Menu
myTrayIconMenu = new QMenu(this);
connect(myTrayIconMenu, SIGNAL(aboutToShow()), SLOT(updateTrayIconMenu()));
myTrayIconMenu->addAction(actionToggleVisibility);
myTrayIconMenu->addSeparator();
myTrayIconMenu->addAction(actionOpen);
2010-12-14 23:02:24 +03:00
//myTrayIconMenu->addAction(actionDownload_from_URL);
myTrayIconMenu->addSeparator();
const bool isAltBWEnabled = Preferences().isAltBandwidthEnabled();
updateAltSpeedsBtn(isAltBWEnabled);
actionUse_alternative_speed_limits->setChecked(isAltBWEnabled);
myTrayIconMenu->addAction(actionUse_alternative_speed_limits);
myTrayIconMenu->addAction(actionSet_global_download_limit);
myTrayIconMenu->addAction(actionSet_global_upload_limit);
myTrayIconMenu->addSeparator();
myTrayIconMenu->addAction(actionStart_All);
myTrayIconMenu->addAction(actionPause_All);
myTrayIconMenu->addSeparator();
myTrayIconMenu->addAction(actionExit);
2012-02-20 21:30:53 +04:00
if (ui_locked)
2010-08-22 23:08:15 +04:00
myTrayIconMenu->setEnabled(false);
return myTrayIconMenu;
}
2010-11-14 00:15:52 +03:00
void MainWindow::createTrayIcon() {
// Tray icon
2011-03-12 23:59:44 +03:00
systrayIcon = new QSystemTrayIcon(getSystrayIcon(), this);
systrayIcon->setContextMenu(getTrayIconMenu());
2009-11-18 21:45:06 +03:00
connect(systrayIcon, SIGNAL(messageClicked()), this, SLOT(balloonClicked()));
// End of Icon Menu
2009-11-18 21:45:06 +03:00
connect(systrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(toggleVisibility(QSystemTrayIcon::ActivationReason)));
systrayIcon->show();
}
// Display Program Options
2010-11-14 00:15:52 +03:00
void MainWindow::on_actionOptions_triggered() {
2012-02-20 21:30:53 +04:00
if (options) {
// Get focus
options->setFocus();
2010-03-03 17:42:57 +03:00
} else {
options = new options_imp(this);
connect(options, SIGNAL(status_changed()), this, SLOT(optionsSaved()));
}
}
2010-11-14 00:15:52 +03:00
void MainWindow::on_actionTop_tool_bar_triggered() {
2010-06-22 18:18:43 +04:00
bool is_visible = static_cast<QAction*>(sender())->isChecked();
toolBar->setVisible(is_visible);
Preferences().setToolbarDisplayed(is_visible);
2010-06-21 22:32:01 +04:00
}
2010-11-14 00:15:52 +03:00
void MainWindow::on_actionSpeed_in_title_bar_triggered() {
2010-06-22 18:18:43 +04:00
displaySpeedInTitle = static_cast<QAction*>(sender())->isChecked();
Preferences().showSpeedInTitleBar(displaySpeedInTitle);
2012-02-20 21:30:53 +04:00
if (displaySpeedInTitle)
2010-06-22 18:18:43 +04:00
updateGUI();
else
setWindowTitle(tr("qBittorrent %1", "e.g: qBittorrent v0.x").arg(QString::fromUtf8(VERSION)));
2010-06-21 22:32:01 +04:00
}
2010-11-14 00:15:52 +03:00
void MainWindow::on_actionRSS_Reader_triggered() {
RssSettings().setRSSEnabled(actionRSS_Reader->isChecked());
2010-06-22 17:39:49 +04:00
displayRSSTab(actionRSS_Reader->isChecked());
}
2010-06-21 22:32:01 +04:00
2010-11-14 00:15:52 +03:00
void MainWindow::on_actionSearch_engine_triggered() {
Preferences().setSearchEnabled(actionSearch_engine->isChecked());
2010-06-22 17:58:22 +04:00
displaySearchTab(actionSearch_engine->isChecked());
}
2010-11-14 00:15:52 +03:00
void MainWindow::on_action_Import_Torrent_triggered()
{
2010-11-14 00:15:52 +03:00
TorrentImportDlg::importTorrent();
}
/*****************************************************
* *
* HTTP Downloader *
* *
*****************************************************/
// Display an input dialog to prompt user for
// an url
2010-11-14 00:15:52 +03:00
void MainWindow::on_actionDownload_from_URL_triggered() {
2012-02-20 21:30:53 +04:00
if (!downloadFromURLDialog) {
downloadFromURLDialog = new downloadFromURL(this);
2010-11-24 23:31:14 +03:00
connect(downloadFromURLDialog, SIGNAL(urlsReadyToBeDownloaded(QStringList)), this, SLOT(downloadFromURLList(QStringList)));
}
}
2010-06-22 17:58:22 +04:00
#if defined(Q_WS_WIN) || defined(Q_WS_MAC)
2010-11-22 23:27:57 +03:00
void MainWindow::handleUpdateCheckFinished(bool update_available, QString new_version)
{
2012-02-20 21:30:53 +04:00
if (update_available) {
if (QMessageBox::question(this, tr("A newer version is available"),
tr("A newer version of qBittorrent is available on Sourceforge.\nWould you like to update qBittorrent to version %1?").arg(new_version),
QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes) == QMessageBox::Yes) {
// The user want to update, let's download the update
ProgramUpdater* updater = dynamic_cast<ProgramUpdater*>(sender());
connect(updater, SIGNAL(updateInstallFinished(QString)), SLOT(handleUpdateInstalled(QString)));
updater->updateProgram();
return;
}
}
sender()->deleteLater();
}
2010-11-22 23:27:57 +03:00
void MainWindow::handleUpdateInstalled(QString error_msg)
{
2012-02-20 21:30:53 +04:00
if (!error_msg.isEmpty()) {
QMessageBox::critical(this, tr("Impossible to update qBittorrent"), tr("qBittorrent failed to update, reason: %1").arg(error_msg));
}
}
#endif
2010-10-31 15:03:24 +03:00
2010-11-14 00:15:52 +03:00
void MainWindow::on_actionDonate_money_triggered()
2010-10-31 15:03:24 +03:00
{
QDesktopServices::openUrl(QUrl("http://sourceforge.net/donate/index.php?group_id=163414"));
}
2010-11-14 00:15:52 +03:00
void MainWindow::showConnectionSettings()
{
on_actionOptions_triggered();
options->showConnectionTab();
2010-10-31 15:03:24 +03:00
}
void MainWindow::minimizeWindow()
{
setWindowState(windowState() ^ Qt::WindowMinimized);
}
void MainWindow::on_actionExecution_Logs_triggered(bool checked)
{
2012-02-20 21:30:53 +04:00
if (checked) {
Q_ASSERT(!m_executionLog);
m_executionLog = new ExecutionLog(tabs);
int index_tab = tabs->addTab(m_executionLog, tr("Execution Log"));
tabs->setTabIcon(index_tab, IconProvider::instance()->getIcon("view-calendar-journal"));
} else {
2012-02-20 21:30:53 +04:00
if (m_executionLog)
delete m_executionLog;
}
Preferences().setExecutionLogEnabled(checked);
}
void MainWindow::on_actionAutoExit_qBittorrent_toggled(bool enabled)
{
qDebug() << Q_FUNC_INFO << enabled;
Preferences().setShutdownqBTWhenDownloadsComplete(enabled);
}
void MainWindow::on_actionAutoSuspend_system_toggled(bool enabled)
{
qDebug() << Q_FUNC_INFO << enabled;
Preferences().setSuspendWhenDownloadsComplete(enabled);
}
void MainWindow::on_actionAutoShutdown_system_toggled(bool enabled)
{
qDebug() << Q_FUNC_INFO << enabled;
Preferences().setShutdownWhenDownloadsComplete(enabled);
}
void MainWindow::checkForActiveTorrents()
{
2011-03-07 23:22:35 +03:00
const TorrentStatusReport report = transferList->getSourceModel()->getTorrentStatusReport();
2012-02-20 21:30:53 +04:00
if (report.nb_active > 0) // Active torrents are present; prevent system from suspend
2011-03-07 23:22:35 +03:00
m_pwr->setActivityState(true);
else
m_pwr->setActivityState(false);
}
2011-03-12 23:59:44 +03:00
QIcon MainWindow::getSystrayIcon() const
{
2011-03-26 12:03:12 +03:00
#if defined(Q_WS_X11)
TrayIcon::Style style = Preferences().trayIconStyle();
switch(style) {
case TrayIcon::MONO_DARK:
2011-03-26 12:03:12 +03:00
return QIcon(":/Icons/skin/qbittorrent_mono_dark.png");
case TrayIcon::MONO_LIGHT:
return QIcon(":/Icons/skin/qbittorrent_mono_light.png");
default:
break;
2011-03-12 23:59:44 +03:00
}
#endif
QIcon icon;
#if defined(Q_WS_X11)
if (Preferences().useSystemIconTheme()) {
icon = QIcon::fromTheme("qbittorrent");
}
#endif
if (icon.isNull()) {
icon.addFile(":/Icons/skin/qbittorrent22.png", QSize(22, 22));
icon.addFile(":/Icons/skin/qbittorrent16.png", QSize(16, 16));
icon.addFile(":/Icons/skin/qbittorrent32.png", QSize(32, 32));
}
2011-03-12 23:59:44 +03:00
return icon;
}