00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <qfile.h>
00025 #include <qlabel.h>
00026 #include <qlayout.h>
00027 #include <qpushbutton.h>
00028
00029 #include <kapplication.h>
00030 #include <kcombobox.h>
00031 #include <kdialog.h>
00032 #include <kfiledialog.h>
00033 #include <kio/netaccess.h>
00034 #include <kinputdialog.h>
00035 #include <klocale.h>
00036 #include <kmessagebox.h>
00037 #include <ktempfile.h>
00038
00039 #include "keywidget.h"
00040
00041 KeyWidget::KeyWidget( QWidget *parent, const char *name )
00042 : QWidget( parent, name )
00043 {
00044 QGridLayout *layout = new QGridLayout( this, 4, 2, KDialog::marginHint(),
00045 KDialog::spacingHint() );
00046
00047 QLabel *label = new QLabel( i18n( "Keys:" ), this );
00048 layout->addWidget( label, 0, 0 );
00049
00050 mKeyCombo = new KComboBox( this );
00051 layout->addWidget( mKeyCombo, 0, 1 );
00052
00053 mAddButton = new QPushButton( i18n( "Add..." ), this );
00054 layout->addMultiCellWidget( mAddButton, 1, 1, 0, 1 );
00055
00056 mRemoveButton = new QPushButton( i18n( "Remove" ), this );
00057 mRemoveButton->setEnabled( false );
00058 layout->addMultiCellWidget( mRemoveButton, 2, 2, 0, 1 );
00059
00060 mExportButton = new QPushButton( i18n( "Export..." ), this );
00061 mExportButton->setEnabled( false );
00062 layout->addMultiCellWidget( mExportButton, 3, 3, 0, 1 );
00063
00064 connect( mAddButton, SIGNAL( clicked() ), SLOT( addKey() ) );
00065 connect( mRemoveButton, SIGNAL( clicked() ), SLOT( removeKey() ) );
00066 connect( mExportButton, SIGNAL( clicked() ), SLOT( exportKey() ) );
00067 }
00068
00069 KeyWidget::~KeyWidget()
00070 {
00071 }
00072
00073 void KeyWidget::setKeys( const KABC::Key::List &list )
00074 {
00075 mKeyList = list;
00076
00077 updateKeyCombo();
00078 }
00079
00080 KABC::Key::List KeyWidget::keys() const
00081 {
00082 return mKeyList;
00083 }
00084
00085 void KeyWidget::addKey()
00086 {
00087 QMap<QString, int> keyMap;
00088 QStringList keyTypeNames;
00089 QStringList existingKeyTypes;
00090
00091 KABC::Key::List::ConstIterator listIt;
00092 for ( listIt = mKeyList.begin(); listIt != mKeyList.end(); ++listIt ) {
00093 if ( (*listIt).type() != KABC::Key::Custom )
00094 existingKeyTypes.append( KABC::Key::typeLabel( (*listIt).type() ) );
00095 }
00096
00097 KABC::Key::TypeList typeList = KABC::Key::typeList();
00098 KABC::Key::TypeList::ConstIterator it;
00099 for ( it = typeList.begin(); it != typeList.end(); ++it ) {
00100 if ( (*it) != KABC::Key::Custom &&
00101 !existingKeyTypes.contains( KABC::Key::typeLabel( *it ) ) ) {
00102 keyMap.insert( KABC::Key::typeLabel( *it ), *it );
00103 keyTypeNames.append( KABC::Key::typeLabel( *it ) );
00104 }
00105 }
00106
00107 bool ok;
00108 QString name = KInputDialog::getItem( i18n( "Key Type" ), i18n( "Select the key type:" ), keyTypeNames, 0, true, &ok );
00109 if ( !ok || name.isEmpty() )
00110 return;
00111
00112 int type = keyMap[ name ];
00113 if ( !keyTypeNames.contains( name ) )
00114 type = KABC::Key::Custom;
00115
00116 KURL url = KFileDialog::getOpenURL();
00117 if ( url.isEmpty() )
00118 return;
00119
00120 QString tmpFile;
00121 if ( KIO::NetAccess::download( url, tmpFile, this ) ) {
00122 QFile file( tmpFile );
00123 if ( !file.open( IO_ReadOnly ) ) {
00124 QString text( i18n( "<qt>Unable to open file <b>%1</b>.</qt>" ) );
00125 KMessageBox::error( this, text.arg( url.url() ) );
00126 return;
00127 }
00128
00129 QTextStream s( &file );
00130 QString data;
00131
00132 s.setEncoding( QTextStream::UnicodeUTF8 );
00133 s >> data;
00134 file.close();
00135
00136 KABC::Key key( data, type );
00137 if ( type == KABC::Key::Custom )
00138 key.setCustomTypeString( name );
00139 mKeyList.append( key );
00140
00141 emit changed();
00142
00143 KIO::NetAccess::removeTempFile( tmpFile );
00144 }
00145
00146 updateKeyCombo();
00147 }
00148
00149 void KeyWidget::removeKey()
00150 {
00151 int pos = mKeyCombo->currentItem();
00152 if ( pos == -1 )
00153 return;
00154
00155 QString type = mKeyCombo->currentText();
00156 QString text = i18n( "<qt>Do you really want to remove the key <b>%1</b>?</qt>" );
00157 if ( KMessageBox::warningContinueCancel( this, text.arg( type ), "", KGuiItem( i18n("&Delete"), "editdelete") ) == KMessageBox::Cancel )
00158 return;
00159
00160 mKeyList.remove( mKeyList.at( pos ) );
00161 emit changed();
00162
00163 updateKeyCombo();
00164 }
00165
00166 void KeyWidget::exportKey()
00167 {
00168 KABC::Key key = (*mKeyList.at( mKeyCombo->currentItem() ) );
00169
00170 KURL url = KFileDialog::getSaveURL();
00171
00172 KTempFile tempFile;
00173 QTextStream *s = tempFile.textStream();
00174 s->setEncoding( QTextStream::UnicodeUTF8 );
00175 (*s) << key.textData();
00176 tempFile.close();
00177
00178 KIO::NetAccess::upload( tempFile.name(), url, kapp->mainWidget() );
00179 }
00180
00181 void KeyWidget::updateKeyCombo()
00182 {
00183 int pos = mKeyCombo->currentItem();
00184 mKeyCombo->clear();
00185
00186 KABC::Key::List::ConstIterator it;
00187 for ( it = mKeyList.begin(); it != mKeyList.end(); ++it ) {
00188 if ( (*it).type() == KABC::Key::Custom )
00189 mKeyCombo->insertItem( (*it).customTypeString() );
00190 else
00191 mKeyCombo->insertItem( KABC::Key::typeLabel( (*it).type() ) );
00192 }
00193
00194 mKeyCombo->setCurrentItem( pos );
00195
00196 bool state = ( mKeyList.count() != 0 );
00197 mRemoveButton->setEnabled( state );
00198 mExportButton->setEnabled( state );
00199 }
00200
00201 #include "keywidget.moc"