27 #include <tqpushbutton.h>
29 #include <tdeapplication.h>
30 #include <kcombobox.h>
32 #include <tdefiledialog.h>
33 #include <tdeio/netaccess.h>
34 #include <kinputdialog.h>
35 #include <tdelocale.h>
36 #include <tdemessagebox.h>
37 #include <tdetempfile.h>
39 #include "keywidget.h"
41 KeyWidget::KeyWidget( TQWidget *parent,
const char *name )
42 : TQWidget( parent, name )
44 TQGridLayout *layout =
new TQGridLayout(
this, 4, 2, KDialog::marginHint(),
45 KDialog::spacingHint() );
47 TQLabel *label =
new TQLabel( i18n(
"Keys:" ),
this );
48 layout->addWidget( label, 0, 0 );
50 mKeyCombo =
new KComboBox(
this );
51 layout->addWidget( mKeyCombo, 0, 1 );
53 mAddButton =
new TQPushButton( i18n(
"Add..." ),
this );
54 layout->addMultiCellWidget( mAddButton, 1, 1, 0, 1 );
56 mRemoveButton =
new TQPushButton( i18n(
"Remove" ),
this );
57 mRemoveButton->setEnabled(
false );
58 layout->addMultiCellWidget( mRemoveButton, 2, 2, 0, 1 );
60 mExportButton =
new TQPushButton( i18n(
"Export..." ),
this );
61 mExportButton->setEnabled(
false );
62 layout->addMultiCellWidget( mExportButton, 3, 3, 0, 1 );
64 connect( mAddButton, TQ_SIGNAL( clicked() ), TQ_SLOT( addKey() ) );
65 connect( mRemoveButton, TQ_SIGNAL( clicked() ), TQ_SLOT( removeKey() ) );
66 connect( mExportButton, TQ_SIGNAL( clicked() ), TQ_SLOT( exportKey() ) );
69 KeyWidget::~KeyWidget()
73 void KeyWidget::setKeys(
const TDEABC::Key::List &list )
80 TDEABC::Key::List KeyWidget::keys()
const
85 void KeyWidget::addKey()
87 TQMap<TQString, int> keyMap;
88 TQStringList keyTypeNames;
89 TQStringList existingKeyTypes;
91 TDEABC::Key::List::ConstIterator listIt;
92 for ( listIt = mKeyList.begin(); listIt != mKeyList.end(); ++listIt ) {
93 if ( (*listIt).type() != TDEABC::Key::Custom )
94 existingKeyTypes.append( TDEABC::Key::typeLabel( (*listIt).type() ) );
97 TDEABC::Key::TypeList typeList = TDEABC::Key::typeList();
98 TDEABC::Key::TypeList::ConstIterator it;
99 for ( it = typeList.begin(); it != typeList.end(); ++it ) {
100 if ( (*it) != TDEABC::Key::Custom &&
101 !existingKeyTypes.contains( TDEABC::Key::typeLabel( *it ) ) ) {
102 keyMap.insert( TDEABC::Key::typeLabel( *it ), *it );
103 keyTypeNames.append( TDEABC::Key::typeLabel( *it ) );
108 TQString name = KInputDialog::getItem( i18n(
"Key Type" ), i18n(
"Select the key type:" ), keyTypeNames, 0,
true, &ok );
109 if ( !ok || name.isEmpty() )
112 int type = keyMap[ name ];
113 if ( !keyTypeNames.contains( name ) )
114 type = TDEABC::Key::Custom;
116 KURL url = KFileDialog::getOpenURL();
121 if ( TDEIO::NetAccess::download( url, tmpFile,
this ) ) {
122 TQFile file( tmpFile );
123 if ( !file.open( IO_ReadOnly ) ) {
124 TQString text( i18n(
"<qt>Unable to open file <b>%1</b>.</qt>" ) );
125 KMessageBox::error(
this, text.arg( url.url() ) );
129 TQTextStream s( &file );
132 s.setEncoding( TQTextStream::UnicodeUTF8 );
136 TDEABC::Key key( data, type );
137 if ( type == TDEABC::Key::Custom )
138 key.setCustomTypeString( name );
139 mKeyList.append( key );
143 TDEIO::NetAccess::removeTempFile( tmpFile );
149 void KeyWidget::removeKey()
151 int pos = mKeyCombo->currentItem();
155 TQString type = mKeyCombo->currentText();
156 TQString text = i18n(
"<qt>Do you really want to remove the key <b>%1</b>?</qt>" );
157 if ( KMessageBox::warningContinueCancel(
this, text.arg( type ),
"", KGuiItem( i18n(
"&Delete"),
"edit-delete") ) == KMessageBox::Cancel )
160 mKeyList.remove( mKeyList.at( pos ) );
166 void KeyWidget::exportKey()
168 TDEABC::Key key = (*mKeyList.at( mKeyCombo->currentItem() ) );
170 KURL url = KFileDialog::getSaveURL();
173 TQTextStream *s = tempFile.textStream();
174 s->setEncoding( TQTextStream::UnicodeUTF8 );
175 (*s) << key.textData();
178 TDEIO::NetAccess::upload( tempFile.name(), url, kapp->mainWidget() );
181 void KeyWidget::updateKeyCombo()
183 int pos = mKeyCombo->currentItem();
186 TDEABC::Key::List::ConstIterator it;
187 for ( it = mKeyList.begin(); it != mKeyList.end(); ++it ) {
188 if ( (*it).type() == TDEABC::Key::Custom )
189 mKeyCombo->insertItem( (*it).customTypeString() );
191 mKeyCombo->insertItem( TDEABC::Key::typeLabel( (*it).type() ) );
194 mKeyCombo->setCurrentItem( pos );
196 bool state = ( mKeyList.count() != 0 );
197 mRemoveButton->setEnabled( state );
198 mExportButton->setEnabled( state );
201 #include "keywidget.moc"