File: kmimetypechooser.cpp

package info (click to toggle)
kwidgetsaddons 5.28.0-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 62,388 kB
  • ctags: 3,611
  • sloc: cpp: 26,647; python: 682; sh: 23; makefile: 6
file content (369 lines) | stat: -rw-r--r-- 11,370 bytes parent folder | download
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
/* This file is part of the KDE libraries
   Copyright (C) 2001 - 2004 Anders Lund <anders@alweb.dk>

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

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "kmimetypechooser.h"

#include "kmimetypeeditor.h"
#include <qmimedatabase.h>

#include <QApplication>
#include <QDialogButtonBox>
#include <QLabel>
#include <QLayout>
#include <QPushButton>
#include <QStandardPaths>
#include <QTreeWidget>

//BEGIN KMimeTypeChooserPrivate
class KMimeTypeChooserPrivate
{
public:
    KMimeTypeChooserPrivate(KMimeTypeChooser *parent)
        : q(parent),
          mimeTypeTree(0),
          btnEditMimeType(0)
    {
    }

    void loadMimeTypes(const QStringList &selected = QStringList());

    void _k_editMimeType();
    void _k_slotCurrentChanged(QTreeWidgetItem *);
    void _k_slotSycocaDatabaseChanged(const QStringList &);

    KMimeTypeChooser *q;
    QTreeWidget *mimeTypeTree;
    QPushButton *btnEditMimeType;

    QString defaultgroup;
    QStringList groups;
    int visuals;
};
//END

static const char s_keditfiletypeExecutable[] = "keditfiletype5";

//BEGIN KMimeTypeChooser
KMimeTypeChooser::KMimeTypeChooser(const QString &text,
                                   const QStringList &selMimeTypes,
                                   const QString &defaultGroup,
                                   const QStringList &groupsToShow,
                                   int visuals,
                                   QWidget *parent)
    : QWidget(parent),
      d(new KMimeTypeChooserPrivate(this))
{
    d->defaultgroup = defaultGroup;
    d->groups = groupsToShow;
    if (visuals & EditButton) {
        if (QStandardPaths::findExecutable(QString::fromLatin1(s_keditfiletypeExecutable)).isEmpty()) {
            visuals &= ~EditButton;
        }
    }
    d->visuals = visuals;

    QVBoxLayout *vboxLayout = new QVBoxLayout(this);
    vboxLayout->setMargin(0);
    if (!text.isEmpty()) {
        vboxLayout->addWidget(new QLabel(text, this));
    }

    d->mimeTypeTree = new QTreeWidget(this);
    vboxLayout->addWidget(d->mimeTypeTree);
    QStringList headerLabels;
    headerLabels.append(tr("Mime Type"));

    if (visuals & Comments) {
        headerLabels.append(tr("Comment"));
    }
    if (visuals & Patterns) {
        headerLabels.append(tr("Patterns"));
    }

    d->mimeTypeTree->setColumnCount(headerLabels.count());
    d->mimeTypeTree->setHeaderLabels(headerLabels);
    QFontMetrics fm(d->mimeTypeTree->fontMetrics());
    d->mimeTypeTree->setColumnWidth(0, 20 * fm.height()); // big enough for most names, but not for the insanely long ones

    d->loadMimeTypes(selMimeTypes);

    if (visuals & EditButton) {
        QHBoxLayout *buttonLayout = new QHBoxLayout();
        buttonLayout->addStretch(1);
        d->btnEditMimeType = new QPushButton(tr("&Edit..."), this);
        buttonLayout->addWidget(d->btnEditMimeType);

        connect(d->btnEditMimeType, SIGNAL(clicked()), this, SLOT(_k_editMimeType()));
        d->btnEditMimeType->setEnabled(false);
        connect(d->mimeTypeTree, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
                this, SLOT(_k_editMimeType()));
        connect(d->mimeTypeTree, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
                this, SLOT(_k_slotCurrentChanged(QTreeWidgetItem*)));

        d->btnEditMimeType->setWhatsThis(tr(
                                            "Click this button to display the familiar KDE mime type editor."));

        vboxLayout->addLayout(buttonLayout);
    }
    setLayout(vboxLayout);
}

KMimeTypeChooser::~KMimeTypeChooser()
{
    delete d;
}

void KMimeTypeChooserPrivate::loadMimeTypes(const QStringList &_selectedMimeTypes)
{
    QStringList selMimeTypes;

    if (!_selectedMimeTypes.isEmpty()) {
        selMimeTypes = _selectedMimeTypes;
    } else {
        selMimeTypes = q->mimeTypes();
    }

    mimeTypeTree->clear();

    QMap<QString, QTreeWidgetItem *> groupItems;
    QMimeDatabase db;
    const QList<QMimeType> mimetypes = db.allMimeTypes();

    bool agroupisopen = false;
    QTreeWidgetItem *idefault = 0; //open this, if all other fails
    QTreeWidgetItem *firstChecked = 0; // make this one visible after the loop

    foreach (const QMimeType &mt, mimetypes) {
        const QString mimetype = mt.name();
        const int index = mimetype.indexOf(QLatin1Char('/'));
        const QString maj = mimetype.left(index);

        if (!groups.isEmpty() && !groups.contains(maj)) {
            continue;
        }

        QTreeWidgetItem *groupItem;
        QMap<QString, QTreeWidgetItem *>::Iterator mit = groupItems.find(maj);
        if (mit == groupItems.end()) {
            groupItem = new QTreeWidgetItem(mimeTypeTree, QStringList(maj));
            groupItems.insert(maj, groupItem);
            if (maj == defaultgroup) {
                idefault = groupItem;
            }
        } else {
            groupItem = mit.value();
        }

        const QString min = mimetype.mid(index + 1);
        QTreeWidgetItem *item = new QTreeWidgetItem(groupItem, QStringList(min));
        item->setIcon(0, QIcon::fromTheme(mt.iconName()));

        int cl = 1;

        if (visuals & KMimeTypeChooser::Comments) {
            item->setText(cl, mt.comment());
            cl++;
        }

        if (visuals & KMimeTypeChooser::Patterns) {
            item->setText(cl, mt.globPatterns().join(QStringLiteral("; ")));
        }

        if (selMimeTypes.contains(mimetype)) {
            item->setCheckState(0, Qt::Checked);
            groupItem->setExpanded(true);
            agroupisopen = true;
            if (!firstChecked) {
                firstChecked = item;
            }
        } else {
            item->setCheckState(0, Qt::Unchecked);
        }
    }

    if (firstChecked) {
        mimeTypeTree->scrollToItem(firstChecked);
    }

    if (!agroupisopen && idefault) {
        idefault->setExpanded(true);
        mimeTypeTree->scrollToItem(idefault);
    }
    mimeTypeTree->resizeColumnToContents(1);
}

void KMimeTypeChooserPrivate::_k_editMimeType()
{
    QTreeWidgetItem *item = mimeTypeTree->currentItem();
    if (!item || !item->parent()) {
        return;
    }
    QString mt = (item->parent())->text(0) + QLatin1Char('/') + item->text(0);
    // KF5 TODO: use a QFileSystemWatcher on one of the shared-mime-info generated files, instead.
    //q->connect( KSycoca::self(), SIGNAL(databaseChanged(QStringList)),
    //            q, SLOT(_k_slotSycocaDatabaseChanged(QStringList)) );
#pragma message("KF5 TODO: use QFileSystemWatcher to be told when keditfiletype changed a mimetype")
    // or a better idea: a QMimeDatabaseWatcher class in Qt itself

    KMimeTypeEditor::editMimeType(mt, q);
}

void KMimeTypeChooserPrivate::_k_slotCurrentChanged(QTreeWidgetItem *item)
{
    if (btnEditMimeType) {
        btnEditMimeType->setEnabled(item && item->parent());
    }
}

// TODO: see _k_editMimeType
void KMimeTypeChooserPrivate::_k_slotSycocaDatabaseChanged(const QStringList &changedResources)
{
    if (changedResources.contains(QStringLiteral("xdgdata-mime"))) {
        loadMimeTypes();
    }
}

// recursive helper for mimeTypes()
static void getCheckedItems(QList<QTreeWidgetItem *> &lst, QTreeWidgetItem *parent)
{
    for (int i = 0; i < parent->childCount(); ++i) {
        QTreeWidgetItem *item = parent->child(i);
        if (item->checkState(0) == Qt::Checked) {
            lst.append(item);
        }
        getCheckedItems(lst, item);
    }
}

static void getCheckedItems(QList<QTreeWidgetItem *> &lst, QTreeWidget *tree)
{
    for (int i = 0; i < tree->topLevelItemCount(); ++i) {
        QTreeWidgetItem *topItem = tree->topLevelItem(i);
        //if (topItem->checkState(0) == Qt::Checked)
        //    lst.append(topItem);
        getCheckedItems(lst, topItem);
    }
}

QStringList KMimeTypeChooser::mimeTypes() const
{
    QStringList mimeList;
    QList<QTreeWidgetItem *> checkedItems;
    getCheckedItems(checkedItems, d->mimeTypeTree);
    foreach (QTreeWidgetItem *item, checkedItems) {
        mimeList.append(item->parent()->text(0) + QLatin1Char('/') + item->text(0));
    }
    return mimeList;
}

QStringList KMimeTypeChooser::patterns() const
{
    QStringList patternList;
    QList<QTreeWidgetItem *> checkedItems;
    getCheckedItems(checkedItems, d->mimeTypeTree);
    QMimeDatabase db;
    foreach (QTreeWidgetItem *item, checkedItems) {
        QMimeType mime = db.mimeTypeForName(item->parent()->text(0) + QLatin1Char('/') + item->text(0));
        Q_ASSERT(mime.isValid());
        patternList += mime.globPatterns();
    }
    return patternList;
}
//END

//BEGIN KMimeTypeChooserDialog::Private

class KMimeTypeChooserDialog::Private
{
public:
    Private(KMimeTypeChooserDialog *parent)
        : q(parent)
    {
    }

    void init();

    KMimeTypeChooserDialog *q;
    KMimeTypeChooser *m_chooser;
};

//END

//BEGIN KMimeTypeChooserDialog
KMimeTypeChooserDialog::KMimeTypeChooserDialog(
    const QString &caption,
    const QString &text,
    const QStringList &selMimeTypes,
    const QString &defaultGroup,
    const QStringList &groupsToShow,
    int visuals,
    QWidget *parent)
    : QDialog(parent), d(new Private(this))
{
    setWindowTitle(caption);

    d->m_chooser = new KMimeTypeChooser(text, selMimeTypes,
                                        defaultGroup, groupsToShow, visuals,
                                        this);
    d->init();
}

KMimeTypeChooserDialog::KMimeTypeChooserDialog(
    const QString &caption,
    const QString &text,
    const QStringList &selMimeTypes,
    const QString &defaultGroup,
    QWidget *parent)
    : QDialog(parent), d(new Private(this))
{
    setWindowTitle(caption);

    d->m_chooser = new KMimeTypeChooser(text, selMimeTypes,
                                        defaultGroup, QStringList(),
                                        KMimeTypeChooser::Comments | KMimeTypeChooser::Patterns | KMimeTypeChooser::EditButton,
                                        this);
    d->init();
}

KMimeTypeChooser *KMimeTypeChooserDialog::chooser()
{
    return d->m_chooser;
}

void KMimeTypeChooserDialog::Private::init()
{
    QVBoxLayout *layout = new QVBoxLayout;
    q->setLayout(layout);

    layout->addWidget(m_chooser);

    QDialogButtonBox *buttonBox = new QDialogButtonBox(q);
    buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    QObject::connect(buttonBox, &QDialogButtonBox::accepted, q, &QDialog::accept);
    QObject::connect(buttonBox, &QDialogButtonBox::rejected, q, &QDialog::reject);
    layout->addWidget(buttonBox);
}

KMimeTypeChooserDialog::~KMimeTypeChooserDialog()
{
    delete d;
}

//END KMimeTypeChooserDialog

#include "moc_kmimetypechooser.cpp"