File: Create_Template_Window.cpp

package info (click to toggle)
aqemu 0.7.3-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,112 kB
  • ctags: 1,783
  • sloc: cpp: 26,502; sh: 234; makefile: 37
file content (223 lines) | stat: -rw-r--r-- 5,804 bytes parent folder | download
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
/****************************************************************************
**
** Copyright (C) 2008-2009 Andrey Rijov <ANDron142@yandex.ru>
**
** This file is part of AQEMU.
**
** 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.
**
** 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 General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor,
** Boston, MA  02110-1301, USA.
**
****************************************************************************/

#include <QFileDialog>
#include <QFile>
#include <QFileInfo>
#include <QMessageBox>

#include "VM.h"
#include "Utils.h"
#include "Settings_Window.h"

Create_Template_Window::Create_Template_Window( QWidget *parent )
	: QDialog( parent )
{
	ui.setupUi( this );
	
	ui.Edit_Template_Folder->setText( Settings.value("VM_Directory","").toString() + "os_templates/" );
}

void Create_Template_Window::Set_VM_Path( const QString &path )
{
	ui.Edit_VM_File->setText( path );
}

void Create_Template_Window::on_TB_VM_File_Browse_clicked()
{
	QFileDialog::Options options;
	QString selectedFilter;
	
	QString fileName = QFileDialog::getOpenFileName( this, tr("Open VM File"),
													 Settings.value("VM_Directory","").toString() + "os_templates/",
													 tr("AQEMU VM Files (*.aqemu)"), &selectedFilter, options );
	
	if( ! (fileName.isNull() || fileName.isEmpty()) )
	{
		ui.Edit_VM_File->setText( fileName );
	}
}

void Create_Template_Window::on_TB_Template_Folder_Browse_clicked()
{
	QString fileName = QFileDialog::getExistingDirectory( this,tr("Select Template Folder"),
														  Get_Last_Dir_Path(ui.Edit_Template_Folder->text()),
														  QFileDialog::ShowDirsOnly );
	
	if( ! (fileName.isNull() || fileName.isEmpty()) )
	{
		ui.Edit_Template_Folder->setText( fileName );
	}
}

void Create_Template_Window::on_Button_Create_clicked()
{
	// Check Values
	if( ui.Edit_Template_Name->text().isEmpty() )
	{
		AQGraphic_Warning( tr("Warning!"),
						   tr("Template Name is Empty!") );
		return;
	}
	
	if( ! Name_is_Unique() )
	{
		AQWarning( "Create_Template_Window::on_Button_Create_clicked()",
				   "Template Name Not Unique!" );
		return;
	}
	
	if( ! QFile::exists(ui.Edit_VM_File->text()) )
	{
		AQGraphic_Warning( tr("Warning!"),
						   tr("VM File Not Exists!") );
		return;
	}
	
	if( ! QFile::exists(ui.Edit_Template_Folder->text()) )
	{
		AQGraphic_Warning( tr("Warning!"),
						   tr("Cannot Locate Template Folder!") );
		return;
	}
	
	if( ui.Edit_Template_Folder->text().at(ui.Edit_Template_Folder->text().count()-1) != '/' )
	{
		ui.Edit_Template_Folder->setText( ui.Edit_Template_Folder->text() + "/" );
	}
	
	// All OK. Creating new template
	Virtual_Machine *tmp_vm = new Virtual_Machine();
	
	if( ! tmp_vm->Load_VM(ui.Edit_VM_File->text()) )
	{
		AQGraphic_Warning( tr("Error!"),
						   tr("Cannot Load VM!") );
		delete tmp_vm;
		return;
	}
	
	Template_Options tmp_options = Template_Save_Default;
	
	if( ui.CH_FDD_CD->isChecked() )
	{
		tmp_options = tmp_options | Template_Save_FDD_CD;
	}
	
	if( ui.CH_HDD->isChecked() )
	{
		tmp_options = tmp_options | Template_Save_HDD;
	}
	
	if( ui.CH_Network->isChecked() )
	{
		tmp_options = tmp_options | Template_Save_Network;
	}
	
	if( ui.CH_Ports->isChecked() )
	{
		tmp_options = tmp_options | Template_Save_Ports;
	}
	
	// Create FS Valid File Name
	QString new_template_name = ui.Edit_Template_Name->text();
	
	QRegExp VM_Name_Val = QRegExp( "[^a-zA-Z0-9_]" );
	new_template_name = new_template_name.replace( VM_Name_Val, "_" );
	new_template_name = new_template_name.replace( "__", "_" );
	
	if( ! tmp_vm->Create_Template(ui.Edit_Template_Folder->text() +
		new_template_name + ".aqvmt",
		ui.Edit_Template_Name->text(), tmp_options ) )
	{
		AQGraphic_Warning( tr("Error!"),
						   tr("Cannot Create Template!") );
		delete tmp_vm;
		return;
	}
	
	delete tmp_vm;
	
	accept();
}

bool Create_Template_Window::Name_is_Unique()
{
	if( ui.CH_Use_Default_Folder->isChecked() ||
	    ui.Edit_Template_Folder->text() == Settings.value("VM_Directory","").toString() + "os_templates/" )
	{
		List_Templates = Get_Templates_List();
		
		for( int ix = 0; ix < List_Templates.count(); ++ix )
		{
			QFileInfo tmp_info = QFileInfo( List_Templates[ix] );
			
			// Create FS Valid File Name
			QString new_template_name = ui.Edit_Template_Name->text();
	
			QRegExp VM_Name_Val = QRegExp( "[^a-zA-Z0-9_]" );
			new_template_name = new_template_name.replace( VM_Name_Val, "_" );
			new_template_name = new_template_name.replace( "__", "_" );
			
			if( tmp_info.completeBaseName() == new_template_name )
			{
				int mes_ret = QMessageBox::question( this, tr("Replace"),
						tr("You Name For Template in Not Unique! Replace Previous Template?"),
						QMessageBox::Yes | QMessageBox::No, QMessageBox::No );
				
				if( mes_ret == QMessageBox::Yes )
				{
					return true;
				}
				else
				{
					return false;
				}
			}
		}
		
		return true;
	}
	else
	{
		// Other Folder
		if( QFile::exists(ui.Edit_Template_Folder->text()) )
		{
			int mes_ret = QMessageBox::question( this, tr("Replace"),
					tr("You Name For Template in Not Unique! Replace Previous Template?"),
					QMessageBox::Yes | QMessageBox::No, QMessageBox::No );
				
			if( mes_ret == QMessageBox::Yes )
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return true;
		}
	}
}