componentchooserbrowser.cpp 6.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/***************************************************************************
                          componentchooserbrowser.cpp
                             -------------------
    copyright            : (C) 2002 by Joseph Wenninger
    email                : jowenn@kde.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License version 2 as     *
 *   published by the Free Software Foundationi                            *
 *                                                                         *
 ***************************************************************************/

#include "componentchooserbrowser.h"
#include <kopenwithdialog.h>
#include <kglobalsettings.h>
#include <kconfiggroup.h>

21
#include <KBuildSycocaProgressDialog>
22
#include <KLocalizedString>
23
#include <KServiceTypeTrader>
24

25 26
#include "../migrationlib/kdelibs4config.h"

Laurent Montel's avatar
KUrl--  
Laurent Montel committed
27
#include <QUrl>
28 29 30 31 32

CfgBrowser::CfgBrowser(QWidget *parent)
    : QWidget(parent), Ui::BrowserConfig_UI(),CfgPlugin()
{
    setupUi(this);
Laurent Montel's avatar
Laurent Montel committed
33 34
    connect(lineExec, &KLineEdit::textChanged, this, &CfgBrowser::configChanged);
    connect(radioKIO, &QRadioButton::toggled, this, &CfgBrowser::configChanged);
35 36
    connect(radioService, &QRadioButton::toggled, this, &CfgBrowser::selectBrowserApp);
    connect(browserCombo, static_cast<void(QComboBox::*)(int)>(&QComboBox::activated), this, &CfgBrowser::selectBrowserApp);
Laurent Montel's avatar
Laurent Montel committed
37
    connect(radioExec, &QRadioButton::toggled, this, &CfgBrowser::configChanged);
38
    connect(btnSelectApplication, &QToolButton::clicked, this, &CfgBrowser::selectBrowser);
39 40 41 42 43
}

CfgBrowser::~CfgBrowser() {
}

44 45 46 47 48 49 50 51
void CfgBrowser::selectBrowserApp()
{
    const QString &storageId = browserCombo->currentData().toString();
    m_browserService = KService::serviceByStorageId(storageId);
    m_browserExec.clear();
    configChanged();
}

52 53 54 55 56 57 58 59 60 61 62 63
void CfgBrowser::configChanged()
{
    emit changed(true);
}

void CfgBrowser::defaults()
{
    load(0);
}

void CfgBrowser::load(KConfig *) 
{
David Faure's avatar
David Faure committed
64 65
    const KConfigGroup config(KSharedConfig::openConfig(QStringLiteral("kdeglobals")), QStringLiteral("General") );
    const QString exec = config.readPathEntry( QStringLiteral("BrowserApplication"), QString() );
66
    if (exec.isEmpty()) {
67 68 69
        radioKIO->setChecked(true);
        m_browserExec = exec;
        m_browserService = 0;
70
    } else {
71
        radioExec->setChecked(true);
72
        if (exec.startsWith('!')) {
73 74
            m_browserExec = exec.mid(1);
            m_browserService = 0;
75
        } else {
76
            m_browserService = KService::serviceByStorageId( exec );
77
            if (m_browserService) {
78
                m_browserExec = m_browserService->desktopEntryName();
79
            } else {
80
                m_browserExec.clear();
81
            }
82 83 84 85 86
        }
    }

    lineExec->setText(m_browserExec);

87 88 89 90 91 92 93 94 95 96 97 98 99
    browserCombo->clear();

    const auto &browsers = KServiceTypeTrader::self()->query(QStringLiteral("Application"),
                                                             QStringLiteral("'WebBrowser' in Categories"));
    for (const auto &service : browsers) {
        browserCombo->addItem(QIcon::fromTheme(service->icon()), service->name(), service->storageId());

        if ((m_browserService && m_browserService->storageId() == service->storageId()) || service->exec() == m_browserExec) {
            browserCombo->setCurrentIndex(browserCombo->count() - 1);
            radioService->setChecked(true);
        }
    }

100 101 102 103 104
    emit changed(false);
}

void CfgBrowser::save(KConfig *)
{
105
    KSharedConfig::Ptr profile = KSharedConfig::openConfig(QStringLiteral("kdeglobals"));
106
    KConfigGroup config(profile, QStringLiteral("General"));
107
    QString exec;
108 109 110 111 112
    if (radioService->isChecked()) {
        if (m_browserService) {
            exec = m_browserService->storageId();
        }
    } else if (radioExec->isChecked()) {
113
        exec = lineExec->text();
114
        if (m_browserService && (exec == m_browserExec)) {
115
            exec = m_browserService->storageId(); // Use service
116
        } else if (!exec.isEmpty()) {
117
            exec = '!' + exec; // Literal command
118
        }
119
    }
120
    config.writePathEntry( QStringLiteral("BrowserApplication"), exec); // KConfig::Normal|KConfig::Global
121 122
    config.sync();

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    // Save the default browser as scheme handler for http(s) in mimeapps.list
    KSharedConfig::Ptr mimeAppList = KSharedConfig::openConfig(QStringLiteral("mimeapps.list"), KConfig::NoGlobals, QStandardPaths::GenericConfigLocation);
    if (mimeAppList->isConfigWritable(true /*warn user if not writable*/)) {
        KConfigGroup defaultApp(mimeAppList, "Default Applications");

        if (m_browserService) {
            defaultApp.writeXdgListEntry(QStringLiteral("x-scheme-handler/http"), QStringList(m_browserService->storageId()));
            defaultApp.writeXdgListEntry(QStringLiteral("x-scheme-handler/https"), QStringList(m_browserService->storageId()));
        } else {
            defaultApp.deleteEntry(QStringLiteral("x-scheme-handler/http"));
            defaultApp.deleteEntry(QStringLiteral("x-scheme-handler/https"));
        }
        mimeAppList->sync();

        KBuildSycocaProgressDialog::rebuildKSycoca(this);
    }

140
    Kdelibs4SharedConfig::syncConfigGroup(QLatin1String("General"), "kdeglobals");
141

142 143 144 145 146 147 148
    KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged);

    emit changed(false);
}

void CfgBrowser::selectBrowser()
{
Laurent Montel's avatar
KUrl--  
Laurent Montel committed
149
    QList<QUrl> urlList;
150 151 152 153 154
    KOpenWithDialog dlg(urlList, i18n("Select preferred Web browser application:"), QString(), this);
    if (dlg.exec() != QDialog::Accepted)
        return;
    m_browserService = dlg.service();
    if (m_browserService) {
155 156 157 158 159 160 161 162 163 164 165
        // check if we have listed it in the browser combo, if so, put it there instead
        const int index = browserCombo->findData(m_browserService->storageId());
        if (index > -1) {
            browserCombo->setCurrentIndex(index);
            radioService->setChecked(true);
        } else {
            m_browserExec = m_browserService->desktopEntryName();
            if (m_browserExec.isEmpty()) {
                m_browserExec = m_browserService->exec();
            }
        }
166 167 168 169 170
    } else {
        m_browserExec = dlg.text();
    }
    lineExec->setText(m_browserExec);
}