resourcefileconfig.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <qlabel.h>
00022 #include <qlayout.h>
00023
00024 #include <kdebug.h>
00025 #include <klocale.h>
00026 #include <kstandarddirs.h>
00027 #include <kdialog.h>
00028
00029 #include <unistd.h>
00030
00031 #include "formatfactory.h"
00032 #include "resourcefile.h"
00033 #include "stdaddressbook.h"
00034
00035 #include "resourcefileconfig.h"
00036
00037 using namespace KABC;
00038
00039 ResourceFileConfig::ResourceFileConfig( QWidget* parent, const char* name )
00040 : ConfigWidget( parent, name )
00041 {
00042 QGridLayout *mainLayout = new QGridLayout( this, 2, 2, 0,
00043 KDialog::spacingHint() );
00044
00045 QLabel *label = new QLabel( i18n( "Format:" ), this );
00046 mFormatBox = new KComboBox( this );
00047
00048 mainLayout->addWidget( label, 0, 0 );
00049 mainLayout->addWidget( mFormatBox, 0, 1 );
00050
00051 label = new QLabel( i18n( "Location:" ), this );
00052 mFileNameEdit = new KURLRequester( this );
00053
00054 connect( mFileNameEdit, SIGNAL( textChanged( const QString & ) ),
00055 SLOT( checkFilePermissions( const QString & ) ) );
00056
00057 mainLayout->addWidget( label, 1, 0 );
00058 mainLayout->addWidget( mFileNameEdit, 1, 1 );
00059
00060 FormatFactory *factory = FormatFactory::self();
00061 QStringList formats = factory->formats();
00062 QStringList::Iterator it;
00063 for ( it = formats.begin(); it != formats.end(); ++it ) {
00064 FormatInfo *info = factory->info( *it );
00065 if ( info ) {
00066 mFormatTypes << (*it);
00067 mFormatBox->insertItem( info->nameLabel );
00068 }
00069 }
00070
00071 mInEditMode = false;
00072 }
00073
00074 void ResourceFileConfig::setEditMode( bool value )
00075 {
00076 mFormatBox->setEnabled( !value );
00077 mInEditMode = value;
00078 }
00079
00080 void ResourceFileConfig::loadSettings( KRES::Resource *res )
00081 {
00082 ResourceFile *resource = dynamic_cast<ResourceFile*>( res );
00083
00084 if ( !resource ) {
00085 kdDebug(5700) << "ResourceFileConfig::loadSettings(): cast failed" << endl;
00086 return;
00087 }
00088
00089 mFormatBox->setCurrentItem( mFormatTypes.findIndex( resource->format() ) );
00090
00091 mFileNameEdit->setURL( resource->fileName() );
00092 if ( mFileNameEdit->url().isEmpty() )
00093 mFileNameEdit->setURL( KABC::StdAddressBook::fileName() );
00094 }
00095
00096 void ResourceFileConfig::saveSettings( KRES::Resource *res )
00097 {
00098 ResourceFile *resource = dynamic_cast<ResourceFile*>( res );
00099
00100 if ( !resource ) {
00101 kdDebug(5700) << "ResourceFileConfig::saveSettings(): cast failed" << endl;
00102 return;
00103 }
00104
00105 if ( !mInEditMode )
00106 resource->setFormat( mFormatTypes[ mFormatBox->currentItem() ] );
00107
00108 resource->setFileName( mFileNameEdit->url() );
00109 }
00110
00111 void ResourceFileConfig::checkFilePermissions( const QString& fileName )
00112 {
00113
00114 if ( access( QFile::encodeName( fileName ), F_OK ) == 0 )
00115 emit setReadOnly( access( QFile::encodeName( fileName ), W_OK ) < 0 );
00116 }
00117
00118 #include "resourcefileconfig.moc"
|