00001 #include "newconnection.h"
00002 #include "ui_newconnection.h"
00003 #include <QMessageBox>
00004 #include <QInputDialog>
00005 NewConnection::NewConnection(QWidget *parent) :
00006 QDialog(parent),
00007 ui(new Ui::NewConnection)
00008 {
00009 ui->setupUi(this);
00010
00011 if (!createConnection())
00012 QMessageBox::critical(0, "Error", "SQL Lite Error: You will not be able to save connections.");
00013
00014 connect (ui->cmdNewConnection, SIGNAL(clicked()), this, SLOT(newConnection()));
00015 connect (ui->cmdSave, SIGNAL(clicked()), this, SLOT(saveConnection()));
00016 connect (ui->cmdDelete, SIGNAL(clicked()), this, SLOT(deleteConnection()));
00017 connect (ui->cmdRename, SIGNAL(clicked()), this, SLOT(renameConnection()));
00018 connect (this, SIGNAL(connectionChanged(bool)), ui->cmdSave, SLOT(setEnabled(bool)));
00019 connect (this, SIGNAL(connectionSelected(bool)), ui->cmdDelete, SLOT(setEnabled(bool)));
00020 connect (this, SIGNAL(connectionSelected(bool)), ui->txtPassword, SLOT(setEnabled(bool)));
00021 connect (this, SIGNAL(connectionSelected(bool)), ui->txtServerName, SLOT(setEnabled(bool)));
00022 connect (this, SIGNAL(connectionSelected(bool)), ui->txtUserName, SLOT(setEnabled(bool)));
00023 connect (this, SIGNAL(connectionSelected(bool)), ui->cmdRename, SLOT(setEnabled(bool)));
00024 emit connectionChanged(false);
00025 emit connectionSelected(false);
00026
00027 loadConnections();
00028 }
00029
00030 NewConnection::~NewConnection()
00031 {
00032 delete ui;
00033 }
00034
00035 void NewConnection::on_cmdConnect_clicked()
00036 {
00037 #ifndef QT_NO_CURSOR
00038 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
00039 #endif
00040 QSqlDatabase testDb = QSqlDatabase::addDatabase("QMYSQL", tr("%1 %2").arg(QDateTime::currentDateTime().toString(),
00041 QTime::currentTime().second()));
00042 testDb.setHostName(ui->txtServerName->text());
00043 testDb.setPort(3306);
00044 testDb.setUserName(ui->txtUserName->text());
00045 testDb.setPassword(ui->txtPassword->text());
00046
00047 if (testDb.open()){
00048 if (ui->cmdSave->isEnabled()) {
00049 #ifndef QT_NO_CURSOR
00050 QApplication::restoreOverrideCursor();
00051 #endif
00052 if (QMessageBox::question(this, "Save Changes",
00053 tr("The details for this connection have changed.\n\nSave Changes?")
00054 , QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
00055 saveConnection();
00056 }
00057 db = testDb;
00058 testDb.close();
00059 #ifndef QT_NO_CURSOR
00060 QApplication::restoreOverrideCursor();
00061 #endif
00062 this->accept();
00063 }
00064 else {
00065 #ifndef QT_NO_CURSOR
00066 QApplication::restoreOverrideCursor();
00067 #endif
00068 QMessageBox::critical(this, "Connection Error",
00069 tr("There was an error while connecting to the server %1\n\nError Message:\n\n%2").arg
00070 (ui->txtServerName->text(),
00071 testDb.lastError().text()));
00072 }
00073 }
00074
00075 void NewConnection::on_cmdCancel_clicked()
00076 {
00077 this->reject();
00078 }
00079
00080 void NewConnection::newConnection() {
00081 QInputDialog inp(this);
00082 inp.setWindowTitle("New Connection");
00083 inp.setLabelText("Enter the new connection name");
00084 if (inp.exec()) {
00085 QString connName = inp.textValue();
00086 if (connName != "") {
00087 if (checkConnectionName(connName)) {
00088 QMessageBox::warning(this, "Error", "The name you entered is already in use.") ;
00089 return;
00090 }
00091 QSqlQuery insertQuery;
00092 insertQuery.exec(tr("INSERT INTO connections (connectionName) VALUES ('%1')").arg(connName));
00093 loadConnections();
00094 ui->cboConnections->setCurrentIndex(ui->cboConnections->count() - 1);
00095 } else {
00096 QMessageBox::critical(this, "Error", "The connection name you entered is invalid. Please retry");
00097 newConnection();
00098 }
00099 }
00100 }
00101
00102 void NewConnection::saveConnection() {
00103 QSqlQuery insertQuery;
00104 insertQuery.exec(tr("UPDATE connections SET "
00105 "serverName = '%1', "
00106 "userName = '%2', "
00107 "password = '%3' "
00108 "WHERE connectionName = '%4'")
00109 .arg(ui->txtServerName->text(),
00110 ui->txtUserName->text(),
00111 ui->txtPassword->text(),
00112 ui->cboConnections->currentText()));
00113 emit connectionChanged(false);
00114 }
00115
00116 void NewConnection::deleteConnection() {
00117 if (QMessageBox::question(this, "Confirm", tr("Are you sure you want to delete this connection?"), QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
00118 QSqlQuery deleteQuery;
00119 QString connName = ui->cboConnections->currentText();
00120 deleteQuery.exec(tr("DELETE FROM connections WHERE connectionName = '%1'").arg(connName));
00121 loadConnections();
00122 }
00123 }
00124
00125 void NewConnection::loadConnections() {
00126 ui->cboConnections->clear();
00127 ui->txtPassword->setText("");
00128 ui->txtPort->setText("3306");
00129 ui->txtServerName->setText("");
00130 ui->txtUserName->setText("");
00131 QSqlQuery query;
00132 query.exec("SELECT * FROM connections");
00133 while (query.next()) {
00134 QString connectionName = query.value(1).toString();
00135 ui->cboConnections->insertItem(ui->cboConnections->count(), connectionName);
00136 }
00137 }
00138
00139 void NewConnection::on_cboConnections_currentIndexChanged(QString )
00140 {
00141 if (ui->cboConnections->count() > 0) {
00142 emit connectionSelected(true);
00143 QString connectionName = ui->cboConnections->currentText();
00144 QSqlQuery selectQuery;
00145 selectQuery.exec(tr("SELECT * FROM connections WHERE connectionName = '%1'").arg(connectionName));
00146 if (selectQuery.lastError().isValid()) {
00147
00148 } else {
00149 while (selectQuery.next()) {
00150 ui->txtServerName->setText(selectQuery.value(2).toString());
00151 ui->txtUserName->setText(selectQuery.value(3).toString());
00152 ui->txtPassword->setText(selectQuery.value(4).toString());
00153 ui->txtPort->setText("3306");
00154 }
00155 }
00156 } else {
00157 emit connectionSelected(false);
00158 }
00159 }
00160
00161 void NewConnection::renameConnection() {
00162 QInputDialog inp(this);
00163 inp.setWindowTitle("Rename Connection");
00164 inp.setLabelText(tr("Enter the new connection name for '%1'").arg(ui->cboConnections->currentText()));
00165 if (inp.exec()) {
00166 QString connName = inp.textValue();
00167 if (connName != "") {
00168 if (checkConnectionName(connName)) {
00169 QMessageBox::warning(this, "Error", "The name you entered is already in use.") ;
00170 return;
00171 }
00172 QSqlQuery insertQuery;
00173 insertQuery.exec(tr("UPDATE connections SET connectionName = '%1' WHERE connectionName = '%2'").arg(connName, ui->cboConnections->currentText()));
00174 loadConnections();
00175 ui->cboConnections->setCurrentIndex(ui->cboConnections->count() - 1);
00176 } else {
00177 QMessageBox::critical(this, "Error", "The connection name you entered is invalid. Please retry");
00178 renameConnection();
00179 }
00180 }
00181 }
00182
00183
00184 void NewConnection::on_txtServerName_textEdited(QString )
00185 {
00186 emit connectionChanged(true);
00187 }
00188
00189 void NewConnection::on_txtUserName_textEdited(QString )
00190 {
00191 emit connectionChanged(true);
00192 }
00193
00194 void NewConnection::on_txtPassword_textEdited(QString )
00195 {
00196 emit connectionChanged(true);
00197 }
00198
00199 bool NewConnection::checkConnectionName(QString checkName) {
00200 QSqlQuery checkQuery;
00201 checkQuery.exec(tr("SELECT * FROM connections WHERE connectionName = '%1'").arg(checkName));
00202
00203 return false;
00204
00205
00206 }
00207