1 /***************************************************************************
2     qgsauthconfigselect.h
3     ---------------------
4     begin                : October 5, 2014
5     copyright            : (C) 2014 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 #ifndef QGSAUTHCONFIGSELECT_H
18 #define QGSAUTHCONFIGSELECT_H
19 
20 #include <QWidget>
21 #include <QLabel>
22 #include "qgis_sip.h"
23 
24 #include "ui_qgsauthconfigselect.h"
25 #include "qgsauthconfig.h"
26 #include "qgis_gui.h"
27 
28 
29 /**
30  * \ingroup gui
31  * \brief Selector widget for authentication configs
32  */
33 class GUI_EXPORT QgsAuthConfigSelect : public QWidget, private Ui::QgsAuthConfigSelect
34 {
35     Q_OBJECT
36 
37   public:
38 
39     /**
40      * Create a dialog for setting an associated authentication config, either
41      * from existing configs, or creating/removing them from auth database
42      * \param parent Parent widget
43      * \param dataprovider The key of the calling layer provider, if applicable
44      */
45     explicit QgsAuthConfigSelect( QWidget *parent SIP_TRANSFERTHIS = nullptr, const QString &dataprovider = QString() );
46 
47     //! Sets the authentication config id for the resource
48     void setConfigId( const QString &authcfg );
49 
50     //! Gets the authentication config id for the resource
configId()51     const QString configId() const { return mAuthCfg; }
52 
53     //! Sets key of layer provider, if applicable
54     void setDataProviderKey( const QString &key );
55 
56   signals:
57     //! Emitted when authentication config is changed or missing
58     void selectedConfigIdChanged( const QString &authcfg );
59 
60     //! Emitted when authentication config is removed
61     void selectedConfigIdRemoved( const QString &authcfg );
62 
63   public slots:
64     //! Show a small message bar with a close button
65     void showMessage( const QString &msg );
66 
67     //! Clear and hide small message bar
68     void clearMessage();
69 
70   private slots:
71     void loadConfig();
72     void clearConfig();
73     void validateConfig();
74     void populateConfigSelector();
75 
76     void cmbConfigSelect_currentIndexChanged( int index );
77 
78     void btnConfigAdd_clicked();
79 
80     void btnConfigEdit_clicked();
81 
82     void btnConfigRemove_clicked();
83 
84     void btnConfigMsgClear_clicked();
85 
86   private:
87     void loadAvailableConfigs();
88 
89     QString mAuthCfg;
90     QString mDataProvider;
91     QgsAuthMethodConfigsMap mConfigs;
92 
93     bool mDisabled = false;
94     QVBoxLayout *mAuthNotifyLayout = nullptr;
95     QLabel *mAuthNotify = nullptr;
96     bool mTemporarilyBlockLoad = false;
97 };
98 
99 
100 //////////////// Embed in dialog ///////////////////
101 
102 #include "ui_qgsauthconfiguriedit.h"
103 
104 class QPushButton;
105 
106 /**
107  * \ingroup gui
108  * \brief Dialog wrapper of select widget to edit an authcfg in a data source URI
109  */
110 class GUI_EXPORT QgsAuthConfigUriEdit : public QDialog, private Ui::QgsAuthConfigUriEdit
111 {
112     Q_OBJECT
113 
114   public:
115 
116     /**
117      * Construct wrapper dialog for select widget to edit an authcfg in a data source URI
118      * \param parent Parent widget
119      * \param datauri URI QString with of without an authcfg=ID string
120      * \param dataprovider The key of the calling layer provider, if applicable
121      */
122     explicit QgsAuthConfigUriEdit( QWidget *parent SIP_TRANSFERTHIS = nullptr,
123                                    const QString &datauri = QString(),
124                                    const QString &dataprovider = QString() );
125 
126     //! Sets the data source URI to parse
127     void setDataSourceUri( const QString &datauri );
128 
129     //! The returned, possibly edited data source URI
130     QString dataSourceUri();
131 
132     //! Whether a string contains an authcfg ID
133     static bool hasConfigId( const QString &txt );
134 
135   private slots:
136     void saveChanges();
137 
138     void resetChanges();
139 
140     void authCfgUpdated( const QString &authcfg );
141 
142     void authCfgRemoved( const QString &authcfg );
143 
144   private:
145     int authCfgIndex();
146 
147     QString authCfgFromUri();
148 
149     void selectAuthCfgInUri();
150 
151     void updateUriWithAuthCfg();
152 
153     void removeAuthCfgFromUri();
154 
155     QString mAuthCfg;
156     QString mDataUri;
157     QString mDataUriOrig;
158 
159     bool mDisabled = false;
160     QVBoxLayout *mAuthNotifyLayout = nullptr;
161     QLabel *mAuthNotify = nullptr;
162 };
163 
164 #endif // QGSAUTHCONFIGSELECT_H
165