2007-09-26 09:44:06 +02:00
/*
* Copyright ( C ) 2007 Ivan Cukic < ivan . cukic + kde @ gmail . com >
*
* This program is free software ; you can redistribute it and / or modify
2007-10-12 10:44:02 +02:00
* it under the terms of the GNU Library / Lesser General Public License
* version 2 , or ( at your option ) any later version , as published by the
* Free Software Foundation
2007-09-26 09:44:06 +02:00
*
* 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
*
2007-10-12 10:44:02 +02:00
* You should have received a copy of the GNU Library / Lesser General Public
2007-09-26 09:44:06 +02:00
* License along with this program ; if not , write to the
* Free Software Foundation , Inc . ,
* 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 , USA .
*/
2007-09-27 21:55:18 +02:00
# include "plasma/appletbrowser.h"
2007-09-27 20:51:31 +02:00
2008-02-29 09:10:14 +01:00
# include <QVBoxLayout>
# include <QLabel>
2007-09-26 09:44:06 +02:00
# include <KAction>
2007-10-08 23:50:38 +02:00
# include <KConfig>
# include <KConfigGroup>
2008-02-26 22:22:13 +01:00
# include <KMenu>
2008-02-29 09:10:14 +01:00
# include <KPageWidgetItem>
2008-02-26 22:22:13 +01:00
# include <KPushButton>
# include <KServiceTypeTrader>
# include <KStandardAction>
2007-09-27 21:39:28 +02:00
2008-06-16 11:04:14 +02:00
# include <knewstuff2/engine.h>
2008-02-29 09:10:14 +01:00
# include "plasma/applet.h"
2007-09-27 21:39:28 +02:00
# include "plasma/corona.h"
# include "plasma/containment.h"
2007-09-27 21:55:18 +02:00
# include "plasma/appletbrowser/kcategorizeditemsview_p.h"
2008-02-29 09:10:14 +01:00
# include "plasma/appletbrowser/plasmaappletitemmodel_p.h"
# include "plasma/appletbrowser/openwidgetassistant_p.h"
2007-09-27 21:39:28 +02:00
namespace Plasma
{
2007-11-16 13:23:42 +01:00
class AppletBrowserWidget : : Private
2007-09-27 21:39:28 +02:00
{
public :
2008-04-29 22:01:23 +02:00
Private ( AppletBrowserWidget * w )
: q ( w ) ,
containment ( 0 ) ,
2007-09-27 21:39:28 +02:00
appletList ( 0 ) ,
2007-10-08 23:50:38 +02:00
config ( " plasmarc " ) ,
configGroup ( & config , " Applet Browser " ) ,
2007-11-16 13:23:42 +01:00
itemModel ( configGroup , w ) ,
filterModel ( w )
2007-09-27 21:39:28 +02:00
{
}
2007-11-06 22:10:11 +01:00
void initFilters ( ) ;
2008-04-29 22:01:23 +02:00
void init ( ) ;
void initRunningApplets ( ) ;
2007-11-06 22:10:11 +01:00
2008-04-29 22:01:23 +02:00
/**
* Tracks a new running applet
*/
void appletAdded ( Plasma : : Applet * applet ) ;
/**
* A running applet is no more
*/
void appletDestroyed ( QObject * applet ) ;
AppletBrowserWidget * q ;
2007-11-06 03:45:24 +01:00
QString application ;
2007-09-27 21:39:28 +02:00
Plasma : : Containment * containment ;
KCategorizedItemsView * appletList ;
2008-02-13 05:03:17 +01:00
QHash < QString , int > runningApplets ; // applet name => count
2007-12-10 02:18:57 +01:00
//extra hash so we can look up the names of deleted applets
2008-02-13 05:03:17 +01:00
QHash < Plasma : : Applet * , QString > appletNames ;
2007-10-23 22:14:29 +02:00
2007-10-08 23:50:38 +02:00
KConfig config ;
KConfigGroup configGroup ;
2007-09-27 21:39:28 +02:00
PlasmaAppletItemModel itemModel ;
KCategorizedItemsViewModels : : DefaultFilterModel filterModel ;
} ;
2007-11-16 13:23:42 +01:00
void AppletBrowserWidget : : Private : : initFilters ( )
2007-11-06 22:10:11 +01:00
{
filterModel . clear ( ) ;
filterModel . addFilter ( i18n ( " All Widgets " ) ,
2008-01-03 20:55:37 +01:00
KCategorizedItemsViewModels : : Filter ( ) , new KIcon ( " plasma " ) ) ;
2007-11-06 22:10:11 +01:00
// Recommended emblems and filters
QRegExp rx ( " recommended[.]([0-9A-Za-z]+) [ . ] caption " ) ;
QMapIterator < QString , QString > i ( configGroup . entryMap ( ) ) ;
while ( i . hasNext ( ) ) {
i . next ( ) ;
if ( ! rx . exactMatch ( i . key ( ) ) ) {
continue ;
}
2008-01-08 02:25:09 +01:00
//kDebug() << "These are the key/vals in rc file " << rx.cap(1) << "\n";
2007-11-06 22:10:11 +01:00
QString id = rx . cap ( 1 ) ;
QString caption = configGroup . readEntry ( " recommended. " + id + " .caption " ) ;
QString icon = configGroup . readEntry ( " recommended. " + id + " .icon " ) ;
QString plugins = configGroup . readEntry ( " recommended. " + id + " .plugins " ) ;
2008-03-04 13:03:34 +01:00
appletList - > addEmblem ( i18n ( " Recommended by %1 " , caption ) , new KIcon ( icon ) ,
2007-11-06 22:10:11 +01:00
KCategorizedItemsViewModels : : Filter ( " recommended. " + id , true ) ) ;
filterModel . addFilter ( i18n ( " Recommended by %1 " , caption ) ,
KCategorizedItemsViewModels : : Filter ( " recommended. " + id , true ) , new KIcon ( icon ) ) ;
}
// Filters: Special
filterModel . addFilter ( i18n ( " My Favorite Widgets " ) ,
KCategorizedItemsViewModels : : Filter ( " favorite " , true ) ,
2008-01-04 00:54:18 +01:00
new KIcon ( " bookmarks " ) ) ;
2007-11-06 22:10:11 +01:00
filterModel . addFilter ( i18n ( " Widgets I Have Used Before " ) ,
KCategorizedItemsViewModels : : Filter ( " used " , true ) ,
2007-12-25 21:17:06 +01:00
new KIcon ( " view-history " ) ) ;
2007-12-10 02:18:57 +01:00
filterModel . addFilter ( i18n ( " Currently Running Widgets " ) ,
KCategorizedItemsViewModels : : Filter ( " running " , true ) ,
2007-12-25 21:17:06 +01:00
new KIcon ( " view-history " ) ) ;
2007-11-06 22:10:11 +01:00
filterModel . addSeparator ( i18n ( " Categories: " ) ) ;
2008-04-16 23:15:38 +02:00
foreach ( const QString & category , Plasma : : Applet : : listCategories ( application ) ) {
2007-11-06 22:10:11 +01:00
filterModel . addFilter ( category ,
KCategorizedItemsViewModels : : Filter ( " category " , category ) ) ;
}
}
2008-04-29 22:01:23 +02:00
AppletBrowserWidget : : AppletBrowserWidget ( QWidget * parent , Qt : : WindowFlags f )
2007-11-16 13:23:42 +01:00
: QWidget ( parent , f ) ,
2008-04-29 22:01:23 +02:00
d ( new Private ( this ) )
2007-09-26 09:44:06 +02:00
{
2008-04-29 22:01:23 +02:00
d - > init ( ) ;
2007-09-26 09:44:06 +02:00
}
2007-11-16 13:23:42 +01:00
AppletBrowserWidget : : ~ AppletBrowserWidget ( )
2007-09-27 21:39:28 +02:00
{
2007-11-16 13:23:42 +01:00
delete d ;
}
2008-04-29 22:01:23 +02:00
void AppletBrowserWidget : : Private : : init ( )
2007-11-16 13:23:42 +01:00
{
2008-04-29 22:01:23 +02:00
QVBoxLayout * layout = new QVBoxLayout ( q ) ;
2007-11-16 13:23:42 +01:00
2008-04-29 22:01:23 +02:00
appletList = new KCategorizedItemsView ( q ) ;
connect ( appletList , SIGNAL ( activated ( const QModelIndex & ) ) , q , SLOT ( addApplet ( ) ) ) ;
layout - > addWidget ( appletList ) ;
2007-09-27 21:39:28 +02:00
2007-10-08 23:50:38 +02:00
// Other Emblems
2008-04-29 22:01:23 +02:00
appletList - > addEmblem ( i18n ( " Widgets I Have Used Before " ) , new KIcon ( " view-history " ) ,
KCategorizedItemsViewModels : : Filter ( " used " , true ) ) ;
2007-09-27 19:50:31 +02:00
2008-04-29 22:01:23 +02:00
initFilters ( ) ;
appletList - > setFilterModel ( & filterModel ) ;
2007-09-26 09:44:06 +02:00
// Other models
2008-04-29 22:01:23 +02:00
appletList - > setItemModel ( & itemModel ) ;
2007-12-10 02:18:57 +01:00
initRunningApplets ( ) ;
2008-02-18 21:55:50 +01:00
2008-04-29 22:01:23 +02:00
q - > setLayout ( layout ) ;
2007-12-10 02:18:57 +01:00
}
2008-04-29 22:01:23 +02:00
void AppletBrowserWidget : : Private : : initRunningApplets ( )
2007-12-10 02:18:57 +01:00
{
//get applets from corona, count them, send results to model
2008-04-29 22:01:23 +02:00
if ( ! containment ) {
2008-02-26 22:22:13 +01:00
return ;
}
2008-04-29 22:01:23 +02:00
//kDebug() << runningApplets.count();
Plasma : : Corona * c = containment - > corona ( ) ;
2007-12-20 00:58:13 +01:00
2007-12-10 02:18:57 +01:00
//we've tried our best to get a corona
//we don't want just one containment, we want them all
if ( ! c ) {
2008-01-08 02:25:09 +01:00
kDebug ( ) < < " can't happen " ;
2007-12-10 02:18:57 +01:00
return ;
}
2007-12-20 00:58:13 +01:00
2008-04-29 22:01:23 +02:00
appletNames . clear ( ) ;
runningApplets . clear ( ) ;
2007-12-20 00:58:13 +01:00
QList < Containment * > containments = c - > containments ( ) ;
2007-12-10 02:18:57 +01:00
foreach ( Containment * containment , containments ) {
2008-04-29 22:01:23 +02:00
connect ( containment , SIGNAL ( appletAdded ( Plasma : : Applet * , QPointF ) ) , q , SLOT ( appletAdded ( Plasma : : Applet * ) ) ) ;
2007-12-10 02:18:57 +01:00
//TODO track containments too?
2008-04-29 22:01:23 +02:00
QList < Applet * > applets = containment - > applets ( ) ;
2007-12-10 02:18:57 +01:00
foreach ( Applet * applet , applets ) {
2008-04-29 22:01:23 +02:00
runningApplets [ applet - > name ( ) ] + + ;
appletNames . insert ( applet , applet - > name ( ) ) ;
connect ( applet , SIGNAL ( destroyed ( QObject * ) ) , q , SLOT ( appletDestroyed ( QObject * ) ) ) ;
2007-12-10 02:18:57 +01:00
}
}
2008-02-13 05:03:17 +01:00
2008-04-29 22:01:23 +02:00
//kDebug() << runningApplets;
itemModel . setRunningApplets ( runningApplets ) ;
2007-09-26 09:44:06 +02:00
}
2007-11-16 13:23:42 +01:00
void AppletBrowserWidget : : setApplication ( const QString & app )
2007-09-26 09:44:06 +02:00
{
2007-11-06 03:45:24 +01:00
d - > application = app ;
2007-11-06 22:10:11 +01:00
d - > initFilters ( ) ;
d - > itemModel . setApplication ( app ) ;
//FIXME: AFAIK this shouldn't be necessary ... but here it is. need to find out what in that
// maze of models and views is screwing up
d - > appletList - > setItemModel ( & d - > itemModel ) ;
2007-12-10 02:18:57 +01:00
2008-02-13 05:03:17 +01:00
kDebug ( ) < < d - > runningApplets ;
d - > itemModel . setRunningApplets ( d - > runningApplets ) ;
2007-11-06 03:45:24 +01:00
}
2007-11-16 13:23:42 +01:00
QString AppletBrowserWidget : : application ( )
2007-11-06 03:45:24 +01:00
{
return d - > application ;
}
2007-12-20 00:58:13 +01:00
2008-02-13 03:30:07 +01:00
void AppletBrowserWidget : : setContainment ( Plasma : : Containment * containment )
{
2008-05-25 22:03:28 +02:00
if ( d - > containment ! = containment ) {
d - > containment = containment ;
d - > initRunningApplets ( ) ;
}
2008-02-13 03:30:07 +01:00
}
Containment * AppletBrowserWidget : : containment ( ) const
{
return d - > containment ;
}
2007-11-16 13:23:42 +01:00
void AppletBrowserWidget : : addApplet ( )
2007-09-27 21:39:28 +02:00
{
2008-01-08 02:25:09 +01:00
kDebug ( ) < < " Button ADD clicked " ;
2007-12-20 00:58:13 +01:00
if ( ! d - > containment ) {
return ;
}
2007-09-27 19:50:31 +02:00
2007-09-27 21:39:28 +02:00
foreach ( AbstractItem * item , d - > appletList - > selectedItems ( ) ) {
PlasmaAppletItem * selectedItem = ( PlasmaAppletItem * ) item ;
2008-01-08 02:25:09 +01:00
kDebug ( ) < < " Adding applet " < < selectedItem - > name ( ) < < " to containment " ;
2007-12-20 00:58:13 +01:00
d - > containment - > addApplet ( selectedItem - > pluginName ( ) , selectedItem - > arguments ( ) ) ;
2007-09-27 19:50:31 +02:00
}
}
2007-09-27 20:51:31 +02:00
2008-04-29 22:01:23 +02:00
void AppletBrowserWidget : : Private : : appletAdded ( Plasma : : Applet * applet )
2007-12-10 02:18:57 +01:00
{
QString name = applet - > name ( ) ;
2008-04-29 22:01:23 +02:00
//kDebug() << name;
2008-02-13 05:03:17 +01:00
2008-04-29 22:01:23 +02:00
runningApplets [ name ] + + ;
appletNames . insert ( applet , name ) ;
connect ( applet , SIGNAL ( destroyed ( QObject * ) ) , q , SLOT ( appletDestroyed ( QObject * ) ) ) ;
itemModel . setRunningApplets ( name , runningApplets [ name ] ) ;
2007-12-10 02:18:57 +01:00
}
2008-04-29 22:01:23 +02:00
void AppletBrowserWidget : : Private : : appletDestroyed ( QObject * applet )
2007-12-10 02:18:57 +01:00
{
2008-04-29 22:01:23 +02:00
//kDebug() << applet;
2007-12-10 02:18:57 +01:00
Plasma : : Applet * a = ( Plasma : : Applet * ) applet ; //don't care if it's valid, just need the address
2008-02-13 05:03:17 +01:00
2008-04-29 22:01:23 +02:00
QString name = appletNames . take ( a ) ;
2008-02-13 05:03:17 +01:00
int count = 0 ;
2008-04-29 22:01:23 +02:00
if ( runningApplets . contains ( name ) ) {
count = runningApplets [ name ] - 1 ;
2008-02-13 05:03:17 +01:00
if ( count < 1 ) {
2008-04-29 22:01:23 +02:00
runningApplets . remove ( name ) ;
2008-02-13 05:03:17 +01:00
} else {
2008-04-29 22:01:23 +02:00
runningApplets [ name ] = count ;
2008-02-13 05:03:17 +01:00
}
}
2008-04-29 22:01:23 +02:00
itemModel . setRunningApplets ( name , count ) ;
2007-12-10 02:18:57 +01:00
}
2008-02-19 10:19:02 +01:00
void AppletBrowserWidget : : destroyApplets ( const QString & name )
2007-12-10 02:18:57 +01:00
{
2008-02-26 22:22:13 +01:00
if ( ! d - > containment ) {
return ;
}
2008-02-13 05:03:17 +01:00
Plasma : : Corona * c = d - > containment - > corona ( ) ;
//we've tried our best to get a corona
//we don't want just one containment, we want them all
if ( ! c ) {
kDebug ( ) < < " can't happen " ;
return ;
}
foreach ( Containment * containment , c - > containments ( ) ) {
QList < Applet * > applets = containment - > applets ( ) ;
2008-04-29 22:01:23 +02:00
foreach ( Applet * applet , applets ) {
2008-02-13 05:03:17 +01:00
if ( applet - > name ( ) = = name ) {
2008-04-29 22:01:23 +02:00
d - > appletNames . remove ( applet ) ;
2008-02-13 05:03:17 +01:00
applet - > disconnect ( this ) ;
2008-04-29 22:01:23 +02:00
applet - > destroy ( ) ;
2008-02-13 05:03:17 +01:00
}
}
2007-12-10 02:18:57 +01:00
}
2008-02-13 05:03:17 +01:00
2007-12-10 02:18:57 +01:00
d - > runningApplets . remove ( name ) ;
2008-02-13 05:03:17 +01:00
d - > itemModel . setRunningApplets ( name , 0 ) ;
2007-12-10 02:18:57 +01:00
}
2008-02-26 22:22:13 +01:00
void AppletBrowserWidget : : downloadWidgets ( )
2007-09-27 21:39:28 +02:00
{
2008-06-16 11:04:14 +02:00
KNS : : Engine engine ( 0 ) ;
if ( engine . init ( " plasmoids.knsrc " ) ) {
KNS : : Entry : : List entries = engine . downloadDialogModal ( this ) ;
}
2007-09-27 21:39:28 +02:00
}
2008-02-26 22:22:13 +01:00
void AppletBrowserWidget : : openWidgetFile ( )
{
2008-02-29 09:10:14 +01:00
// TODO: if we already have one of these showing and the user clicks to add it again, show the same window?
OpenWidgetAssistant * assistant = new OpenWidgetAssistant ( topLevelWidget ( ) ) ;
assistant - > setAttribute ( Qt : : WA_DeleteOnClose , true ) ;
2008-03-04 13:03:34 +01:00
assistant - > show ( ) ;
2008-02-26 22:22:13 +01:00
}
2007-11-16 13:23:42 +01:00
2008-02-26 22:22:13 +01:00
class AppletBrowser : : Private
2007-11-16 13:23:42 +01:00
{
2008-02-26 22:22:13 +01:00
public :
2008-04-29 22:01:23 +02:00
void init ( AppletBrowser * ) ;
2008-02-26 22:22:13 +01:00
AppletBrowserWidget * widget ;
} ;
2007-11-16 13:23:42 +01:00
2008-04-29 22:01:23 +02:00
AppletBrowser : : AppletBrowser ( QWidget * parent , Qt : : WindowFlags f )
2007-11-16 13:23:42 +01:00
: KDialog ( parent , f ) ,
2008-02-26 22:22:13 +01:00
d ( new Private )
2007-11-16 13:23:42 +01:00
{
2008-04-29 22:01:23 +02:00
d - > init ( this ) ;
2007-11-16 13:23:42 +01:00
}
2008-04-29 22:01:23 +02:00
void AppletBrowser : : Private : : init ( AppletBrowser * q )
2007-11-16 13:23:42 +01:00
{
2008-04-29 22:01:23 +02:00
widget = new AppletBrowserWidget ( q ) ;
2008-02-26 22:22:13 +01:00
q - > setMainWidget ( widget ) ;
q - > setWindowTitle ( i18n ( " Widgets " ) ) ;
q - > setButtons ( KDialog : : Apply | KDialog : : Close | KDialog : : User1 ) ;
q - > setButtonText ( KDialog : : Apply , i18n ( " Add Widget " ) ) ;
2008-02-28 04:53:26 +01:00
q - > setButtonText ( KDialog : : User1 , i18n ( " Install New Widgets " ) ) ;
2008-02-26 22:22:13 +01:00
KMenu * widgetsMenu = new KMenu ( i18n ( " Get New Widgets " ) , q ) ;
QAction * action = new QAction ( KIcon ( " applications-internet " ) ,
2008-03-04 13:03:34 +01:00
i18n ( " Download From Internet " ) , q ) ;
2008-02-26 22:22:13 +01:00
connect ( action , SIGNAL ( triggered ( bool ) ) , widget , SLOT ( downloadWidgets ( ) ) ) ;
widgetsMenu - > addAction ( action ) ;
action = new QAction ( KIcon ( " applications-internet " ) ,
2008-03-04 13:03:34 +01:00
i18n ( " Install From File... " ) , q ) ;
2008-02-26 22:22:13 +01:00
connect ( action , SIGNAL ( triggered ( bool ) ) , widget , SLOT ( openWidgetFile ( ) ) ) ;
widgetsMenu - > addAction ( action ) ;
q - > button ( KDialog : : User1 ) - > setMenu ( widgetsMenu ) ;
q - > setButtonToolTip ( KDialog : : Close , i18n ( " Close the dialog " ) ) ;
q - > setButtonWhatsThis ( KDialog : : Close , i18n ( " <qt>When clicking <b>Close</b>, this dialog will be closed with no further action taken.</qt> " ) ) ;
q - > setButtonToolTip ( KDialog : : Apply , i18n ( " Add selected widgets " ) ) ;
q - > setButtonWhatsThis ( KDialog : : Apply , i18n ( " <qt>When clicking <b>Add Widget</b>, the selected widgets will be added to your desktop.</qt> " ) ) ;
2008-03-30 01:56:34 +01:00
q - > setButtonToolTip ( KDialog : : User1 , i18n ( " Install new widgets " ) ) ;
q - > setButtonWhatsThis ( KDialog : : User1 , i18n ( " <qt>Selecting <b>Get New Widgets</b> will show a window that allows you to download new widgets directly from the Internet, while Install From File allows you to add new widgets from files you have on disk.</qt> " ) ) ;
2008-02-26 22:22:13 +01:00
connect ( q , SIGNAL ( applyClicked ( ) ) , widget , SLOT ( addApplet ( ) ) ) ;
2008-02-28 04:53:26 +01:00
q - > setInitialSize ( QSize ( 400 , 600 ) ) ;
KConfigGroup cg ( KGlobal : : config ( ) , " PlasmaAppletBrowserDialog " ) ;
q - > restoreDialogSize ( cg ) ;
2007-11-16 13:23:42 +01:00
}
AppletBrowser : : ~ AppletBrowser ( )
{
2008-02-28 04:53:26 +01:00
KConfigGroup cg ( KGlobal : : config ( ) , " PlasmaAppletBrowserDialog " ) ;
saveDialogSize ( cg ) ;
2007-11-16 13:23:42 +01:00
}
void AppletBrowser : : setApplication ( const QString & app )
{
2008-02-26 22:22:13 +01:00
d - > widget - > setApplication ( app ) ;
2007-11-16 13:23:42 +01:00
}
QString AppletBrowser : : application ( )
{
2008-02-26 22:22:13 +01:00
return d - > widget - > application ( ) ;
2007-11-16 13:23:42 +01:00
}
2008-02-13 03:30:07 +01:00
void AppletBrowser : : setContainment ( Plasma : : Containment * containment )
{
2008-02-26 22:22:13 +01:00
d - > widget - > setContainment ( containment ) ;
2008-02-13 03:30:07 +01:00
}
Containment * AppletBrowser : : containment ( ) const
{
2008-02-26 22:22:13 +01:00
return d - > widget - > containment ( ) ;
2008-02-13 03:30:07 +01:00
}
2007-09-27 21:39:28 +02:00
} // namespace Plasma
2007-09-27 21:48:51 +02:00
# include "appletbrowser.moc"