File: Snapshots_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 (391 lines) | stat: -rw-r--r-- 10,285 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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/****************************************************************************
**
** 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 <QMessageBox>
#include <QProcess>
#include "Utils.h"
#include "Snapshots_Window.h"
#include "Snapshot_Properties_Window.h"
#include "HDD_Image_Info.h"

Snapshots_Window::Snapshots_Window( QWidget *parent )
	: QDialog( parent )
{
	ui.setupUi( this );
}

void Snapshots_Window::Set_VM( Virtual_Machine *vm )
{
	Current_VM = vm;
	on_Button_Update_clicked();
	
	if( Current_VM->Get_State() != VM::VMS_Running )
	{
		ui.Button_Create->setEnabled( false );
		ui.Button_Delete->setEnabled( false );
	}
}

void Snapshots_Window::on_Snapshots_Tree_itemSelectionChanged()
{
	QList<QTreeWidgetItem*> sel_list = ui.Snapshots_Tree->selectedItems();
	
	if( sel_list.count() > 0 )
	{
		QString name = sel_list[0]->text( ui.Snapshots_Tree->currentColumn() );
		
		for( int ix = 0; ix < Current_VM->Get_Snapshots().count(); ++ix )
		{
			QString tmp_str = "";
			
			if( Current_VM->Get_Snapshots()[ix].Get_Name().isEmpty() )
			{
				tmp_str = Current_VM->Get_Snapshots()[ix].Get_Tag();
			}
			else
			{
				tmp_str = Current_VM->Get_Snapshots()[ix].Get_Name();
			}
			
			if( tmp_str == name )
			{
				ui.Label_ID->setText( tr("ID:") + " " + Current_VM->Get_Snapshots()[ix].Get_ID() );
				ui.Label_Size->setText( tr("Size:") + " " + Current_VM->Get_Snapshots()[ix].Get_Size() );
				ui.Label_Date->setText( tr("Date:") + " " + Current_VM->Get_Snapshots()[ix].Get_Date() );
				ui.Label_VM_Clock->setText( tr("VM Clock:") + " " + Current_VM->Get_Snapshots()[ix].Get_VM_Clock() );
				ui.Label_Description->setText( tr("Description:") + " " + Current_VM->Get_Snapshots()[ix].Get_Description() );
			}
		}
	}
}

void Snapshots_Window::on_Button_Create_clicked()
{
	Snapshot_Properties_Window *prop_win = new Snapshot_Properties_Window( this );
	
	if( prop_win->exec() == QDialog::Accepted )
	{
		QString tag = Get_FS_Compatible_VM_Name( prop_win->Get_Snapshot_Name() );
		
		bool next = true;
		
		for( int sx = 0; sx < Current_VM->Get_Snapshots().count(); ++sx )
		{
			if( Current_VM->Get_Snapshots()[sx].Get_Tag() == tag )
			{
				next = false;
			}
		}
		
		if( next == false )
		{
			for( int ix = 0; ix < 1024; ++ix )
			{
				tag = "snapshot" + QString::number( ix );
				
				bool find = false;
				
				for( int sx = 0; sx < Current_VM->Get_Snapshots().count(); ++sx )
				{
					if( Current_VM->Get_Snapshots()[sx].Get_Tag() == tag )
					{
						find = true;
					}
				}
				
				if( ! find ) break;
			}
		}
		
		Current_VM->Add_Snapshot( tag, prop_win->Get_Snapshot_Name(), prop_win->Get_Snapshot_Description() );
		Current_VM->Save_VM_State( tag, false );
		
		delete prop_win;
		
		accept();
	}
	else
	{
		delete prop_win;
	}
}

void Snapshots_Window::on_Button_Delete_clicked()
{
	if( ui.Snapshots_Tree->currentItem() == NULL ) return;
	
	if( ui.Snapshots_Tree->currentItem()->text(0).isEmpty() )
	{
		AQGraphic_Warning( tr("Warning!"), tr("Before Select Snapshot For Delete!") );
		return;
	}
	
	int ret = QMessageBox::question( this, tr("Warning"), tr("Delete Current Snapshot?"),
									 QMessageBox::Yes | QMessageBox::No, QMessageBox::No );
	
	if( ret == QMessageBox::Yes )
	{
		Current_VM->Delete_Snapshot( ui.Snapshots_Tree->currentItem()->text(0) );
		ui.Snapshots_Tree->currentItem()->setHidden( true );
	}
}

void Snapshots_Window::on_Button_Start_clicked()
{
	if( ui.Snapshots_Tree->currentItem() == NULL ) return;
	
	if( Current_VM->Get_State() == VM::VMS_Power_Off ||
		Current_VM->Get_State() == VM::VMS_Saved )
	{
		if( ui.Snapshots_Tree->currentItem()->text(0).isEmpty() )
		{
			AQGraphic_Warning( tr("Warning!"), tr("Before Select Snapshot For Start!") );
			return;
		}
		else
		{
			if( Current_VM->Start_Snapshot(ui.Snapshots_Tree->currentItem()->text(0)) )
			{
				QSettings settings;
				
				if( settings.value( "Show_Emulator_Control_Window", "yes" ).toString() == "yes" )
				{
					Current_VM->Show_Emu_Ctl_Win();
				}
				
				accept();
			}
			else
			{
				AQError( "void Snapshots_Window::on_Button_Start_clicked()",
						 "Cannot Start VM!" );
			}
		}
	}
	else
	{
		int ret = QMessageBox::question( this, tr("Warning"), tr("This VM Running! Stop VM and Load Snapshot?"),
										 QMessageBox::Yes | QMessageBox::No, QMessageBox::No );
		
		if( ret == QMessageBox::Yes )
		{
			Current_VM->Load_VM_State( ui.Snapshots_Tree->currentItem()->text(0) );
			accept();
		}
	}
}

void Snapshots_Window::on_Button_Properties_clicked()
{
	if( ui.Snapshots_Tree->currentItem() == NULL ) return;
	
	Snapshot_Properties_Window *prop_win = new Snapshot_Properties_Window( this );
	
	QList<QTreeWidgetItem*> sel_list = ui.Snapshots_Tree->selectedItems();
	
	if( sel_list.count() > 0 )
	{
		QString tag = sel_list[0]->text( ui.Snapshots_Tree->currentColumn() );
		
		for( int ix = 0; ix < Current_VM->Get_Snapshots().count(); ++ix )
		{
			QString tmp_str = "";
			
			if( Current_VM->Get_Snapshots()[ix].Get_Name().isEmpty() )
			{
				tmp_str = Current_VM->Get_Snapshots()[ix].Get_Tag();
			}
			else
			{
				tmp_str = Current_VM->Get_Snapshots()[ix].Get_Name();
			}
			
			if( tmp_str == tag )
			{
				if( Current_VM->Get_Snapshots()[ix].Get_Name().isEmpty() )
				{
					prop_win->Set_Snapshot_Name( Current_VM->Get_Snapshots()[ix].Get_Tag() );
				}
				else
				{
					prop_win->Set_Snapshot_Name( Current_VM->Get_Snapshots()[ix].Get_Name() );
				}
				
				prop_win->Set_Snapshot_Description( Current_VM->Get_Snapshots()[ix].Get_Description() );
				
				if( prop_win->exec() == QDialog::Accepted )
				{
					VM_Snapshot tmp = Current_VM->Get_Snapshots()[ix];
					
					tmp.Set_Name( prop_win->Get_Snapshot_Name() );
					tmp.Set_Description( prop_win->Get_Snapshot_Description() );
					
					Current_VM->Set_Snapshot( ix, tmp );
					Current_VM->Save_VM();
					
					on_Button_Update_clicked();
					
					return;
				}
			}
		}
	}
}

void Snapshots_Window::on_Button_Update_clicked()
{
	ui.Snapshots_Tree->clear();
	
	if(  Update_Info() )
	{
		for( int ix = 0; ix < Current_VM->Get_Snapshots().count(); ++ix )
		{
			QTreeWidgetItem *tmp_it = new QTreeWidgetItem( ui.Snapshots_Tree );
			
			if( Current_VM->Get_Snapshots()[ix].Get_Name().isEmpty() )
			{
				tmp_it->setText( 0, Current_VM->Get_Snapshots()[ix].Get_Tag() );
			}
			else
			{
				tmp_it->setText( 0, Current_VM->Get_Snapshots()[ix].Get_Name() );
			}
			
			ui.Snapshots_Tree->insertTopLevelItem( 0, tmp_it );
		}
	}
	
	if( Current_VM->Get_Snapshots().count() < 1 )
	{
		ui.Button_Start->setEnabled( false );
	}
}

bool Snapshots_Window::Update_Info()
{
	if( Current_VM == NULL )
	{
		AQError( "bool Snapshots_Window::Update_Info()", "Current_VM == NULL" );
		return false;
	}
	
	if( ! Current_VM->Get_HDA().Get_Enabled() )
	{
		AQGraphic_Warning( tr("Error!"), tr("For Use Snapshot, You Most Add Hard Drive to This VM!") );
		return false;
	}
	
	QProcess *qimg = new QProcess( this );
	QStringList qimg_args;
	qimg_args << "info" << Current_VM->Get_HDA().Get_Image_File_Name();
	
	QString img_path = Current_VM->Get_Current_Emulator_Binary_Path( "qemu-img kvm-img" );
	
	if( img_path.isEmpty() ) img_path = "qemu-img";
	
	qimg->start( img_path, qimg_args );
	
	if( ! qimg->waitForFinished(500) )
	{
		AQError( "bool Snapshots_Window::Update_Info()",
				 "qemu-img Not Finished! Hard Disk Info Cannot Read!" );
		return false;
	}
	
	QByteArray info_str_ba = qimg->readAll();
	QString info_str = QString( info_str_ba ); // Create QString
	
	delete qimg;
	
	// Pre Str RegExp:
	// .*Snapshot list:.*ID[\s]+TAG[\s]+VM[\s]+SIZE[\s]+DATE[\s]+VM[\s]+CLOCK(.+)
	QRegExp RegInfo = QRegExp( ".*Snapshot list:.*ID[\\s]+TAG[\\s]+VM[\\s]+SIZE[\\s]+DATE[\\s]+VM[\\s]+CLOCK(.+)" );
	
	if( ! RegInfo.exactMatch(info_str) )
	{
		AQError( "bool Snapshots_Window::Update_Info()",
				 "QRegExp Not Matched!" );
		return false;
	}
	
	QStringList info_lines = RegInfo.capturedTexts();
	
	QList<VM_Snapshot> finded_snapshots;
	
	if( info_lines.count() == 2 )
	{
		// Snapshot Line RegExp:
		// ([\d])+[\s]+([a-zA-Z0-9_]+)[\s]+([0-9KMG]+|[0-9]+.[0-9KMG]+)[\s]+(.+)[\s]{2,}(.+)
		
		QStringList snapshots_list = info_lines[1].split( "\n", QString::SkipEmptyParts );
		QRegExp snap_regexp = QRegExp( "([\\d])+[\\s]+([a-zA-Z0-9_]+)[\\s]+([0-9KMG]+|[0-9]+.[0-9KMG]+)[\\s]+(.+)[\\s]{2,}(.+)" );
		
		for( int sx = 0; sx < snapshots_list.count(); ++sx )
		{
			if( ! snap_regexp.exactMatch(snapshots_list[sx]) ) continue;
			
			QStringList snap_info = snap_regexp.capturedTexts();
			
			if( snap_info.count() != 6 ) continue;
			
			VM_Snapshot tmp_snap;
			tmp_snap.Set_ID( snap_info[1] );
			tmp_snap.Set_Tag( snap_info[2] );
			tmp_snap.Set_Size( snap_info[3] );
			tmp_snap.Set_Date( snap_info[4] );
			tmp_snap.Set_VM_Clock( snap_info[5] );
			
			finded_snapshots << tmp_snap;
		}
	}
	else
	{
		return false;
	}
	
	// Unique
	QList<VM_Snapshot> compare_list = Current_VM->Get_Snapshots();
	
	for( int fx = 0; fx < finded_snapshots.count(); ++fx )
	{
		bool next = true;
		
		for( int cx = 0; next && cx < compare_list.count(); ++cx )
		{
			if( finded_snapshots[fx].Get_Tag() == compare_list[cx].Get_Tag() )
			{
				next = false;
				
				finded_snapshots[fx].Set_Name( compare_list[cx].Get_Name() );
				finded_snapshots[fx].Set_Description( compare_list[cx].Get_Description() );
				
				compare_list.removeAt( cx );
			}
		}
	}
	
	// Add and Save
	Current_VM->Set_Snapshots( finded_snapshots );
	Current_VM->Save_VM();
	
	return true;
}