QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgsautheditorwidgets.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsautheditorwidgets.cpp
3  ---------------------
4  begin : April 26, 2015
5  copyright : (C) 2015 by Boundless Spatial, Inc. USA
6  author : Larry Shaffer
7  email : lshaffer at boundlessgeo dot com
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #include "qgsautheditorwidgets.h"
18 #include "ui_qgsauthmethodplugins.h"
19 
20 #include <QAction>
21 #include <QMenu>
22 #include <QSettings>
23 #include <QWidget>
24 #include <QTableWidget>
25 
27 #include "qgsauthguiutils.h"
28 #include "qgsauthmanager.h"
29 
30 
32  : QDialog( parent )
33  , mAuthNotifyLayout( nullptr )
34  , mAuthNotify( nullptr )
35 {
36  if ( QgsAuthManager::instance()->isDisabled() )
37  {
38  mAuthNotifyLayout = new QVBoxLayout;
39  this->setLayout( mAuthNotifyLayout );
40  mAuthNotify = new QLabel( QgsAuthManager::instance()->disabledMessage(), this );
41  mAuthNotifyLayout->addWidget( mAuthNotify );
42  }
43  else
44  {
45  setupUi( this );
46  connect( buttonBox, SIGNAL( rejected() ), this, SLOT( reject() ) );
47 
48  setupTable();
49  populateTable();
50  }
51 }
52 
54 {
55 }
56 
57 void QgsAuthMethodPlugins::setupTable()
58 {
59  tblAuthPlugins->setColumnCount( 3 );
60  tblAuthPlugins->verticalHeader()->hide();
61  tblAuthPlugins->horizontalHeader()->setVisible( true );
62  tblAuthPlugins->setHorizontalHeaderItem( 0, new QTableWidgetItem( tr( "Method" ) ) );
63  tblAuthPlugins->setHorizontalHeaderItem( 1, new QTableWidgetItem( tr( "Description" ) ) );
64  tblAuthPlugins->setHorizontalHeaderItem( 2, new QTableWidgetItem( tr( "Works with" ) ) );
65  tblAuthPlugins->horizontalHeader()->setStretchLastSection( true );
66  tblAuthPlugins->setAlternatingRowColors( true );
67  tblAuthPlugins->setColumnWidth( 0, 150 );
68  tblAuthPlugins->setColumnWidth( 1, 300 );
69  tblAuthPlugins->setRowCount( QgsAuthManager::instance()->authMethodsKeys().size() );
70  tblAuthPlugins->verticalHeader()->setResizeMode( QHeaderView::ResizeToContents );
71  tblAuthPlugins->setSortingEnabled( true );
72  tblAuthPlugins->setSelectionBehavior( QAbstractItemView::SelectRows );
73 }
74 
75 void QgsAuthMethodPlugins::populateTable()
76 {
77  QgsAuthMethodsMap authmethods( QgsAuthManager::instance()->authMethodsMap() );
78 
79  int i = 0;
80  for ( QgsAuthMethodsMap::const_iterator it = authmethods.constBegin(); it != authmethods.constEnd(); ++it, i++ )
81  {
82  QgsAuthMethod *authmethod( it.value() );
83  if ( !authmethod )
84  {
85  continue;
86  }
87 
88  QTableWidgetItem *twi = new QTableWidgetItem( authmethod->key() );
89  twi->setFlags( twi->flags() & ~Qt::ItemIsEditable );
90  tblAuthPlugins->setItem( i, 0, twi );
91 
92  twi = new QTableWidgetItem( authmethod->displayDescription() );
93  twi->setFlags( twi->flags() & ~Qt::ItemIsEditable );
94  tblAuthPlugins->setItem( i, 1, twi );
95 
96  twi = new QTableWidgetItem( authmethod->supportedDataProviders().join( ", " ) );
97  twi->setFlags( twi->flags() & ~Qt::ItemIsEditable );
98  tblAuthPlugins->setItem( i, 2, twi );
99  }
100  tblAuthPlugins->sortItems( 0 );
101 }
102 
103 
104 
106  : QWidget( parent )
107  , mAuthUtilitiesMenu( nullptr )
108  , mActionSetMasterPassword( nullptr )
109  , mActionClearCachedMasterPassword( nullptr )
110  , mActionResetMasterPassword( nullptr )
111  , mActionClearCachedAuthConfigs( nullptr )
112  , mActionRemoveAuthConfigs( nullptr )
113  , mActionEraseAuthDatabase( nullptr )
114 {
115  setupUi( this );
116  if ( !QgsAuthManager::instance()->isDisabled() )
117  {
118  wdgtConfigEditor->setRelayMessages( false );
119  wdgtConfigEditor->setShowUtilitiesButton( false );
120  setupUtilitiesMenu();
121  }
122  else
123  {
124  grpbxManagers->setEnabled( false );
125  }
126 }
127 
129 {
130 }
131 
132 void QgsAuthEditorWidgets::on_btnCertManager_clicked()
133 {
134  QgsAuthCertManager *dlg = new QgsAuthCertManager( this );
135  dlg->setWindowModality( Qt::ApplicationModal );
136  dlg->resize( 750, 500 );
137  dlg->exec();
138  dlg->deleteLater();
139 }
140 
141 void QgsAuthEditorWidgets::on_btnAuthPlugins_clicked()
142 {
143  QgsAuthMethodPlugins *dlg = new QgsAuthMethodPlugins( this );
144  dlg->setWindowModality( Qt::WindowModal );
145  dlg->resize( 675, 500 );
146  dlg->exec();
147  dlg->deleteLater();
148 }
149 
150 void QgsAuthEditorWidgets::setupUtilitiesMenu()
151 {
152  connect( QgsAuthManager::instance(), SIGNAL( messageOut( const QString&, const QString&, QgsAuthManager::MessageLevel ) ),
153  this, SLOT( authMessageOut( const QString&, const QString&, QgsAuthManager::MessageLevel ) ) );
154 
155  // set up utility actions menu
156  mActionSetMasterPassword = new QAction( "Input master password", this );
157  mActionClearCachedMasterPassword = new QAction( "Clear cached master password", this );
158  mActionResetMasterPassword = new QAction( "Reset master password", this );
159  mActionClearCachedAuthConfigs = new QAction( "Clear cached authentication configurations", this );
160  mActionRemoveAuthConfigs = new QAction( "Remove all authentication configurations", this );
161  mActionEraseAuthDatabase = new QAction( "Erase authentication database", this );
162 
163  connect( mActionSetMasterPassword, SIGNAL( triggered() ), this, SLOT( setMasterPassword() ) );
164  connect( mActionClearCachedMasterPassword, SIGNAL( triggered() ), this, SLOT( clearCachedMasterPassword() ) );
165  connect( mActionResetMasterPassword, SIGNAL( triggered() ), this, SLOT( resetMasterPassword() ) );
166  connect( mActionClearCachedAuthConfigs, SIGNAL( triggered() ), this, SLOT( clearCachedAuthenticationConfigs() ) );
167  connect( mActionRemoveAuthConfigs, SIGNAL( triggered() ), this, SLOT( removeAuthenticationConfigs() ) );
168  connect( mActionEraseAuthDatabase, SIGNAL( triggered() ), this, SLOT( eraseAuthenticationDatabase() ) );
169 
170  mAuthUtilitiesMenu = new QMenu( this );
171  mAuthUtilitiesMenu->addAction( mActionSetMasterPassword );
172  mAuthUtilitiesMenu->addAction( mActionClearCachedMasterPassword );
173  mAuthUtilitiesMenu->addAction( mActionResetMasterPassword );
174  mAuthUtilitiesMenu->addSeparator();
175  mAuthUtilitiesMenu->addAction( mActionClearCachedAuthConfigs );
176  mAuthUtilitiesMenu->addAction( mActionRemoveAuthConfigs );
177  mAuthUtilitiesMenu->addSeparator();
178  mAuthUtilitiesMenu->addAction( mActionEraseAuthDatabase );
179 
180  btnAuthUtilities->setMenu( mAuthUtilitiesMenu );
181 }
182 
183 void QgsAuthEditorWidgets::setMasterPassword()
184 {
185  QgsAuthGuiUtils::setMasterPassword( messageBar(), messageTimeout() );
186 }
187 
188 void QgsAuthEditorWidgets::clearCachedMasterPassword()
189 {
190  QgsAuthGuiUtils::clearCachedMasterPassword( messageBar(), messageTimeout() );
191 }
192 
193 void QgsAuthEditorWidgets::resetMasterPassword()
194 {
195  QgsAuthGuiUtils::resetMasterPassword( messageBar(), messageTimeout(), this );
196 }
197 
198 void QgsAuthEditorWidgets::clearCachedAuthenticationConfigs()
199 {
200  QgsAuthGuiUtils::clearCachedAuthenticationConfigs( messageBar(), messageTimeout() );
201 }
202 
203 void QgsAuthEditorWidgets::removeAuthenticationConfigs()
204 {
205  QgsAuthGuiUtils::removeAuthenticationConfigs( messageBar(), messageTimeout(), this );
206 }
207 
208 void QgsAuthEditorWidgets::eraseAuthenticationDatabase()
209 {
210  QgsAuthGuiUtils::eraseAuthenticationDatabase( messageBar(), messageTimeout(), this );
211 }
212 
213 void QgsAuthEditorWidgets::authMessageOut( const QString& message, const QString& authtag, QgsAuthManager::MessageLevel level )
214 {
215  int levelint = ( int )level;
216  messageBar()->pushMessage( authtag, message, ( QgsMessageBar::MessageLevel )levelint, 7 );
217 }
218 
219 QgsMessageBar *QgsAuthEditorWidgets::messageBar()
220 {
221  return mMsgBar;
222 }
223 
224 int QgsAuthEditorWidgets::messageTimeout()
225 {
226  QSettings settings;
227  return settings.value( "/qgis/messageTimeout", 5 ).toInt();
228 }
static void resetMasterPassword(QgsMessageBar *msgbar, int timeout=0, QWidget *parent=nullptr)
Reset the cached master password, updating its hash in authentication database and reseting all exist...
static void eraseAuthenticationDatabase(QgsMessageBar *msgbar, int timeout=0, QWidget *parent=nullptr)
Completely clear out the authentication database (configs and master password)
void setupUi(QWidget *widget)
virtual void reject()
Dialog for viewing available authentication method plugins.
static QgsAuthManager * instance()
Enforce singleton pattern.
void setWindowModality(Qt::WindowModality windowModality)
static void clearCachedMasterPassword(QgsMessageBar *msgbar, int timeout=0)
Clear the currently cached master password (not its hash in database)
void rejected()
static void removeAuthenticationConfigs(QgsMessageBar *msgbar, int timeout=0, QWidget *parent=nullptr)
Remove all authentication configs.
void addAction(QAction *action)
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:42
int exec()
static void setMasterPassword(QgsMessageBar *msgbar, int timeout=0)
Sets the cached master password (and verifies it if its hash is in authentication database) ...
QString tr(const char *sourceText, const char *disambiguation, int n)
MessageLevel
Message log level (mirrors that of QgsMessageLog, so it can also output there)
static void clearCachedAuthenticationConfigs(QgsMessageBar *msgbar, int timeout=0)
Clear all cached authentication configs for session.
QSize size() const
void pushMessage(const QString &text, MessageLevel level=INFO, int duration=5)
convenience method for pushing a message to the bar
Definition: qgsmessagebar.h:90
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
void setLayout(QLayout *layout)
const_iterator constEnd() const
Dialog wrapper for widget to manage available certificate editors.
int toInt(bool *ok) const
QgsAuthMethodPlugins(QWidget *parent=nullptr)
Construct a dialog for viewing available authentication method plugins.
void deleteLater()
QAction * addSeparator()
QVariant value(const QString &key, const QVariant &defaultValue) const
const_iterator constBegin() const
void setFlags(QFlags< Qt::ItemFlag > flags)
Abstract base class for authentication method plugins.
Definition: qgsauthmethod.h:33
Qt::ItemFlags flags() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const
QgsAuthEditorWidgets(QWidget *parent=nullptr)
Construct a widget to contain various authentication editors.