/
Delete_VM_Files_Window.cpp
413 lines (342 loc) · 9.91 KB
/
Delete_VM_Files_Window.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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/****************************************************************************
**
** Copyright (C) 2010 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 "Delete_VM_Files_Window.h"
#include "Utils.h"
#include <QHeaderView>
#include <QFile>
#include <QMessageBox>
Delete_VM_Files_Window::Delete_VM_Files_Window( QWidget *parent )
: QDialog( parent )
{
ui.setupUi( this );
QHeaderView *hv = new QHeaderView( Qt::Vertical, ui.Files_List );
hv->setResizeMode( QHeaderView::Fixed );
ui.Files_List->setVerticalHeader( hv );
hv = new QHeaderView( Qt::Horizontal, ui.Files_List );
hv->setStretchLastSection( true );
hv->setResizeMode( QHeaderView::ResizeToContents );
ui.Files_List->setHorizontalHeader( hv );
}
Delete_VM_Files_Window::Delete_VM_Files_Window( Virtual_Machine *vm, QWidget *parent )
: QDialog( parent )
{
ui.setupUi( this );
QHeaderView *hv = new QHeaderView( Qt::Vertical, ui.Files_List );
hv->setResizeMode( QHeaderView::Fixed );
ui.Files_List->setVerticalHeader( hv );
hv = new QHeaderView( Qt::Horizontal, ui.Files_List );
hv->setStretchLastSection( true );
hv->setResizeMode( QHeaderView::ResizeToContents );
ui.Files_List->setHorizontalHeader( hv );
Set_VM( vm );
}
void Delete_VM_Files_Window::Set_VM( Virtual_Machine *vm )
{
if( vm == NULL )
{
AQError( "void Delete_VM_Files_Window::Set_VM( Virtual_Machine *vm )",
"vm == NULL" );
return;
}
// Clear List
Clear_List();
VM_Name = vm->Get_Machine_Name();
File_List_Item tmp;
// VM File
VM_Path = vm->Get_VM_XML_File_Path();
// Screenshot
if( Path_Valid(vm->Get_Screenshot_Path()) )
{
tmp.Hard_Drive = false;
tmp.Name = tr( "Screenshot" );
tmp.Path = vm->Get_Screenshot_Path();
File_List_Items << tmp;
Add_To_Files_List( tmp );
}
// FD0
if( Path_Valid(vm->Get_FD0().Get_File_Name()) )
{
tmp.Hard_Drive = false;
tmp.Name = tr( "Floppy A" );
tmp.Path = vm->Get_FD0().Get_File_Name();
File_List_Items << tmp;
Add_To_Files_List( tmp );
}
// FD1
if( Path_Valid(vm->Get_FD1().Get_File_Name()) )
{
tmp.Hard_Drive = false;
tmp.Name = tr( "Floppy B" );
tmp.Path = vm->Get_FD1().Get_File_Name();
File_List_Items << tmp;
Add_To_Files_List( tmp );
}
// CD-ROM
if( Path_Valid(vm->Get_CD_ROM().Get_File_Name()) )
{
tmp.Hard_Drive = false;
tmp.Name = tr( "CD-ROM" );
tmp.Path = vm->Get_CD_ROM().Get_File_Name();
File_List_Items << tmp;
Add_To_Files_List( tmp );
}
// HDA
if( Path_Valid(vm->Get_HDA().Get_File_Name()) )
{
tmp.Hard_Drive = true;
tmp.Name = tr( "HDA (Primary Master)" );
tmp.Path = vm->Get_HDA().Get_File_Name();
File_List_Items << tmp;
Add_To_Files_List( tmp );
}
// HDB
if( Path_Valid(vm->Get_HDB().Get_File_Name()) )
{
tmp.Hard_Drive = true;
tmp.Name = tr( "HDB (Primary Slave)" );
tmp.Path = vm->Get_HDB().Get_File_Name();
File_List_Items << tmp;
Add_To_Files_List( tmp );
}
// HDC
if( Path_Valid(vm->Get_HDC().Get_File_Name()) )
{
tmp.Hard_Drive = true;
tmp.Name = tr( "HDC (Secondary Master)" );
tmp.Path = vm->Get_HDC().Get_File_Name();
File_List_Items << tmp;
Add_To_Files_List( tmp );
}
// HDD
if( Path_Valid(vm->Get_HDD().Get_File_Name()) )
{
tmp.Hard_Drive = true;
tmp.Name = tr( "HDD (Secondary Slave)" );
tmp.Path = vm->Get_HDD().Get_File_Name();
File_List_Items << tmp;
Add_To_Files_List( tmp );
}
// ROM File
if( Path_Valid(vm->Get_ROM_File()) )
{
tmp.Hard_Drive = false;
tmp.Name = tr( "ROM File" );
tmp.Path = vm->Get_ROM_File();
File_List_Items << tmp;
Add_To_Files_List( tmp );
}
// MTDBlock File
if( Path_Valid(vm->Get_MTDBlock_File()) )
{
tmp.Hard_Drive = false;
tmp.Name = tr( "MTDBlock File" );
tmp.Path = vm->Get_MTDBlock_File();
File_List_Items << tmp;
Add_To_Files_List( tmp );
}
// SD Card File
if( Path_Valid(vm->Get_SecureDigital_File()) )
{
tmp.Hard_Drive = false;
tmp.Name = tr( "SD Card File" );
tmp.Path = vm->Get_SecureDigital_File();
File_List_Items << tmp;
Add_To_Files_List( tmp );
}
// PFlash File
if( Path_Valid(vm->Get_PFlash_File()) )
{
tmp.Hard_Drive = false;
tmp.Name = tr( "PFlash File" );
tmp.Path = vm->Get_PFlash_File();
File_List_Items << tmp;
Add_To_Files_List( tmp );
}
// bzImage
if( Path_Valid(vm->Get_bzImage_Path()) )
{
tmp.Hard_Drive = false;
tmp.Name = tr( "Kernel bzImage" );
tmp.Path = vm->Get_bzImage_Path();
File_List_Items << tmp;
Add_To_Files_List( tmp );
}
// Initrd
if( Path_Valid(vm->Get_Initrd_Path()) )
{
tmp.Hard_Drive = false;
tmp.Name = tr( "Kernel Initrd" );
tmp.Path = vm->Get_Initrd_Path();
File_List_Items << tmp;
Add_To_Files_List( tmp );
}
// Serial Ports
if( vm->Get_Serial_Ports().count() > 0 )
{
QList<VM_Port> tmp_port = vm->Get_Serial_Ports();
for( int ix = 0; ix < tmp_port.count(); ix++ )
{
if( tmp_port[ix].Get_Port_Redirection() == VM::PR_file )
{
if( Path_Valid(tmp_port[ix].Get_Parametrs_Line()) )
{
tmp.Hard_Drive = false;
tmp.Name = tr( "Serial Port Redirection" );
tmp.Path = tmp_port[ix].Get_Parametrs_Line();
File_List_Items << tmp;
Add_To_Files_List( tmp );
}
}
}
}
// Parallel Ports
if( vm->Get_Parallel_Ports().count() > 0 )
{
QList<VM_Port> tmp_port = vm->Get_Parallel_Ports();
for( int ix = 0; ix < tmp_port.count(); ix++ )
{
if( tmp_port[ix].Get_Port_Redirection() == VM::PR_file )
{
if( Path_Valid(tmp_port[ix].Get_Parametrs_Line()) )
{
tmp.Hard_Drive = false;
tmp.Name = tr( "Parallel Port Redirection" );
tmp.Path = tmp_port[ix].Get_Parametrs_Line();
File_List_Items << tmp;
Add_To_Files_List( tmp );
}
}
}
}
// Storage Devices
if( vm->Get_Storage_Devices_List().count() > 0 )
{
QList<VM_Nativ_Storage_Device> tmp_dev = vm->Get_Storage_Devices_List();
for( int ix = 0; ix < tmp_dev.count(); ix++ )
{
if( Path_Valid(tmp_dev[ix].Get_File_Path()) )
{
if( tmp_dev[ix].Get_Media() == VM::DM_Disk ) tmp.Hard_Drive = true;
else tmp.Hard_Drive = false;
tmp.Name = tr( "Storage Device" );
tmp.Path = tmp_dev[ix].Get_File_Path();
File_List_Items << tmp;
Add_To_Files_List( tmp );
}
}
}
}
void Delete_VM_Files_Window::on_Button_Delete_clicked()
{
int mes_ret = QMessageBox::question( this, tr("Confirm Delete"),
tr("Delete \"") + VM_Name + tr("\" VM and Selected Files?"),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No );
QString no_Delete_Files_List;
if( mes_ret == QMessageBox::Yes )
{
// Delete VM XML File
if( ! QFile::remove(VM_Path) )
{
AQError( "void Delete_VM_Files_Window::on_Button_Delete_clicked()",
"Cannot Delete VM File: \"" + VM_Path + "\"" );
no_Delete_Files_List += VM_Path + "\n";
}
// Delete Files
for( int ix = 0; ix < ui.Files_List->rowCount(); ix++ )
{
QTableWidgetItem *item_CheckBox = ui.Files_List->item( ix, 0 );
QTableWidgetItem *item_Text = ui.Files_List->item( ix, 2 );
if( item_CheckBox == NULL || item_Text == NULL )
{
AQError( "void Delete_VM_Files_Window::on_Button_Delete_clicked()",
"item_CheckBox == NULL || item_Text == NULL" );
continue;
}
// Cheked?
if( item_CheckBox->checkState() == Qt::Checked )
{
if( ! QFile::remove(item_Text->text()) )
{
AQError( "void Delete_VM_Files_Window::on_Button_Delete_clicked()",
"Cannot Delete File: \"" + item_Text->text() + "\"" );
no_Delete_Files_List += item_Text->text() + "\n";
continue;
}
}
}
// Show Errors
if( ! no_Delete_Files_List.isEmpty() )
{
QMessageBox::information( this, tr("An error occurred while deleting files"),
tr("This Files Not Deleted:\n") + no_Delete_Files_List + tr("Please Check Permissions!"),
QMessageBox::Ok );
}
// Send accept
accept();
}
}
void Delete_VM_Files_Window::on_RB_Show_HDD_toggled( bool checked )
{
static bool old = false;
if( old == checked ) return;
else old = checked;
Clear_List();
for( int ix = 0; ix < File_List_Items.count(); ix++ )
{
if( checked )
{
if( File_List_Items[ix].Hard_Drive ) Add_To_Files_List( File_List_Items[ix] );
}
else
{
Add_To_Files_List( File_List_Items[ix] );
}
}
}
void Delete_VM_Files_Window::Add_To_Files_List( const File_List_Item &item )
{
if( item.Name.isEmpty() || item.Path.isEmpty() ) return;
QTableWidgetItem *it = new QTableWidgetItem();
ui.Files_List->insertRow( ui.Files_List->rowCount() );
// CheckBox
it->setFlags( Qt::ItemIsEnabled | Qt::ItemIsUserCheckable );
it->setCheckState( Qt::Checked );
ui.Files_List->setItem( ui.Files_List->rowCount()-1, 0, it );
// Name
it = new QTableWidgetItem( item.Name );
ui.Files_List->setItem( ui.Files_List->rowCount()-1, 1, it );
// Path
it = new QTableWidgetItem( item.Path );
ui.Files_List->setItem( ui.Files_List->rowCount()-1, 2, it );
}
bool Delete_VM_Files_Window::Path_Valid( const QString &path )
{
if( path.isEmpty() ) return false;
if( ! QFile::exists(path) ) return false;
if( path.startsWith("/dev/") ) return false; // FIXME Windows version
return true; // All OK
}
void Delete_VM_Files_Window::Clear_List()
{
ui.Files_List->clearContents();
while( ui.Files_List->rowCount() > 0 ) ui.Files_List->removeRow( 0 );
}