kservicelistwidget.cpp 13.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* This file is part of the KDE project
   Copyright (C) 2003 Waldo Bastian <bastian@kde.org>
   Copyright (C) 2003 David Faure <faure@kde.org>
   Copyright (C) 2002 Daniel Molkentin <molkentin@kde.org>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License version 2 as published by the Free Software Foundation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
Dirk Mueller's avatar
Dirk Mueller committed
17
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Dirk Mueller's avatar
Dirk Mueller committed
18
   Boston, MA 02110-1301, USA.
19
20
*/

21
22
23
24
// Own
#include "kservicelistwidget.h"

// Qt
25
26
#include <QBoxLayout>
#include <QDebug>
27
#include <QLayout>
28
#include <QPushButton>
29
#include <QStandardPaths>
30

31
// KDE
32
#include <klocalizedstring.h>
33
#include <kmessagebox.h>
Urs Wolfer's avatar
Urs Wolfer committed
34
#include <kopenwithdialog.h>
35
#include <kpropertiesdialog.h>
36

37
// Local
38
#include "kserviceselectdlg.h"
39
#include "mimetypedata.h"
40

41
42
KServiceListItem::KServiceListItem( const KService::Ptr& pService, int kind )
    : QListWidgetItem(), storageId(pService->storageId()), desktopPath(pService->entryPath())
43
{
44
45
46
    if ( kind == KServiceListWidget::SERVICELIST_APPLICATIONS )
        setText( pService->name() );
    else
47
        setText( i18n( "%1 (%2)", pService->name(), pService->desktopEntryName() ) );
48

49
50
    setIcon( QIcon::fromTheme( pService->icon() ) );

David Faure's avatar
David Faure committed
51
    if (!pService->isApplication())
52
      localPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/kservices5/") + desktopPath;
53
54
55
56
    else
      localPath = pService->locateLocal();
}

57

58
59
60
61




Laurent Montel's avatar
Laurent Montel committed
62
63
64
KServiceListWidget::KServiceListWidget(int kind, QWidget *parent)
  : QGroupBox( kind == SERVICELIST_APPLICATIONS ? i18n("Application Preference Order")
               : i18n("Services Preference Order"), parent ),
Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
65
    m_kind( kind ), m_mimeTypeData( nullptr )
66
{
67
  QHBoxLayout *lay= new QHBoxLayout(this);
68

Urs Wolfer's avatar
Urs Wolfer committed
69
  servicesLB = new QListWidget(this);
70
  connect(servicesLB, SIGNAL(itemSelectionChanged()), SLOT(enableMoveButtons()));
71
  lay->addWidget(servicesLB);
Laurent Montel's avatar
Laurent Montel committed
72
  connect( servicesLB, SIGNAL( itemDoubleClicked(QListWidgetItem*)), this, SLOT( editService()));
73

74
75
76
77
  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"
78
          " \"Open With...\". If more than one application is associated with this file type,"
79
80
81
82
          " 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"
83
          " a \"Preview with...\" option. If more than one service is associated with this file type,"
84
85
86
          " then the list is ordered by priority with the uppermost item taking precedence"
          " over the others."));

Urs Wolfer's avatar
Urs Wolfer committed
87
  setWhatsThis( wtstr );
88
  servicesLB->setWhatsThis( wtstr );
89

90
91
92
  QVBoxLayout *btnsLay= new QVBoxLayout();
  lay->addLayout(btnsLay);

93
  servUpButton = new QPushButton(i18n("Move &Up"), this);
94
  servUpButton->setIcon(QIcon::fromTheme(QStringLiteral("arrow-up")));
95
96
  servUpButton->setEnabled(false);
  connect(servUpButton, SIGNAL(clicked()), SLOT(promoteService()));
97
  btnsLay->addWidget(servUpButton);
98

99
  servUpButton->setWhatsThis( kind == SERVICELIST_APPLICATIONS ?
100
101
102
103
104
105
                   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."));
106

107
  servDownButton = new QPushButton(i18n("Move &Down"), this);
108
  servDownButton->setIcon(QIcon::fromTheme(QStringLiteral("arrow-down")));
109
110
  servDownButton->setEnabled(false);
  connect(servDownButton, SIGNAL(clicked()), SLOT(demoteService()));
111
  btnsLay->addWidget(servDownButton);
112
  servDownButton->setWhatsThis( kind == SERVICELIST_APPLICATIONS ?
113
                   i18n("Assigns a lower priority to the selected\n"
114
                        "application, moving it down in the list. Note: This \n"
115
116
117
118
                        "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."));
119

120
  servNewButton = new QPushButton(i18n("Add..."), this);
121
  servNewButton->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
122
123
  servNewButton->setEnabled(false);
  connect(servNewButton, SIGNAL(clicked()), SLOT(addService()));
124
  btnsLay->addWidget(servNewButton);
125
  servNewButton->setWhatsThis( i18n( "Add a new application for this file type." ) );
126

127

128
  servEditButton = new QPushButton(i18n("Edit..."), this);
129
  servEditButton->setIcon(QIcon::fromTheme(QStringLiteral("edit-rename")));
130
131
  servEditButton->setEnabled(false);
  connect(servEditButton, SIGNAL(clicked()), SLOT(editService()));
132
  btnsLay->addWidget(servEditButton);
133
  servEditButton->setWhatsThis( i18n( "Edit command line of the selected application." ) );
134
135


136
  servRemoveButton = new QPushButton(i18n("Remove"), this);
137
  servRemoveButton->setIcon(QIcon::fromTheme(QStringLiteral("list-remove")));
138
139
  servRemoveButton->setEnabled(false);
  connect(servRemoveButton, SIGNAL(clicked()), SLOT(removeService()));
140
  btnsLay->addWidget(servRemoveButton);
141
  servRemoveButton->setWhatsThis( i18n( "Remove the selected application from the list." ) );
142

143
  btnsLay->addStretch(1);
144
145
}

