1 /**********************************************************************************************
2     Copyright (C) 2014 Oliver Eichler <oliver.eichler@gmx.de>
3 
4     This program is free software: you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation, either version 3 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 
17 **********************************************************************************************/
18 
19 #include "CMainWindow.h"
20 #include "gis/CGisListDB.h"
21 #include "gis/db/CSetupDatabase.h"
22 #include "helpers/CSettings.h"
23 
24 #include <QtWidgets>
25 
CSetupDatabase(CGisListDB & parent)26 CSetupDatabase::CSetupDatabase(CGisListDB& parent)
27     : QDialog(&parent)
28     , list(parent)
29 {
30     setupUi(this);
31 
32     lineUser->setText(CMainWindow::getUser());
33 
34     connect(toolNewDB, &QToolButton::clicked, this, &CSetupDatabase::slotNewDB);
35     connect(toolAddDB, &QToolButton::clicked, this, &CSetupDatabase::slotOpenDB);
36     connect(lineName, &QLineEdit::textChanged, this, &CSetupDatabase::slotUpdateButtonBox);
37     connect(lineServer, &QLineEdit::textChanged, this, &CSetupDatabase::slotUpdateButtonBox);
38     connect(lineUser, &QLineEdit::textChanged, this, &CSetupDatabase::slotUpdateButtonBox);
39     connect(radioSqlite, &QRadioButton::clicked, this, &CSetupDatabase::slotUpdateButtonBox);
40     connect(radioMysql, &QRadioButton::clicked, this, &CSetupDatabase::slotUpdateButtonBox);
41     connect(checkMySQLNoPasswd, &QCheckBox::clicked, linePasswd, &QLineEdit::setDisabled);
42 
43     if(!QSqlDatabase::isDriverAvailable("QMYSQL"))
44     {
45         gridLayout->removeWidget(frameMysql);
46 
47         QString errorTitle = tr("Missing Requirement");
48         QString errorText = tr("MySQL cannot be used at this point, because the corresponding driver (QMYSQL) is not available.<br />Please make sure you have installed the corresponding package.<br />If you don't know what to do now you should have <a href=\"%1\">a look at the wiki</a>.").arg("https://github.com/Maproom/qmapshack/wiki/DocGisDatabaseAddRemove#mysql--565");
49 
50         QLabel* errorMissingMySQL = new QLabel(QString("<b>%1</b><br /><br />%2").arg(errorTitle, errorText));
51         errorMissingMySQL->setOpenExternalLinks(true);
52         errorMissingMySQL->setWordWrap(true);
53         gridLayout->addWidget(errorMissingMySQL, 4, 1, Qt::AlignTop);
54 
55         radioSqlite->setChecked(true);
56         radioMysql->setDisabled(true);
57     }
58 
59     slotUpdateButtonBox();
60     adjustSize();
61 }
62 
~CSetupDatabase()63 CSetupDatabase::~CSetupDatabase()
64 {
65 }
66 
slotUpdateButtonBox()67 void CSetupDatabase::slotUpdateButtonBox()
68 {
69     bool enable = !lineName->text().isEmpty();
70 
71     if(radioSqlite->isChecked())
72     {
73         if(labelFilename->text() == "-")
74         {
75             enable = false;
76         }
77         frameSqlite->setEnabled(true);
78         frameMysql->setEnabled(false);
79     }
80     else if(radioMysql->isChecked())
81     {
82         if(lineServer->text().isEmpty())
83         {
84             enable = false;
85         }
86         if(lineUser->text().isEmpty())
87         {
88             enable = false;
89         }
90         frameSqlite->setEnabled(false);
91         frameMysql->setEnabled(true);
92     }
93 
94     buttonBox->button(QDialogButtonBox::Ok)->setEnabled(enable);
95 }
96 
97 
accept()98 void CSetupDatabase::accept()
99 {
100     QString name = lineName->text();
101     if(list.hasDatabase(name))
102     {
103         QMessageBox::warning(CMainWindow::getBestWidgetForParent(), tr("Error..."), tr("There is already a database with name '%1'").arg(name), QMessageBox::Abort);
104         return;
105     }
106 
107     QDialog::accept();
108 }
109 
110 
slotNewDB()111 void CSetupDatabase::slotNewDB()
112 {
113     SETTINGS;
114     QString path = cfg.value("Database/lastDatabasePath", QDir::homePath()).toString();
115 
116     QString filename = QFileDialog::getSaveFileName(this, tr("New database..."), path, "QMapShack Database (*.db)");
117     if(filename.isEmpty())
118     {
119         return;
120     }
121 
122     QFileInfo fi(filename);
123     if(fi.suffix().toLower() != "db")
124     {
125         filename += ".db";
126     }
127 
128 
129     cfg.setValue("Database/lastDatabasePath", fi.absolutePath());
130 
131     labelFilename->setText(filename);
132 
133     slotUpdateButtonBox();
134 }
135 
slotOpenDB()136 void CSetupDatabase::slotOpenDB()
137 {
138     SETTINGS;
139     QString path = cfg.value("Database/lastDatabasePath", QDir::homePath()).toString();
140 
141     QString filename = QFileDialog::getOpenFileName(this, tr("Open database..."), path, "QMapShack Database (*.db)");
142     if(filename.isEmpty())
143     {
144         return;
145     }
146 
147     QFileInfo fi(filename);
148     if(fi.suffix().toLower() != "db")
149     {
150         filename += ".db";
151     }
152 
153 
154     cfg.setValue("Database/lastDatabasePath", fi.absolutePath());
155 
156     labelFilename->setText(filename);
157 
158     slotUpdateButtonBox();
159 }
160 
161 
noPasswd() const162 bool CSetupDatabase::noPasswd() const
163 {
164     return radioMysql->isChecked() && checkMySQLNoPasswd->isChecked();
165 }
166