From b58b4d00662896fefd8f539ebaeb4db36a71d38c Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Sun, 18 Sep 2011 16:42:50 +0300 Subject: [PATCH] Add file association settings to program preferences (Windows) --- Changelog | 1 + src/mainwindow.cpp | 5 ++- src/preferences/options.ui | 49 +++++++++++++++------ src/preferences/options_imp.cpp | 15 +++++++ src/preferences/preferences.h | 75 ++++++++++++++++++++++++--------- 5 files changed, 110 insertions(+), 35 deletions(-) diff --git a/Changelog b/Changelog index 1f9bbacd3..2014a4e04 100644 --- a/Changelog +++ b/Changelog @@ -1,4 +1,5 @@ * Unreleased - Christophe Dumez - v2.9.0 + - FEATURE: Add file association settings to program preferences (Windows) - BUGFIX: Add tray menu entry for toggling window visibility - COSMETIC: Display speed at the beginning of the Window title - OTHER: Display libraries versions in about dialog (sledgehammer999) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index e027ee09b..4fca06134 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -284,11 +284,12 @@ MainWindow::MainWindow(QWidget *parent, QStringList torrentCmdLine) : QMainWindo qDebug("GUI Built"); #ifdef Q_WS_WIN - if(!pref.neverCheckFileAssoc() && !Preferences::isFileAssocOk()) { + if(!pref.neverCheckFileAssoc() && (!Preferences::isTorrentFileAssocSet() || !Preferences::isMagnetLinkAssocSet())) { if(QMessageBox::question(0, tr("Torrent file association"), 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::setFileAssoc(); + Preferences::setTorrentFileAssoc(true); + Preferences::setMagnetLinkAssoc(true); } else { pref.setNeverCheckFileAssoc(); } diff --git a/src/preferences/options.ui b/src/preferences/options.ui index 28735c4f1..1b8c00521 100644 --- a/src/preferences/options.ui +++ b/src/preferences/options.ui @@ -170,9 +170,9 @@ 0 - 0 - 489 - 601 + -176 + 485 + 632 @@ -434,6 +434,29 @@ + + + + File association + + + + + + Use qBittorrent for .torrent files + + + + + + + Use qBittorrent for magnet links + + + + + + @@ -487,8 +510,8 @@ 0 0 - 501 - 999 + 488 + 902 @@ -947,8 +970,8 @@ 0 0 - 489 - 560 + 443 + 506 @@ -1391,8 +1414,8 @@ 0 0 - 507 - 510 + 384 + 440 @@ -1803,8 +1826,8 @@ 0 0 - 546 - 492 + 530 + 444 @@ -2176,8 +2199,8 @@ 0 0 - 451 - 594 + 419 + 535 diff --git a/src/preferences/options_imp.cpp b/src/preferences/options_imp.cpp index fef0227ff..8a021cf66 100644 --- a/src/preferences/options_imp.cpp +++ b/src/preferences/options_imp.cpp @@ -120,6 +120,11 @@ options_imp::options_imp(QWidget *parent): #if defined(QT_NO_OPENSSL) checkWebUiHttps->setVisible(false); #endif + +#ifndef Q_WS_WIN + groupFileAssociation->setVisible(false); +#endif + // Connect signals / slots // Proxy tab connect(comboProxyType, SIGNAL(currentIndexChanged(int)),this, SLOT(enableProxy(int))); @@ -355,6 +360,11 @@ void options_imp::saveOptions(){ pref.setSplashScreenDisabled(isSlashScreenDisabled()); pref.setConfirmOnExit(checkProgramExitConfirm->isChecked()); pref.setPreventFromSuspend(preventFromSuspend()); +#ifdef Q_WS_WIN + // Windows: file association settings + Preferences::setTorrentFileAssoc(checkAssociateTorrents->isChecked()); + Preferences::setMagnetLinkAssoc(checkAssociateMagnetLinks->isChecked()); +#endif // End General preferences // Downloads preferences @@ -519,6 +529,11 @@ void options_imp::loadOptions(){ comboTrayIcon->setCurrentIndex(pref.trayIconStyle()); checkProgramExitConfirm->setChecked(pref.confirmOnExit()); checkPreventFromSuspend->setChecked(pref.preventFromSuspend()); +#ifdef Q_WS_WIN + // Windows: file association settings + checkAssociateTorrents->setChecked(Preferences::isTorrentFileAssocSet()); + checkAssociateMagnetLinks->setChecked(Preferences::isMagnetLinkAssocSet()); +#endif // End General preferences // Downloads preferences QString save_path = pref.getSavePath(); diff --git a/src/preferences/preferences.h b/src/preferences/preferences.h index 659dfbbf5..31bb5649a 100644 --- a/src/preferences/preferences.h +++ b/src/preferences/preferences.h @@ -1083,11 +1083,11 @@ public: return value(QString::fromUtf8("Preferences/Win32/NeverCheckFileAssocation"), false).toBool(); } - void setNeverCheckFileAssoc(bool check=true) { + void setNeverCheckFileAssoc(bool check = true) { setValue(QString::fromUtf8("Preferences/Win32/NeverCheckFileAssocation"), check); } - static bool isFileAssocOk() { + static bool isTorrentFileAssocSet() { QSettings settings("HKEY_CLASSES_ROOT", QIniSettings::NativeFormat); if(settings.value(".torrent/Default").toString() != "qBittorrent") { qDebug(".torrent != qBittorrent"); @@ -1107,37 +1107,72 @@ public: const QString icon_str = "\""+qApp->applicationFilePath().replace("/", "\\")+"\",1"; if(settings.value("qBittorrent/DefaultIcon/Default", icon_str).toString().compare(icon_str, Qt::CaseInsensitive) != 0) return false; + + return true; + } + + static bool isMagnetLinkAssocSet() { + QSettings settings("HKEY_CLASSES_ROOT", QIniSettings::NativeFormat); + // Check magnet link assoc - shell_command = settings.value("Magnet/shell/open/command/Default", "").toString(); + QRegExp exe_reg("\"([^\"]+)\".*"); + QString shell_command = settings.value("Magnet/shell/open/command/Default", "").toString(); if(exe_reg.indexIn(shell_command) < 0) return false; - assoc_exe = exe_reg.cap(1); + QString assoc_exe = exe_reg.cap(1); qDebug("exe: %s", qPrintable(assoc_exe)); if(assoc_exe.compare(qApp->applicationFilePath().replace("/", "\\"), Qt::CaseInsensitive) != 0) return false; return true; } - static void setFileAssoc() { + static void setTorrentFileAssoc(bool set) { QSettings settings("HKEY_CLASSES_ROOT", QSettings::NativeFormat); + // .Torrent association - settings.setValue(".torrent/Default", "qBittorrent"); - settings.setValue(".torrent/Content Type", "application/x-bittorrent"); - settings.setValue("qBittorrent/shell/Default", "open"); - const QString command_str = "\""+qApp->applicationFilePath().replace("/", "\\")+"\" \"%1\""; - settings.setValue("qBittorrent/shell/open/command/Default", command_str); - settings.setValue("qBittorrent/Content Type/Default", "application/x-bittorrent"); - const QString icon_str = "\""+qApp->applicationFilePath().replace("/", "\\")+"\",1"; - settings.setValue("qBittorrent/DefaultIcon/Default", icon_str); - // Magnet association - settings.setValue("Magnet/Default", "Magnet URI"); - settings.setValue("Magnet/Content Type", "application/x-magnet"); - settings.setValue("Magnet/URL Protocol", ""); - settings.setValue("Magnet/DefaultIcon/Default", icon_str); - settings.setValue("Magnet/shell/Default", "open"); - settings.setValue("Magnet/shell/open/command/Default", command_str); + if (set) { + const QString command_str = "\""+qApp->applicationFilePath().replace("/", "\\")+"\" \"%1\""; + const QString icon_str = "\""+qApp->applicationFilePath().replace("/", "\\")+"\",1"; + + settings.setValue(".torrent/Default", "qBittorrent"); + settings.setValue(".torrent/Content Type", "application/x-bittorrent"); + settings.setValue("qBittorrent/shell/Default", "open"); + settings.setValue("qBittorrent/shell/open/command/Default", command_str); + settings.setValue("qBittorrent/Content Type/Default", "application/x-bittorrent"); + settings.setValue("qBittorrent/DefaultIcon/Default", icon_str); + } else { + settings.remove(".torrent/Default"); + settings.remove(".torrent/Content Type"); + settings.remove("qBittorrent/shell/Default"); + settings.remove("qBittorrent/shell/open/command/Default"); + settings.remove("qBittorrent/Content Type/Default"); + settings.remove("qBittorrent/DefaultIcon/Default"); + } } + static void setMagnetLinkAssoc(bool set) { + QSettings settings("HKEY_CLASSES_ROOT", QSettings::NativeFormat); + + // Magnet association + if (set) { + const QString command_str = "\""+qApp->applicationFilePath().replace("/", "\\")+"\" \"%1\""; + const QString icon_str = "\""+qApp->applicationFilePath().replace("/", "\\")+"\",1"; + + settings.setValue("Magnet/Default", "Magnet URI"); + settings.setValue("Magnet/Content Type", "application/x-magnet"); + settings.setValue("Magnet/URL Protocol", ""); + settings.setValue("Magnet/DefaultIcon/Default", icon_str); + settings.setValue("Magnet/shell/Default", "open"); + settings.setValue("Magnet/shell/open/command/Default", command_str); + } else { + settings.remove("Magnet/Default"); + settings.remove("Magnet/Content Type"); + settings.remove("Magnet/URL Protocol"); + settings.remove("Magnet/DefaultIcon/Default"); + settings.remove("Magnet/shell/Default"); + settings.remove("Magnet/shell/open/command/Default"); + } + } #endif bool isTrackerEnabled() const {