src/terralib/ws/ogc/wcs/qt/WCSConnectorDialog.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/ws/ogc/wcs/qt/WCSConnectorDialog.cpp
22 
23  \brief A dialog window for showing the WCS connector widget.
24 */
25 
26 #include "WCSConnectorDialog.h"
27 #include "ui_WCSConnectorDialogForm2.h"
28 
29 
30 // TerraLib
31 #include "../../../../core/translator/Translator.h"
32 #include "../../../../core/uri/URI.h"
33 #include "../../../../core/uri/Utils.h"
34 #include "../../../../dataaccess/datasource/DataSource.h"
35 #include "../../../../dataaccess/datasource/DataSourceFactory.h"
36 #include "../../../../dataaccess/datasource/DataSourceInfo.h"
37 #include "../../../../dataaccess/datasource/DataSourceManager.h"
38 #include "../dataaccess/Exception.h"
39 #include "../../../core/Exception.h"
40 #include "../../../../qt/af/ApplicationController.h"
41 
42 
43 // Boost
44 #include <boost/algorithm/string/case_conv.hpp>
45 #include <boost/uuid/random_generator.hpp>
46 #include <boost/uuid/uuid_io.hpp>
47 #include <boost/lexical_cast.hpp>
48 
49 //Qt
50 #include <QMessageBox>
51 #include <QFileDialog>
52 
54  : QDialog(parent, f),
55  m_ui(new Ui::WCSConnectorDialogForm2)
56 {
57  // add controls
58  m_ui->setupUi(this);
59 
60  // set icons
61  m_ui->m_wcsDirToolButton->setIcon(QIcon::fromTheme("folder"));
62 
63  // connect signal and slots
64  connect(m_ui->m_authCheckBox, SIGNAL(stateChanged(int)), this, SLOT(authCheckBoxStateChanged(int)));
65  connect(m_ui->m_openPushButton, SIGNAL(pressed()), this, SLOT(openPushButtonPressed()));
66  connect(m_ui->m_testPushButton, SIGNAL(pressed()), this, SLOT(testPushButtonPressed()));
67  connect(m_ui->m_helpPushButton, SIGNAL(pressed()), this, SLOT(helpPushButtonPressed()));
68  connect(m_ui->m_wcsDirToolButton, SIGNAL(clicked()), this, SLOT(wcsDirToolButtonClicked()));
69 
70  m_ui->m_authCheckBox->setCheckState(Qt::Unchecked);
71  m_ui->m_authUsernameLineEdit->setEnabled(false);
72  m_ui->m_authPasswordLineEdit->setEnabled(false);
73 }
74 
76 
78 {
79  return m_datasource;
80 }
81 
83 {
84  return m_driver;
85 }
86 
88 {
89  m_datasource = ds;
90 
91  if(m_datasource.get() != nullptr)
92  {
93  const te::core::URI& connInfo = m_datasource->getConnInfo();
94 
95  std::map<std::string, std::string> kvp = te::core::Expand(connInfo.query());
96 
97  std::string serviceURL = kvp["URI"];
98 
99  te::core::URI serviceURI (serviceURL);
100 
101  if(!serviceURI.user().empty() && !serviceURI.password().empty())
102  {
103  std::string baseURL = serviceURI.scheme() + std::string("://") + serviceURI.host();
104 
105  if(!serviceURI.port().empty())
106  {
107  baseURL = baseURL + std::string(":") + serviceURI.port();
108  }
109 
110  baseURL = baseURL + serviceURI.path();
111 
112  if(!serviceURI.query().empty())
113  {
114  baseURL = baseURL + std::string("?") + serviceURI.query();
115  }
116 
117  if(!serviceURI.fragment().empty())
118  {
119  baseURL = baseURL + std::string("#") + serviceURI.fragment();
120  }
121 
122  m_ui->m_serverLineEdit->setText(QString::fromUtf8(baseURL.c_str()));
123 
124 
125  m_ui->m_authCheckBox->setCheckState(Qt::Checked);
126  m_ui->m_authUsernameLineEdit->setEnabled(true);
127  m_ui->m_authPasswordLineEdit->setEnabled(true);
128 
129  m_ui->m_authUsernameLineEdit->setText(QString::fromUtf8(serviceURI.user().c_str()));
130  m_ui->m_authPasswordLineEdit->setText(QString::fromUtf8(serviceURI.password().c_str()));
131  }
132  else
133  {
134  m_ui->m_serverLineEdit->setText(QString::fromUtf8(serviceURL.c_str()));
135  }
136 
137  m_ui->m_datasourceTitleLineEdit->setText(QString::fromUtf8(m_datasource->getTitle().c_str()));
138 
139  m_ui->m_datasourceDescriptionTextEdit->setText(QString::fromUtf8(m_datasource->getDescription().c_str()));
140 
141  std::string usrDataDir = kvp["USERDATADIR"];
142 
143  m_ui->m_wcsDirLineEdit->setText(QString::fromUtf8(usrDataDir.c_str()));
144  }
145 }
146 
148 {
149  try
150  {
151  // Check if driver is loaded
152  if(te::da::DataSourceFactory::find("WCS2") == 0)
153  throw te::ws::ogc::wcs::da::Exception(TE_TR("Sorry! No data access driver loaded for WCS data sources!"));
154 
155  // Get the data source connection info based on form data
156  std::string dsInfo = getConnectionInfo();
157 
158  // Perform connection
159  std::unique_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("WCS2", dsInfo);
160  ds->open();
161  m_driver.reset(ds.release());
162 
163  if(m_driver.get() == nullptr)
164  throw te::ws::ogc::wcs::da::Exception(TE_TR("Could not open WCS data source due to an unknown error!"));
165 
166  QString title = m_ui->m_datasourceTitleLineEdit->text().trimmed();
167 
168  if(title.isEmpty())
169  title = m_ui->m_serverLineEdit->text().trimmed();
170 
171  if(m_datasource.get() == nullptr)
172  {
173  // Create a new data source based on the form data
175 
176  m_datasource->setConnInfo(dsInfo);
177 
178  boost::uuids::basic_random_generator<boost::mt19937> gen;
179  boost::uuids::uuid u = gen();
180  std::string dsId = boost::uuids::to_string(u);
181 
182  m_datasource->setId(dsId);
183  m_driver->setId(dsId);
184  m_datasource->setTitle(title.toUtf8().data());
185  m_datasource->setDescription(m_ui->m_datasourceDescriptionTextEdit->toPlainText().trimmed().toUtf8().data());
186  m_datasource->setAccessDriver("WCS2");
187  m_datasource->setType("WCS2");
188  }
189  else
190  {
191  m_driver->setId(m_datasource->getId());
192  m_datasource->setConnInfo(dsInfo);
193  m_datasource->setTitle(title.toUtf8().data());
194  m_datasource->setDescription(m_ui->m_datasourceDescriptionTextEdit->toPlainText().trimmed().toUtf8().data());
195  }
196  }
197  catch(te::ws::core::Exception& e)
198  {
199  if(const std::string* d =
200  boost::get_error_info<te::ErrorDescription>(e))
201  {
202  QMessageBox::warning(this,
203  tr("TerraLib Qt Components"),
204  tr(d->c_str()));
205  }
206  return;
207  }
208  catch(...)
209  {
210  QMessageBox::warning(this,
211  tr("TerraLib Qt Components"),
212  tr("Unknown error while opening WCS data source!"));
213  return;
214  }
215 
216  accept();
217 }
218 
220 {
221  try
222  {
223  // Check if driver is loaded
224  if(te::da::DataSourceFactory::find("WCS2") == 0)
225  throw te::ws::ogc::wcs::da::Exception(TE_TR("Sorry! No data access driver loaded for WCS data sources!"));
226 
227  // Perform connection
228  std::unique_ptr<te::da::DataSource> ds(te::da::DataSourceFactory::make("WCS2", getConnectionInfo()));
229 
230  if(ds.get() == nullptr)
231  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("Could not open WCS server!"));
232 
233  ds->open();
234 
235  QMessageBox::information(this,
236  tr("TerraLib Qt Components"),
237  tr("Data source is ok!"));
238 
239  ds->close();
240  }
241  catch(te::ws::core::Exception& e)
242  {
243  if(const std::string* d =
244  boost::get_error_info<te::ErrorDescription>(e))
245  {
246  QMessageBox::warning(this,
247  tr("TerraLib Qt Components"),
248  tr(d->c_str()));
249  }
250  }
251  catch(...)
252  {
253  QMessageBox::warning(this,
254  tr("TerraLib Qt Components"),
255  tr("Unknown error while testing WCS data source!"));
256  }
257 }
258 
260 {
261  QMessageBox::warning(this,
262  tr("TerraLib Qt Components"),
263  tr("Not implemented yet!\nWe will provide it soon!"));
264 }
265 
267 {
268  Qt::CheckState checkState = (Qt::CheckState) state;
269 
270  if(checkState == Qt::Checked)
271  {
272  m_ui->m_authUsernameLineEdit->setEnabled(true);
273  m_ui->m_authPasswordLineEdit->setEnabled(true);
274  }
275  else
276  {
277  m_ui->m_authUsernameLineEdit->setEnabled(false);
278  m_ui->m_authPasswordLineEdit->setEnabled(false);
279  m_ui->m_authUsernameLineEdit->clear();
280  m_ui->m_authPasswordLineEdit->clear();
281  }
282 }
283 
285 {
286  QString folderName = QFileDialog::getExistingDirectory(this, tr("Select a directory"), "");
287 
288  if (folderName.isEmpty())
289  return;
290 
291  m_ui->m_wcsDirLineEdit->setText(folderName);
292 }
293 
295 {
296  std::string strURI("wcs://");
297 
298  // Get the server URL
299  std::string serviceURL = m_ui->m_serverLineEdit->text().trimmed().toUtf8().constData();
300 
301  if (serviceURL.empty())
302  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("Please define the server address first!"));
303 
304  std::string usrDataDir = m_ui->m_wcsDirLineEdit->text().trimmed().toUtf8().constData();
305 
306  if (usrDataDir.empty())
307  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("Please define output directory to WCS files."));
308 
309  if(m_ui->m_authCheckBox->checkState() == Qt::Checked)
310  {
311 
312  if(m_ui->m_authUsernameLineEdit->text().isEmpty() || m_ui->m_authPasswordLineEdit->text().isEmpty())
313  {
314  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("Username or password is not defined!"));
315  }
316 
317  std::string username = m_ui->m_authUsernameLineEdit->text().toUtf8().constData();
318  std::string password = m_ui->m_authPasswordLineEdit->text().toUtf8().constData();
319  std::string userAndPassword = username + std::string(":") + password;
320 
321  te::core::URI aux(serviceURL);
322 
323  serviceURL = aux.scheme() + std::string("://") + userAndPassword + std::string("@")
324  + aux.host();
325 
326  if(!aux.port().empty())
327  {
328  serviceURL = serviceURL + std::string(":") + aux.port();
329  }
330 
331  serviceURL = serviceURL + aux.path();
332 
333  if(!aux.query().empty())
334  {
335  serviceURL = serviceURL + std::string("?") + aux.query();
336  }
337 
338  if(!aux.fragment().empty())
339  {
340  serviceURL = serviceURL + std::string("#") + aux.fragment();
341  }
342  }
343 
344  std::string encodedURL = te::core::URIEncode(serviceURL);
345 
346  strURI = strURI + "?URI="+ encodedURL + "&VERSION=2.0.1" + "&USERDATADIR=" + usrDataDir;
347 
348  return strURI;
349 }
A dialog window for showing the WCS connector widget.
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
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 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