-
Notifications
You must be signed in to change notification settings - Fork 0
/
vagrantup.cpp
203 lines (143 loc) · 4.3 KB
/
vagrantup.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "vagrantup.h"
#include "ui_vagrantup.h"
#include <QFileDialog>
#include <QSettings>
#include <QInputDialog>
#include <QSettings>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QDebug>
VagrantUp::VagrantUp(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::VagrantUp)
{
ui->setupUi(this);
this->statusBar()->setSizeGripEnabled(false);
settings = new QSettings("VagrantUp");
vagrantFilenames = new QStringList();
loadSettings();
checkVagrant();
}
void VagrantUp::loadSettings()
{
QString vagrantfiles = settings->value("vagrantfiles").toString();
QString debug_message = "Loading previous Vagrantfiles: "
+ settings->value("vagrantfiles").toString();
qDebug(debug_message.toStdString().c_str());
QString current_file;
for( int i = 0; i < vagrantfiles.length(); i++ )
{
if(vagrantfiles.at(i) == ',' ) {
processFilename(current_file);
current_file.clear();
} else {
current_file.append(vagrantfiles.at(i));
}
}
processFilename(current_file);
}
void VagrantUp::checkVagrant()
{
QString command = "vagrant -h";
int status = system(command.toLocal8Bit().data());
if (status) {
ui->statusBar->showMessage("Vagrant is not installed.");
} else {
ui->statusBar->showMessage("Vagrant is installed and ready.");
}
}
VagrantUp::~VagrantUp()
{
delete ui;
}
void VagrantUp::bring_up_vagrantfiles(QList<QListWidgetItem*>* vagrantFiles)
{
}
void VagrantUp::updateSettings()
{
// create of string of comma seperated values to serialize the list of vagrantfiles
// it is hacky, but it works just fine
QString serializedFilenames;
for (int i = 0; i < vagrantFilenames->length(); i++)
{
serializedFilenames.append(vagrantFilenames->at(i));
if (i < vagrantFilenames->length() - 1 ) {
serializedFilenames.append(QString(","));
}
}
settings->setValue("vagrantfiles",settings->value("vagrantfiles",serializedFilenames));
}
void VagrantUp::processFilename(QString filename)
{
QString debugmsg = "Processing filename '" + filename +"'";
qDebug(debugmsg.toStdString().c_str());
QString enviroName = "";
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file);
QString line = in.readLine();
while ( !in.atEnd() )
{
if(line.contains("config.vm.hostname"))
{
enviroName = line.mid(line.indexOf("\"")+1);
enviroName = enviroName.mid(0,enviroName.indexOf("\""));
}
line = in.readLine();
}
if (enviroName == "")
enviroName = filename;
ui->listWidget->addItem(enviroName);
vagrantFilenames->append(filename);
updateSettings();
}
void VagrantUp::on_add_pressed()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Vagrantfile"),"~/",tr("Vagrantfile (Vagrantfile)"));
processFilename(fileName);
}
void VagrantUp::on_remove_pressed()
{
qDeleteAll(ui->listWidget->selectedItems());
if(ui->listWidget->count() == 0) {
ui->remove->setEnabled(false);
ui->up->setEnabled(false);
}
// TODO: Need to keep meta data in another data structure
// Will finish when Vagrantfile class is finished
vagrantFilenames->removeOne();
}
void VagrantUp::on_listWidget_itemSelectionChanged()
{
if(ui->listWidget->selectedItems().length() > 0)
ui->remove->setEnabled(true);
else
ui->remove->setEnabled(false);
}
void VagrantUp::on_refresh_pressed()
{
checkVagrant();
}
void VagrantUp::on_listWidget_itemActivated(QListWidgetItem *item)
{
ui->up->setEnabled(true);
}
void VagrantUp::on_up_clicked()
{
QString command('vagrant up');
QList<QListWidgetItem*> vagrantFiles = ui->listWidget->selectedItems();
if (vagrantFiles.length() > 1)
{
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "Multiple Vagrantfiles Selected",
"Are you sure you want to bring up mutliple Vagrant environments?",
QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::Yes) {
bring_up_vagrantfiles(&vagrantFiles);
} else {
// Do nothing, fall back to Vagrantfile list
}
}
}