146
void KServiceListWidget::setMimeTypeData( MimeTypeData * mimeTypeData )
147
{
148
  m_mimeTypeData = mimeTypeData;
149
150
  if ( servNewButton )
    servNewButton->setEnabled(true);
151
152
153
  // will need a selection
  servUpButton->setEnabled(false);
  servDownButton->setEnabled(false);
154

155
156
157
  servicesLB->clear();
  servicesLB->setEnabled(false);

158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
    if (m_mimeTypeData) {
        const QStringList services = ( m_kind == SERVICELIST_APPLICATIONS )
                                     ? m_mimeTypeData->appServices()
                                     : m_mimeTypeData->embedServices();

        if (services.isEmpty()) {
            if (m_kind == SERVICELIST_APPLICATIONS)
                servicesLB->addItem(i18nc("No applications associated with this file type", "None"));
            else
                servicesLB->addItem(i18nc("No components associated with this file type", "None"));
        } else {
            Q_FOREACH(const QString& service, services) {
                KService::Ptr pService = KService::serviceByStorageId(service);
                if (pService)
                    servicesLB->addItem( new KServiceListItem(pService, m_kind) );
            }
            servicesLB->setEnabled(true);
        }
176
    }
177
178
179
180
181

    if (servRemoveButton)
        servRemoveButton->setEnabled(servicesLB->currentRow() > -1);
    if (servEditButton)
        servEditButton->setEnabled(servicesLB->currentRow() > -1);
182
183
184
185
186
187
188
189
}

void KServiceListWidget::promoteService()
{
  if (!servicesLB->isEnabled()) {
    return;
  }

Robert Knight's avatar
Robert Knight committed
190
  int selIndex = servicesLB->currentRow();
191
192
193
194
  if (selIndex == 0) {
    return;
  }

Robert Knight's avatar
Robert Knight committed
195
196
197
198
  QListWidgetItem *selItem = servicesLB->item(selIndex);
  servicesLB->takeItem(selIndex);
  servicesLB->insertItem(selIndex-1,selItem);
  servicesLB->setCurrentRow(selIndex - 1);
199
200
201
202
203
204
205
206
207
208
209
210

  updatePreferredServices();

  emit changed(true);
}

void KServiceListWidget::demoteService()
{
  if (!servicesLB->isEnabled()) {
    return;
  }

Robert Knight's avatar
Robert Knight committed
211
  int selIndex = servicesLB->currentRow();
212
213
214
215
  if (selIndex == servicesLB->count() - 1) {
    return;
  }

Robert Knight's avatar
Robert Knight committed
216
217
218
219
  QListWidgetItem *selItem = servicesLB->item(selIndex);
  servicesLB->takeItem(selIndex);
  servicesLB->insertItem(selIndex + 1, selItem);
  servicesLB->setCurrentRow(selIndex + 1);
220
221
222
223
224
225
226
227

  updatePreferredServices();

  emit changed(true);
}

