File indexing completed on 2022-11-29 20:24:04

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 "kdfconfig.h"
0014 
0015 #include <KConfigGroup>
0016 
0017 #include <QCheckBox>
0018 #include <QCloseEvent>
0019 #include <QTreeWidget>
0020 #include <QTreeWidgetItem>
0021 
0022 #ifndef GUI_DEFINED
0023 static bool GUI;
0024 #define GUI_DEFINED
0025 #endif
0026 
0027 
0028 KDFConfigWidget::KDFConfigWidget(QWidget *parent, bool init)
0029         : QWidget( parent)
0030 {
0031 
0032     m_columnList.append( Column( QStringLiteral( "Icon" ), i18nc("TODO", "Icon"), 20, KDFWidget::IconCol) );
0033     m_columnList.append( Column( QStringLiteral( "Device" ), i18nc("Device of the storage", "Device"), 100, KDFWidget::DeviceCol) );
0034     m_columnList.append( Column( QStringLiteral( "Type" ), i18nc("Filesystem on storage", "Type"), 100 , KDFWidget::TypeCol) );
0035     m_columnList.append( Column( QStringLiteral( "Size" ), i18nc("Total size of the storage", "Size"), 100, KDFWidget::SizeCol) );
0036     m_columnList.append( Column( QStringLiteral( "MountPoint" ), i18nc("Mount point of the storage", "Mount Point"), 100, KDFWidget::MountPointCol) );
0037     m_columnList.append( Column( QStringLiteral( "Free" ), i18nc("Free space in storage", "Free"), 100, KDFWidget::FreeCol) );
0038     m_columnList.append( Column( QStringLiteral( "Full%" ), i18nc("Used storage space in %", "Full %"), 50, KDFWidget::FullCol ));
0039     m_columnList.append( Column( QStringLiteral( "UsageBar" ), i18nc("Usage graphical bar", "Usage"), 100, KDFWidget::UsageBarCol) );
0040 
0041     GUI = !init;
0042     if( GUI )
0043     {
0044         setupUi(this);
0045 
0046         QStringList headerLabels;
0047         for ( const Column &c : std::as_const(m_columnList))
0048         {
0049             headerLabels << c.columnName;
0050         }
0051         m_listWidget->setHeaderLabels(headerLabels);
0052 
0053         connect( m_listWidget, &QTreeWidget::itemClicked,
0054                  this, &KDFConfigWidget::toggleListText );
0055         connect( m_listWidget, &QTreeWidget::itemClicked,
0056                  this, &KDFConfigWidget::slotChanged );
0057         const int smallIcon = qApp->style()->pixelMetric(QStyle::PM_ToolBarIconSize);
0058         iconVisible = QIcon::fromTheme( QStringLiteral( "dialog-ok-apply" ) ).pixmap(smallIcon);
0059         iconHidden = QIcon::fromTheme( QStringLiteral( "dialog-cancel" ) ).pixmap(smallIcon);
0060 
0061         QTreeWidgetItem * item = new QTreeWidgetItem( m_listWidget );
0062         for( int i=0; i < m_columnList.size(); i++ )
0063         {
0064             item->setText( i, i18nc("Visible items on device information columns (enable|disable)", "visible") );
0065             item->setIcon( i, iconVisible );
0066             item->setData( i, Qt::UserRole, QVariant( true ) );
0067         }
0068         m_listWidget->setCurrentItem( item );
0069 
0070         connect(m_updateSpinBox, SIGNAL(valueChanged(int)), this, SLOT(slotChanged()) );
0071 
0072         connect(mFileManagerEdit,&QLineEdit::textChanged,this,&KDFConfigWidget::slotChanged);
0073 
0074         connect(mSystemFileManagerCheck,&QAbstractButton::toggled,this,&KDFConfigWidget::slotChanged);
0075         connect(mSystemFileManagerCheck,&QAbstractButton::toggled,mFileManagerEdit,&QWidget::setDisabled);
0076         connect(mSystemFileManagerCheck,&QAbstractButton::toggled,fileManagerEdit,&QWidget::setDisabled);
0077 
0078         connect(mOpenMountCheck,&QAbstractButton::toggled,this,&KDFConfigWidget::slotChanged);
0079 
0080         connect(mPopupFullCheck,&QAbstractButton::toggled,this,&KDFConfigWidget::slotChanged);
0081 
0082         if(!QStandardPaths::locate(QStandardPaths::RuntimeLocation, QLatin1String("flatpak-info")).isEmpty())
0083         {
0084             mFlatpakLabel->setVisible( true );
0085             mSystemFileManagerCheck->setEnabled( false );
0086             mFileManagerEdit->setEnabled( false );
0087         }
0088     }
0089 
0090     loadSettings();
0091     if( init )
0092         applySettings();
0093 
0094 }
0095 
0096 
0097 KDFConfigWidget::~KDFConfigWidget()
0098 {
0099     delete m_listWidget;
0100 }
0101 
0102 
0103 void KDFConfigWidget::closeEvent(QCloseEvent *)
0104 {
0105     applySettings();
0106     qApp->quit();
0107 }
0108 
0109 
0110 void KDFConfigWidget::applySettings( void )
0111 {
0112     KConfigGroup config(KSharedConfig::openConfig(), "KDFConfig");
0113 
0114     if( GUI )
0115     {
0116         //config.writeEntry( "Width", width() );
0117         //config.writeEntry( "Height", height() );
0118 
0119         mStd.setFileManager( mFileManagerEdit->text() );
0120         mStd.setUpdateFrequency( m_updateSpinBox->value() );
0121         mStd.setPopupIfFull( mPopupFullCheck->isChecked() );
0122         mStd.setOpenFileManager( mOpenMountCheck->isChecked() );
0123         mStd.setUseSystemFileManager( mSystemFileManagerCheck->isChecked() );
0124         mStd.writeConfiguration();
0125 
0126         QTreeWidgetItem * item = m_listWidget->topLevelItem(0);
0127         for( int i=0; i < m_columnList.size(); i++ )
0128         {
0129             bool visible = item->data( i, Qt::UserRole ).toBool();
0130             config.writeEntry( m_columnList.at(i).name , visible );
0131         }
0132 
0133     }
0134     else
0135     {
0136         mStd.writeDefaultFileManager();
0137     }
0138     config.sync();
0139 }
0140 
0141 
0142 void KDFConfigWidget::loadSettings( void )
0143 {
0144     KConfigGroup config(KSharedConfig::openConfig(), "KDFConfig");
0145 
0146     if( GUI )
0147     {
0148         mStd.updateConfiguration();
0149         m_updateSpinBox->setValue( mStd.updateFrequency() );
0150         mPopupFullCheck->setChecked( mStd.popupIfFull() );
0151         mOpenMountCheck->setChecked( mStd.openFileManager() );
0152         mSystemFileManagerCheck->setChecked( mStd.useSystemFileManager() );
0153         mFileManagerEdit->setText( mStd.fileManager() );
0154 
0155         mFileManagerEdit->setEnabled( !mStd.useSystemFileManager() );
0156         fileManagerEdit->setEnabled( !mStd.useSystemFileManager() );
0157 
0158         QTreeWidgetItem * item = m_listWidget->topLevelItem(0);
0159         for( int i=0; i < m_columnList.size(); i++ )
0160         {
0161             bool visible = config.readEntry( m_columnList.at(i).name, true );
0162 
0163             item->setText( i, visible ? i18nc("Are items on device information columns visible?", "visible") :
0164                            i18nc("Are items on device information columns hidden?", "hidden") );
0165             item->setIcon( i, visible ? iconVisible : iconHidden );
0166             item->setData( i, Qt::UserRole, QVariant( visible ) );
0167         }
0168     }
0169 
0170 }
0171 
0172 void KDFConfigWidget::defaultsBtnClicked( void )
0173 {
0174     mStd.setDefault();
0175     m_updateSpinBox->setValue( mStd.updateFrequency() );
0176     mPopupFullCheck->setChecked( mStd.popupIfFull() );
0177     mOpenMountCheck->setChecked( mStd.openFileManager() );
0178     mSystemFileManagerCheck->setChecked( mStd.useSystemFileManager() );
0179     mFileManagerEdit->setText( mStd.fileManager() );
0180 
0181     QTreeWidgetItem * item = m_listWidget->topLevelItem(0);
0182     for( int i=0; i < m_columnList.size(); i++ )
0183     {
0184         item->setText( i, i18nc("Visible items on device information columns (enable|disable)", "visible") );
0185         item->setIcon( i, iconVisible );
0186         item->setData( i, Qt::UserRole, QVariant( true ) );
0187     }
0188     m_listWidget->setCurrentItem( item );
0189 }
0190 
0191 
0192 void KDFConfigWidget::toggleListText( QTreeWidgetItem *item, int column )
0193 {
0194     if (!item)
0195         return;
0196     QString text = item->text( column );
0197 
0198     bool visible = item->data( column, Qt::UserRole).toBool();
0199     item->setData( column, Qt::UserRole, !visible );
0200 
0201     item->setText(column, visible ? i18nc("Device information item is hidden", "hidden"):
0202                   i18nc("Device information item is visible", "visible") );
0203     item->setIcon( column, visible ? iconHidden : iconVisible );
0204 }
0205 
0206 void KDFConfigWidget::slotChanged()
0207 {
0208     Q_EMIT configChanged();
0209 }