File: machinewizard.cpp

package info (click to toggle)
qtemu 1.0.5-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,264 kB
  • ctags: 237
  • sloc: cpp: 2,043; makefile: 8
file content (303 lines) | stat: -rw-r--r-- 9,189 bytes parent folder | download | duplicates (3)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/****************************************************************************
**
** Copyright (C) 2006-2007 Urs Wolfer <uwolfer @ fwo.ch>
**
** This file is part of QtEmu.
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU Library General Public License
** along with this library; see the file COPYING.LIB.  If not, write to
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
** Boston, MA 02110-1301, USA.
**
****************************************************************************/

#include "machinewizard.h"

#include "config.h"

#include <QProcess>
#include <QMessageBox>
#include <QFile>
#include <QDoubleSpinBox>
#include <QBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QDir>
#include <QIcon>
#include <QComboBox>
#include <QTextStream>
#include <QSettings>
#include <QPushButton>
#include <QFileDialog>
#include <QDomElement>

MachineWizard::MachineWizard(const QString &myMachinesPathParent, QWidget *parent)
    : Wizard(parent)
{
    myMachinesPath = myMachinesPathParent;
    chooseSystemPage = new ChooseSystemPage(this);
    locationPage = new LocationPage(this);
    imagePage = new ImagePage(this);

    setFirstPage(chooseSystemPage);

    setWindowTitle(tr("Create a new Machine"));
    resize(400, 350);
}

QString MachineWizard::newMachine(const QString &myMachinesPath, QWidget *parent)
{
    MachineWizard wizard(myMachinesPath, parent);

    QLineEdit *pathLineEdit = qobject_cast<QLineEdit *>(wizard.locationPage->pathLineEdit);
    QLineEdit *nameLineEdit = qobject_cast<QLineEdit *>(wizard.locationPage->nameLineEdit);

    QString result;
    bool accepted = (wizard.exec() == QDialog::Accepted);
    if (accepted)
        result = pathLineEdit->text()+'/'+nameLineEdit->text().replace(' ', '_')+".qte";
    else
        result = QString();

    return result;
}

ChooseSystemPage::ChooseSystemPage(MachineWizard *wizard)
    : MachineWizardPage(wizard)
{
    comboSystem = new QComboBox;
    comboSystem->addItem(tr("Select a System..."));
    comboSystem->addItem(tr("Linux"));
    comboSystem->addItem(tr("Windows 98"));
    comboSystem->addItem(tr("Windows 2000"));
    comboSystem->addItem(tr("Windows XP"));
    comboSystem->addItem(tr("ReactOS"));
    comboSystem->addItem(tr("Other"));

    connect(comboSystem, SIGNAL(activated(int)), this, SIGNAL(completeStateChanged()));

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(comboSystem);
    layout->addStretch();
    setLayout(layout);
}

void ChooseSystemPage::updateTitle()
{
    wizard->setTitle(tr("Select the operating system you want to install"));
}

void ChooseSystemPage::resetPage()
{
}

WizardPage *ChooseSystemPage::nextPage()
{
    wizard->osName = comboSystem->currentText();
    return wizard->locationPage;
}

bool ChooseSystemPage::isComplete()
{
    return !(comboSystem->currentText()==tr("Select a System..."));
}


LocationPage::LocationPage(MachineWizard *wizard)
    : MachineWizardPage(wizard)
{
    nameLabel = new QLabel(tr("&Name:"));
    nameLineEdit = new QLineEdit;
    nameLabel->setBuddy(nameLineEdit);
    connect(nameLineEdit, SIGNAL(textChanged(QString)), this, SLOT(privateSlot()));

    pathLabel = new QLabel(tr("&Path:"));
    pathLineEdit = new QLineEdit;
    privateSlot();
    pathLabel->setBuddy(pathLineEdit);

    QSettings settings("QtEmu", "QtEmu");
    QString iconTheme = settings.value("iconTheme", "oxygen").toString();

    QPushButton *pathSelectButton = new QPushButton(QIcon(":/images/" + iconTheme + "/open.png"), QString(), this);
    connect(pathSelectButton, SIGNAL(clicked()), this, SLOT(setNewPath()));

    QHBoxLayout *pathLayout = new QHBoxLayout;
    pathLayout->addWidget(pathLineEdit);
    pathLayout->addWidget(pathSelectButton);

    connect(nameLineEdit, SIGNAL(textChanged(QString)), this, SIGNAL(completeStateChanged()));

    QGridLayout *layout = new QGridLayout;
    layout->addWidget(nameLabel, 1, 0);
    layout->addWidget(nameLineEdit, 1, 1);
    layout->addWidget(pathLabel, 2, 0);
    layout->addLayout(pathLayout, 2, 1);
    layout->setRowStretch(3, 1);
    setLayout(layout);
}

void LocationPage::updateTitle()
{
    wizard->setTitle(tr("Choose name and location for the new machine"));
}

