source: branches/release-0.3.0-rc1/src/MediaDBLib/sqllogin.cpp @ 208

Last change on this file since 208 was 208, checked in by Joachim Langenbach, 11 years ago
  • Added some command line options to specifiy username, password, database, database host, database driver, print version, print help
File size: 7.0 KB
Line 
1/*
2    MediaDBLib - The shared components of MediaDB
3    Copyright (C) 2010  Joachim Langenbach
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    This program 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 General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18*/
19
20#include "sqllogin.h"
21#include "ui_sqllogin.h"
22
23#include <QSettings>
24#include <QSqlError>
25
26SqlLogin::SqlLogin ( QWidget *parent ) : MDialog ( parent ), m_ui ( new Ui::SqlLogin )
27{
28  init();
29}
30
31SqlLogin::SqlLogin ( QString title, QString newConnName, QWidget *parent ) : MDialog ( parent ), m_ui ( new Ui::SqlLogin )
32{
33  connName = newConnName;
34  init();
35  setWindowTitle ( title );
36}
37
38SqlLogin::~SqlLogin()
39{
40  close();
41  delete m_ui;
42}
43
44QStringList SqlLogin::data()
45{
46  QStringList result;
47  result.append ( m_ui->usernameEdit->text() );
48  result.append ( m_ui->passwordEdit->text() );
49  return result;
50}
51
52void SqlLogin::reloadSettingsByPass()
53{
54  loadAndCheckSettings();
55  emit reloadSettings();
56}
57
58bool SqlLogin::loadAndCheckSettings(QString username, QString host, QString database,
59                                                                                                                                                QString driver, QString password)
60{
61  QSettings qSettings;
62  qSettings.beginGroup ( "database" );
63        if(!username.isEmpty())
64                this->username = username;
65        if(!password.isEmpty())
66                this->password = password;
67       
68        if(!host.isEmpty()){
69                this->host = host;
70        }
71        else{
72                this->host = qSettings.value ( "host" ).toString();
73        }
74        if(!driver.isEmpty()){
75                this->driver = driver;
76        }
77        else{
78                this->driver = qSettings.value ( "type" ).toString();
79        }
80        if(!database.isEmpty()){
81                this->database = database;
82        }
83        else{
84                this->database = qSettings.value ( "database" ).toString();
85        }
86  qSettings.endGroup();
87
88  if ( ( this->driver.contains ( "SQLITE" ) ) && ( !this->database.isEmpty() ) ) {
89    return true;
90  }
91
92  else if ( ( !this->host.isEmpty() ) || ( !this->driver.isEmpty() ) || ( !this->database.isEmpty() ) ) {
93    return true;
94  }
95
96  return false;
97}
98
99bool SqlLogin::checkSettings()
100{
101  QSettings qSettings;
102  qSettings.beginGroup ( "database" );
103  QString myDriver = qSettings.value ( "type" ).toString();
104  QString myHost = qSettings.value ( "host" ).toString();
105  QString myDatabase = qSettings.value ( "database" ).toString();
106  qSettings.endGroup();
107
108  if ( ( myDriver.contains ( "SQLITE" ) ) && ( !myDatabase.isEmpty() ) ) {
109    return true;
110  }
111
112  else if ( ( !myHost.isEmpty() ) || ( !myDriver.isEmpty() ) || ( !myDatabase.isEmpty() ) ) {
113    return true;
114  }
115
116  return false;
117}
118
119int SqlLogin::exec()
120{
121  // try to get already established connections
122  QSqlDatabase checkDB;
123
124  if ( connName.isEmpty() ) {
125    checkDB = QSqlDatabase::database();
126  }
127
128  else {
129    checkDB = QSqlDatabase::database ( connName );
130  }
131
132  if ( checkDB.open() ){
133    checkDB.close();
134    return QDialog::Accepted;
135  }
136
137  // if we have database like SQLite where no login is needed, we doesn't show the
138  // login screen, but connect and return accepted
139  if ( driver.contains ( "SQLITE" ) ) {
140    finish();
141    // parts of the standard exec call of QDialog
142    int res = result();
143//              bool destructiveClose = testWFlags(WDestructiveClose);
144//              if(destructiveClose)
145//         delete this;
146
147    if ( res == QDialog::Accepted ) {
148      return res;
149    }
150
151    else {
152      emit error ( m_ui->statusLabel->text() );
153      return QDialog::Rejected;
154    }
155  }
156
157        m_ui->usernameEdit->setText(username);
158        m_ui->passwordEdit->setText(password);
159        // try to finish
160        if((!username.isEmpty()) && (!password.isEmpty())
161                && (!host.isEmpty()) && (!database.isEmpty())
162                && (!driver.isEmpty())){
163                finish();
164                int res = result();
165                if ( res == QDialog::Accepted )
166      return res;
167        }
168       
169  return QDialog::exec();
170}
171
172
173void SqlLogin::changeEvent ( QEvent *e )
174{
175  QDialog::changeEvent ( e );
176
177  switch ( e->type() ) {
178
179    case QEvent::LanguageChange:
180      m_ui->retranslateUi ( this );
181      break;
182
183    default:
184      break;
185  }
186}
187
188void SqlLogin::finish()
189{
190  if ( !driver.contains ( "SQLITE" ) ) {
191    if ( m_ui->usernameEdit->text().isEmpty() ) {
192      displayError ( tr ( "Please enter your username" ) );
193      return;
194    }
195
196    if ( m_ui->passwordEdit->text().isEmpty() ) {
197      displayError ( tr ( "Please enter your password" ) );
198      return;
199    }
200  }
201
202// could not be done, if we have things from command line
203//   if ( !loadAndCheckSettings() ) {
204//     emit showSettings();
205//     return;
206//   }
207
208  if ( !connName.isEmpty() ) {
209    if ( db.connectionName() != connName ) {
210      if ( db.connectionNames().contains ( connName ) ) {
211        QSqlDatabase::removeDatabase ( connName );
212      }
213
214      db = QSqlDatabase::addDatabase ( driver, connName );
215    }
216  }
217
218  else {
219    if ( !db.isValid() ) {
220      db = QSqlDatabase::addDatabase ( driver );
221    }
222
223    if ( db.driverName() != driver ) {
224      QSqlDatabase::removeDatabase ( QLatin1String ( "defaultConnection" ) );
225      db = QSqlDatabase::addDatabase ( driver );
226    }
227  }
228
229  db.setDatabaseName("");
230  if(!withoutDatabase)
231    db.setDatabaseName ( database );
232
233  if ( !driver.contains ( "SQLITE" ) ) {
234    db.setHostName ( host );
235    db.setUserName ( m_ui->usernameEdit->text() );
236    db.setPassword ( m_ui->passwordEdit->text() );
237  }
238
239  if ( !db.open() ) {
240    if(!withoutDatabase){
241      // may be the database does not exists,
242      // but SqlInit creates it, so try to connect without database
243      withoutDatabase = true;
244      finish();
245      return;
246    }
247    else{
248                        // both tries (with and wihtout database) failed,
249                        // so it must be a wrong username and password.
250                        // Therefore we set withoutDatabase back initial value (false)
251                        withoutDatabase = false;
252                }
253    // we already tried without database and it doesn't work, so we give up
254    displayError ( tr ( "Can not connect to database:\n%1" ).arg ( db.lastError().text() ) );
255    m_ui->passwordEdit->setText ( "" );
256    return;
257  }
258
259  accept();
260}
261
262void SqlLogin::showSettingsDialog()
263{
264  emit showSettings();
265}
266
267void SqlLogin::init()
268{
269  withoutDatabase = false;
270  setWindowModality ( Qt::WindowModal );
271  m_ui->setupUi ( this );
272
273  connect ( m_ui->loginButton, SIGNAL ( clicked() ), this, SLOT ( finish() ) );
274  connect ( m_ui->cancelButton, SIGNAL ( clicked() ), this, SLOT ( reject() ) );
275  connect ( m_ui->settingsButton, SIGNAL ( clicked() ), this, SLOT ( showSettingsDialog() ) );
276
277  if ( !loadAndCheckSettings() ) {
278    emit showSettings();
279  }
280}
281
282void SqlLogin::displayError ( QString msg )
283{
284  m_ui->statusLabel->setText ( QString ( "<font color='red'>%1</font>" ).arg ( msg ) );
285}
286
287void SqlLogin::displayInformation ( QString msg )
288{
289  m_ui->statusLabel->setText ( QString ( "<font color='Green'>%1</font>" ).arg ( msg ) );
290}
291
292#include "sqllogin.moc"
Note: See TracBrowser for help on using the repository browser.