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-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-01-23 00:31:57 +01:00
Private ( Containment * cont , AppletBrowserWidget * w )
2007-12-20 00:58:13 +01:00
: containment ( cont ) ,
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 ( ) ;
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: " ) ) ;
foreach ( const QString & category , Plasma : : Applet : : knownCategories ( application ) ) {
filterModel . addFilter ( category ,
KCategorizedItemsViewModels : : Filter ( " category " , category ) ) ;
}
}
2008-02-26 22:22:13 +01:00
AppletBrowserWidget : : AppletBrowserWidget ( Plasma : : Containment * containment , QWidget * parent , Qt : : WindowFlags f )
2007-11-16 13:23:42 +01:00
: QWidget ( parent , f ) ,
2008-02-26 22:22:13 +01:00
d ( new Private ( containment , this ) )
2007-09-26 09:44:06 +02:00
{
init ( ) ;
}
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 ;
}
void AppletBrowserWidget : : init ( )
{
QVBoxLayout * layout = new QVBoxLayout ( this ) ;
d - > appletList = new KCategorizedItemsView ( this ) ;
2007-10-23 22:14:29 +02:00
connect ( d - > appletList , SIGNAL ( activated ( const QModelIndex & ) ) , this , SLOT ( addApplet ( ) ) ) ;
2007-11-16 13:23:42 +01:00
layout - > addWidget ( d - > appletList ) ;
2007-09-27 21:39:28 +02:00
2007-10-08 23:50:38 +02:00
// Other Emblems
2008-03-04 13:03:34 +01:00
d - > appletList - > addEmblem ( i18n ( " Widgets I Have Used Before " ) , new KIcon ( " view-history " ) ,
2007-09-27 21:39:28 +02:00
KCategorizedItemsViewModels : : Filter ( " used " , true ) ) ;
2007-09-27 19:50:31 +02:00
2007-11-06 22:10:11 +01:00
d - > initFilters ( ) ;
2007-09-27 21:39:28 +02:00
d - > appletList - > setFilterModel ( & d - > filterModel ) ;
2007-09-26 09:44:06 +02:00
// Other models
2007-09-27 21:39:28 +02:00
d - > appletList - > setItemModel ( & d - > itemModel ) ;
2007-12-10 02:18:57 +01:00
initRunningApplets ( ) ;
2008-02-18 21:55:50 +01:00
setLayout ( layout ) ;
2007-12-10 02:18:57 +01:00
}
void AppletBrowserWidget : : initRunningApplets ( )
{
//get applets from corona, count them, send results to model
2008-02-26 22:22:13 +01:00
if ( ! d - > containment ) {
return ;
}
2008-01-08 02:25:09 +01:00
kDebug ( ) < < d - > runningApplets . count ( ) ;
2007-12-20 00:58:13 +01:00
Plasma : : Corona * c = d - > containment - > corona ( ) ;
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-02-13 05:03:17 +01:00
d - > appletNames . clear ( ) ;
d - > 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 ) {
connect ( containment , SIGNAL ( appletAdded ( Plasma : : Applet * ) ) , this , SLOT ( appletAdded ( Plasma : : Applet * ) ) ) ;
//TODO track containments too?
QList < Applet * > applets = containment - > applets ( ) ;
foreach ( Applet * applet , applets ) {
2008-02-13 05:03:17 +01:00
d - > runningApplets [ applet - > name ( ) ] + + ;
2007-12-10 02:18:57 +01:00
d - > appletNames . insert ( applet , applet - > name ( ) ) ;
connect ( applet , SIGNAL ( destroyed ( QObject * ) ) , this , SLOT ( appletDestroyed ( QObject * ) ) ) ;
}
}
2008-02-13 05:03:17 +01:00
kDebug ( ) < < d - > runningApplets ;
d - > itemModel . setRunningApplets ( d - > 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 )
{
d - > containment = containment ;
}
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
2007-12-10 02:18:57 +01:00
void AppletBrowserWidget : : appletAdded ( Plasma : : Applet * applet )
{
QString name = applet - > name ( ) ;
2008-01-08 02:25:09 +01:00
kDebug ( ) < < name ;
2008-02-13 05:03:17 +01:00
2008-02-13 10:33:29 +01:00
d - > runningApplets [ name ] + + ;
2007-12-10 02:18:57 +01:00
d - > appletNames . insert ( applet , name ) ;
connect ( applet , SIGNAL ( destroyed ( QObject * ) ) , this , SLOT ( appletDestroyed ( QObject * ) ) ) ;
2008-02-13 05:03:17 +01:00
d - > itemModel . setRunningApplets ( name , d - > runningApplets [ name ] ) ;
2007-12-10 02:18:57 +01:00
}
void AppletBrowserWidget : : appletDestroyed ( QObject * applet )
{
2008-01-08 02:25:09 +01: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-02-13 10:33:29 +01:00
QString name = d - > appletNames . take ( a ) ;
2008-02-13 05:03:17 +01:00
int count = 0 ;
if ( d - > runningApplets . contains ( name ) ) {
count = d - > runningApplets [ name ] - 1 ;
if ( count < 1 ) {
d - > runningApplets . remove ( name ) ;
} else {
d - > runningApplets [ name ] = count ;
}
}
d - > 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 ( ) ;
foreach ( Applet * applet , applets ) {
d - > appletNames . remove ( applet ) ;
if ( applet - > name ( ) = = name ) {
applet - > disconnect ( this ) ;
applet - > destroy ( ) ;
}
}
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
{
//TODO: implement
2008-01-08 02:25:09 +01:00
kDebug ( ) < < " GHNS button clicked " ;
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 :
void init ( AppletBrowser * , Plasma : : Containment * ) ;
AppletBrowserWidget * widget ;
} ;
2007-11-16 13:23:42 +01:00
AppletBrowser : : AppletBrowser ( Plasma : : Containment * containment , QWidget * parent , Qt : : WindowFlags f )
: KDialog ( parent , f ) ,
2008-02-26 22:22:13 +01:00
d ( new Private )
2007-11-16 13:23:42 +01:00
{
2008-02-26 22:22:13 +01:00
d - > init ( this , containment ) ;
2007-11-16 13:23:42 +01:00
}
2008-02-26 22:22:13 +01:00
void AppletBrowser : : Private : : init ( AppletBrowser * q , Plasma : : Containment * containment )
2007-11-16 13:23:42 +01:00
{
2008-02-26 22:22:13 +01:00
widget = new AppletBrowserWidget ( containment , q ) ;
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> " ) ) ;
q - > setButtonToolTip ( KDialog : : User1 , i18n ( " Download new widgets " ) ) ;
q - > setButtonWhatsThis ( KDialog : : User1 , i18n ( " <qt>When clicking <b>Get New Widgets</b>, a dialog will open to allow you to download new widgets. You need to be connected to the Internet.</qt> " ) ) ;
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"