void KServiceListWidget::addService()
{
228
  if (!m_mimeTypeData)
229
230
      return;

231
  KService::Ptr service;
232
233
  if ( m_kind == SERVICELIST_APPLICATIONS )
  {
Craig Drummond's avatar
Craig Drummond committed
234
      KOpenWithDialog dlg(m_mimeTypeData->name(), QString(), this);
Waldo Bastian's avatar
Waldo Bastian committed
235
      dlg.setSaveNewApplications(true);
236
      if (dlg.exec() != QDialog::Accepted)
237
          return;
238

239
      service = dlg.service();
240

Dirk Mueller's avatar
compile    
Dirk Mueller committed
241
      Q_ASSERT(service);
242
243
244
245
246
      if (!service)
          return; // Don't crash if KOpenWith wasn't able to create service.
  }
  else
  {
Craig Drummond's avatar
Craig Drummond committed
247
      KServiceSelectDlg dlg(m_mimeTypeData->name(), QString(), this);
248
      if (dlg.exec() != QDialog::Accepted)
249
250
          return;
       service = dlg.service();
Dirk Mueller's avatar
compile    
Dirk Mueller committed
251
       Q_ASSERT(service);
252
253
254
       if (!service)
           return;
  }
255

256
257
258
259
260
261
262
  // Did the list simply show "None"?
  const bool hadDummyEntry = ( m_kind == SERVICELIST_APPLICATIONS )
                               ? m_mimeTypeData->appServices().isEmpty()
                               : m_mimeTypeData->embedServices().isEmpty();

  if (hadDummyEntry) {
      delete servicesLB->takeItem(0); // Remove the "None" item.
263
      servicesLB->setEnabled(true);
264
  } else {
265
      // check if it is a duplicate entry
266
      for (int index = 0; index < servicesLB->count(); index++) {
267
        if (static_cast<KServiceListItem*>( servicesLB->item(index) )->desktopPath
268
269
            == service->entryPath()) {
          // ##### shouldn't we make the existing entry the default one?
270
          return;
271
272
        }
      }
273
274
  }

275
  servicesLB->insertItem(0, new KServiceListItem(service, m_kind));
Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
276
  servicesLB->setCurrentItem(nullptr);
277
278
279
280
281
282

  updatePreferredServices();

  emit changed(true);
}

283
284
void KServiceListWidget::editService()
{
285
286
287
288
289
    if (!m_mimeTypeData)
        return;
    const int selected = servicesLB->currentRow();
    if (selected < 0)
        return;
290
291
292

    // Only edit applications, not services as
    // they don't have any parameters
293
294
    if (m_kind != SERVICELIST_APPLICATIONS)
        return;
Waldo Bastian's avatar
Waldo Bastian committed
295

296
297
298
299
300
301
302
    // 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)
303
        return;
Waldo Bastian's avatar
Waldo Bastian committed
304

305
    QString path = service->entryPath();
306
307
308
309
310
311
312
313
    {
        // 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;
        }
    }
Waldo Bastian's avatar
Waldo Bastian committed
314

315
    KFileItem item(QUrl::fromLocalFile(path), QStringLiteral("application/x-desktop"), KFileItem::Unknown);
316
317
    KPropertiesDialog dlg(item, this);
    if (dlg.exec() != QDialog::Accepted)
318
        return;
319

320
321
322
323
324
325
    // 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)
326
        return;
Waldo Bastian's avatar
Waldo Bastian committed
327

328
329
    // Remove the old one...
    delete servicesLB->takeItem(selected);
330

331
332
333
334
335
336
337
    // ...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;
338
        }
339
    }
340

341
342
343
    // ...and add it in the same place as the old one:
    if (addIt) {
        servicesLB->insertItem(selected, new KServiceListItem(service, m_kind));
Robert Knight's avatar
Robert Knight committed
344
        servicesLB->setCurrentRow(selected);
345
    }
346

347
    updatePreferredServices();
348

349
    emit changed(true);
350
351
}

352
353
void KServiceListWidget::removeService()
{
354
  if (!m_mimeTypeData) return;
355

Robert Knight's avatar
Robert Knight committed
356
  int selected = servicesLB->currentRow();
357
358

  if ( selected >= 0 ) {
359
360
    delete servicesLB->takeItem( selected );
    updatePreferredServices();
361

362
    emit changed(true);
363
364
  }

365
366
    // Update buttons and service list again (e.g. to re-add "None")
    setMimeTypeData(m_mimeTypeData);
367
368
369
370
}

void KServiceListWidget::updatePreferredServices()
{
371
  if (!m_mimeTypeData)
372
373
374
375
376
377
    return;
  QStringList sl;
  unsigned int count = servicesLB->count();

  for (unsigned int i = 0; i < count; i++) {
    KServiceListItem *sli = (KServiceListItem *) servicesLB->item(i);
378
    sl.append( sli->storageId );
379
  }
380
  sl.removeDuplicates();
381
  if ( m_kind == SERVICELIST_APPLICATIONS )
382
    m_mimeTypeData->setAppServices(sl);
383
  else
384
    m_mimeTypeData->setEmbedServices(sl);
385
386
}

387
void KServiceListWidget::enableMoveButtons()
388
{
389
390
  int idx = servicesLB->currentRow();
  if (servicesLB->model()->rowCount() <= 1)
391
392
393
394
  {
    servUpButton->setEnabled(false);
    servDownButton->setEnabled(false);
  }
395
  else if ( idx == (servicesLB->model()->rowCount() - 1) )
396
397
398
399
  {
    servUpButton->setEnabled(true);
    servDownButton->setEnabled(false);
  }
400
  else if (idx == 0)
401
402
403
404
405
406
407
408
409
410
  {
    servUpButton->setEnabled(false);
    servDownButton->setEnabled(true);
  }
  else
  {
    servUpButton->setEnabled(true);
    servDownButton->setEnabled(true);
  }

411
412
  if ( servRemoveButton )
    servRemoveButton->setEnabled(true);
413

414
  if ( servEditButton )
415
    servEditButton->setEnabled( m_kind == SERVICELIST_APPLICATIONS );
416
417
}