1 /* This file is part of the KDE libraries
2 Copyright (C) 2001 - 2004 Anders Lund <anders@alweb.dk>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
19 #include "kmimetypechooser.h"
22 #include <kiconloader.h>
24 #include <kmimetype.h>
31 #include <QPushButton>
32 #include <QTreeWidget>
33 #include <kconfiggroup.h>
35 //BEGIN KMimeTypeChooserPrivate
36 class KMimeTypeChooserPrivate
39 KMimeTypeChooserPrivate( KMimeTypeChooser
*parent
)
46 void loadMimeTypes( const QStringList
&selected
= QStringList() );
48 void _k_editMimeType();
49 void _k_slotCurrentChanged(QTreeWidgetItem
*);
50 void _k_slotSycocaDatabaseChanged(const QStringList
&);
53 QTreeWidget
*mimeTypeTree
;
54 QPushButton
*btnEditMimeType
;
62 //BEGIN KMimeTypeChooser
63 KMimeTypeChooser::KMimeTypeChooser( const QString
&text
,
64 const QStringList
&selMimeTypes
,
65 const QString
&defaultGroup
,
66 const QStringList
&groupsToShow
,
70 d(new KMimeTypeChooserPrivate(this))
72 d
->defaultgroup
= defaultGroup
;
73 d
->groups
= groupsToShow
;
76 setSpacing( KDialog::spacingHint() );
78 if ( !text
.isEmpty() )
80 new QLabel( text
, this );
83 d
->mimeTypeTree
= new QTreeWidget( this );
84 QStringList headerLabels
;
85 headerLabels
.append( i18n("Mime Type") );
86 // d->mimeTypeTree->setColumnWidthMode( 0, QListView::Manual );
88 if ( visuals
& Comments
) {
89 headerLabels
.append( i18n("Comment") );
90 //d->mimeTypeTree->setColumnWidthMode( 1, Q3ListView::Manual );
92 if ( visuals
& Patterns
) {
93 headerLabels
.append( i18n("Patterns") );
96 // d->mimeTypeTree->setRootIsDecorated( true );
97 d
->mimeTypeTree
->setColumnCount(headerLabels
.count());
98 d
->mimeTypeTree
->setHeaderLabels(headerLabels
);
100 d
->loadMimeTypes( selMimeTypes
);
102 if (visuals
& EditButton
)
104 KHBox
*btns
= new KHBox( this );
105 ((QBoxLayout
*)btns
->layout())->addStretch(1);
106 d
->btnEditMimeType
= new QPushButton( i18n("&Edit..."), btns
);
108 connect( d
->btnEditMimeType
, SIGNAL(clicked()), this, SLOT(_k_editMimeType()) );
109 d
->btnEditMimeType
->setEnabled( false );
110 connect( d
->mimeTypeTree
, SIGNAL(itemDoubleClicked(QTreeWidgetItem
*,int)),
111 this, SLOT(_k_editMimeType()));
112 connect( d
->mimeTypeTree
, SIGNAL(currentItemChanged(QTreeWidgetItem
*,QTreeWidgetItem
*)),
113 this, SLOT(_k_slotCurrentChanged(QTreeWidgetItem
*)) );
115 d
->btnEditMimeType
->setWhatsThis(i18n(
116 "Click this button to display the familiar KDE mime type editor.") );
120 KMimeTypeChooser::~KMimeTypeChooser()
125 void KMimeTypeChooserPrivate::loadMimeTypes( const QStringList
&_selectedMimeTypes
)
127 QStringList selMimeTypes
;
129 if ( !_selectedMimeTypes
.isEmpty() )
130 selMimeTypes
= _selectedMimeTypes
;
132 selMimeTypes
= q
->mimeTypes();
134 mimeTypeTree
->clear();
136 QMap
<QString
, QTreeWidgetItem
*> groupItems
;
137 const KMimeType::List mimetypes
= KMimeType::allMimeTypes();
139 bool agroupisopen
= false;
140 QTreeWidgetItem
*idefault
= 0; //open this, if all other fails
141 QTreeWidgetItem
*firstChecked
= 0; // make this one visible after the loop
143 foreach (const KMimeType::Ptr
& mt
, mimetypes
)
145 const QString mimetype
= mt
->name();
146 const int index
= mimetype
.indexOf('/');
147 const QString maj
= mimetype
.left(index
);
149 if ( !groups
.isEmpty() && !groups
.contains( maj
) )
152 QTreeWidgetItem
*groupItem
;
153 QMap
<QString
,QTreeWidgetItem
*>::Iterator mit
= groupItems
.find( maj
);
154 if ( mit
== groupItems
.end() )
156 groupItem
= new QTreeWidgetItem( mimeTypeTree
, QStringList(maj
) );
157 groupItems
.insert( maj
, groupItem
);
158 if ( maj
== defaultgroup
)
159 idefault
= groupItem
;
162 groupItem
= mit
.value();
164 const QString min
= mimetype
.mid(index
+1);
165 QTreeWidgetItem
*item
= new QTreeWidgetItem( groupItem
, QStringList(min
) );
166 item
->setIcon( 0, SmallIcon( mt
->iconName() ) );
170 if ( visuals
& KMimeTypeChooser::Comments
)
172 item
->setText( cl
, mt
->comment() );
176 if ( visuals
& KMimeTypeChooser::Patterns
)
177 item
->setText( cl
, mt
->patterns().join("; ") );
179 if ( selMimeTypes
.contains(mimetype
) ) {
180 item
->setCheckState( 0, Qt::Checked
);
181 groupItem
->setExpanded( true );
186 item
->setCheckState( 0, Qt::Unchecked
);
191 mimeTypeTree
->scrollToItem( firstChecked
);
193 if ( !agroupisopen
&& idefault
)
195 idefault
->setExpanded( true );
196 mimeTypeTree
->scrollToItem( idefault
);
200 void KMimeTypeChooserPrivate::_k_editMimeType()
202 QTreeWidgetItem
* item
= mimeTypeTree
->currentItem();
203 if ( !item
|| !item
->parent() )
205 QString mt
= (item
->parent())->text(0) + '/' + item
->text(0);
206 // thanks to libkonq/konq_operations.cc
207 q
->connect( KSycoca::self(), SIGNAL(databaseChanged(QStringList
)),
208 q
, SLOT(_k_slotSycocaDatabaseChanged(QStringList
)) );
209 QString keditfiletype
= QString::fromLatin1("keditfiletype");
210 KRun::runCommand( keditfiletype
212 + " --parent " + QString::number( (ulong
)q
->topLevelWidget()->winId())
214 + ' ' + KShell::quoteArg(mt
),
215 keditfiletype
, keditfiletype
/*unused*/, q
->topLevelWidget());
218 void KMimeTypeChooserPrivate::_k_slotCurrentChanged(QTreeWidgetItem
* item
)
220 if ( btnEditMimeType
)
221 btnEditMimeType
->setEnabled( item
->parent() );
224 void KMimeTypeChooserPrivate::_k_slotSycocaDatabaseChanged(const QStringList
& changedResources
)
226 if (changedResources
.contains("xdgdata-mime"))
230 // recursive helper for mimeTypes()
231 static void getCheckedItems(QList
<QTreeWidgetItem
*> &lst
, QTreeWidgetItem
* parent
)
233 for (int i
= 0; i
< parent
->childCount(); ++i
) {
234 QTreeWidgetItem
* item
= parent
->child(i
);
235 if (item
->checkState(0) == Qt::Checked
)
237 getCheckedItems(lst
, item
);
241 static void getCheckedItems(QList
<QTreeWidgetItem
*>& lst
, QTreeWidget
* tree
)
243 for (int i
= 0; i
< tree
->topLevelItemCount(); ++i
) {
244 QTreeWidgetItem
* topItem
= tree
->topLevelItem(i
);
245 //if (topItem->checkState(0) == Qt::Checked)
246 // lst.append(topItem);
247 getCheckedItems(lst
, topItem
);
251 QStringList
KMimeTypeChooser::mimeTypes() const
253 QStringList mimeList
;
254 QList
<QTreeWidgetItem
*> checkedItems
;
255 getCheckedItems(checkedItems
, d
->mimeTypeTree
);
256 foreach(QTreeWidgetItem
* item
, checkedItems
) {
257 mimeList
.append( item
->parent()->text(0) + '/' + item
->text(0) );
262 QStringList
KMimeTypeChooser::patterns() const
264 QStringList patternList
;
265 QList
<QTreeWidgetItem
*> checkedItems
;
266 getCheckedItems(checkedItems
, d
->mimeTypeTree
);
267 foreach(QTreeWidgetItem
* item
, checkedItems
) {
268 KMimeType::Ptr p
= KMimeType::mimeType( item
->parent()->text(0) + '/' + item
->text(0) );
270 patternList
+= p
->patterns();
276 //BEGIN KMimeTypeChooserDialog::Private
278 class KMimeTypeChooserDialog::Private
281 Private( KMimeTypeChooserDialog
*parent
)
288 KMimeTypeChooserDialog
*q
;
289 KMimeTypeChooser
*m_chooser
;
294 //BEGIN KMimeTypeChooserDialog
295 KMimeTypeChooserDialog::KMimeTypeChooserDialog(
296 const QString
&caption
,
298 const QStringList
&selMimeTypes
,
299 const QString
&defaultGroup
,
300 const QStringList
&groupsToShow
,
303 : KDialog( parent
), d(new Private(this))
305 setCaption( caption
);
308 d
->m_chooser
= new KMimeTypeChooser( text
, selMimeTypes
,
309 defaultGroup
, groupsToShow
, visuals
,
311 setMainWidget(d
->m_chooser
);
314 KMimeTypeChooserDialog::KMimeTypeChooserDialog(
315 const QString
&caption
,
317 const QStringList
&selMimeTypes
,
318 const QString
&defaultGroup
,
320 : KDialog( parent
), d(new Private(this))
322 setCaption( caption
);
325 d
->m_chooser
= new KMimeTypeChooser( text
, selMimeTypes
,
326 defaultGroup
, QStringList(),
327 KMimeTypeChooser::Comments
|KMimeTypeChooser::Patterns
|KMimeTypeChooser::EditButton
,
329 setMainWidget(d
->m_chooser
);
332 KMimeTypeChooser
* KMimeTypeChooserDialog::chooser()
337 void KMimeTypeChooserDialog::Private::init()
339 q
->setButtons( Cancel
| Ok
);
341 q
->setDefaultButton( Ok
);
343 KConfigGroup
group( KGlobal::config(), "KMimeTypeChooserDialog");
344 q
->resize( group
.readEntry("size", QSize(500,400)));
347 KMimeTypeChooserDialog::~KMimeTypeChooserDialog()
349 KConfigGroup
group( KGlobal::config(), "KMimeTypeChooserDialog");
350 group
.writeEntry("size", size());
355 //END KMimeTypeChooserDialog
357 // kate: space-indent on; indent-width 2; replace-tabs on;
358 #include "kmimetypechooser.moc"