resourcefileconfig.cpp

00001 /*
00002     This file is part of libkabc.
00003     Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018     Boston, MA 02110-1301, USA.
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   // If file exist but is not writeable...
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"
KDE Home | KDE Accessibility Home | Description of Access Keys