Skip to content

Commit

Permalink
Extract desktop integration stuff into separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
glassez authored Jul 9, 2022
1 parent 2ef0598 commit 8906309
Show file tree
Hide file tree
Showing 12 changed files with 541 additions and 347 deletions.
56 changes: 55 additions & 1 deletion src/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@

#ifndef DISABLE_GUI
#include "gui/addnewtorrentdialog.h"
#include "gui/desktopintegration.h"
#include "gui/mainwindow.h"
#include "gui/shutdownconfirmdialog.h"
#include "gui/uithememanager.h"
Expand All @@ -102,6 +103,7 @@ namespace
{
#define SETTINGS_KEY(name) u"Application/" name
#define FILELOGGER_SETTINGS_KEY(name) (SETTINGS_KEY(u"FileLogger/") name)
#define NOTIFICATIONS_SETTINGS_KEY(name) (SETTINGS_KEY(u"GUI/Notifications/"_qs) name)

const QString LOG_FOLDER = u"logs"_qs;
const QChar PARAMS_SEPARATOR = u'|';
Expand All @@ -112,7 +114,7 @@ namespace
const int MAX_FILELOG_SIZE = 1000 * 1024 * 1024; // 1000MiB
const int DEFAULT_FILELOG_SIZE = 65 * 1024; // 65KiB

#if !defined(DISABLE_GUI)
#ifndef DISABLE_GUI
const int PIXMAP_CACHE_SIZE = 64 * 1024 * 1024; // 64MiB
#endif
}
Expand All @@ -132,6 +134,9 @@ Application::Application(int &argc, char **argv)
#ifdef Q_OS_WIN
, m_processMemoryPriority(SETTINGS_KEY(u"ProcessMemoryPriority"_qs))
#endif
#ifndef DISABLE_GUI
, m_storeNotificationTorrentAdded(NOTIFICATIONS_SETTINGS_KEY(u"TorrentAdded"_qs))
#endif
{
qRegisterMetaType<Log::Msg>("Log::Msg");
qRegisterMetaType<Log::Peer>("Log::Peer");
Expand Down Expand Up @@ -198,10 +203,25 @@ Application::~Application()
}

#ifndef DISABLE_GUI
DesktopIntegration *Application::desktopIntegration()
{
return m_desktopIntegration;
}

MainWindow *Application::mainWindow()
{
return m_window;
}

bool Application::isTorrentAddedNotificationsEnabled() const
{
return m_storeNotificationTorrentAdded;
}

void Application::setTorrentAddedNotificationsEnabled(const bool value)
{
m_storeNotificationTorrentAdded = value;
}
#endif

const QBtCommandLineParameters &Application::commandLineArgs() const
Expand Down Expand Up @@ -683,6 +703,40 @@ int Application::exec(const QStringList &params)
#endif // DISABLE_WEBUI
#else
UIThemeManager::initInstance();

const auto *btSession = BitTorrent::Session::instance();

m_desktopIntegration = new DesktopIntegration(this);
connect(btSession, &BitTorrent::Session::fullDiskError, this
, [this](const BitTorrent::Torrent *torrent, const QString &msg)
{
m_desktopIntegration->showNotification(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(torrent->name(), msg));
});
connect(btSession, &BitTorrent::Session::loadTorrentFailed, this
, [this](const QString &error)
{
m_desktopIntegration->showNotification(tr("Error"), tr("Failed to add torrent: %1").arg(error));
});
connect(btSession, &BitTorrent::Session::torrentAdded, this
, [this](const BitTorrent::Torrent *torrent)
{
if (isTorrentAddedNotificationsEnabled())
m_desktopIntegration->showNotification(tr("Torrent added"), tr("'%1' was added.", "e.g: xxx.avi was added.").arg(torrent->name()));
});
connect(btSession, &BitTorrent::Session::torrentFinished, this
, [this](const BitTorrent::Torrent *torrent)
{
m_desktopIntegration->showNotification(tr("Download completed"), tr("'%1' has finished downloading.", "e.g: xxx.avi has finished downloading.").arg(torrent->name()));
});
connect(btSession, &BitTorrent::Session::downloadFromUrlFailed, this
, [this](const QString &url, const QString &reason)
{
m_desktopIntegration->showNotification(tr("URL download error")
, tr("Couldn't download file at URL '%1', reason: %2.").arg(url, reason));
});

m_window = new MainWindow(this);
#endif // DISABLE_GUI

Expand Down
8 changes: 8 additions & 0 deletions src/app/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ namespace RSS
}

#ifndef DISABLE_GUI
class DesktopIntegration;
class MainWindow;

using BaseApplication = QApplication;
Expand Down Expand Up @@ -124,7 +125,11 @@ class Application final : public BaseApplication, public BaseIApplication
#endif

#ifndef DISABLE_GUI
DesktopIntegration *desktopIntegration() override;
MainWindow *mainWindow() override;

bool isTorrentAddedNotificationsEnabled() const override;
void setTorrentAddedNotificationsEnabled(bool value) override;
#endif

private slots:
Expand Down Expand Up @@ -194,6 +199,9 @@ private slots:
#endif

#ifndef DISABLE_GUI
SettingValue<bool> m_storeNotificationTorrentAdded;

DesktopIntegration *m_desktopIntegration = nullptr;
MainWindow *m_window = nullptr;
#endif

