Better wording
[kdepim.git] / kleopatra / dialogs / exportsecretkeydialog.cpp
blob5f4e48fbf27f55aab33aefc53347261182134689
1 /* -*- mode: c++; c-basic-offset:4 -*-
2 dialogs/exportsecretkeydialog.cpp
4 This file is part of Kleopatra, the KDE keymanager
5 Copyright (c) 2008 Klarälvdalens Datakonsult AB
7 Kleopatra is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 Kleopatra is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 In addition, as a special exception, the copyright holders give
22 permission to link the code of this program with any edition of
23 the Qt library by Trolltech AS, Norway (or with modified versions
24 of Qt that use the same license as Qt), and distribute linked
25 combinations including the two. You must obey the GNU General
26 Public License in all respects for all of the code used other than
27 Qt. If you modify this file, you may extend this exception to
28 your version of the file, but you are not obligated to do so. If
29 you do not wish to do so, delete this exception statement from
30 your version.
33 #include <config-kleopatra.h>
35 #include "exportsecretkeydialog.h"
37 #include "ui_exportsecretkeydialog.h"
39 #include <utils/formatting.h>
41 #include <gpgme++/key.h>
43 #include <KDebug>
44 #include <KMessageBox>
45 #include <KLocalizedString>
47 #include <cassert>
49 using namespace Kleo;
50 using namespace Kleo::Dialogs;
51 using namespace GpgME;
53 // This comes from gnupg's sources, agent/minip12.c
54 // In fact, any charset supported by iconv would work, but we don't link to iconv directly...
55 static const char *charsets[] = {
56 "utf8",
57 "iso-8859-1",
58 "iso-8859-15",
59 "iso-8859-2",
60 "iso-8859-3",
61 "iso-8859-4",
62 "iso-8859-5",
63 "iso-8859-6",
64 "iso-8859-7",
65 "iso-8859-8",
66 "iso-8859-9",
67 "koi8-r",
68 "ibm437",
69 "ibm850",
70 "euc-jp",
71 "big5",
73 static const unsigned int numCharsets = sizeof charsets / sizeof *charsets;
75 class ExportSecretKeyDialog::Private {
76 friend class ::Kleo::Dialogs::ExportSecretKeyDialog;
77 ExportSecretKeyDialog * const q;
78 public:
79 explicit Private( ExportSecretKeyDialog * qq )
80 : q( qq ),
81 ui( q )
86 private:
87 void updateWidgets() {
88 const bool x509 = key.protocol() == CMS;
89 ui.charsetCB->setVisible( x509 );
90 ui.charsetLB->setVisible( x509 );
93 void updateFileName() {
94 const bool x509 = key.protocol() == CMS;
95 const bool armor = q->useArmor();
97 static const char * extensions[] = {
98 ".gpg", ".asc", ".p12", ".pem"
100 const unsigned int idx = 2*x509+armor;
101 const char * const extension = extensions[idx];
103 const QString nf = i18n("Secret Key Files") + QString::fromLatin1("(*%1 *%2 *%3 *%4 *.pgp)")
104 .arg( extensions[idx], extensions[(idx+1)%4], extensions[(idx+2)%4], extensions[(idx+3)%4] );
105 ui.outputFileFR->setNameFilter( nf );
107 QString fn = q->fileName();
108 if ( fn.isEmpty() )
109 return;
111 bool found = false;
112 for ( unsigned int i = 0 ; i < sizeof extensions / sizeof *extensions ; ++i )
113 if ( fn.endsWith( extensions[i], Qt::CaseInsensitive ) ) {
114 fn.chop( 4 );
115 found = true;
116 break;
118 if ( found )
119 q->setFileName( fn + extension );
122 void updateLabel() {
123 ui.descriptionLB->setText( i18nc("@info",
124 "Please select export options for %1:",
125 Formatting::formatForComboBox( key ) ) );
127 private:
128 Key key;
130 struct UI : public Ui_ExportSecretKeyDialog {
131 explicit UI( Dialogs::ExportSecretKeyDialog * qq )
132 : Ui_ExportSecretKeyDialog()
134 setupUi( qq );
136 outputFileFR->setExistingOnly( false );
137 outputFileFR->setFilter( QDir::Files );
138 outputFileFR->setNameFilter( i18n("Secret Key Files (*.pem *.p12 *.gpg *.asc *.pgp)") );
140 for ( unsigned int i = 0 ; i < numCharsets ; ++i )
141 charsetCB->addItem( QString::fromLatin1( charsets[i] ) );
142 charsetCB->setCurrentIndex( 0 );
145 } ui;
148 ExportSecretKeyDialog::ExportSecretKeyDialog( QWidget * p, Qt::WindowFlags f )
149 : QDialog( p, f ), d( new Private( this ) )
154 ExportSecretKeyDialog::~ExportSecretKeyDialog() {}
157 void ExportSecretKeyDialog::setKey( const Key & key ) {
158 if ( qstricmp( key.primaryFingerprint(), d->key.primaryFingerprint() ) == 0 )
159 return;
160 d->key = key;
161 d->updateWidgets();
162 d->updateLabel();
163 d->updateFileName();
166 Key ExportSecretKeyDialog::key() const {
167 return d->key;
170 void ExportSecretKeyDialog::setFileName( const QString & fileName ) {
171 d->ui.outputFileFR->setFileName( fileName );
174 QString ExportSecretKeyDialog::fileName() const {
175 return d->ui.outputFileFR->fileName();
178 void ExportSecretKeyDialog::setCharset( const QByteArray & charset ) {
179 for ( unsigned int i = 0 ; i < sizeof charsets / sizeof *charsets ; ++i )
180 if ( charset == charsets[i] ) {
181 d->ui.charsetCB->setCurrentIndex( i );
182 return;
186 QByteArray ExportSecretKeyDialog::charset() const {
187 if ( d->ui.charsetCB->isVisible() )
188 return d->ui.charsetCB->currentText().toLatin1();
189 else
190 return QByteArray();
193 void ExportSecretKeyDialog::setUseArmor( bool on ) {
194 d->ui.armorCB->setChecked( on );
197 bool ExportSecretKeyDialog::useArmor() const {
198 return d->ui.armorCB->isChecked();
201 void ExportSecretKeyDialog::accept() {
202 d->updateFileName();
203 const QString fn = fileName();
204 if ( fn.isEmpty() ) {
205 KMessageBox::information( this, i18nc("@info",
206 "You have to enter an output filename." ),
207 i18nc("@title", "Incomplete data") );
208 d->ui.outputFileFR->setFocus();
209 return;
212 const QByteArray cs = charset();
213 if ( d->key.protocol() == CMS && cs.isEmpty() ) {
214 KMessageBox::information( this, i18nc("@info",
215 "You have to choose a passphrase character set." ),
216 i18nc("@title", "Incomplete data") );
217 d->ui.charsetCB->setFocus();
218 return;
221 QDialog::accept();
224 #include "moc_exportsecretkeydialog.cpp"