• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kleopatra

  • sources
  • kde-4.14
  • kdepim
  • kleopatra
  • kwatchgnupg
kwatchgnupgmainwin.cpp
Go to the documentation of this file.
1 /*
2  kwatchgnupgmainwin.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2001,2002,2004 Klar�vdalens Datakonsult AB
6 
7  Kleopatra is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  Kleopatra 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 Qt library by Trolltech AS, Norway (or with modified versions
24  of Qt that use the same license as Qt), 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  Qt. 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 #include <config-kleopatra.h>
34 
35 #include "kwatchgnupgmainwin.h"
36 #include "kwatchgnupgconfig.h"
37 #include "kwatchgnupg.h"
38 #include "tray.h"
39 
40 #include "libkleo/kleo/cryptobackendfactory.h"
41 #include "libkleo/kleo/cryptoconfig.h"
42 
43 #include "utils/kdlogtextwidget.h"
44 
45 #include <kdebug.h>
46 #include <kmessagebox.h>
47 #include <klocale.h>
48 #include <kapplication.h>
49 #include <kaction.h>
50 #include <kactioncollection.h>
51 #include <kstandardaction.h>
52 #include <kprocess.h>
53 #include <kconfig.h>
54 #include <kedittoolbar.h>
55 #include <kshortcutsdialog.h>
56 #include <kglobal.h>
57 #include <kicon.h>
58 
59 #include <QEventLoop>
60 #include <QTextStream>
61 #include <QDateTime>
62 #include <QFileDialog>
63 
64 KWatchGnuPGMainWindow::KWatchGnuPGMainWindow( QWidget* parent )
65  : KXmlGuiWindow( parent, Qt::Window ), mConfig(0)
66 {
67  createActions();
68  createGUI();
69 
70  mCentralWidget = new KDLogTextWidget( this );
71  KDAB_SET_OBJECT_NAME( mCentralWidget );
72 
73  setCentralWidget( mCentralWidget );
74 
75  mWatcher = new KProcess;
76  connect( mWatcher, SIGNAL(finished(int,QProcess::ExitStatus)),
77  this, SLOT(slotWatcherExited(int,QProcess::ExitStatus)) );
78 
79  connect( mWatcher, SIGNAL(readyReadStandardOutput()),
80  this, SLOT(slotReadStdout()) );
81 
82  slotReadConfig();
83  mSysTray = new KWatchGnuPGTray( this );
84  mSysTray->show();
85  connect( mSysTray, SIGNAL(quitSelected()),
86  this, SLOT(slotQuit()) );
87 
88  setAutoSaveSettings();
89 }
90 
91 KWatchGnuPGMainWindow::~KWatchGnuPGMainWindow()
92 {
93  delete mWatcher;
94 }
95 
96 void KWatchGnuPGMainWindow::slotClear()
97 {
98  mCentralWidget->clear();
99  mCentralWidget->message( i18n("[%1] Log cleared", QDateTime::currentDateTime().toString(Qt::ISODate) ) );
100 }
101 
102 void KWatchGnuPGMainWindow::createActions()
103 {
104  KAction *action = actionCollection()->addAction( QLatin1String("clear_log") );
105  action->setIcon( KIcon(QLatin1String("edit-clear-history")) );
106  action->setText( i18n("C&lear History") );
107  connect(action, SIGNAL(triggered()), SLOT(slotClear()));
108  action->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_L));
109  (void)KStandardAction::saveAs( this, SLOT(slotSaveAs()), actionCollection() );
110  (void)KStandardAction::close( this, SLOT(close()), actionCollection() );
111  (void)KStandardAction::quit( this, SLOT(slotQuit()), actionCollection() );
112  (void)KStandardAction::preferences( this, SLOT(slotConfigure()), actionCollection() );
113  ( void )KStandardAction::keyBindings(this, SLOT(configureShortcuts()), actionCollection());
114  ( void )KStandardAction::configureToolbars(this, SLOT(slotConfigureToolbars()), actionCollection());
115 }
116 
117 void KWatchGnuPGMainWindow::configureShortcuts()
118 {
119  KShortcutsDialog::configure( actionCollection(), KShortcutsEditor::LetterShortcutsAllowed, this );
120 }
121 
122 void KWatchGnuPGMainWindow::slotConfigureToolbars()
123 {
124  KEditToolBar dlg( factory() );
125  dlg.exec();
126 }
127 
128 void KWatchGnuPGMainWindow::startWatcher()
129 {
130  disconnect( mWatcher, SIGNAL(finished(int,QProcess::ExitStatus)),
131  this, SLOT(slotWatcherExited(int,QProcess::ExitStatus)) );
132  if( mWatcher->state()== QProcess::Running ) {
133  mWatcher->kill();
134  while( mWatcher->state()== QProcess::Running ) {
135  qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
136  }
137  mCentralWidget->message(i18n("[%1] Log stopped", QDateTime::currentDateTime().toString(Qt::ISODate)));
138  }
139  mWatcher->clearProgram();
140 
141  {
142  const KConfigGroup config(KGlobal::config(), "WatchGnuPG");
143  *mWatcher << config.readEntry("Executable", WATCHGNUPGBINARY);
144  *mWatcher << QLatin1String("--force");
145  *mWatcher << config.readEntry("Socket", WATCHGNUPGSOCKET);
146  }
147 
148  mWatcher->setOutputChannelMode( KProcess::OnlyStdoutChannel );
149  mWatcher->start();
150  const bool ok = mWatcher->waitForStarted();
151  if( !ok) {
152  KMessageBox::sorry( this, i18n("The watchgnupg logging process could not be started.\nPlease install watchgnupg somewhere in your $PATH.\nThis log window is unable to display any useful information." ) );
153  } else {
154  mCentralWidget->message( i18n("[%1] Log started",QDateTime::currentDateTime().toString(Qt::ISODate) ) );
155  }
156  connect( mWatcher, SIGNAL(finished(int,QProcess::ExitStatus)),
157  this, SLOT(slotWatcherExited(int,QProcess::ExitStatus)) );
158 }
159 
160 void KWatchGnuPGMainWindow::setGnuPGConfig()
161 {
162  QStringList logclients;
163  // Get config object
164  Kleo::CryptoConfig* cconfig = Kleo::CryptoBackendFactory::instance()->config();
165  if ( !cconfig )
166  return;
167  //Q_ASSERT( cconfig );
168  KConfigGroup config(KGlobal::config(), "WatchGnuPG");
169  const QStringList comps = cconfig->componentList();
170  for( QStringList::const_iterator it = comps.constBegin(); it != comps.constEnd(); ++it ) {
171  Kleo::CryptoConfigComponent* comp = cconfig->component( *it );
172  Q_ASSERT(comp);
173  // Look for log-file entry in Debug group
174  Kleo::CryptoConfigGroup* group = comp->group(QLatin1String("Debug"));
175  if( group ) {
176  Kleo::CryptoConfigEntry* entry = group->entry(QLatin1String("log-file"));
177  if( entry ) {
178  entry->setStringValue( QString::fromLatin1("socket://")+ config.readEntry("Socket", WATCHGNUPGSOCKET ));
179  logclients << QString::fromLatin1("%1 (%2)").arg(*it).arg(comp->description());
180  }
181  entry = group->entry(QLatin1String("debug-level"));
182  if( entry ) {
183  entry->setStringValue( config.readEntry("LogLevel", "basic") );
184  }
185  }
186  }
187  cconfig->sync(true);
188  if( logclients.isEmpty() ) {
189  KMessageBox::sorry( 0, i18n("There are no components available that support logging." ) );
190  }
191 }
192 
193 void KWatchGnuPGMainWindow::slotWatcherExited(int, QProcess::ExitStatus)
194 {
195  if( KMessageBox::questionYesNo( this, i18n("The watchgnupg logging process died.\nDo you want to try to restart it?"), QString(), KGuiItem(i18n("Try Restart")), KGuiItem(i18n("Do Not Try")) ) == KMessageBox::Yes ) {
196  mCentralWidget->message( i18n("====== Restarting logging process =====") );
197  startWatcher();
198  } else {
199  KMessageBox::sorry( this, i18n("The watchgnupg logging process is not running.\nThis log window is unable to display any useful information." ) );
200  }
201 }
202 
203 void KWatchGnuPGMainWindow::slotReadStdout()
204 {
205  if ( !mWatcher )
206  return;
207  while(mWatcher->canReadLine()){
208  QString str = QString::fromUtf8( mWatcher->readLine() );
209  if ( str.endsWith( QLatin1Char('\n') ) )
210  str.chop( 1 );
211  if ( str.endsWith( QLatin1Char('\r') ) )
212  str.chop( 1 );
213  mCentralWidget->message(str);
214  if( !isVisible() ) {
215  // Change tray icon to show something happened
216  // PENDING(steffen)
217  mSysTray->setAttention(true);
218  }
219  }
220 }
221 
222 void KWatchGnuPGMainWindow::show()
223 {
224  mSysTray->setAttention(false);
225  KMainWindow::show();
226 }
227 
228 void KWatchGnuPGMainWindow::slotSaveAs()
229 {
230  const QString filename = QFileDialog::getSaveFileName( this, i18n("Save Log to File") );
231  if( filename.isEmpty() ) return;
232  QFile file(filename);
233  if( file.exists() ) {
234  if( KMessageBox::Yes !=
235  KMessageBox::warningYesNo( this, i18n("The file named \"%1\" already "
236  "exists. Are you sure you want "
237  "to overwrite it?", filename),
238  i18n("Overwrite File"), KStandardGuiItem::overwrite(), KStandardGuiItem::cancel() ) ) {
239  return;
240  }
241  }
242  if( file.open( QIODevice::WriteOnly ) )
243  QTextStream( &file ) << mCentralWidget->text();
244  else
245  KMessageBox::information( this, i18n("Could not save file %1: %2",
246  filename, file.errorString() ) );
247 }
248 
249 void KWatchGnuPGMainWindow::slotQuit()
250 {
251  disconnect( mWatcher, SIGNAL(finished(int,QProcess::ExitStatus)),
252  this, SLOT(slotWatcherExited(int,QProcess::ExitStatus)) );
253  mWatcher->kill();
254  kapp->quit();
255 }
256 
257 void KWatchGnuPGMainWindow::slotConfigure()
258 {
259  if( !mConfig ) {
260  mConfig = new KWatchGnuPGConfig( this );
261  mConfig->setObjectName( QLatin1String("config dialog") );
262  connect( mConfig, SIGNAL(reconfigure()),
263  this, SLOT(slotReadConfig()) );
264  }
265  mConfig->loadConfig();
266  mConfig->exec();
267 }
268 
269 void KWatchGnuPGMainWindow::slotReadConfig()
270 {
271  const KConfigGroup config(KGlobal::config(), "LogWindow");
272  const int maxLogLen = config.readEntry( "MaxLogLen", 10000 );
273  mCentralWidget->setHistorySize( maxLogLen < 1 ? -1 : maxLogLen );
274  setGnuPGConfig();
275  startWatcher();
276 }
277 
278 bool KWatchGnuPGMainWindow::queryClose()
279 {
280  if ( !kapp->sessionSaving() ) {
281  hide();
282  return false;
283  }
284  return KMainWindow::queryClose();
285 }
286 
QWidget
KWatchGnuPGConfig::loadConfig
void loadConfig()
Definition: kwatchgnupgconfig.cpp:175
KWatchGnuPGTray
Definition: tray.h:41
KDLogTextWidget::text
QString text
Definition: kdlogtextwidget.h:37
kwatchgnupgconfig.h
KWatchGnuPGMainWindow::queryClose
bool queryClose()
Definition: kwatchgnupgmainwin.cpp:278
KDLogTextWidget
A high-speed text display widget.
Definition: kdlogtextwidget.h:34
QString::chop
void chop(int n)
KWatchGnuPGConfig
Definition: kwatchgnupgconfig.h:47
QList::const_iterator
QFile
QTextStream
KWatchGnuPGTray::setAttention
void setAttention(bool att)
Definition: tray.cpp:64
kwatchgnupgmainwin.h
QString::fromUtf8
QString fromUtf8(const char *str, int size)
KXmlGuiWindow
QList::isEmpty
bool isEmpty() const
QString::isEmpty
bool isEmpty() const
KDAB_SET_OBJECT_NAME
#define KDAB_SET_OBJECT_NAME(x)
Definition: kdtoolsglobal.h:66
kwatchgnupg.h
QString::endsWith
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
WATCHGNUPGSOCKET
#define WATCHGNUPGSOCKET
Definition: kwatchgnupg.h:39
KWatchGnuPGMainWindow::show
void show()
Definition: kwatchgnupgmainwin.cpp:222
QString
WATCHGNUPGBINARY
#define WATCHGNUPGBINARY
Definition: kwatchgnupg.h:38
tray.h
QStringList
QLatin1Char
QDateTime::currentDateTime
QDateTime currentDateTime()
kdlogtextwidget.h
QTest::toString
char * toString(const T &value)
QLatin1String
QKeySequence
KWatchGnuPGMainWindow::~KWatchGnuPGMainWindow
~KWatchGnuPGMainWindow()
Definition: kwatchgnupgmainwin.cpp:91
KDLogTextWidget::setHistorySize
void setHistorySize(unsigned int size)
Definition: kdlogtextwidget.cpp:159
KDLogTextWidget::clear
void clear()
Definition: kdlogtextwidget.cpp:291
QFileDialog::getSaveFileName
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFlags< QFileDialog::Option > options)
QString::fromLatin1
QString fromLatin1(const char *str, int size)
QList::constEnd
const_iterator constEnd() const
KDLogTextWidget::message
void message(const QString &msg, const QColor &color)
Definition: kdlogtextwidget.cpp:306
QList::constBegin
const_iterator constBegin() const
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
KWatchGnuPGMainWindow::KWatchGnuPGMainWindow
KWatchGnuPGMainWindow(QWidget *parent=0)
Definition: kwatchgnupgmainwin.cpp:64
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:33:11 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kleopatra

Skip menu "kleopatra"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal