1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
|
/* This file is part of the KDE project
SPDX-FileCopyrightText: 2003 Waldo Bastian <bastian@kde.org>
SPDX-FileCopyrightText: 2003 David Faure <faure@kde.org>
SPDX-FileCopyrightText: 2002 Daniel Molkentin <molkentin@kde.org>
SPDX-License-Identifier: GPL-2.0-only
*/
// Own
#include "kservicelistwidget.h"
// Qt
#include <QHBoxLayout>
#include <QPushButton>
#include <QStandardPaths>
#include <QVBoxLayout>
// KDE
#include <KLocalizedString>
#include <KOpenWithDialog>
#include <KPropertiesDialog>
// Local
#include "kserviceselectdlg.h"
#include "mimetypedata.h"
KServiceListItem::KServiceListItem(const KService::Ptr &pService)
: QListWidgetItem()
, storageId(pService->storageId())
, desktopPath(pService->entryPath())
{
setText(pService->name());
setIcon(QIcon::fromTheme(pService->icon()));
if (!pService->isApplication()) {
localPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/kservices6/") + desktopPath;
} else {
localPath = pService->locateLocal();
}
}
PluginListItem::PluginListItem(const KPluginMetaData &data)
: QListWidgetItem()
, metaData(data)
{
setText(i18n("%1 (%2)", metaData.name(), metaData.pluginId()));
setIcon(QIcon::fromTheme(metaData.iconName()));
}
KServiceListWidget::KServiceListWidget(Kind kind, QWidget *parent)
: QGroupBox(kind == SERVICELIST_APPLICATIONS ? i18n("Application Preference Order") : i18n("Services Preference Order"), parent)
, m_kind(kind)
, m_mimeTypeData(nullptr)
, m_allowMultiApply(false)
{
QHBoxLayout *lay = new QHBoxLayout(this);
servicesLB = new QListWidget(this);
connect(servicesLB, &QListWidget::itemSelectionChanged, this, &KServiceListWidget::enableMoveButtons);
lay->addWidget(servicesLB);
connect(servicesLB, &QListWidget::itemDoubleClicked, this, &KServiceListWidget::editService);
QString wtstr = (kind == SERVICELIST_APPLICATIONS ? i18n("This is a list of applications associated with files of the selected"
" file type. This list is shown in Konqueror's context menus when you select"
" \"Open With...\". If more than one application is associated with this file type,"
" then the list is ordered by priority with the uppermost item taking precedence"
" over the others.")
: i18n("This is a list of services associated with files of the selected"
" file type. This list is shown in Konqueror's context menus when you select"
" a \"Preview with...\" option. If more than one service is associated with this file type,"
" then the list is ordered by priority with the uppermost item taking precedence"
" over the others."));
setWhatsThis(wtstr);
servicesLB->setWhatsThis(wtstr);
QVBoxLayout *btnsLay = new QVBoxLayout();
lay->addLayout(btnsLay);
servUpButton = new QPushButton(i18n("Move &Up"), this);
servUpButton->setIcon(QIcon::fromTheme(QStringLiteral("arrow-up")));
servUpButton->setEnabled(false);
connect(servUpButton, &QAbstractButton::clicked, this, &KServiceListWidget::promoteService);
btnsLay->addWidget(servUpButton);
servUpButton->setWhatsThis(kind == SERVICELIST_APPLICATIONS ? i18n("Assigns a higher priority to the selected\n"
"application, moving it up in the list. Note: This\n"
"only affects the selected application if the file type is\n"
"associated with more than one application.")
: i18n("Assigns a higher priority to the selected\n"
"service, moving it up in the list."));
servDownButton = new QPushButton(i18n("Move &Down"), this);
servDownButton->setIcon(QIcon::fromTheme(QStringLiteral("arrow-down")));
servDownButton->setEnabled(false);
connect(servDownButton, &QAbstractButton::clicked, this, &KServiceListWidget::demoteService);
btnsLay->addWidget(servDownButton);
servDownButton->setWhatsThis(kind == SERVICELIST_APPLICATIONS ? i18n("Assigns a lower priority to the selected\n"
"application, moving it down in the list. Note: This \n"
"only affects the selected application if the file type is\n"
"associated with more than one application.")
: i18n("Assigns a lower priority to the selected\n"
"service, moving it down in the list."));
servNewButton = new QPushButton(i18n("Add..."), this);
servNewButton->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
servNewButton->setEnabled(false);
connect(servNewButton, &QAbstractButton::clicked, this, &KServiceListWidget::addService);
btnsLay->addWidget(servNewButton);
servNewButton->setWhatsThis(i18n("Add a new application for this file type."));
servEditButton = new QPushButton(i18n("Edit..."), this);
servEditButton->setIcon(QIcon::fromTheme(QStringLiteral("edit-rename")));
servEditButton->setEnabled(false);
connect(servEditButton, &QAbstractButton::clicked, this, &KServiceListWidget::editService);
btnsLay->addWidget(servEditButton);
servEditButton->setWhatsThis(i18n("Edit command line of the selected application."));
servRemoveButton = new QPushButton(i18n("Remove"), this);
servRemoveButton->setIcon(QIcon::fromTheme(QStringLiteral("list-remove")));
servRemoveButton->setEnabled(false);
connect(servRemoveButton, &QAbstractButton::clicked, this, &KServiceListWidget::removeService);
btnsLay->addWidget(servRemoveButton);
servRemoveButton->setWhatsThis(i18n("Remove the selected application from the list."));
servApplyToButton = new QPushButton(i18n("Apply To..."), this);
servApplyToButton->setIcon(QIcon::fromTheme(QStringLiteral("edit-copy")));
servApplyToButton->setEnabled(false);
connect(servApplyToButton, &QAbstractButton::clicked, this, &KServiceListWidget::applyTo);
btnsLay->addWidget(servApplyToButton);
servApplyToButton->setWhatsThis(i18n("Apply the current preference order to other file types."));
btnsLay->addStretch(1);
}
void KServiceListWidget::setMimeTypeData(MimeTypeData *mimeTypeData)
{
m_mimeTypeData = mimeTypeData;
if (servNewButton) {
servNewButton->setEnabled(true);
}
// will need a selection
servUpButton->setEnabled(false);
servDownButton->setEnabled(false);
servicesLB->clear();
servicesLB->setEnabled(false);
if (m_mimeTypeData) {
if (m_kind == SERVICELIST_APPLICATIONS) {
const QStringList services = m_mimeTypeData->appServices();
if (services.isEmpty()) {
if (m_kind == SERVICELIST_APPLICATIONS) {
servicesLB->addItem(i18nc("No applications associated with this file type", "None"));
}
} else {
for (const QString &service : services) {
if (KService::Ptr pService = KService::serviceByStorageId(service)) {
servicesLB->addItem(new KServiceListItem(pService));
}
}
servicesLB->setEnabled(true);
}
} else {
const QStringList parts = m_mimeTypeData->embedParts();
if (parts.isEmpty()) {
servicesLB->addItem(new QListWidgetItem(i18nc("No components associated with this file type", "None"), nullptr, -1));
} else {
servicesLB->setEnabled(true);
for (const QString &partId : parts) {
if (KPluginMetaData data(QStringLiteral("kf6/parts/") + partId); data.isValid()) {
servicesLB->addItem(new PluginListItem(data));
}
}
}
}
}
if (servRemoveButton) {
servRemoveButton->setEnabled(servicesLB->currentRow() > -1);
}
if (servEditButton) {
servEditButton->setEnabled(servicesLB->currentRow() > -1);
}
if (servApplyToButton) {
servApplyToButton->setEnabled(m_allowMultiApply);
}
}
void KServiceListWidget::allowMultiApply(bool allow)
{
m_allowMultiApply = allow;
if (m_mimeTypeData && servApplyToButton) {
servApplyToButton->setEnabled(m_allowMultiApply);
}
}
void KServiceListWidget::promoteService()
{
if (!servicesLB->isEnabled()) {
return;
}
int selIndex = servicesLB->currentRow();
if (selIndex == 0) {
return;
}
QListWidgetItem *selItem = servicesLB->item(selIndex);
servicesLB->takeItem(selIndex);
servicesLB->insertItem(selIndex - 1, selItem);
servicesLB->setCurrentRow(selIndex - 1);
updatePreferredServices();
Q_EMIT changed(true);
}
void KServiceListWidget::demoteService()
{
if (!servicesLB->isEnabled()) {
return;
}
int selIndex = servicesLB->currentRow();
if (selIndex == servicesLB->count() - 1) {
return;
}
QListWidgetItem *selItem = servicesLB->item(selIndex);
servicesLB->takeItem(selIndex);
servicesLB->insertItem(selIndex + 1, selItem);
servicesLB->setCurrentRow(selIndex + 1);
updatePreferredServices();
Q_EMIT changed(true);
}
void KServiceListWidget::addService()
{
if (!m_mimeTypeData) {
return;
}
if (m_kind == SERVICELIST_APPLICATIONS) {
KOpenWithDialog dlg(m_mimeTypeData->name(), QString(), this);
dlg.setSaveNewApplications(true);
if (dlg.exec() != QDialog::Accepted) {
return;
}
KService::Ptr service = dlg.service();
Q_ASSERT(service);
if (!service) {
return; // Don't crash if KOpenWith wasn't able to create service.
}
if (m_mimeTypeData->appServices().contains(service->storageId())) {
return;
}
servicesLB->insertItem(0, new KServiceListItem(service));
} else {
KPartSelectDlg dlg(this);
if (dlg.exec() != QDialog::Accepted) {
return;
}
const bool valid = dlg.chosenPart().isValid();
Q_ASSERT(valid);
if (m_mimeTypeData->embedParts().contains(dlg.chosenPart().pluginId())) {
return;
}
auto item = new PluginListItem(dlg.chosenPart());
servicesLB->insertItem(0, item);
}
// We inserted a new service at the beginning, if we had a None entry before, it must
// be the second on in the element. Check the type to be sure of it
if (servicesLB->count() > 0 && servicesLB->item(1)->type() == -1) {
delete servicesLB->takeItem(1);
servicesLB->setEnabled(true);
}
servicesLB->setCurrentItem(nullptr);
updatePreferredServices();
Q_EMIT changed(true);
}
void KServiceListWidget::editService()
{
if (!m_mimeTypeData) {
return;
}
const int selected = servicesLB->currentRow();
if (selected < 0) {
return;
}
// Only edit applications, not services as
// they don't have any parameters
if (m_kind != SERVICELIST_APPLICATIONS) {
return;
}
// Just like popping up an add dialog except that we
// pass the current command line as a default
KServiceListItem *selItem = (KServiceListItem *)servicesLB->item(selected);
const QString desktopPath = selItem->desktopPath;
KService::Ptr service = KService::serviceByDesktopPath(desktopPath);
if (!service) {
return;
}
QString path = service->entryPath();
{
// If the path to the desktop file is relative, try to get the full
// path from QStandardPaths.
QString fullPath = QStandardPaths::locate(QStandardPaths::ApplicationsLocation, path);
if (!fullPath.isEmpty()) {
path = fullPath;
}
}
KFileItem item(QUrl::fromLocalFile(path), QStringLiteral("application/x-desktop"), KFileItem::Unknown);
KPropertiesDialog dlg(item, this);
if (dlg.exec() != QDialog::Accepted) {
return;
}
// Note that at this point, ksycoca has been updated,
// and setMimeTypeData has been called again, so all the items have been recreated.
// Reload service
service = KService::serviceByDesktopPath(desktopPath);
if (!service) {
return;
}
// Remove the old one...
delete servicesLB->takeItem(selected);
// ...check that it's not a duplicate entry...
bool addIt = true;
for (int index = 0; index < servicesLB->count(); index++) {
if (static_cast<KServiceListItem *>(servicesLB->item(index))->desktopPath == service->entryPath()) {
addIt = false;
break;
}
}
// ...and add it in the same place as the old one:
if (addIt) {
servicesLB->insertItem(selected, new KServiceListItem(service));
servicesLB->setCurrentRow(selected);
}
updatePreferredServices();
Q_EMIT changed(true);
}
void KServiceListWidget::removeService()
{
if (!m_mimeTypeData) {
return;
}
int selected = servicesLB->currentRow();
if (selected >= 0) {
delete servicesLB->takeItem(selected);
updatePreferredServices();
Q_EMIT changed(true);
}
// Update buttons and service list again (e.g. to re-add "None")
setMimeTypeData(m_mimeTypeData);
}
void KServiceListWidget::updatePreferredServices()
{
if (!m_mimeTypeData) {
return;
}
QStringList sl;
unsigned int count = servicesLB->count();
for (unsigned int i = 0; i < count; i++) {
const auto &item = servicesLB->item(i);
Q_ASSERT(item->type() != -1);
if (m_kind == SERVICELIST_APPLICATIONS) {
auto sli = dynamic_cast<KServiceListItem *>(item);
if (!sli) { // Is a textual item like the default 'None' entry
continue;
}
sl.append(sli->storageId);
} else {
auto pluginItem = dynamic_cast<PluginListItem *>(item);
if (!pluginItem) { // Is a textual item like the default 'None' entry
continue;
}
sl.append(pluginItem->metaData.pluginId());
}
}
sl.removeDuplicates();
if (m_kind == SERVICELIST_APPLICATIONS) {
m_mimeTypeData->setAppServices(sl);
} else {
m_mimeTypeData->setEmbedParts(sl);
}
}
void KServiceListWidget::applyTo()
{
Q_EMIT multiApply(m_kind);
}
void KServiceListWidget::enableMoveButtons()
{
int idx = servicesLB->currentRow();
if (servicesLB->model()->rowCount() <= 1) {
servUpButton->setEnabled(false);
servDownButton->setEnabled(false);
} else if (idx == (servicesLB->model()->rowCount() - 1)) {
servUpButton->setEnabled(true);
servDownButton->setEnabled(false);
} else if (idx == 0) {
servUpButton->setEnabled(false);
servDownButton->setEnabled(true);
} else {
servUpButton->setEnabled(true);
servDownButton->setEnabled(true);
}
if (servRemoveButton) {
servRemoveButton->setEnabled(true);
}
if (servEditButton) {
servEditButton->setEnabled(m_kind == SERVICELIST_APPLICATIONS);
}
}
#include "moc_kservicelistwidget.cpp"
|