Expand Down
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ add_library(qbt_gui STATIC
cookiesdialog.h
cookiesmodel.h
deletionconfirmationdialog.h
desktopintegration.h
downloadfromurldialog.h
executionlogwidget.h
fspathedit.h
Expand Down Expand Up @@ -128,6 +129,7 @@ add_library(qbt_gui STATIC
cookiesdialog.cpp
cookiesmodel.cpp
deletionconfirmationdialog.cpp
desktopintegration.cpp
downloadfromurldialog.cpp
executionlogwidget.cpp
fspathedit.cpp
Expand Down
23 changes: 11 additions & 12 deletions src/gui/advancedsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "base/preferences.h"
#include "base/unicodestrings.h"
#include "gui/addnewtorrentdialog.h"
#include "gui/desktopintegration.h"
#include "gui/mainwindow.h"
#include "interfaces/iguiapplication.h"

Expand Down Expand Up @@ -271,16 +272,15 @@ void AdvancedSettings::saveAdvancedSettings() const
// Stop tracker timeout
session->setStopTrackerTimeout(m_spinBoxStopTrackerTimeout.value());
// Program notification
MainWindow *mainWindow = app()->mainWindow();
mainWindow->setNotificationsEnabled(m_checkBoxProgramNotifications.isChecked());
mainWindow->setTorrentAddedNotificationsEnabled(m_checkBoxTorrentAddedNotifications.isChecked());
#if (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)) && defined(QT_DBUS_LIB)
mainWindow->setNotificationTimeout(m_spinBoxNotificationTimeout.value());
app()->desktopIntegration()->setNotificationsEnabled(m_checkBoxProgramNotifications.isChecked());
#ifdef QBT_USES_CUSTOMDBUSNOTIFICATIONS
app()->desktopIntegration()->setNotificationTimeout(m_spinBoxNotificationTimeout.value());
#endif
app()->setTorrentAddedNotificationsEnabled(m_checkBoxTorrentAddedNotifications.isChecked());
// Reannounce to all trackers when ip/port changed
session->setReannounceWhenAddressChangedEnabled(m_checkBoxReannounceWhenAddressChanged.isChecked());
// Misc GUI properties
mainWindow->setDownloadTrackerFavicon(m_checkBoxTrackerFavicon.isChecked());
app()->mainWindow()->setDownloadTrackerFavicon(m_checkBoxTrackerFavicon.isChecked());
AddNewTorrentDialog::setSavePathHistoryLength(m_spinBoxSavePathHistoryLength.value());
pref->setSpeedWidgetEnabled(m_checkBoxSpeedWidgetEnabled.isChecked());
#ifndef Q_OS_MACOS
Expand Down Expand Up @@ -677,17 +677,16 @@ void AdvancedSettings::loadAdvancedSettings()
, &m_spinBoxStopTrackerTimeout);

// Program notifications
const MainWindow *mainWindow = app()->mainWindow();
m_checkBoxProgramNotifications.setChecked(mainWindow->isNotificationsEnabled());
m_checkBoxProgramNotifications.setChecked(app()->desktopIntegration()->isNotificationsEnabled());
addRow(PROGRAM_NOTIFICATIONS, tr("Display notifications"), &m_checkBoxProgramNotifications);
// Torrent added notifications
m_checkBoxTorrentAddedNotifications.setChecked(mainWindow->isTorrentAddedNotificationsEnabled());
m_checkBoxTorrentAddedNotifications.setChecked(app()->isTorrentAddedNotificationsEnabled());
addRow(TORRENT_ADDED_NOTIFICATIONS, tr("Display notifications for added torrents"), &m_checkBoxTorrentAddedNotifications);
#if (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)) && defined(QT_DBUS_LIB)
#ifdef QBT_USES_CUSTOMDBUSNOTIFICATIONS
// Notification timeout
m_spinBoxNotificationTimeout.setMinimum(-1);
m_spinBoxNotificationTimeout.setMaximum(std::numeric_limits<int>::max());
m_spinBoxNotificationTimeout.setValue(mainWindow->getNotificationTimeout());
m_spinBoxNotificationTimeout.setValue(app()->desktopIntegration()->notificationTimeout());
m_spinBoxNotificationTimeout.setSpecialValueText(tr("System default"));
m_spinBoxNotificationTimeout.setSuffix(tr(" ms", " milliseconds"));
addRow(NOTIFICATION_TIMEOUT, tr("Notification timeout [0: infinite]"), &m_spinBoxNotificationTimeout);
Expand All @@ -696,7 +695,7 @@ void AdvancedSettings::loadAdvancedSettings()
m_checkBoxReannounceWhenAddressChanged.setChecked(session->isReannounceWhenAddressChangedEnabled());
addRow(REANNOUNCE_WHEN_ADDRESS_CHANGED, tr("Reannounce to all trackers when IP or port changed"), &m_checkBoxReannounceWhenAddressChanged);
// Download tracker's favicon
m_checkBoxTrackerFavicon.setChecked(mainWindow->isDownloadTrackerFavicon());
m_checkBoxTrackerFavicon.setChecked(app()->mainWindow()->isDownloadTrackerFavicon());
addRow(DOWNLOAD_TRACKER_FAVICON, tr("Download tracker's favicon"), &m_checkBoxTrackerFavicon);
// Save path history length
m_spinBoxSavePathHistoryLength.setRange(AddNewTorrentDialog::minPathHistoryLength, AddNewTorrentDialog::maxPathHistoryLength);
Expand Down
Loading

0 comments on commit 8906309

Please sign in to comment.