mimetypesettings.cpp 20.3 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
hjk's avatar
hjk committed
3
4
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
23
24
25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29
30
31
32
33
34
35
36
37
38
39
40
41

#include "mimetypesettings.h"
#include "ui_mimetypesettingspage.h"
#include "mimetypemagicdialog.h"
#include "mimedatabase.h"
#include "coreconstants.h"
#include "editormanager.h"
#include "icore.h"
#include "ieditorfactory.h"
#include "iexternaleditor.h"

#include <extensionsystem/pluginmanager.h>

42
43
44
45
46
47
48
49
#include <QCoreApplication>
#include <QStringList>
#include <QSet>
#include <QScopedPointer>
#include <QAbstractTableModel>
#include <QHash>
#include <QTableWidgetItem>
#include <QMessageBox>
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <QtAlgorithms>

#include <algorithm>

namespace Core {
namespace Internal {

struct MimeTypeComp
{
    bool operator()(const MimeType &a, const MimeType &b)
    { return a.type().compare(b.type(), Qt::CaseInsensitive) < 0; }
};

// MimeTypeSettingsModel
class MimeTypeSettingsModel : public QAbstractTableModel
{
    Q_OBJECT

public:
    MimeTypeSettingsModel(QObject *parent = 0)
hjk's avatar
hjk committed
70
        : QAbstractTableModel(parent) {}
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
    virtual ~MimeTypeSettingsModel() {}

    virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
    virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
    virtual QVariant headerData(int section, Qt::Orientation orientation,
                                int role = Qt::DisplayRole) const;
    virtual QVariant data(const QModelIndex &modelIndex, int role = Qt::DisplayRole) const;

    void load();
    void validatePatterns(QStringList *candidates, const MimeType &mimeType) const;
    void updateKnownPatterns(const QStringList &oldPatterns, const QStringList &newPatterns);

    QList<MimeType> m_mimeTypes;
    QSet<QString> m_knownPatterns;
    QHash<QString, QString> m_handlersByMimeType;
};

int MimeTypeSettingsModel::rowCount(const QModelIndex &) const
{
    return m_mimeTypes.size();
}

int MimeTypeSettingsModel::columnCount(const QModelIndex &) const
{
    return 2;
}

QVariant MimeTypeSettingsModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
        return QVariant();

    if (section == 0)
        return tr("MIME Type");
    else
        return tr("Handler");
}

QVariant MimeTypeSettingsModel::data(const QModelIndex &modelIndex, int role) const
{
    if (!modelIndex.isValid())
        return QVariant();

    const int column = modelIndex.column();
    if (role == Qt::DisplayRole) {
        const QString &type = m_mimeTypes.at(modelIndex.row()).type();
        if (column == 0)
            return type;
        else
            return m_handlersByMimeType.value(type);
    } else if (role == Qt::TextAlignmentRole) {
        if (column == 1)
            return Qt::AlignCenter;
    }
    return QVariant();
}

void MimeTypeSettingsModel::load()
{
hjk's avatar
hjk committed
130
    m_mimeTypes = ICore::mimeDatabase()->mimeTypes();
131
132
    qSort(m_mimeTypes.begin(), m_mimeTypes.end(), MimeTypeComp());
    m_knownPatterns = QSet<QString>::fromList(
hjk's avatar
hjk committed
133
        MimeDatabase::fromGlobPatterns(ICore::mimeDatabase()->globPatterns()));
134
135
136
137

    foreach (const MimeType &mimeType, m_mimeTypes) {
        QString value;
        const QList<IEditorFactory *> factories =
hjk's avatar
hjk committed
138
            EditorManager::editorFactories(mimeType);
139
140
141
142
        if (!factories.isEmpty()) {
            value = factories.front()->displayName();
        } else {
            const QList<IExternalEditor *> externalEditors =
hjk's avatar
hjk committed
143
                EditorManager::externalEditors(mimeType);
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
            if (!externalEditors.isEmpty())
                value = externalEditors.front()->displayName();
            else
                value = tr("Undefined");
        }
        m_handlersByMimeType.insert(mimeType.type(), value);
    }
}

void MimeTypeSettingsModel::validatePatterns(QStringList *candidates,
                                             const MimeType &mimeType) const
{
    QSet<QString> oldPatterns =
        QSet<QString>::fromList(MimeDatabase::fromGlobPatterns(mimeType.globPatterns()));

    QStringList duplicates;
    QStringList::iterator it = candidates->begin();
    while (it != candidates->end()) {
        const QString &current = *it;
        if (!oldPatterns.contains(current) && m_knownPatterns.contains(current)) {
            duplicates.append(current);
            it = candidates->erase(it);
        } else {
            ++it;
        }
    }

    if (!duplicates.isEmpty()) {
        QMessageBox msgBox;
        msgBox.setWindowTitle(tr("Invalid MIME Type"));
        msgBox.setText(tr("Conflicting pattern(s) will be discarded."));
        msgBox.setInformativeText(tr("%n pattern(s) already in use.", 0, duplicates.size()));
        msgBox.setDetailedText(duplicates.join(QLatin1String("\n")));
        msgBox.exec();
    }
}

void MimeTypeSettingsModel::updateKnownPatterns(const QStringList &oldPatterns,
                                                const QStringList &newPatterns)
{
    QStringList all = oldPatterns;
    all.append(newPatterns);
    all.removeDuplicates();
    foreach (const QString &pattern, all) {
        QSet<QString>::iterator it = m_knownPatterns.find(pattern);
        if (it == m_knownPatterns.end()) {
            // A pattern was added.
            m_knownPatterns.insert(pattern);
        } else {
            // A pattern was removed.
            m_knownPatterns.erase(it);
        }
    }
}

199
// MimeTypeSettingsPrivate
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
class MimeTypeSettingsPrivate : public QObject
{
    Q_OBJECT

public:
    MimeTypeSettingsPrivate();
    virtual ~MimeTypeSettingsPrivate();