void LocationPage::privateSlot()
{
    pathLineEdit->setText(wizard->myMachinesPath+
                          '/'+nameLineEdit->text().replace(' ', '_'));
}

void LocationPage::setNewPath()
{
    QString newPath = QFileDialog::getExistingDirectory(this, tr("Select a folder for saving the hard disk image"),
                                                        wizard->myMachinesPath);
    if (!newPath.isEmpty())
        pathLineEdit->setText(newPath);
}

void LocationPage::resetPage()
{
    nameLineEdit->setText(wizard->osName);
}

WizardPage *LocationPage::nextPage()
{
    wizard->osNameUser = nameLineEdit->text();
    wizard->osPathUser = pathLineEdit->text();
    return wizard->imagePage;
}

bool LocationPage::isComplete()
{
    return !(nameLineEdit->text().isEmpty());
}


ImagePage::ImagePage(MachineWizard *wizard)
    : MachineWizardPage(wizard)
{
    sizeLabel = new QLabel(tr("&Disk image size:"));
    sizeSpinBox = new QDoubleSpinBox;
    sizeSpinBox->setFixedSize(sizeSpinBox->sizeHint());
    sizeLabel->setBuddy(sizeSpinBox);
    sizeGbLabel = new QLabel(tr("GB"));
    sizeGbLabel->setFixedSize(sizeGbLabel->sizeHint());
    sizeGbLabel->setBuddy(sizeSpinBox);
    connect(sizeSpinBox, SIGNAL(valueChanged(double)), this, SIGNAL(completeStateChanged()));

    QVBoxLayout *layout = new QVBoxLayout;

    QHBoxLayout *sizeLayout = new QHBoxLayout;
    sizeLayout->addWidget(sizeLabel);
    sizeLayout->addWidget(sizeSpinBox);
    sizeLayout->addWidget(sizeGbLabel);

    layout->addLayout(sizeLayout);
    layout->addStretch();
    setLayout(layout);

    connect(wizard, SIGNAL(finished()), this, SLOT(privateSlot()));
}

void ImagePage::updateTitle()
{
    wizard->setTitle(tr("Specify disk image details"));
}

void ImagePage::resetPage()
{
    sizeSpinBox->setValue(0);
}

bool ImagePage::isComplete()
{
    return (sizeSpinBox->value()!=0);
}

void ImagePage::privateSlot()
{
    QString osName = wizard->osNameUser;
    QString osPath = wizard->osPathUser;

    dir = new QDir();
    dir->mkpath(osPath);

    QDomDocument domDocument("qtemu");
    QDomProcessingInstruction process = domDocument.createProcessingInstruction(
                              "xml", "version=\"1.0\" encoding=\"UTF-8\"");
    domDocument.appendChild(process);

    QDomElement root = domDocument.createElement("qtemu");
    root.setAttribute("version", "1.0");
    domDocument.appendChild(root);

    QDomElement machine = domDocument.createElement("machine");
    root.appendChild(machine);

    QDomElement domElement;
    QDomText domText;

    domElement = domDocument.createElement("name");
    machine.appendChild(domElement);
    domText = domDocument.createTextNode(osName);
    domElement.appendChild(domText);

    domElement = domDocument.createElement("hdd");
    machine.appendChild(domElement);
    domText = domDocument.createTextNode(osPath+'/'+osName.replace(' ', '_')+".img");
    domElement.appendChild(domText);

    domElement = domDocument.createElement("memory");
    machine.appendChild(domElement);
    domText = domDocument.createTextNode("128");
    domElement.appendChild(domText);

    domElement = domDocument.createElement("notes");
    machine.appendChild(domElement);
    domText = domDocument.createTextNode(tr("Click here to write down some notes "
                                            "about this machine."));
    domElement.appendChild(domText);

    domElement = domDocument.createElement("snapshot");
    machine.appendChild(domElement);
#ifdef DEVELOPER
    domText = domDocument.createTextNode("true");
#else
    domText = domDocument.createTextNode("false");
#endif
    domElement.appendChild(domText);

    QFile file(osPath+'/'+osName.replace(' ', '_')+".qte");
    if (!file.open(QFile::WriteOnly | QFile::Truncate))
    {
        QMessageBox::critical(this, tr("Error"), tr("Image NOT created!"));
        return;
    }

    QTextStream out(&file);
    domDocument.save(out, 4);

    imageCreateProcess = new QProcess(this);
    imageCreateProcess->setWorkingDirectory(osPath);
    QStringList arguments;
    arguments << "create" << "-f" << "raw"
              << osName.replace(' ', '_')+".img"
              << QString::number(sizeSpinBox->value()*1000)+'M';
#ifndef Q_OS_WIN32
    imageCreateProcess->start("qemu-img", arguments);
#elif defined(Q_OS_WIN32)
    imageCreateProcess->start("qemu/qemu-img.exe", arguments);
#endif
    QMessageBox::information(this, tr("Finished"), tr("Image created"));
}