2013-08-20 12:55:03 +01:00
/**
* Copyright 2013 Albert Vaca < albertvaka @ gmail . com >
*
* This program 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 ) version 3 or any later version
* accepted by the membership of KDE e . V . ( or its successor approved
* by the membership of KDE e . V . ) , which shall act as a proxy
* defined in Section 14 of version 3 of the license .
*
* This program 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 "notificationsdbusinterface.h"
# include <QDebug>
# include <QDBusConnection>
2013-08-22 02:21:08 +01:00
# include <KNotification>
# include <KIcon>
2013-08-20 12:55:03 +01:00
NotificationsDbusInterface : : NotificationsDbusInterface ( Device * device , QObject * parent )
: QDBusAbstractAdaptor ( parent )
, mDevice ( device )
, mLastId ( 0 )
{
}
NotificationsDbusInterface : : ~ NotificationsDbusInterface ( )
{
qDeleteAll ( mNotifications ) ;
}
QStringList NotificationsDbusInterface : : activeNotifications ( )
{
return mNotifications . keys ( ) ;
}
void NotificationsDbusInterface : : processPackage ( const NetworkPackage & np )
{
if ( np . get < bool > ( " isCancel " ) ) {
removeNotification ( np . get < QString > ( " id " ) ) ;
} else {
Notification * noti = new Notification ( np , this ) ;
2013-08-22 03:39:04 +01:00
2013-09-20 14:54:30 +01:00
//TODO: Store the app icon if any under tmp with the app name as filename (so we only store one per app) and export the path to that file to dbus inside Notification
2013-09-10 14:04:08 +01:00
//Do not show updates to existent notification nor answers to a initialization request
if ( ! mInternalIdToPublicId . contains ( noti - > internalId ( ) ) & & ! np . get < bool > ( " requestAnswer " , false ) ) {
2013-08-22 03:39:04 +01:00
KNotification * notification = new KNotification ( " notification " ) ;
notification - > setPixmap ( KIcon ( " preferences-desktop-notification " ) . pixmap ( 48 , 48 ) ) ;
notification - > setComponentData ( KComponentData ( " kdeconnect " , " kdeconnect " ) ) ;
notification - > setTitle ( mDevice - > name ( ) ) ;
notification - > setText ( noti - > appName ( ) + " : " + noti - > ticker ( ) ) ;
notification - > sendEvent ( ) ;
}
2013-09-10 14:04:08 +01:00
addNotification ( noti ) ;
2013-08-20 12:55:03 +01:00
}
}
void NotificationsDbusInterface : : addNotification ( Notification * noti )
{
const QString & internalId = noti - > internalId ( ) ;
if ( mInternalIdToPublicId . contains ( internalId ) ) {
removeNotification ( internalId ) ;
}
connect ( noti , SIGNAL ( dismissRequested ( Notification * ) ) ,
this , SLOT ( dismissRequested ( Notification * ) ) ) ;
const QString & publicId = newId ( ) ;
mNotifications [ publicId ] = noti ;
mInternalIdToPublicId [ internalId ] = publicId ;
QDBusConnection : : sessionBus ( ) . registerObject ( mDevice - > dbusPath ( ) + " /notifications/ " + publicId , noti , QDBusConnection : : ExportScriptableContents ) ;
Q_EMIT notificationPosted ( publicId ) ;
2013-08-22 02:21:08 +01:00
2013-08-20 12:55:03 +01:00
}
void NotificationsDbusInterface : : removeNotification ( const QString & internalId )
{
qDebug ( ) < < " removeNotification " < < internalId ;
if ( ! mInternalIdToPublicId . contains ( internalId ) ) {
qDebug ( ) < < " Not found " ;
return ;
}
QString publicId = mInternalIdToPublicId [ internalId ] ;
mInternalIdToPublicId . remove ( internalId ) ;
if ( ! mNotifications . contains ( publicId ) ) {
qDebug ( ) < < " Not found " ;
return ;
}
Notification * noti = mNotifications [ publicId ] ;
mNotifications . remove ( publicId ) ;
2013-08-20 13:05:22 +01:00
//Deleting the notification will unregister it automatically
2013-08-20 12:55:03 +01:00
//QDBusConnection::sessionBus().unregisterObject(mDevice->dbusPath()+"/notifications/"+publicId);
noti - > deleteLater ( ) ;
Q_EMIT notificationRemoved ( publicId ) ;
}
void NotificationsDbusInterface : : dismissRequested ( Notification * notification )
{
const QString & internalId = notification - > internalId ( ) ;
NetworkPackage np ( PACKAGE_TYPE_NOTIFICATION ) ;
np . set < QString > ( " cancel " , internalId ) ;
mDevice - > sendPackage ( np ) ;
//This should be called automatically back from server
//removeNotification(internalId);
}
QString NotificationsDbusInterface : : newId ( )
{
return QString : : number ( + + mLastId ) ;
}