kaddressbook

keywidget.cpp
1/*
2 This file is part of KAddressBook.
3 Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 As a special exception, permission is given to link this program
20 with any edition of TQt, and distribute the resulting executable,
21 without including the source code for TQt in the source distribution.
22*/
23
24#include <tqfile.h>
25#include <tqlabel.h>
26#include <tqlayout.h>
27#include <tqpushbutton.h>
28
29#include <tdeapplication.h>
30#include <kcombobox.h>
31#include <kdialog.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>
38
39#include "keywidget.h"
40
41KeyWidget::KeyWidget( TQWidget *parent, const char *name )
42 : TQWidget( parent, name )
43{
44 TQGridLayout *layout = new TQGridLayout( this, 4, 2, KDialog::marginHint(),
45 KDialog::spacingHint() );
46
47 TQLabel *label = new TQLabel( i18n( "Keys:" ), this );
48 layout->addWidget( label, 0, 0 );
49
50 mKeyCombo = new KComboBox( this );
51 layout->addWidget( mKeyCombo, 0, 1 );
52
53 mAddButton = new TQPushButton( i18n( "Add..." ), this );
54 layout->addMultiCellWidget( mAddButton, 1, 1, 0, 1 );
55
56 mRemoveButton = new TQPushButton( i18n( "Remove" ), this );
57 mRemoveButton->setEnabled( false );
58 layout->addMultiCellWidget( mRemoveButton, 2, 2, 0, 1 );
59
60 mExportButton = new TQPushButton( i18n( "Export..." ), this );
61 mExportButton->setEnabled( false );
62 layout->addMultiCellWidget( mExportButton, 3, 3, 0, 1 );
63
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() ) );
67}
68
69KeyWidget::~KeyWidget()
70{
71}
72
73void KeyWidget::setKeys( const TDEABC::Key::List &list )
74{
75 mKeyList = list;
76
77 updateKeyCombo();
78}
79
80TDEABC::Key::List KeyWidget::keys() const
81{
82 return mKeyList;
83}
84
85void KeyWidget::addKey()
86{
87 TQMap<TQString, int> keyMap;
88 TQStringList keyTypeNames;
89 TQStringList existingKeyTypes;
90
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() ) );
95 }
96
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 ) );
104 }
105 }
106
107 bool ok;
108 TQString name = KInputDialog::getItem( i18n( "Key Type" ), i18n( "Select the key type:" ), keyTypeNames, 0, true, &ok );
109 if ( !ok || name.isEmpty() )
110 return;
111
112 int type = keyMap[ name ];
113 if ( !keyTypeNames.contains( name ) )
114 type = TDEABC::Key::Custom;
115
116 KURL url = KFileDialog::getOpenURL();
117 if ( url.isEmpty() )
118 return;
119
120 TQString tmpFile;
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() ) );
126 return;
127 }
128
129 TQTextStream s( &file );
130 TQString data;
131
132 s.setEncoding( TQTextStream::UnicodeUTF8 );
133 s >> data;
134 file.close();
135
136 TDEABC::Key key( data, type );
137 if ( type == TDEABC::Key::Custom )
138 key.setCustomTypeString( name );
139 mKeyList.append( key );
140
141 emit changed();
142
143 TDEIO::NetAccess::removeTempFile( tmpFile );
144 }
145
146 updateKeyCombo();
147}
148
149void KeyWidget::removeKey()
150{
151 int pos = mKeyCombo->currentItem();
152 if ( pos == -1 )
153 return;
154
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 )
158 return;
159
160 mKeyList.remove( mKeyList.at( pos ) );
161 emit changed();
162
163 updateKeyCombo();
164}
165
166void KeyWidget::exportKey()
167{
168 TDEABC::Key key = (*mKeyList.at( mKeyCombo->currentItem() ) );
169
170 KURL url = KFileDialog::getSaveURL();
171
172 KTempFile tempFile;
173 TQTextStream *s = tempFile.textStream();
174 s->setEncoding( TQTextStream::UnicodeUTF8 );
175 (*s) << key.textData();
176 tempFile.close();
177
178 TDEIO::NetAccess::upload( tempFile.name(), url, tdeApp->mainWidget() );
179}
180
181void KeyWidget::updateKeyCombo()
182{
183 int pos = mKeyCombo->currentItem();
184 mKeyCombo->clear();
185
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() );
190 else
191 mKeyCombo->insertItem( TDEABC::Key::typeLabel( (*it).type() ) );
192 }
193
194 mKeyCombo->setCurrentItem( pos );
195
196 bool state = ( mKeyList.count() != 0 );
197 mRemoveButton->setEnabled( state );
198 mExportButton->setEnabled( state );
199}
200
201#include "keywidget.moc"