certmanager Library API Documentation

kwatchgnupgmainwin.cpp

00001 /*
00002     kwatchgnupgmainwin.cpp
00003 
00004     This file is part of Kleopatra, the KDE keymanager
00005     Copyright (c) 2001,2002,2004 Klarälvdalens Datakonsult AB
00006 
00007     Kleopatra is free software; you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation; either version 2 of the License, or
00010     (at your option) any later version.
00011 
00012     Kleopatra is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     In addition, as a special exception, the copyright holders give
00022     permission to link the code of this program with any edition of
00023     the Qt library by Trolltech AS, Norway (or with modified versions
00024     of Qt that use the same license as Qt), and distribute linked
00025     combinations including the two.  You must obey the GNU General
00026     Public License in all respects for all of the code used other than
00027     Qt.  If you modify this file, you may extend this exception to
00028     your version of the file, but you are not obligated to do so.  If
00029     you do not wish to do so, delete this exception statement from
00030     your version.
00031 */
00032 
00033 #include "kwatchgnupgmainwin.h"
00034 #include "kwatchgnupgconfig.h"
00035 #include "tray.h"
00036 
00037 #include <kleo/cryptobackendfactory.h>
00038 #include <kleo/cryptoconfig.h>
00039 
00040 #include <kdebug.h>
00041 #include <kmessagebox.h>
00042 #include <klocale.h>
00043 #include <kapplication.h>
00044 #include <kaction.h>
00045 #include <kstdaction.h>
00046 #include <kprocio.h>
00047 #include <kconfig.h>
00048 #include <kfiledialog.h>
00049 #include <kedittoolbar.h>
00050 #include <kkeydialog.h>
00051 
00052 #include <qtextedit.h>
00053 #include <qdir.h>
00054 #include <qeventloop.h>
00055 #include <qtimer.h>
00056 #include <qtextcodec.h>
00057 
00058 #define WATCHGNUPGBINARY "watchgnupg"
00059 #define WATCHGNUPGSOCKET ( QDir::home().canonicalPath() + "/.gnupg/log-socket")
00060 
00061 KWatchGnuPGMainWindow::KWatchGnuPGMainWindow( QWidget* parent, const char* name )
00062   : KMainWindow( parent, name, WType_TopLevel ), mConfig(0)
00063 {
00064   createActions();
00065   createGUI();
00066 
00067   mCentralWidget = new QTextEdit( this, "central log view" );
00068   mCentralWidget->setTextFormat( QTextEdit::LogText );
00069   setCentralWidget( mCentralWidget );
00070 
00071   mWatcher = new KProcIO( QTextCodec::codecForMib( 106 /*utf-8*/ ) );
00072   connect( mWatcher, SIGNAL( processExited(KProcess*) ),
00073            this, SLOT( slotWatcherExited() ) );
00074   connect( mWatcher, SIGNAL( readReady(KProcIO*) ),
00075            this, SLOT( slotReadStdout() ) );
00076 
00077   slotReadConfig();
00078   mSysTray = new KWatchGnuPGTray( this );
00079   mSysTray->show();
00080   connect( mSysTray, SIGNAL( quitSelected() ),
00081            this, SLOT( slotQuit() ) );
00082   setAutoSaveSettings();
00083 }
00084 
00085 KWatchGnuPGMainWindow::~KWatchGnuPGMainWindow()
00086 {
00087   delete mWatcher;
00088 }
00089 
00090 void KWatchGnuPGMainWindow::slotClear()
00091 {
00092   mCentralWidget->clear();
00093   mCentralWidget->append( tr("[%1] Log cleared").arg( QDateTime::currentDateTime().toString(Qt::ISODate) ) );
00094 }
00095 
00096 void KWatchGnuPGMainWindow::createActions()
00097 {
00098   (void)new KAction( i18n("C&lear History"), "history_clear", CTRL+Key_L,
00099              this, SLOT( slotClear() ),
00100              actionCollection(), "clear_log" );
00101   (void)KStdAction::saveAs( this, SLOT(slotSaveAs()), actionCollection() );
00102   (void)KStdAction::close( this, SLOT(close()), actionCollection() );
00103   (void)KStdAction::quit( this, SLOT(slotQuit()), actionCollection() );
00104   (void)KStdAction::preferences( this, SLOT(slotConfigure()), actionCollection() );
00105   ( void )KStdAction::keyBindings(this, SLOT(configureShortcuts()), actionCollection());
00106   ( void )KStdAction::configureToolbars(this, SLOT(slotConfigureToolbars()), actionCollection());
00107 
00108 #if 0
00109   (void)new KAction( i18n("Configure KWatchGnuPG..."), QString::fromLatin1("configure"),
00110                      0, this, SLOT( slotConfigure() ),
00111                      actionCollection(), "configure" );
00112 #endif
00113 
00114 }
00115 
00116 void KWatchGnuPGMainWindow::configureShortcuts()
00117 {
00118   KKeyDialog::configure( actionCollection(), this );
00119 }
00120 
00121 void KWatchGnuPGMainWindow::slotConfigureToolbars()
00122 {
00123     KEditToolbar dlg( factory() );
00124 
00125     dlg.exec();
00126 }
00127 
00128 void KWatchGnuPGMainWindow::startWatcher()
00129 {
00130   disconnect( mWatcher, SIGNAL( processExited(KProcess*) ),
00131               this, SLOT( slotWatcherExited() ) );
00132   if( mWatcher->isRunning() ) {
00133     mWatcher->kill();
00134     while( mWatcher->isRunning() ) {
00135       kapp->eventLoop()->processEvents(QEventLoop::ExcludeUserInput);
00136     }
00137     mCentralWidget->append(tr("[%1] Log stopped")
00138                            .arg( QDateTime::currentDateTime().toString(Qt::ISODate)));
00139   }
00140   mWatcher->clearArguments();
00141   KConfig* config = kapp->config();
00142   config->setGroup("WatchGnuPG");
00143   *mWatcher << config->readEntry("Executable", WATCHGNUPGBINARY);
00144   *mWatcher << "--force";
00145   *mWatcher << config->readEntry("Socket", WATCHGNUPGSOCKET);
00146   config->setGroup(QString::null);
00147   if( !mWatcher->start() ) {
00148     KMessageBox::sorry( this, i18n("The watchgnupg logging process could not be started.\nPlease install watchgnupg somewhere in your $PATH.\nThis log window is now completely useless." ) );
00149   } else {
00150     mCentralWidget->append( tr("[%1] Log started")
00151                             .arg( QDateTime::currentDateTime().toString(Qt::ISODate) ) );
00152   }
00153   connect( mWatcher, SIGNAL( processExited(KProcess*) ),
00154            this, SLOT( slotWatcherExited() ) );
00155 }
00156 
00157 void KWatchGnuPGMainWindow::setGnuPGConfig()
00158 {
00159   QStringList logclients;
00160   // Get config object
00161   Kleo::CryptoConfig* cconfig = Kleo::CryptoBackendFactory::instance()->config();
00162   if ( !cconfig )
00163     return;
00164   //Q_ASSERT( cconfig );
00165   KConfig* config = kapp->config();
00166   config->setGroup("WatchGnuPG");
00167   QStringList comps = cconfig->componentList();
00168   for( QStringList::const_iterator it = comps.begin(); it != comps.end(); ++it ) {
00169     Kleo::CryptoConfigComponent* comp = cconfig->component( *it );
00170     Q_ASSERT(comp);
00171     // Look for log-file entry in Debug group
00172     Kleo::CryptoConfigGroup* group = comp->group("Debug");
00173     if( group ) {
00174       Kleo::CryptoConfigEntry* entry = group->entry("log-file");
00175       if( entry ) {
00176         entry->setStringValue( QString("socket://")+
00177                                config->readEntry("Socket",
00178                                                  WATCHGNUPGSOCKET ));
00179         logclients << QString("%1 (%2)").arg(*it).arg(comp->description());
00180       }
00181       entry = group->entry("debug-level");
00182       if( entry ) {
00183         entry->setStringValue( config->readEntry("LogLevel", "basic") );
00184       }
00185     }
00186   }
00187   cconfig->sync(true);
00188   if( logclients.isEmpty() ) {
00189     KMessageBox::sorry( 0, i18n("There are no components available that support logging." ) );
00190   }
00191 }
00192 
00193 void KWatchGnuPGMainWindow::slotWatcherExited()
00194 {
00195   if( KMessageBox::questionYesNo( this, i18n("The watchgnupg logging process died.\nDo you want to try to restart it?") ) == KMessageBox::Yes ) {
00196     mCentralWidget->append( i18n("====== Restarting logging process =====") );
00197     startWatcher();
00198   } else {
00199     KMessageBox::sorry( this, i18n("The watchgnupg logging process is not running.\nThis log window is now completely useless." ) );
00200   }
00201 }
00202 
00203 void KWatchGnuPGMainWindow::slotReadStdout()
00204 {
00205   if ( !mWatcher )
00206     return;
00207   QString str;
00208   while( mWatcher->readln(str,false) > 0 ) {
00209     mCentralWidget->append( str );
00210     if( !isVisible() ) {
00211       // Change tray icon to show something happened
00212       // PENDING(steffen)
00213       mSysTray->setAttention(true);
00214     }
00215   }
00216   QTimer::singleShot( 0, this, SLOT(slotAckRead()) );
00217 }
00218 
00219 void KWatchGnuPGMainWindow::slotAckRead() {
00220   if ( mWatcher )
00221     mWatcher->ackRead();
00222 }
00223 
00224 void KWatchGnuPGMainWindow::show()
00225 {
00226   mSysTray->setAttention(false);
00227   KMainWindow::show();
00228 }
00229 
00230 void KWatchGnuPGMainWindow::slotSaveAs()
00231 {
00232   QString filename = KFileDialog::getSaveFileName( QString::null, QString::null,
00233                                                    this, i18n("Save Log to File") );
00234   if( filename.isEmpty() ) return;
00235   QFile file(filename);
00236   if( file.exists() ) {
00237     if( KMessageBox::Yes !=
00238         KMessageBox::warningYesNo( this, i18n("The file named \"%1\" already "
00239                                               "exists. Are you sure you want "
00240                                               "to overwrite it?").arg(filename),
00241                                    i18n("Overwrite File") ) ) {
00242       return;
00243     }
00244   }
00245   if( file.open( IO_WriteOnly ) ) {
00246     QTextStream st(&file);
00247     st << mCentralWidget->text();
00248     file.close();
00249   }
00250 }
00251 
00252 void KWatchGnuPGMainWindow::slotQuit()
00253 {
00254   disconnect( mWatcher, SIGNAL( processExited(KProcess*) ),
00255               this, SLOT( slotWatcherExited() ) );
00256   mWatcher->kill();
00257   kapp->quit();
00258 }
00259 
00260 void KWatchGnuPGMainWindow::slotConfigure()
00261 {
00262   if( !mConfig ) {
00263     mConfig = new KWatchGnuPGConfig( this, "config dialog" );
00264     connect( mConfig, SIGNAL( reconfigure() ),
00265              this, SLOT( slotReadConfig() ) );
00266   }
00267   mConfig->loadConfig();
00268   mConfig->exec();
00269 }
00270 
00271 void KWatchGnuPGMainWindow::slotReadConfig()
00272 {
00273   KConfig* config = kapp->config();
00274   config->setGroup("LogWindow");
00275   mCentralWidget->setWordWrap( config->readBoolEntry("WordWrap", false)
00276                                ?QTextEdit::WidgetWidth
00277                                :QTextEdit::NoWrap );
00278   mCentralWidget->setMaxLogLines( config->readNumEntry( "MaxLogLen", 10000 ) );
00279   setGnuPGConfig();
00280   startWatcher();
00281 }
00282 
00283 bool KWatchGnuPGMainWindow::queryClose()
00284 {
00285   if ( !kapp->sessionSaving() ) {
00286     hide();
00287     return false;
00288   }
00289   return KMainWindow::queryClose();
00290 }
00291 
00292 #include "kwatchgnupgmainwin.moc"
KDE Logo
This file is part of the documentation for certmanager Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 23 22:39:33 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003