File indexing completed on 2022-09-27 16:31:49

0001 /*
0002     SPDX-FileCopyrightText: 1999 Michael Kropfberger <michael.kropfberger@gmx.net>
0003     SPDX-FileCopyrightText: 2009 Dario Andres Rodriguez <andresbajotierra@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 //
0009 // 1999-11-29 Espen Sand
0010 // Converted to QLayout and QListView + cleanups
0011 //
0012 
0013 #include "mntconfig.h"
0014 
0015 #include <KMessageBox>
0016 #include <KIconLoader>
0017 #include <KConfigGroup>
0018 #include <KSharedConfig>
0019 
0020 #include <QPixmap>
0021 #include <QCloseEvent>
0022 #include <QFileDialog>
0023 #include <QTreeWidget>
0024 #include <QTreeWidgetItem>
0025 
0026 #ifndef CMAKE_UNITY_BUILD
0027 static bool GUI;
0028 #endif
0029 
0030 MntConfigWidget::MntConfigWidget(QWidget *parent, bool init)
0031         : QWidget(parent)
0032 {
0033     mInitializing = false;
0034 
0035     GUI = !init;
0036     if (GUI)
0037     {
0038         setupUi(this);
0039 
0040         //tabList fillup waits until disklist.readDF() is done...
0041         mDiskList.readFSTAB();
0042         mDiskList.readDF();
0043         mInitializing = true;
0044         connect( &mDiskList, &DiskList::readDFDone, this, &MntConfigWidget::readDFDone);
0045 
0046         connect ( m_listWidget, &QTreeWidget::itemClicked , this, &MntConfigWidget::clicked );
0047         m_listWidget->setHeaderLabels( QStringList() << QLatin1String( "" ) << i18n("Device")
0048                                        << i18n("Mount Point") << i18n("Mount Command") << i18n("Unmount Command") );
0049         m_listWidget->setColumnWidth( 0, 20 );
0050 
0051         QString text = QStringLiteral("%1: %2  %3: %4").
0052                arg(i18n("Device")).
0053                arg(i18nc("No device is selected", "None")).
0054                arg(i18n("Mount Point")).
0055                arg(i18nc("No mount point is selected", "None"));
0056 
0057         mGroupBox->setEnabled( false );
0058         mGroupBox->setTitle(text);
0059 
0060         connect( mIconLineEdit, &QLineEdit::textEdited,
0061                  this,&MntConfigWidget::iconChanged);
0062         connect( mIconLineEdit, &QLineEdit::textEdited,
0063                  this,&MntConfigWidget::slotChanged);
0064 
0065         mIconButton->setIconType(KIconLoader::Small, KIconLoader::Device);
0066         mIconButton->setFixedHeight( mIconButton->sizeHint().height() );
0067 
0068         connect( mIconButton, &KIconButton::iconChanged, this, &MntConfigWidget::iconChangedButton);
0069         connect( mIconButton, &KIconButton::iconChanged, this, &MntConfigWidget::slotChanged);
0070 
0071         connect( mDefaultIconButton, &QAbstractButton::clicked, this, &MntConfigWidget::iconDefault );
0072         connect( mDefaultIconButton, &QAbstractButton::clicked, this, &MntConfigWidget::slotChanged );
0073 
0074         connect( mMountLineEdit,&QLineEdit::textChanged,
0075                 this,&MntConfigWidget::mntCmdChanged);
0076         connect( mMountLineEdit, &QLineEdit::textChanged,
0077                 this,&MntConfigWidget::slotChanged);
0078 
0079         connect( mMountButton, &QAbstractButton::clicked, this, &MntConfigWidget::selectMntFile );
0080 
0081         connect( mUmountLineEdit, &QLineEdit::textChanged,
0082                  this,&MntConfigWidget::umntCmdChanged);
0083         connect( mUmountLineEdit, &QLineEdit::textChanged,
0084                  this,&MntConfigWidget::slotChanged);
0085 
0086         connect( mUmountButton,&QAbstractButton::clicked,this,&MntConfigWidget::selectUmntFile);
0087     }
0088 
0089     loadSettings();
0090     if(init)
0091     {
0092         applySettings();
0093     }
0094 }
0095 
0096 
0097 MntConfigWidget::~MntConfigWidget( void )
0098 {
0099     delete m_listWidget;
0100 }
0101 
0102 
0103 void MntConfigWidget::readDFDone( void )
0104 {
0105     mInitializing = false;
0106     m_listWidget->clear();
0107 
0108     QTreeWidgetItem *item = nullptr;
0109 
0110     DisksConstIterator itr = mDiskList.disksConstIteratorBegin();
0111     DisksConstIterator end = mDiskList.disksConstIteratorEnd();
0112     for (; itr != end; ++itr)
0113     {
0114         DiskEntry * disk = *itr;
0115         item = new QTreeWidgetItem( m_listWidget, QStringList() << QString() << disk->deviceName()
0116                                     << disk->mountPoint() << disk->mountCommand() << disk->umountCommand() );
0117         item->setIcon( IconCol, QIcon::fromTheme( disk->iconName() ) );
0118     }
0119 
0120     /*
0121     //Adjust dialog size (?)
0122     m_listWidget->resizeColumnToContents( 2 );
0123     m_listWidget->resize(m_listWidget->sizeHint());
0124     m_listWidget->adjustSize();
0125     */
0126 
0127     loadSettings();
0128     applySettings();
0129 }
0130 
0131 
0132 void MntConfigWidget::applySettings( void )
0133 {
0134     mDiskList.applySettings();
0135 
0136     KConfigGroup config(KSharedConfig::openConfig(), "MntConfig");
0137     if( GUI )
0138     {
0139         config.writeEntry("Width", width() );
0140         config.writeEntry("Height", height() );
0141     }
0142     config.sync();
0143 }
0144 
0145 
0146 void MntConfigWidget::loadSettings( void )
0147 {
0148     KConfigGroup config = KSharedConfig::openConfig()->group("MntConfig");
0149     if( !mInitializing && GUI )
0150     {
0151         if( isTopLevel() )
0152         {
0153             int w = config.readEntry("Width",this->width() );
0154             int h = config.readEntry("Height",this->height() );
0155             resize(w,h);
0156         }
0157 
0158         QList<QTreeWidgetItem*> list = m_listWidget->selectedItems();
0159         if( list.size() == 1 )
0160             clicked( list.at(0), 0 );
0161     }
0162 }
0163 
0164 
0165 void MntConfigWidget::clicked( QTreeWidgetItem * item , int col )
0166 {
0167     Q_UNUSED(col);
0168 
0169     QTreeWidgetItem * header = m_listWidget->headerItem();
0170 
0171     mGroupBox->setEnabled( true );
0172     mGroupBox->setTitle( QStringLiteral("%1: %2  %3: %4").
0173                          arg(header->text( DeviceCol )).
0174                          arg(item->text( DeviceCol )).
0175                          arg(header->text( MountPointCol )).
0176                          arg(item->text( MountPointCol )) );
0177 
0178 
0179     const QIcon icon = item->icon( IconCol );
0180     if( !icon.isNull() )
0181         mIconButton->setIcon( icon );
0182 
0183     DiskEntry * disk = selectedDisk( item );
0184     if (!disk)
0185         return;
0186 
0187     mIconLineEdit->setText( disk->iconName() );
0188 
0189     mMountLineEdit->setText( item->text( MountCommandCol ) );
0190     mUmountLineEdit->setText( item->text( UmountCommandCol ) );
0191 }
0192 
0193 
0194 void MntConfigWidget::iconChangedButton(const QString &iconName)
0195 {
0196     iconChanged(iconName);
0197 }
0198 
0199 void MntConfigWidget::iconChanged(const QString &iconName)
0200 {
0201     QList<QTreeWidgetItem*> list = m_listWidget->selectedItems();
0202     QTreeWidgetItem * item = list.at(0);
0203 
0204     DiskEntry * disk = selectedDisk( item );
0205     if ( !disk )
0206         return;
0207 
0208     disk->setIconName(iconName);
0209     mIconLineEdit->setText(iconName);
0210 
0211     const int smallIcon = qApp->style()->pixelMetric(QStyle::PM_SmallIconSize);
0212     const QPixmap icon = QIcon::fromTheme( iconName ).pixmap(smallIcon);
0213     item->setIcon( IconCol, icon );
0214     mIconButton->setIcon( icon );
0215 
0216     slotChanged();
0217 }
0218 
0219 void MntConfigWidget::iconDefault()
0220 {
0221     QList<QTreeWidgetItem*> list = m_listWidget->selectedItems();
0222     QTreeWidgetItem * item = list.at(0);
0223 
0224     DiskEntry * disk = selectedDisk( item );
0225     if ( !disk )
0226         return;
0227 
0228     iconChanged(disk->guessIconName());
0229 }
0230 
0231 void MntConfigWidget::selectMntFile()
0232 {
0233     QUrl url = QFileDialog::getOpenFileUrl( this, QStringLiteral( "*" ));
0234 
0235     if( url.isEmpty() )
0236         return;
0237 
0238     if( !url.isLocalFile() )
0239     {
0240         KMessageBox::error( nullptr, i18n( "Only local files supported." ) );
0241         return;
0242     }
0243 
0244     mMountLineEdit->setText( url.path() );
0245 }
0246 
0247 void MntConfigWidget::selectUmntFile()
0248 {
0249     QUrl url = QFileDialog::getOpenFileUrl( this, QStringLiteral( "*" ));
0250 
0251     if( url.isEmpty() )
0252         return;
0253 
0254     if( !url.isLocalFile() )
0255     {
0256         KMessageBox::error( nullptr, i18n( "Only local files are currently supported." ) );
0257         return;
0258     }
0259 
0260     mUmountLineEdit->setText( url.path() );
0261 }
0262 
0263 void MntConfigWidget::mntCmdChanged( const QString &data )
0264 {
0265 
0266     QList<QTreeWidgetItem*> list = m_listWidget->selectedItems();
0267     QTreeWidgetItem * item = list.at(0);
0268 
0269     DiskEntry * disk = selectedDisk( item );
0270     if ( !disk )
0271         return;
0272 
0273     disk->setMountCommand( data );
0274     item->setText( MountCommandCol , data );
0275 
0276 }
0277 
0278 
0279 void MntConfigWidget::umntCmdChanged( const QString &data )
0280 {
0281     QList<QTreeWidgetItem*> list = m_listWidget->selectedItems();
0282     QTreeWidgetItem * item = list.at(0);
0283 
0284     DiskEntry * disk = selectedDisk( item );
0285     if ( !disk )
0286         return;
0287 
0288     disk->setUmountCommand( data );
0289     item->setText( UmountCommandCol , data );
0290 
0291 }
0292 
0293 DiskEntry * MntConfigWidget::selectedDisk( QTreeWidgetItem * item )
0294 {
0295     if( item == nullptr )
0296     {
0297         QList<QTreeWidgetItem*> selected = m_listWidget->selectedItems();
0298         if ( selected.size() == 1 )
0299             item =  selected.at(0);
0300         else
0301             return nullptr;
0302     }
0303 
0304     DiskEntry * tmpDisk = new DiskEntry(item->text( DeviceCol ));
0305     tmpDisk->setMountPoint(item->text( MountPointCol ));
0306 
0307     int pos = mDiskList.find(tmpDisk);
0308 
0309     delete tmpDisk;
0310 
0311     return mDiskList.at(pos);
0312 }
0313 
0314 void MntConfigWidget::closeEvent(QCloseEvent *)
0315 {
0316 }
0317 
0318 void MntConfigWidget::slotChanged()
0319 {
0320     Q_EMIT configChanged();
0321 }
0322 
0323