2009-09-16 19:08:45 +02:00
/*
* Copyright 2009 Marco Martin < notmart @ gmail . com >
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation ; either version 2 , 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 Library General Public
* License along with this program ; if not , write to the
* Free Software Foundation , Inc . ,
* 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 , USA .
*/
# include "associatedapplicationmanager_p.h"
2010-10-14 14:27:15 +02:00
# include "config-plasma.h"
2009-09-16 19:08:45 +02:00
# include <QHash>
# include <QFile>
2012-06-05 00:01:21 +02:00
# include <qstandardpaths.h>
2012-09-17 18:56:56 +02:00
# include <klocalizedstring.h>
2012-04-28 16:34:56 +02:00
# include <kiconloader.h>
2009-09-16 19:08:45 +02:00
2012-08-24 03:16:04 +02:00
# if !PLASMA_NO_KIO
2010-10-14 14:27:15 +02:00
# include <krun.h>
# else
# include <QProcess>
# include <QDesktopServices>
# endif
2009-09-16 19:08:45 +02:00
# include "plasma/applet.h"
namespace Plasma
{
class AssociatedApplicationManagerPrivate
{
public :
AssociatedApplicationManagerPrivate ( )
{
}
~ AssociatedApplicationManagerPrivate ( )
{
}
void cleanupApplet ( QObject * obj )
{
Plasma : : Applet * applet = static_cast < Plasma : : Applet * > ( obj ) ;
applicationNames . remove ( applet ) ;
urlLists . remove ( applet ) ;
}
QHash < const Plasma : : Applet * , QString > applicationNames ;
2012-07-15 22:28:17 +02:00
QHash < const Plasma : : Applet * , QList < QUrl > > urlLists ;
2009-09-16 19:08:45 +02:00
} ;
class AssociatedApplicationManagerSingleton
{
public :
AssociatedApplicationManager self ;
} ;
2012-07-19 21:16:59 +02:00
Q_GLOBAL_STATIC ( AssociatedApplicationManagerSingleton , privateAssociatedApplicationManagerSelf )
2009-09-16 19:08:45 +02:00
AssociatedApplicationManager : : AssociatedApplicationManager ( QObject * parent )
: QObject ( parent ) ,
d ( new AssociatedApplicationManagerPrivate ( ) )
{
}
AssociatedApplicationManager : : ~ AssociatedApplicationManager ( )
{
delete d ;
}
AssociatedApplicationManager * AssociatedApplicationManager : : self ( )
{
2012-07-19 21:16:59 +02:00
return & privateAssociatedApplicationManagerSelf ( ) - > self ;
2009-09-16 19:08:45 +02:00
}
void AssociatedApplicationManager : : setApplication ( Plasma : : Applet * applet , const QString & application )
{
KService : : Ptr service = KService : : serviceByDesktopName ( application ) ;
2012-06-05 00:01:21 +02:00
if ( service | | ! QStandardPaths : : findExecutable ( application ) . isNull ( ) | | QFile : : exists ( application ) ) {
2009-09-16 19:08:45 +02:00
d - > applicationNames [ applet ] = application ;
if ( ! d - > urlLists . contains ( applet ) ) {
2012-01-22 03:11:26 +01:00
connect ( applet , SIGNAL ( destroyed ( QObject * ) ) , this , SLOT ( cleanupApplet ( QObject * ) ) ) ;
2009-09-16 19:08:45 +02:00
}
}
}
QString AssociatedApplicationManager : : application ( const Plasma : : Applet * applet ) const
{
return d - > applicationNames . value ( applet ) ;
}
2012-07-15 22:28:17 +02:00
void AssociatedApplicationManager : : setUrls ( Plasma : : Applet * applet , const QList < QUrl > & urls )
2009-09-16 19:08:45 +02:00
{
d - > urlLists [ applet ] = urls ;
}
2012-07-15 22:28:17 +02:00
QList < QUrl > AssociatedApplicationManager : : urls ( const Plasma : : Applet * applet ) const
2009-09-16 19:08:45 +02:00
{
return d - > urlLists . value ( applet ) ;
}
void AssociatedApplicationManager : : run ( Plasma : : Applet * applet )
{
if ( d - > applicationNames . contains ( applet ) ) {
2012-08-24 03:16:04 +02:00
# if !PLASMA_NO_KIO
2009-09-16 19:08:45 +02:00
bool success = KRun : : run ( d - > applicationNames . value ( applet ) , d - > urlLists . value ( applet ) , 0 ) ;
2010-10-14 14:27:15 +02:00
# else
QString execCommand = d - > applicationNames . value ( applet ) ;
// Clean-up the %u and friends from the exec command (KRun expect them, not QProcess)
execCommand = execCommand . replace ( QRegExp ( " %[a-z] " ) , QString ( ) ) ;
execCommand = execCommand . trimmed ( ) ;
QStringList parameters = d - > urlLists . value ( applet ) . toStringList ( ) ;
bool success = QProcess : : startDetached ( execCommand , parameters ) ;
# endif
2009-09-16 19:08:45 +02:00
if ( ! success ) {
2012-04-28 13:03:35 +02:00
applet - > showMessage ( KDE : : icon ( " application-exit " ) , i18n ( " There was an error attempting to exec the associated application with this widget. " ) , ButtonOk ) ;
2009-09-16 19:08:45 +02:00
}
2010-10-14 14:27:15 +02:00
2009-09-25 08:58:32 +02:00
} else if ( d - > urlLists . contains ( applet ) & & ! d - > urlLists . value ( applet ) . isEmpty ( ) ) {
2012-08-24 03:16:04 +02:00
# if !PLASMA_NO_KIO
2009-09-16 19:08:45 +02:00
KRun * krun = new KRun ( d - > urlLists . value ( applet ) . first ( ) , 0 ) ;
krun - > setAutoDelete ( true ) ;
2010-10-14 14:27:15 +02:00
# else
QDesktopServices : : openUrl ( d - > urlLists . value ( applet ) . first ( ) ) ;
# endif
2009-09-16 19:08:45 +02:00
}
}
bool AssociatedApplicationManager : : appletHasValidAssociatedApplication ( const Plasma : : Applet * applet ) const
{
return ( d - > applicationNames . contains ( applet ) | | d - > urlLists . contains ( applet ) ) ;
}
} // namespace Plasma
# include <moc_associatedapplicationmanager_p.cpp>