obtainkeysjob.cpp
1 /*
2  obtainkeysjob.cpp
3 
4  This file is part of libkleopatra, the KDE keymanagement library
5  Copyright (c) 2005 Klarälvdalens Datakonsult AB
6 
7  Libkleopatra is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of the
10  License, or (at your option) any later version.
11 
12  Libkleopatra 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.
16 
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
20 
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 TQt library by Trolltech AS, Norway (or with modified versions
24  of TQt that use the same license as TQt), 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  TQt. 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.
31 */
32 
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36 
37 #include "obtainkeysjob.h"
38 
39 #include "chiasmusbackend.h"
40 
41 #include "kleo/cryptoconfig.h"
42 
43 #include <tdelocale.h>
44 #include <tdemessagebox.h>
45 #include <kshell.h>
46 
47 #include <tqdir.h>
48 #include <tqstringlist.h>
49 #include <tqvariant.h>
50 #include <tqtimer.h>
51 #include <tqfileinfo.h>
52 
53 #include <gpg-error.h>
54 
55 #include <cassert>
56 
57 Kleo::ObtainKeysJob::ObtainKeysJob()
58  : SpecialJob( 0, 0 ),
59  mIndex( 0 ),
60  mCanceled( false )
61 {
62  assert( ChiasmusBackend::instance() );
63  assert( ChiasmusBackend::instance()->config() );
64  const CryptoConfigEntry * keypaths =
65  ChiasmusBackend::instance()->config()->entry( "Chiasmus", "General", "keydir" );
66  assert( keypaths );
67  mKeyPaths = TQStringList( keypaths->urlValue().path() );
68 }
69 
70 Kleo::ObtainKeysJob::~ObtainKeysJob() {}
71 
73  TQTimer::singleShot( 0, this, TQ_SLOT(slotPerform()) );
74  return mError = 0;
75 }
76 
77 GpgME::Error Kleo::ObtainKeysJob::exec() {
78  slotPerform( false );
79  return mError;
80 }
81 
82 void Kleo::ObtainKeysJob::slotCancel() {
83  mCanceled = true;
84 }
85 
86 void Kleo::ObtainKeysJob::slotPerform() {
87  slotPerform( true );
88 }
89 
90 void Kleo::ObtainKeysJob::slotPerform( bool async ) {
91  if ( mCanceled && !mError )
92  mError = gpg_error( GPG_ERR_CANCELED );
93  if ( mIndex >= mKeyPaths.size() || mError ) {
94  emit done();
95  emit SpecialJob::result( mError, TQVariant( mResult ) );
96  return;
97  }
98 
99  emit progress( i18n( "Scanning directory %1..." ).arg( mKeyPaths[mIndex] ),
100  mIndex, mKeyPaths.size() );
101 
102  const TQDir dir( KShell::tildeExpand( mKeyPaths[mIndex] ) );
103 
104  if ( const TQFileInfoList * xisFiles = dir.entryInfoList( "*.xis;*.XIS", TQDir::Files ) )
105  for ( TQFileInfoList::const_iterator it = xisFiles->begin(), end = xisFiles->end() ; it != end ; ++it )
106  if ( (*it)->isReadable() )
107  mResult.push_back( (*it)->absFilePath() );
108 
109  ++mIndex;
110 
111  if ( async )
112  TQTimer::singleShot( 0, this, TQ_SLOT(slotPerform()) );
113  else
114  slotPerform( false );
115 }
116 
117 void Kleo::ObtainKeysJob::showErrorDialog( TQWidget * parent, const TQString & caption ) const {
118  if ( !mError )
119  return;
120  if ( mError.isCanceled() )
121  return;
122  const TQString msg = TQString::fromUtf8( mError.asString() );
123  KMessageBox::error( parent, msg, caption );
124 }
125 
126 #include "obtainkeysjob.moc"
GpgME::Error start()
void showErrorDialog(TQWidget *, const TQString &) const
GpgME::Error exec()