File indexing completed on 2024-05-12 17:18:54

0001 /*
0002  * SPDX-FileCopyrightText: 2006 Peter Penz <peter.penz19@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "previewssettingspage.h"
0008 
0009 #include "dolphin_generalsettings.h"
0010 #include "settings/servicemodel.h"
0011 
0012 #include <KIO/PreviewJob>
0013 #include <KLocalizedString>
0014 #include <KPluginMetaData>
0015 
0016 #include <QHBoxLayout>
0017 #include <QLabel>
0018 #include <QListView>
0019 #include <QScroller>
0020 #include <QShowEvent>
0021 #include <QSortFilterProxyModel>
0022 #include <QSpinBox>
0023 
0024 // default settings
0025 namespace
0026 {
0027 const int DefaultMaxLocalPreviewSize = 0; // 0 MB
0028 const int DefaultMaxRemotePreviewSize = 0; // 0 MB
0029 }
0030 
0031 PreviewsSettingsPage::PreviewsSettingsPage(QWidget *parent)
0032     : SettingsPageBase(parent)
0033     , m_initialized(false)
0034     , m_listView(nullptr)
0035     , m_enabledPreviewPlugins()
0036     , m_localFileSizeBox(nullptr)
0037     , m_remoteFileSizeBox(nullptr)
0038 {
0039     QVBoxLayout *topLayout = new QVBoxLayout(this);
0040 
0041     QLabel *showPreviewsLabel = new QLabel(i18nc("@title:group", "Show previews in the view for:"), this);
0042 
0043     m_listView = new QListView(this);
0044     QScroller::grabGesture(m_listView->viewport(), QScroller::TouchGesture);
0045 
0046     ServiceModel *serviceModel = new ServiceModel(this);
0047     QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
0048     proxyModel->setSourceModel(serviceModel);
0049     proxyModel->setSortRole(Qt::DisplayRole);
0050     proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
0051 
0052     m_listView->setModel(proxyModel);
0053     m_listView->setVerticalScrollMode(QListView::ScrollPerPixel);
0054     m_listView->setUniformItemSizes(true);
0055 
0056     QLabel *localFileSizeLabel = new QLabel(i18n("Skip previews for local files above:"), this);
0057 
0058     m_localFileSizeBox = new QSpinBox(this);
0059     m_localFileSizeBox->setSingleStep(1);
0060     m_localFileSizeBox->setSuffix(i18nc("Mebibytes; used as a suffix in a spinbox showing e.g. '3 MiB'", " MiB"));
0061     m_localFileSizeBox->setRange(0, 9999999); /* MB */
0062     m_localFileSizeBox->setSpecialValueText(i18n("No limit"));
0063 
0064     QHBoxLayout *localFileSizeBoxLayout = new QHBoxLayout();
0065     localFileSizeBoxLayout->addWidget(localFileSizeLabel);
0066     localFileSizeBoxLayout->addStretch(0);
0067     localFileSizeBoxLayout->addWidget(m_localFileSizeBox);
0068 
0069     QLabel *remoteFileSizeLabel = new QLabel(i18nc("@label", "Skip previews for remote files above:"), this);
0070 
0071     m_remoteFileSizeBox = new QSpinBox(this);
0072     m_remoteFileSizeBox->setSingleStep(1);
0073     m_remoteFileSizeBox->setSuffix(i18nc("Mebibytes; used as a suffix in a spinbox showing e.g. '3 MiB'", " MiB"));
0074     m_remoteFileSizeBox->setRange(0, 9999999); /* MB */
0075     m_remoteFileSizeBox->setSpecialValueText(i18n("No previews"));
0076 
0077     QHBoxLayout *remoteFileSizeBoxLayout = new QHBoxLayout();
0078     remoteFileSizeBoxLayout->addWidget(remoteFileSizeLabel);
0079     remoteFileSizeBoxLayout->addStretch(0);
0080     remoteFileSizeBoxLayout->addWidget(m_remoteFileSizeBox);
0081 
0082     topLayout->addWidget(showPreviewsLabel);
0083     topLayout->addWidget(m_listView);
0084     topLayout->addLayout(localFileSizeBoxLayout);
0085     topLayout->addLayout(remoteFileSizeBoxLayout);
0086 
0087     loadSettings();
0088 
0089     connect(m_listView, &QListView::clicked, this, &PreviewsSettingsPage::changed);
0090     connect(m_localFileSizeBox, &QSpinBox::valueChanged, this, &PreviewsSettingsPage::changed);
0091     connect(m_remoteFileSizeBox, &QSpinBox::valueChanged, this, &PreviewsSettingsPage::changed);
0092 }
0093 
0094 PreviewsSettingsPage::~PreviewsSettingsPage()
0095 {
0096 }
0097 
0098 void PreviewsSettingsPage::applySettings()
0099 {
0100     const QAbstractItemModel *model = m_listView->model();
0101     const int rowCount = model->rowCount();
0102     if (rowCount > 0) {
0103         m_enabledPreviewPlugins.clear();
0104         for (int i = 0; i < rowCount; ++i) {
0105             const QModelIndex index = model->index(i, 0);
0106             const bool checked = model->data(index, Qt::CheckStateRole).value<Qt::CheckState>() == Qt::Checked;
0107             if (checked) {
0108                 const QString enabledPlugin = model->data(index, Qt::UserRole).toString();
0109                 m_enabledPreviewPlugins.append(enabledPlugin);
0110             }
0111         }
0112     }
0113 
0114     KConfigGroup globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
0115     globalConfig.writeEntry("Plugins", m_enabledPreviewPlugins);
0116 
0117     if (!m_localFileSizeBox->value()) {
0118         globalConfig.deleteEntry("MaximumSize", KConfigBase::Normal | KConfigBase::Global);
0119     } else {
0120         const qulonglong maximumLocalSize = static_cast<qulonglong>(m_localFileSizeBox->value()) * 1024 * 1024;
0121         globalConfig.writeEntry("MaximumSize", maximumLocalSize, KConfigBase::Normal | KConfigBase::Global);
0122     }
0123 
0124     const qulonglong maximumRemoteSize = static_cast<qulonglong>(m_remoteFileSizeBox->value()) * 1024 * 1024;
0125     globalConfig.writeEntry("MaximumRemoteSize", maximumRemoteSize, KConfigBase::Normal | KConfigBase::Global);
0126 
0127     globalConfig.sync();
0128 }
0129 
0130 void PreviewsSettingsPage::restoreDefaults()
0131 {
0132     m_localFileSizeBox->setValue(DefaultMaxLocalPreviewSize);
0133     m_remoteFileSizeBox->setValue(DefaultMaxRemotePreviewSize);
0134 }
0135 
0136 void PreviewsSettingsPage::showEvent(QShowEvent *event)
0137 {
0138     if (!event->spontaneous() && !m_initialized) {
0139         loadPreviewPlugins();
0140         m_initialized = true;
0141     }
0142     SettingsPageBase::showEvent(event);
0143 }
0144 
0145 void PreviewsSettingsPage::loadPreviewPlugins()
0146 {
0147     QAbstractItemModel *model = m_listView->model();
0148 
0149     const QVector<KPluginMetaData> plugins = KIO::PreviewJob::availableThumbnailerPlugins();
0150     for (const KPluginMetaData &plugin : plugins) {
0151         const bool show = m_enabledPreviewPlugins.contains(plugin.pluginId());
0152 
0153         model->insertRow(0);
0154         const QModelIndex index = model->index(0, 0);
0155         model->setData(index, show ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole);
0156         model->setData(index, plugin.name(), Qt::DisplayRole);
0157         model->setData(index, plugin.pluginId(), ServiceModel::DesktopEntryNameRole);
0158     }
0159 
0160     model->sort(Qt::DisplayRole);
0161 }
0162 
0163 void PreviewsSettingsPage::loadSettings()
0164 {
0165     const KConfigGroup globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
0166     m_enabledPreviewPlugins = globalConfig.readEntry("Plugins", KIO::PreviewJob::defaultPlugins());
0167 
0168     const qulonglong defaultLocalPreview = static_cast<qulonglong>(DefaultMaxLocalPreviewSize) * 1024 * 1024;
0169     const qulonglong maxLocalByteSize = globalConfig.readEntry("MaximumSize", defaultLocalPreview);
0170     const int maxLocalMByteSize = maxLocalByteSize / (1024 * 1024);
0171     m_localFileSizeBox->setValue(maxLocalMByteSize);
0172 
0173     const qulonglong defaultRemotePreview = static_cast<qulonglong>(DefaultMaxRemotePreviewSize) * 1024 * 1024;
0174     const qulonglong maxRemoteByteSize = globalConfig.readEntry("MaximumRemoteSize", defaultRemotePreview);
0175     const int maxRemoteMByteSize = maxRemoteByteSize / (1024 * 1024);
0176     m_remoteFileSizeBox->setValue(maxRemoteMByteSize);
0177 }
0178 
0179 #include "moc_previewssettingspage.cpp"