TDE personal information management applications
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

messagebox.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. messagebox.cpp
  3. This file is part of libkleopatra, the KDE keymanagement library
  4. Copyright (c) 2004 Klarälvdalens Datakonsult AB
  5. Libkleopatra is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. License, or (at your option) any later version.
  9. Libkleopatra is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. In addition, as a special exception, the copyright holders give
  17. permission to link the code of this program with any edition of
  18. the Qt library by Trolltech AS, Norway (or with modified versions
  19. of Qt that use the same license as Qt), and distribute linked
  20. combinations including the two. You must obey the GNU General
  21. Public License in all respects for all of the code used other than
  22. Qt. If you modify this file, you may extend this exception to
  23. your version of the file, but you are not obligated to do so. If
  24. you do not wish to do so, delete this exception statement from
  25. your version.
  26. */
  27. #ifdef HAVE_CONFIG_H
  28. #include <config.h>
  29. #endif
  30. #include "messagebox.h"
  31. #include "kleo/job.h"
  32. #include <gpgmepp/signingresult.h>
  33. #include <gpgmepp/encryptionresult.h>
  34. #include <kfiledialog.h>
  35. #include <kdialogbase.h>
  36. #include <klocale.h>
  37. #include <ksavefile.h>
  38. #include <kguiitem.h>
  39. #include <kdebug.h>
  40. #include <tqtextedit.h>
  41. #include <tqtextstream.h>
  42. #include <tqvbox.h>
  43. #include <tqapplication.h>
  44. #include <tqstylesheet.h>
  45. #include <gpg-error.h>
  46. using namespace Kleo;
  47. using namespace GpgME;
  48. namespace {
  49. static KGuiItem KGuiItem_save() {
  50. return KGuiItem( i18n("&Save to Disk..."), "filesaveas" );
  51. }
  52. static KGuiItem KGuiItem_copy() {
  53. return KGuiItem( i18n("&Copy to Clipboard"), "editcopy", i18n("Copy Audit Log to Clipboard") );
  54. }
  55. static KGuiItem KGuiItem_showAuditLog() {
  56. return KGuiItem( i18n("&Show Audit Log") ); // "view_log"?
  57. }
  58. class AuditLogViewer : public KDialogBase {
  59. // Q_OBJECT
  60. public:
  61. explicit AuditLogViewer( const TQString & log, TQWidget * parent=0, const char * name=0, WFlags f=0 )
  62. : KDialogBase( parent, name, false, i18n("View GnuPG Audit Log"),
  63. Close|User1|User2, Close, false, KGuiItem_save(), KGuiItem_copy() ),
  64. m_log( /* sic */ ),
  65. m_textEdit( new TQTextEdit( this, "m_textEdit" ) )
  66. {
  67. setWFlags( f );
  68. setMainWidget( m_textEdit );
  69. m_textEdit->setTextFormat( TQTextEdit::RichText );
  70. m_textEdit->setReadOnly( true );
  71. setAuditLog( log );
  72. }
  73. ~AuditLogViewer() {}
  74. void setAuditLog( const TQString & log ) {
  75. if ( log == m_log )
  76. return;
  77. m_log = log;
  78. m_textEdit->setText( "<qt>" + log + "</qt>" );
  79. const TQRect rect = m_textEdit->paragraphRect( 0 );
  80. kdDebug() << "setAuditLog: rect = " << rect << endl;
  81. if ( !rect.isValid() )
  82. return;
  83. TQSize maxSize = qApp->desktop()->screenGeometry( this ).size() * 2 / 3 ;
  84. if ( !maxSize.isValid() )
  85. maxSize = TQSize( 640, 480 );
  86. m_textEdit->setMinimumSize( rect.size().boundedTo( maxSize ) );
  87. }
  88. private:
  89. void slotUser1() {
  90. const TQString fileName = KFileDialog::getSaveFileName( TQString(), TQString(),
  91. this, i18n("Choose File to Save GnuPG Audit Log to") );
  92. if ( fileName.isEmpty() )
  93. return;
  94. KSaveFile file( fileName );
  95. if ( TQTextStream * const s = file.textStream() ) {
  96. *s << "<html><head>";
  97. if ( !caption().isEmpty() )
  98. *s << "\n<title>" << /*TQt*/TQStyleSheet::escape( caption() ) << "</title>\n";
  99. *s << "</head><body>\n"
  100. << m_log
  101. << "\n</body></html>" << endl;
  102. file.close();
  103. }
  104. if ( const int err = file.status() )
  105. KMessageBox::error( this, i18n("Couldn't save to file \"%1\": %2")
  106. .arg( file.name(), TQString::fromLocal8Bit( strerror( err ) ) ),
  107. i18n("File Save Error") );
  108. }
  109. void slotUser2() {
  110. m_textEdit->selectAll();
  111. m_textEdit->copy();
  112. m_textEdit->selectAll( false );
  113. }
  114. private:
  115. TQString m_log;
  116. TQTextEdit * m_textEdit;
  117. };
  118. } // anon namespace
  119. // static
  120. void MessageBox::auditLog( TQWidget * parent, const Job * job, const TQString & caption ) {
  121. if ( !job )
  122. return;
  123. if ( !GpgME::hasFeature( AuditLogFeature ) || !job->isAuditLogSupported() ) {
  124. KMessageBox::information( parent, i18n("Your system does not have support for GnuPG Audit Logs"),
  125. i18n("System Error") );
  126. return;
  127. }
  128. const GpgME::Error err = job->auditLogError();
  129. if ( err.code() != GPG_ERR_NO_DATA ) {
  130. KMessageBox::information( parent, i18n("An error occurred while trying to retrieve the GnuPG Audit Log:\n%1")
  131. .arg( TQString::fromLocal8Bit( err.asString() ) ),
  132. i18n("GnuPG Audit Log Error") );
  133. return;
  134. }
  135. const TQString log = job->auditLogAsHtml();
  136. if ( log.isEmpty() ) {
  137. KMessageBox::information( parent, i18n("No GnuPG Audit Log available for this operation."),
  138. i18n("No GnuPG Audit Log") );
  139. return;
  140. }
  141. auditLog( parent, log, caption );
  142. }
  143. // static
  144. void MessageBox::auditLog( TQWidget * parent, const TQString & log, const TQString & caption ) {
  145. AuditLogViewer * const alv = new AuditLogViewer( log, parent, "alv", Qt::WDestructiveClose );
  146. alv->setCaption( caption );
  147. alv->show();
  148. }
  149. // static
  150. void MessageBox::auditLog( TQWidget * parent, const Job * job ) {
  151. auditLog( parent, job, i18n("GnuPG Audit Log Viewer") );
  152. }
  153. // static
  154. void MessageBox::auditLog( TQWidget * parent, const TQString & log ) {
  155. auditLog( parent, log, i18n("GnuPG Audit Log Viewer") );
  156. }
  157. static TQString to_information_string( const SigningResult & result ) {
  158. return result.error()
  159. ? i18n("Signing failed: %1").arg( TQString::fromLocal8Bit( result.error().asString() ) )
  160. : i18n("Signing successful") ;
  161. }
  162. static TQString to_error_string( const SigningResult & result ) {
  163. return to_information_string( result );
  164. }
  165. static TQString to_information_string( const EncryptionResult & result ) {
  166. return result.error()
  167. ? i18n("Encryption failed: %1").arg( TQString::fromLocal8Bit( result.error().asString() ) )
  168. : i18n("Encryption successful") ;
  169. }
  170. static TQString to_error_string( const EncryptionResult & result ) {
  171. return to_information_string( result );
  172. }
  173. static TQString to_information_string( const SigningResult & sresult, const EncryptionResult & eresult ) {
  174. return to_information_string( sresult ) + '\n' + to_information_string( eresult );
  175. }
  176. static TQString to_error_string( const SigningResult & sresult, const EncryptionResult & eresult ) {
  177. return to_information_string( sresult, eresult );
  178. }
  179. // static
  180. void MessageBox::information( TQWidget * parent, const SigningResult & result, const Job * job, int options ) {
  181. information( parent, result, job, i18n("Signing Result"), options );
  182. }
  183. // static
  184. void MessageBox::information( TQWidget * parent, const SigningResult & result, const Job * job, const TQString & caption, int options ) {
  185. make( parent, TQMessageBox::Information, to_information_string( result ), job, caption, options );
  186. }
  187. // static
  188. void MessageBox::error( TQWidget * parent, const SigningResult & result, const Job * job, int options ) {
  189. error( parent, result, job, i18n("Signing Error"), options );
  190. }
  191. // static
  192. void MessageBox::error( TQWidget * parent, const SigningResult & result, const Job * job, const TQString & caption, int options ) {
  193. make( parent, TQMessageBox::Critical, to_error_string( result ), job, caption, options );
  194. }
  195. // static
  196. void MessageBox::information( TQWidget * parent, const EncryptionResult & result, const Job * job, int options ) {
  197. information( parent, result, job, i18n("Encryption Result"), options );
  198. }
  199. // static
  200. void MessageBox::information( TQWidget * parent, const EncryptionResult & result, const Job * job, const TQString & caption, int options ) {
  201. make( parent, TQMessageBox::Information, to_information_string( result ), job, caption, options );
  202. }
  203. // static
  204. void MessageBox::error( TQWidget * parent, const EncryptionResult & result, const Job * job, int options ) {
  205. error( parent, result, job, i18n("Encryption Error"), options );
  206. }
  207. // static
  208. void MessageBox::error( TQWidget * parent, const EncryptionResult & result, const Job * job, const TQString & caption, int options ) {
  209. make( parent, TQMessageBox::Critical, to_error_string( result ), job, caption, options );
  210. }
  211. // static
  212. void MessageBox::information( TQWidget * parent, const SigningResult & sresult, const EncryptionResult & eresult, const Job * job, int options ) {
  213. information( parent, sresult, eresult, job, i18n("Encryption Result"), options );
  214. }
  215. // static
  216. void MessageBox::information( TQWidget * parent, const SigningResult & sresult, const EncryptionResult & eresult, const Job * job, const TQString & caption, int options ) {
  217. make( parent, TQMessageBox::Information, to_information_string( sresult, eresult ), job, caption, options );
  218. }
  219. // static
  220. void MessageBox::error( TQWidget * parent, const SigningResult & sresult, const EncryptionResult & eresult, const Job * job, int options ) {
  221. error( parent, sresult, eresult, job, i18n("Encryption Error"), options );
  222. }
  223. // static
  224. void MessageBox::error( TQWidget * parent, const SigningResult & sresult, const EncryptionResult & eresult, const Job * job, const TQString & caption, int options ) {
  225. make( parent, TQMessageBox::Critical, to_error_string( sresult, eresult ), job, caption, options );
  226. }
  227. // static
  228. bool MessageBox::showAuditLogButton( const Kleo::Job * job ) {
  229. if ( !job ) {
  230. kdDebug() << "not showing audit log button (no job instance)" << endl;
  231. return false;
  232. }
  233. if ( !GpgME::hasFeature( GpgME::AuditLogFeature ) ) {
  234. kdDebug() << "not showing audit log button (gpgme too old)" << endl;
  235. return false;
  236. }
  237. if ( !job->isAuditLogSupported() ) {
  238. kdDebug() << "not showing audit log button (not supported)" << endl;
  239. return false;
  240. }
  241. if ( job->auditLogError().code() == GPG_ERR_NO_DATA ) {
  242. kdDebug() << "not showing audit log button (GPG_ERR_NO_DATA)" << endl;
  243. return false;
  244. }
  245. if ( !job->auditLogError() && job->auditLogAsHtml().isEmpty() ) {
  246. kdDebug() << "not showing audit log button (success, but result empty)" << endl;
  247. return false;
  248. }
  249. return true;
  250. }
  251. // static
  252. void MessageBox::make( TQWidget * parent, TQMessageBox::Icon icon, const TQString & text, const Job * job, const TQString & caption, int options ) {
  253. KDialogBase * dialog = showAuditLogButton( job )
  254. ? new KDialogBase( caption, KDialogBase::Yes | KDialogBase::No,
  255. KDialogBase::Yes, KDialogBase::Yes,
  256. parent, "error", true, true,
  257. KStdGuiItem::ok(), KGuiItem_showAuditLog() )
  258. : new KDialogBase( caption, KDialogBase::Yes,
  259. KDialogBase::Yes, KDialogBase::Yes,
  260. parent, "error", true, true,
  261. KStdGuiItem::ok() ) ;
  262. if ( options & KMessageBox::PlainCaption )
  263. dialog->setPlainCaption( caption );
  264. if ( KDialogBase::No == KMessageBox::createKMessageBox( dialog, icon, text, TQStringList(), TQString::null, 0, options ) )
  265. auditLog( 0, job );
  266. }