-rw-r--r-- | kaddressbook/keywidget.cpp | 28 |
1 files changed, 15 insertions, 13 deletions
diff --git a/kaddressbook/keywidget.cpp b/kaddressbook/keywidget.cpp index c117d34..8ebee19 100644 --- a/kaddressbook/keywidget.cpp +++ b/kaddressbook/keywidget.cpp | |||
@@ -1,277 +1,279 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of KAddressBook. | 2 | This file is part of KAddressBook. |
3 | Copyright (c) 2003 Tobias Koenig <tokoe@kde.org> | 3 | Copyright (c) 2003 Tobias Koenig <tokoe@kde.org> |
4 | 4 | ||
5 | This program is free software; you can redistribute it and/or modify | 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 | 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 | 7 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. | 8 | (at your option) any later version. |
9 | 9 | ||
10 | This program is distributed in the hope that it will be useful, | 10 | This program is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | GNU General Public License for more details. | 13 | GNU General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU General Public License | 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 | 16 | along with this program; if not, write to the Free Software |
17 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | 17 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 | 18 | ||
19 | As a special exception, permission is given to link this program | 19 | As a special exception, permission is given to link this program |
20 | with any edition of Qt, and distribute the resulting executable, | 20 | with any edition of Qt, and distribute the resulting executable, |
21 | without including the source code for Qt in the source distribution. | 21 | without including the source code for Qt in the source distribution. |
22 | */ | 22 | */ |
23 | 23 | ||
24 | #include <qfile.h> | 24 | #include <qfile.h> |
25 | 25 | ||
26 | #include <qinputdialog.h> | 26 | #include <qinputdialog.h> |
27 | 27 | ||
28 | #include <qlabel.h> | 28 | #include <qlabel.h> |
29 | #include <qlayout.h> | 29 | #include <qlayout.h> |
30 | #include <qpushbutton.h> | 30 | #include <qpushbutton.h> |
31 | //Added by qt3to4: | ||
32 | #include <Q3GridLayout> | ||
31 | 33 | ||
32 | #ifndef KAB_EMBEDDED | 34 | #ifndef KAB_EMBEDDED |
33 | #include <kaccelmanager.h> | 35 | #include <kaccelmanager.h> |
34 | #include <kio/netaccess.h> | 36 | #include <kio/netaccess.h> |
35 | #include <kmessagebox.h> | 37 | #include <kmessagebox.h> |
36 | #else //KAB_EMBEDDED | 38 | #else //KAB_EMBEDDED |
37 | #include <qmap.h> | 39 | #include <qmap.h> |
38 | #include <qmessagebox.h> | 40 | #include <qmessagebox.h> |
39 | #include <qtextstream.h> | 41 | #include <q3textstream.h> |
40 | #include <kurl.h> | 42 | #include <kurl.h> |
41 | #endif //KAB_EMBEDDED | 43 | #endif //KAB_EMBEDDED |
42 | 44 | ||
43 | #include <kapplication.h> | 45 | #include <kapplication.h> |
44 | #include <kcombobox.h> | 46 | #include <kcombobox.h> |
45 | #include <kdialog.h> | 47 | #include <kdialog.h> |
46 | #include <kfiledialog.h> | 48 | #include <kfiledialog.h> |
47 | #include <klocale.h> | 49 | #include <klocale.h> |
48 | #include <ktempfile.h> | 50 | #include <ktempfile.h> |
49 | 51 | ||
50 | #include "keywidget.h" | 52 | #include "keywidget.h" |
51 | 53 | ||
52 | KeyWidget::KeyWidget( QWidget *parent, const char *name ) | 54 | KeyWidget::KeyWidget( QWidget *parent, const char *name ) |
53 | : QWidget( parent, name ) | 55 | : QWidget( parent, name ) |
54 | { | 56 | { |
55 | QGridLayout *layout = new QGridLayout( this, 2, 2, KDialog::marginHint(), | 57 | Q3GridLayout *layout = new Q3GridLayout( this, 2, 2, KDialog::marginHint(), |
56 | KDialog::spacingHint() ); | 58 | KDialog::spacingHint() ); |
57 | 59 | ||
58 | QLabel *label = new QLabel( i18n( "Keys:" ), this ); | 60 | QLabel *label = new QLabel( i18n( "Keys:" ), this ); |
59 | layout->addWidget( label, 0, 0 ); | 61 | layout->addWidget( label, 0, 0 ); |
60 | 62 | ||
61 | mKeyCombo = new KComboBox( this ); | 63 | mKeyCombo = new KComboBox( this ); |
62 | layout->addWidget( mKeyCombo, 0, 1 ); | 64 | layout->addWidget( mKeyCombo, 0, 1 ); |
63 | 65 | ||
64 | mAddButton = new QPushButton( i18n( "Add" ), this ); | 66 | mAddButton = new QPushButton( i18n( "Add" ), this ); |
65 | layout->addWidget( mAddButton, 1, 0 ); | 67 | layout->addWidget( mAddButton, 1, 0 ); |
66 | 68 | ||
67 | mRemoveButton = new QPushButton( i18n( "Remove" ), this ); | 69 | mRemoveButton = new QPushButton( i18n( "Remove" ), this ); |
68 | mRemoveButton->setEnabled( false ); | 70 | mRemoveButton->setEnabled( false ); |
69 | layout->addWidget( mRemoveButton, 1, 1 ); | 71 | layout->addWidget( mRemoveButton, 1, 1 ); |
70 | 72 | ||
71 | mExportButton = new QPushButton( i18n( "Export" ), this ); | 73 | mExportButton = new QPushButton( i18n( "Export" ), this ); |
72 | mExportButton->setEnabled( false ); | 74 | mExportButton->setEnabled( false ); |
73 | layout->addMultiCellWidget( mExportButton, 2, 2, 0, 1 ); | 75 | layout->addMultiCellWidget( mExportButton, 2, 2, 0, 1 ); |
74 | 76 | ||
75 | connect( mAddButton, SIGNAL( clicked() ), SLOT( addKey() ) ); | 77 | connect( mAddButton, SIGNAL( clicked() ), SLOT( addKey() ) ); |
76 | connect( mRemoveButton, SIGNAL( clicked() ), SLOT( removeKey() ) ); | 78 | connect( mRemoveButton, SIGNAL( clicked() ), SLOT( removeKey() ) ); |
77 | connect( mExportButton, SIGNAL( clicked() ), SLOT( exportKey() ) ); | 79 | connect( mExportButton, SIGNAL( clicked() ), SLOT( exportKey() ) ); |
78 | } | 80 | } |
79 | 81 | ||
80 | KeyWidget::~KeyWidget() | 82 | KeyWidget::~KeyWidget() |
81 | { | 83 | { |
82 | } | 84 | } |
83 | 85 | ||
84 | void KeyWidget::setKeys( const KABC::Key::List &list ) | 86 | void KeyWidget::setKeys( const KABC::Key::List &list ) |
85 | { | 87 | { |
86 | mKeyList = list; | 88 | mKeyList = list; |
87 | 89 | ||
88 | updateKeyCombo(); | 90 | updateKeyCombo(); |
89 | } | 91 | } |
90 | 92 | ||
91 | KABC::Key::List KeyWidget::keys() const | 93 | KABC::Key::List KeyWidget::keys() const |
92 | { | 94 | { |
93 | return mKeyList; | 95 | return mKeyList; |
94 | } | 96 | } |
95 | 97 | ||
96 | void KeyWidget::addKey() | 98 | void KeyWidget::addKey() |
97 | { | 99 | { |
98 | QMap<QString, int> keyMap; | 100 | QMap<QString, int> keyMap; |
99 | QStringList keyTypeNames; | 101 | QStringList keyTypeNames; |
100 | QStringList existingKeyTypes; | 102 | QStringList existingKeyTypes; |
101 | 103 | ||
102 | KABC::Key::List::Iterator listIt; | 104 | KABC::Key::List::Iterator listIt; |
103 | for ( listIt = mKeyList.begin(); listIt != mKeyList.end(); ++listIt ) { | 105 | for ( listIt = mKeyList.begin(); listIt != mKeyList.end(); ++listIt ) { |
104 | if ( (*listIt).type() != KABC::Key::Custom ) | 106 | if ( (*listIt).type() != KABC::Key::Custom ) |
105 | existingKeyTypes.append( KABC::Key::typeLabel( (*listIt).type() ) ); | 107 | existingKeyTypes.append( KABC::Key::typeLabel( (*listIt).type() ) ); |
106 | } | 108 | } |
107 | 109 | ||
108 | KABC::Key::TypeList typeList = KABC::Key::typeList(); | 110 | KABC::Key::TypeList typeList = KABC::Key::typeList(); |
109 | KABC::Key::TypeList::Iterator it; | 111 | KABC::Key::TypeList::Iterator it; |
110 | for ( it = typeList.begin(); it != typeList.end(); ++it ) { | 112 | for ( it = typeList.begin(); it != typeList.end(); ++it ) { |
111 | if ( (*it) != KABC::Key::Custom && | 113 | if ( (*it) != KABC::Key::Custom && |
112 | !existingKeyTypes.contains( KABC::Key::typeLabel( *it ) ) ) { | 114 | !existingKeyTypes.contains( KABC::Key::typeLabel( *it ) ) ) { |
113 | keyMap.insert( KABC::Key::typeLabel( *it ), *it ); | 115 | keyMap.insert( KABC::Key::typeLabel( *it ), *it ); |
114 | keyTypeNames.append( KABC::Key::typeLabel( *it ) ); | 116 | keyTypeNames.append( KABC::Key::typeLabel( *it ) ); |
115 | } | 117 | } |
116 | } | 118 | } |
117 | 119 | ||
118 | bool ok; | 120 | bool ok; |
119 | QString name = QInputDialog::getItem( i18n( "Key type" ), i18n( "Select the key type." ), keyTypeNames, 0, true, &ok ); | 121 | QString name = QInputDialog::getItem( i18n( "Key type" ), i18n( "Select the key type." ), keyTypeNames, 0, true, &ok ); |
120 | 122 | ||
121 | 123 | ||
122 | 124 | ||
123 | if ( !ok || name.isEmpty() ) | 125 | if ( !ok || name.isEmpty() ) |
124 | return; | 126 | return; |
125 | 127 | ||
126 | int type = keyMap[ name ]; | 128 | int type = keyMap[ name ]; |
127 | if ( !keyTypeNames.contains( name ) ) | 129 | if ( !keyTypeNames.contains( name ) ) |
128 | type = KABC::Key::Custom; | 130 | type = KABC::Key::Custom; |
129 | 131 | ||
130 | #ifndef KAB_EMBEDDED | 132 | #ifndef KAB_EMBEDDED |
131 | KURL url = KFileDialog::getOpenURL(); | 133 | KURL url = KFileDialog::getOpenURL(); |
132 | 134 | ||
133 | if ( url.isEmpty() ) | 135 | if ( url.isEmpty() ) |
134 | return; | 136 | return; |
135 | 137 | ||
136 | QString tmpFile; | 138 | QString tmpFile; |
137 | if ( KIO::NetAccess::download( url, tmpFile ) ) { | 139 | if ( KIO::NetAccess::download( url, tmpFile ) ) { |
138 | QFile file( tmpFile ); | 140 | QFile file( tmpFile ); |
139 | if ( !file.open( IO_ReadOnly ) ) { | 141 | if ( !file.open( QIODevice::ReadOnly ) ) { |
140 | QString text( i18n( "<qt>Unable to open file <b>%1</b>.</qt>" ) ); | 142 | QString text( i18n( "<qt>Unable to open file <b>%1</b>.</qt>" ) ); |
141 | KMessageBox::error( this, text.arg( url.url() ) ); | 143 | KMessageBox::error( this, text.arg( url.url() ) ); |
142 | return; | 144 | return; |
143 | } | 145 | } |
144 | 146 | ||
145 | 147 | ||
146 | #else //KAB_EMBEDDED | 148 | #else //KAB_EMBEDDED |
147 | QString keyfile = KFileDialog::getOpenFileName( "huhu", | 149 | QString keyfile = KFileDialog::getOpenFileName( "huhu", |
148 | "Select Key", this ); | 150 | "Select Key", this ); |
149 | 151 | ||
150 | if ( keyfile.isEmpty() ) | 152 | if ( keyfile.isEmpty() ) |
151 | return; | 153 | return; |
152 | 154 | ||
153 | { | 155 | { |
154 | QFile file( keyfile ); | 156 | QFile file( keyfile ); |
155 | if ( !file.open( IO_ReadOnly ) ) { | 157 | if ( !file.open( QIODevice::ReadOnly ) ) { |
156 | QString text( i18n( "<qt>Unable to open file <b>%1</b>.</qt>" ) ); | 158 | QString text( i18n( "<qt>Unable to open file <b>%1</b>.</qt>" ) ); |
157 | QString caption( i18n( "Error" ) ); | 159 | QString caption( i18n( "Error" ) ); |
158 | QMessageBox::critical( this, caption, text.arg( keyfile ) ); | 160 | QMessageBox::critical( this, caption, text.arg( keyfile ) ); |
159 | return; | 161 | return; |
160 | } | 162 | } |
161 | 163 | ||
162 | #endif //KAB_EMBEDDED | 164 | #endif //KAB_EMBEDDED |
163 | 165 | ||
164 | 166 | ||
165 | 167 | ||
166 | QTextStream s( &file ); | 168 | Q3TextStream s( &file ); |
167 | QString data; | 169 | QString data; |
168 | 170 | ||
169 | s.setEncoding( QTextStream::UnicodeUTF8 ); | 171 | s.setEncoding( Q3TextStream::UnicodeUTF8 ); |
170 | s >> data; | 172 | s >> data; |
171 | file.close(); | 173 | file.close(); |
172 | 174 | ||
173 | KABC::Key key( data, type ); | 175 | KABC::Key key( data, type ); |
174 | if ( type == KABC::Key::Custom ) | 176 | if ( type == KABC::Key::Custom ) |
175 | key.setCustomTypeString( name ); | 177 | key.setCustomTypeString( name ); |
176 | mKeyList.append( key ); | 178 | mKeyList.append( key ); |
177 | 179 | ||
178 | emit changed(); | 180 | emit changed(); |
179 | 181 | ||
180 | #ifndef KAB_EMBEDDED | 182 | #ifndef KAB_EMBEDDED |
181 | KIO::NetAccess::removeTempFile( tmpFile ); | 183 | KIO::NetAccess::removeTempFile( tmpFile ); |
182 | #endif //KAB_EMBEDDED | 184 | #endif //KAB_EMBEDDED |
183 | 185 | ||
184 | } | 186 | } |
185 | 187 | ||
186 | updateKeyCombo(); | 188 | updateKeyCombo(); |
187 | } | 189 | } |
188 | 190 | ||
189 | void KeyWidget::removeKey() | 191 | void KeyWidget::removeKey() |
190 | { | 192 | { |
191 | int pos = mKeyCombo->currentItem(); | 193 | int pos = mKeyCombo->currentItem(); |
192 | if ( pos == -1 ) | 194 | if ( pos == -1 ) |
193 | return; | 195 | return; |
194 | 196 | ||
195 | QString type = mKeyCombo->currentText(); | 197 | QString type = mKeyCombo->currentText(); |
196 | QString text = i18n( "<qt>Do you really want to remove the key <b>%1</b>?</qt>" ); | 198 | QString text = i18n( "<qt>Do you really want to remove the key <b>%1</b>?</qt>" ); |
197 | 199 | ||
198 | 200 | ||
199 | #ifndef KAB_EMBEDDED | 201 | #ifndef KAB_EMBEDDED |
200 | if ( KMessageBox::questionYesNo( this, text.arg( type ) ) == KMessageBox::No ) | 202 | if ( KMessageBox::questionYesNo( this, text.arg( type ) ) == KMessageBox::No ) |
201 | return; | 203 | return; |
202 | #else //KAB_EMBEDDED | 204 | #else //KAB_EMBEDDED |
203 | QString caption = i18n( "Confirm Delete" ); | 205 | QString caption = i18n( "Confirm Delete" ); |
204 | if (QMessageBox::information( this, caption, | 206 | if (QMessageBox::information( this, caption, |
205 | text.arg( type ), | 207 | text.arg( type ), |
206 | i18n("Yes!"), i18n("No"), 0, 0 ) == 1) | 208 | i18n("Yes!"), i18n("No"), 0, 0 ) == 1) |
207 | return; | 209 | return; |
208 | #endif //KAB_EMBEDDED | 210 | #endif //KAB_EMBEDDED |
209 | 211 | ||
210 | mKeyList.remove( mKeyList.at( pos ) ); | 212 | mKeyList.remove( mKeyList.at( pos ) ); |
211 | emit changed(); | 213 | emit changed(); |
212 | 214 | ||
213 | updateKeyCombo(); | 215 | updateKeyCombo(); |
214 | } | 216 | } |
215 | 217 | ||
216 | void KeyWidget::exportKey() | 218 | void KeyWidget::exportKey() |
217 | { | 219 | { |
218 | KABC::Key key = (*mKeyList.at( mKeyCombo->currentItem() ) ); | 220 | KABC::Key key = (*mKeyList.at( mKeyCombo->currentItem() ) ); |
219 | 221 | ||
220 | #ifndef KAB_EMBEDDED | 222 | #ifndef KAB_EMBEDDED |
221 | KURL url = KFileDialog::getSaveURL(); | 223 | KURL url = KFileDialog::getSaveURL(); |
222 | 224 | ||
223 | KTempFile tempFile; | 225 | KTempFile tempFile; |
224 | QTextStream *s = tempFile.textStream(); | 226 | Q3TextStream *s = tempFile.textStream(); |
225 | s->setEncoding( QTextStream::UnicodeUTF8 ); | 227 | s->setEncoding( Q3TextStream::UnicodeUTF8 ); |
226 | (*s) << key.textData(); | 228 | (*s) << key.textData(); |
227 | tempFile.close(); | 229 | tempFile.close(); |
228 | 230 | ||
229 | KIO::NetAccess::upload( tempFile.name(), url, kapp->mainWidget() ); | 231 | KIO::NetAccess::upload( tempFile.name(), url, kapp->mainWidget() ); |
230 | #else //KAB_EMBEDDED | 232 | #else //KAB_EMBEDDED |
231 | QString keyfile = KFileDialog::getSaveFileName( "huhu", | 233 | QString keyfile = KFileDialog::getSaveFileName( "huhu", |
232 | "Save Key", this ); | 234 | "Save Key", this ); |
233 | 235 | ||
234 | if ( keyfile.isEmpty() ) | 236 | if ( keyfile.isEmpty() ) |
235 | return; | 237 | return; |
236 | 238 | ||
237 | QFile file( keyfile ); | 239 | QFile file( keyfile ); |
238 | if ( !file.open( IO_ReadWrite ) ) { | 240 | if ( !file.open( QIODevice::ReadWrite ) ) { |
239 | QString text( i18n( "<qt>Unable to open file <b>%1</b>.</qt>" ) ); | 241 | QString text( i18n( "<qt>Unable to open file <b>%1</b>.</qt>" ) ); |
240 | QString caption( i18n( "Error" ) ); | 242 | QString caption( i18n( "Error" ) ); |
241 | QMessageBox::critical( this, caption, text.arg( keyfile ) ); | 243 | QMessageBox::critical( this, caption, text.arg( keyfile ) ); |
242 | return; | 244 | return; |
243 | } | 245 | } |
244 | 246 | ||
245 | QTextStream s( &file ); | 247 | Q3TextStream s( &file ); |
246 | s.setEncoding( QTextStream::UnicodeUTF8 ); | 248 | s.setEncoding( Q3TextStream::UnicodeUTF8 ); |
247 | s << key.textData(); | 249 | s << key.textData(); |
248 | file.close(); | 250 | file.close(); |
249 | 251 | ||
250 | 252 | ||
251 | #endif //KAB_EMBEDDED | 253 | #endif //KAB_EMBEDDED |
252 | 254 | ||
253 | } | 255 | } |
254 | 256 | ||
255 | void KeyWidget::updateKeyCombo() | 257 | void KeyWidget::updateKeyCombo() |
256 | { | 258 | { |
257 | int pos = mKeyCombo->currentItem(); | 259 | int pos = mKeyCombo->currentItem(); |
258 | mKeyCombo->clear(); | 260 | mKeyCombo->clear(); |
259 | 261 | ||
260 | KABC::Key::List::Iterator it; | 262 | KABC::Key::List::Iterator it; |
261 | for ( it = mKeyList.begin(); it != mKeyList.end(); ++it ) { | 263 | for ( it = mKeyList.begin(); it != mKeyList.end(); ++it ) { |
262 | if ( (*it).type() == KABC::Key::Custom ) | 264 | if ( (*it).type() == KABC::Key::Custom ) |
263 | mKeyCombo->insertItem( (*it).customTypeString() ); | 265 | mKeyCombo->insertItem( (*it).customTypeString() ); |
264 | else | 266 | else |
265 | mKeyCombo->insertItem( KABC::Key::typeLabel( (*it).type() ) ); | 267 | mKeyCombo->insertItem( KABC::Key::typeLabel( (*it).type() ) ); |
266 | } | 268 | } |
267 | 269 | ||
268 | mKeyCombo->setCurrentItem( pos ); | 270 | mKeyCombo->setCurrentItem( pos ); |
269 | 271 | ||
270 | bool state = ( mKeyList.count() != 0 ); | 272 | bool state = ( mKeyList.count() != 0 ); |
271 | mRemoveButton->setEnabled( state ); | 273 | mRemoveButton->setEnabled( state ); |
272 | mExportButton->setEnabled( state ); | 274 | mExportButton->setEnabled( state ); |
273 | } | 275 | } |
274 | 276 | ||
275 | #ifndef KAB_EMBEDDED | 277 | #ifndef KAB_EMBEDDED_ |
276 | #include "keywidget.moc" | 278 | #include "moc_keywidget.cpp" |
277 | #endif //KAB_EMBEDDED | 279 | #endif //KAB_EMBEDDED |