Skip to content

Commit

Permalink
Make usage of D-Bus optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Emdek committed Sep 16, 2018
1 parent ccd5c25 commit 7fabce8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Expand Up @@ -30,10 +30,13 @@
#include "../../../../3rdparty/libmimeapps/Index.h"

#include <QtConcurrent/QtConcurrent>
#ifdef OTTER_ENABLE_DBUS
#include <QtDBus/QtDBus>
#include <QtDBus/QDBusReply>
#endif
#include <QtGui/QDesktopServices>
#include <QtGui/QIcon>
#ifdef OTTER_ENABLE_DBUS
#include <QtGui/QRgb>

QDBusArgument& operator<<(QDBusArgument &argument, const QImage &image)
Expand Down Expand Up @@ -94,23 +97,28 @@ const QDBusArgument& operator>>(const QDBusArgument &argument, QImage &image)

return argument;
}
#endif

namespace Otter
{

FreeDesktopOrgPlatformIntegration::FreeDesktopOrgPlatformIntegration(QObject *parent) : PlatformIntegration(parent),
m_notificationsInterface(new QDBusInterface(QLatin1String("org.freedesktop.Notifications"), QLatin1String("/org/freedesktop/Notifications"), QLatin1String("org.freedesktop.Notifications"), QDBusConnection::sessionBus(), this))
FreeDesktopOrgPlatformIntegration::FreeDesktopOrgPlatformIntegration(QObject *parent) : PlatformIntegration(parent)
#ifdef OTTER_ENABLE_DBUS
, m_notificationsInterface(new QDBusInterface(QLatin1String("org.freedesktop.Notifications"), QLatin1String("/org/freedesktop/Notifications"), QLatin1String("org.freedesktop.Notifications"), QDBusConnection::sessionBus(), this))
#endif
{
#if QT_VERSION >= 0x050700
QGuiApplication::setDesktopFileName(QLatin1String("otter-browser.desktop"));
#endif

#ifdef OTTER_ENABLE_DBUS
qDBusRegisterMetaType<QImage>();

m_notificationsInterface->connection().connect(m_notificationsInterface->service(), m_notificationsInterface->path(), m_notificationsInterface->interface(), QLatin1String("NotificationClosed"), this, SLOT(handleNotificationIgnored(quint32,quint32)));
m_notificationsInterface->connection().connect(m_notificationsInterface->service(), m_notificationsInterface->path(), m_notificationsInterface->interface(), QLatin1String("ActionInvoked"), this, SLOT(handleNotificationClicked(quint32,QString)));

updateTransfersProgress();
#endif

QTimer::singleShot(250, this, [&]()
{
Expand All @@ -120,17 +128,21 @@ FreeDesktopOrgPlatformIntegration::FreeDesktopOrgPlatformIntegration(QObject *pa
});
});

#ifdef OTTER_ENABLE_DBUS
connect(TransfersManager::getInstance(), &TransfersManager::transferChanged, this, &FreeDesktopOrgPlatformIntegration::updateTransfersProgress);
connect(TransfersManager::getInstance(), &TransfersManager::transferStarted, this, &FreeDesktopOrgPlatformIntegration::updateTransfersProgress);
connect(TransfersManager::getInstance(), &TransfersManager::transferFinished, this, &FreeDesktopOrgPlatformIntegration::updateTransfersProgress);
connect(TransfersManager::getInstance(), &TransfersManager::transferRemoved, this, &FreeDesktopOrgPlatformIntegration::updateTransfersProgress);
connect(TransfersManager::getInstance(), &TransfersManager::transferStopped, this, &FreeDesktopOrgPlatformIntegration::updateTransfersProgress);
#endif
}

#ifdef OTTER_ENABLE_DBUS
FreeDesktopOrgPlatformIntegration::~FreeDesktopOrgPlatformIntegration()
{
setTransfersProgress(0, 0, 0);
}
#endif

void FreeDesktopOrgPlatformIntegration::runApplication(const QString &command, const QUrl &url) const
{
Expand Down Expand Up @@ -160,6 +172,7 @@ void FreeDesktopOrgPlatformIntegration::runApplication(const QString &command, c
QProcess::startDetached(QString::fromStdString(parsed.at(0)), arguments);
}

#ifdef OTTER_ENABLE_DBUS
void FreeDesktopOrgPlatformIntegration::handleNotificationCallFinished(QDBusPendingCallWatcher *watcher)
{
Notification *notification(m_notificationWatchers.value(watcher, nullptr));
Expand Down Expand Up @@ -281,6 +294,7 @@ void FreeDesktopOrgPlatformIntegration::setTransfersProgress(qint64 bytesTotal,

QDBusConnection::sessionBus().send(message);
}
#endif

Style* FreeDesktopOrgPlatformIntegration::createStyle(const QString &name) const
{
Expand Down Expand Up @@ -319,9 +333,11 @@ QVector<ApplicationInformation> FreeDesktopOrgPlatformIntegration::getApplicatio
return result;
}

#ifdef OTTER_ENABLE_DBUS
bool FreeDesktopOrgPlatformIntegration::canShowNotifications() const
{
return m_notificationsInterface->isValid();
}
#endif

}
Expand Up @@ -24,12 +24,14 @@

#include "../../../core/PlatformIntegration.h"

#ifdef OTTER_ENABLE_DBUS
#include <QtDBus/QDBusArgument>
#include <QtDBus/QDBusInterface>
#include <QtDBus/QDBusPendingCallWatcher>

QDBusArgument& operator<<(QDBusArgument &argument, const QImage &image);
const QDBusArgument& operator>>(const QDBusArgument &argument, QImage &image);
#endif

namespace Otter
{
Expand All @@ -40,11 +42,14 @@ class FreeDesktopOrgPlatformIntegration final : public PlatformIntegration

public:
explicit FreeDesktopOrgPlatformIntegration(QObject *parent);
#ifdef OTTER_ENABLE_DBUS
~FreeDesktopOrgPlatformIntegration();
#endif

void runApplication(const QString &command, const QUrl &url = {}) const override;
Style* createStyle(const QString &name) const override;
QVector<ApplicationInformation> getApplicationsForMimeType(const QMimeType &mimeType) override;
#ifdef OTTER_ENABLE_DBUS
bool canShowNotifications() const override;

public slots:
Expand All @@ -58,11 +63,14 @@ protected slots:
void handleNotificationIgnored(quint32 identifier, quint32 reason);
void handleNotificationClicked(quint32 identifier, const QString &action);
void updateTransfersProgress();
#endif

private:
#ifdef OTTER_ENABLE_DBUS
QDBusInterface *m_notificationsInterface;
QHash<QDBusPendingCallWatcher*, Notification*> m_notificationWatchers;
QHash<quint32, Notification*> m_notifications;
#endif
QHash<QString, QVector<ApplicationInformation> > m_applicationsCache;
};

Expand Down

0 comments on commit 7fabce8

Please sign in to comment.