1 /****************************************************************************
2 **
3 ** Copyright (C) 2008-2010 Andrey Rijov <ANDron142@yandex.ru>
4 **
5 ** This file is part of AQEMU.
6 **
7 ** This program is free software; you can redistribute it and/or modify
8 ** it under the terms of the GNU General Public License as published by
9 ** the Free Software Foundation; either version 2 of the License.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 ** Boston, MA 02110-1301, USA.
20 **
21 ****************************************************************************/
22
23 #include <QDir>
24 #include <QFileDialog>
25 #include <QFileInfo>
26
27 #include "Properties_Window.h"
28 #include "Create_HDD_Image_Window.h"
29 #include "Utils.h"
30 #include "System_Info.h"
31 #include "Add_New_Device_Window.h"
32
Properties_Window(QWidget * parent)33 Properties_Window::Properties_Window( QWidget *parent )
34 : QDialog( parent )
35 {
36 ui.setupUi( this );
37
38 HDD_Info = new HDD_Image_Info();
39
40 connect( ui.CB_FD_Devices, SIGNAL(editTextChanged(QString)),
41 this, SLOT(on_Button_Update_Info_clicked()) );
42
43 connect( ui.CB_CDROM_Devices, SIGNAL(editTextChanged(QString)),
44 this, SLOT(on_Button_Update_Info_clicked()) );
45
46 connect( ui.Edit_HDD_Image_Path, SIGNAL(textChanged(QString)),
47 this, SLOT(on_Button_Update_Info_clicked()) );
48 }
49
Get_Floppy()50 const VM_Storage_Device &Properties_Window::Get_Floppy()
51 {
52 PW_Storage.Set_Enabled( true );
53 return PW_Storage;
54 }
55
Get_CD_ROM()56 const VM_Storage_Device &Properties_Window::Get_CD_ROM()
57 {
58 PW_Storage.Set_Enabled( true );
59 return PW_Storage;
60 }
61
Get_HDD()62 const VM_HDD &Properties_Window::Get_HDD()
63 {
64 PW_HDD.Set_Enabled( true );
65 return PW_HDD;
66 }
67
Set_Floppy(const VM_Storage_Device & fd,const QString & name)68 void Properties_Window::Set_Floppy( const VM_Storage_Device &fd, const QString &name )
69 {
70 PW_Storage = fd;
71
72 ui.Label_Name->setText( ui.Label_Name->text() + name );
73
74 ui.GB_Floppy->setVisible( true );
75 ui.GB_Floppy->setEnabled( true );
76 ui.GB_CDROM->setVisible( false );
77 ui.GB_CDROM->setEnabled( false );
78 ui.GB_HDD->setVisible( false );
79 ui.GB_HDD->setEnabled( false );
80
81 // Find Floppy's
82 ui.CB_FD_Devices->clear();
83
84 QStringList fd_list = System_Info::Get_Host_FDD_List();
85 fd_list += Get_FDD_Recent_Images_List();
86
87 if( fd_list.count() < 1 )
88 {
89 AQDebug( "void Properties_Window::Set_Floppy( const VM_Storage_Device &fd, const QString &name )",
90 "Cannot Find Host Floppy Devices!" );
91 }
92 else
93 {
94 for( int d = 0; d < fd_list.count(); ++d )
95 ui.CB_FD_Devices->addItem( fd_list[d] );
96 }
97
98 ui.CB_FD_Devices->setEditText( fd.Get_File_Name() );
99
100 on_Button_Update_Info_clicked();
101
102 resize( width(), minimumSizeHint().height() );
103 }
104
Set_CD_ROM(const VM_Storage_Device & cd,const QString & name)105 void Properties_Window::Set_CD_ROM( const VM_Storage_Device &cd, const QString &name )
106 {
107 PW_Storage = cd;
108
109 ui.Label_Name->setText( ui.Label_Name->text() + name );
110
111 ui.GB_Floppy->setVisible( false );
112 ui.GB_Floppy->setEnabled( false );
113 ui.GB_CDROM->setVisible( true );
114 ui.GB_CDROM->setEnabled( true );
115 ui.GB_HDD->setVisible( false );
116 ui.GB_HDD->setEnabled( false );
117
118 // Find CD_ROM's
119 ui.CB_CDROM_Devices->clear();
120
121 QStringList cd_list = System_Info::Get_Host_CDROM_List();
122 cd_list += Get_CD_Recent_Images_List();
123
124 if( cd_list.count() < 1 )
125 {
126 AQDebug( "void Properties_Window::Set_CD_ROM( const VM_CDROM &cd, const QString &name )",
127 "Cannot Find Host CD-ROM Devices!" );
128 }
129 else
130 {
131 for( int d = 0; d < cd_list.count(); ++d )
132 ui.CB_CDROM_Devices->addItem( cd_list[d] );
133 }
134
135 ui.CB_CDROM_Devices->setEditText( cd.Get_File_Name() );
136
137 on_Button_Update_Info_clicked();
138
139 resize( width(), minimumSizeHint().height() );
140 }
141
Set_HDD(const VM_HDD & hd,const QString & name)142 void Properties_Window::Set_HDD( const VM_HDD &hd, const QString &name )
143 {
144 PW_HDD = hd;
145
146 connect( HDD_Info, SIGNAL(Completed(bool)),
147 this, SLOT(Update_HDD(bool)) );
148
149 ui.Label_Name->setText( ui.Label_Name->text() + name );
150 ui.Edit_HDD_Image_Path->setText( hd.Get_File_Name() );
151
152 ui.GB_Floppy->setVisible( false );
153 ui.GB_Floppy->setEnabled( false );
154 ui.GB_CDROM->setVisible( false );
155 ui.GB_CDROM->setEnabled( false );
156 ui.GB_HDD->setVisible( true );
157 ui.GB_HDD->setEnabled( true );
158
159 on_Button_Update_Info_clicked();
160
161 resize( width(), minimumSizeHint().height() );
162 }
163
Set_Enabled(bool enabled)164 void Properties_Window::Set_Enabled( bool enabled )
165 {
166 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled( enabled );
167 }
168
Set_Current_Machine_Devices(const Available_Devices * dev)169 void Properties_Window::Set_Current_Machine_Devices( const Available_Devices *dev )
170 {
171 Current_Machine_Devices = dev;
172 }
173
done(int r)174 void Properties_Window::done(int r)
175 {
176 if ( r == QDialog::Accepted )
177 {
178 if( ui.GB_Floppy->isEnabled() )
179 {
180 if( ! QFile::exists(ui.CB_FD_Devices->lineEdit()->text()) )
181 {
182 AQGraphic_Warning( tr("Warning"), tr("Image file doesn't exist!") );
183 return;
184 }
185 else
186 {
187 Add_To_Recent_FDD_Files( ui.CB_FD_Devices->lineEdit()->text() );
188 QDialog::done(r);
189 return;
190 }
191 }
192 else if( ui.GB_CDROM->isEnabled() )
193 {
194 if( ! QFile::exists(ui.CB_CDROM_Devices->lineEdit()->text()) )
195 {
196 AQGraphic_Warning( tr("Warning"), tr("Image file doesn't exist!") );
197 return;
198 }
199 else
200 {
201 Add_To_Recent_CD_Files( ui.CB_CDROM_Devices->lineEdit()->text() );
202 QDialog::done(r);
203 return;
204 }
205 }
206 else if( ui.GB_HDD->isEnabled() )
207 {
208 if( ! QFile::exists(ui.Edit_HDD_Image_Path->text()) )
209 {
210 AQGraphic_Warning( tr("Warning"), tr("Image file doesn't exist!") );
211 return;
212 }
213 else
214 {
215 QDialog::done(r);
216 return;
217 }
218 }
219 else
220 {
221 AQError( "void Properties_Window::done(int)",
222 "Default Section!" );
223 return;
224 }
225 }
226 QDialog::done(r);
227 }
228
on_Button_Update_Info_clicked()229 void Properties_Window::on_Button_Update_Info_clicked()
230 {
231 if( ui.GB_Floppy->isEnabled() )
232 {
233 VM_Storage_Device *fd = new VM_Storage_Device( true, ui.CB_FD_Devices->lineEdit()->text() );
234
235 if( ui.CB_FD_Devices->lineEdit()->text().isEmpty() )
236 {
237 ui.Label_Info->setText( tr("Image Size: ") + QString::number(0) + tr("Kb") );
238 return;
239 }
240
241 if( ! QFile::exists(ui.CB_FD_Devices->lineEdit()->text()) )
242 {
243 ui.Label_Info->setText( tr("Image Size: ") + QString::number(0) + tr("Kb") );
244 return;
245 }
246
247 QFileInfo fd_img = QFileInfo( fd->Get_File_Name() );
248 qint64 size_in_bytes = fd_img.size();
249
250 if( size_in_bytes <= 0 )
251 ui.Label_Info->setText( tr("Image Size: ") + QString::number(0) + tr("Kb") );
252 else
253 ui.Label_Info->setText( tr("Image Size: ") +
254 QString::number((int)size_in_bytes / 1024.0) + tr("Kb") );
255
256 delete fd;
257 }
258 else if( ui.GB_CDROM->isEnabled() )
259 {
260 VM_Storage_Device *cd = new VM_Storage_Device( true, ui.CB_CDROM_Devices->lineEdit()->text() );
261
262 if( ui.CB_CDROM_Devices->lineEdit()->text().isEmpty() )
263 {
264 ui.Label_Info->setText( tr("Image Size: ") + QString::number(0) + tr("Mb") );
265 return;
266 }
267
268 if( ! QFile::exists(ui.CB_CDROM_Devices->lineEdit()->text()) )
269 {
270 ui.Label_Info->setText( tr("Image Size: ") + QString::number(0) + tr("Mb") );
271 return;
272 }
273
274 QFileInfo cd_img = QFileInfo( cd->Get_File_Name() );
275 qint64 size_in_bytes = cd_img.size();
276
277 if( size_in_bytes <= 0 )
278 ui.Label_Info->setText( tr("Image Size: ") + QString::number(0) + tr("Mb") );
279 else
280 ui.Label_Info->setText( tr("Image Size: ") +
281 QString::number((float)size_in_bytes / 1024.0 / 1024.0, 'f', 2) + tr("Mb") );
282
283 delete cd;
284 }
285 else if( ui.GB_HDD->isEnabled() )
286 {
287 HDD_Info->Update_Disk_Info( ui.Edit_HDD_Image_Path->text() );
288 }
289 else
290 {
291 AQError( "void Properties_Window::on_Button_Update_Info_clicked()",
292 "No Enabled GB!" );
293 }
294 }
295
on_TB_FD_Image_Browse_clicked()296 void Properties_Window::on_TB_FD_Image_Browse_clicked()
297 {
298 QString fileName = QFileDialog::getOpenFileName( this, tr("Open Floppy Image File"),
299 Get_Last_Dir_Path(ui.CB_FD_Devices->lineEdit()->text()),
300 tr("All Files (*);;Images Files (*.img *.ima)") );
301
302 if( ! fileName.isEmpty() )
303 {
304 fileName = QDir::toNativeSeparators( fileName );
305
306 ui.CB_FD_Devices->lineEdit()->setText( fileName );
307 Add_To_Recent_FDD_Files( fileName );
308
309 // Add this path to ComboBox items?
310 for( int ix = 0; ix < ui.CB_FD_Devices->count(); ++ix )
311 {
312 if( ui.CB_FD_Devices->itemText(ix) == fileName )
313 return;
314 }
315
316 ui.CB_FD_Devices->addItem( fileName );
317 }
318 }
319
on_TB_CDROM_Image_Browse_clicked()320 void Properties_Window::on_TB_CDROM_Image_Browse_clicked()
321 {
322 QString fileName = QFileDialog::getOpenFileName( this, tr("Open CD\\DVD-ROM Image File"),
323 Get_Last_Dir_Path(ui.CB_CDROM_Devices->lineEdit()->text()),
324 tr("All Files (*);;Images Files (*.iso)") );
325
326 if( ! fileName.isEmpty() )
327 {
328 fileName = QDir::toNativeSeparators( fileName );
329
330 ui.CB_CDROM_Devices->lineEdit()->setText( fileName );
331 Add_To_Recent_CD_Files( fileName );
332
333 // Add this path to ComboBox items?
334 for( int ix = 0; ix < ui.CB_CDROM_Devices->count(); ++ix )
335 {
336 if( ui.CB_CDROM_Devices->itemText(ix) == fileName )
337 return;
338 }
339
340 ui.CB_CDROM_Devices->addItem( fileName );
341 }
342 }
343
on_TB_HDD_Image_Browse_clicked()344 void Properties_Window::on_TB_HDD_Image_Browse_clicked()
345 {
346 QString fileName = QFileDialog::getOpenFileName( this, tr("Open HDD Image File"),
347 Get_Last_Dir_Path(ui.Edit_HDD_Image_Path->text()),
348 tr("All Files (*);;Images Files (*.img *.qcow *.wmdk)") );
349
350 if( ! fileName.isEmpty() )
351 ui.Edit_HDD_Image_Path->setText( QDir::toNativeSeparators(fileName) );
352 }
353
on_Button_HDD_New_clicked()354 void Properties_Window::on_Button_HDD_New_clicked()
355 {
356 Create_HDD_Image_Window *Create_HDD_Win = new Create_HDD_Image_Window( this );
357
358 if( Create_HDD_Win->exec() == QDialog::Accepted )
359 {
360 ui.Edit_HDD_Image_Path->setText( Create_HDD_Win->Get_Image_File_Name() );
361 on_Button_Update_Info_clicked();
362 }
363
364 delete Create_HDD_Win;
365 }
366
on_Button_HDD_Format_clicked()367 void Properties_Window::on_Button_HDD_Format_clicked()
368 {
369 Create_HDD_Image_Window *Create_HDD_Win = new Create_HDD_Image_Window( this );
370 Create_HDD_Win->Set_Image_File_Name( ui.Edit_HDD_Image_Path->text() );
371
372 if( Create_HDD_Win->exec() == QDialog::Accepted )
373 on_Button_Update_Info_clicked();
374
375 delete Create_HDD_Win;
376 }
377
Update_HDD(bool ok)378 void Properties_Window::Update_HDD( bool ok )
379 {
380 PW_HDD.Set_Disk_Info( HDD_Info->Get_Disk_Info() );
381
382 QString suf_v = Get_TR_Size_Suffix( PW_HDD.Get_Virtual_Size() );
383 QString suf_d = Get_TR_Size_Suffix( PW_HDD.Get_Disk_Size() );
384
385 ui.Label_Info->setText( tr("Format: ") + PW_HDD.Get_Image_Format() +
386 tr(" Virtual Size: ") + QString::number(PW_HDD.Get_Virtual_Size().Size) + Get_TR_Size_Suffix(PW_HDD.Get_Virtual_Size()) +
387 tr("\nOn Disk Size: ") + QString::number(PW_HDD.Get_Disk_Size().Size) + Get_TR_Size_Suffix(PW_HDD.Get_Disk_Size()) +
388 tr(" Cluster Size: ") + QString::number(PW_HDD.Get_Cluster_Size()) );
389 }
390
on_TB_FD_Advanced_Settings_clicked()391 void Properties_Window::on_TB_FD_Advanced_Settings_clicked()
392 {
393 // Set device
394 Add_New_Device_Window *win = new Add_New_Device_Window();
395 win->Set_Device( PW_Storage.Get_Nativ_Device() );
396
397 // Set emulator
398 if( ! Current_Machine_Devices )
399 {
400 AQError( "void Properties_Window::on_TB_FD_Advanced_Settings_clicked()",
401 "Current_Machine_Devices == NULL" );
402 return;
403 }
404
405 win->Set_Emulator_Devices( *Current_Machine_Devices );
406
407 // Show dialog
408 if( win->exec() == QDialog::Accepted )
409 {
410 // Set new values
411 PW_Storage.Set_Nativ_Device( win->Get_Device() );
412
413 if( PW_Storage.Get_Nativ_Device().Get_File_Path() != ui.CB_FD_Devices->currentText() )
414 ui.CB_FD_Devices->setEditText( PW_Storage.Get_Nativ_Device().Get_File_Path() );
415
416 // Nativ Mode - on, File - not used
417 if( PW_Storage.Get_Nativ_Device().Get_Nativ_Mode() &&
418 PW_Storage.Get_Nativ_Device().Use_File_Path() == false )
419 ui.CB_FD_Devices->setEditText( "" );
420 }
421
422 delete win;
423 }
424
on_CB_FD_Devices_editTextChanged(const QString & text)425 void Properties_Window::on_CB_FD_Devices_editTextChanged( const QString &text )
426 {
427 PW_Storage.Set_File_Name( text );
428
429 VM_Nativ_Storage_Device tmpDev = PW_Storage.Get_Nativ_Device();
430
431 // Update nativ device file path
432 if( text.isEmpty() )
433 {
434 tmpDev.Use_File_Path( false );
435 }
436 else
437 {
438 tmpDev.Set_File_Path( text );
439
440 if( PW_Storage.Get_Nativ_Device().Get_Nativ_Mode() )
441 tmpDev.Use_File_Path( true );
442 }
443
444 PW_Storage.Set_Nativ_Device( tmpDev );
445
446 // Update info
447 on_Button_Update_Info_clicked();
448 }
449
on_TB_CDROM_Advanced_Settings_clicked()450 void Properties_Window::on_TB_CDROM_Advanced_Settings_clicked()
451 {
452 // Set device
453 Add_New_Device_Window *win = new Add_New_Device_Window();
454 win->Set_Device( PW_Storage.Get_Nativ_Device() );
455
456 // Set emulator
457 if( ! Current_Machine_Devices )
458 {
459 AQError( "void Properties_Window::on_TB_CDROM_Advanced_Settings_clicked()",
460 "Current_Machine_Devices == NULL" );
461 return;
462 }
463
464 win->Set_Emulator_Devices( *Current_Machine_Devices );
465
466 // Show dialog
467 if( win->exec() == QDialog::Accepted )
468 {
469 // Set new values
470 PW_Storage.Set_Nativ_Device( win->Get_Device() );
471
472 if( PW_Storage.Get_Nativ_Device().Get_File_Path() != ui.CB_CDROM_Devices->currentText() )
473 ui.CB_CDROM_Devices->setEditText( PW_Storage.Get_Nativ_Device().Get_File_Path() );
474
475 // Nativ Mode - on, File - not used
476 if( PW_Storage.Get_Nativ_Device().Get_Nativ_Mode() &&
477 PW_Storage.Get_Nativ_Device().Use_File_Path() == false )
478 ui.CB_CDROM_Devices->setEditText( "" );
479 }
480
481 delete win;
482 }
483
on_CB_CDROM_Devices_editTextChanged(const QString & text)484 void Properties_Window::on_CB_CDROM_Devices_editTextChanged( const QString &text )
485 {
486 PW_Storage.Set_File_Name( text );
487
488 VM_Nativ_Storage_Device tmpDev = PW_Storage.Get_Nativ_Device();
489
490 // Update nativ device file path
491 if( text.isEmpty() )
492 {
493 tmpDev.Use_File_Path( false );
494 }
495 else
496 {
497 tmpDev.Set_File_Path( text );
498
499 if( PW_Storage.Get_Nativ_Device().Get_Nativ_Mode() )
500 tmpDev.Use_File_Path( true );
501 }
502
503 PW_Storage.Set_Nativ_Device( tmpDev );
504
505 // Update info
506 on_Button_Update_Info_clicked();
507 }
508
on_TB_HDD_Advanced_Settings_clicked()509 void Properties_Window::on_TB_HDD_Advanced_Settings_clicked()
510 {
511 // Set device
512 Add_New_Device_Window *win = new Add_New_Device_Window();
513 win->Set_Device( PW_HDD.Get_Nativ_Device() );
514
515 // Set emulator
516 if( ! Current_Machine_Devices )
517 {
518 AQError( "void Properties_Window::on_TB_HDD_Advanced_Settings_clicked()",
519 "Current_Machine_Devices == NULL" );
520 return;
521 }
522
523 win->Set_Emulator_Devices( *Current_Machine_Devices );
524
525 // Show dialog
526 if( win->exec() == QDialog::Accepted )
527 {
528 // Set new values
529 PW_HDD.Set_Nativ_Device( win->Get_Device() );
530
531 if( PW_HDD.Get_Nativ_Device().Get_File_Path() != ui.Edit_HDD_Image_Path->text() )
532 ui.Edit_HDD_Image_Path->setText( PW_HDD.Get_Nativ_Device().Get_File_Path() );
533
534 // Nativ Mode - on, File - not used
535 if( PW_HDD.Get_Nativ_Device().Get_Nativ_Mode() &&
536 PW_HDD.Get_Nativ_Device().Use_File_Path() == false )
537 ui.Edit_HDD_Image_Path->setText( "" );
538 }
539
540 delete win;
541 }
542
on_Edit_HDD_Image_Path_textChanged()543 void Properties_Window::on_Edit_HDD_Image_Path_textChanged()
544 {
545 PW_HDD.Set_File_Name( ui.Edit_HDD_Image_Path->text() );
546
547 VM_Nativ_Storage_Device tmpDev = PW_HDD.Get_Nativ_Device();
548
549 // Update nativ device file path
550 if( ui.Edit_HDD_Image_Path->text().isEmpty() )
551 {
552 tmpDev.Use_File_Path( false );
553 }
554 else
555 {
556 tmpDev.Set_File_Path( ui.Edit_HDD_Image_Path->text() );
557
558 if( PW_HDD.Get_Nativ_Device().Get_Nativ_Mode() )
559 tmpDev.Use_File_Path( true );
560 }
561
562 PW_HDD.Set_Nativ_Device( tmpDev );
563
564 // Update info
565 //on_Button_Update_Info_clicked();
566 HDD_Info->Update_Disk_Info( ui.Edit_HDD_Image_Path->text() );
567 }
568