2005-12-29 22:55:22 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2005 by Aaron Seigo <aseigo@kde.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation
|
|
|
|
*
|
|
|
|
* 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.,
|
2006-01-23 12:37:31 +01:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2005-12-29 22:55:22 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QEvent>
|
|
|
|
#include <QList>
|
|
|
|
#include <QSize>
|
2006-04-13 02:11:16 +02:00
|
|
|
#include <QStringList>
|
2005-12-29 22:55:22 +01:00
|
|
|
#include <QTimer>
|
|
|
|
|
2006-12-17 00:04:44 +01:00
|
|
|
#include <KStandardDirs>
|
2005-12-29 22:55:22 +01:00
|
|
|
|
|
|
|
#include "applet.h"
|
2006-12-17 00:04:44 +01:00
|
|
|
#include "interface.h"
|
2006-04-13 02:11:16 +02:00
|
|
|
|
2005-12-29 22:55:22 +01:00
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
class Applet::Private
|
|
|
|
{
|
|
|
|
public:
|
2007-03-03 02:41:27 +01:00
|
|
|
Private( KService::Ptr appletDescription, int uniqueID )
|
|
|
|
: id( uniqueID ),
|
|
|
|
globalConfig( 0 ),
|
|
|
|
appletConfig( 0 ),
|
2007-02-27 22:53:12 +01:00
|
|
|
appletDescription(appletDescription)
|
2005-12-29 22:55:22 +01:00
|
|
|
{ }
|
|
|
|
|
2006-04-13 02:11:16 +02:00
|
|
|
~Private()
|
|
|
|
{
|
2007-03-03 02:41:27 +01:00
|
|
|
foreach ( const QString& engine, loadedEngines ) {
|
|
|
|
Interface::self()->unloadDataEngine( engine );
|
2006-04-13 02:11:16 +02:00
|
|
|
}
|
|
|
|
}
|
2005-12-29 22:55:22 +01:00
|
|
|
|
|
|
|
int id;
|
|
|
|
KSharedConfig::Ptr globalConfig;
|
|
|
|
KSharedConfig::Ptr appletConfig;
|
|
|
|
KService::Ptr appletDescription;
|
|
|
|
QList<QObject*> watchedForFocus;
|
2006-04-13 02:11:16 +02:00
|
|
|
QStringList loadedEngines;
|
2005-12-29 22:55:22 +01:00
|
|
|
};
|
|
|
|
|
2007-03-03 02:41:27 +01:00
|
|
|
Applet::Applet( QGraphicsItem *parent,
|
|
|
|
KService::Ptr appletDescription,
|
|
|
|
int id )
|
|
|
|
: QWidget( 0 ),
|
|
|
|
QGraphicsItemGroup( parent ),
|
|
|
|
d( new Private( appletDescription, id ) )
|
2005-12-29 22:55:22 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Applet::~Applet()
|
|
|
|
{
|
2007-03-03 02:41:27 +01:00
|
|
|
needsFocus( false );
|
2005-12-29 22:55:22 +01:00
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2006-04-13 02:11:16 +02:00
|
|
|
KSharedConfig::Ptr Applet::appletConfig() const
|
|
|
|
{
|
2007-03-03 02:41:27 +01:00
|
|
|
if ( !d->appletConfig ) {
|
|
|
|
QString file = KStandardDirs::locateLocal( "appdata",
|
2006-12-17 00:04:44 +01:00
|
|
|
"applets/" + instanceName() + "rc",
|
2007-03-03 02:41:27 +01:00
|
|
|
true );
|
|
|
|
d->appletConfig = KSharedConfig::openConfig( file );
|
2006-04-13 02:11:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return d->appletConfig;
|
|
|
|
}
|
|
|
|
|
2006-12-17 00:04:44 +01:00
|
|
|
KSharedConfig::Ptr Applet::globalAppletConfig() const
|
2005-12-29 22:55:22 +01:00
|
|
|
{
|
2007-03-03 02:41:27 +01:00
|
|
|
if ( !d->globalConfig ) {
|
|
|
|
QString file = KStandardDirs::locateLocal( "config", "plasma_" + globalName() + "rc" );
|
|
|
|
d->globalConfig = KSharedConfig::openConfig( file );
|
2005-12-29 22:55:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return d->globalConfig;
|
|
|
|
}
|
|
|
|
|
2007-03-03 02:41:27 +01:00
|
|
|
bool Applet::loadDataEngine( const QString& name )
|
2005-12-29 22:55:22 +01:00
|
|
|
{
|
2007-03-03 02:41:27 +01:00
|
|
|
if ( d->loadedEngines.indexOf( name ) != -1 ) {
|
2006-04-13 02:11:16 +02:00
|
|
|
return true;
|
2005-12-29 22:55:22 +01:00
|
|
|
}
|
|
|
|
|
2007-03-03 02:41:27 +01:00
|
|
|
if ( Plasma::Interface::self()->loadDataEngine( name ) ) {
|
|
|
|
d->loadedEngines.append( name );
|
2006-04-13 02:11:16 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2005-12-29 22:55:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Applet::constraintsUpdated()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Applet::globalName() const
|
|
|
|
{
|
|
|
|
return d->appletDescription->library();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Applet::instanceName() const
|
|
|
|
{
|
2007-03-03 02:41:27 +01:00
|
|
|
return d->appletDescription->library() + QString::number( d->id );
|
2005-12-29 22:55:22 +01:00
|
|
|
}
|
|
|
|
|
2007-03-01 02:09:20 +01:00
|
|
|
void Applet::watchForFocus(QObject *widget, bool watch)
|
2005-12-29 22:55:22 +01:00
|
|
|
{
|
2007-03-03 02:41:27 +01:00
|
|
|
if ( !widget ) {
|
2005-12-29 22:55:22 +01:00
|
|
|
return;
|
2007-03-03 02:41:27 +01:00
|
|
|
}
|
2005-12-29 22:55:22 +01:00
|
|
|
|
|
|
|
int index = d->watchedForFocus.indexOf(widget);
|
2007-03-03 02:41:27 +01:00
|
|
|
if ( watch ) {
|
|
|
|
if ( index == -1 ) {
|
|
|
|
d->watchedForFocus.append( widget );
|
|
|
|
widget->installEventFilter( this );
|
2005-12-29 22:55:22 +01:00
|
|
|
}
|
2007-03-03 02:41:27 +01:00
|
|
|
} else if ( index != -1 ) {
|
|
|
|
d->watchedForFocus.removeAt( index );
|
|
|
|
widget->removeEventFilter( this );
|
2005-12-29 22:55:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-03 02:41:27 +01:00
|
|
|
void Applet::needsFocus( bool focus )
|
2005-12-29 22:55:22 +01:00
|
|
|
{
|
2007-03-03 02:41:27 +01:00
|
|
|
if ( focus == QWidget::hasFocus() ||
|
|
|
|
focus == QGraphicsItem::hasFocus() ) {
|
2005-12-29 22:55:22 +01:00
|
|
|
return;
|
2007-03-03 02:41:27 +01:00
|
|
|
}
|
2005-12-29 22:55:22 +01:00
|
|
|
|
|
|
|
emit requestFocus(focus);
|
|
|
|
}
|
|
|
|
|
2007-03-03 02:41:27 +01:00
|
|
|
bool Applet::eventFilter( QObject *o, QEvent * e )
|
2005-12-29 22:55:22 +01:00
|
|
|
{
|
2007-03-03 02:41:27 +01:00
|
|
|
if ( !d->watchedForFocus.contains( o ) )
|
2005-12-29 22:55:22 +01:00
|
|
|
{
|
2007-03-03 02:41:27 +01:00
|
|
|
if ( e->type() == QEvent::MouseButtonRelease ||
|
|
|
|
e->type() == QEvent::FocusIn ) {
|
|
|
|
needsFocus( true );
|
|
|
|
} else if ( e->type() == QEvent::FocusOut ) {
|
|
|
|
needsFocus( false );
|
2005-12-29 22:55:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-03 02:41:27 +01:00
|
|
|
return QWidget::eventFilter( o, e );
|
2005-12-29 22:55:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // Plasma namespace
|
|
|
|
|
|
|
|
#include "applet.moc"
|