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