-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_host_dialog.cpp
130 lines (110 loc) · 4.09 KB
/
add_host_dialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* (C) Copyright 2014, MongoDB, Inc. */
#include "add_host_dialog.h"
#include "host_types.h"
#include <QCompleter>
#include <QDir>
#include <QFileDialog>
#include <QFileSystemModel>
#include <QIntValidator>
#include <QMessageBox>
#include <QStringListModel>
AddHostDialog::AddHostDialog(const QStringList& replicaSets, QWidget* parent)
: QDialog(parent)
{
_ui.setupUi(this);
connect(_ui.toolButtonDBPath, SIGNAL(clicked()), this, SLOT(browseForDBPath()));
connect(_ui.comboBoxType, SIGNAL(currentIndexChanged(int)), this, SLOT(typeChanged(int)));
// Give the dialog a fixed border.
setWindowFlags(windowFlags() | Qt::MSWindowsFixedSizeDialogHint);
// Force the port to be an integer in the correct range.
_ui.lineEditPort->setValidator(new QIntValidator(1, 0xFFFF, this));
// Enable path auto-completion.
QCompleter* completer = new QCompleter(this);
QFileSystemModel* model = new QFileSystemModel(completer);
model->setRootPath(QDir::currentPath());
completer->setModel(model);
_ui.lineEditDBPath->setCompleter(completer);
// Enable replica set auto-completion.
if (!replicaSets.isEmpty()) {
QCompleter* rsCompleter = new QCompleter(this);
rsCompleter->setModel(new QStringListModel(replicaSets, rsCompleter));
_ui.lineEditReplicaSet->setCompleter(rsCompleter);
}
// Add all host types to the combo box.
for (int i = 0; i < HOST_TYPE_MAX; i++) {
_ui.comboBoxType->addItem(getHostTypeName(i));
}
// Default to a standard host.
_ui.comboBoxType->setCurrentIndex(HOST_TYPE_STANDARD);
}
HostType AddHostDialog::type() const {
return (HostType)_ui.comboBoxType->currentIndex();
}
int AddHostDialog::port() const {
return _ui.lineEditPort->text().toInt();
}
QString AddHostDialog::dbPath() const {
return _ui.lineEditDBPath->text();
}
QString AddHostDialog::replicaSet() const {
return _ui.lineEditReplicaSet->text();
}
QString AddHostDialog::configDB() const {
return _ui.lineEditConfigDB->text();
}
void AddHostDialog::accept() {
// Verify that the required fields are specified.
int type = _ui.comboBoxType->currentIndex();
if (_ui.lineEditPort->text().isEmpty()) {
QMessageBox::critical(this, QString(), "Please enter a port number.");
return;
}
if (_ui.lineEditDBPath->text().isEmpty() && type != HOST_TYPE_MONGOS) {
QMessageBox::critical(this, QString(), "Please enter a database path.");
return;
}
if (_ui.lineEditConfigDB->text().isEmpty() && type == HOST_TYPE_MONGOS) {
QMessageBox::critical(this, QString(), "Please enter a configuration database.");
return;
}
// Prompt to create the DB path if it doesn't exist.
if (!_ui.labelDBPath->text().isEmpty()) {
QDir dbPath(_ui.lineEditDBPath->text());
if (!dbPath.exists()) {
int result = QMessageBox::question(
this,
QString(),
"The specified database path does not exist. Do you want to create it?");
if (result != QMessageBox::Yes) {
return;
}
if (!dbPath.mkpath(".")) {
QMessageBox::critical(this, QString(), "Failed to create the database path!");
return;
}
}
}
QDialog::accept();
}
void AddHostDialog::browseForDBPath() {
QString newDBPath = QFileDialog::getExistingDirectory(
this,
"Browse for DB Path",
_ui.lineEditDBPath->text());
if (newDBPath.isEmpty()) {
return;
}
_ui.lineEditDBPath->setText(newDBPath);
}
void AddHostDialog::typeChanged(int index) {
if (index == HOST_TYPE_MONGOS) {
_ui.lineEditDBPath->clear();
}
_ui.lineEditDBPath->setEnabled(index != HOST_TYPE_MONGOS);
_ui.toolButtonDBPath->setEnabled(index != HOST_TYPE_MONGOS);
if (index == HOST_TYPE_MONGOS || index == HOST_TYPE_CONFIG) {
_ui.lineEditReplicaSet->clear();
}
_ui.lineEditReplicaSet->setEnabled(index != HOST_TYPE_MONGOS && index != HOST_TYPE_CONFIG);
_ui.lineEditConfigDB->setEnabled(index == HOST_TYPE_MONGOS);
}