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 | : QComboBox(parent),CfgPlugin() | ||
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() | 40 | void CfgFileManager::defaults() | ||
43 | { | 41 | { | ||
44 | if (m_dolphinIndex != -1) { | 42 | if (m_dolphinIndex != -1) { | ||
45 | combofileManager->setCurrentIndex(m_dolphinIndex); | 43 | setCurrentIndex(m_dolphinIndex); | ||
46 | } | 44 | } | ||
47 | } | 45 | } | ||
48 | 46 | | |||
49 | bool CfgFileManager::isDefaults() const | 47 | bool CfgFileManager::isDefaults() const | ||
50 | { | 48 | { | ||
51 | return m_dolphinIndex == -1 || m_dolphinIndex == combofileManager->currentIndex(); | 49 | return m_dolphinIndex == -1 || m_dolphinIndex == currentIndex(); | ||
50 | } | ||||
51 | | ||||
52 | bool CfgFileManager::hasChanged() const | ||||
53 | { | ||||
54 | return m_currentIndex != -1 && m_currentIndex != currentIndex(); | ||||
broulik: This seems to be the same in all classes. Since now all of them are just `QComboBox`es, would… | |||||
52 | } | 55 | } | ||
53 | 56 | | |||
54 | void CfgFileManager::selectFileManager(int index) | 57 | void CfgFileManager::selectFileManager(int index) | ||
55 | { | 58 | { | ||
56 | if (index == combofileManager->count() -1) { | 59 | if (index == count() -1) { | ||
57 | 60 | | |||
58 | KOpenWithDialog dlg({}, i18n("Select preferred file manager:"), QString(), this); | 61 | KOpenWithDialog dlg({}, i18n("Select preferred file manager:"), QString(), this); | ||
59 | dlg.setSaveNewApplications(true); | 62 | dlg.setSaveNewApplications(true); | ||
60 | if (dlg.exec() != QDialog::Accepted) { | 63 | if (dlg.exec() != QDialog::Accepted) { | ||
61 | combofileManager->setCurrentIndex(m_currentIndex); | 64 | setCurrentIndex(m_currentIndex); | ||
62 | return; | 65 | return; | ||
63 | } | 66 | } | ||
64 | 67 | | |||
65 | const auto service = dlg.service(); | 68 | const auto service = dlg.service(); | ||
66 | 69 | | |||
67 | // if the selected service is already in the list | 70 | // 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()); | 71 | const auto matching = model()->match(model()->index(0,0), Qt::UserRole, service->storageId()); | ||
69 | if (!matching.isEmpty()) { | 72 | if (!matching.isEmpty()) { | ||
70 | const int index = matching.at(0).row(); | 73 | const int index = matching.at(0).row(); | ||
71 | combofileManager->setCurrentIndex(index); | 74 | setCurrentIndex(index); | ||
72 | changed(index != m_currentIndex); | 75 | changed(index != m_currentIndex); | ||
73 | } else { | 76 | } else { | ||
74 | const QString icon = !service->icon().isEmpty() ? service->icon() : QStringLiteral("application-x-shellscript"); | 77 | 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()); | 78 | insertItem(count() -1, QIcon::fromTheme(icon), service->name(), service->storageId()); | ||
76 | combofileManager->setCurrentIndex(combofileManager->count() - 2); | 79 | setCurrentIndex(count() - 2); | ||
77 | 80 | | |||
78 | changed(true); | 81 | changed(true); | ||
79 | } | 82 | } | ||
80 | } else { | 83 | } else { | ||
81 | changed(index != m_currentIndex); | 84 | changed(index != m_currentIndex); | ||
82 | } | 85 | } | ||
83 | } | 86 | } | ||
84 | 87 | | |||
85 | static const QString mime = QStringLiteral("inode/directory"); | 88 | static const QString mime = QStringLiteral("inode/directory"); | ||
86 | 89 | | |||
87 | void CfgFileManager::load(KConfig *) | 90 | void CfgFileManager::load(KConfig *) | ||
88 | { | 91 | { | ||
89 | combofileManager->clear(); | 92 | clear(); | ||
90 | m_currentIndex = -1; | 93 | m_currentIndex = -1; | ||
91 | 94 | | |||
92 | const KService::Ptr fileManager = KMimeTypeTrader::self()->preferredService(mime); | 95 | const KService::Ptr fileManager = KMimeTypeTrader::self()->preferredService(mime); | ||
93 | 96 | | |||
94 | const auto constraint = QStringLiteral("'FileManager' in Categories and 'inode/directory' in ServiceTypes"); | 97 | const auto constraint = QStringLiteral("'FileManager' in Categories and 'inode/directory' in ServiceTypes"); | ||
95 | const KService::List fileManagers = KServiceTypeTrader::self()->query(QStringLiteral("Application"), constraint); | 98 | const KService::List fileManagers = KServiceTypeTrader::self()->query(QStringLiteral("Application"), constraint); | ||
96 | for (const KService::Ptr &service : fileManagers) { | 99 | for (const KService::Ptr &service : fileManagers) { | ||
97 | combofileManager->addItem(QIcon::fromTheme(service->icon()), service->name(), service->storageId()); | 100 | addItem(QIcon::fromTheme(service->icon()), service->name(), service->storageId()); | ||
98 | 101 | | |||
99 | if (fileManager->storageId() == service->storageId()) { | 102 | if (fileManager->storageId() == service->storageId()) { | ||
100 | combofileManager->setCurrentIndex(combofileManager->count() -1); | 103 | setCurrentIndex(count() -1); | ||
101 | m_currentIndex = combofileManager->count() -1; | 104 | m_currentIndex = count() -1; | ||
102 | } | 105 | } | ||
103 | if (service->storageId() == QStringLiteral("org.kde.dolphin.desktop")) { | 106 | if (service->storageId() == QStringLiteral("org.kde.dolphin.desktop")) { | ||
104 | m_dolphinIndex = combofileManager->count() -1; | 107 | m_dolphinIndex = count() -1; | ||
105 | } | 108 | } | ||
106 | } | 109 | } | ||
107 | 110 | | |||
108 | // in case of a service not associated with FileManager Category | 111 | // in case of a service not associated with FileManager Category | ||
109 | if (m_currentIndex == -1 && !fileManager->storageId().isEmpty()) { | 112 | if (m_currentIndex == -1 && !fileManager->storageId().isEmpty()) { | ||
110 | const KService::Ptr service = KService::serviceByStorageId(fileManager->storageId()); | 113 | const KService::Ptr service = KService::serviceByStorageId(fileManager->storageId()); | ||
111 | 114 | | |||
112 | const QString icon = !service->icon().isEmpty() ? service->icon() : QStringLiteral("application-x-shellscript"); | 115 | const QString icon = !service->icon().isEmpty() ? service->icon() : QStringLiteral("application-x-shellscript"); | ||
113 | combofileManager->addItem(QIcon::fromTheme(icon), service->name(), service->storageId()); | 116 | addItem(QIcon::fromTheme(icon), service->name(), service->storageId()); | ||
114 | combofileManager->setCurrentIndex(combofileManager->count() -1); | 117 | setCurrentIndex(count() -1); | ||
115 | m_currentIndex = combofileManager->count() -1; | 118 | m_currentIndex = count() -1; | ||
116 | } | 119 | } | ||
117 | 120 | | |||
118 | // add a other option to add a new file manager with KOpenWithDialog | 121 | // add a other option to add a new file manager with KOpenWithDialog | ||
119 | combofileManager->addItem(QIcon::fromTheme(QStringLiteral("application-x-shellscript")), i18n("Other..."), QStringLiteral()); | 122 | addItem(QIcon::fromTheme(QStringLiteral("application-x-shellscript")), i18n("Other..."), QStringLiteral()); | ||
120 | 123 | | |||
121 | emit changed(false); | 124 | emit changed(false); | ||
122 | } | 125 | } | ||
123 | 126 | | |||
124 | static const char s_DefaultApplications[] = "Default Applications"; | 127 | static const char s_DefaultApplications[] = "Default Applications"; | ||
125 | static const char s_AddedAssociations[] = "Added Associations"; | 128 | static const char s_AddedAssociations[] = "Added Associations"; | ||
126 | 129 | | |||
127 | void CfgFileManager::save(KConfig *) | 130 | void CfgFileManager::save(KConfig *) | ||
128 | { | 131 | { | ||
129 | const QString storageId = combofileManager->currentData().toString(); | 132 | const QString storageId = currentData().toString(); | ||
130 | if (!storageId.isEmpty()) { | 133 | if (!storageId.isEmpty()) { | ||
131 | 134 | m_currentIndex = currentIndex(); | |||
132 | m_currentIndex = combofileManager->currentIndex(); | | |||
133 | 135 | | |||
134 | // This is taken from filetypes/mimetypedata.cpp | 136 | // This is taken from filetypes/mimetypedata.cpp | ||
135 | KSharedConfig::Ptr profile = KSharedConfig::openConfig(QStringLiteral("mimeapps.list"), KConfig::NoGlobals, QStandardPaths::GenericConfigLocation); | 137 | 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) | 138 | if (!profile->isConfigWritable(true)) // warn user if mimeapps.list is root-owned (#155126/#94504) | ||
137 | return; | 139 | return; | ||
138 | KConfigGroup addedApps(profile, s_AddedAssociations); | 140 | KConfigGroup addedApps(profile, s_AddedAssociations); | ||
139 | QStringList userApps = addedApps.readXdgListEntry(mime); | 141 | QStringList userApps = addedApps.readXdgListEntry(mime); | ||
140 | userApps.removeAll(storageId); // remove if present, to make it first in the list | 142 | userApps.removeAll(storageId); // remove if present, to make it first in the list | ||
141 | userApps.prepend(storageId); | 143 | userApps.prepend(storageId); | ||
142 | addedApps.writeXdgListEntry(mime, userApps); | 144 | addedApps.writeXdgListEntry(mime, userApps); | ||
143 | 145 | | |||
144 | // Save the default file manager as per mime-apps spec 1.0.1 | 146 | // Save the default file manager as per mime-apps spec 1.0.1 | ||
145 | KConfigGroup defaultApp(profile, s_DefaultApplications); | 147 | KConfigGroup defaultApp(profile, s_DefaultApplications); | ||
146 | defaultApp.writeXdgListEntry(mime, QStringList(storageId)); | 148 | defaultApp.writeXdgListEntry(mime, QStringList(storageId)); | ||
147 | 149 | | |||
148 | profile->sync(); | 150 | profile->sync(); | ||
149 | 151 | | |||
150 | KBuildSycocaProgressDialog::rebuildKSycoca(this); | | |||
151 | emit changed(false); | 152 | emit changed(false); | ||
152 | } | 153 | } | ||
153 | } | 154 | } |
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?