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

kleopatra

  • sources
  • kde-4.12
  • 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 <QTextEdit>
60 #include <QDir>
61 #include <QEventLoop>
62 #include <QTimer>
63 #include <QTextCodec>
64 #include <QTextStream>
65 #include <QDateTime>
66 #include <QFileDialog>
67 
68 KWatchGnuPGMainWindow::KWatchGnuPGMainWindow( QWidget* parent )
69  : KXmlGuiWindow( parent, Qt::Window ), mConfig(0)
70 {
71  createActions();
72  createGUI();
73 
74  mCentralWidget = new KDLogTextWidget( this );
75  KDAB_SET_OBJECT_NAME( mCentralWidget );
76 
77  setCentralWidget( mCentralWidget );
78 
79  mWatcher = new KProcess;
80  connect( mWatcher, SIGNAL(finished(int,QProcess::ExitStatus)),
81  this, SLOT(slotWatcherExited(int,QProcess::ExitStatus)) );
82 
83  connect( mWatcher, SIGNAL(readyReadStandardOutput()),
84  this, SLOT(slotReadStdout()) );
85 
86  slotReadConfig();
87  mSysTray = new KWatchGnuPGTray( this );
88  mSysTray->show();
89  connect( mSysTray, SIGNAL(quitSelected()),
90  this, SLOT(slotQuit()) );
91 
92  setAutoSaveSettings();
93 }
94 
95 KWatchGnuPGMainWindow::~KWatchGnuPGMainWindow()
96 {
97  delete mWatcher;
98 }
99 
100 void KWatchGnuPGMainWindow::slotClear()
101 {
102  mCentralWidget->clear();
103  mCentralWidget->message( i18n("[%1] Log cleared", QDateTime::currentDateTime().toString(Qt::ISODate) ) );
104 }
105 
106 void KWatchGnuPGMainWindow::createActions()
107 {
108  KAction *action = actionCollection()->addAction( QLatin1String("clear_log") );
109  action->setIcon( KIcon(QLatin1String("edit-clear-history")) );
110  action->setText( i18n("C&lear History") );
111  connect(action, SIGNAL(triggered()), SLOT(slotClear()));
112  action->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_L));
113  (void)KStandardAction::saveAs( this, SLOT(slotSaveAs()), actionCollection() );
114  (void)KStandardAction::close( this, SLOT(close()), actionCollection() );
115  (void)KStandardAction::quit( this, SLOT(slotQuit()), actionCollection() );
116  (void)KStandardAction::preferences( this, SLOT(slotConfigure()), actionCollection() );
117  ( void )KStandardAction::keyBindings(this, SLOT(configureShortcuts()), actionCollection());
118  ( void )KStandardAction::configureToolbars(this, SLOT(slotConfigureToolbars()), actionCollection());
119 }
120 
121 void KWatchGnuPGMainWindow::configureShortcuts()
122 {
123  KShortcutsDialog::configure( actionCollection(), KShortcutsEditor::LetterShortcutsAllowed, this );
124 }
125 
126 void KWatchGnuPGMainWindow::slotConfigureToolbars()
127 {
128  KEditToolBar dlg( factory() );
129  dlg.exec();
130 }
131 
132 void KWatchGnuPGMainWindow::startWatcher()
133 {
134  disconnect( mWatcher, SIGNAL(finished(int,QProcess::ExitStatus)),
135  this, SLOT(slotWatcherExited(int,QProcess::ExitStatus)) );
136  if( mWatcher->state()== QProcess::Running ) {
137  mWatcher->kill();
138  while( mWatcher->state()== QProcess::Running ) {
139  qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
140  }
141  mCentralWidget->message(i18n("[%1] Log stopped", QDateTime::currentDateTime().toString(Qt::ISODate)));
142  }
143  mWatcher->clearProgram();
144 
145  {
146  const KConfigGroup config(KGlobal::config(), "WatchGnuPG");
147  *mWatcher << config.readEntry("Executable", WATCHGNUPGBINARY);
148  *mWatcher << QLatin1String("--force");
149  *mWatcher << config.readEntry("Socket", WATCHGNUPGSOCKET);
150  }
151 
152  mWatcher->setOutputChannelMode( KProcess::OnlyStdoutChannel );
153  mWatcher->start();
154  const bool ok = mWatcher->waitForStarted();
155  if( !ok) {
156  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." ) );
157  } else {
158  mCentralWidget->message( i18n("[%1] Log started",QDateTime::currentDateTime().toString(Qt::ISODate) ) );
159  }
160  connect( mWatcher, SIGNAL(finished(int,QProcess::ExitStatus)),
161  this, SLOT(slotWatcherExited(int,QProcess::ExitStatus)) );
162 }
163 
164 void KWatchGnuPGMainWindow::setGnuPGConfig()
165 {
166  QStringList logclients;
167  // Get config object
168  Kleo::CryptoConfig* cconfig = Kleo::CryptoBackendFactory::instance()->config();
169  if ( !cconfig )
170  return;
171  //Q_ASSERT( cconfig );
172  KConfigGroup config(KGlobal::config(), "WatchGnuPG");
173  const QStringList comps = cconfig->componentList();
174  for( QStringList::const_iterator it = comps.constBegin(); it != comps.constEnd(); ++it ) {
175  Kleo::CryptoConfigComponent* comp = cconfig->component( *it );
176  Q_ASSERT(comp);
177  // Look for log-file entry in Debug group
178  Kleo::CryptoConfigGroup* group = comp->group(QLatin1String("Debug"));
179  if( group ) {
180  Kleo::CryptoConfigEntry* entry = group->entry(QLatin1String("log-file"));
181  if( entry ) {
182  entry->setStringValue( QString::fromLatin1("socket://")+ config.readEntry("Socket", WATCHGNUPGSOCKET ));
183  logclients << QString::fromLatin1("%1 (%2)").arg(*it).arg(comp->description());
184  }
185  entry = group->entry(QLatin1String("debug-level"));
186  if( entry ) {
187  entry->setStringValue( config.readEntry("LogLevel", "basic") );
188  }
189  }
190  }
191  cconfig->sync(true);
192  if( logclients.isEmpty() ) {
193  KMessageBox::sorry( 0, i18n("There are no components available that support logging." ) );
194  }
195 }
196 
197 void KWatchGnuPGMainWindow::slotWatcherExited(int, QProcess::ExitStatus)
198 {
199  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 ) {
200  mCentralWidget->message( i18n("====== Restarting logging process =====") );
201  startWatcher();
202  } else {
203  KMessageBox::sorry( this, i18n("The watchgnupg logging process is not running.\nThis log window is unable to display any useful information." ) );
204  }
205 }
206 
207 void KWatchGnuPGMainWindow::slotReadStdout()
208 {
209  if ( !mWatcher )
210  return;
211  while(mWatcher->canReadLine()){
212  QString str = QString::fromUtf8( mWatcher->readLine() );
213  if ( str.endsWith( QLatin1Char('\n') ) )
214  str.chop( 1 );
215  if ( str.endsWith( QLatin1Char('\r') ) )
216  str.chop( 1 );
217  mCentralWidget->message(str);
218  if( !isVisible() ) {
219  // Change tray icon to show something happened
220  // PENDING(steffen)
221  mSysTray->setAttention(true);
222  }
223  }
224 }
225 
226 void KWatchGnuPGMainWindow::show()
227 {
228  mSysTray->setAttention(false);
229  KMainWindow::show();
230 }
231 
232 void KWatchGnuPGMainWindow::slotSaveAs()
233 {
234  const QString filename = QFileDialog::getSaveFileName( this, i18n("Save Log to File") );
235  if( filename.isEmpty() ) return;
236  QFile file(filename);
237  if( file.exists() ) {
238  if( KMessageBox::Yes !=
239  KMessageBox::warningYesNo( this, i18n("The file named \"%1\" already "
240  "exists. Are you sure you want "
241  "to overwrite it?", filename),
242  i18n("Overwrite File"), KStandardGuiItem::overwrite(), KStandardGuiItem::cancel() ) ) {
243  return;
244  }
245  }
246  if( file.open( QIODevice::WriteOnly ) )
247  QTextStream( &file ) << mCentralWidget->text();
248  else
249  KMessageBox::information( this, i18n("Could not save file %1: %2",
250  filename, file.errorString() ) );
251 }
252 
253 void KWatchGnuPGMainWindow::slotQuit()
254 {
255  disconnect( mWatcher, SIGNAL(finished(int,QProcess::ExitStatus)),
256  this, SLOT(slotWatcherExited(int,QProcess::ExitStatus)) );
257  mWatcher->kill();
258  kapp->quit();
259 }
260 
261 void KWatchGnuPGMainWindow::slotConfigure()
262 {
263  if( !mConfig ) {
264  mConfig = new KWatchGnuPGConfig( this );
265  mConfig->setObjectName( QLatin1String("config dialog") );
266  connect( mConfig, SIGNAL(reconfigure()),
267  this, SLOT(slotReadConfig()) );
268  }
269  mConfig->loadConfig();
270  mConfig->exec();
271 }
272 
273 void KWatchGnuPGMainWindow::slotReadConfig()
274 {
275  const KConfigGroup config(KGlobal::config(), "LogWindow");
276  const int maxLogLen = config.readEntry( "MaxLogLen", 10000 );
277  mCentralWidget->setHistorySize( maxLogLen < 1 ? -1 : maxLogLen );
278  setGnuPGConfig();
279  startWatcher();
280 }
281 
282 bool KWatchGnuPGMainWindow::queryClose()
283 {
284  if ( !kapp->sessionSaving() ) {
285  hide();
286  return false;
287  }
288  return KMainWindow::queryClose();
289 }
290 
291 #include "kwatchgnupgmainwin.moc"
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:282
KDLogTextWidget
A high-speed text display widget.
Definition: kdlogtextwidget.h:34
QWidget
KWatchGnuPGConfig
Definition: kwatchgnupgconfig.h:47
KWatchGnuPGTray::setAttention
void setAttention(bool att)
Definition: tray.cpp:64
kwatchgnupgmainwin.h
KXmlGuiWindow
KDAB_SET_OBJECT_NAME
#define KDAB_SET_OBJECT_NAME(x)
Definition: kdtoolsglobal.h:66
kwatchgnupg.h
WATCHGNUPGSOCKET
#define WATCHGNUPGSOCKET
Definition: kwatchgnupg.h:39
KWatchGnuPGMainWindow::show
void show()
Definition: kwatchgnupgmainwin.cpp:226
WATCHGNUPGBINARY
#define WATCHGNUPGBINARY
Definition: kwatchgnupg.h:38
tray.h
Kleo::FileDialog::getSaveFileName
QString getSaveFileName(QWidget *parent=0, const QString &caption=QString(), const QString &dirID=QString(), const QString &filter=QString())
Definition: filedialog.cpp:126
kdlogtextwidget.h
KWatchGnuPGMainWindow::~KWatchGnuPGMainWindow
~KWatchGnuPGMainWindow()
Definition: kwatchgnupgmainwin.cpp:95
KDLogTextWidget::setHistorySize
void setHistorySize(unsigned int size)
Definition: kdlogtextwidget.cpp:159
KDLogTextWidget::clear
void clear()
Definition: kdlogtextwidget.cpp:291
KDLogTextWidget::message
void message(const QString &msg, const QColor &color)
Definition: kdlogtextwidget.cpp:306
KWatchGnuPGMainWindow::KWatchGnuPGMainWindow
KWatchGnuPGMainWindow(QWidget *parent=0)
Definition: kwatchgnupgmainwin.cpp:68
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:41 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

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