The KDE Resource library
The KDE Resource framework can be used to manage resources of different types, organized in families. The Resource framework is for example used for addressbook resources in libtdeabc and for calendar resources in libkcal.
When you want to use the framework for a new family, you need to
- Define a name for your resource family
- subclass Resource and add the fields and method that are needed in your application
- If needed, override the doOpen() and doClose() methods.
- In your application, you can use ResourceManager to keep track of the resources in your family, and you can use ResourceSelectDialog to let the user select a single resource.
When you want to add a new resource type to an existing resource family, you need to
- Further subclass the family-specific Resource to implement resource type-specific operation
- Subclass ResourceConfigWidget to provide a configuration widget for your new resource type
- Provide a .desktop file so that the new resource type can be found automatically by the ResourceManager
Example:
resourceexample.h:
#include <tdeconfig.h>
#include <tderesources/resource.h>
{
public:
ResourceExample( const TDEConfig * );
~ResourceExample();
void writeConfig( TDEConfig *config );
private:
TQString mLocation;
TQString mPassword;
}
This class provides a resource which is managed in a general way.
Definition: resource.h:256
resourceexample.cpp:
#include <tdeconfig.h>
#include "resourceexample.h"
ResourceExample::ResourceExample( const TDEConfig *config )
: Resource( config )
{
if ( config ) {
mLocation = config->readPathEntry( "Location" );
mPassword = KStringHandler::obscure( config->readEntry( "Password" ) );
} else {
mLocation = ""; // Or some sensible default
mPassword = "";
}
}
void ResourceExample::writeConfig( TDEConfig *config )
{
KRES::Resource::writeConfig( config );
config->writePathEntry( "Location", mLocation );
config->writeEntry( "Password", KStringHandler::obscure( mPassword ) );
}
extern "C"
{
KRES::ResourceExample *config_widget( TQWidget *parent ) {
return new ResourceExampleConfig( parent, "Configure Example Resource" );
}
KRES::Resource *resource( const TDEConfig *config ) {
return new ResourceExample( config );
}
}
virtual void writeConfig(TDEConfig *config)
Write configuration information for this resource to a configuration file.
Definition: resource.cpp:74
resourceexampleconfig.h:
#include <klineedit.h>
#include <tderesources/resourceconfigwidget.h>
#include "resourceexample.h"
class ResourceExampleConfig : public KRES::ResourceConfigWidget
{
TQ_OBJECT
public:
ResourceExampleConfig( TQWidget *parent = 0, const char *name = 0 );
public slots:
private:
KLineEdit *mLocationEdit;
KLineEdit *mPasswordEdit;
};
resourceexampleconfig.cpp:
#include <tqlayout.h>
#include <tqlabel.h>
#include <tderesources/resourceconfigwidget.h>
#include "resourceexample.h"
#include "resourceexampleconfig.h"
ResourceExampleConfig::ResourceExampleConfig( TQWidget *parent, const char *name )
: KRES::ResourceConfigWidget( parent, name )
{
TQGridLayout *mainLayout = new TQGridLayout( this, 2, 2 );
TQLabel *label = new TQLabel( i18n( "Location:" ), this );
mHostEdit = new KLineEdit( this );
mainLayout->addWidget( label, 1, 0 );
mainLayout->addWidget( mHostEdit, 1, 1 );
label = new TQLabel( i18n( "Password:" ), this );
mPasswordEdit = new KLineEdit( this );
mPasswordEdit->setEchoMode( TQLineEdit::Password );
mainLayout->addWidget( label, 2, 0 );
mainLayout->addWidget( mPasswordEdit, 2, 1 );
}
void ResourceExampleConfig::loadSettings( KRES::Resource *resource )
{
ResourceExample *res = dynamic_cast<ResourceExample *>( resource );
if ( res ) {
mHostEdit->setText( res->host() );
mPasswordEdit->setText( res->password() );
} else
kdDebug() << "ERROR: ResourceExampleConfig::loadSettings(): no ResourceExample, cast failed" << endl;
}
void ResourceExampleConfig::saveSettings( KRES::Resource *resource )
{
ResourceExample *res = dynamic_cast<ResourceExample *>( resource );
if ( res ) {
res->setHost( mHostEdit->text() );
res->setPassword( mPasswordEdit->text() );
} else
kdDebug() << "ERROR: ResourceExampleConfig::saveSettings(): no ResourceExample, cast failed" << endl;
}
resourceexample.desktop:
[Desktop Entry]
Type=Service
[Misc]
Encoding=UTF-8
Name=Example Resource
[Plugin]
Type=exchange
X-TDE-Library=resourceexample
Makefile.am
kde_module_LTLIBRARIES = resourceexample.la
resourceexample_la_SOURCES = resourceexample.cpp resourceexampleconfig.cpp
resourceexample_la_LDFLAGS = $(all_libraries) -module $(KDE_PLUGIN)
resourceexample_la_LIBADD = -ltderesources
servicedir = $(kde_datadir)/resources/example
service_DATA = resourceexample.desktop