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 | You should have received a copy of the GNU General Public License | 15 | You should have received a copy of the GNU General Public License | ||
16 | along with this program; see the file COPYING. If not, write to | 16 | along with this program; see the file COPYING. If not, write to | ||
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 <QDebug> | | |||
24 | #include <kprocess.h> | 23 | #include <kprocess.h> | ||
25 | #include <kmimetypetrader.h> | 24 | #include <kmimetypetrader.h> | ||
26 | #include <kopenwithdialog.h> | 25 | #include <kopenwithdialog.h> | ||
27 | #include <kconfiggroup.h> | 26 | #include <kconfiggroup.h> | ||
28 | #include <QStandardPaths> | 27 | #include <QStandardPaths> | ||
29 | #include <QFileInfo> | 28 | #include <KSharedConfig> | ||
30 | 29 | | |||
31 | #include "../migrationlib/kdelibs4config.h" | 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 | } | ||||
32 | 44 | | |||
33 | CfgFileManager::CfgFileManager(QWidget *parent) | 45 | CfgFileManager::CfgFileManager(QWidget *parent) | ||
34 | : QWidget(parent), Ui::FileManagerConfig_UI(),CfgPlugin() | 46 | : QWidget(parent), Ui::FileManagerConfig_UI(),CfgPlugin() | ||
35 | { | 47 | { | ||
36 | setupUi(this); | 48 | setupUi(this); | ||
37 | connect(btnSelectFileManager, &QToolButton::clicked, this, &CfgFileManager::slotAddFileManager); | 49 | connect(btnSelectFileManager, &QToolButton::clicked, this, &CfgFileManager::slotAddFileManager); | ||
38 | } | 50 | } | ||
39 | 51 | | |||
40 | CfgFileManager::~CfgFileManager() { | 52 | CfgFileManager::~CfgFileManager() { | ||
41 | } | 53 | } | ||
42 | 54 | | |||
43 | void CfgFileManager::configChanged() | 55 | void CfgFileManager::configChanged() | ||
44 | { | 56 | { | ||
45 | emit changed(true); | 57 | emit changed(true); | ||
46 | } | 58 | } | ||
47 | 59 | | |||
48 | void CfgFileManager::defaults() | 60 | void CfgFileManager::defaults() | ||
49 | { | 61 | { | ||
50 | load(nullptr); | 62 | load(nullptr); | ||
63 | | ||||
64 | const auto radio = ::findDolphinRadio(mDynamicRadioButtons); | ||||
65 | if (radio) { | ||||
66 | radio->setChecked(true); | ||||
67 | } | ||||
68 | } | ||||
69 | | ||||
70 | bool CfgFileManager::isDefaults() const | ||||
71 | { | ||||
72 | const auto dolphinRadio = ::findDolphinRadio(mDynamicRadioButtons); | ||||
73 | // When dolphin is not present, we can't assume any default value | ||||
74 | return !dolphinRadio || dolphinRadio->isChecked(); | ||||
51 | } | 75 | } | ||
52 | 76 | | |||
53 | static KService::List appOffers() | 77 | static KService::List appOffers() | ||
54 | { | 78 | { | ||
55 | return KMimeTypeTrader::self()->query(QStringLiteral("inode/directory"), QStringLiteral("Application")); | 79 | return KMimeTypeTrader::self()->query(QStringLiteral("inode/directory"), QStringLiteral("Application")); | ||
56 | } | 80 | } | ||
57 | 81 | | |||
58 | void CfgFileManager::load(KConfig *) { | 82 | void CfgFileManager::load(KConfig *) { | ||
59 | qDeleteAll(mDynamicWidgets); | 83 | qDeleteAll(mDynamicRadioButtons); | ||
60 | mDynamicWidgets.clear(); | 84 | mDynamicRadioButtons.clear(); | ||
61 | const KService::List apps = appOffers(); | 85 | const KService::List apps = appOffers(); | ||
62 | bool first = true; | 86 | bool first = true; | ||
63 | Q_FOREACH(const KService::Ptr& service, apps) | 87 | for (const KService::Ptr &service : apps) { | ||
64 | { | | |||
65 | QRadioButton* button = new QRadioButton(service->name(), this); | 88 | QRadioButton *button = new QRadioButton(service->name(), this); | ||
66 | connect(button, &QRadioButton::toggled, this, &CfgFileManager::configChanged); | 89 | connect(button, &QRadioButton::toggled, this, &CfgFileManager::configChanged); | ||
67 | button->setProperty("storageId", service->storageId()); | 90 | button->setProperty("storageId", service->storageId()); | ||
68 | radioLayout->addWidget(button); | 91 | radioLayout->addWidget(button); | ||
69 | if (first) { | 92 | if (first) { | ||
70 | button->setChecked(true); | 93 | button->setChecked(true); | ||
71 | first = false; | 94 | first = false; | ||
72 | } | 95 | } | ||
73 | mDynamicWidgets << button; | 96 | mDynamicRadioButtons << button; | ||
74 | } | 97 | } | ||
75 | 98 | | |||
76 | emit changed(false); | 99 | emit changed(false); | ||
77 | } | 100 | } | ||
78 | 101 | | |||
79 | static const char s_DefaultApplications[] = "Default Applications"; | 102 | static const char s_DefaultApplications[] = "Default Applications"; | ||
80 | static const char s_AddedAssociations[] = "Added Associations"; | 103 | static const char s_AddedAssociations[] = "Added Associations"; | ||
81 | static const char s_RemovedAssociations[] = "Removed Associations"; | | |||
82 | 104 | | |||
83 | void CfgFileManager::save(KConfig *) | 105 | void CfgFileManager::save(KConfig *) | ||
84 | { | 106 | { | ||
85 | QString storageId; | 107 | QString storageId; | ||
86 | Q_FOREACH(QRadioButton* button, qFindChildren<QRadioButton*>(this)) { | 108 | for (QRadioButton *button : qAsConst(mDynamicRadioButtons)) { | ||
87 | if (button->isChecked()) { | 109 | if (button->isChecked()) { | ||
88 | storageId = button->property("storageId").toString(); | 110 | storageId = button->property("storageId").toString(); | ||
89 | } | 111 | } | ||
90 | } | 112 | } | ||
91 | 113 | | |||
92 | qDebug() << storageId; | | |||
93 | if (!storageId.isEmpty()) { | 114 | if (!storageId.isEmpty()) { | ||
94 | // This is taken from filetypes/mimetypedata.cpp | 115 | // This is taken from filetypes/mimetypedata.cpp | ||
95 | KSharedConfig::Ptr profile = KSharedConfig::openConfig(QStringLiteral("mimeapps.list"), KConfig::NoGlobals, QStandardPaths::GenericConfigLocation); | 116 | KSharedConfig::Ptr profile = KSharedConfig::openConfig(QStringLiteral("mimeapps.list"), KConfig::NoGlobals, QStandardPaths::GenericConfigLocation); | ||
96 | if (!profile->isConfigWritable(true)) // warn user if mimeapps.list is root-owned (#155126/#94504) | 117 | if (!profile->isConfigWritable(true)) // warn user if mimeapps.list is root-owned (#155126/#94504) | ||
97 | return; | 118 | return; | ||
98 | const QString mime = QStringLiteral("inode/directory"); | 119 | const QString mime = QStringLiteral("inode/directory"); | ||
99 | KConfigGroup addedApps(profile, s_AddedAssociations); | 120 | KConfigGroup addedApps(profile, s_AddedAssociations); | ||
100 | QStringList userApps = addedApps.readXdgListEntry(mime); | 121 | QStringList userApps = addedApps.readXdgListEntry(mime); | ||
101 | userApps.removeAll(storageId); // remove if present, to make it first in the list | 122 | userApps.removeAll(storageId); // remove if present, to make it first in the list | ||
102 | userApps.prepend(storageId); | 123 | userApps.prepend(storageId); | ||
103 | addedApps.writeXdgListEntry(mime, userApps); | 124 | addedApps.writeXdgListEntry(mime, userApps); | ||
104 | 125 | | |||
105 | // Save the default file manager as per mime-apps spec 1.0.1 | 126 | // Save the default file manager as per mime-apps spec 1.0.1 | ||
106 | KConfigGroup defaultApp(profile, s_DefaultApplications); | 127 | KConfigGroup defaultApp(profile, s_DefaultApplications); | ||
107 | defaultApp.writeXdgListEntry(mime, QStringList(storageId)); | 128 | defaultApp.writeXdgListEntry(mime, QStringList(storageId)); | ||
108 | 129 | | |||
109 | Kdelibs4SharedConfig::syncConfigGroup(QLatin1String("Added Associations"), QStringLiteral("mimeapps.list")); | | |||
110 | | ||||
111 | profile->sync(); | 130 | profile->sync(); | ||
112 | 131 | | |||
113 | // Clean out any kde-mimeapps.list which would take precedence any cancel our changes. | | |||
114 | // (also taken from filetypes/mimetypedata.cpp) | | |||
115 | const QString desktops = QString::fromLocal8Bit(qgetenv("XDG_CURRENT_DESKTOP")); | | |||
116 | foreach (const QString &desktop, desktops.split(":", QString::SkipEmptyParts)) { | | |||
117 | const QString file = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) | | |||
118 | + QLatin1Char('/') + desktop.toLower() + QLatin1String("-mimeapps.list"); | | |||
119 | if (QFileInfo::exists(file)) { | | |||
120 | qDebug() << "Cleaning up" << file; | | |||
121 | KConfig conf(file, KConfig::NoGlobals); | | |||
122 | KConfigGroup(&conf, s_DefaultApplications).deleteEntry(mime); | | |||
123 | KConfigGroup(&conf, s_AddedAssociations).deleteEntry(mime); | | |||
124 | KConfigGroup(&conf, s_RemovedAssociations).deleteEntry(mime); | | |||
125 | } | | |||
126 | } | | |||
127 | | ||||
128 | KBuildSycocaProgressDialog::rebuildKSycoca(this); | 132 | KBuildSycocaProgressDialog::rebuildKSycoca(this); | ||
129 | } | 133 | } | ||
130 | 134 | | |||
131 | emit changed(false); | 135 | emit changed(false); | ||
132 | } | 136 | } | ||
133 | 137 | | |||
134 | void CfgFileManager::slotAddFileManager() | 138 | void CfgFileManager::slotAddFileManager() | ||
135 | { | 139 | { | ||
136 | KProcess proc; | 140 | KProcess proc; | ||
137 | proc << QStringLiteral("keditfiletype5"); | 141 | proc << QStringLiteral("keditfiletype5"); | ||
138 | proc << QStringLiteral("inode/directory"); | 142 | proc << QStringLiteral("inode/directory"); | ||
139 | if (proc.execute() == 0) { | 143 | if (proc.execute() == 0) { | ||
140 | load(nullptr); | 144 | load(nullptr); | ||
141 | } | 145 | } | ||
142 | } | 146 | } |