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

KIO

  • 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 // d->mimeTypeTree->setColumnWidthMode( 0, QListView::Manual );
84 
85  if ( visuals & Comments ) {
86  headerLabels.append( i18n("Comment") );
87  //d->mimeTypeTree->setColumnWidthMode( 1, Q3ListView::Manual );
88  }
89  if ( visuals & Patterns ) {
90  headerLabels.append( i18n("Patterns") );
91  }
92 
93 // d->mimeTypeTree->setRootIsDecorated( true );
94  d->mimeTypeTree->setColumnCount(headerLabels.count());
95  d->mimeTypeTree->setHeaderLabels(headerLabels);
96 
97  d->loadMimeTypes( selMimeTypes );
98 
99  if (visuals & EditButton)
100  {
101  KHBox *btns = new KHBox( this );
102  ((QBoxLayout*)btns->layout())->addStretch(1);
103  d->btnEditMimeType = new QPushButton( i18n("&Edit..."), btns );
104 
105  connect( d->btnEditMimeType, SIGNAL(clicked()), this, SLOT(_k_editMimeType()) );
106  d->btnEditMimeType->setEnabled( false );
107  connect( d->mimeTypeTree, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
108  this, SLOT(_k_editMimeType()));
109  connect( d->mimeTypeTree, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
110  this, SLOT(_k_slotCurrentChanged(QTreeWidgetItem*)) );
111 
112  d->btnEditMimeType->setWhatsThis(i18n(
113  "Click this button to display the familiar KDE mime type editor.") );
114  }
115 }
116 
117 KMimeTypeChooser::~KMimeTypeChooser()
118 {
119  delete d;
120 }
121 
122 void KMimeTypeChooserPrivate::loadMimeTypes( const QStringList &_selectedMimeTypes )
123 {
124  QStringList selMimeTypes;
125 
126  if ( !_selectedMimeTypes.isEmpty() )
127  selMimeTypes = _selectedMimeTypes;
128  else
129  selMimeTypes = q->mimeTypes();
130 
131  mimeTypeTree->clear();
132 
133  QMap<QString, QTreeWidgetItem*> groupItems;
134  const KMimeType::List mimetypes = KMimeType::allMimeTypes();
135 
136  bool agroupisopen = false;
137  QTreeWidgetItem *idefault = 0; //open this, if all other fails
138  QTreeWidgetItem *firstChecked = 0; // make this one visible after the loop
139 
140  foreach (const KMimeType::Ptr& mt, mimetypes)
141  {
142  const QString mimetype = mt->name();
143  const int index = mimetype.indexOf('/');
144  const QString maj = mimetype.left(index);
145 
146  if ( !groups.isEmpty() && !groups.contains( maj ) )
147  continue;
148 
149  QTreeWidgetItem *groupItem;
150  QMap<QString,QTreeWidgetItem*>::Iterator mit = groupItems.find( maj );
151  if ( mit == groupItems.end() )
152  {
153  groupItem = new QTreeWidgetItem( mimeTypeTree, QStringList(maj) );
154  groupItems.insert( maj, groupItem );
155  if ( maj == defaultgroup )
156  idefault = groupItem;
157  }
158  else
159  groupItem = mit.value();
160 
161  const QString min = mimetype.mid(index+1);
162  QTreeWidgetItem *item = new QTreeWidgetItem( groupItem, QStringList(min) );
163  item->setIcon( 0, KIcon( mt->iconName() ) );
164 
165  int cl = 1;
166 
167  if ( visuals & KMimeTypeChooser::Comments )
168  {
169  item->setText( cl, mt->comment() );
170  cl++;
171  }
172 
173  if ( visuals & KMimeTypeChooser::Patterns )
174  item->setText( cl, mt->patterns().join("; ") );
175 
176  if ( selMimeTypes.contains(mimetype) ) {
177  item->setCheckState( 0, Qt::Checked );
178  groupItem->setExpanded( true );
179  agroupisopen = true;
180  if ( !firstChecked )
181  firstChecked = item;
182  } else {
183  item->setCheckState( 0, Qt::Unchecked );
184  }
185  }
186 
187  if ( firstChecked )
188  mimeTypeTree->scrollToItem( firstChecked );
189 
190  if ( !agroupisopen && idefault )
191  {
192  idefault->setExpanded( true );
193  mimeTypeTree->scrollToItem( idefault );
194  }
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  + ' ' + KShell::quoteArg(mt),
212  keditfiletype, keditfiletype /*unused*/, q->topLevelWidget());
213 }
214 
215 void KMimeTypeChooserPrivate::_k_slotCurrentChanged(QTreeWidgetItem* item)
216 {
217  if ( btnEditMimeType )
218  btnEditMimeType->setEnabled( item->parent() );
219 }
220 
221 void KMimeTypeChooserPrivate::_k_slotSycocaDatabaseChanged(const QStringList& changedResources)
222 {
223  if (changedResources.contains("xdgdata-mime"))
224  loadMimeTypes();
225 }
226 
227 // recursive helper for mimeTypes()
228 static void getCheckedItems(QList<QTreeWidgetItem *> &lst, QTreeWidgetItem* parent)
229 {
230  for (int i = 0; i < parent->childCount(); ++i ) {
231  QTreeWidgetItem* item = parent->child(i);
232  if (item->checkState(0) == Qt::Checked)
233  lst.append(item);
234  getCheckedItems(lst, item);
235  }
236 }
237 
238 static void getCheckedItems(QList<QTreeWidgetItem *>& lst, QTreeWidget* tree)
239 {
240  for (int i = 0; i < tree->topLevelItemCount(); ++i ) {
241  QTreeWidgetItem* topItem = tree->topLevelItem(i);
242  //if (topItem->checkState(0) == Qt::Checked)
243  // lst.append(topItem);
244  getCheckedItems(lst, topItem);
245  }
246 }
247 
248 QStringList KMimeTypeChooser::mimeTypes() const
249 {
250  QStringList mimeList;
251  QList<QTreeWidgetItem *> checkedItems;
252  getCheckedItems(checkedItems, d->mimeTypeTree);
253  foreach(QTreeWidgetItem* item, checkedItems) {
254  mimeList.append( item->parent()->text(0) + '/' + item->text(0) );
255  }
256  return mimeList;
257 }
258 
259 QStringList KMimeTypeChooser::patterns() const
260 {
261  QStringList patternList;
262  QList<QTreeWidgetItem *> checkedItems;
263  getCheckedItems(checkedItems, d->mimeTypeTree);
264  foreach(QTreeWidgetItem* item, checkedItems) {
265  KMimeType::Ptr p = KMimeType::mimeType( item->parent()->text(0) + '/' + item->text(0) );
266  Q_ASSERT(p);
267  patternList += p->patterns();
268  }
269  return patternList;
270 }
271 //END
272 
273 //BEGIN KMimeTypeChooserDialog::Private
274 
275 class KMimeTypeChooserDialog::Private
276 {
277  public:
278  Private( KMimeTypeChooserDialog *parent )
279  : q(parent)
280  {
281  }
282 
283  void init();
284 
285  KMimeTypeChooserDialog *q;
286  KMimeTypeChooser *m_chooser;
287 };
288 
289 //END
290 
291 //BEGIN KMimeTypeChooserDialog
292 KMimeTypeChooserDialog::KMimeTypeChooserDialog(
293  const QString &caption,
294  const QString& text,
295  const QStringList &selMimeTypes,
296  const QString &defaultGroup,
297  const QStringList &groupsToShow,
298  int visuals,
299  QWidget *parent )
300  : KDialog( parent ), d(new Private(this))
301 {
302  setCaption( caption );
303  d->init();
304 
305  d->m_chooser = new KMimeTypeChooser( text, selMimeTypes,
306  defaultGroup, groupsToShow, visuals,
307  this );
308  setMainWidget(d->m_chooser);
309 }
310 
311 KMimeTypeChooserDialog::KMimeTypeChooserDialog(
312  const QString &caption,
313  const QString& text,
314  const QStringList &selMimeTypes,
315  const QString &defaultGroup,
316  QWidget *parent )
317  : KDialog( parent ), d(new Private(this))
318 {
319  setCaption( caption );
320  d->init();
321 
322  d->m_chooser = new KMimeTypeChooser( text, selMimeTypes,
323  defaultGroup, QStringList(),
324  KMimeTypeChooser::Comments|KMimeTypeChooser::Patterns|KMimeTypeChooser::EditButton,
325  this );
326  setMainWidget(d->m_chooser);
327 }
328 
329 KMimeTypeChooser* KMimeTypeChooserDialog::chooser()
330 {
331  return d->m_chooser;
332 }
333 
334 void KMimeTypeChooserDialog::Private::init()
335 {
336  q->setButtons( Cancel | Ok );
337  q->setModal( true );
338  q->setDefaultButton( Ok );
339 
340  KConfigGroup group( KGlobal::config(), "KMimeTypeChooserDialog");
341  q->resize( group.readEntry("size", QSize(500,400)));
342 }
343 
344 KMimeTypeChooserDialog::~KMimeTypeChooserDialog()
345 {
346  KConfigGroup group( KGlobal::config(), "KMimeTypeChooserDialog");
347  group.writeEntry("size", size());
348 
349  delete d;
350 }
351 
352 //END KMimeTypeChooserDialog
353 
354 // kate: space-indent on; indent-width 2; replace-tabs on;
355 #include "kmimetypechooser.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Fri Nov 16 2012 15:10:05 by doxygen 1.8.1 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-4.8.5 API Reference

Skip menu "kdelibs-4.8.5 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
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