strawberry-audio-player-win.../src/widgets/osd_x11.cpp

177 lines
4.7 KiB
C++
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
*
* Strawberry 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 3 of the License, or
* (at your option) any later version.
*
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#include "config.h"
#include <stdbool.h>
2018-02-27 18:06:05 +01:00
#include <memory>
2018-07-03 21:21:33 +02:00
#ifdef HAVE_DBUS
# include <dbus/notification.h>
#endif
#include <QtGlobal>
#include <QObject>
#include <QByteArray>
#include <QDateTime>
#include <QMap>
#include <QVariant>
#include <QString>
#include <QStringList>
#include <QImage>
#include <QColor>
#ifdef HAVE_DBUS
# include <QCoreApplication>
# include <QDBusArgument>
# include <QDBusConnection>
# include <QDBusError>
# include <QDBusPendingCall>
# include <QDBusPendingReply>
#endif
#include <QJsonObject>
2018-02-27 18:06:05 +01:00
#include <QtDebug>
#include "core/logging.h"
#include "osd.h"
2018-02-27 18:06:05 +01:00
#ifdef HAVE_DBUS
2018-07-04 00:55:09 +02:00
QDBusArgument &operator<<(QDBusArgument &arg, const QImage &image) {
2018-02-27 18:06:05 +01:00
if (image.isNull()) {
// Sometimes this gets called with a null QImage for no obvious reason.
arg.beginStructure();
arg << 0 << 0 << 0 << false << 0 << 0 << QByteArray();
arg.endStructure();
return arg;
}
QImage scaled = image.scaledToHeight(100, Qt::SmoothTransformation);
scaled = scaled.convertToFormat(QImage::Format_ARGB32);
#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
// ABGR -> ARGB
QImage i = scaled.rgbSwapped();
#else
// ABGR -> GBAR
QImage i(scaled.size(), scaled.format());
for (int y = 0; y < i.height(); ++y) {
2018-07-04 00:55:09 +02:00
QRgb *p = (QRgb*)scaled.scanLine(y);
QRgb *q = (QRgb*)i.scanLine(y);
QRgb *end = p + scaled.width();
2018-02-27 18:06:05 +01:00
while (p < end) {
*q = qRgba(qGreen(*p), qBlue(*p), qAlpha(*p), qRed(*p));
p++;
q++;
}
}
#endif
arg.beginStructure();
arg << i.width();
arg << i.height();
arg << i.bytesPerLine();
arg << i.hasAlphaChannel();
int channels = i.isGrayscale() ? 1 : (i.hasAlphaChannel() ? 4 : 3);
arg << i.depth() / channels;
arg << channels;
arg << QByteArray(reinterpret_cast<const char*>(i.bits()), i.byteCount());
arg.endStructure();
return arg;
2018-02-27 18:06:05 +01:00
}
2018-07-04 00:55:09 +02:00
const QDBusArgument &operator>>(const QDBusArgument &arg, QImage &image) {
2018-02-27 18:06:05 +01:00
// This is needed to link but shouldn't be called.
Q_ASSERT(0);
return arg;
}
#endif // HAVE_DBUS
void OSD::Init() {
2018-02-27 18:06:05 +01:00
#ifdef HAVE_DBUS
notification_id_ = 0;
interface_.reset(new OrgFreedesktopNotificationsInterface(OrgFreedesktopNotificationsInterface::staticInterfaceName(), "/org/freedesktop/Notifications", QDBusConnection::sessionBus()));
2018-02-27 18:06:05 +01:00
if (!interface_->isValid()) {
qLog(Warning) << "Error connecting to notifications service.";
}
#endif // HAVE_DBUS
2018-02-27 18:06:05 +01:00
}
bool OSD::SupportsNativeNotifications() {
2018-02-27 18:06:05 +01:00
#ifdef HAVE_DBUS
return true;
#else
return false;
#endif
2018-02-27 18:06:05 +01:00
}
bool OSD::SupportsTrayPopups() { return true; }
2018-07-04 00:55:09 +02:00
void OSD::ShowMessageNative(const QString &summary, const QString &message, const QString &icon, const QImage &image) {
2018-02-27 18:06:05 +01:00
#ifdef HAVE_DBUS
if (!interface_) return;
QVariantMap hints;
if (!image.isNull()) {
hints["image_data"] = QVariant(image);
}
int id = 0;
if (last_notification_time_.secsTo(QDateTime::currentDateTime()) * 1000 < timeout_msec_) {
2018-02-27 18:06:05 +01:00
// Reuse the existing popup if it's still open. The reason we don't always
// reuse the popup is because the notification daemon on KDE4 won't re-show the bubble if it's already gone to the tray. See issue #118
2018-02-27 18:06:05 +01:00
id = notification_id_;
}
QDBusPendingReply<uint> reply = interface_->Notify(QCoreApplication::applicationName(), id, icon, summary, message, QStringList(), hints, timeout_msec_);
2018-07-04 00:55:09 +02:00
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this);
connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), SLOT(CallFinished(QDBusPendingCallWatcher*)));
2018-02-27 18:06:05 +01:00
#else // HAVE_DBUS
qLog(Warning) << "not implemented";
#endif // HAVE_DBUS
2018-02-27 18:06:05 +01:00
}
#ifdef HAVE_DBUS
2018-07-04 00:55:09 +02:00
void OSD::CallFinished(QDBusPendingCallWatcher *watcher) {
2018-02-27 18:06:05 +01:00
std::unique_ptr<QDBusPendingCallWatcher> w(watcher);
2019-04-08 18:46:11 +02:00
QDBusPendingReply<uint> reply = *w.get();
2018-02-27 18:06:05 +01:00
if (reply.isError()) {
qLog(Warning) << "Error sending notification" << reply.error().name();
return;
}
uint id = reply.value();
if (id != 0) {
notification_id_ = id;
last_notification_time_ = QDateTime::currentDateTime();
}
}
#endif