KIO
kmimetypechooser.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2001 - 2004 Anders Lund <anders@alweb.dk> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00016 Boston, MA 02110-1301, USA. 00017 */ 00018 00019 #include "kmimetypechooser.h" 00020 00021 #include <klocale.h> 00022 #include <kmimetype.h> 00023 #include <kshell.h> 00024 #include <krun.h> 00025 #include <ksycoca.h> 00026 00027 #include <QLabel> 00028 #include <QLayout> 00029 #include <QPushButton> 00030 #include <QTreeWidget> 00031 #include <kconfiggroup.h> 00032 00033 //BEGIN KMimeTypeChooserPrivate 00034 class KMimeTypeChooserPrivate 00035 { 00036 public: 00037 KMimeTypeChooserPrivate( KMimeTypeChooser *parent ) 00038 : q(parent), 00039 mimeTypeTree(0), 00040 btnEditMimeType(0) 00041 { 00042 } 00043 00044 void loadMimeTypes( const QStringList &selected = QStringList() ); 00045 00046 void _k_editMimeType(); 00047 void _k_slotCurrentChanged(QTreeWidgetItem*); 00048 void _k_slotSycocaDatabaseChanged(const QStringList&); 00049 00050 KMimeTypeChooser *q; 00051 QTreeWidget *mimeTypeTree; 00052 QPushButton *btnEditMimeType; 00053 00054 QString defaultgroup; 00055 QStringList groups; 00056 int visuals; 00057 }; 00058 //END 00059 00060 //BEGIN KMimeTypeChooser 00061 KMimeTypeChooser::KMimeTypeChooser( const QString &text, 00062 const QStringList &selMimeTypes, 00063 const QString &defaultGroup, 00064 const QStringList &groupsToShow, 00065 int visuals, 00066 QWidget *parent ) 00067 : KVBox( parent ), 00068 d(new KMimeTypeChooserPrivate(this)) 00069 { 00070 d->defaultgroup = defaultGroup; 00071 d->groups = groupsToShow; 00072 d->visuals = visuals; 00073 setSpacing(-1); 00074 00075 if ( !text.isEmpty() ) 00076 { 00077 new QLabel( text, this ); 00078 } 00079 00080 d->mimeTypeTree = new QTreeWidget( this ); 00081 QStringList headerLabels; 00082 headerLabels.append( i18n("Mime Type") ); 00083 00084 if ( visuals & Comments ) { 00085 headerLabels.append( i18n("Comment") ); 00086 } 00087 if ( visuals & Patterns ) { 00088 headerLabels.append( i18n("Patterns") ); 00089 } 00090 00091 d->mimeTypeTree->setColumnCount(headerLabels.count()); 00092 d->mimeTypeTree->setHeaderLabels(headerLabels); 00093 QFontMetrics fm(d->mimeTypeTree->fontMetrics()); 00094 d->mimeTypeTree->setColumnWidth(0, 20 * fm.height()); // big enough for most names, but not for the insanely long ones 00095 00096 d->loadMimeTypes( selMimeTypes ); 00097 00098 if (visuals & EditButton) 00099 { 00100 KHBox *btns = new KHBox( this ); 00101 ((QBoxLayout*)btns->layout())->addStretch(1); 00102 d->btnEditMimeType = new QPushButton( i18n("&Edit..."), btns ); 00103 00104 connect( d->btnEditMimeType, SIGNAL(clicked()), this, SLOT(_k_editMimeType()) ); 00105 d->btnEditMimeType->setEnabled( false ); 00106 connect( d->mimeTypeTree, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), 00107 this, SLOT(_k_editMimeType())); 00108 connect( d->mimeTypeTree, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)), 00109 this, SLOT(_k_slotCurrentChanged(QTreeWidgetItem*)) ); 00110 00111 d->btnEditMimeType->setWhatsThis(i18n( 00112 "Click this button to display the familiar KDE mime type editor.") ); 00113 } 00114 } 00115 00116 KMimeTypeChooser::~KMimeTypeChooser() 00117 { 00118 delete d; 00119 } 00120 00121 void KMimeTypeChooserPrivate::loadMimeTypes( const QStringList &_selectedMimeTypes ) 00122 { 00123 QStringList selMimeTypes; 00124 00125 if ( !_selectedMimeTypes.isEmpty() ) 00126 selMimeTypes = _selectedMimeTypes; 00127 else 00128 selMimeTypes = q->mimeTypes(); 00129 00130 mimeTypeTree->clear(); 00131 00132 QMap<QString, QTreeWidgetItem*> groupItems; 00133 const KMimeType::List mimetypes = KMimeType::allMimeTypes(); 00134 00135 bool agroupisopen = false; 00136 QTreeWidgetItem *idefault = 0; //open this, if all other fails 00137 QTreeWidgetItem *firstChecked = 0; // make this one visible after the loop 00138 00139 foreach (const KMimeType::Ptr& mt, mimetypes) 00140 { 00141 const QString mimetype = mt->name(); 00142 const int index = mimetype.indexOf('/'); 00143 const QString maj = mimetype.left(index); 00144 00145 if ( !groups.isEmpty() && !groups.contains( maj ) ) 00146 continue; 00147 00148 QTreeWidgetItem *groupItem; 00149 QMap<QString,QTreeWidgetItem*>::Iterator mit = groupItems.find( maj ); 00150 if ( mit == groupItems.end() ) 00151 { 00152 groupItem = new QTreeWidgetItem( mimeTypeTree, QStringList(maj) ); 00153 groupItems.insert( maj, groupItem ); 00154 if ( maj == defaultgroup ) 00155 idefault = groupItem; 00156 } 00157 else 00158 groupItem = mit.value(); 00159 00160 const QString min = mimetype.mid(index+1); 00161 QTreeWidgetItem *item = new QTreeWidgetItem( groupItem, QStringList(min) ); 00162 item->setIcon( 0, KIcon( mt->iconName() ) ); 00163 00164 int cl = 1; 00165 00166 if ( visuals & KMimeTypeChooser::Comments ) 00167 { 00168 item->setText( cl, mt->comment() ); 00169 cl++; 00170 } 00171 00172 if ( visuals & KMimeTypeChooser::Patterns ) 00173 item->setText( cl, mt->patterns().join("; ") ); 00174 00175 if ( selMimeTypes.contains(mimetype) ) { 00176 item->setCheckState( 0, Qt::Checked ); 00177 groupItem->setExpanded( true ); 00178 agroupisopen = true; 00179 if ( !firstChecked ) 00180 firstChecked = item; 00181 } else { 00182 item->setCheckState( 0, Qt::Unchecked ); 00183 } 00184 } 00185 00186 if ( firstChecked ) 00187 mimeTypeTree->scrollToItem( firstChecked ); 00188 00189 if ( !agroupisopen && idefault ) 00190 { 00191 idefault->setExpanded( true ); 00192 mimeTypeTree->scrollToItem( idefault ); 00193 } 00194 mimeTypeTree->resizeColumnToContents(1); 00195 } 00196 00197 void KMimeTypeChooserPrivate::_k_editMimeType() 00198 { 00199 QTreeWidgetItem* item = mimeTypeTree->currentItem(); 00200 if ( !item || !item->parent() ) 00201 return; 00202 QString mt = (item->parent())->text(0) + '/' + item->text(0); 00203 // thanks to libkonq/konq_operations.cc 00204 q->connect( KSycoca::self(), SIGNAL(databaseChanged(QStringList)), 00205 q, SLOT(_k_slotSycocaDatabaseChanged(QStringList)) ); 00206 QString keditfiletype = QString::fromLatin1("keditfiletype"); 00207 KRun::runCommand( keditfiletype 00208 #ifndef Q_OS_WIN 00209 + " --parent " + QString::number( (ulong)q->topLevelWidget()->winId()) 00210 #endif 00211 + ' ' + KShell::quoteArg(mt), 00212 keditfiletype, keditfiletype /*unused*/, q->topLevelWidget()); 00213 } 00214 00215 void KMimeTypeChooserPrivate::_k_slotCurrentChanged(QTreeWidgetItem* item) 00216 { 00217 if ( btnEditMimeType ) 00218 btnEditMimeType->setEnabled( item->parent() ); 00219 } 00220 00221 void KMimeTypeChooserPrivate::_k_slotSycocaDatabaseChanged(const QStringList& changedResources) 00222 { 00223 if (changedResources.contains("xdgdata-mime")) 00224 loadMimeTypes(); 00225 } 00226 00227 // recursive helper for mimeTypes() 00228 static void getCheckedItems(QList<QTreeWidgetItem *> &lst, QTreeWidgetItem* parent) 00229 { 00230 for (int i = 0; i < parent->childCount(); ++i ) { 00231 QTreeWidgetItem* item = parent->child(i); 00232 if (item->checkState(0) == Qt::Checked) 00233 lst.append(item); 00234 getCheckedItems(lst, item); 00235 } 00236 } 00237 00238 static void getCheckedItems(QList<QTreeWidgetItem *>& lst, QTreeWidget* tree) 00239 { 00240 for (int i = 0; i < tree->topLevelItemCount(); ++i ) { 00241 QTreeWidgetItem* topItem = tree->topLevelItem(i); 00242 //if (topItem->checkState(0) == Qt::Checked) 00243 // lst.append(topItem); 00244 getCheckedItems(lst, topItem); 00245 } 00246 } 00247 00248 QStringList KMimeTypeChooser::mimeTypes() const 00249 { 00250 QStringList mimeList; 00251 QList<QTreeWidgetItem *> checkedItems; 00252 getCheckedItems(checkedItems, d->mimeTypeTree); 00253 foreach(QTreeWidgetItem* item, checkedItems) { 00254 mimeList.append( item->parent()->text(0) + '/' + item->text(0) ); 00255 } 00256 return mimeList; 00257 } 00258 00259 QStringList KMimeTypeChooser::patterns() const 00260 { 00261 QStringList patternList; 00262 QList<QTreeWidgetItem *> checkedItems; 00263 getCheckedItems(checkedItems, d->mimeTypeTree); 00264 foreach(QTreeWidgetItem* item, checkedItems) { 00265 KMimeType::Ptr p = KMimeType::mimeType( item->parent()->text(0) + '/' + item->text(0) ); 00266 Q_ASSERT(p); 00267 patternList += p->patterns(); 00268 } 00269 return patternList; 00270 } 00271 //END 00272 00273 //BEGIN KMimeTypeChooserDialog::Private 00274 00275 class KMimeTypeChooserDialog::Private 00276 { 00277 public: 00278 Private( KMimeTypeChooserDialog *parent ) 00279 : q(parent) 00280 { 00281 } 00282 00283 void init(); 00284 00285 KMimeTypeChooserDialog *q; 00286 KMimeTypeChooser *m_chooser; 00287 }; 00288 00289 //END 00290 00291 //BEGIN KMimeTypeChooserDialog 00292 KMimeTypeChooserDialog::KMimeTypeChooserDialog( 00293 const QString &caption, 00294 const QString& text, 00295 const QStringList &selMimeTypes, 00296 const QString &defaultGroup, 00297 const QStringList &groupsToShow, 00298 int visuals, 00299 QWidget *parent ) 00300 : KDialog( parent ), d(new Private(this)) 00301 { 00302 setCaption( caption ); 00303 d->init(); 00304 00305 d->m_chooser = new KMimeTypeChooser( text, selMimeTypes, 00306 defaultGroup, groupsToShow, visuals, 00307 this ); 00308 setMainWidget(d->m_chooser); 00309 } 00310 00311 KMimeTypeChooserDialog::KMimeTypeChooserDialog( 00312 const QString &caption, 00313 const QString& text, 00314 const QStringList &selMimeTypes, 00315 const QString &defaultGroup, 00316 QWidget *parent ) 00317 : KDialog( parent ), d(new Private(this)) 00318 { 00319 setCaption( caption ); 00320 d->init(); 00321 00322 d->m_chooser = new KMimeTypeChooser( text, selMimeTypes, 00323 defaultGroup, QStringList(), 00324 KMimeTypeChooser::Comments|KMimeTypeChooser::Patterns|KMimeTypeChooser::EditButton, 00325 this ); 00326 setMainWidget(d->m_chooser); 00327 } 00328 00329 KMimeTypeChooser* KMimeTypeChooserDialog::chooser() 00330 { 00331 return d->m_chooser; 00332 } 00333 00334 void KMimeTypeChooserDialog::Private::init() 00335 { 00336 q->setButtons( Cancel | Ok ); 00337 q->setModal( true ); 00338 q->setDefaultButton( Ok ); 00339 00340 KConfigGroup group( KGlobal::config(), "KMimeTypeChooserDialog"); 00341 q->resize( group.readEntry("size", QSize(600,500))); 00342 } 00343 00344 KMimeTypeChooserDialog::~KMimeTypeChooserDialog() 00345 { 00346 KConfigGroup group( KGlobal::config(), "KMimeTypeChooserDialog"); 00347 group.writeEntry("size", size()); 00348 00349 delete d; 00350 } 00351 00352 //END KMimeTypeChooserDialog 00353 00354 // kate: space-indent on; indent-width 2; replace-tabs on; 00355 #include "kmimetypechooser.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2019 The KDE developers.
Generated on Mon Jan 21 2019 12:35:00 by doxygen 1.7.5.1 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2019 The KDE developers.
Generated on Mon Jan 21 2019 12:35:00 by doxygen 1.7.5.1 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.