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
2007-09-26 09:44:06 +02:00
# include <KAction>
# include <KStandardAction>
2007-10-08 23:50:38 +02:00
# include <KConfig>
# include <KConfigGroup>
2007-09-26 09:44:06 +02:00
2007-09-27 21:39:28 +02:00
# include "plasma/corona.h"
# include "plasma/containment.h"
2007-12-10 02:18:57 +01:00
# include "plasma/applet.h"
2007-09-27 21:55:18 +02:00
# include "plasma/appletbrowser/plasmaappletitemmodel_p.h"
# include "plasma/appletbrowser/kcategorizeditemsview_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 :
2007-11-16 13:23:42 +01:00
Private ( Corona * co , 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-12-10 02:18:57 +01:00
//update the itemModel on our running applets
void updateRunningApplets ( ) ;
2007-11-06 22:10:11 +01:00
2007-11-06 03:45:24 +01:00
QString application ;
2007-09-27 21:39:28 +02:00
Plasma : : Containment * containment ;
KCategorizedItemsView * appletList ;
2007-12-10 02:18:57 +01:00
QMultiHash < QString , Plasma : : Applet * > runningApplets ;
//extra hash so we can look up the names of deleted applets
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 " ) ;
appletList - > addEmblem ( i18n ( " Recommended by %1 " , caption ) , new KIcon ( icon ) ,
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 ) ) ;
}
}
2007-12-10 02:18:57 +01:00
void AppletBrowserWidget : : Private : : updateRunningApplets ( )
{
QHash < QString , int > appCount ;
foreach ( QString key , runningApplets . uniqueKeys ( ) ) {
appCount [ key ] = runningApplets . count ( key ) ;
}
2008-01-08 02:25:09 +01:00
kDebug ( ) < < appCount ;
2007-12-10 02:18:57 +01:00
itemModel . setRunningApplets ( appCount ) ;
}
2007-12-20 00:58:13 +01:00
/*
2007-11-16 13:23:42 +01:00
AppletBrowserWidget : : AppletBrowserWidget ( Plasma : : Corona * corona , bool showButtons , QWidget * parent , Qt : : WindowFlags f )
: QWidget ( parent , f ) ,
d ( new Private ( corona , 0 , this ) ) ,
m_showButtons ( showButtons )
2007-09-26 09:44:06 +02:00
{
init ( ) ;
}
2007-12-20 00:58:13 +01:00
*/
2007-11-16 13:23:42 +01:00
AppletBrowserWidget : : AppletBrowserWidget ( Plasma : : Containment * containment , bool showButtons , QWidget * parent , Qt : : WindowFlags f )
: QWidget ( parent , f ) ,
d ( new Private ( 0 , containment , this ) ) ,
m_showButtons ( showButtons )
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-11-16 13:23:42 +01:00
if ( m_showButtons ) {
QHBoxLayout * buttonLayout = new QHBoxLayout ( ) ;
buttonLayout - > setSpacing ( KDialog : : spacingHint ( ) ) ;
buttonLayout - > setMargin ( KDialog : : marginHint ( ) ) ;
2007-10-12 19:30:30 +02:00
2007-11-16 13:23:42 +01:00
QPushButton * addButton = new QPushButton ( i18n ( " Add Widget " ) , this ) ;
connect ( addButton , SIGNAL ( clicked ( ) ) , this , SLOT ( addApplet ( ) ) ) ;
buttonLayout - > addWidget ( addButton ) ;
2007-09-26 09:44:06 +02:00
2007-11-16 13:23:42 +01:00
QPushButton * newButton = new QPushButton ( i18n ( " Get New Widgets " ) , this ) ; //TODO: not overly happy with this text
newButton - > setEnabled ( false ) ; //TODO: enable when GHNS integration is implemented
connect ( newButton , SIGNAL ( clicked ( ) ) , this , SLOT ( downloadApplets ( ) ) ) ;
buttonLayout - > addWidget ( newButton ) ;
2007-09-27 19:50:31 +02:00
2007-11-16 13:23:42 +01:00
layout - > addItem ( buttonLayout ) ;
}
2007-09-26 09:44:06 +02:00
2007-10-08 23:50:38 +02:00
// Other Emblems
2007-12-25 21:17:06 +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 ( ) ;
}
void AppletBrowserWidget : : initRunningApplets ( )
{
//get applets from corona, count them, send results to model
2008-01-08 02:25:09 +01:00
kDebug ( ) < < d - > runningApplets . count ( ) ;
2007-12-10 02:18:57 +01:00
QHash < QString , int > appCount ;
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
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 ) {
d - > runningApplets . insert ( applet - > name ( ) , applet ) ;
d - > appletNames . insert ( applet , applet - > name ( ) ) ;
connect ( applet , SIGNAL ( destroyed ( QObject * ) ) , this , SLOT ( appletDestroyed ( QObject * ) ) ) ;
appCount [ applet - > name ( ) ] + + ;
}
}
2008-01-08 02:25:09 +01:00
kDebug ( ) < < appCount ;
2007-12-10 02:18:57 +01:00
d - > itemModel . setRunningApplets ( appCount ) ;
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
d - > updateRunningApplets ( ) ;
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
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 ;
2007-12-10 02:18:57 +01:00
d - > runningApplets . insert ( name , applet ) ;
d - > appletNames . insert ( applet , name ) ;
connect ( applet , SIGNAL ( destroyed ( QObject * ) ) , this , SLOT ( appletDestroyed ( QObject * ) ) ) ;
d - > itemModel . setRunningApplets ( name , d - > runningApplets . count ( name ) ) ;
}
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
QString name = d - > appletNames . take ( a ) ;
//if !name, was the applet not found or was the name actually ""?
d - > runningApplets . remove ( name , a ) ;
d - > itemModel . setRunningApplets ( name , d - > runningApplets . count ( name ) ) ;
}
void AppletBrowserWidget : : destroyApplets ( QString name )
{
foreach ( Plasma : : Applet * app , d - > runningApplets . values ( name ) ) {
//FIXME I have a hard time believing this is safe without QPointer
app - > disconnect ( this ) ; //don't need to be told it's being destroyed
app - > destroy ( ) ;
d - > appletNames . remove ( app ) ;
}
d - > runningApplets . remove ( name ) ;
}
2007-11-16 13:23:42 +01:00
void AppletBrowserWidget : : downloadApplets ( )
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
}
2007-11-16 13:23:42 +01:00
2007-12-20 00:58:13 +01:00
/*AppletBrowser::AppletBrowser(Plasma::Corona * corona, QWidget * parent, Qt::WindowFlags f)
2007-11-16 13:23:42 +01:00
: KDialog ( parent , f ) ,
m_widget ( new AppletBrowserWidget ( corona , false , this ) )
{
2007-11-22 20:46:48 +01:00
winId ( ) ; // this is to get us a win id so that the next line doesn't abort on us
setWindowRole ( " appletbrowser " ) ;
2007-11-16 13:23:42 +01:00
init ( ) ;
2007-12-20 00:58:13 +01:00
} */
2007-11-16 13:23:42 +01:00
AppletBrowser : : AppletBrowser ( Plasma : : Containment * containment , QWidget * parent , Qt : : WindowFlags f )
: KDialog ( parent , f ) ,
m_widget ( new AppletBrowserWidget ( containment , false , this ) )
{
init ( ) ;
}
void AppletBrowser : : init ( )
{
setMainWidget ( m_widget ) ;
setWindowTitle ( i18n ( " Widgets " ) ) ;
setButtons ( KDialog : : Apply | KDialog : : Close | KDialog : : User1 ) ;
setButtonText ( KDialog : : Apply , i18n ( " Add Widget " ) ) ;
setButtonText ( KDialog : : User1 , i18n ( " Get New Widgets " ) ) ; //TODO: not overly happy with this text
enableButton ( KDialog : : User1 , false ) ; //TODO: enable when GHNS integration is implemented
2008-01-15 10:09:00 +01:00
setButtonToolTip ( KDialog : : Close , i18n ( " Close the dialog " ) ) ;
setButtonWhatsThis ( KDialog : : Close , i18n ( " <qt>When clicking <b>Close</b>, this dialog will be closed with no further action taken.</qt> " ) ) ;
setButtonToolTip ( KDialog : : Apply , i18n ( " Add selected widgets " ) ) ;
setButtonWhatsThis ( KDialog : : Apply , i18n ( " <qt>When clicking <b>Add Widget</b>, the selected widgets will be added to your desktop.</qt> " ) ) ;
setButtonToolTip ( KDialog : : User1 , i18n ( " Download new widgets " ) ) ;
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> " ) ) ;
2007-11-16 13:23:42 +01:00
connect ( this , SIGNAL ( applyClicked ( ) ) , m_widget , SLOT ( addApplet ( ) ) ) ;
connect ( this , SIGNAL ( user1Clicked ( ) ) , m_widget , SLOT ( downloadApplets ( ) ) ) ;
QAction * quit = KStandardAction : : quit ( qApp , SLOT ( quit ( ) ) , this ) ;
addAction ( quit ) ;
}
AppletBrowser : : ~ AppletBrowser ( )
{
}
void AppletBrowser : : setApplication ( const QString & app )
{
m_widget - > setApplication ( app ) ;
}
QString AppletBrowser : : application ( )
{
return m_widget - > application ( ) ;
}
2007-09-27 21:39:28 +02:00
} // namespace Plasma
2007-09-27 21:48:51 +02:00
# include "appletbrowser.moc"