• 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
  • kgpgconf
mainwindow.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  mainwindow.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2007 Klarälvdalens 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 "mainwindow.h"
34 
35 #include "configreader.h"
36 #include "configuration.h"
37 #include "configwriter.h"
38 #include "exception.h"
39 
40 #include <QFile>
41 #include <QFileDialog>
42 #include <QMessageBox>
43 #include <QMenu>
44 #include <QMenuBar>
45 #include <QStringList>
46 #include <QTemporaryFile>
47 #include <QTimer>
48 #include <QTreeWidgetItem>
49 
50 #include <cassert>
51 
52 MainWindow::MainWindow( QWidget* parent, Qt::WindowFlags flags ) : QMainWindow( parent, flags ), m_config( 0 ), m_selectedEntry( 0 )
53 {
54  QWidget* mainWidget = new QWidget( this );
55  m_ui.setupUi( mainWidget );
56  setCentralWidget( mainWidget );
57  connect( m_ui.treeWidget, SIGNAL(itemSelectionChanged()),
58  SLOT(treeWidgetItemSelectionChanged()) );
59  connect( m_ui.treeWidget, SIGNAL(itemChanged(QTreeWidgetItem*,int)),
60  SLOT(treeWidgetItemChanged(QTreeWidgetItem*,int)) );
61  connect( m_ui.readOnlyBox, SIGNAL(stateChanged(int)), SLOT(readOnlyStateChanged(int)) );
62  connect( m_ui.valueLE, SIGNAL(textChanged(QString)), SLOT(optionValueChanged()) );
63  connect( m_ui.useCustomRB, SIGNAL(toggled(bool)), m_ui.valueLE, SLOT(setEnabled(bool)) );
64  connect( m_ui.useDefaultRB, SIGNAL(toggled(bool)), SLOT(useDefaultToggled(bool)) );
65 
66  QMenu* const fileMenu = menuBar()->addMenu( i18n( "&File" ) );
67  fileMenu->addAction( i18n( "Save As..." ), this, SLOT(saveAs()) );
68  fileMenu->addSeparator();
69  QAction* const quit = fileMenu->addAction( i18n( "Quit" ), qApp, SLOT(quit()) );
70  quit->setShortcut( Qt::CTRL + Qt::Key_Q );
71  resize( 640, 480 );
72  QTimer::singleShot( 0, this, SLOT(delayedInit()) );
73 }
74 
75 void MainWindow::delayedInit()
76 {
77  ConfigReader reader;
78  try
79  {
80  m_config = reader.readConfig();
81  readConfiguration();
82  }
83  catch ( const GpgConfRunException& e )
84  {
85  QMessageBox::critical( this, i18n( "Setup Error" ), i18n( "KGpgConf could not execute gpgconf.exe.\n\nError: %1\nError Code: %2", e.message(), e.errorCode() ) );
86  qApp->quit();
87  }
88  catch( const MalformedGpgConfOutputException& e )
89  {
90  QMessageBox::critical( this, i18n( "Parsing Error" ), i18n( "An error occurred while reading the current configuration.\n\nError: %1", e.message() ) );
91  qApp->quit();
92  }
93  catch( const KGpgConfException& e )
94  {
95  QMessageBox::critical( this, i18n( "Error" ), i18n( "An error occurred while reading the current configuration.\n\nError: %1", e.message() ) );
96  qApp->quit();
97  }
98 }
99 
100 MainWindow::~MainWindow()
101 {
102  delete m_config;
103 }
104 
105 void MainWindow::treeWidgetItemSelectionChanged()
106 {
107  m_selectedEntry = 0;
108  const QList<QTreeWidgetItem*> selected = m_ui.treeWidget->selectedItems();
109  assert( selected.count() <= 1 );
110  ConfigEntry* const entry = selected.isEmpty() ? 0 : m_itemToEntry[selected.first()];
111  m_ui.componentLabel->setText( entry ? m_componentForEntry[entry]->name() : QString() );
112  m_ui.optionLabel->setText( entry ? entry->name() : QString() );
113  m_ui.descriptionLabel->setText( entry ? entry->description() : QString() );
114  m_ui.typeLabel->setText( entry ? entry->typeDescription() : QString() );
115  m_ui.valueLE->setText( entry ? entry->stringValue() : QString() );
116  m_ui.useDefaultRB->setChecked( entry && entry->useBuiltInDefault() );
117  m_ui.useCustomRB->setChecked( entry && !entry->useBuiltInDefault() );
118  m_ui.readOnlyBox->setCheckState( ( entry && entry->mutability() == ConfigEntry::NoChange ) ? Qt::Checked : Qt::Unchecked );
119  m_ui.readOnlyBox->setEnabled( entry );
120  m_ui.useCustomRB->setEnabled( entry );
121  m_ui.useDefaultRB->setEnabled( entry );
122  m_ui.valueLE->setEnabled( entry );
123  m_ui.optionLabelLabel->setEnabled( entry );
124  m_ui.typeLabelLabel->setEnabled( entry );
125  m_ui.componentLabelLabel->setEnabled( entry );
126  m_ui.descriptionLabelLabel->setEnabled( entry );
127  m_selectedEntry = entry;
128 }
129 
130 void MainWindow::treeWidgetItemChanged( QTreeWidgetItem* item, int column )
131 {
132  if ( column != ReadOnlyColumn )
133  return;
134  ConfigEntry* entry = m_itemToEntry[item];
135  assert( entry );
136  entry->setMutability( item->checkState( ReadOnlyColumn ) == Qt::Checked ? ConfigEntry::NoChange : ConfigEntry::UnspecifiedMutability );
137  if ( entry == m_selectedEntry )
138  {
139  m_ui.readOnlyBox->setCheckState( entry->mutability() == ConfigEntry::NoChange ? Qt::Checked : Qt::Unchecked );
140  }
141 }
142 
143 void MainWindow::useDefaultToggled( bool useDefault )
144 {
145  if ( !m_selectedEntry )
146  return;
147  m_selectedEntry->setUseBuiltInDefault( useDefault );
148 }
149 
150 void MainWindow::readOnlyStateChanged( int state )
151 {
152  if ( !m_selectedEntry )
153  return;
154  assert( state != Qt::PartiallyChecked );
155  m_selectedEntry->setMutability( state == Qt::Checked ? ConfigEntry::NoChange: ConfigEntry::UnspecifiedMutability );
156  QTreeWidgetItem* const item = m_entryToItem[m_selectedEntry];
157  assert( item );
158  item->setCheckState( ReadOnlyColumn, static_cast<Qt::CheckState>( state ) );
159 }
160 
161 void MainWindow::optionValueChanged()
162 {
163  if ( !m_selectedEntry )
164  return;
165  m_selectedEntry->setValueFromUiString( m_ui.valueLE->text() );
166 }
167 
168 void MainWindow::readConfiguration()
169 {
170  QStringList components = m_config->componentList();
171  qSort( components );
172  Q_FOREACH ( const QString& i, components )
173  {
174  ConfigComponent* const component = m_config->component( i );
175  assert( component );
176  QTreeWidgetItem* const componentItem = new QTreeWidgetItem;
177  componentItem->setText( NameColumn, component->name() );
178  m_ui.treeWidget->addTopLevelItem( componentItem );
179  Q_FOREACH ( const QString& j, component->groupList() )
180  {
181  ConfigGroup* const group = component->group( j );
182  assert( group );
183  QTreeWidgetItem* const groupItem = new QTreeWidgetItem;
184  groupItem->setText( NameColumn, group->name() );
185  componentItem->addChild( groupItem );
186  Q_FOREACH( const QString& k, group->entryList() )
187  {
188  ConfigEntry* const entry = group->entry( k );
189  assert( entry );
190  QTreeWidgetItem* const entryItem = new QTreeWidgetItem;
191  entryItem->setData( NameColumn, IsOptionRole, true );
192  entryItem->setText( NameColumn, entry->name() );
193 // entryItem->setText( ValueColumn, QString() );
194  entryItem->setCheckState( ReadOnlyColumn, entry->mutability() == ConfigEntry::NoChange ? Qt::Checked : Qt::Unchecked );
195  groupItem->addChild( entryItem );
196  m_componentForEntry[entry] = component;
197  m_itemToEntry[entryItem] = entry;
198  m_entryToItem[entry] = entryItem;
199  }
200  }
201  }
202 }
203 
204 void MainWindow::saveToFile( const QString& fileName )
205 {
206  if ( fileName.isEmpty() )
207  return;
208 
209  QTemporaryFile tmp( fileName );
210  if ( !tmp.open() )
211  {
212  QMessageBox::warning( this, i18n( "Write Error" ), i18n( "Could not open file %1 for writing. You might not have the permission to write to that file.", fileName ) );
213  return;
214  }
215  tmp.setTextModeEnabled( true );
216  ConfigWriter writer( &tmp );
217  if ( writer.writeConfig( m_config ) )
218  {
219  const QString tmpFileName = tmp.fileName(); // close() clears fileName()
220  tmp.close();
221  tmp.setAutoRemove( false );
222  for ( int i = 0; i < 10; ++i )
223  {
224  if ( QFile::rename( tmpFileName, fileName ) )
225  return;
226  else
227  QFile::remove( fileName );
228  }
229  tmp.setAutoRemove( true );
230  }
231  QMessageBox::critical( this, i18n( "Write Error" ), i18n( "Error while writing to file %1.", fileName ) );
232 
233 }
234 
235 void MainWindow::saveAs()
236 {
237  saveToFile( QFileDialog::getSaveFileName( this, i18n( "Save As"), QString(), QLatin1String("*.conf") ) );
238 }
239 
flags
static const char * flags[]
Definition: readerstatus.cpp:114
QWidget
ConfigComponent::groupList
QStringList groupList() const
Definition: configuration.cpp:175
Config::component
ConfigComponent * component(const QString &name) const
Definition: configuration.cpp:133
QTreeWidgetItem::checkState
Qt::CheckState checkState(int column) const
KGpgConfException::message
QString message() const
Definition: exception.h:43
ConfigComponent::name
QString name() const
Definition: configuration.cpp:155
QMainWindow::menuBar
QMenuBar * menuBar() const
QFile::remove
bool remove()
ConfigGroup::entry
ConfigEntry * entry(const QString &name) const
Definition: configuration.cpp:248
mainwindow.h
QFile::rename
bool rename(const QString &newName)
QMenu::addAction
void addAction(QAction *action)
ConfigEntry::setValueFromUiString
void setValueFromUiString(const QString &str)
Definition: configuration.cpp:473
ConfigEntry::setUseBuiltInDefault
void setUseBuiltInDefault(bool useDefault)
Definition: configuration.cpp:319
QTreeWidgetItem::setData
virtual void setData(int column, int role, const QVariant &value)
Config::componentList
QStringList componentList() const
Definition: configuration.cpp:128
exception.h
QWidget::resize
void resize(int w, int h)
ConfigComponent::group
ConfigGroup * group(const QString &name) const
Definition: configuration.cpp:180
ConfigWriter
Definition: configwriter.h:40
QObject::name
const char * name() const
QWidget::setEnabled
void setEnabled(bool)
QList::count
int count(const T &value) const
GpgConfRunException::errorCode
int errorCode() const
Definition: exception.h:55
components
static const char *const components[]
Definition: selftestcommand.cpp:72
ConfigComponent
Definition: configuration.h:64
QList::isEmpty
bool isEmpty() const
configuration.h
QString::isEmpty
bool isEmpty() const
MalformedGpgConfOutputException
Definition: exception.h:61
QMainWindow::setCentralWidget
void setCentralWidget(QWidget *widget)
QList::first
T & first()
QMenu::addSeparator
QAction * addSeparator()
QString
QList
Definition: commands/command.h:46
ConfigGroup
Definition: configuration.h:94
QStringList
QTreeWidgetItem::addChild
void addChild(QTreeWidgetItem *child)
QMainWindow
ConfigReader::readConfig
Config * readConfig() const
Definition: configreader.cpp:119
QMenu
ConfigReader
Definition: configreader.h:42
ConfigEntry::mutability
Mutability mutability() const
Definition: configuration.cpp:309
QAction::setShortcut
void setShortcut(const QKeySequence &shortcut)
KGpgConfException
Definition: exception.h:38
QTreeWidgetItem::setCheckState
void setCheckState(int column, Qt::CheckState state)
ConfigEntry::setMutability
void setMutability(Mutability mutability)
Definition: configuration.cpp:301
QMenuBar::addMenu
QAction * addMenu(QMenu *menu)
QTreeWidgetItem
QLatin1String
QAction
QWidget::QWidget
QWidget(QWidget *parent, QFlags< Qt::WindowType > f)
QTreeWidgetItem::setText
void setText(int column, const QString &text)
QFileDialog::getSaveFileName
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFlags< QFileDialog::Option > options)
QMessageBox::critical
StandardButton critical(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
ConfigEntry::name
QString name() const
Definition: configuration.cpp:275
ConfigEntry::NoChange
Definition: configuration.h:129
configwriter.h
QMessageBox::warning
StandardButton warning(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
Qt::WindowFlags
typedef WindowFlags
configreader.h
MainWindow::~MainWindow
~MainWindow()
Definition: mainwindow.cpp:100
ConfigGroup::entryList
QStringList entryList() const
Definition: configuration.cpp:243
QTemporaryFile
ConfigEntry::UnspecifiedMutability
Definition: configuration.h:128
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
ConfigEntry
Definition: configuration.h:123
MainWindow::MainWindow
MainWindow(QWidget *parent=0, Qt::WindowFlags flags=0)
Definition: mainwindow.cpp:52
GpgConfRunException
Definition: exception.h:49
ConfigGroup::name
QString name() const
Definition: configuration.cpp:223
QTimer::singleShot
singleShot
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