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 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
|
/****************************************************************************
**
** Copyright (C) 2008-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 <QDir>
#include <QFile>
#include <QFileInfo>
#include <QFileInfoList>
#include <QDateTime>
#include <QMessageBox>
#include <QApplication>
#include <QSettings>
#include <QRegExp>
#include <QProcess>
#include <QStringList>
#include <QTextStream>
#ifdef Q_OS_WIN32
#include <iostream>
#include <windows.h>
HANDLE Console_HANDLE = GetStdHandle( STD_OUTPUT_HANDLE );
#else
#include <QDebug>
#endif
#include "Utils.h"
#include "System_Info.h"
static uint Messages_Index = 0;
static bool Save_Messages_To_Log = true;
static QString Log_Path = "";
static bool Use_Stdout;
static bool Stdout_Debug;
static bool Stdout_Warning;
static bool Stdout_Error;
static QStringList Recent_CD_Images;
static QStringList Recent_FDD_Images;
void AQDebug( const QString &sender, const QString &mes )
{
if( Use_Stdout && Stdout_Debug )
{
#ifdef Q_OS_WIN32
SetConsoleTextAttribute( Console_HANDLE, 10 );
std::cout << QString( "\nAQEMU Debug [%1] >>>\nSender: %2\nMessage: %3" )
.arg(Messages_Index).arg(sender).arg(mes).toStdString();
#else
qDebug( qPrintable(QString(
"\n\33[32mAQEMU Debug\33[0m [%1] >>>\n\33[32mSender:\33[0m %2\n\33[32mMessage:\33[0m %3")
.arg(Messages_Index).arg(sender).arg(mes)) );
#endif
}
if( Save_Messages_To_Log && Stdout_Debug )
AQSave_To_Log( "Debug", sender, mes );
Messages_Index++;
}
void AQWarning( const QString &sender, const QString &mes )
{
if( Use_Stdout && Stdout_Warning )
{
#ifdef Q_OS_WIN32
SetConsoleTextAttribute( Console_HANDLE, 14 );
std::cout << QString( "\nAQEMU Warning [%1] >>>\nSender: %2\nMessage: %3" )
.arg(Messages_Index).arg(sender).arg(mes).toStdString();
#else
qDebug( qPrintable(QString(
"\n\33[34mAQEMU Warning\33[0m [%1] >>>\n\33[34mSender:\33[0m %2\n\33[34mMessage:\33[0m %3")
.arg(Messages_Index).arg(sender).arg(mes)) );
#endif
}
if( Save_Messages_To_Log && Stdout_Warning )
AQSave_To_Log( "Warning", sender, mes );
Messages_Index++;
}
void AQError( const QString &sender, const QString &mes )
{
if( Use_Stdout && Stdout_Error )
{
#ifdef Q_OS_WIN32
SetConsoleTextAttribute( Console_HANDLE, 12 );
std::cout << QString( "\nAQEMU Error [%1] >>>\nSender: %2\nMessage: %3" )
.arg(Messages_Index).arg(sender).arg(mes).toStdString();
#else
qDebug( qPrintable(QString(
"\n\33[31mAQEMU Error\33[0m [%1] >>>\n\33[31mSender:\33[0m %2\n\33[31mMessage:\33[0m %3")
.arg(Messages_Index).arg(sender).arg(mes)) );
#endif
}
if( Save_Messages_To_Log && Stdout_Error )
AQSave_To_Log( "Error", sender, mes );
Messages_Index++;
}
void AQGraphic_Warning( const QString &caption, const QString &mes )
{
QMessageBox::warning( NULL, caption, mes, QMessageBox::Ok );
}
void AQGraphic_Warning( const QString &sender, const QString &caption, const QString &mes, bool fatal )
{
if( fatal )
{
QMessageBox::warning( NULL, caption,
QString("Sender: %1\nMessage: %2\n").arg(sender).arg(mes) +
QObject::tr("This Fatal Error. Recomend Close AQEMU."),
QMessageBox::Ok );
}
else
{
QMessageBox::warning( NULL, caption,
QString("Sender: %1\nMessage: %2").arg(sender).arg(mes),
QMessageBox::Ok );
}
if( Save_Messages_To_Log )
AQSave_To_Log( "Warning", sender, mes );
}
void AQGraphic_Error( const QString &sender, const QString &caption, const QString &mes, bool fatal )
{
if( fatal )
{
QMessageBox::critical( NULL, caption,
QString("Sender: %1\nMessage: %2\n").arg(sender).arg(mes) +
QObject::tr("Fatal error. It's recommended to close AQEMU"),
QMessageBox::Ok );
}
else
{
QMessageBox::critical( NULL, caption,
QString("Sender: %1\nMessage: %2").arg(sender).arg(mes),
QMessageBox::Ok );
}
if( Save_Messages_To_Log )
AQSave_To_Log( "Error", sender, mes );
}
void AQUse_Log( bool use )
{
Save_Messages_To_Log = use;
}
void AQUse_Debug_Output( bool use, bool d, bool w, bool e )
{
Use_Stdout = use;
Stdout_Debug = d;
Stdout_Warning = w;
Stdout_Error = e;
}
void AQLog_Path( const QString& path )
{
Log_Path = path;
}
void AQSave_To_Log( const QString &mes_type, const QString &sender, const QString &mes )
{
if( Log_Path.isEmpty() ) return;
QFile log_file( Log_Path );
if( ! log_file.open(QIODevice::Append | QIODevice::Text) )
{
AQUse_Log( false ); // off loging
AQError( "void AQSave_To_Log( const QString& mes_type, const QString& sender, const QString& mes )",
"Cannot Open Log file to Write! Log Path: \"" + Log_Path + "\"" );
}
else
{
QTextStream out( &log_file );
out << "Type: " << mes_type << " Num: " << Messages_Index << "\nDate: "
<< QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss zzz")
<< "\nSender: " << sender << "\nMessage: " << mes << "\n\n";
}
}
bool Create_New_HDD_Image( bool encrypted, const QString &base_image,
const QString &file_name, const QString &format, VM::Device_Size size, bool verbose )
{
// Create command line
QStringList args;
args << "create";
if( encrypted )
args << "-e";
if( ! base_image.isEmpty() )
args << "-b" << base_image;
args << "-f" << format;
args << file_name;
switch( size.Suffix )
{
case VM::Size_Suf_Mb: // MB
args << QString::number( (long)(size.Size * 1024) );
break;
case VM::Size_Suf_Gb: // GB
args << QString::number( (long)(size.Size * 1024) ) + "M";
break;
default: // KG
args << QString::number( (long)size.Size );
break;
}
// Start qemu-img process
QProcess qemu_img;
QSettings settings;
qemu_img.start( settings.value("QEMU-IMG_Path", "qemu-img").toString(), args );
if( ! qemu_img.waitForStarted(2000) )
{
AQGraphic_Error( "bool Create_New_HDD_Image( bool encrypted, const QString &base_image,"
"const QString &file_name, const QString &format, VM::Device_Size size, bool verbose )",
QObject::tr("Error!"), QObject::tr("Cannot Start qemu-img! Image isn't Created!") );
return false;
}
if( ! qemu_img.waitForFinished(10000) )
{
AQGraphic_Error( "bool Create_New_HDD_Image( bool encrypted, const QString &base_image,"
"const QString &file_name, const QString &format, VM::Device_Size size, bool verbose )",
QObject::tr("Error!"), QObject::tr("qemu-img Cannot Finish! Image isn't Created!") );
return false;
}
else
{
QByteArray err = qemu_img.readAllStandardError();
QByteArray out = qemu_img.readAllStandardOutput();
if( err.count() > 0 )
{
AQGraphic_Error( "bool Create_New_HDD_Image( bool encrypted, const QString &base_image,"
"const QString &file_name, const QString &format, VM::Device_Size size, bool verbose )",
QObject::tr("Error!"), QObject::tr("Cannot Create Image!\nInformation: ") + err );
}
QRegExp rx( "Format*ing*fmt*size*" );
rx.setPatternSyntax( QRegExp::Wildcard );
if( verbose )
{
if( rx.exactMatch( out ) )
{
QMessageBox::information( NULL, QObject::tr("Complete!"),
QObject::tr("QEMU-IMG is Creates HDD Image.\nAdditional Information:\n") + out );
}
else
{
QMessageBox::information( NULL, QObject::tr("Warning!"),
QObject::tr("QEMU-IMG is Returned non Standard Message!.\nAdditional Information:\n") + out );
}
}
return true;
}
}
bool Create_New_HDD_Image( const QString &file_name, VM::Device_Size size )
{
QSettings settings;
QString format = settings.value( "Default_HDD_Image_Format", "qcow2" ).toString();
return Create_New_HDD_Image( false, "", file_name, format, size, false );
}
bool Format_HDD_Image( const QString &file_name, VM::Disk_Info info )
{
if( file_name.isEmpty() )
{
AQError( "bool Format_HDD_Image( const QString &file_name )",
"File Name is Empty!" );
return false;
}
VM_HDD tmp_hd = VM_HDD( true, file_name );
tmp_hd.Set_Disk_Info( info );
QString hd_format = tmp_hd.Get_Image_Format();
if( hd_format.isEmpty() )
{
AQError( "bool Format_HDD_Image( const QString &file_name )",
"Format is Empty!" );
return false;
}
return Create_New_HDD_Image( false, "", file_name, hd_format, tmp_hd.Get_Virtual_Size(), false );
}
QList<QString> Get_Templates_List()
{
QList<QString> all_templates;
QSettings settings;
// VM Templates
QDir sys_templates_dir( QDir::toNativeSeparators(settings.value("AQEMU_Data_Folder", "").toString() + "/os_templates/") );
QFileInfoList sys_templates_list = sys_templates_dir.entryInfoList(
QStringList("*.aqvmt"), QDir::Files, QDir::Unsorted );
QDir user_templates_dir( QDir::toNativeSeparators(settings.value("VM_Directory", "").toString() + "/os_templates/") );
QFileInfoList user_templates_list = user_templates_dir.entryInfoList(
QStringList("*.aqvmt"), QDir::Files, QDir::Unsorted );
for( int tx = 0; tx < sys_templates_list.count(); ++tx )
{
for( int ux = 0; ux < user_templates_list.count(); ++ux )
{
if( sys_templates_list[tx].completeBaseName() ==
user_templates_list[ux].completeBaseName() )
{
sys_templates_list.takeAt( tx ); // delete system template
tx -= 1;
ux = user_templates_list.count();
}
}
}
// OK. In Template Lists Only Unique Values
for( int ix = 0; ix < sys_templates_list.count(); ++ix )
all_templates.append( sys_templates_list[ix].absoluteFilePath() );
for( int ix = 0; ix < user_templates_list.count(); ++ix )
all_templates.append( user_templates_list[ix].absoluteFilePath() );
// sort
qSort( all_templates );
return all_templates;
}
QString Get_FS_Compatible_VM_Name( const QString &name )
{
//QRegExp vm_name_val = QRegExp( "[^a-zA-Z0-9_]" ); // old style
QRegExp vm_name_val = QRegExp( "[^\\w\\.]" );
QString tmp = name;
tmp = tmp.replace( vm_name_val, "_" );
return tmp.replace( "__", "_" );
}
QString Get_Complete_VM_File_Path( const QString &vm_name )
{
//QRegExp vm_name_val = QRegExp( "[^a-zA-Z0-9_]" ); // old style
QRegExp vm_name_val = QRegExp( "[^\\w]" );
QString tmp = vm_name;
tmp = tmp.replace( vm_name_val, "_" );
QString new_name = tmp.replace( "__", "_" );
QString tmp_str = new_name;
QSettings settings;
QString ret_str = settings.value("VM_Directory", "").toString() + tmp_str;
if( ! ret_str.endsWith(".aqemu") )
ret_str += ".aqemu";
for( int ix = 0; QFile::exists(ret_str); ++ix )
tmp_str = new_name + QString::number( ix );
return ret_str;
}
QString Get_TR_Size_Suffix( VM::Device_Size suf )
{
switch( suf.Suffix )
{
case VM::Size_Suf_Kb:
return QObject::tr("Kb");
case VM::Size_Suf_Mb:
return QObject::tr("Mb");
case VM::Size_Suf_Gb:
return QObject::tr("Gb");
default:
AQError( "QString Get_TR_Size_Suffix( VM::Device_Size suf )",
"Virtual Size Suffix Default Section!" );
return "";
}
}
QString Get_Last_Dir_Path( const QString &path )
{
QFileInfo info( path );
QString dir = info.path();
if( dir.isEmpty() ) return "/";
else return dir;
}
bool It_Host_Device( const QString &path )
{
#ifdef Q_OS_WIN32
// FIXME
return false;
#else
if( path.startsWith("/dev/") ) return true;
else return false;
#endif
}
void Check_AQEMU_Permissions()
{
QSettings settings;
QFileInfo test_perm;
#ifndef Q_OS_WIN32
// This Section For Unix Like OS.
test_perm = QFileInfo( settings.fileName() );
if( test_perm.exists() )
{
if( ! test_perm.isWritable() )
{
AQGraphic_Error( "void Check_AQEMU_Permissions()", QObject::tr("Error!"),
QObject::tr("AQEMU Config File is Read Only!\nCheck Permissions For File: ") +
settings.fileName(), true );
}
}
#endif
// Check VM Dir Permissions
test_perm = QFileInfo( settings.value("VM_Directory", "~").toString() );
if( test_perm.exists() )
{
if( ! test_perm.isWritable() )
{
AQGraphic_Error( "void Check_AQEMU_Permissions()", QObject::tr("Error!"),
QObject::tr("AQEMU VM Directory is Read Only!\nCheck Permissions For: ") +
settings.value("VM_Directory", "~").toString(), true );
}
}
// Check VM Templates Dir Permissions
test_perm = QFileInfo( settings.value("VM_Directory", "~").toString() + "os_templates" );
if( test_perm.exists() )
{
if( ! test_perm.isWritable() )
{
AQGraphic_Error( "void Check_AQEMU_Permissions()", QObject::tr("Error!"),
QObject::tr("AQEMU VM Template Directory is Read Only!\nCheck Permissions For: ") +
settings.value("VM_Directory", "~").toString() + "os_templates", true );
}
}
// Check AQEMU Log File Permissions
if( ! settings.value("Log/Log_Path", "").toString().isEmpty() )
{
test_perm = QFileInfo( settings.value("Log/Log_Path", "").toString() );
if( test_perm.exists() )
{
if( ! test_perm.isWritable() )
{
AQGraphic_Error( "void Check_AQEMU_Permissions()", QObject::tr("Error!"),
QObject::tr("AQEMU Log File is Read Only!\nCheck Permissions For File: ") +
settings.value("Log/Log_Path", "").toString(), false );
}
}
}
}
VM::Emulator_Version String_To_Emulator_Version( const QString &str )
{
if( str == "QEMU 0.9.0" ) return VM::QEMU_0_9_0;
else if( str == "QEMU 0.9.1" ) return VM::QEMU_0_9_1;
else if( str == "QEMU 0.10.X" ) return VM::QEMU_0_10;
else if( str == "QEMU 0.11.X" ) return VM::QEMU_0_11;
else if( str == "QEMU 0.12.X" ) return VM::QEMU_0_12;
else if( str == "QEMU 0.13.X" ) return VM::QEMU_0_13;
else if( str == "QEMU 0.14.X" ) return VM::QEMU_0_14;
else if( str == "KVM 7X" ) return VM::KVM_7X;
else if( str == "KVM 8X" ) return VM::KVM_8X;
else if( str == "KVM 0.11.X" ) return VM::KVM_0_11;
else if( str == "KVM 0.12.X" ) return VM::KVM_0_12;
else if( str == "KVM 0.13.X" ) return VM::KVM_0_13;
else if( str == "KVM 0.14.X" ) return VM::KVM_0_14;
else if( str == "Obsolete" ) return VM::Obsolete;
else
{
AQError( "VM::Emulator_Version String_To_Emulator_Version( const QString &str )",
QString("Emulator version \"%1\" not valid!").arg(str) );
return VM::Obsolete;
}
}
QString Emulator_Version_To_String( VM::Emulator_Version ver )
{
switch( ver )
{
case VM::QEMU_0_9_0:
return "QEMU 0.9.0";
case VM::QEMU_0_9_1:
return "QEMU 0.9.1";
case VM::QEMU_0_10:
return "QEMU 0.10.X";
case VM::QEMU_0_11:
return "QEMU 0.11.X";
case VM::QEMU_0_12:
return "QEMU 0.12.X";
case VM::QEMU_0_13:
return "QEMU 0.13.X";
case VM::QEMU_0_14:
return "QEMU 0.14.X";
case VM::KVM_7X:
return "KVM 7X";
case VM::KVM_8X:
return "KVM 8X";
case VM::KVM_0_11:
return "KVM 0.11.X";
case VM::KVM_0_12:
return "KVM 0.12.X";
case VM::KVM_0_13:
return "KVM 0.13.X";
case VM::KVM_0_14:
return "KVM 0.14.X";
case VM::Obsolete:
return "Obsolete";
default:
AQError( "QString Emulator_Version_To_String( VM::Emulator_Version ver )",
QString("Emulator version \"%1\" not valid!").arg((int)ver) );
return "";
}
}
static QList<Emulator> Emulators_List;
static QList<Emulator> Empty_Emul_List;
static Emulator Empty_Emul;
bool Update_Emulators_List()
{
// Clear old emulator list
Emulators_List.clear();
// Get dir path
QSettings settings;
QFileInfo info( settings.fileName() );
QString aqemuSettingsFolder = info.absoluteDir().absolutePath();
if( ! (aqemuSettingsFolder.endsWith("/") || aqemuSettingsFolder.endsWith("\\")) )
aqemuSettingsFolder = QDir::toNativeSeparators( aqemuSettingsFolder + "/" );
if( ! QFile::exists(aqemuSettingsFolder) )
{
AQError( "bool Update_Emulators_List()",
QString("Cannot get path for save emulator! Folder \"%1\" not exists!").arg(aqemuSettingsFolder) );
return false;
}
// Get all *.emulators files
QDir emulDir( aqemuSettingsFolder );
QStringList emulFiles = emulDir.entryList( QStringList("*.emulator"), QDir::Files, QDir::Name );
if( emulFiles.isEmpty() )
{
AQWarning( "bool Update_Emulators_List()",
QString("No emulators found in \"%1\"").arg(aqemuSettingsFolder) );
return false;
}
// Check default emulators
bool qDef = false, kDef = false;
bool qInst = false, kInst = false; // Installed?
// Load emulators
for( int ex = 0; ex < emulFiles.count(); ++ex )
{
Emulator tmp;
if( ! tmp.Load(aqemuSettingsFolder + emulFiles[ex]) )
{
AQError( "bool Update_Emulators_List()",
QString("Cannot load emulator from file: \"%1\"").arg(emulFiles[ex]) );
continue;
}
// Installed?
if( tmp.Get_Type() == VM::QEMU ) qInst = true;
else if( tmp.Get_Type() == VM::KVM ) kInst = true;
// Default?
if( tmp.Get_Default() )
{
if( tmp.Get_Type() == VM::QEMU )
{
if( qDef )
{
AQWarning( "bool Update_Emulators_List()",
"Default QEMU emulator already loaded." );
tmp.Set_Default( false );
}
else qDef = true;
}
else if( tmp.Get_Type() == VM::KVM )
{
if( kDef )
{
AQWarning( "bool Update_Emulators_List()",
"Default KVM emulator already loaded." );
tmp.Set_Default( false );
}
else kDef = true;
}
}
// Update available options?
if( tmp.Get_Check_Available_Options() )
{
QMap<QString, QString> tmpBinFiles = tmp.Get_Binary_Files();
QMap<QString, Available_Devices> tmpDevMap; // result
for( QMap<QString, QString>::const_iterator it = tmpBinFiles.constBegin(); it != tmpBinFiles.constEnd(); ++it )
{
if( ! it.value().isEmpty() )
{
bool ok = false;
Available_Devices tmpDev = System_Info::Get_Emulator_Info( it.value(), &ok, tmp.Get_Version(), it.key() );
if( ! ok )
{
AQError( "bool Update_Emulators_List()",
"Cannot set new emulator available options!" );
continue;
}
// Adding device
tmpDevMap[ it.key() ] = tmpDev;
}
}
// Set all devices
tmp.Set_Devices( tmpDevMap );
}
// Check version?
if( tmp.Get_Check_Version() )
{
// Get bin file path
QString binFilePath = "";
QMap<QString, QString> tmpBinFiles = tmp.Get_Binary_Files();
for( QMap<QString, QString>::const_iterator it = tmpBinFiles.constBegin(); it != tmpBinFiles.constEnd(); ++it )
{
if( QFile::exists(it.value()) )
{
binFilePath = it.value();
break;
}
}
if( binFilePath.isEmpty() )
{
AQError( "bool Update_Emulators_List()",
QString("Cannot find exists emulator binary file for emulator \"%1\"!").arg(tmp.Get_Name()) );
}
// All OK, check version
tmp.Set_Version( System_Info::Get_Emulator_Version(binFilePath) );
}
// Adding emulator
Emulators_List << tmp;
}
// Emulator loaded?
if( Emulators_List.isEmpty() )
{
AQWarning( "bool Update_Emulators_List()",
"No emulators loaded!" );
return false;
}
// Check defaults
if( qInst == true && qDef == false )
{
for( int ex = 0; ex < Emulators_List.count(); ++ex )
{
if( Emulators_List[ex].Get_Type() == VM::QEMU )
{
Emulators_List[ex].Set_Default( true );
break;
}
}
}
if( kInst == true && kDef == false )
{
for( int ex = 0; ex < Emulators_List.count(); ++ex )
{
if( Emulators_List[ex].Get_Type() == VM::KVM )
{
Emulators_List[ex].Set_Default( true );
break;
}
}
}
// All OK
return true;
}
const QList<Emulator> &Get_Emulators_List()
{
if( Update_Emulators_List() ) return Emulators_List; // FIXME Update
else
{
AQError( "QList<Emulator> Get_Emulators_List()",
"Cannot Update Emulators List" );
return Empty_Emul_List;
}
}
bool Remove_All_Emulators_Files()
{
// Get emulators dir path
QSettings settings;
QFileInfo info( settings.fileName() );
QString aqemuSettingsFolder = info.absoluteDir().absolutePath();
aqemuSettingsFolder += aqemuSettingsFolder.endsWith( QDir::toNativeSeparators("/") )
? ""
: QDir::toNativeSeparators( "/" );
if( ! QFile::exists(aqemuSettingsFolder) )
{
AQError( "bool Remove_All_Emulators_Files()",
QString("Cannot get path for save emulator! Folder \"%1\" not exists!").arg(aqemuSettingsFolder) );
return false;
}
else
{
// Get all *.emulators files
QDir emulDir( aqemuSettingsFolder );
QStringList emulFiles = emulDir.entryList( QStringList("*.emulator"), QDir::Files, QDir::Name );
bool allFilesRemoved = true;
for( int dx = 0; dx < emulFiles.count(); ++dx )
{
if( ! QFile::remove(aqemuSettingsFolder + emulFiles[dx]) )
{
AQError( "bool Remove_All_Emulators_Files()",
QString("Cannot delete file \"%1\"!").arg(emulFiles[dx]) );
allFilesRemoved = false;
}
}
return allFilesRemoved;
}
}
const Emulator &Get_Default_Emulator( VM::Emulator_Type type )
{
if( Emulators_List.count() <= 0 )
AQError( "const Emulator &Get_Default_Emulator( VM::Emulator_Type type )",
"Emulator Count == 0" );
else
{
for( int ix = 0; ix < Emulators_List.count(); ix++ )
{
if( Emulators_List[ix].Get_Type() == type && Emulators_List[ix].Get_Default() )
return Emulators_List[ ix ];
}
}
AQWarning( "const Emulator &Get_Default_Emulator( VM::Emulator_Type type )",
"Cannot Find!" );
return Empty_Emul;
}
const Emulator &Get_Emulator_By_Name( const QString &name )
{
if( Emulators_List.count() <= 0 )
AQError( "const Emulator Get_Emulator_By_Name( const QString &name )",
"Emulator Count == 0" );
else
{
for( int ix = 0; ix < Emulators_List.count(); ix++ )
{
if( Emulators_List[ix].Get_Name() == name )
return Emulators_List[ ix ];
}
}
AQWarning( "const Emulator Get_Emulator_By_Name( const QString &name )",
"Cannot Find!" );
return Empty_Emul;
}
int Get_Random( int min, int max )
{
if( min < 0 || max > RAND_MAX || min > max )
{
return -1;
}
qsrand( QTime::currentTime().msec() );
return int( qrand() / (RAND_MAX + 1.0) * (max + 1 - min) + min );
}
void Load_Recent_Images_List()
{
QSettings settings;
// CD
int max_cd = settings.value( "CD_ROM_Exits_Images/Max", "5" ).toString().toInt();
Recent_CD_Images.clear();
for( int ix = 0; ix < max_cd; ++ix )
{
QString tmp = settings.value( "CD_ROM_Exits_Images/item" + QString::number(ix), "" ).toString();
if( ! tmp.isEmpty() ) Recent_CD_Images << tmp;
}
// FDD
int max_fdd = settings.value( "Floppy_Exits_Images/Max", "5" ).toString().toInt();
Recent_FDD_Images.clear();
for( int ix = 0; ix < max_fdd; ++ix )
{
QString tmp = settings.value( "Floppy_Exits_Images/item" + QString::number(ix), "" ).toString();
if( ! tmp.isEmpty() ) Recent_FDD_Images << tmp;
}
}
const QStringList &Get_CD_Recent_Images_List()
{
return Recent_CD_Images;
}
void Add_To_Recent_CD_Files( const QString &path )
{
QSettings settings;
int max = settings.value( "CD_ROM_Exits_Images/Max", "5" ).toString().toInt();
// This Unique Path?
for( int fx = 0; fx < Recent_CD_Images.count() && fx < max; ++fx )
{
if( Recent_CD_Images[fx] == path )
{
AQDebug( "void Add_To_Recent_CD_Files( const QString &path )",
"CD-ROM Path Not Unique." );
// Up it path in list
if( fx < Recent_CD_Images.count()-1 )
{
// swap items
QString tmp = Recent_CD_Images[ fx+1 ];
Recent_CD_Images[ fx+1 ] = Recent_CD_Images[ fx ];
Recent_CD_Images[ fx ] = tmp;
}
else return;
}
}
// Add to List
if( Recent_CD_Images.count() < max )
{
Recent_CD_Images << path;
settings.setValue( "CD_ROM_Exits_Images/item" + QString::number(Recent_CD_Images.count()-1), path );
}
else
{
// Delete first element and add new to end
for( int ix = 0; ix < Recent_CD_Images.count() -1 && ix < max -1; ++ix )
{
Recent_CD_Images[ ix ] = Recent_CD_Images[ ix +1 ];
}
Recent_CD_Images[ max -1 ] = path;
// Save Items
for( int ix = 0; ix < Recent_CD_Images.count(); ix++ )
{
settings.setValue( "CD_ROM_Exits_Images/item" + QString::number(ix), Recent_CD_Images[ix] );
}
}
}
const QStringList &Get_FDD_Recent_Images_List()
{
return Recent_FDD_Images;
}
void Add_To_Recent_FDD_Files( const QString &path )
{
QSettings settings;
int max = settings.value( "Floppy_Exits_Images/Max", "5" ).toString().toInt();
// This Unique Path?
for( int fx = 0; fx < Recent_FDD_Images.count() && fx < max; ++fx )
{
if( Recent_FDD_Images[fx] == path )
{
AQDebug( "void Add_To_Recent_FDD_Files( const QString &path )",
"Floppy Path Not Unique." );
// Up it path in list
if( fx < Recent_FDD_Images.count()-1 )
{
// swap items
QString tmp = Recent_FDD_Images[ fx+1 ];
Recent_FDD_Images[ fx+1 ] = Recent_FDD_Images[ fx ];
Recent_FDD_Images[ fx ] = tmp;
}
else return;
}
}
// Add to List
if( Recent_FDD_Images.count() < max )
{
Recent_FDD_Images << path;
settings.setValue( "Floppy_Exits_Images/item" + QString::number(Recent_FDD_Images.count()-1), path );
}
else
{
// Delete first element and add new to end
for( int ix = 0; ix < Recent_FDD_Images.count() -1 && ix < max -1; ++ix )
{
Recent_FDD_Images[ ix ] = Recent_FDD_Images[ ix +1 ];
}
Recent_FDD_Images[ max -1 ] = path;
// Save Items
for( int ix = 0; ix < Recent_FDD_Images.count(); ix++ )
{
settings.setValue( "Floppy_Exits_Images/item" + QString::number(ix), Recent_FDD_Images[ix] );
}
}
}
static bool Show_Error_Window;
bool Get_Show_Error_Window()
{
return Show_Error_Window;
}
void Set_Show_Error_Window( bool show )
{
Show_Error_Window = show;
}
|