1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
/******************************************************************************
*
* Copyright (C) 2002 Hugo PEREIRA <mailto: hugo.pereira@free.fr>
*
* This 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 software 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, see <http://www.gnu.org/licenses/>.
*
*******************************************************************************/
#include "SystemNotifications_p.h"
#include "Debug.h"
#include "IconSize.h"
#ifndef QT_NO_DBUS
#include <QDBusArgument>
#include <QDBusMetaType>
#include <QDBusPendingCall>
#include <QDBusReply>
#endif
#ifndef QT_NO_DBUS
//____________________________________________________________
QDBusArgument &operator << (QDBusArgument &argument, const Notifications::ImageData &imageData)
{
argument.beginStructure();
argument
<< imageData.width
<< imageData.height
<< imageData.rowStride
<< imageData.hasAlpha
<< imageData.bitsPerSample
<< imageData.channels
<< imageData.data;
argument.endStructure();
return argument;
}
//____________________________________________________________
const QDBusArgument & operator >>(const QDBusArgument &argument, Notifications::ImageData &imageData)
{
argument.beginStructure();
argument
>> imageData.width
>> imageData.height
>> imageData.rowStride
>> imageData.hasAlpha
>> imageData.bitsPerSample
>> imageData.channels
>> imageData.data;
argument.endStructure();
return argument;
}
#endif
namespace Private
{
//____________________________________________
SystemNotificationsP::SystemNotificationsP( QObject* parent ):
QObject( parent ),
Counter( "SystemNotificationsP" )
{}
//____________________________________________
SystemNotificationsP::~SystemNotificationsP()
{
#ifndef QT_NO_DBUS
delete dbusInterface_;
#endif
}
//____________________________________________
void SystemNotificationsP::setApplicationName( const QString& value )
{ applicationName_ = value; }
//____________________________________________
void SystemNotificationsP::setApplicationIcon( const QIcon& icon )
{
if( icon.isNull() ) imageData_ = Notifications::ImageData();
else imageData_ = Notifications::ImageData( icon.pixmap( IconSize::get( IconSize::Maximum ) ).toImage() );
}
//____________________________________________
void SystemNotificationsP::initialize()
{
Debug::Throw( "SystemNotificationsP::initialize.\n" );
if( initialized_ ) return;
initialized_ = true;
#ifndef QT_NO_DBUS
// check session bus
auto dbus( QDBusConnection::sessionBus() );
if( !dbus.isConnected() ) return;
// connections
dbus.connect( "org.freedesktop.Notifications", "/org/freedesktop/Notifications", "org.freedesktop.Notifications","NotificationClosed", this, SLOT(_notificationClosed(quint32,quint32)) );
dbus.connect( "org.freedesktop.Notifications", "/org/freedesktop/Notifications", "org.freedesktop.Notifications","ActionInvoked", this, SLOT(_checkActionInvoked(quint32,QString)) );
// delete old interface if any
delete dbusInterface_;
// create new interface
dbusInterface_ = new QDBusInterface(
"org.freedesktop.Notifications",
"/org/freedesktop/Notifications",
"org.freedesktop.Notifications",
dbus );
// setup type for image transfer
if( !typeId_ ) typeId_ = qDBusRegisterMetaType<Notifications::ImageData>();
#endif
}
//____________________________________________
bool SystemNotificationsP::isSupported() const
{
Debug::Throw( "SystemNotificationsP::isSupported.\n" );
#ifndef QT_NO_DBUS
return initialized_ && QDBusConnection::sessionBus().isConnected() && dbusInterface_->isValid();
#else
return false;
#endif
}
//____________________________________________
void SystemNotificationsP::send( Notification notification )
{
#ifndef QT_NO_DBUS
// initialize
initialize();
// setup hints
QVariantMap hints;
const QString iconDataString( "icon_data" );
if( !notification.icon().isNull() )
{
hints.insert( iconDataString, QVariant::fromValue( Notifications::ImageData( notification.icon().pixmap( IconSize::get( IconSize::Maximum ) ).toImage() ) ) );
} else if( imageData_.isValid() ) {
hints.insert( iconDataString, QVariant::fromValue( imageData_ ) );
}
if( notification.flags() & Notification::Transient )
{ hints.insert( "transient", QVariant( true ) ); }
if( !notification.category().isEmpty() )
{ hints.insert( "category", QVariant( notification.category() ) ); }
// copy application name
if( notification.applicationName().isEmpty() ) notification.setApplicationName( applicationName_ );
// store actions
lastNotificationActions_ = notification.actionList();
// send
auto pendingCall = dbusInterface_->asyncCall( "Notify", "Notification", (uint)0,
notification.applicationName(),
notification.summary(),
notification.body(),
notification.actionList(),
hints,
notification.timeout() );
auto watcher = new QDBusPendingCallWatcher( pendingCall, this );
connect( watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), SLOT(_pendingCallFinished(QDBusPendingCallWatcher*)));
#endif
}
#ifndef QT_NO_DBUS
//____________________________________________
void SystemNotificationsP::_pendingCallFinished( QDBusPendingCallWatcher* watcher )
{
QDBusReply<quint32> reply( *watcher );
if( reply.isValid() )
{
notificationIds_.insert( reply.value(), lastNotificationActions_ );
emit notificationSent( reply.value() );
}
lastNotificationActions_.clear();
watcher->deleteLater();
}
//____________________________________________
void SystemNotificationsP::_notificationClosed( quint32 id, quint32 reason )
{
emit notificationClosed( id );
notificationIds_.remove( id );
}
//____________________________________________
void SystemNotificationsP::_checkActionInvoked( quint32 id, QString key )
{
// find matching notification in map, and check action key
auto iter = notificationIds_.find( id );
if( iter != notificationIds_.end() && iter.value().contains( key ) )
{ emit actionInvoked( id, key ); }
}
#endif
}
|