ADOConnectorDialog.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/widgets/connector/ado/ADOConnectorDialog.cpp
22 
23  \brief ....
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 "../../../widgets/Utils.h"
36 #include "ui_ADOConnectorDialogForm.h"
37 #include "ADOConnectorDialog.h"
38 
39 // Boost
40 #include <boost/algorithm/string/case_conv.hpp>
41 #include <boost/lexical_cast.hpp>
42 #include <boost/uuid/random_generator.hpp>
43 #include <boost/uuid/uuid_io.hpp>
44 
45 // Qt
46 #include <QFileDialog>
47 #include <QMessageBox>
48 
50  : QDialog(parent, f),
51  m_ui(new Ui::ADOConnectorDialogForm)
52 {
53 // add controls
54  m_ui->setupUi(this);
55 
56 // popule providers
57 #ifdef _M_IX86
58  m_ui->m_providerComboBox->addItem("Microsoft.Jet.OLEDB.4.0");
59 #else
60  m_ui->m_providerComboBox->addItem("Microsoft.ACE.OLEDB.12.0");
61  m_ui->m_providerComboBox->addItem("Microsoft.ACE.OLEDB.15.0");
62 #endif
63 
64 // connect signal and slots
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_searchDatabaseToolButton, SIGNAL(pressed()), this, SLOT(searchDatabaseToolButtonPressed()));
69 
70  m_ui->m_helpPushButton->setNameSpace("dpi.inpe.br.plugins");
71  m_ui->m_helpPushButton->setPageReference("plugins/ado/ado.html");
72 
73  if(op == UPDATE)
74  m_ui->m_openPushButton->setText(TE_TR("Update"));
75 }
76 
78 {
79 }
80 
82 {
83  return m_datasource;
84 }
85 
87 {
88  return m_driver;
89 }
90 
92 {
93  m_datasource = ds;
94 
95  if(m_datasource.get() != 0)
96  {
97  setConnectionInfo(m_datasource->getConnInfoAsString());
98 
99  m_ui->m_datasourceTitleLineEdit->setText(QString::fromUtf8(m_datasource->getTitle().c_str()));
100 
101  m_ui->m_datasourceDescriptionTextEdit->setText(QString::fromUtf8(m_datasource->getDescription().c_str()));
102  }
103 }
104 
106 {
107  try
108  {
109  // check if driver is loaded
110  if(te::da::DataSourceFactory::find("ADO") == 0)
111  throw te::qt::widgets::Exception(TE_TR("Sorry! No data access driver loaded for ADO data sources!"));
112 
113  // perform connection
114  std::unique_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("ADO", getConnectionInfo(true));
115  ds->open();
116  m_driver.reset(ds.release());
117 
118  if(m_driver.get() == 0)
119  throw te::qt::widgets::Exception(TE_TR("Could not open ADO data source due to an unknown error!"));
120 
121  QString title = m_ui->m_datasourceTitleLineEdit->text().trimmed();
122 
123  if(title.isEmpty())
124  title = m_ui->m_fileLineEdit->text().trimmed();
125 
126  if(m_datasource.get() == 0)
127  {
128 // create a new data source based on form data
130 
131  m_datasource->setConnInfo(getConnectionInfo(m_ui->m_savePasswordCheckBox->isChecked()));
132 
133  boost::uuids::basic_random_generator<boost::mt19937> gen;
134  boost::uuids::uuid u = gen();
135  std::string dsId = boost::uuids::to_string(u);
136 
137  m_datasource->setId(dsId);
138  m_driver->setId(dsId);
139  m_datasource->setTitle(title.toUtf8().data());
140  m_datasource->setDescription(m_ui->m_datasourceDescriptionTextEdit->toPlainText().trimmed().toUtf8().data());
141  m_datasource->setAccessDriver("ADO");
142  m_datasource->setType("ADO");
143  }
144  else
145  {
146  m_driver->setId(m_datasource->getId());
147  m_datasource->setConnInfo(getConnectionInfo(m_ui->m_savePasswordCheckBox->isChecked()));
148  m_datasource->setTitle(title.toUtf8().data());
149  m_datasource->setDescription(m_ui->m_datasourceDescriptionTextEdit->toPlainText().trimmed().toUtf8().data());
150  }
151  }
152  catch(const std::exception& e)
153  {
154  QMessageBox::warning(this,
155  tr("TerraLib Qt Components"),
156  tr(e.what()));
157  return;
158  }
159  catch(...)
160  {
161  QMessageBox::warning(this,
162  tr("TerraLib Qt Components"),
163  tr("Unknown error while opening ADO database!"));
164  return;
165  }
166 
167  accept();
168 }
169 
171 {
172  try
173  {
174 // check if driver is loaded
175  if(te::da::DataSourceFactory::find("ADO") == 0)
176  throw te::qt::widgets::Exception(TE_TR("Sorry! No data access driver loaded for ADO data sources!"));
177 
178 // perform connection
179  std::unique_ptr<te::da::DataSource> ds(te::da::DataSourceFactory::make("ADO", getConnectionInfo(true)));
180  ds->open();
181 
182  if(ds.get() == 0)
183  throw te::qt::widgets::Exception(TE_TR("Could not open ADO database!"));
184 
185  QMessageBox::warning(this,
186  tr("TerraLib Qt Components"),
187  tr("Data source is ok!"));
188  }
189  catch(const std::exception& e)
190  {
191  QMessageBox::warning(this,
192  tr("TerraLib Qt Components"),
193  tr(e.what()));
194  }
195  catch(...)
196  {
197  QMessageBox::warning(this,
198  tr("TerraLib Qt Components"),
199  tr("Unknown error while testing ADO data source!"));
200  }
201 }
202 
204 {
205  QMessageBox::warning(this,
206  tr("TerraLib Qt Components"),
207  tr("Not implemented yet!\nWe will provide it soon!"));
208 }
209 
211 {
212  QString fileName = QFileDialog::getOpenFileName(this, tr("Open ADO Database"), te::qt::widgets::GetFilePathFromSettings("vector"), tr("Database files (*.accdb *.mdb);; All Files (*.*)"), 0, QFileDialog::ReadOnly);
213 
214  if(fileName.isEmpty())
215  return;
216 
217  m_ui->m_fileLineEdit->setText(fileName);
218 
219  QFileInfo info(fileName);
220 
221  te::qt::widgets::AddFilePathToSettings(info.absolutePath(), "vector");
222 }
223 
224 const std::string te::qt::plugins::ado::ADOConnectorDialog::getConnectionInfo(bool getPrivateKeys) const
225 {
226  std::string strURI = "file://"; // The base of the URI
227  QString qstr; // Auxiliary string used to hold temporary data
228 
229  if(getPrivateKeys)
230  {
231  // The password
232  qstr = m_ui->m_passwordLineEdit->text().trimmed();
233  strURI += qstr.isEmpty() ? "" : "user:" + std::string(qstr.toUtf8().data()) + "@";
234  }
235 
236  // Path to the database
237  qstr = m_ui->m_fileLineEdit->text().trimmed();
238  strURI += qstr.isEmpty() ? "" : qstr.toUtf8().data();
239 
240  // The provider
241  qstr = m_ui->m_providerComboBox->currentText().trimmed();
242  if(!qstr.isEmpty())
243  strURI += "?PROVIDER=" + std::string(qstr.toUtf8().data());
244 
245  return strURI;
246 }
247 
249 {
250  const te::core::URI uri(connInfo);
251 
252  std::string dbName = te::core::URIDecode(uri.host() + uri.path());
253  std::string password = uri.password();
254 
255  if(!dbName.empty())
256  m_ui->m_fileLineEdit->setText(dbName.c_str());
257 
258  if(!password.empty())
259  m_ui->m_passwordLineEdit->setText(password.c_str());
260 }
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)
const std::string getConnectionInfo(bool getPrivateKeys) const
boost::shared_ptr< DataSource > DataSourcePtr
TEQTWIDGETSEXPORT void AddFilePathToSettings(const QString &path, const QString &typeFile)
Save last used path in QSettings.
std::string password() const
Retrieving the password information.
Definition: URI.cpp:103
static te::dt::Date ds(2010, 01, 01)
const te::da::DataSourcePtr & getDriver() const
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
void setConnectionInfo(const std::string &connInfo)
ADOConnectorDialog(QWidget *parent=0, Qt::WindowFlags f=0, Operation op=CONNECTION)
void set(const te::da::DataSourceInfoPtr &ds)
std::string host() const
Retrieving the host.
Definition: URI.cpp:108
A class for representing an Uniform Resource Identifier (URI).
Definition: URI.h:49
A class that represents a data source component.
TEQTWIDGETSEXPORT QString GetFilePathFromSettings(const QString &typeFile)
Returns the value of the last saved file path for the typeFile required.
TECOREEXPORT std::string URIDecode(const std::string &srcUri)
Decodes an encoded URI. The algorithm implementation is based on http://www.codeguru.com/cpp/cpp/algorithms/strings/article.php/c12759/URI-Encoding-and-Decoding.htm.
std::unique_ptr< Ui::ADOConnectorDialogForm > m_ui
const te::da::DataSourceInfoPtr & getDataSource() const
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr