File indexing completed on 2024-05-05 04:51:42

0001 /*
0002     SPDX-FileCopyrightText: 2003-2007 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-FileCopyrightText: 2010 Michal Malek <michalm@jabster.pl>
0004     SPDX-FileCopyrightText: 1998-2010 Sebastian Trueg <trueg@k3b.org>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "k3bbootimagedialog.h"
0010 #include "k3bbootimagemodel.h"
0011 
0012 #include "k3bdatadoc.h"
0013 #include "k3bbootitem.h"
0014 #include "k3bintvalidator.h"
0015 
0016 #include <KLocalizedString>
0017 #include <KMessageBox>
0018 
0019 #include <QDebug>
0020 #include <QItemSelectionModel>
0021 #include <QString>
0022 #include <QCheckBox>
0023 #include <QFileDialog>
0024 #include <QLineEdit>
0025 #include <QPushButton>
0026 #include <QRadioButton>
0027 
0028 
0029 
0030 
0031 K3b::BootImageDialog::BootImageDialog( K3b::DataDoc* doc, QWidget* parent )
0032     : QDialog( parent ),
0033       m_doc( doc ),
0034       m_bootImageModel( new BootImageModel( m_doc, this ) )
0035 {
0036     setWindowTitle(i18n("Boot Images"));
0037     setupUi( this );
0038     
0039     m_viewImages->setModel( m_bootImageModel );
0040 
0041     connect( m_buttonNew, SIGNAL(clicked()),
0042              this, SLOT(slotNewBootImage()) );
0043     connect( m_buttonDelete, SIGNAL(clicked()),
0044              this, SLOT(slotDeleteBootImage()) );
0045     connect( m_buttonToggleOptions, SIGNAL(clicked()),
0046              this, SLOT(slotToggleOptions()) );
0047     connect( m_viewImages->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
0048              this, SLOT(slotCurrentChanged(QModelIndex,QModelIndex)) );
0049     connect( m_radioNoEmulation, SIGNAL(toggled(bool)),
0050              this, SLOT(slotNoEmulationToggled(bool)) );
0051     connect( m_radioFloppy, SIGNAL(toggled(bool)),this,SLOT(slotOptionsChanged()) );
0052     connect( m_radioHarddisk, SIGNAL(toggled(bool)),this,SLOT(slotOptionsChanged()) );
0053     connect( m_checkNoBoot, SIGNAL(toggled(bool)),this,SLOT(slotOptionsChanged()) );
0054     connect( m_checkInfoTable, SIGNAL(toggled(bool)),this,SLOT(slotOptionsChanged()) );
0055     connect( m_radioNoEmulation, SIGNAL(toggled(bool)),this,SLOT(slotOptionsChanged()) );
0056     connect( m_editLoadSegment, SIGNAL(textChanged(QString)),this,SLOT(slotOptionsChanged()) );
0057     connect( m_editLoadSize, SIGNAL(textChanged(QString)),this,SLOT(slotOptionsChanged()) );
0058     connect( m_radioFloppy, SIGNAL(toggled(bool)),this,SLOT(slotOptionsChanged()) );
0059 
0060     K3b::IntValidator* v = new K3b::IntValidator( this );
0061     m_editLoadSegment->setValidator( v );
0062     m_editLoadSize->setValidator( v );
0063 
0064     showAdvancedOptions( false );
0065     loadBootItemSettings(0);
0066 }
0067 
0068 K3b::BootImageDialog::~BootImageDialog()
0069 {
0070 }
0071 
0072 
0073 void K3b::BootImageDialog::slotToggleOptions()
0074 {
0075     showAdvancedOptions( !m_groupOptions->isVisible() );
0076 }
0077 
0078 
0079 void K3b::BootImageDialog::showAdvancedOptions( bool show )
0080 {
0081     if( show ) {
0082         m_groupOptions->show();
0083         m_buttonToggleOptions->setText( i18n("Hide Advanced Options") );
0084     }
0085     else {
0086         m_groupOptions->hide();
0087         m_buttonToggleOptions->setText( i18n("Show Advanced Options") );
0088     }
0089 }
0090 
0091 
0092 void K3b::BootImageDialog::slotNewBootImage()
0093 {
0094     QString file = QFileDialog::getOpenFileName( this, i18n("Please Choose Boot Image") );
0095     if( !file.isEmpty() ) {
0096         KIO::filesize_t fsize = QFileInfo( file ).size();
0097         BootItem::ImageType boottype = K3b::BootItem::FLOPPY;
0098         if( fsize != 1200*1024 &&
0099             fsize != 1440*1024 &&
0100             fsize != 2880*1024 ) {
0101             switch( KMessageBox::warningTwoActionsCancel( this,
0102                                                           i18n("<p>The file you selected is not a floppy image (floppy images are "
0103                                                                "of size 1200 KB, 1440 KB, or 2880 KB). You may still use boot images "
0104                                                                "of other sizes by emulating a harddisk or disabling emulation completely. "
0105                                                                "<p>If you are not familiar with terms like 'harddisk emulation' you most "
0106                                                                "likely want to use a floppy image here. Floppy images can be created by "
0107                                                                "directly extracting them from a real floppy disk:"
0108                                                                "<pre>dd if=/dev/floppy of=/tmp/floppy.img</pre>"
0109                                                                "or by using one of the many boot floppy generators that can be found on "
0110                                                                "<a href=\"https://www.google.com/search?q=linux+boot+floppy&ie=UTF-8&oe=UTF-8\">the Internet</a>."),
0111                                                           i18n("No Floppy image selected"),
0112                                                           KGuiItem( i18n("Use harddisk emulation") ),
0113                                                           KGuiItem( i18n("Use no emulation") ),
0114                                                           KStandardGuiItem::cancel(),
0115                                                           QString(),
0116                                                           KMessageBox::AllowLink ) ) {
0117             case KMessageBox::PrimaryAction:
0118                 boottype = K3b::BootItem::HARDDISK;
0119                 break;
0120             case KMessageBox::SecondaryAction:
0121                 boottype = K3b::BootItem::NONE;
0122                 break;
0123             default:
0124                 return;
0125             }
0126         }
0127 
0128         m_bootImageModel->createBootItem( file, boottype );
0129     }
0130 }
0131 
0132 
0133 void K3b::BootImageDialog::slotDeleteBootImage()
0134 {
0135     QModelIndex index = m_viewImages->currentIndex();
0136     if( index.isValid() ) {
0137         m_bootImageModel->removeRow( index.row() );
0138     }
0139 }
0140 
0141 
0142 void K3b::BootImageDialog::slotCurrentChanged( const QModelIndex& current, const QModelIndex& /*previous*/ )
0143 {
0144     loadBootItemSettings( m_bootImageModel->bootItemForIndex( current ) );
0145 }
0146 
0147 
0148 void K3b::BootImageDialog::loadBootItemSettings( K3b::BootItem* item )
0149 {
0150     // this is needed to prevent the slots to change stuff
0151     m_loadingItem = true;
0152 
0153     if( item ) {
0154         m_groupOptions->setEnabled(true);
0155         m_groupImageType->setEnabled(true);
0156 
0157         m_checkNoBoot->setChecked( item->noBoot() );
0158         m_checkInfoTable->setChecked( item->bootInfoTable() );
0159         m_editLoadSegment->setText( "0x" + QString::number( item->loadSegment(), 16 ) );
0160         m_editLoadSize->setText( "0x" + QString::number( item->loadSize(), 16 ) );
0161 
0162         if( item->imageType() == K3b::BootItem::FLOPPY )
0163             m_radioFloppy->setChecked(true);
0164         else if( item->imageType() == K3b::BootItem::HARDDISK )
0165             m_radioHarddisk->setChecked(true);
0166         else
0167             m_radioNoEmulation->setChecked(true);
0168 
0169         // force floppy size
0170         KIO::filesize_t fsize = QFileInfo( item->localPath() ).size();
0171         m_radioFloppy->setDisabled( fsize != 1200*1024 &&
0172                                     fsize != 1440*1024 &&
0173                                     fsize != 2880*1024 );
0174     }
0175     else {
0176         m_groupOptions->setEnabled(false);
0177         m_groupImageType->setEnabled(false);
0178     }
0179 
0180     m_loadingItem = false;
0181 }
0182 
0183 
0184 void K3b::BootImageDialog::slotOptionsChanged()
0185 {
0186     if( !m_loadingItem ) {
0187         QModelIndex index = m_viewImages->currentIndex();
0188         if( BootItem* item = m_bootImageModel->bootItemForIndex( index ) ) {
0189             item->setNoBoot( m_checkNoBoot->isChecked() );
0190             item->setBootInfoTable( m_checkInfoTable->isChecked() );
0191 
0192             // TODO: create some class K3b::IntEdit : public QLineEdit
0193             bool ok = true;
0194             item->setLoadSegment( K3b::IntValidator::toInt( m_editLoadSegment->text(), &ok ) );
0195             if( !ok )
0196                 qDebug() << "(K3b::BootImageDialog) parsing number failed: " << m_editLoadSegment->text().toLower();
0197             item->setLoadSize( K3b::IntValidator::toInt( m_editLoadSize->text(), &ok ) );
0198             if( !ok )
0199                 qDebug() << "(K3b::BootImageDialog) parsing number failed: " << m_editLoadSize->text().toLower();
0200 
0201             if( m_radioFloppy->isChecked() )
0202                 m_bootImageModel->setImageType( index, BootItem::FLOPPY );
0203             else if( m_radioHarddisk->isChecked() )
0204                 m_bootImageModel->setImageType( index, BootItem::HARDDISK );
0205             else
0206                 m_bootImageModel->setImageType( index, BootItem::NONE );
0207         }
0208     }
0209 }
0210 
0211 
0212 void K3b::BootImageDialog::slotNoEmulationToggled( bool on )
0213 {
0214     // it makes no sense to combine no emulation and no boot!
0215     // the base_widget takes care of the disabling
0216     if( on )
0217         m_checkNoBoot->setChecked(false);
0218 }
0219 
0220 #include "moc_k3bbootimagedialog.cpp"