aboutsummaryrefslogblamecommitdiff
path: root/src/plugins/General/kdenotify/kdenotify.cpp
blob: 8929670134bb0c3df6f1ef2174ff04b7927ee2ca (plain) (tree)
1
2
                                                                            
                                                                            

















                                                                             


                                   


                    
                 

                 
                       
                                     


                                 
 
                                                                                   
 
                                                                

                                                                                        
                                                           
     

                                                                   
                                                                   








                                                                                                        
     
                                                                    







                                                                                                
 


                                                                 
                                                               

                                                                         
                        





                                                                                  



                       
                           
                            



                                    
                                                       
 
                    
     

                                                                                                   
     
                                                                                      

 
                                                

                                            

                                                                             
                                 
     
                         

                                   

                                   
                                                                                 
                                                                        
 

                                                                     
 

                      
     


                                                                                                     
                                    

                                                                                                 
     
                           
                                                     

                  








                                                                                                                  






                                              
                                                                  
 
/***************************************************************************
 *   Copyright (C) 2009-2010 by Artur Guzik                                *
 *   a.guzik88@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) any later version.                                   *
 *                                                                         *
 *   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, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QDBusInterface>
#include <QFileInfo>
#include <QDir>
#include <QSettings>
#include <QDebug>
#include <QTimer>
#include <QImage>
#include <QApplication>
#include <qmmpui/metadataformatter.h>
#include <qmmp/soundcore.h>
#include <qmmp/metadatamanager.h>
#include "kdenotify.h"

KdeNotify::KdeNotify(QObject *parent) : General(parent),m_UseFreedesktopSpec(false)
{
    notifier = new QDBusInterface("org.kde.VisualNotifications",
                                  "/VisualNotifications", "org.kde.VisualNotifications",
                                  QDBusConnection::sessionBus(), this);
    if(notifier->lastError().type() != QDBusError::NoError)
    {
        delete(notifier);
        qWarning() << "KdeNotify: unable to create dbus interface."
                << "Have you got KDE SC 4.4 or newer? Lets try...";

        notifier = new QDBusInterface("org.freedesktop.Notifications",
                                      "/org/freedesktop/Notifications","org.freedesktop.Notifications");
        if(notifier->lastError().type() != QDBusError::NoError)
        {
            qWarning() << "KdeNotify: Can't create interface. Sorry.";
            return;
        }
        m_UseFreedesktopSpec = true;
    }
    qWarning() << "KdeNotify: DBus interfece created successfully.";
    //m_ConfigDir = QFileInfo(Qmmp::configFile()).absoluteDir().path();
    QString path = QFileInfo(Qmmp::configFile()).absoluteDir().path();
    QDir dir(path);
    if(!dir.exists("kdenotifycache"))
        dir.mkdir("kdenotifycache");
    dir.cd("kdenotifycache");
    m_coverPath = dir.absolutePath() + "/cover.jpg";
    m_imagesDir = QDir(qApp->applicationFilePath () +"/../../share/qmmp/images").absolutePath();

    QSettings settings(Qmmp::configFile(),QSettings::IniFormat);
    settings.beginGroup("Kde_Notifier");
    m_NotifyDelay = settings.value("notify_delay",10000).toInt();
    m_ShowCovers = settings.value("show_covers",true).toBool();
    m_template = settings.value("template", DEFAULT_TEMPLATE).toString();
    m_template.remove("\n");
    settings.endGroup();

    QTimer *timer = new QTimer(this);
    timer->setSingleShot(true);
    timer->setInterval(3000); //after that notification will be showed.
    connect(timer,SIGNAL(timeout()),SLOT(showMetaData()));
    connect(SoundCore::instance(),SIGNAL(metaDataChanged()),timer, SLOT(start()));
}

KdeNotify::~KdeNotify()
{
    QDir dir(QDir::home());
    dir.remove(m_coverPath);
}

QString KdeNotify::totalTimeString()
{
    int time = SoundCore::instance()->totalTime()/1000;

    if(time >= 3600)
    {
        return QString("%1:%2:%3").arg(time/3600,2,10,QChar('0')).arg(time%3600/60,2,10,QChar('0'))
                .arg(time%60,2,10,QChar('0'));
    }
    return QString("%1:%2").arg(time/60,2,10,QChar('0')).arg(time%60,2,10,QChar('0'));
}

QList<QVariant> KdeNotify::prepareNotification()
{
    SoundCore *core = SoundCore::instance();
    if(core->metaData(Qmmp::URL).isEmpty()) //prevent show empty notification
    {
        return QList<QVariant>();
    }
    QList<QVariant> args;
    args.append("Qmmp"); //app-name
    args.append(0U); //replaces-id
    if(!m_UseFreedesktopSpec)
        args.append(""); //event-id
    args.append(m_imagesDir + "/app_icon.png");  //app-icon(path to icon on disk)
    args.append(tr("Qmmp now playing:")); //summary (notification title)

    MetaDataFormatter f(m_template);
    QString body = f.parse(core->metaData(), core->totalTime()/1000);

    QString coverPath;
    if(m_ShowCovers)
    {
        QPixmap cover = MetaDataManager::instance()->instance()->getCover(core->metaData(Qmmp::URL));
        if(!cover.isNull())
        {
            coverPath = m_coverPath;
            cover.scaled(100,100,Qt::IgnoreAspectRatio,Qt::SmoothTransformation).save(coverPath);
        }
    }
    if(coverPath.isEmpty())
        coverPath = m_imagesDir + "/empty_cover.png";

    QString nBody;
    nBody.append("<table padding=\"3px\"><tr><td width=\"80px\" height=\"80px\" padding=\"3px\">");
    nBody.append("<img height=\"80\" width=\"80\" src=\"%1\"></td><td width=\"10\"></td><td>%2</td></tr><table>");
    nBody = nBody.arg(coverPath,body);

    args.append(nBody); //body
    args.append(QStringList()); //actions
    args.append(QVariantMap()); //hints
    args.append(m_NotifyDelay); //timeout

    return args;
}

void KdeNotify::showMetaData()
{
    QList<QVariant> n = prepareNotification();
    if(!n.isEmpty())
        notifier->callWithArgumentList(QDBus::NoBlock,"Notify",n);
}