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 <tqfile.h>
00025 #include <tqlabel.h>
00026 #include <tqlayout.h>
00027 #include <tqpushbutton.h>
00028
00029 #include <tdeapplication.h>
00030 #include <kcombobox.h>
00031 #include <kdialog.h>
00032 #include <tdefiledialog.h>
00033 #include <tdeio/netaccess.h>
00034 #include <kinputdialog.h>
00035 #include <tdelocale.h>
00036 #include <tdemessagebox.h>
00037 #include <tdetempfile.h>
00038
00039 #include "keywidget.h"
00040
00041 KeyWidget::KeyWidget( TQWidget *parent, const char *name )
00042 : TQWidget( parent, name )
00043 {
00044 TQGridLayout *layout = new TQGridLayout( this, 4, 2, KDialog::marginHint(),
00045 KDialog::spacingHint() );
00046
00047 TQLabel *label = new TQLabel( 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 TQPushButton( i18n( "Add..." ), this );
00054 layout->addMultiCellWidget( mAddButton, 1, 1, 0, 1 );
00055
00056 mRemoveButton = new TQPushButton( i18n( "Remove" ), this );
00057 mRemoveButton->setEnabled( false );
00058 layout->addMultiCellWidget( mRemoveButton, 2, 2, 0, 1 );
00059
00060 mExportButton = new TQPushButton( i18n( "Export..." ), this );
00061 mExportButton->setEnabled( false );
00062 layout->addMultiCellWidget( mExportButton, 3, 3, 0, 1 );
00063
00064 connect( mAddButton, TQT_SIGNAL( clicked() ), TQT_SLOT( addKey() ) );
00065 connect( mRemoveButton, TQT_SIGNAL( clicked() ), TQT_SLOT( removeKey() ) );
00066 connect( mExportButton, TQT_SIGNAL( clicked() ), TQT_SLOT( exportKey() ) );
00067 }
00068
00069 KeyWidget::~KeyWidget()
00070 {
00071 }
00072
00073 void KeyWidget::setKeys( const TDEABC::Key::List &list )
00074 {
00075 mKeyList = list;
00076
00077 updateKeyCombo();
00078 }
00079
00080 TDEABC::Key::List KeyWidget::keys() const
00081 {
00082 return mKeyList;
00083 }
00084
00085 void KeyWidget::addKey()
00086 {
00087 TQMap<TQString, int> keyMap;
00088 TQStringList keyTypeNames;
00089 TQStringList existingKeyTypes;
00090
00091 TDEABC::Key::List::ConstIterator listIt;
00092 for ( listIt = mKeyList.begin(); listIt != mKeyList.end(); ++listIt ) {
00093 if ( (*listIt).type() != TDEABC::Key::Custom )
00094 existingKeyTypes.append( TDEABC::Key::typeLabel( (*listIt).type() ) );
00095 }
00096
00097 TDEABC::Key::TypeList typeList = TDEABC::Key::typeList();
00098 TDEABC::Key::TypeList::ConstIterator it;
00099 for ( it = typeList.begin(); it != typeList.end(); ++it ) {
00100 if ( (*it) != TDEABC::Key::Custom &&
00101 !existingKeyTypes.contains( TDEABC::Key::typeLabel( *it ) ) ) {
00102 keyMap.insert( TDEABC::Key::typeLabel( *it ), *it );
00103 keyTypeNames.append( TDEABC::Key::typeLabel( *it ) );
00104 }
00105 }
00106
00107 bool ok;
00108 TQString 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 = TDEABC::Key::Custom;
00115
00116 KURL url = KFileDialog::getOpenURL();
00117 if ( url.isEmpty() )
00118 return;
00119
00120 TQString tmpFile;
00121 if ( TDEIO::NetAccess::download( url, tmpFile, this ) ) {
00122 TQFile file( tmpFile );
00123 if ( !file.open( IO_ReadOnly ) ) {
00124 TQString text( i18n( "<qt>Unable to open file <b>%1</b>.</qt>" ) );
00125 KMessageBox::error( this, text.arg( url.url() ) );
00126 return;
00127 }
00128
00129 TQTextStream s( &file );
00130 TQString data;
00131
00132 s.setEncoding( TQTextStream::UnicodeUTF8 );
00133 s >> data;
00134 file.close();
00135
00136 TDEABC::Key key( data, type );
00137 if ( type == TDEABC::Key::Custom )
00138 key.setCustomTypeString( name );
00139 mKeyList.append( key );
00140
00141 emit changed();
00142
00143 TDEIO::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 TQString type = mKeyCombo->currentText();
00156 TQString 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"), "edit-delete") ) == 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 TDEABC::Key key = (*mKeyList.at( mKeyCombo->currentItem() ) );
00169
00170 KURL url = KFileDialog::getSaveURL();
00171
00172 KTempFile tempFile;
00173 TQTextStream *s = tempFile.textStream();
00174 s->setEncoding( TQTextStream::UnicodeUTF8 );
00175 (*s) << key.textData();
00176 tempFile.close();
00177
00178 TDEIO::NetAccess::upload( tempFile.name(), url, kapp->mainWidget() );
00179 }
00180
00181 void KeyWidget::updateKeyCombo()
00182 {
00183 int pos = mKeyCombo->currentItem();
00184 mKeyCombo->clear();
00185
00186 TDEABC::Key::List::ConstIterator it;
00187 for ( it = mKeyList.begin(); it != mKeyList.end(); ++it ) {
00188 if ( (*it).type() == TDEABC::Key::Custom )
00189 mKeyCombo->insertItem( (*it).customTypeString() );
00190 else
00191 mKeyCombo->insertItem( TDEABC::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"