    void configureUi(QWidget *w);
    static void configureTable(QTableView *tableView);

    bool checkSelectedMimeType() const;
    bool checkSelectedMagicHeader() const;

    void markMimeForPatternSync(int index);
    void markMimeForMagicSync(int index);
    void syncMimePattern();
    void syncMimeMagic();
    void clearSyncData();
    void markAsModified(int index);

    void addMagicHeaderRow(const MagicData &data);
    MagicData getMagicHeaderRowData(const int row) const;
    void editMagicHeaderRowData(const int row, const MagicData &data);

    void updateMimeDatabase();
226
    void resetState();
227
228
229
230
231
232
233
234

public slots:
    void syncData(const QModelIndex &current, const QModelIndex &previous);
    void handlePatternEdited();
    void addMagicHeader();
    void removeMagicHeader();
    void editMagicHeader();
    void resetMimeTypes();
235
    void updateMagicHeaderButtons();
236
237
238
239
240
241
242
243
244
245

public:
    static const QChar kSemiColon;

    QString m_keywords;
    MimeDatabase *m_mimeDatabase;
    QScopedPointer<MimeTypeSettingsModel> m_model;
    int m_mimeForPatternSync;
    int m_mimeForMagicSync;
    bool m_reset;
246
    bool m_persist;
247
248
249
250
251
252
253
    QList<int> m_modifiedMimeTypes;
    Ui::MimeTypeSettingsPage m_ui;
};

const QChar MimeTypeSettingsPrivate::kSemiColon(QLatin1Char(';'));

MimeTypeSettingsPrivate::MimeTypeSettingsPrivate()
hjk's avatar
hjk committed
254
    : m_mimeDatabase(ICore::mimeDatabase())
255
256
257
258
    , m_model(new MimeTypeSettingsModel)
    , m_mimeForPatternSync(-1)
    , m_mimeForMagicSync(-1)
    , m_reset(false)
259
    , m_persist(false)
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
{}

MimeTypeSettingsPrivate::~MimeTypeSettingsPrivate()
{}

void MimeTypeSettingsPrivate::configureUi(QWidget *w)
{
    m_ui.setupUi(w);

    m_model->load();
    m_ui.mimeTypesTableView->setModel(m_model.data());

    configureTable(m_ui.mimeTypesTableView);
    configureTable(m_ui.magicHeadersTableWidget);

    connect(m_ui.mimeTypesTableView->selectionModel(),
            SIGNAL(currentChanged(QModelIndex,QModelIndex)),
            this,
            SLOT(syncData(QModelIndex,QModelIndex)));
    connect(m_ui.patternsLineEdit, SIGNAL(textEdited(QString)),
            this, SLOT(handlePatternEdited()));
    connect(m_ui.addMagicButton, SIGNAL(clicked()), this, SLOT(addMagicHeader()));
    connect(m_ui.removeMagicButton, SIGNAL(clicked()), this, SLOT(removeMagicHeader()));
    connect(m_ui.editMagicButton, SIGNAL(clicked()), this, SLOT(editMagicHeader()));
    connect(m_ui.resetButton, SIGNAL(clicked()), this, SLOT(resetMimeTypes()));
285
286
287
288
289
    connect(m_ui.magicHeadersTableWidget->selectionModel(),
            SIGNAL(currentChanged(QModelIndex,QModelIndex)),
            this,
            SLOT(updateMagicHeaderButtons()));
    updateMagicHeaderButtons();
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
}

void MimeTypeSettingsPrivate::configureTable(QTableView *tableView)
{
    tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    tableView->verticalHeader()->setVisible(false);
    tableView->verticalHeader()->setDefaultSectionSize(20);
    tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
    tableView->horizontalHeader()->setResizeMode(0, QHeaderView::Interactive);
    tableView->horizontalHeader()->resizeSection(
        0, 4 * tableView->horizontalHeader()->defaultSectionSize());
    tableView->horizontalHeader()->setHighlightSections(false);
    tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
}

bool MimeTypeSettingsPrivate::checkSelectedMimeType() const
{
    const QModelIndex &modelIndex = m_ui.mimeTypesTableView->selectionModel()->currentIndex();
    if (!modelIndex.isValid()) {
        QMessageBox::critical(0, tr("Error"), tr("No MIME type selected."));
        return false;
    }
    return true;
}

bool MimeTypeSettingsPrivate::checkSelectedMagicHeader() const
{
    const QModelIndex &modelIndex = m_ui.magicHeadersTableWidget->selectionModel()->currentIndex();
    if (!modelIndex.isValid()) {
        QMessageBox::critical(0, tr("Error"), tr("No magic header selected."));
        return false;
    }
    return true;
}

void MimeTypeSettingsPrivate::markMimeForPatternSync(int index)
{
    if (m_mimeForPatternSync != index) {
        m_mimeForPatternSync = index;
        markAsModified(index);
    }
}

void MimeTypeSettingsPrivate::markMimeForMagicSync(int index)
{
    if (m_mimeForMagicSync != index) {
        m_mimeForMagicSync = index;
        markAsModified(index);
    }
}

void MimeTypeSettingsPrivate::markAsModified(int index)
{
    // Duplicates are handled later.
    m_modifiedMimeTypes.append(index);
}

void MimeTypeSettingsPrivate::syncMimePattern()
{
    MimeType &mimeType = m_model->m_mimeTypes[m_mimeForPatternSync];
    QStringList patterns = m_ui.patternsLineEdit->text().split(kSemiColon);
    patterns.removeDuplicates();
    m_model->validatePatterns(&patterns, mimeType);
    m_model->updateKnownPatterns(MimeDatabase::fromGlobPatterns(mimeType.globPatterns()), patterns);
    mimeType.setGlobPatterns(MimeDatabase::toGlobPatterns(patterns));
}

void MimeTypeSettingsPrivate::syncMimeMagic()
{
    typedef MagicRuleMatcher::MagicRuleList MagicRuleList;
    typedef MagicRuleMatcher::MagicRuleSharedPointer MagicRuleSharedPointer;

    // Gather the magic rules.
    QHash<int, MagicRuleList> rulesByPriority;
    for (int row = 0; row < m_ui.magicHeadersTableWidget->rowCount(); ++row) {
        const MagicData &data = getMagicHeaderRowData(row);
        // @TODO: Validate magic rule?
        MagicRule *magicRule;
        if (data.m_type == MagicStringRule::kMatchType)
            magicRule = new MagicStringRule(data.m_value, data.m_start, data.m_end);
        else
            magicRule = new MagicByteRule(data.m_value, data.m_start, data.m_end);
        rulesByPriority[data.m_priority].append(MagicRuleSharedPointer(magicRule));
    }

    const QList<QSharedPointer<IMagicMatcher> > &matchers =
        MagicRuleMatcher::createMatchers(rulesByPriority);
    m_model->m_mimeTypes[m_mimeForMagicSync].setMagicRuleMatchers(matchers);
}

void MimeTypeSettingsPrivate::clearSyncData()
{
    m_mimeForPatternSync = -1;
    m_mimeForMagicSync = -1;
}

void MimeTypeSettingsPrivate::syncData(const QModelIndex &current,
                                           const QModelIndex &previous)
{
    if (previous.isValid()) {
        if (m_mimeForPatternSync == previous.row())
            syncMimePattern();
        if (m_mimeForMagicSync == previous.row())
            syncMimeMagic();
        clearSyncData();

        m_ui.patternsLineEdit->clear();
        m_ui.magicHeadersTableWidget->clearContents();
        m_ui.magicHeadersTableWidget->setRowCount(0);
    }

    if (current.isValid()) {
        const MimeType &currentMimeType = m_model->m_mimeTypes.at(current.row());

        QStringList formatedPatterns;
        foreach (const MimeGlobPattern &pattern, currentMimeType.globPatterns())
            formatedPatterns.append(pattern.regExp().pattern());
        m_ui.patternsLineEdit->setText(formatedPatterns.join(kSemiColon));

        // Consider only rule-based matchers.
        const QList<QSharedPointer<IMagicMatcher> > &matchers = currentMimeType.magicRuleMatchers();
        foreach (const QSharedPointer<IMagicMatcher> &matcher, matchers) {
            MagicRuleMatcher *ruleMatcher = static_cast<MagicRuleMatcher *>(matcher.data());
            const int priority = ruleMatcher->priority();
            const MagicRuleMatcher::MagicRuleList &rules = ruleMatcher->magicRules();
            foreach (const MagicRuleMatcher::MagicRuleSharedPointer &rule, rules)
                addMagicHeaderRow(MagicData(rule->matchValue(),
                                            rule->matchType(),
                                            rule->startPos(),
                                            rule->endPos(),
                                            priority));
        }
    }
}

void MimeTypeSettingsPrivate::handlePatternEdited()
{
    if (m_mimeForPatternSync == -1) {
        const QModelIndex &modelIndex = m_ui.mimeTypesTableView->selectionModel()->currentIndex();
        if (modelIndex.isValid())
            markMimeForPatternSync(modelIndex.row());
    }
}

void MimeTypeSettingsPrivate::addMagicHeaderRow(const MagicData &data)
{
    const int row = m_ui.magicHeadersTableWidget->rowCount();
    m_ui.magicHeadersTableWidget->insertRow(row);
    editMagicHeaderRowData(row, data);
}

MagicData MimeTypeSettingsPrivate::getMagicHeaderRowData(const int row) const
{
    MagicData data;
    data.m_value = m_ui.magicHeadersTableWidget->item(row, 0)->text();
    data.m_type = m_ui.magicHeadersTableWidget->item(row, 1)->text();
    QPair<int, int> startEnd =
        MagicRule::fromOffset(m_ui.magicHeadersTableWidget->item(row, 2)->text());
    data.m_start = startEnd.first;
    data.m_end = startEnd.second;
    data.m_priority = m_ui.magicHeadersTableWidget->item(row, 3)->text().toInt();

    return data;
}

void MimeTypeSettingsPrivate::editMagicHeaderRowData(const int row, const MagicData &data)
{
    for (int col = 0; col < m_ui.magicHeadersTableWidget->columnCount(); ++col) {
        QTableWidgetItem *item = new QTableWidgetItem;
        if (col == 0) {
            item->setText(data.m_value);
        } else {
            item->setTextAlignment(Qt::AlignCenter);
            if (col == 1)
                item->setText(data.m_type);
            else if (col == 2)
                item->setText(MagicRule::toOffset(qMakePair(data.m_start, data.m_end)));
            else
                item->setText(QString::number(data.m_priority));
        }
        m_ui.magicHeadersTableWidget->setItem(row, col, item);
    }
}

void MimeTypeSettingsPrivate::addMagicHeader()
{
    if (!checkSelectedMimeType())
        return;

    MimeTypeMagicDialog dlg;
    if (dlg.exec()) {
        addMagicHeaderRow(dlg.magicData());
        markMimeForMagicSync(m_ui.mimeTypesTableView->selectionModel()->currentIndex().row());
    }
}

void MimeTypeSettingsPrivate::removeMagicHeader()
{
    if (!checkSelectedMagicHeader())
        return;

    m_ui.magicHeadersTableWidget->removeRow(m_ui.magicHeadersTableWidget->currentRow());
    markMimeForMagicSync(m_ui.mimeTypesTableView->selectionModel()->currentIndex().row());
}

void MimeTypeSettingsPrivate::editMagicHeader()
{
    if (!checkSelectedMagicHeader())
        return;

    MimeTypeMagicDialog dlg;
    dlg.setMagicData(getMagicHeaderRowData(m_ui.magicHeadersTableWidget->currentRow()));
    if (dlg.exec()) {
        editMagicHeaderRowData(m_ui.magicHeadersTableWidget->currentRow(), dlg.magicData());
        markMimeForMagicSync(m_ui.mimeTypesTableView->selectionModel()->currentIndex().row());
    }
}

void MimeTypeSettingsPrivate::updateMimeDatabase()
{
511
512
513
    if (m_modifiedMimeTypes.isEmpty())
        return;

514
515
516
517
518
    // For this case it is a better approach to simply use a list and to remove duplicates
    // afterwards than to keep a more complex data structure like a hash table.
    qSort(m_modifiedMimeTypes.begin(), m_modifiedMimeTypes.end());
    m_modifiedMimeTypes.erase(std::unique(m_modifiedMimeTypes.begin(), m_modifiedMimeTypes.end()),
                              m_modifiedMimeTypes.end());
519

hjk's avatar
hjk committed
520
    MimeDatabase *db = ICore::mimeDatabase();
521
522
523
524
525
526
    QList<MimeType> allModified;
    foreach (int index, m_modifiedMimeTypes) {
        const MimeType &mimeType = m_model->m_mimeTypes.at(index);
        db->setGlobPatterns(mimeType.type(), mimeType.globPatterns());
        db->setMagicMatchers(mimeType.type(), mimeType.magicMatchers());
        allModified.append(mimeType);
527
    }
528
529
530
531
532
533
534
535
536
    db->writeUserModifiedMimeTypes(allModified);
}

void MimeTypeSettingsPrivate::resetState()
{
    clearSyncData();
    m_modifiedMimeTypes.clear();
    m_reset = false;
    m_persist = false;
537
538
539
540
541
542
543
544
545
546
}

void MimeTypeSettingsPrivate::resetMimeTypes()
{
    QMessageBox::information(0,
                             tr("MIME Types"),
                             tr("Changes will take effect in the next time you start Qt Creator."));
    m_reset = true;
}

547
548
549
void MimeTypeSettingsPrivate::updateMagicHeaderButtons()
{
    const QModelIndex &modelIndex = m_ui.magicHeadersTableWidget->selectionModel()->currentIndex();
hjk's avatar
hjk committed
550
    const bool enabled = modelIndex.isValid();
551
552
553
554
555

    m_ui.removeMagicButton->setEnabled(enabled);
    m_ui.editMagicButton->setEnabled(enabled);
}

556
557
558
// MimeTypeSettingsPage
MimeTypeSettings::MimeTypeSettings(QObject *parent)
    : IOptionsPage(parent)
hjk's avatar
hjk committed
559
    , d(new MimeTypeSettingsPrivate)
560
{
hjk's avatar
hjk committed
561
    setId(Core::Constants::SETTINGS_ID_MIMETYPES);
562
    setDisplayName(tr("MIME Types"));
hjk's avatar
hjk committed
563
    setCategory(Core::Constants::SETTINGS_CATEGORY_CORE);
564
565
566
    setDisplayCategory(QCoreApplication::translate("Core",
        Core::Constants::SETTINGS_TR_CATEGORY_CORE));
    setCategoryIcon(QLatin1String(Core::Constants::SETTINGS_CATEGORY_CORE_ICON));
567
568
}

569
MimeTypeSettings::~MimeTypeSettings()
570
{
571
    delete d;
572
573
574
575
}

bool MimeTypeSettings::matches(const QString &s) const
{
hjk's avatar
hjk committed
576
    return d->m_keywords.contains(s, Qt::CaseInsensitive);
577
578
579
580
581
}

QWidget *MimeTypeSettings::createPage(QWidget *parent)
{
    QWidget *w = new QWidget(parent);
hjk's avatar
hjk committed
582
    d->configureUi(w);
583
584
585
586
587
    return w;
}

void MimeTypeSettings::apply()
{
hjk's avatar
hjk committed
588
    if (!d->m_modifiedMimeTypes.isEmpty()) {
589
        const QModelIndex &modelIndex =
hjk's avatar
hjk committed
590
            d->m_ui.mimeTypesTableView->selectionModel()->currentIndex();
591
        if (modelIndex.isValid()) {
hjk's avatar
hjk committed
592
593
594
595
            if (d->m_mimeForPatternSync == modelIndex.row())
                d->syncMimePattern();
            if (d->m_mimeForMagicSync == modelIndex.row())
                d->syncMimeMagic();
596
        }
hjk's avatar
hjk committed
597
        d->clearSyncData();
598
    }
599

hjk's avatar
hjk committed
600
601
    if (!d->m_persist)
        d->m_persist = true;
602
603
604
}

void MimeTypeSettings::finish()
605
{
hjk's avatar
hjk committed
606
607
    if (d->m_persist) {
        if (d->m_reset)
hjk's avatar
hjk committed
608
            ICore::mimeDatabase()->clearUserModifiedMimeTypes();
609
        else
hjk's avatar
hjk committed
610
            d->updateMimeDatabase();
611
    }
hjk's avatar
hjk committed
612
    d->resetState();
613
}
614
615
616
617
618

} // Internal
} // Core

#include "mimetypesettings.moc"