PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Delete_VM_Files_Window.cpp

https://gitlab.com/pranith/aqemu
C++ | 414 lines | 294 code | 71 blank | 49 comment | 55 complexity | bf6876ca57a27a35b71318eba21486e9 MD5 | raw file
  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. #include "Delete_VM_Files_Window.h"
  24. #include "Utils.h"
  25. #include <QHeaderView>
  26. #include <QFile>
  27. #include <QMessageBox>
  28. Delete_VM_Files_Window::Delete_VM_Files_Window( QWidget *parent )
  29. : QDialog( parent )
  30. {
  31. ui.setupUi( this );
  32. QHeaderView *hv = new QHeaderView( Qt::Vertical, ui.Files_List );
  33. hv->setSectionResizeMode( QHeaderView::Fixed );
  34. ui.Files_List->setVerticalHeader( hv );
  35. hv = new QHeaderView( Qt::Horizontal, ui.Files_List );
  36. hv->setStretchLastSection( true );
  37. hv->setSectionResizeMode( QHeaderView::ResizeToContents );
  38. ui.Files_List->setHorizontalHeader( hv );
  39. }
  40. Delete_VM_Files_Window::Delete_VM_Files_Window( Virtual_Machine *vm, QWidget *parent )
  41. : QDialog( parent )
  42. {
  43. ui.setupUi( this );
  44. QHeaderView *hv = new QHeaderView( Qt::Vertical, ui.Files_List );
  45. hv->setSectionResizeMode( QHeaderView::Fixed );
  46. ui.Files_List->setVerticalHeader( hv );
  47. hv = new QHeaderView( Qt::Horizontal, ui.Files_List );
  48. hv->setStretchLastSection( true );
  49. hv->setSectionResizeMode( QHeaderView::ResizeToContents );
  50. ui.Files_List->setHorizontalHeader( hv );
  51. Set_VM( vm );
  52. }
  53. void Delete_VM_Files_Window::Set_VM( Virtual_Machine *vm )
  54. {
  55. if( vm == NULL )
  56. {
  57. AQError( "void Delete_VM_Files_Window::Set_VM( Virtual_Machine *vm )",
  58. "vm == NULL" );
  59. return;
  60. }
  61. // Clear List
  62. Clear_List();
  63. VM_Name = vm->Get_Machine_Name();
  64. File_List_Item tmp;
  65. // VM File
  66. VM_Path = vm->Get_VM_XML_File_Path();
  67. // Screenshot
  68. if( Path_Valid(vm->Get_Screenshot_Path()) )
  69. {
  70. tmp.Hard_Drive = false;
  71. tmp.Name = tr( "Screenshot" );
  72. tmp.Path = vm->Get_Screenshot_Path();
  73. File_List_Items << tmp;
  74. Add_To_Files_List( tmp );
  75. }
  76. // FD0
  77. if( Path_Valid(vm->Get_FD0().Get_File_Name()) )
  78. {
  79. tmp.Hard_Drive = false;
  80. tmp.Name = tr( "Floppy A" );
  81. tmp.Path = vm->Get_FD0().Get_File_Name();
  82. File_List_Items << tmp;
  83. Add_To_Files_List( tmp );
  84. }
  85. // FD1
  86. if( Path_Valid(vm->Get_FD1().Get_File_Name()) )
  87. {
  88. tmp.Hard_Drive = false;
  89. tmp.Name = tr( "Floppy B" );
  90. tmp.Path = vm->Get_FD1().Get_File_Name();
  91. File_List_Items << tmp;
  92. Add_To_Files_List( tmp );
  93. }
  94. // CD-ROM
  95. if( Path_Valid(vm->Get_CD_ROM().Get_File_Name()) )
  96. {
  97. tmp.Hard_Drive = false;
  98. tmp.Name = tr( "CD-ROM" );
  99. tmp.Path = vm->Get_CD_ROM().Get_File_Name();
  100. File_List_Items << tmp;
  101. Add_To_Files_List( tmp );
  102. }
  103. // HDA
  104. if( Path_Valid(vm->Get_HDA().Get_File_Name()) )
  105. {
  106. tmp.Hard_Drive = true;
  107. tmp.Name = tr( "HDA (Primary Master)" );
  108. tmp.Path = vm->Get_HDA().Get_File_Name();
  109. File_List_Items << tmp;
  110. Add_To_Files_List( tmp );
  111. }
  112. // HDB
  113. if( Path_Valid(vm->Get_HDB().Get_File_Name()) )
  114. {
  115. tmp.Hard_Drive = true;
  116. tmp.Name = tr( "HDB (Primary Slave)" );
  117. tmp.Path = vm->Get_HDB().Get_File_Name();
  118. File_List_Items << tmp;
  119. Add_To_Files_List( tmp );
  120. }
  121. // HDC
  122. if( Path_Valid(vm->Get_HDC().Get_File_Name()) )
  123. {
  124. tmp.Hard_Drive = true;
  125. tmp.Name = tr( "HDC (Secondary Master)" );
  126. tmp.Path = vm->Get_HDC().Get_File_Name();
  127. File_List_Items << tmp;
  128. Add_To_Files_List( tmp );
  129. }
  130. // HDD
  131. if( Path_Valid(vm->Get_HDD().Get_File_Name()) )
  132. {
  133. tmp.Hard_Drive = true;
  134. tmp.Name = tr( "HDD (Secondary Slave)" );
  135. tmp.Path = vm->Get_HDD().Get_File_Name();
  136. File_List_Items << tmp;
  137. Add_To_Files_List( tmp );
  138. }
  139. // ROM File
  140. if( Path_Valid(vm->Get_ROM_File()) )
  141. {
  142. tmp.Hard_Drive = false;
  143. tmp.Name = tr( "ROM File" );
  144. tmp.Path = vm->Get_ROM_File();
  145. File_List_Items << tmp;
  146. Add_To_Files_List( tmp );
  147. }
  148. // MTDBlock File
  149. if( Path_Valid(vm->Get_MTDBlock_File()) )
  150. {
  151. tmp.Hard_Drive = false;
  152. tmp.Name = tr( "MTDBlock File" );
  153. tmp.Path = vm->Get_MTDBlock_File();
  154. File_List_Items << tmp;
  155. Add_To_Files_List( tmp );
  156. }
  157. // SD Card File
  158. if( Path_Valid(vm->Get_SecureDigital_File()) )
  159. {
  160. tmp.Hard_Drive = false;
  161. tmp.Name = tr( "SD Card File" );
  162. tmp.Path = vm->Get_SecureDigital_File();
  163. File_List_Items << tmp;
  164. Add_To_Files_List( tmp );
  165. }
  166. // PFlash File
  167. if( Path_Valid(vm->Get_PFlash_File()) )
  168. {
  169. tmp.Hard_Drive = false;
  170. tmp.Name = tr( "PFlash File" );
  171. tmp.Path = vm->Get_PFlash_File();
  172. File_List_Items << tmp;
  173. Add_To_Files_List( tmp );
  174. }
  175. // bzImage
  176. if( Path_Valid(vm->Get_bzImage_Path()) )
  177. {
  178. tmp.Hard_Drive = false;
  179. tmp.Name = tr( "Kernel bzImage" );
  180. tmp.Path = vm->Get_bzImage_Path();
  181. File_List_Items << tmp;
  182. Add_To_Files_List( tmp );
  183. }
  184. // Initrd
  185. if( Path_Valid(vm->Get_Initrd_Path()) )
  186. {
  187. tmp.Hard_Drive = false;
  188. tmp.Name = tr( "Kernel Initrd" );
  189. tmp.Path = vm->Get_Initrd_Path();
  190. File_List_Items << tmp;
  191. Add_To_Files_List( tmp );
  192. }
  193. // Serial Ports
  194. if( vm->Get_Serial_Ports().count() > 0 )
  195. {
  196. QList<VM_Port> tmp_port = vm->Get_Serial_Ports();
  197. for( int ix = 0; ix < tmp_port.count(); ix++ )
  198. {
  199. if( tmp_port[ix].Get_Port_Redirection() == VM::PR_file )
  200. {
  201. if( Path_Valid(tmp_port[ix].Get_Parametrs_Line()) )
  202. {
  203. tmp.Hard_Drive = false;
  204. tmp.Name = tr( "Serial Port Redirection" );
  205. tmp.Path = tmp_port[ix].Get_Parametrs_Line();
  206. File_List_Items << tmp;
  207. Add_To_Files_List( tmp );
  208. }
  209. }
  210. }
  211. }
  212. // Parallel Ports
  213. if( vm->Get_Parallel_Ports().count() > 0 )
  214. {
  215. QList<VM_Port> tmp_port = vm->Get_Parallel_Ports();
  216. for( int ix = 0; ix < tmp_port.count(); ix++ )
  217. {
  218. if( tmp_port[ix].Get_Port_Redirection() == VM::PR_file )
  219. {
  220. if( Path_Valid(tmp_port[ix].Get_Parametrs_Line()) )
  221. {
  222. tmp.Hard_Drive = false;
  223. tmp.Name = tr( "Parallel Port Redirection" );
  224. tmp.Path = tmp_port[ix].Get_Parametrs_Line();
  225. File_List_Items << tmp;
  226. Add_To_Files_List( tmp );
  227. }
  228. }
  229. }
  230. }
  231. // Storage Devices
  232. if( vm->Get_Storage_Devices_List().count() > 0 )
  233. {
  234. QList<VM_Nativ_Storage_Device> tmp_dev = vm->Get_Storage_Devices_List();
  235. for( int ix = 0; ix < tmp_dev.count(); ix++ )
  236. {
  237. if( Path_Valid(tmp_dev[ix].Get_File_Path()) )
  238. {
  239. if( tmp_dev[ix].Get_Media() == VM::DM_Disk ) tmp.Hard_Drive = true;
  240. else tmp.Hard_Drive = false;
  241. tmp.Name = tr( "Storage Device" );
  242. tmp.Path = tmp_dev[ix].Get_File_Path();
  243. File_List_Items << tmp;
  244. Add_To_Files_List( tmp );
  245. }
  246. }
  247. }
  248. }
  249. void Delete_VM_Files_Window::on_Button_Delete_clicked()
  250. {
  251. int mes_ret = QMessageBox::question( this, tr("Confirm Delete"),
  252. tr("Delete \"") + VM_Name + tr("\" VM and Selected Files?"),
  253. QMessageBox::Yes | QMessageBox::No, QMessageBox::No );
  254. QString no_Delete_Files_List;
  255. if( mes_ret == QMessageBox::Yes )
  256. {
  257. // Delete VM XML File
  258. if( ! QFile::remove(VM_Path) )
  259. {
  260. AQError( "void Delete_VM_Files_Window::on_Button_Delete_clicked()",
  261. "Cannot Delete VM File: \"" + VM_Path + "\"" );
  262. no_Delete_Files_List += VM_Path + "\n";
  263. }
  264. // Delete Files
  265. for( int ix = 0; ix < ui.Files_List->rowCount(); ix++ )
  266. {
  267. QTableWidgetItem *item_CheckBox = ui.Files_List->item( ix, 0 );
  268. QTableWidgetItem *item_Text = ui.Files_List->item( ix, 2 );
  269. if( item_CheckBox == NULL || item_Text == NULL )
  270. {
  271. AQError( "void Delete_VM_Files_Window::on_Button_Delete_clicked()",
  272. "item_CheckBox == NULL || item_Text == NULL" );
  273. continue;
  274. }
  275. // Cheked?
  276. if( item_CheckBox->checkState() == Qt::Checked )
  277. {
  278. if( ! QFile::remove(item_Text->text()) )
  279. {
  280. AQError( "void Delete_VM_Files_Window::on_Button_Delete_clicked()",
  281. "Cannot Delete File: \"" + item_Text->text() + "\"" );
  282. no_Delete_Files_List += item_Text->text() + "\n";
  283. continue;
  284. }
  285. }
  286. }
  287. // Show Errors
  288. if( ! no_Delete_Files_List.isEmpty() )
  289. {
  290. QMessageBox::information( this, tr("An error occurred while deleting files"),
  291. tr("This Files Not Deleted:\n") + no_Delete_Files_List + tr("Please Check Permissions!"),
  292. QMessageBox::Ok );
  293. }
  294. // Send accept
  295. accept();
  296. }
  297. }
  298. void Delete_VM_Files_Window::on_RB_Show_HDD_toggled( bool checked )
  299. {
  300. static bool old = false;
  301. if( old == checked ) return;
  302. else old = checked;
  303. Clear_List();
  304. for( int ix = 0; ix < File_List_Items.count(); ix++ )
  305. {
  306. if( checked )
  307. {
  308. if( File_List_Items[ix].Hard_Drive ) Add_To_Files_List( File_List_Items[ix] );
  309. }
  310. else
  311. {
  312. Add_To_Files_List( File_List_Items[ix] );
  313. }
  314. }
  315. }
  316. void Delete_VM_Files_Window::Add_To_Files_List( const File_List_Item &item )
  317. {
  318. if( item.Name.isEmpty() || item.Path.isEmpty() ) return;
  319. QTableWidgetItem *it = new QTableWidgetItem();
  320. ui.Files_List->insertRow( ui.Files_List->rowCount() );
  321. // CheckBox
  322. it->setFlags( Qt::ItemIsEnabled | Qt::ItemIsUserCheckable );
  323. it->setCheckState( Qt::Checked );
  324. ui.Files_List->setItem( ui.Files_List->rowCount()-1, 0, it );
  325. // Name
  326. it = new QTableWidgetItem( item.Name );
  327. ui.Files_List->setItem( ui.Files_List->rowCount()-1, 1, it );
  328. // Path
  329. it = new QTableWidgetItem( item.Path );
  330. ui.Files_List->setItem( ui.Files_List->rowCount()-1, 2, it );
  331. }
  332. bool Delete_VM_Files_Window::Path_Valid( const QString &path )
  333. {
  334. if( path.isEmpty() ) return false;
  335. if( ! QFile::exists(path) ) return false;
  336. if( path.startsWith("/dev/") ) return false; // FIXME Windows version
  337. return true; // All OK
  338. }
  339. void Delete_VM_Files_Window::Clear_List()
  340. {
  341. ui.Files_List->clearContents();
  342. while( ui.Files_List->rowCount() > 0 ) ui.Files_List->removeRow( 0 );
  343. }