• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KIO

  • sources
  • kde-4.14
  • kdelibs
  • kio
  • kio
kmimetypechooser.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2001 - 2004 Anders Lund <anders@alweb.dk>
3 
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.
7 
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.
12 
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.
17 */
18 
19 #include "kmimetypechooser.h"
20 
21 #include <klocale.h>
22 #include <kmimetype.h>
23 #include <kshell.h>
24 #include <krun.h>
25 #include <ksycoca.h>
26 
27 #include <QLabel>
28 #include <QLayout>
29 #include <QPushButton>
30 #include <QTreeWidget>
31 #include <kconfiggroup.h>
32 
33 //BEGIN KMimeTypeChooserPrivate
34 class KMimeTypeChooserPrivate
35 {
36  public:
37  KMimeTypeChooserPrivate( KMimeTypeChooser *parent )
38  : q(parent),
39  mimeTypeTree(0),
40  btnEditMimeType(0)
41  {
42  }
43 
44  void loadMimeTypes( const QStringList &selected = QStringList() );
45 
46  void _k_editMimeType();
47  void _k_slotCurrentChanged(QTreeWidgetItem*);
48  void _k_slotSycocaDatabaseChanged(const QStringList&);
49 
50  KMimeTypeChooser *q;
51  QTreeWidget *mimeTypeTree;
52  QPushButton *btnEditMimeType;
53 
54  QString defaultgroup;
55  QStringList groups;
56  int visuals;
57 };
58 //END
59 
60 //BEGIN KMimeTypeChooser
61 KMimeTypeChooser::KMimeTypeChooser( const QString &text,
62  const QStringList &selMimeTypes,
63  const QString &defaultGroup,
64  const QStringList &groupsToShow,
65  int visuals,
66  QWidget *parent )
67  : KVBox( parent ),
68  d(new KMimeTypeChooserPrivate(this))
69 {
70  d->defaultgroup = defaultGroup;
71  d->groups = groupsToShow;
72  d->visuals = visuals;
73  setSpacing(-1);
74 
75  if ( !text.isEmpty() )
76  {
77  new QLabel( text, this );
78  }
79 
80  d->mimeTypeTree = new QTreeWidget( this );
81  QStringList headerLabels;
82  headerLabels.append( i18n("Mime Type") );
83 
84  if ( visuals & Comments ) {
85  headerLabels.append( i18n("Comment") );
86  }
87  if ( visuals & Patterns ) {
88  headerLabels.append( i18n("Patterns") );
89  }
90 
91  d->mimeTypeTree->setColumnCount(headerLabels.count());
92  d->mimeTypeTree->setHeaderLabels(headerLabels);
93  QFontMetrics fm(d->mimeTypeTree->fontMetrics());
94  d->mimeTypeTree->setColumnWidth(0, 20 * fm.height()); // big enough for most names, but not for the insanely long ones
95 
96  d->loadMimeTypes( selMimeTypes );
97 
98  if (visuals & EditButton)
99  {
100  KHBox *btns = new KHBox( this );
101  ((QBoxLayout*)btns->layout())->addStretch(1);
102  d->btnEditMimeType = new QPushButton( i18n("&Edit..."), btns );
103 
104  connect( d->btnEditMimeType, SIGNAL(clicked()), this, SLOT(_k_editMimeType()) );
105  d->btnEditMimeType->setEnabled( false );
106  connect( d->mimeTypeTree, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
107  this, SLOT(_k_editMimeType()));
108  connect( d->mimeTypeTree, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
109  this, SLOT(_k_slotCurrentChanged(QTreeWidgetItem*)) );
110 
111  d->btnEditMimeType->setWhatsThis(i18n(
112  "Click this button to display the familiar KDE mime type editor.") );
113  }
114 }
115 
116 KMimeTypeChooser::~KMimeTypeChooser()
117 {
118  delete d;
119 }
120 
121 void KMimeTypeChooserPrivate::loadMimeTypes( const QStringList &_selectedMimeTypes )
122 {
123  QStringList selMimeTypes;
124 
125  if ( !_selectedMimeTypes.isEmpty() )
126  selMimeTypes = _selectedMimeTypes;
127  else
128  selMimeTypes = q->mimeTypes();
129 
130  mimeTypeTree->clear();
131 
132  QMap<QString, QTreeWidgetItem*> groupItems;
133  const KMimeType::List mimetypes = KMimeType::allMimeTypes();
134 
135  bool agroupisopen = false;
136  QTreeWidgetItem *idefault = 0; //open this, if all other fails
137  QTreeWidgetItem *firstChecked = 0; // make this one visible after the loop
138 
139  foreach (const KMimeType::Ptr& mt, mimetypes)
140  {
141  const QString mimetype = mt->name();
142  const int index = mimetype.indexOf('/');
143  const QString maj = mimetype.left(index);
144 
145  if ( !groups.isEmpty() && !groups.contains( maj ) )
146  continue;
147 
148  QTreeWidgetItem *groupItem;
149  QMap<QString,QTreeWidgetItem*>::Iterator mit = groupItems.find( maj );
150  if ( mit == groupItems.end() )
151  {
152  groupItem = new QTreeWidgetItem( mimeTypeTree, QStringList(maj) );
153  groupItems.insert( maj, groupItem );
154  if ( maj == defaultgroup )
155  idefault = groupItem;
156  }
157  else
158  groupItem = mit.value();
159 
160  const QString min = mimetype.mid(index+1);
161  QTreeWidgetItem *item = new QTreeWidgetItem( groupItem, QStringList(min) );
162  item->setIcon( 0, KIcon( mt->iconName() ) );
163 
164  int cl = 1;
165 
166  if ( visuals & KMimeTypeChooser::Comments )
167  {
168  item->setText( cl, mt->comment() );
169  cl++;
170  }
171 
172  if ( visuals & KMimeTypeChooser::Patterns )
173  item->setText( cl, mt->patterns().join("; ") );
174 
175  if ( selMimeTypes.contains(mimetype) ) {
176  item->setCheckState( 0, Qt::Checked );
177  groupItem->setExpanded( true );
178  agroupisopen = true;
179  if ( !firstChecked )
180  firstChecked = item;
181  } else {
182  item->setCheckState( 0, Qt::Unchecked );
183  }
184  }
185 
186  if ( firstChecked )
187  mimeTypeTree->scrollToItem( firstChecked );
188 
189  if ( !agroupisopen && idefault )
190  {
191  idefault->setExpanded( true );
192  mimeTypeTree->scrollToItem( idefault );
193  }
194  mimeTypeTree->resizeColumnToContents(1);
195 }
196 
197 void KMimeTypeChooserPrivate::_k_editMimeType()
198 {
199  QTreeWidgetItem* item = mimeTypeTree->currentItem();
200  if ( !item || !item->parent() )
201  return;
202  QString mt = (item->parent())->text(0) + '/' + item->text(0);
203  // thanks to libkonq/konq_operations.cc
204  q->connect( KSycoca::self(), SIGNAL(databaseChanged(QStringList)),
205  q, SLOT(_k_slotSycocaDatabaseChanged(QStringList)) );
206  QString keditfiletype = QString::fromLatin1("keditfiletype");
207  KRun::runCommand( keditfiletype
208 #ifndef Q_OS_WIN
209  + " --parent " + QString::number( (ulong)q->topLevelWidget()->winId())
210 #endif
211  + " --caption " + KShell::quoteArg(KGlobal::caption())
212  + ' ' + KShell::quoteArg(mt),
213  keditfiletype, keditfiletype /*unused*/, q->topLevelWidget());
214 }
215 
216 void KMimeTypeChooserPrivate::_k_slotCurrentChanged(QTreeWidgetItem* item)
217 {
218  if ( btnEditMimeType )
219  btnEditMimeType->setEnabled(item && item->parent());
220 }
221 
222 void KMimeTypeChooserPrivate::_k_slotSycocaDatabaseChanged(const QStringList& changedResources)
223 {
224  if (changedResources.contains("xdgdata-mime"))
225  loadMimeTypes();
226 }
227 
228 // recursive helper for mimeTypes()
229 static void getCheckedItems(QList<QTreeWidgetItem *> &lst, QTreeWidgetItem* parent)
230 {
231  for (int i = 0; i < parent->childCount(); ++i ) {
232  QTreeWidgetItem* item = parent->child(i);
233  if (item->checkState(0) == Qt::Checked)
234  lst.append(item);
235  getCheckedItems(lst, item);
236  }
237 }
238 
239 static void getCheckedItems(QList<QTreeWidgetItem *>& lst, QTreeWidget* tree)
240 {
241  for (int i = 0; i < tree->topLevelItemCount(); ++i ) {
242  QTreeWidgetItem* topItem = tree->topLevelItem(i);
243  //if (topItem->checkState(0) == Qt::Checked)
244  // lst.append(topItem);
245  getCheckedItems(lst, topItem);
246  }
247 }
248 
249 QStringList KMimeTypeChooser::mimeTypes() const
250 {
251  QStringList mimeList;
252  QList<QTreeWidgetItem *> checkedItems;
253  getCheckedItems(checkedItems, d->mimeTypeTree);
254  foreach(QTreeWidgetItem* item, checkedItems) {
255  mimeList.append( item->parent()->text(0) + '/' + item->text(0) );
256  }
257  return mimeList;
258 }
259 
260 QStringList KMimeTypeChooser::patterns() const
261 {
262  QStringList patternList;
263  QList<QTreeWidgetItem *> checkedItems;
264  getCheckedItems(checkedItems, d->mimeTypeTree);
265  foreach(QTreeWidgetItem* item, checkedItems) {
266  KMimeType::Ptr p = KMimeType::mimeType( item->parent()->text(0) + '/' + item->text(0) );
267  Q_ASSERT(p);
268  patternList += p->patterns();
269  }
270  return patternList;
271 }
272 //END
273 
274 //BEGIN KMimeTypeChooserDialog::Private
275 
276 class KMimeTypeChooserDialog::Private
277 {
278  public:
279  Private( KMimeTypeChooserDialog *parent )
280  : q(parent)
281  {
282  }
283 
284  void init();
285 
286  KMimeTypeChooserDialog *q;
287  KMimeTypeChooser *m_chooser;
288 };
289 
290 //END
291 
292 //BEGIN KMimeTypeChooserDialog
293 KMimeTypeChooserDialog::KMimeTypeChooserDialog(
294  const QString &caption,
295  const QString& text,
296  const QStringList &selMimeTypes,
297  const QString &defaultGroup,
298  const QStringList &groupsToShow,
299  int visuals,
300  QWidget *parent )
301  : KDialog( parent ), d(new Private(this))
302 {
303  setCaption( caption );
304  d->init();
305 
306  d->m_chooser = new KMimeTypeChooser( text, selMimeTypes,
307  defaultGroup, groupsToShow, visuals,
308  this );
309  setMainWidget(d->m_chooser);
310 }
311 
312 KMimeTypeChooserDialog::KMimeTypeChooserDialog(
313  const QString &caption,
314  const QString& text,
315  const QStringList &selMimeTypes,
316  const QString &defaultGroup,
317  QWidget *parent )
318  : KDialog( parent ), d(new Private(this))
319 {
320  setCaption( caption );
321  d->init();
322 
323  d->m_chooser = new KMimeTypeChooser( text, selMimeTypes,
324  defaultGroup, QStringList(),
325  KMimeTypeChooser::Comments|KMimeTypeChooser::Patterns|KMimeTypeChooser::EditButton,
326  this );
327  setMainWidget(d->m_chooser);
328 }
329 
330 KMimeTypeChooser* KMimeTypeChooserDialog::chooser()
331 {
332  return d->m_chooser;
333 }
334 
335 void KMimeTypeChooserDialog::Private::init()
336 {
337  q->setButtons( Cancel | Ok );
338  q->setModal( true );
339  q->setDefaultButton( Ok );
340 
341  KConfigGroup group( KGlobal::config(), "KMimeTypeChooserDialog");
342  q->resize( group.readEntry("size", QSize(600,500)));
343 }
344 
345 KMimeTypeChooserDialog::~KMimeTypeChooserDialog()
346 {
347  KConfigGroup group( KGlobal::config(), "KMimeTypeChooserDialog");
348  group.writeEntry("size", size());
349 
350  delete d;
351 }
352 
353 //END KMimeTypeChooserDialog
354 
355 // kate: space-indent on; indent-width 2; replace-tabs on;
356 #include "kmimetypechooser.moc"
QWidget::layout
QLayout * layout() const
KGlobal::caption
QString caption()
i18n
QString i18n(const char *text)
QList::clear
void clear()
QString::indexOf
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
QWidget
KVBox
QTreeWidgetItem::checkState
Qt::CheckState checkState(int column) const
kmimetype.h
QTreeWidgetItem::child
QTreeWidgetItem * child(int index) const
group
KIO::mimetype
MimetypeJob * mimetype(const KUrl &url, JobFlags flags=DefaultFlags)
Find mimetype for one file or directory.
Definition: job.cpp:1856
QMap
Definition: netaccess.h:36
kshell.h
KHBox::KHBox
KHBox(QWidget *parent=0)
QStringList::contains
bool contains(const QString &str, Qt::CaseSensitivity cs) const
QTreeWidgetItem::setIcon
void setIcon(int column, const QIcon &icon)
KConfigGroup::writeEntry
void writeEntry(const QString &key, const QVariant &value, WriteConfigFlags pFlags=Normal)
KMimeTypeChooser::Comments
Show the Mimetypes Comment field in a column ("HTML Document").
Definition: kmimetypechooser.h:45
KDialog
QFontMetrics
KMimeTypeChooser::Patterns
Show the Mimetypes Patterns field in a column ("*.html;*.htm").
Definition: kmimetypechooser.h:46
klocale.h
KGlobal::config
KSharedConfigPtr config()
QWidget::size
QSize size() const
QTreeWidget
QObject::name
const char * name() const
QString::number
QString number(int n, int base)
QList::count
int count(const T &value) const
KMimeTypeChooser::~KMimeTypeChooser
~KMimeTypeChooser()
Definition: kmimetypechooser.cpp:116
QList::append
void append(const T &value)
QList::isEmpty
bool isEmpty() const
QString::isEmpty
bool isEmpty() const
KRun::runCommand
static bool runCommand(const QString &cmd, QWidget *window)
Run the given shell command and notifies KDE of the starting of the application.
Definition: krun.cpp:1057
KIcon
getCheckedItems
static void getCheckedItems(QList< QTreeWidgetItem * > &lst, QTreeWidgetItem *parent)
Definition: kmimetypechooser.cpp:229
QString
QList
QMap::end
iterator end()
QStringList
KMimeTypeChooserDialog
A Dialog to choose some mimetypes.
Definition: kmimetypechooser.h:111
KShell::quoteArg
QString quoteArg(const QString &arg)
QSize
QTreeWidgetItem::parent
QTreeWidgetItem * parent() const
KMimeTypeChooser::mimeTypes
QStringList mimeTypes() const
Definition: kmimetypechooser.cpp:249
ksycoca.h
KConfigGroup
krun.h
QTreeWidgetItem::setExpanded
void setExpanded(bool expand)
QTreeWidgetItem::setCheckState
void setCheckState(int column, Qt::CheckState state)
KMimeTypeChooser::EditButton
Show the "Edit" button, allowing to edit the selected type.
Definition: kmimetypechooser.h:47
QString::mid
QString mid(int position, int n) const
QTreeWidgetItem
KMimeTypeChooser::KMimeTypeChooser
KMimeTypeChooser(const QString &text=QString(), const QStringList &selectedMimeTypes=QStringList(), const QString &defaultGroup=QString(), const QStringList &groupsToShow=QStringList(), int visuals=Comments|Patterns|EditButton, QWidget *parent=0)
Create a new KMimeTypeChooser.
Definition: kmimetypechooser.cpp:61
kmimetypechooser.h
QWidget::setCaption
void setCaption(const QString &c)
KHBox::setSpacing
void setSpacing(int space)
QTreeWidgetItem::setText
void setText(int column, const QString &text)
QString::left
QString left(int n) const
KMimeTypeChooserDialog::chooser
KMimeTypeChooser * chooser()
Definition: kmimetypechooser.cpp:330
QString::fromLatin1
QString fromLatin1(const char *str, int size)
QPushButton
QMap::insert
iterator insert(const Key &key, const T &value)
KMimeTypeChooser::patterns
QStringList patterns() const
Definition: kmimetypechooser.cpp:260
KHBox
QTreeWidget::topLevelItem
QTreeWidgetItem * topLevelItem(int index) const
KMimeTypeChooser
This widget provides a checkable list of all available mimetypes, and a list of selected ones...
Definition: kmimetypechooser.h:36
QTreeWidget::topLevelItemCount
topLevelItemCount
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QLabel
QObject::parent
QObject * parent() const
QTreeWidgetItem::childCount
int childCount() const
QTreeWidgetItem::text
QString text(int column) const
KMimeTypeChooserDialog::KMimeTypeChooserDialog
KMimeTypeChooserDialog(const QString &caption=QString(), const QString &text=QString(), const QStringList &selectedMimeTypes=QStringList(), const QString &defaultGroup=QString(), const QStringList &groupsToShow=QStringList(), int visuals=KMimeTypeChooser::Comments|KMimeTypeChooser::Patterns|KMimeTypeChooser::EditButton, QWidget *parent=0)
Create a KMimeTypeChooser dialog.
Definition: kmimetypechooser.cpp:293
KSycoca::self
static KSycoca * self()
QBoxLayout
QMap::find
iterator find(const Key &key)
kconfiggroup.h
KMimeTypeChooserDialog::~KMimeTypeChooserDialog
~KMimeTypeChooserDialog()
Definition: kmimetypechooser.cpp:345
QMap::value
const T value(const Key &key) const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:24:53 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal