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