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

0001 /*
0002  * SPDX-FileCopyrightText: 2006 Peter Penz (peter.penz@gmx.at) and Patrice Tremblay
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "folderstabssettingspage.h"
0008 #include "dolphinmainwindow.h"
0009 #include "dolphinviewcontainer.h"
0010 #include "global.h"
0011 
0012 #include <KLocalizedString>
0013 #include <KMessageBox>
0014 #include <KProtocolManager>
0015 
0016 #include <QButtonGroup>
0017 #include <QCheckBox>
0018 #include <QFileDialog>
0019 #include <QFormLayout>
0020 #include <QGridLayout>
0021 #include <QHBoxLayout>
0022 #include <QLineEdit>
0023 #include <QPushButton>
0024 #include <QRadioButton>
0025 #include <QSpacerItem>
0026 
0027 FoldersTabsSettingsPage::FoldersTabsSettingsPage(QWidget *parent)
0028     : SettingsPageBase(parent)
0029     , m_homeUrlBoxLayoutContainer(nullptr)
0030     , m_buttonBoxLayoutContainer(nullptr)
0031     , m_homeUrlRadioButton(nullptr)
0032     , m_homeUrl(nullptr)
0033     , m_rememberOpenedTabsRadioButton(nullptr)
0034     , m_openNewTabAfterLastTab(nullptr)
0035     , m_openNewTabAfterCurrentTab(nullptr)
0036     , m_splitView(nullptr)
0037     , m_filterBar(nullptr)
0038     , m_showFullPathInTitlebar(nullptr)
0039     , m_openExternallyCalledFolderInNewTab(nullptr)
0040     , m_useTabForSplitViewSwitch(nullptr)
0041 {
0042     QFormLayout *topLayout = new QFormLayout(this);
0043 
0044     // Show on startup
0045     m_rememberOpenedTabsRadioButton = new QRadioButton(i18nc("@option:radio Show on startup", "Folders, tabs, and window state from last time"));
0046     m_homeUrlRadioButton = new QRadioButton();
0047     // HACK: otherwise the radio button has too much spacing in a grid layout
0048     m_homeUrlRadioButton->setMaximumWidth(24);
0049 
0050     QButtonGroup *initialViewGroup = new QButtonGroup(this);
0051     initialViewGroup->addButton(m_rememberOpenedTabsRadioButton);
0052     initialViewGroup->addButton(m_homeUrlRadioButton);
0053 
0054     // create 'Home URL' editor
0055     m_homeUrlBoxLayoutContainer = new QWidget(this);
0056     QHBoxLayout *homeUrlBoxLayout = new QHBoxLayout(m_homeUrlBoxLayoutContainer);
0057     homeUrlBoxLayout->setContentsMargins(0, 0, 0, 0);
0058 
0059     m_homeUrl = new QLineEdit();
0060     m_homeUrl->setClearButtonEnabled(true);
0061     homeUrlBoxLayout->addWidget(m_homeUrl);
0062 
0063     QPushButton *selectHomeUrlButton = new QPushButton(QIcon::fromTheme(QStringLiteral("folder-open")), QString());
0064     homeUrlBoxLayout->addWidget(selectHomeUrlButton);
0065 
0066 #ifndef QT_NO_ACCESSIBILITY
0067     selectHomeUrlButton->setAccessibleName(i18nc("@action:button", "Select Home Location"));
0068 #endif
0069 
0070     connect(selectHomeUrlButton, &QPushButton::clicked, this, &FoldersTabsSettingsPage::selectHomeUrl);
0071 
0072     m_buttonBoxLayoutContainer = new QWidget(this);
0073     QHBoxLayout *buttonBoxLayout = new QHBoxLayout(m_buttonBoxLayoutContainer);
0074     buttonBoxLayout->setContentsMargins(0, 0, 0, 0);
0075 
0076     QPushButton *useCurrentButton = new QPushButton(i18nc("@action:button", "Use Current Location"));
0077     buttonBoxLayout->addWidget(useCurrentButton);
0078     connect(useCurrentButton, &QPushButton::clicked, this, &FoldersTabsSettingsPage::useCurrentLocation);
0079     QPushButton *useDefaultButton = new QPushButton(i18nc("@action:button", "Use Default Location"));
0080     buttonBoxLayout->addWidget(useDefaultButton);
0081     connect(useDefaultButton, &QPushButton::clicked, this, &FoldersTabsSettingsPage::useDefaultLocation);
0082 
0083     QGridLayout *startInLocationLayout = new QGridLayout();
0084     startInLocationLayout->setHorizontalSpacing(0);
0085     startInLocationLayout->setContentsMargins(0, 0, 0, 0);
0086     startInLocationLayout->addWidget(m_homeUrlRadioButton, 0, 0);
0087     startInLocationLayout->addWidget(m_homeUrlBoxLayoutContainer, 0, 1);
0088     startInLocationLayout->addWidget(m_buttonBoxLayoutContainer, 1, 1);
0089 
0090     topLayout->addRow(i18nc("@label:textbox", "Show on startup:"), m_rememberOpenedTabsRadioButton);
0091     topLayout->addRow(QString(), startInLocationLayout);
0092 
0093     topLayout->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT, QSizePolicy::Fixed, QSizePolicy::Fixed));
0094     // Opening Folders
0095     m_openExternallyCalledFolderInNewTab = new QCheckBox(i18nc("@option:check Opening Folders", "Keep a single Dolphin window, opening new folders in tabs"));
0096     topLayout->addRow(i18nc("@label:checkbox", "Opening Folders:"), m_openExternallyCalledFolderInNewTab);
0097     // Window
0098     m_showFullPathInTitlebar = new QCheckBox(i18nc("@option:check Startup Settings", "Show full path in title bar"));
0099     topLayout->addRow(i18nc("@label:checkbox", "Window:"), m_showFullPathInTitlebar);
0100     m_filterBar = new QCheckBox(i18nc("@option:check Window Startup Settings", "Show filter bar"));
0101     topLayout->addRow(QString(), m_filterBar);
0102 
0103     topLayout->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT, QSizePolicy::Fixed, QSizePolicy::Fixed));
0104 
0105     // Tabs properties
0106     m_openNewTabAfterCurrentTab = new QRadioButton(i18nc("option:radio", "After current tab"));
0107     m_openNewTabAfterLastTab = new QRadioButton(i18nc("option:radio", "At end of tab bar"));
0108     QButtonGroup *tabsBehaviorGroup = new QButtonGroup(this);
0109     tabsBehaviorGroup->addButton(m_openNewTabAfterCurrentTab);
0110     tabsBehaviorGroup->addButton(m_openNewTabAfterLastTab);
0111     topLayout->addRow(i18nc("@title:group", "Open new tabs: "), m_openNewTabAfterCurrentTab);
0112     topLayout->addRow(QString(), m_openNewTabAfterLastTab);
0113 
0114     // Split Views
0115     topLayout->addItem(new QSpacerItem(0, Dolphin::VERTICAL_SPACER_HEIGHT, QSizePolicy::Fixed, QSizePolicy::Fixed));
0116 
0117     // 'Switch between panes of split views with tab key'
0118     m_useTabForSplitViewSwitch = new QCheckBox(i18nc("option:check split view panes", "Switch between panes with Tab key"));
0119     topLayout->addRow(i18nc("@title:group", "Split view: "), m_useTabForSplitViewSwitch);
0120 
0121     // 'Close active pane when turning off split view'
0122     m_closeActiveSplitView = new QCheckBox(i18nc("option:check", "Turning off split view closes active pane"));
0123     topLayout->addRow(QString(), m_closeActiveSplitView);
0124     m_closeActiveSplitView->setToolTip(i18n("When deactivated, turning off split view will close the inactive pane"));
0125 
0126     // 'Begin in split view mode'
0127     m_splitView = new QCheckBox(i18nc("@option:check Startup Settings", "Begin in split view mode"));
0128     topLayout->addRow(i18n("New windows:"), m_splitView);
0129 
0130     loadSettings();
0131 
0132     updateInitialViewOptions();
0133 
0134     connect(m_homeUrl, &QLineEdit::textChanged, this, &FoldersTabsSettingsPage::slotSettingsChanged);
0135     connect(m_rememberOpenedTabsRadioButton, &QRadioButton::toggled, this, &FoldersTabsSettingsPage::slotSettingsChanged);
0136     connect(m_homeUrlRadioButton, &QRadioButton::toggled, this, &FoldersTabsSettingsPage::slotSettingsChanged);
0137 
0138     connect(m_splitView, &QCheckBox::toggled, this, &FoldersTabsSettingsPage::slotSettingsChanged);
0139     connect(m_filterBar, &QCheckBox::toggled, this, &FoldersTabsSettingsPage::slotSettingsChanged);
0140 
0141     connect(m_openExternallyCalledFolderInNewTab, &QCheckBox::toggled, this, &FoldersTabsSettingsPage::slotSettingsChanged);
0142     connect(m_showFullPathInTitlebar, &QCheckBox::toggled, this, &FoldersTabsSettingsPage::slotSettingsChanged);
0143 
0144     connect(m_useTabForSplitViewSwitch, &QCheckBox::toggled, this, &FoldersTabsSettingsPage::changed);
0145     connect(m_closeActiveSplitView, &QCheckBox::toggled, this, &FoldersTabsSettingsPage::changed);
0146 
0147     connect(m_openNewTabAfterCurrentTab, &QRadioButton::toggled, this, &FoldersTabsSettingsPage::changed);
0148     connect(m_openNewTabAfterLastTab, &QRadioButton::toggled, this, &FoldersTabsSettingsPage::changed);
0149 }
0150 
0151 FoldersTabsSettingsPage::~FoldersTabsSettingsPage()
0152 {
0153 }
0154 
0155 void FoldersTabsSettingsPage::applySettings()
0156 {
0157     GeneralSettings *settings = GeneralSettings::self();
0158 
0159     settings->setUseTabForSwitchingSplitView(m_useTabForSplitViewSwitch->isChecked());
0160     settings->setCloseActiveSplitView(m_closeActiveSplitView->isChecked());
0161     const QUrl url(QUrl::fromUserInput(m_homeUrl->text(), QString(), QUrl::AssumeLocalFile));
0162     if (url.isValid() && KProtocolManager::supportsListing(url)) {
0163         KIO::StatJob *job = KIO::stat(url, KIO::StatJob::SourceSide, KIO::StatDetail::StatBasic, KIO::JobFlag::HideProgressInfo);
0164         connect(job, &KJob::result, this, [this, settings, url](KJob *job) {
0165             if (job->error() == 0 && qobject_cast<KIO::StatJob *>(job)->statResult().isDir()) {
0166                 settings->setHomeUrl(url.toDisplayString(QUrl::PreferLocalFile));
0167             } else {
0168                 showSetDefaultDirectoryError();
0169             }
0170         });
0171     } else {
0172         showSetDefaultDirectoryError();
0173     }
0174 
0175     // Remove saved state if "remember open tabs" has been turned off
0176     if (!m_rememberOpenedTabsRadioButton->isChecked()) {
0177         KConfigGroup windowState{KSharedConfig::openConfig(QStringLiteral("dolphinrc")), QStringLiteral("WindowState")};
0178         if (windowState.exists()) {
0179             windowState.deleteGroup();
0180         }
0181     }
0182 
0183     settings->setRememberOpenedTabs(m_rememberOpenedTabsRadioButton->isChecked());
0184     settings->setSplitView(m_splitView->isChecked());
0185     settings->setFilterBar(m_filterBar->isChecked());
0186     settings->setOpenExternallyCalledFolderInNewTab(m_openExternallyCalledFolderInNewTab->isChecked());
0187     settings->setShowFullPathInTitlebar(m_showFullPathInTitlebar->isChecked());
0188 
0189     settings->setOpenNewTabAfterLastTab(m_openNewTabAfterLastTab->isChecked());
0190 
0191     settings->save();
0192 }
0193 
0194 void FoldersTabsSettingsPage::restoreDefaults()
0195 {
0196     GeneralSettings *settings = GeneralSettings::self();
0197     settings->useDefaults(true);
0198     loadSettings();
0199     settings->useDefaults(false);
0200 }
0201 
0202 void FoldersTabsSettingsPage::slotSettingsChanged()
0203 {
0204     // Provide a hint that the startup settings have been changed. This allows the views
0205     // to apply the startup settings only if they have been explicitly changed by the user
0206     // (see bug #254947).
0207     GeneralSettings::setModifiedStartupSettings(true);
0208 
0209     // Enable and disable home URL controls appropriately
0210     updateInitialViewOptions();
0211     Q_EMIT changed();
0212 }
0213 
0214 void FoldersTabsSettingsPage::updateInitialViewOptions()
0215 {
0216     m_homeUrlBoxLayoutContainer->setEnabled(m_homeUrlRadioButton->isChecked());
0217     m_buttonBoxLayoutContainer->setEnabled(m_homeUrlRadioButton->isChecked());
0218 }
0219 
0220 void FoldersTabsSettingsPage::selectHomeUrl()
0221 {
0222     const QUrl homeUrl(QUrl::fromUserInput(m_homeUrl->text(), QString(), QUrl::AssumeLocalFile));
0223     QUrl url = QFileDialog::getExistingDirectoryUrl(this, QString(), homeUrl);
0224     if (!url.isEmpty()) {
0225         m_homeUrl->setText(url.toDisplayString(QUrl::PreferLocalFile));
0226         slotSettingsChanged();
0227     }
0228 }
0229 
0230 void FoldersTabsSettingsPage::useCurrentLocation()
0231 {
0232     m_homeUrl->setText(m_url.toDisplayString(QUrl::PreferLocalFile));
0233 }
0234 
0235 void FoldersTabsSettingsPage::useDefaultLocation()
0236 {
0237     m_homeUrl->setText(QDir::homePath());
0238 }
0239 
0240 void FoldersTabsSettingsPage::loadSettings()
0241 {
0242     const QUrl url(Dolphin::homeUrl());
0243     m_homeUrl->setText(url.toDisplayString(QUrl::PreferLocalFile));
0244     m_rememberOpenedTabsRadioButton->setChecked(GeneralSettings::rememberOpenedTabs());
0245     m_homeUrlRadioButton->setChecked(!GeneralSettings::rememberOpenedTabs());
0246     m_splitView->setChecked(GeneralSettings::splitView());
0247     m_filterBar->setChecked(GeneralSettings::filterBar());
0248     m_showFullPathInTitlebar->setChecked(GeneralSettings::showFullPathInTitlebar());
0249     m_openExternallyCalledFolderInNewTab->setChecked(GeneralSettings::openExternallyCalledFolderInNewTab());
0250 
0251     m_useTabForSplitViewSwitch->setChecked(GeneralSettings::useTabForSwitchingSplitView());
0252     m_closeActiveSplitView->setChecked(GeneralSettings::closeActiveSplitView());
0253 
0254     m_openNewTabAfterLastTab->setChecked(GeneralSettings::openNewTabAfterLastTab());
0255     m_openNewTabAfterCurrentTab->setChecked(!m_openNewTabAfterLastTab->isChecked());
0256 }
0257 
0258 void FoldersTabsSettingsPage::showSetDefaultDirectoryError()
0259 {
0260     KMessageBox::error(this, i18nc("@info", "The location for the home folder is invalid or does not exist, it will not be applied."));
0261 }
0262 
0263 #include "moc_folderstabssettingspage.cpp"