GOFIGURE2  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
QGoConnectServerPage.cxx
Go to the documentation of this file.
1 /*=========================================================================
2  Authors: The GoFigure Dev. Team.
3  at Megason Lab, Systems biology, Harvard Medical school, 2009-11
4 
5  Copyright (c) 2009-11, President and Fellows of Harvard College.
6  All rights reserved.
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions are met:
10 
11  Redistributions of source code must retain the above copyright notice,
12  this list of conditions and the following disclaimer.
13  Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16  Neither the name of the President and Fellows of Harvard College
17  nor the names of its contributors may be used to endorse or promote
18  products derived from this software without specific prior written
19  permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
25  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 =========================================================================*/
34 
35 #include "QGoConnectServerPage.h"
36 #include "CreateDataBaseHelper.h"
37 #include "QGoWizardDB.h"
38 
39 #include "QueryDataBaseHelper.h"
40 #include <QVariant>
41 #include <QFormLayout>
42 #include <QMessageBox>
43 #include <iostream>
44 #include <list>
45 
47  QWizardPage(iParent)
48 {
49  this->m_ImgSessionName.clear();
50  this->m_IsAnOpenRecentFile = false;
51  QFont tfont;
52  tfont.setBold(false);
53  this->setFont(tfont);
54  setSubTitle( tr("Connect to a MySQL DataBase Server:") );
55 
56  QFormLayout *formLayout = new QFormLayout;
57  lineServerName = new QLineEdit( tr("localhost") );
58  lineUserName = new QLineEdit( tr("gofigure") );
59  linePassword = new QLineEdit( tr("gofigure") );
60  linePassword->setEchoMode(QLineEdit::Password);
62  lineDBName = new QLineEdit;
63 
64  formLayout->addRow(tr("&ServerName:"), lineServerName);
65  formLayout->addRow(tr("&User:"), lineUserName);
66  formLayout->addRow(tr("&Password:"), linePassword);
67  setLayout(formLayout);
68 
69  setLayout(formLayout);
70 
71  registerField("ServerName", lineServerName);
72  registerField("User", lineUserName);
73  registerField("Password", linePassword);
74  registerField("DBName", lineDBName);
75 }
76 
77 //-------------------------------------------------------------------------
78 
79 //-------------------------------------------------------------------------
81 {
82  delete lineDBName;
83  // make sure vtkMySQLDatabase* has been deleted
85 }
86 
87 //-------------------------------------------------------------------------
88 
89 //-------------------------------------------------------------------------
91 {
92  if ( field("ServerName").toString() == ""
93  || field("User").toString() == ""
94  || field("Password").toString() == "" )
95  {
96  QMessageBox msgBox;
97  msgBox.setText(
98  tr("Please fill all the fields.") );
99  msgBox.exec();
100  return false;
101  }
102  this->OpenConnectionToServer();
103  if ( !m_ConnectionServer.first )
104  {
105  QMessageBox msgBox;
106  msgBox.setText(
107  tr("Unable to connect to the server: please make sure you entered the right fields.") );
108  msgBox.exec();
109  return false;
110  }
111  std::list< std::string > ListGoDB = ListGofigureDatabases();
112  if ( ListGoDB.empty() )
113  {
114  QMessageBox msgBox;
115  msgBox.setText(
116  tr(
117  "You have not yet set up your Gofigure Database\nPlease go to the menu 'Settings' and select 'Set Up Database'.") );
118  msgBox.exec();
119  emit NoGofigureDatabase();
120  return false;
121  }
122 
123  if ( ListGoDB.size() > 1 )
124  {
125  std::cout << "There is more than one Gofigure DataBase" << std::endl;
126  std::cout << "Debug: In " << __FILE__ << ", line " << __LINE__;
127  std::cout << std::endl;
128  return false;
129  }
130 
131  std::list< std::string >::iterator i = ListGoDB.begin();
132  std::string DBName = *i;
133  this->wizard()->setField( "DBName", DBName.c_str() );
134  emit GofigureDatabaseExists();
135  //std::cout << "the db name to open is: " <<
136  // field("DBName").toString().toStdString().c_str() << std::endl;
137  return true;
138 }
139 
140 //-------------------------------------------------------------------------
141 
142 //-------------------------------------------------------------------------
144 {
145  if ( !this->m_IsAnOpenRecentFile )
146  {
148  }
149  else
150  {
151  return -1;
152  }
153 }
154 
155 //-------------------------------------------------------------------------
156 
157 //-------------------------------------------------------------------------
158 std::list< std::string > QGoConnectServerPage::ListGofigureDatabases() const
159 {
160  //Get the list of all the existing databases:
161  this->OpenConnectionToServer();
162  std::vector< std::string > vectListDB = ListDatabases(m_ConnectionServer.second);
164 
165  /*For each existing database, check if they are of Gofigure Type, if so, put them
166  in the ListGoDB*/
167  std::list< std::string > ListGoDB;
168  for ( unsigned int i = 0; i < vectListDB.size(); ++i )
169  {
170  //First, create the connection to the database named vectListDB[i]and check
171  // it is open:
172  std::pair< bool, vtkMySQLDatabase * > DatabaseConnection = ConnectToDatabase(
173  field("ServerName").toString().toStdString(),
174  field("User").toString().toStdString(),
175  field("Password").toString().toStdString(), vectListDB[i]);
176  if ( !DatabaseConnection.first )
177  {
178  std::cout << "Cannot check if " << vectListDB[i].c_str()
179  << " is of\
180  Gofigure Type"
181  <<
182  std::endl;
183  std::cout << "Debug: In " << __FILE__ << ", line " << __LINE__;
184  std::cout << std::endl;
185  }
186  //test if it is of GofigureType:
187  if ( IsDatabaseOfGoFigureType(DatabaseConnection.second) )
188  {
189  ListGoDB.push_back(vectListDB[i]);
190  }
191  DatabaseConnection.second->Close();
192  DatabaseConnection.second->Delete();
193  }
194  return ListGoDB;
195 }
196 
197 //-------------------------------------------------------------------------
198 
199 //-------------------------------------------------------------------------
201 {
202  if ( m_ConnectionServer.second )
203  {
204  m_ConnectionServer.second->Close();
205  m_ConnectionServer.second->Delete();
206  m_ConnectionServer.second = 0;
207  }
208 }
209 
210 //-------------------------------------------------------------------------
211 
212 //-------------------------------------------------------------------------
214 {
215  if ( this->m_ConnectionServer.second == 0 )
216  {
218  field("ServerName").toString().toStdString(),
219  field("User").toString().toStdString(),
220  field("Password").toString().toStdString() );
221  }
222 }
223 
224 //-------------------------------------------------------------------------
225 
226 //-------------------------------------------------------------------------
227 void QGoConnectServerPage::SetImgSessionName(std::string iImgSessionName)
228 {
229  this->m_ImgSessionName = iImgSessionName;
230  this->m_IsAnOpenRecentFile = true;
231 }
232 
233 //-------------------------------------------------------------------------
234 
235 //-------------------------------------------------------------------------
236 void QGoConnectServerPage::SetIsAnOpenRecentFile(bool iIsAnOpenRecentFile)
237 {
238  this->m_IsAnOpenRecentFile = iIsAnOpenRecentFile;
239 }
void registerField(const QString &name, QWidget *widget, const char *property, const char *changedSignal)
void setSubTitle(const QString &subTitle)
void setEchoMode(EchoMode)
std::vector< std::string > ListDatabases(vtkMySQLDatabase *ServerConnector)
QString tr(const char *sourceText, const char *disambiguation, int n)
void SetIsAnOpenRecentFile(bool iIsAnOpenRecentFile)
void setBold(bool enable)
void setLayout(QLayout *layout)
void setText(const QString &text)
QGoConnectServerPage(QWidget *parent=0)
void NoGofigureDatabase() const
void addRow(QWidget *label, QWidget *field)
QVariant field(const QString &name) const
std::pair< bool, vtkMySQLDatabase * > ConnectToDatabase(std::string ServerName, std::string login, std::string Password, std::string DBName)
void setFont(const QFont &)
std::pair< bool, vtkMySQLDatabase * > ConnectToServer(std::string ServerName, std::string login, std::string Password)
QWizard * wizard() const
std::list< std::string > ListGofigureDatabases() const
return the list of the names of the databases of gofigure type
void setField(const QString &name, const QVariant &value)
void SetImgSessionName(std::string iImgSessionName)
bool IsDatabaseOfGoFigureType(vtkMySQLDatabase *DatabaseConnector)
std::pair< bool, vtkMySQLDatabase * > m_ConnectionServer
void GofigureDatabaseExists() const
void OpenConnectionToServer() const