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 "canvas/CCanvas.h"
20 #include "gis/db/CDBFolderMysql.h"
21 #include "gis/db/CDBFolderSqlite.h"
22 #include "gis/db/CSelectDBFolder.h"
23 #include "helpers/CSettings.h"
24 
25 #include <QtWidgets>
26 
CSelectDBFolder(QList<quint64> & ids,QString & db,QString & host,QWidget * parent)27 CSelectDBFolder::CSelectDBFolder(QList<quint64>& ids, QString& db, QString& host, QWidget* parent)
28     : QDialog(parent)
29     , ids(ids)
30     , db(db)
31     , host(host)
32 {
33     setupUi(this);
34     treeWidget->setProperty("showItems", false);
35     treeWidget->setProperty("showCheckBoxes", false);
36     setProperty("showLostFound", false);
37 
38     buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
39 
40     SETTINGS;
41     cfg.beginGroup("Database");
42     const QStringList& names = cfg.value("names").toStringList();
43     cfg.beginGroup("Entries");
44     for(const QString& name : names)
45     {
46         if(!db.isEmpty() && (db != name))
47         {
48             continue;
49         }
50 
51         cfg.beginGroup(name);
52         const QString& type = cfg.value("type", "SQLite").toString();
53         if(type == "SQLite")
54         {
55             const QString& filename = cfg.value("filename", "").toString();
56             new CDBFolderSqlite(filename, name, treeWidget);
57         }
58 
59         if(type == "MySQL")
60         {
61             const QString& server = cfg.value("server", "").toString();
62             const QString& port = cfg.value("port", "").toString();
63             const QString& user = cfg.value("user", "").toString();
64             const QString& passwd = cfg.value("passwd", "").toString();
65             bool noPasswd = cfg.value("noPasswd", false).toBool();
66             new CDBFolderMysql(server, port, user, passwd, noPasswd, name, treeWidget);
67         }
68         cfg.endGroup(); // name
69     }
70     cfg.endGroup(); // Database
71 
72     connect(treeWidget, &QTreeWidget::itemExpanded, this, &CSelectDBFolder::slotItemExpanded);
73     connect(treeWidget, &QTreeWidget::itemSelectionChanged, this, &CSelectDBFolder::slotItemSelectionChanged);
74 
75     CCanvas::setOverrideCursor(Qt::ArrowCursor, "CSelectDBFolder");
76 }
77 
~CSelectDBFolder()78 CSelectDBFolder::~CSelectDBFolder()
79 {
80     CCanvas::restoreOverrideCursor("~CSelectDBFolder");
81 }
82 
slotItemExpanded(QTreeWidgetItem * item)83 void CSelectDBFolder::slotItemExpanded(QTreeWidgetItem* item)
84 {
85     IDBFolder* folder = dynamic_cast<IDBFolder*>(item);
86     if(nullptr != folder)
87     {
88         folder->expanding();
89     }
90 }
91 
slotItemSelectionChanged()92 void CSelectDBFolder::slotItemSelectionChanged()
93 {
94     IDBFolder* folder = dynamic_cast<IDBFolder*>(treeWidget->currentItem());
95     if(folder)
96     {
97         if(projectsOnly && (folder->type() != IDBFolder::eTypeProject) && (folder->type() != IDBFolder::eTypeOther))
98         {
99             ids.clear();
100             db.clear();
101             buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
102             return;
103         }
104 
105         ids.clear();
106         ids << folder->getId();
107         db = folder->getDBName();
108         host = folder->getDBHost();
109 
110         IDBFolder* folder1 = dynamic_cast<IDBFolder*>(folder->parent());
111         while(folder1 != nullptr)
112         {
113             ids << folder1->getId();
114             folder1 = dynamic_cast<IDBFolder*>(folder1->parent());
115         }
116 
117         buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
118     }
119     else
120     {
121         ids.clear();
122         db.clear();
123         buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
124     }
125 }
126