attic/src/qt/plugins/datasource/wms/WMSConnectorDialog.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/qt/plugins/datasource/wms/WMSConnectorDialog.cpp
22 
23  \brief A dialog window for showing the WMS connector widget.
24 */
25 
26 // TerraLib
27 #include "../../../../core/translator/Translator.h"
28 #include "../../../../core/uri/URI.h"
29 #include "../../../../core/uri/Utils.h"
30 #include "../../../../dataaccess/datasource/DataSource.h"
31 #include "../../../../dataaccess/datasource/DataSourceFactory.h"
32 #include "../../../../dataaccess/datasource/DataSourceInfo.h"
33 #include "../../../../dataaccess/datasource/DataSourceManager.h"
34 #include "../../../widgets/Exception.h"
35 #include "WMSConnectorDialog.h"
36 #include "ui_WMSConnectorDialogForm.h"
37 
38 // Boost
39 #include <boost/algorithm/string/case_conv.hpp>
40 #include <boost/uuid/random_generator.hpp>
41 #include <boost/uuid/uuid_io.hpp>
42 #include <boost/lexical_cast.hpp>
43 
44 // Qt
45 #include <QMessageBox>
46 
48  : QDialog(parent, f),
49  m_ui(new Ui::WMSConnectorDialogForm)
50 {
51 // add controls
52  m_ui->setupUi(this);
53 
54 // connect signal and slots
55  connect(m_ui->m_openPushButton, SIGNAL(pressed()), this, SLOT(openPushButtonPressed()));
56  connect(m_ui->m_testPushButton, SIGNAL(pressed()), this, SLOT(testPushButtonPressed()));
57  connect(m_ui->m_helpPushButton, SIGNAL(pressed()), this, SLOT(helpPushButtonPressed()));
58 }
59 
61 {
62 }
63 
65 {
66  return m_datasource;
67 }
68 
70 {
71  return m_driver;
72 }
73 
75 {
76  m_datasource = ds;
77 
78  if(m_datasource.get() != 0)
79  {
80  const te::core::URI& connInfo = m_datasource->getConnInfo();
81 
82  if(!connInfo.uri().empty())
83  m_ui->m_serverLineEdit->setText(QString::fromUtf8(connInfo.uri().c_str()));
84 
85  m_ui->m_datasourceTitleLineEdit->setText(QString::fromUtf8(m_datasource->getTitle().c_str()));
86 
87  m_ui->m_dataSourceDescriptionTextEdit->setText(QString::fromUtf8(m_datasource->getDescription().c_str()));
88  }
89 }
90 
92 {
93  try
94  {
95  // Check if driver is loaded
96  if(te::da::DataSourceFactory::find("WMS") == 0)
97  throw te::qt::widgets::Exception(TE_TR("Sorry! No data access driver loaded for WMS data sources!"));
98 
99  // Get the data source connection info based on form data
100  std::string dsInfo = getConnectionInfo();
101 
102  // Perform connection
103  std::unique_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("WMS", dsInfo);
104  ds->open();
105  m_driver.reset(ds.release());
106 
107  if(m_driver.get() == 0)
108  throw te::qt::widgets::Exception(TE_TR("Could not open WMS data source due to an unknown error!"));
109 
110  QString title = m_ui->m_datasourceTitleLineEdit->text().trimmed();
111 
112  if(title.isEmpty())
113  title = m_ui->m_serverLineEdit->text().trimmed();
114 
115  if(m_datasource.get() == 0)
116  {
117  // Create a new data source based on the form data
119 
120  m_datasource->setConnInfo(dsInfo);
121 
122  boost::uuids::basic_random_generator<boost::mt19937> gen;
123  boost::uuids::uuid u = gen();
124  std::string dsId = boost::uuids::to_string(u);
125 
126  m_datasource->setId(dsId);
127  m_driver->setId(dsId);
128  m_datasource->setTitle(title.toUtf8().data());
129  m_datasource->setDescription(m_ui->m_dataSourceDescriptionTextEdit->toPlainText().trimmed().toUtf8().data());
130  m_datasource->setAccessDriver("WMS");
131  m_datasource->setType("WMS");
132  }
133  else
134  {
135  m_driver->setId(m_datasource->getId());
136  m_datasource->setConnInfo(dsInfo);
137  m_datasource->setTitle(title.toUtf8().data());
138  m_datasource->setDescription(m_ui->m_dataSourceDescriptionTextEdit->toPlainText().trimmed().toUtf8().data());
139  }
140  }
141  catch(const std::exception& e)
142  {
143  QMessageBox::warning(this,
144  tr("TerraLib Qt Components"),
145  tr(e.what()));
146  }
147  catch(...)
148  {
149  QMessageBox::warning(this,
150  tr("TerraLib Qt Components"),
151  tr("Unknown error while opening WMS data source!"));
152  }
153 
154  accept();
155 }
156 
158 {
159  try
160  {
161  // Check if driver is loaded
162  if(te::da::DataSourceFactory::find("WMS") == 0)
163  throw te::qt::widgets::Exception(TE_TR("Sorry! No data access driver loaded for WMS data sources!"));
164 
165  // Perform connection
166  std::unique_ptr<te::da::DataSource> ds(te::da::DataSourceFactory::make("WMS", getConnectionInfo()));
167 
168  if(ds.get() == 0)
169  throw te::qt::widgets::Exception(TE_TR("Could not open WMS server!"));
170 
171  ds->open();
172 
173  QMessageBox::information(this,
174  tr("TerraLib Qt Components"),
175  tr("Data source is ok!"));
176 
177  ds->close();
178  }
179  catch(const std::exception& e)
180  {
181  QMessageBox::warning(this,
182  tr("TerraLib Qt Components"),
183  tr(e.what()));
184  }
185  catch(...)
186  {
187  QMessageBox::warning(this,
188  tr("TerraLib Qt Components"),
189  tr("Unknown error while testing WMS data source!"));
190  }
191 }
192 
194 {
195  QMessageBox::warning(this,
196  tr("TerraLib Qt Components"),
197  tr("Not implemented yet!\nWe will provide it soon!"));
198 }
199 
201 {
202  QString qstr; // Auxiliary string used to hold temporary data
203 
204  qstr = m_ui->m_serverLineEdit->text().trimmed();
205  std::string strURI("WMS:");
206 
207  if (qstr.isEmpty())
208  throw te::qt::widgets::Exception(TE_TR("Please select a feature file first!"));
209 
210  strURI += qstr.toUtf8().data();
211 
212  return strURI;
213 }
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
boost::shared_ptr< DataSource > DataSourcePtr
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
const std::string & uri() const
Retrieving the full URI.
Definition: URI.cpp:88
A class for representing an Uniform Resource Identifier (URI).
Definition: URI.h:49
A class that represents a data source component.
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr