Changeset View
Changeset View
Standalone View
Standalone View
kcms/componentchooser/componentchooserfilemanager.cpp
Show All 16 Lines | 1 | /* This file is part of the KDE project | |||
---|---|---|---|---|---|
17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
18 | Boston, MA 02110-1301, USA. | 18 | Boston, MA 02110-1301, USA. | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include "componentchooserfilemanager.h" | 21 | #include "componentchooserfilemanager.h" | ||
22 | #include <kbuildsycocaprogressdialog.h> | 22 | #include <kbuildsycocaprogressdialog.h> | ||
23 | #include <kprocess.h> | 23 | #include <kprocess.h> | ||
24 | #include <kmimetypetrader.h> | 24 | #include <kmimetypetrader.h> | ||
25 | #include <KServiceTypeTrader> | ||||
25 | #include <kopenwithdialog.h> | 26 | #include <kopenwithdialog.h> | ||
26 | #include <kconfiggroup.h> | 27 | #include <kconfiggroup.h> | ||
27 | #include <QStandardPaths> | 28 | #include <QStandardPaths> | ||
28 | #include <KSharedConfig> | 29 | #include <KSharedConfig> | ||
29 | 30 | | |||
30 | namespace { | | |||
31 | | ||||
32 | QRadioButton *findDolphinRadio(const QList<QRadioButton *> &radioButtons) | | |||
33 | { | | |||
34 | auto it = std::find_if(radioButtons.begin(), radioButtons.end(), [=](QRadioButton *radio) { | | |||
35 | return radio->property("storageId") == QStringLiteral("org.kde.dolphin.desktop"); | | |||
36 | }); | | |||
37 | if (it == radioButtons.end()) { | | |||
38 | return nullptr; | | |||
39 | } | | |||
40 | return *it; | | |||
41 | } | | |||
42 | | ||||
43 | } | | |||
44 | | ||||
45 | CfgFileManager::CfgFileManager(QWidget *parent) | 31 | CfgFileManager::CfgFileManager(QWidget *parent) | ||
46 | : QWidget(parent), Ui::FileManagerConfig_UI(),CfgPlugin() | 32 | : QWidget(parent), Ui::FileManagerConfig_UI(),CfgPlugin() | ||
47 | { | 33 | { | ||
48 | setupUi(this); | 34 | setupUi(this); | ||
49 | connect(btnSelectFileManager, &QToolButton::clicked, this, &CfgFileManager::slotAddFileManager); | 35 | connect(combofileManager, static_cast<void(QComboBox::*)(int)>(&QComboBox::activated), this, &CfgFileManager::selectFileManager); | ||
50 | } | 36 | } | ||
51 | 37 | | |||
52 | CfgFileManager::~CfgFileManager() { | 38 | CfgFileManager::~CfgFileManager() { | ||
53 | } | 39 | } | ||
54 | 40 | | |||
55 | void CfgFileManager::configChanged() | 41 | void CfgFileManager::defaults() | ||
56 | { | 42 | { | ||
57 | emit changed(true); | 43 | if (m_dolphinIndex != -1) { | ||
44 | combofileManager->setCurrentIndex(m_dolphinIndex); | ||||
45 | } | ||||
58 | } | 46 | } | ||
59 | 47 | | |||
60 | void CfgFileManager::defaults() | 48 | bool CfgFileManager::isDefaults() const | ||
61 | { | 49 | { | ||
62 | load(nullptr); | 50 | return m_dolphinIndex == -1 || m_dolphinIndex == combofileManager->currentIndex(); | ||
51 | } | ||||
52 | | ||||
53 | void CfgFileManager::selectFileManager(int index) | ||||
54 | { | ||||
55 | if (index == combofileManager->count() -1) { | ||||
56 | | ||||
57 | KOpenWithDialog dlg({}, i18n("Select preferred file manager:"), QString(), this); | ||||
58 | dlg.setSaveNewApplications(true); | ||||
59 | if (dlg.exec() != QDialog::Accepted) { | ||||
60 | combofileManager->setCurrentIndex(m_currentIndex); | ||||
61 | return; | ||||
62 | } | ||||
63 | | ||||
64 | const auto service = dlg.service(); | ||||
63 | 65 | | |||
64 | const auto radio = ::findDolphinRadio(mDynamicRadioButtons); | 66 | // if the selected service is already in the list | ||
65 | if (radio) { | 67 | const auto matching = combofileManager->model()->match(combofileManager->model()->index(0,0), Qt::UserRole, service->storageId()); | ||
66 | radio->setChecked(true); | 68 | if (!matching.isEmpty()) { | ||
69 | const int index = matching.at(0).row(); | ||||
70 | combofileManager->setCurrentIndex(index); | ||||
71 | changed(index != m_currentIndex); | ||||
72 | } else { | ||||
73 | const QString icon = !service->icon().isEmpty() ? service->icon() : QStringLiteral("application-x-shellscript"); | ||||
74 | combofileManager->insertItem(combofileManager->count() -1, QIcon::fromTheme(icon), service->name(), service->storageId()); | ||||
75 | combofileManager->setCurrentIndex(combofileManager->count() - 2); | ||||
76 | | ||||
77 | changed(true); | ||||
78 | } | ||||
79 | } else { | ||||
80 | changed(index != m_currentIndex); | ||||
67 | } | 81 | } | ||
68 | } | 82 | } | ||
69 | 83 | | |||
70 | bool CfgFileManager::isDefaults() const | 84 | static const QString mime = QStringLiteral("inode/directory"); | ||
85 | | ||||
86 | void CfgFileManager::load(KConfig *) | ||||
71 | { | 87 | { | ||
72 | const auto dolphinRadio = ::findDolphinRadio(mDynamicRadioButtons); | 88 | combofileManager->clear(); | ||
73 | // When dolphin is not present, we can't assume any default value | 89 | m_currentIndex = -1; | ||
74 | return !dolphinRadio || dolphinRadio->isChecked(); | 90 | | ||
75 | } | 91 | const KService::Ptr fileManager = KMimeTypeTrader::self()->preferredService(mime); | ||
76 | 92 | | |||
77 | static KService::List appOffers() | 93 | const auto constraint = QStringLiteral("'FileManager' in Categories and 'inode/directory' in ServiceTypes"); | ||
78 | { | 94 | const KService::List fileManagers = KServiceTypeTrader::self()->query(QStringLiteral("Application"), constraint); | ||
79 | return KMimeTypeTrader::self()->query(QStringLiteral("inode/directory"), QStringLiteral("Application")); | 95 | for (const KService::Ptr &service : fileManagers) { | ||
80 | } | 96 | combofileManager->addItem(QIcon::fromTheme(service->icon()), service->name(), service->storageId()); | ||
81 | 97 | | |||
82 | void CfgFileManager::load(KConfig *) { | 98 | if (fileManager->storageId() == service->storageId()) { | ||
83 | qDeleteAll(mDynamicRadioButtons); | 99 | combofileManager->setCurrentIndex(combofileManager->count() -1); | ||
84 | mDynamicRadioButtons.clear(); | 100 | m_currentIndex = combofileManager->count() -1; | ||
85 | const KService::List apps = appOffers(); | | |||
86 | bool first = true; | | |||
87 | for (const KService::Ptr &service : apps) { | | |||
88 | QRadioButton *button = new QRadioButton(service->name(), this); | | |||
89 | connect(button, &QRadioButton::toggled, this, &CfgFileManager::configChanged); | | |||
90 | button->setProperty("storageId", service->storageId()); | | |||
91 | radioLayout->addWidget(button); | | |||
92 | if (first) { | | |||
93 | button->setChecked(true); | | |||
94 | first = false; | | |||
95 | } | 101 | } | ||
96 | mDynamicRadioButtons << button; | 102 | if (service->storageId() == QStringLiteral("org.kde.dolphin.desktop")) { | ||
103 | m_dolphinIndex = combofileManager->count() -1; | ||||
97 | } | 104 | } | ||
105 | } | ||||
106 | | ||||
107 | // in case of a service not associated with FileManager Category | ||||
108 | if (m_currentIndex == -1 && !fileManager->storageId().isEmpty()) { | ||||
109 | const KService::Ptr service = KService::serviceByStorageId(fileManager->storageId()); | ||||
110 | | ||||
111 | combofileManager->addItem(QIcon::fromTheme(service->icon()), service->name(), service->storageId()); | ||||
112 | combofileManager->setCurrentIndex(combofileManager->count() -1); | ||||
113 | m_currentIndex = combofileManager->count() -1; | ||||
114 | } | ||||
115 | | ||||
116 | // add a other option to add a new file manager with KOpenWithDialog | ||||
meven: wrong comment | |||||
117 | combofileManager->addItem(QIcon::fromTheme(QStringLiteral("application-x-shellscript")), i18n("Other..."), QStringLiteral()); | ||||
98 | 118 | | |||
99 | emit changed(false); | 119 | emit changed(false); | ||
100 | } | 120 | } | ||
101 | 121 | | |||
102 | static const char s_DefaultApplications[] = "Default Applications"; | 122 | static const char s_DefaultApplications[] = "Default Applications"; | ||
103 | static const char s_AddedAssociations[] = "Added Associations"; | 123 | static const char s_AddedAssociations[] = "Added Associations"; | ||
104 | 124 | | |||
105 | void CfgFileManager::save(KConfig *) | 125 | void CfgFileManager::save(KConfig *) | ||
106 | { | 126 | { | ||
107 | QString storageId; | 127 | const QString storageId = combofileManager->currentData().toString(); | ||
108 | for (QRadioButton *button : qAsConst(mDynamicRadioButtons)) { | | |||
109 | if (button->isChecked()) { | | |||
110 | storageId = button->property("storageId").toString(); | | |||
111 | } | | |||
112 | } | | |||
113 | | ||||
114 | if (!storageId.isEmpty()) { | 128 | if (!storageId.isEmpty()) { | ||
129 | | ||||
130 | m_currentIndex = combofileManager->currentIndex(); | ||||
131 | | ||||
115 | // This is taken from filetypes/mimetypedata.cpp | 132 | // This is taken from filetypes/mimetypedata.cpp | ||
116 | KSharedConfig::Ptr profile = KSharedConfig::openConfig(QStringLiteral("mimeapps.list"), KConfig::NoGlobals, QStandardPaths::GenericConfigLocation); | 133 | KSharedConfig::Ptr profile = KSharedConfig::openConfig(QStringLiteral("mimeapps.list"), KConfig::NoGlobals, QStandardPaths::GenericConfigLocation); | ||
117 | if (!profile->isConfigWritable(true)) // warn user if mimeapps.list is root-owned (#155126/#94504) | 134 | if (!profile->isConfigWritable(true)) // warn user if mimeapps.list is root-owned (#155126/#94504) | ||
118 | return; | 135 | return; | ||
119 | const QString mime = QStringLiteral("inode/directory"); | | |||
120 | KConfigGroup addedApps(profile, s_AddedAssociations); | 136 | KConfigGroup addedApps(profile, s_AddedAssociations); | ||
121 | QStringList userApps = addedApps.readXdgListEntry(mime); | 137 | QStringList userApps = addedApps.readXdgListEntry(mime); | ||
122 | userApps.removeAll(storageId); // remove if present, to make it first in the list | 138 | userApps.removeAll(storageId); // remove if present, to make it first in the list | ||
123 | userApps.prepend(storageId); | 139 | userApps.prepend(storageId); | ||
124 | addedApps.writeXdgListEntry(mime, userApps); | 140 | addedApps.writeXdgListEntry(mime, userApps); | ||
125 | 141 | | |||
126 | // Save the default file manager as per mime-apps spec 1.0.1 | 142 | // Save the default file manager as per mime-apps spec 1.0.1 | ||
127 | KConfigGroup defaultApp(profile, s_DefaultApplications); | 143 | KConfigGroup defaultApp(profile, s_DefaultApplications); | ||
128 | defaultApp.writeXdgListEntry(mime, QStringList(storageId)); | 144 | defaultApp.writeXdgListEntry(mime, QStringList(storageId)); | ||
129 | 145 | | |||
130 | profile->sync(); | 146 | profile->sync(); | ||
131 | 147 | | |||
132 | KBuildSycocaProgressDialog::rebuildKSycoca(this); | 148 | KBuildSycocaProgressDialog::rebuildKSycoca(this); | ||
133 | } | | |||
134 | | ||||
135 | emit changed(false); | 149 | emit changed(false); | ||
136 | } | 150 | } | ||
137 | | ||||
138 | void CfgFileManager::slotAddFileManager() | | |||
139 | { | | |||
140 | KProcess proc; | | |||
141 | proc << QStringLiteral("keditfiletype5"); | | |||
142 | proc << QStringLiteral("inode/directory"); | | |||
143 | if (proc.execute() == 0) { | | |||
144 | load(nullptr); | | |||
145 | } | | |||
146 | } | 151 | } |
wrong comment