src/terralib/ws/ogc/wms/qt/WMSConnectorDialog.cpp
Go to the documentation of this file.
1 #include "WMSConnectorDialog.h"
2 #include "ui_WMSConnectorDialogForm.h"
3 
4 //TerraLib
5 #include "../../../../core/translator/Translator.h"
6 #include "../../../../core/uri/Utils.h"
7 #include "../../../../dataaccess/datasource/DataSource.h"
8 #include "../../../../dataaccess/datasource/DataSourceFactory.h"
9 #include "../../../../dataaccess/datasource/DataSourceInfo.h"
10 #include "../../../../dataaccess/datasource/DataSourceManager.h"
11 #include "../../../core/Exception.h"
12 #include "../../../core/CurlWrapper.h"
13 #include "../../../../qt/af/ApplicationController.h"
14 
15 // Boost
16 #include <boost/algorithm/string/case_conv.hpp>
17 #include <boost/uuid/random_generator.hpp>
18 #include <boost/uuid/uuid_io.hpp>
19 #include <boost/lexical_cast.hpp>
20 
21 //Qt
22 #include <QMessageBox>
23 
25  : QDialog(parent, f),
26  m_ui(new Ui::WMSConnectorDialogForm)
27 {
28  m_ui->setupUi(this);
29 
30  connect(m_ui->m_authCheckBox, SIGNAL(stateChanged(int)), this, SLOT(authCheckBoxStateChanged(int)));
31  connect(m_ui->m_openPushButton, SIGNAL(pressed()), this, SLOT(openPushButtonPressed()));
32  connect(m_ui->m_testPushButton, SIGNAL(pressed()), this, SLOT(testPushButtonPressed()));
33  connect(m_ui->m_helpPushButton, SIGNAL(pressed()), this, SLOT(helpPushButtonPressed()));
34 
35  m_ui->m_authCheckBox->setCheckState(Qt::Unchecked);
36  m_ui->m_authUsernameLineEdit->setEnabled(false);
37  m_ui->m_authPasswordLineEdit->setEnabled(false);
38 }
39 
41 
43 {
44  return m_datasource;
45 }
46 
48 {
49  return m_driver;
50 }
51 
53 {
54  m_datasource = ds;
55 
56  if(m_datasource.get() != nullptr)
57  {
58  const te::core::URI& connInfo = m_datasource->getConnInfo();
59 
60  std::map<std::string, std::string> kvp = te::core::Expand(connInfo.query());
61 
62  std::string serviceURL = kvp["URI"];
63 
64  te::core::URI serviceURI (serviceURL);
65 
66  if(!serviceURI.user().empty() && !serviceURI.password().empty())
67  {
68  std::string baseURL = serviceURI.scheme() + std::string("://") + serviceURI.host();
69 
70  if(!serviceURI.port().empty())
71  {
72  baseURL = baseURL + std::string(":") + serviceURI.port();
73  }
74 
75  baseURL = baseURL + serviceURI.path();
76 
77  if(!serviceURI.query().empty())
78  {
79  baseURL = baseURL + std::string("?") + serviceURI.query();
80  }
81 
82  if(!serviceURI.fragment().empty())
83  {
84  baseURL = baseURL + std::string("#") + serviceURI.fragment();
85  }
86 
87  m_ui->m_serverLineEdit->setText(QString::fromUtf8(baseURL.c_str()));
88 
89 
90  m_ui->m_authCheckBox->setCheckState(Qt::Checked);
91  m_ui->m_authUsernameLineEdit->setEnabled(true);
92  m_ui->m_authPasswordLineEdit->setEnabled(true);
93 
94  m_ui->m_authUsernameLineEdit->setText(QString::fromUtf8(serviceURI.user().c_str()));
95  m_ui->m_authPasswordLineEdit->setText(QString::fromUtf8(serviceURI.password().c_str()));
96  }
97  else
98  {
99  m_ui->m_serverLineEdit->setText(QString::fromUtf8(serviceURL.c_str()));
100  }
101 
102  m_ui->m_datasourceTitleLineEdit->setText(QString::fromUtf8(m_datasource->getTitle().c_str()));
103 
104  m_ui->m_dataSourceDescriptionTextEdit->setText(QString::fromUtf8(m_datasource->getDescription().c_str()));
105  }
106 }
107 
109 {
110  try
111  {
112  // Check if driver is loaded
113  if(te::da::DataSourceFactory::find("WMS2") == 0)
114  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("Sorry! No data access driver loaded for WMS data sources!"));
115 
116  // Get the data source connection info based on form data
117  std::string dsInfo = getConnectionInfo();
118 
119  // Perform connection
120  std::unique_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("WMS2", dsInfo);
121 
122  ds->open();
123  m_driver.reset(ds.release());
124 
125  if(m_driver.get() == nullptr)
126  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("Could not open WMS data source due to an unknown error!"));
127 
128  QString title = m_ui->m_datasourceTitleLineEdit->text().trimmed();
129 
130  if(title.isEmpty())
131  title = m_ui->m_serverLineEdit->text().trimmed();
132 
133  if(m_datasource.get() == nullptr)
134  {
135  // Create a new data source based on the form data
137 
138  m_datasource->setConnInfo(dsInfo);
139 
140  boost::uuids::basic_random_generator<boost::mt19937> gen;
141  boost::uuids::uuid u = gen();
142  std::string dsId = boost::uuids::to_string(u);
143 
144  m_datasource->setId(dsId);
145  m_driver->setId(dsId);
146  m_datasource->setTitle(title.toUtf8().data());
147  m_datasource->setDescription(m_ui->m_dataSourceDescriptionTextEdit->toPlainText().trimmed().toUtf8().data());
148  m_datasource->setAccessDriver("WMS2");
149  m_datasource->setType("WMS2");
150  }
151  else
152  {
153  m_driver->setId(m_datasource->getId());
154  m_datasource->setConnInfo(dsInfo);
155  m_datasource->setTitle(title.toUtf8().data());
156  m_datasource->setDescription(m_ui->m_dataSourceDescriptionTextEdit->toPlainText().trimmed().toUtf8().data());
157  }
158  }
159  catch(te::ws::core::Exception& e)
160  {
161  if(const std::string* d =
162  boost::get_error_info<te::ErrorDescription>(e))
163  {
164  QMessageBox::warning(this,
165  tr("TerraLib Qt Components"),
166  tr(d->c_str()));
167  }
168  return;
169  }
170  catch(...)
171  {
172  QMessageBox::warning(this,
173  tr("TerraLib Qt Components"),
174  tr("Unknown error while opening WMS data source!"));
175  return;
176  }
177 
178  accept();
179 }
180 
182 {
183  try
184  {
185  // Check if driver is loaded
186  if(te::da::DataSourceFactory::find("WMS2") == 0)
187  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("Sorry! No data access driver loaded for WMS data sources!"));
188 
189  // Get the data source connection info based on form data
190  std::string dsInfo = getConnectionInfo();
191 
192  // Perform connection
193  std::unique_ptr<te::da::DataSource> ds(te::da::DataSourceFactory::make("WMS2", dsInfo));
194 
195  if(ds.get() == nullptr)
196  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("Could not open WMS server!"));
197 
198  ds->open();
199 
200  QMessageBox::information(this,
201  tr("TerraLib Qt Components"),
202  tr("Data source is ok!"));
203 
204  ds->close();
205  }
206  catch(te::ws::core::Exception& e)
207  {
208  if(const std::string* d =
209  boost::get_error_info<te::ErrorDescription>(e))
210  {
211  QMessageBox::warning(this,
212  tr("TerraLib Qt Components"),
213  tr(d->c_str()));
214  }
215  }
216  catch(...)
217  {
218  QMessageBox::warning(this,
219  tr("TerraLib Qt Components"),
220  tr("Unknown error while testing WMS data source!"));
221  }
222 }
223 
225 {
226  QMessageBox::warning(this,
227  tr("TerraLib Qt Components"),
228  tr("Not implemented yet!\nWe will provide it soon!"));
229 }
230 
232 {
233  Qt::CheckState checkState = (Qt::CheckState) state;
234 
235  if(checkState == Qt::Checked)
236  {
237  m_ui->m_authUsernameLineEdit->setEnabled(true);
238  m_ui->m_authPasswordLineEdit->setEnabled(true);
239  }
240  else
241  {
242  m_ui->m_authUsernameLineEdit->setEnabled(false);
243  m_ui->m_authPasswordLineEdit->setEnabled(false);
244  m_ui->m_authUsernameLineEdit->clear();
245  m_ui->m_authPasswordLineEdit->clear();
246  }
247 }
248 
250 {
251  std::string serviceURL; // Auxiliary string used to hold temporary data
252 
253  std::string strURI("wms://");
254 
255  // Get the server URL
256  serviceURL = m_ui->m_serverLineEdit->text().trimmed().toUtf8().constData();
257 
258  if (serviceURL.empty())
259  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("Please define the server address first!"));
260 
261  if(m_ui->m_authCheckBox->checkState() == Qt::Checked)
262  {
263 
264  if(m_ui->m_authUsernameLineEdit->text().isEmpty() || m_ui->m_authPasswordLineEdit->text().isEmpty())
265  {
266  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("Username or password is not defined!"));
267  }
268 
269  std::string username = m_ui->m_authUsernameLineEdit->text().toUtf8().constData();
270  std::string password = m_ui->m_authPasswordLineEdit->text().toUtf8().constData();
271  std::string userAndPassword = username + std::string(":") + password;
272 
273  te::core::URI aux(serviceURL);
274 
275  serviceURL = aux.scheme() + std::string("://") + userAndPassword + std::string("@")
276  + aux.host();
277 
278  if(!aux.port().empty())
279  {
280  serviceURL = serviceURL + std::string(":") + aux.port();
281  }
282 
283  serviceURL = serviceURL + aux.path();
284 
285  if(!aux.query().empty())
286  {
287  serviceURL = serviceURL + std::string("?") + aux.query();
288  }
289 
290  if(!aux.fragment().empty())
291  {
292  serviceURL = serviceURL + std::string("#") + aux.fragment();
293  }
294  }
295 
296  std::string encodedURL = te::core::URIEncode(serviceURL);
297 
298  std::string usrDataDir = te::qt::af::AppCtrlSingleton::getInstance().getUserDataDir().toUtf8().data();
299 
300  strURI = strURI + "?URI="+ encodedURL + "&VERSION=1.1.1" + "&USERDATADIR=" + usrDataDir;
301 
302  return strURI;
303 }
std::string path() const
Retrieving the path.
Definition: URI.cpp:118
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
std::string scheme() const
Retrieving the scheme.
Definition: URI.cpp:93
boost::shared_ptr< DataSource > DataSourcePtr
TECOREEXPORT std::string URIEncode(const std::string &srcUri)
Encodes an decoded URI. The algorithm implementation is based on http://www.codeguru.com/cpp/cpp/algorithms/strings/article.php/c12759/URI-Encoding-and-Decoding.htm.
std::string fragment() const
Retrieving the fragment.
Definition: URI.cpp:128
std::string password() const
Retrieving the password information.
Definition: URI.cpp:103
static te::dt::Date ds(2010, 01, 01)
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
A dialog window for showing the WMS connector widget.
std::string query() const
Retrieving the query.
Definition: URI.cpp:123
boost::error_info< struct tag_error_description, std::string > ErrorDescription
The base type for error report messages.
static ApplicationController & getInstance()
It returns a reference to the singleton instance.
static te::dt::DateTime d(2010, 8, 9, 15, 58, 39)
std::string port() const
Retrieving the port.
Definition: URI.cpp:113
std::string host() const
Retrieving the host.
Definition: URI.cpp:108
Base exception class for WS Core Runtime Library.
A class for representing an Uniform Resource Identifier (URI).
Definition: URI.h:49
TECOREEXPORT std::map< std::string, std::string > Expand(const std::string &query_str)
Split a query string into its components.
A class that represents a data source component.
std::string user() const
Retrieving the user information.
Definition: URI.cpp:98
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr