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 <qtextedit.h> 00051 #include <qdir.h> 00052 #include <qeventloop.h> 00053 #include <qtimer.h> 00054 #include <kkeydialog.h> 00055 00056 #define WATCHGNUPGBINARY "watchgnupg" 00057 #define WATCHGNUPGSOCKET ( QDir::home().canonicalPath() + "/.gnupg/log-socket") 00058 00059 KWatchGnuPGMainWindow::KWatchGnuPGMainWindow( QWidget* parent, const char* name ) 00060 : KMainWindow( parent, name, WType_TopLevel ), mConfig(0) 00061 { 00062 createActions(); 00063 createGUI(); 00064 00065 mCentralWidget = new QTextEdit( this, "central log view" ); 00066 mCentralWidget->setTextFormat( QTextEdit::LogText ); 00067 setCentralWidget( mCentralWidget ); 00068 00069 mWatcher = new KProcIO(); 00070 connect( mWatcher, SIGNAL( processExited(KProcess*) ), 00071 this, SLOT( slotWatcherExited() ) ); 00072 connect( mWatcher, SIGNAL( readReady(KProcIO*) ), 00073 this, SLOT( slotReadStdout() ) ); 00074 00075 slotReadConfig(); 00076 mSysTray = new KWatchGnuPGTray( this ); 00077 mSysTray->show(); 00078 connect( mSysTray, SIGNAL( quitSelected() ), 00079 this, SLOT( slotQuit() ) ); 00080 setAutoSaveSettings(); 00081 } 00082 00083 KWatchGnuPGMainWindow::~KWatchGnuPGMainWindow() 00084 { 00085 delete mWatcher; 00086 } 00087 00088 void KWatchGnuPGMainWindow::slotClear() 00089 { 00090 mCentralWidget->clear(); 00091 mCentralWidget->append( tr("[%1] Log cleared").arg( QDateTime::currentDateTime().toString(Qt::ISODate) ) ); 00092 } 00093 00094 void KWatchGnuPGMainWindow::createActions() 00095 { 00096 (void)new KAction( i18n("C&lear History"), "history_clear", CTRL+Key_L, 00097 this, SLOT( slotClear() ), 00098 actionCollection(), "clear_log" ); 00099 (void)KStdAction::saveAs( this, SLOT(slotSaveAs()), actionCollection() ); 00100 (void)KStdAction::close( this, SLOT(close()), actionCollection() ); 00101 (void)KStdAction::quit( this, SLOT(slotQuit()), actionCollection() ); 00102 (void)KStdAction::preferences( this, SLOT(slotConfigure()), actionCollection() ); 00103 ( void )KStdAction::keyBindings(this, SLOT(configureShortcuts()), actionCollection()); 00104 ( void )KStdAction::configureToolbars(this, SLOT(slotConfigureToolbars()), actionCollection()); 00105 00106 #if 0 00107 (void)new KAction( i18n("Configure KWatchGnuPG..."), QString::fromLatin1("configure"), 00108 0, this, SLOT( slotConfigure() ), 00109 actionCollection(), "configure" ); 00110 #endif 00111 00112 } 00113 00114 void KWatchGnuPGMainWindow::configureShortcuts() 00115 { 00116 KKeyDialog::configure( actionCollection(), this ); 00117 } 00118 00119 void KWatchGnuPGMainWindow::slotConfigureToolbars() 00120 { 00121 KEditToolbar dlg( factory() ); 00122 00123 dlg.exec(); 00124 } 00125 00126 void KWatchGnuPGMainWindow::startWatcher() 00127 { 00128 disconnect( mWatcher, SIGNAL( processExited(KProcess*) ), 00129 this, SLOT( slotWatcherExited() ) ); 00130 if( mWatcher->isRunning() ) { 00131 mWatcher->kill(); 00132 while( mWatcher->isRunning() ) { 00133 kapp->eventLoop()->processEvents(QEventLoop::ExcludeUserInput); 00134 } 00135 mCentralWidget->append(tr("[%1] Log stopped") 00136 .arg( QDateTime::currentDateTime().toString(Qt::ISODate))); 00137 } 00138 mWatcher->clearArguments(); 00139 KConfig* config = kapp->config(); 00140 config->setGroup("WatchGnuPG"); 00141 *mWatcher << config->readEntry("Executable", WATCHGNUPGBINARY); 00142 *mWatcher << "--force"; 00143 *mWatcher << config->readEntry("Socket", WATCHGNUPGSOCKET); 00144 config->setGroup(QString::null); 00145 if( !mWatcher->start() ) { 00146 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." ) ); 00147 } else { 00148 mCentralWidget->append( tr("[%1] Log started") 00149 .arg( QDateTime::currentDateTime().toString(Qt::ISODate) ) ); 00150 } 00151 connect( mWatcher, SIGNAL( processExited(KProcess*) ), 00152 this, SLOT( slotWatcherExited() ) ); 00153 } 00154 00155 void KWatchGnuPGMainWindow::setGnuPGConfig() 00156 { 00157 QStringList logclients; 00158 // Get config object 00159 Kleo::CryptoConfig* cconfig = Kleo::CryptoBackendFactory::instance()->config(); 00160 if ( !cconfig ) 00161 return; 00162 //Q_ASSERT( cconfig ); 00163 KConfig* config = kapp->config(); 00164 config->setGroup("WatchGnuPG"); 00165 QStringList comps = cconfig->componentList(); 00166 for( QStringList::const_iterator it = comps.begin(); it != comps.end(); ++it ) { 00167 Kleo::CryptoConfigComponent* comp = cconfig->component( *it ); 00168 Q_ASSERT(comp); 00169 // Look for log-file entry in Debug group 00170 Kleo::CryptoConfigGroup* group = comp->group("Debug"); 00171 if( group ) { 00172 Kleo::CryptoConfigEntry* entry = group->entry("log-file"); 00173 if( entry ) { 00174 entry->setStringValue( QString("socket://")+ 00175 config->readEntry("Socket", 00176 WATCHGNUPGSOCKET )); 00177 logclients << QString("%1 (%2)").arg(*it).arg(comp->description()); 00178 } 00179 entry = group->entry("debug-level"); 00180 if( entry ) { 00181 entry->setStringValue( config->readEntry("LogLevel", "basic") ); 00182 } 00183 } 00184 } 00185 cconfig->sync(true); 00186 if( logclients.isEmpty() ) { 00187 KMessageBox::sorry( 0, i18n("There are no components available that support logging." ) ); 00188 } 00189 } 00190 00191 void KWatchGnuPGMainWindow::slotWatcherExited() 00192 { 00193 if( KMessageBox::questionYesNo( this, i18n("The watchgnupg logging process died.\nDo you want to try to restart it?") ) == KMessageBox::Yes ) { 00194 mCentralWidget->append( i18n("====== Restarting logging process =====") ); 00195 startWatcher(); 00196 } else { 00197 KMessageBox::sorry( this, i18n("The watchgnupg logging process is not running.\nThis log window is now completely useless." ) ); 00198 } 00199 } 00200 00201 void KWatchGnuPGMainWindow::slotReadStdout() 00202 { 00203 if ( !mWatcher ) 00204 return; 00205 QString str; 00206 while( mWatcher->readln(str,false) > 0 ) { 00207 mCentralWidget->append( str ); 00208 if( !isVisible() ) { 00209 // Change tray icon to show something happened 00210 // PENDING(steffen) 00211 mSysTray->setAttention(true); 00212 } 00213 } 00214 QTimer::singleShot( 0, this, SLOT(slotAckRead()) ); 00215 } 00216 00217 void KWatchGnuPGMainWindow::slotAckRead() { 00218 if ( mWatcher ) 00219 mWatcher->ackRead(); 00220 } 00221 00222 void KWatchGnuPGMainWindow::show() 00223 { 00224 mSysTray->setAttention(false); 00225 KMainWindow::show(); 00226 } 00227 00228 void KWatchGnuPGMainWindow::slotSaveAs() 00229 { 00230 QString filename = KFileDialog::getSaveFileName( QString::null, QString::null, 00231 this, i18n("Save Log to File") ); 00232 if( filename.isEmpty() ) return; 00233 QFile file(filename); 00234 if( file.exists() ) { 00235 if( KMessageBox::Yes != 00236 KMessageBox::warningYesNo( this, i18n("The file named \"%1\" already " 00237 "exists. Are you sure you want " 00238 "to overwrite it?").arg(filename), 00239 i18n("Overwrite File") ) ) { 00240 return; 00241 } 00242 } 00243 if( file.open( IO_WriteOnly ) ) { 00244 QTextStream st(&file); 00245 st << mCentralWidget->text(); 00246 file.close(); 00247 } 00248 } 00249 00250 void KWatchGnuPGMainWindow::slotQuit() 00251 { 00252 disconnect( mWatcher, SIGNAL( processExited(KProcess*) ), 00253 this, SLOT( slotWatcherExited() ) ); 00254 mWatcher->kill(); 00255 kapp->quit(); 00256 } 00257 00258 void KWatchGnuPGMainWindow::slotConfigure() 00259 { 00260 if( !mConfig ) { 00261 mConfig = new KWatchGnuPGConfig( this, "config dialog" ); 00262 connect( mConfig, SIGNAL( reconfigure() ), 00263 this, SLOT( slotReadConfig() ) ); 00264 } 00265 mConfig->loadConfig(); 00266 mConfig->exec(); 00267 } 00268 00269 void KWatchGnuPGMainWindow::slotReadConfig() 00270 { 00271 KConfig* config = kapp->config(); 00272 config->setGroup("LogWindow"); 00273 mCentralWidget->setWordWrap( config->readBoolEntry("WordWrap", false) 00274 ?QTextEdit::WidgetWidth 00275 :QTextEdit::NoWrap ); 00276 mCentralWidget->setMaxLogLines( config->readNumEntry( "MaxLogLen", 10000 ) ); 00277 setGnuPGConfig(); 00278 startWatcher(); 00279 } 00280 00281 bool KWatchGnuPGMainWindow::queryClose() 00282 { 00283 if ( !kapp->sessionSaving() ) { 00284 hide(); 00285 return false; 00286 } 00287 return KMainWindow::queryClose(); 00288 } 00289 00290 #include "kwatchgnupgmainwin.moc"
KDE Logo
This file is part of the documentation for certmanager Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Oct 1 15:18:47 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003