1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 Andrey Rijov <ANDron142@yandex.ru>
4 ** Copyright (C) 2016 Tobias Gläßer (Qt5 port)
5 **
6 ** This file is part of AQEMU.
7 **
8 ** This program is free software; you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License as published by
10 ** the Free Software Foundation; either version 2 of the License.
11 **
12 ** This program is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 ** GNU General Public License for more details.
16 **
17 ** You should have received a copy of the GNU General Public License
18 ** along with this program; if not, write to the Free Software
19 ** Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 ** Boston, MA  02110-1301, USA.
21 **
22 ****************************************************************************/
23 
24 #include "Delete_VM_Files_Window.h"
25 #include "Utils.h"
26 #include <QHeaderView>
27 #include <QFile>
28 #include <QMessageBox>
29 
Delete_VM_Files_Window(QWidget * parent)30 Delete_VM_Files_Window::Delete_VM_Files_Window( QWidget *parent )
31 	: QDialog( parent )
32 {
33 	ui.setupUi( this );
34 
35 	QHeaderView *hv = new QHeaderView( Qt::Vertical, ui.Files_List );
36 	hv->setSectionResizeMode( QHeaderView::Fixed );
37 	ui.Files_List->setVerticalHeader( hv );
38 
39 	hv = new QHeaderView( Qt::Horizontal, ui.Files_List );
40 	hv->setStretchLastSection( true );
41 	hv->setSectionResizeMode( QHeaderView::ResizeToContents );
42 	ui.Files_List->setHorizontalHeader( hv );
43 }
44 
Delete_VM_Files_Window(Virtual_Machine * vm,QWidget * parent)45 Delete_VM_Files_Window::Delete_VM_Files_Window( Virtual_Machine *vm, QWidget *parent )
46 	: QDialog( parent )
47 {
48 	ui.setupUi( this );
49 
50 	QHeaderView *hv = new QHeaderView( Qt::Vertical, ui.Files_List );
51     hv->setSectionResizeMode( QHeaderView::Fixed );
52 	ui.Files_List->setVerticalHeader( hv );
53 
54 	hv = new QHeaderView( Qt::Horizontal, ui.Files_List );
55 	hv->setStretchLastSection( true );
56 	hv->setSectionResizeMode( QHeaderView::ResizeToContents );
57 	ui.Files_List->setHorizontalHeader( hv );
58 
59 	Set_VM( vm );
60 }
61 
Set_VM(Virtual_Machine * vm)62 void Delete_VM_Files_Window::Set_VM( Virtual_Machine *vm )
63 {
64 	if( vm == NULL )
65 	{
66 		AQError( "void Delete_VM_Files_Window::Set_VM( Virtual_Machine *vm )",
67 				 "vm == NULL" );
68 		return;
69 	}
70 
71 	// Clear List
72 	Clear_List();
73 
74 	VM_Name = vm->Get_Machine_Name();
75 
76 	File_List_Item tmp;
77 
78 	// VM File
79 	VM_Path = vm->Get_VM_XML_File_Path();
80 
81 	// Screenshot
82 	if( Path_Valid(vm->Get_Screenshot_Path()) )
83 	{
84 		tmp.Hard_Drive = false;
85 		tmp.Name = tr( "Screenshot" );
86 		tmp.Path = vm->Get_Screenshot_Path();
87 
88 		File_List_Items << tmp;
89 		Add_To_Files_List( tmp );
90 	}
91 
92 	// FD0
93 	if( Path_Valid(vm->Get_FD0().Get_File_Name()) )
94 	{
95 		tmp.Hard_Drive = false;
96 		tmp.Name = tr( "Floppy A" );
97 		tmp.Path = vm->Get_FD0().Get_File_Name();
98 
99 		File_List_Items << tmp;
100 		Add_To_Files_List( tmp );
101 	}
102 
103 	// FD1
104 	if( Path_Valid(vm->Get_FD1().Get_File_Name()) )
105 	{
106 		tmp.Hard_Drive = false;
107 		tmp.Name = tr( "Floppy B" );
108 		tmp.Path = vm->Get_FD1().Get_File_Name();
109 
110 		File_List_Items << tmp;
111 		Add_To_Files_List( tmp );
112 	}
113 
114 	// CD-ROM
115 	if( Path_Valid(vm->Get_CD_ROM().Get_File_Name()) )
116 	{
117 		tmp.Hard_Drive = false;
118 		tmp.Name = tr( "CD-ROM" );
119 		tmp.Path = vm->Get_CD_ROM().Get_File_Name();
120 
121 		File_List_Items << tmp;
122 		Add_To_Files_List( tmp );
123 	}
124 
125 	// HDA
126 	if( Path_Valid(vm->Get_HDA().Get_File_Name()) )
127 	{
128 		tmp.Hard_Drive = true;
129 		tmp.Name = tr( "HDA (Primary Master)" );
130 		tmp.Path = vm->Get_HDA().Get_File_Name();
131 
132 		File_List_Items << tmp;
133 		Add_To_Files_List( tmp );
134 	}
135 
136 	// HDB
137 	if( Path_Valid(vm->Get_HDB().Get_File_Name()) )
138 	{
139 		tmp.Hard_Drive = true;
140 		tmp.Name = tr( "HDB (Primary Slave)" );
141 		tmp.Path = vm->Get_HDB().Get_File_Name();
142 
143 		File_List_Items << tmp;
144 		Add_To_Files_List( tmp );
145 	}
146 
147 	// HDC
148 	if( Path_Valid(vm->Get_HDC().Get_File_Name()) )
149 	{
150 		tmp.Hard_Drive = true;
151 		tmp.Name = tr( "HDC (Secondary Master)" );
152 		tmp.Path = vm->Get_HDC().Get_File_Name();
153 
154 		File_List_Items << tmp;
155 		Add_To_Files_List( tmp );
156 	}
157 
158 	// HDD
159 	if( Path_Valid(vm->Get_HDD().Get_File_Name()) )
160 	{
161 		tmp.Hard_Drive = true;
162 		tmp.Name = tr( "HDD (Secondary Slave)" );
163 		tmp.Path = vm->Get_HDD().Get_File_Name();
164 
165 		File_List_Items << tmp;
166 		Add_To_Files_List( tmp );
167 	}
168 
169 	// ROM File
170 	if( Path_Valid(vm->Get_ROM_File()) )
171 	{
172 		tmp.Hard_Drive = false;
173 		tmp.Name = tr( "ROM File" );
174 		tmp.Path = vm->Get_ROM_File();
175 
176 		File_List_Items << tmp;
177 		Add_To_Files_List( tmp );
178 	}
179 
180 	// MTDBlock File
181 	if( Path_Valid(vm->Get_MTDBlock_File()) )
182 	{
183 		tmp.Hard_Drive = false;
184 		tmp.Name = tr( "MTDBlock File" );
185 		tmp.Path = vm->Get_MTDBlock_File();
186 
187 		File_List_Items << tmp;
188 		Add_To_Files_List( tmp );
189 	}
190 
191 	// SD Card File
192 	if( Path_Valid(vm->Get_SecureDigital_File()) )
193 	{
194 		tmp.Hard_Drive = false;
195 		tmp.Name = tr( "SD Card File" );
196 		tmp.Path = vm->Get_SecureDigital_File();
197 
198 		File_List_Items << tmp;
199 		Add_To_Files_List( tmp );
200 	}
201 
202 	// PFlash File
203 	if( Path_Valid(vm->Get_PFlash_File()) )
204 	{
205 		tmp.Hard_Drive = false;
206 		tmp.Name = tr( "PFlash File" );
207 		tmp.Path = vm->Get_PFlash_File();
208 
209 		File_List_Items << tmp;
210 		Add_To_Files_List( tmp );
211 	}
212 
213 	// bzImage
214 	if( Path_Valid(vm->Get_bzImage_Path()) )
215 	{
216 		tmp.Hard_Drive = false;
217 		tmp.Name = tr( "Kernel bzImage" );
218 		tmp.Path = vm->Get_bzImage_Path();
219 
220 		File_List_Items << tmp;
221 		Add_To_Files_List( tmp );
222 	}
223 
224 	// Initrd
225 	if( Path_Valid(vm->Get_Initrd_Path()) )
226 	{
227 		tmp.Hard_Drive = false;
228 		tmp.Name = tr( "Kernel Initrd" );
229 		tmp.Path = vm->Get_Initrd_Path();
230 
231 		File_List_Items << tmp;
232 		Add_To_Files_List( tmp );
233 	}
234 
235 	// Serial Ports
236 	if( vm->Get_Serial_Ports().count() > 0 )
237 	{
238 		QList<VM_Port> tmp_port = vm->Get_Serial_Ports();
239 
240 		for( int ix = 0; ix < tmp_port.count(); ix++ )
241 		{
242 			if( tmp_port[ix].Get_Port_Redirection() == VM::PR_file )
243 			{
244 				if( Path_Valid(tmp_port[ix].Get_Parametrs_Line()) )
245 				{
246 					tmp.Hard_Drive = false;
247 					tmp.Name = tr( "Serial Port Redirection" );
248 					tmp.Path = tmp_port[ix].Get_Parametrs_Line();
249 
250 					File_List_Items << tmp;
251 					Add_To_Files_List( tmp );
252 				}
253 			}
254 		}
255 	}
256 
257 	// Parallel Ports
258 	if( vm->Get_Parallel_Ports().count() > 0 )
259 	{
260 		QList<VM_Port> tmp_port = vm->Get_Parallel_Ports();
261 
262 		for( int ix = 0; ix < tmp_port.count(); ix++ )
263 		{
264 			if( tmp_port[ix].Get_Port_Redirection() == VM::PR_file )
265 			{
266 				if( Path_Valid(tmp_port[ix].Get_Parametrs_Line()) )
267 				{
268 					tmp.Hard_Drive = false;
269 					tmp.Name = tr( "Parallel Port Redirection" );
270 					tmp.Path = tmp_port[ix].Get_Parametrs_Line();
271 
272 					File_List_Items << tmp;
273 					Add_To_Files_List( tmp );
274 				}
275 			}
276 		}
277 	}
278 
279 	// Storage Devices
280 	if( vm->Get_Storage_Devices_List().count() > 0 )
281 	{
282 		QList<VM_Nativ_Storage_Device> tmp_dev = vm->Get_Storage_Devices_List();
283 
284 		for( int ix = 0; ix < tmp_dev.count(); ix++ )
285 		{
286 			if( Path_Valid(tmp_dev[ix].Get_File_Path()) )
287 			{
288 				if( tmp_dev[ix].Get_Media() == VM::DM_Disk ) tmp.Hard_Drive = true;
289 				else tmp.Hard_Drive = false;
290 
291 				tmp.Name = tr( "Storage Device" );
292 				tmp.Path = tmp_dev[ix].Get_File_Path();
293 
294 				File_List_Items << tmp;
295 				Add_To_Files_List( tmp );
296 			}
297 		}
298 	}
299 }
300 
on_Button_Delete_clicked()301 void Delete_VM_Files_Window::on_Button_Delete_clicked()
302 {
303 	int mes_ret = QMessageBox::question( this, tr("Confirm Delete"),
304 										 tr("Delete \"") + VM_Name + tr("\" VM and Selected Files?"),
305 										 QMessageBox::Yes | QMessageBox::No, QMessageBox::No );
306 
307 	QString no_Delete_Files_List;
308 
309 	if( mes_ret == QMessageBox::Yes )
310 	{
311 		// Delete VM XML File
312 		if( ! QFile::remove(VM_Path) )
313 		{
314 			AQError( "void Delete_VM_Files_Window::on_Button_Delete_clicked()",
315 					 "Cannot Delete VM File: \"" + VM_Path + "\"" );
316 			no_Delete_Files_List += VM_Path + "\n";
317 		}
318 
319 		// Delete Files
320 		for( int ix = 0; ix < ui.Files_List->rowCount(); ix++ )
321 		{
322 			QTableWidgetItem *item_CheckBox = ui.Files_List->item( ix, 0 );
323 			QTableWidgetItem *item_Text = ui.Files_List->item( ix, 2 );
324 
325 			if( item_CheckBox == NULL || item_Text == NULL )
326 			{
327 				AQError( "void Delete_VM_Files_Window::on_Button_Delete_clicked()",
328 						 "item_CheckBox == NULL || item_Text == NULL" );
329 				continue;
330 			}
331 
332 			// Cheked?
333 			if( item_CheckBox->checkState() == Qt::Checked )
334 			{
335 				if( ! QFile::remove(item_Text->text()) )
336 				{
337 					AQError( "void Delete_VM_Files_Window::on_Button_Delete_clicked()",
338 							 "Cannot Delete File: \"" + item_Text->text() + "\"" );
339 					no_Delete_Files_List += item_Text->text() + "\n";
340 					continue;
341 				}
342 			}
343 		}
344 
345 		// Show Errors
346 		if( ! no_Delete_Files_List.isEmpty() )
347 		{
348 			QMessageBox::information( this, tr("An error occurred while deleting files"),
349 									tr("This Files Not Deleted:\n") + no_Delete_Files_List + tr("Please Check Permissions!"),
350 									QMessageBox::Ok );
351 		}
352 
353 		// Send accept
354 		accept();
355 	}
356 }
357 
on_RB_Show_HDD_toggled(bool checked)358 void Delete_VM_Files_Window::on_RB_Show_HDD_toggled( bool checked )
359 {
360 	static bool old = false;
361 
362 	if( old == checked ) return;
363 	else old = checked;
364 
365 	Clear_List();
366 
367 	for( int ix = 0; ix < File_List_Items.count(); ix++ )
368 	{
369 		if( checked )
370 		{
371 			if( File_List_Items[ix].Hard_Drive ) Add_To_Files_List( File_List_Items[ix] );
372 		}
373 		else
374 		{
375 			Add_To_Files_List( File_List_Items[ix] );
376 		}
377 	}
378 }
379 
Add_To_Files_List(const File_List_Item & item)380 void Delete_VM_Files_Window::Add_To_Files_List( const File_List_Item &item )
381 {
382 	if( item.Name.isEmpty() || item.Path.isEmpty() ) return;
383 
384 	QTableWidgetItem *it = new QTableWidgetItem();
385 	ui.Files_List->insertRow( ui.Files_List->rowCount() );
386 
387 	// CheckBox
388 	it->setFlags( Qt::ItemIsEnabled | Qt::ItemIsUserCheckable );
389 	it->setCheckState( Qt::Checked );
390 	ui.Files_List->setItem( ui.Files_List->rowCount()-1, 0, it );
391 
392 	// Name
393 	it = new QTableWidgetItem( item.Name );
394 	ui.Files_List->setItem( ui.Files_List->rowCount()-1, 1, it );
395 
396 	// Path
397 	it = new QTableWidgetItem( item.Path );
398 	ui.Files_List->setItem( ui.Files_List->rowCount()-1, 2, it );
399 }
400 
Path_Valid(const QString & path)401 bool Delete_VM_Files_Window::Path_Valid( const QString &path )
402 {
403 	if( path.isEmpty() ) return false;
404 	if( ! QFile::exists(path) ) return false;
405 	if( path.startsWith("/dev/") ) return false; // FIXME Windows version
406 
407 	return true; // All OK
408 }
409 
Clear_List()410 void Delete_VM_Files_Window::Clear_List()
411 {
412 	ui.Files_List->clearContents();
413 	while( ui.Files_List->rowCount() > 0 ) ui.Files_List->removeRow( 0 );
414 }
415