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 // d->mimeTypeTree->setColumnWidthMode( 0, QListView::Manual ); 00084 00085 if ( visuals & Comments ) { 00086 headerLabels.append( i18n("Comment") ); 00087 //d->mimeTypeTree->setColumnWidthMode( 1, Q3ListView::Manual ); 00088 } 00089 if ( visuals & Patterns ) { 00090 headerLabels.append( i18n("Patterns") ); 00091 } 00092 00093 // d->mimeTypeTree->setRootIsDecorated( true ); 00094 d->mimeTypeTree->setColumnCount(headerLabels.count()); 00095 d->mimeTypeTree->setHeaderLabels(headerLabels); 00096 00097 d->loadMimeTypes( selMimeTypes ); 00098 00099 if (visuals & EditButton) 00100 { 00101 KHBox *btns = new KHBox( this ); 00102 ((QBoxLayout*)btns->layout())->addStretch(1); 00103 d->btnEditMimeType = new QPushButton( i18n("&Edit..."), btns ); 00104 00105 connect( d->btnEditMimeType, SIGNAL(clicked()), this, SLOT(_k_editMimeType()) ); 00106 d->btnEditMimeType->setEnabled( false ); 00107 connect( d->mimeTypeTree, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), 00108 this, SLOT(_k_editMimeType())); 00109 connect( d->mimeTypeTree, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)), 00110 this, SLOT(_k_slotCurrentChanged(QTreeWidgetItem*)) ); 00111 00112 d->btnEditMimeType->setWhatsThis(i18n( 00113 "Click this button to display the familiar KDE mime type editor.") ); 00114 } 00115 } 00116 00117 KMimeTypeChooser::~KMimeTypeChooser() 00118 { 00119 delete d; 00120 } 00121 00122 void KMimeTypeChooserPrivate::loadMimeTypes( const QStringList &_selectedMimeTypes ) 00123 { 00124 QStringList selMimeTypes; 00125 00126 if ( !_selectedMimeTypes.isEmpty() ) 00127 selMimeTypes = _selectedMimeTypes; 00128 else 00129 selMimeTypes = q->mimeTypes(); 00130 00131 mimeTypeTree->clear(); 00132 00133 QMap<QString, QTreeWidgetItem*> groupItems; 00134 const KMimeType::List mimetypes = KMimeType::allMimeTypes(); 00135 00136 bool agroupisopen = false; 00137 QTreeWidgetItem *idefault = 0; //open this, if all other fails 00138 QTreeWidgetItem *firstChecked = 0; // make this one visible after the loop 00139 00140 foreach (const KMimeType::Ptr& mt, mimetypes) 00141 { 00142 const QString mimetype = mt->name(); 00143 const int index = mimetype.indexOf('/'); 00144 const QString maj = mimetype.left(index); 00145 00146 if ( !groups.isEmpty() && !groups.contains( maj ) ) 00147 continue; 00148 00149 QTreeWidgetItem *groupItem; 00150 QMap<QString,QTreeWidgetItem*>::Iterator mit = groupItems.find( maj ); 00151 if ( mit == groupItems.end() ) 00152 { 00153 groupItem = new QTreeWidgetItem( mimeTypeTree, QStringList(maj) ); 00154 groupItems.insert( maj, groupItem ); 00155 if ( maj == defaultgroup ) 00156 idefault = groupItem; 00157 } 00158 else 00159 groupItem = mit.value(); 00160 00161 const QString min = mimetype.mid(index+1); 00162 QTreeWidgetItem *item = new QTreeWidgetItem( groupItem, QStringList(min) ); 00163 item->setIcon( 0, KIcon( mt->iconName() ) ); 00164 00165 int cl = 1; 00166 00167 if ( visuals & KMimeTypeChooser::Comments ) 00168 { 00169 item->setText( cl, mt->comment() ); 00170 cl++; 00171 } 00172 00173 if ( visuals & KMimeTypeChooser::Patterns ) 00174 item->setText( cl, mt->patterns().join("; ") ); 00175 00176 if ( selMimeTypes.contains(mimetype) ) { 00177 item->setCheckState( 0, Qt::Checked ); 00178 groupItem->setExpanded( true ); 00179 agroupisopen = true; 00180 if ( !firstChecked ) 00181 firstChecked = item; 00182 } else { 00183 item->setCheckState( 0, Qt::Unchecked ); 00184 } 00185 } 00186 00187 if ( firstChecked ) 00188 mimeTypeTree->scrollToItem( firstChecked ); 00189 00190 if ( !agroupisopen && idefault ) 00191 { 00192 idefault->setExpanded( true ); 00193 mimeTypeTree->scrollToItem( idefault ); 00194 } 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(500,400))); 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"
KDE 4.7 API Reference