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 | } | ||||
63 | 52 | | |||
64 | const auto radio = ::findDolphinRadio(mDynamicRadioButtons); | 53 | void CfgFileManager::selectFileManager(int index) | ||
65 | if (radio) { | 54 | { | ||
66 | radio->setChecked(true); | 55 | if (index == combofileManager->count() -1) { | ||
56 | KProcess proc; | ||||
57 | proc << QStringLiteral("keditfiletype5"); | ||||
58 | proc << QStringLiteral("inode/directory"); | ||||
59 | if (proc.execute() == 0) { | ||||
60 | load(nullptr); | ||||
61 | } | ||||
62 | } else { | ||||
63 | changed(index != m_currentIndex); | ||||
67 | } | 64 | } | ||
68 | } | 65 | } | ||
69 | 66 | | |||
70 | bool CfgFileManager::isDefaults() const | 67 | void CfgFileManager::load(KConfig *) | ||
71 | { | 68 | { | ||
72 | const auto dolphinRadio = ::findDolphinRadio(mDynamicRadioButtons); | 69 | combofileManager->clear(); | ||
73 | // When dolphin is not present, we can't assume any default value | 70 | m_currentIndex = -1; | ||
74 | return !dolphinRadio || dolphinRadio->isChecked(); | 71 | | ||
75 | } | 72 | const KService::Ptr fileManager = KMimeTypeTrader::self()->preferredService(QStringLiteral("inode/directory")); | ||
76 | 73 | | |||
77 | static KService::List appOffers() | 74 | const auto constraint = QStringLiteral("'FileManager' in Categories and 'inode/directory' in ServiceTypes"); | ||
78 | { | 75 | const KService::List fileManagers = KServiceTypeTrader::self()->query(QStringLiteral("Application"), constraint); | ||
79 | return KMimeTypeTrader::self()->query(QStringLiteral("inode/directory"), QStringLiteral("Application")); | 76 | for (const KService::Ptr &service : fileManagers) { | ||
80 | } | 77 | combofileManager->addItem(QIcon::fromTheme(service->icon()), service->name(), service->storageId()); | ||
81 | 78 | | |||
82 | void CfgFileManager::load(KConfig *) { | 79 | if (fileManager->storageId() == service->storageId()) { | ||
83 | qDeleteAll(mDynamicRadioButtons); | 80 | combofileManager->setCurrentIndex(combofileManager->count() -1); | ||
84 | mDynamicRadioButtons.clear(); | 81 | 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 | } | 82 | } | ||
96 | mDynamicRadioButtons << button; | 83 | if (service->storageId() == QStringLiteral("org.kde.dolphin.desktop")) { | ||
84 | m_dolphinIndex = combofileManager->count() -1; | ||||
97 | } | 85 | } | ||
86 | } | ||||
87 | | ||||
88 | // in case the user a service not associated with FileManager Category | ||||
89 | if (m_currentIndex == -1 && !fileManager->storageId().isEmpty()) { | ||||
90 | const KService::Ptr service = KService::serviceByStorageId(fileManager->storageId()); | ||||
91 | | ||||
92 | combofileManager->addItem(QIcon::fromTheme(service->icon()), service->name(), service->storageId()); | ||||
93 | combofileManager->setCurrentIndex(combofileManager->count() -1); | ||||
94 | m_currentIndex = combofileManager->count() -1; | ||||
95 | } | ||||
96 | | ||||
97 | // add a other option to add a new file manager with keditfiletype5 | ||||
meven: wrong comment | |||||
98 | combofileManager->addItem(QIcon::fromTheme(QStringLiteral("application-x-shellscript")), i18n("Other..."), QStringLiteral()); | ||||
98 | 99 | | |||
99 | emit changed(false); | 100 | emit changed(false); | ||
100 | } | 101 | } | ||
101 | 102 | | |||
102 | static const char s_DefaultApplications[] = "Default Applications"; | 103 | static const char s_DefaultApplications[] = "Default Applications"; | ||
103 | static const char s_AddedAssociations[] = "Added Associations"; | 104 | static const char s_AddedAssociations[] = "Added Associations"; | ||
104 | 105 | | |||
105 | void CfgFileManager::save(KConfig *) | 106 | void CfgFileManager::save(KConfig *) | ||
106 | { | 107 | { | ||
107 | QString storageId; | 108 | 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()) { | 109 | if (!storageId.isEmpty()) { | ||
115 | // This is taken from filetypes/mimetypedata.cpp | 110 | // This is taken from filetypes/mimetypedata.cpp | ||
116 | KSharedConfig::Ptr profile = KSharedConfig::openConfig(QStringLiteral("mimeapps.list"), KConfig::NoGlobals, QStandardPaths::GenericConfigLocation); | 111 | 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) | 112 | if (!profile->isConfigWritable(true)) // warn user if mimeapps.list is root-owned (#155126/#94504) | ||
118 | return; | 113 | return; | ||
119 | const QString mime = QStringLiteral("inode/directory"); | 114 | const QString mime = QStringLiteral("inode/directory"); | ||
120 | KConfigGroup addedApps(profile, s_AddedAssociations); | 115 | KConfigGroup addedApps(profile, s_AddedAssociations); | ||
121 | QStringList userApps = addedApps.readXdgListEntry(mime); | 116 | QStringList userApps = addedApps.readXdgListEntry(mime); | ||
122 | userApps.removeAll(storageId); // remove if present, to make it first in the list | 117 | userApps.removeAll(storageId); // remove if present, to make it first in the list | ||
123 | userApps.prepend(storageId); | 118 | userApps.prepend(storageId); | ||
124 | addedApps.writeXdgListEntry(mime, userApps); | 119 | addedApps.writeXdgListEntry(mime, userApps); | ||
125 | 120 | | |||
126 | // Save the default file manager as per mime-apps spec 1.0.1 | 121 | // Save the default file manager as per mime-apps spec 1.0.1 | ||
127 | KConfigGroup defaultApp(profile, s_DefaultApplications); | 122 | KConfigGroup defaultApp(profile, s_DefaultApplications); | ||
128 | defaultApp.writeXdgListEntry(mime, QStringList(storageId)); | 123 | defaultApp.writeXdgListEntry(mime, QStringList(storageId)); | ||
129 | 124 | | |||
130 | profile->sync(); | 125 | profile->sync(); | ||
131 | 126 | | |||
132 | KBuildSycocaProgressDialog::rebuildKSycoca(this); | 127 | KBuildSycocaProgressDialog::rebuildKSycoca(this); | ||
133 | } | | |||
134 | | ||||
135 | emit changed(false); | 128 | emit changed(false); | ||
136 | } | 129 | } | ||
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 | } | 130 | } |
wrong comment