kio Library API Documentation

kmimetypechooser.cpp

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., 59 Temple Place - Suite 330, 00016 Boston, MA 02111-1307, USA. 00017 */ 00018 00019 #include "kmimetypechooser.h" 00020 00021 #include <kconfig.h> 00022 #include <kiconloader.h> 00023 #include <klistview.h> 00024 #include <klocale.h> 00025 #include <kmimetype.h> 00026 #include <kprocess.h> 00027 #include <krun.h> 00028 #include <ksycoca.h> 00029 00030 #include <qlabel.h> 00031 #include <qlayout.h> 00032 #include <qlineedit.h> 00033 #include <qpushbutton.h> 00034 #include <qwhatsthis.h> 00035 00036 //BEGIN KMimeTypeChooserPrivate 00037 class KMimeTypeChooserPrivate 00038 { 00039 public: 00040 KListView *lvMimeTypes; 00041 QPushButton *btnEditMimeType; 00042 00043 QString defaultgroup; 00044 QStringList groups; 00045 int visuals; 00046 }; 00047 //END 00048 00049 //BEGIN KMimeTypeChooser 00050 KMimeTypeChooser::KMimeTypeChooser( const QString &text, 00051 const QStringList &selMimeTypes, 00052 const QString &defaultGroup, 00053 const QStringList &groupsToShow, 00054 int visuals, 00055 QWidget *parent, 00056 const char *name ) 00057 : QVBox( parent, name ) 00058 { 00059 d = new KMimeTypeChooserPrivate(); 00060 d->lvMimeTypes = 0; 00061 d->btnEditMimeType = 0; 00062 d->defaultgroup = defaultGroup; 00063 d->groups = groupsToShow; 00064 d->visuals = visuals; 00065 00066 setSpacing( KDialogBase::spacingHint() ); 00067 00068 if ( !text.isEmpty() ) 00069 { 00070 new QLabel( text, this ); 00071 } 00072 00073 d->lvMimeTypes = new KListView( this ); 00074 00075 d->lvMimeTypes->addColumn( i18n("Mime Type") ); 00076 // d->lvMimeTypes->setColumnWidthMode( 0, QListView::Manual ); 00077 00078 if ( visuals & Comments ) 00079 { 00080 d->lvMimeTypes->addColumn( i18n("Comment") ); 00081 d->lvMimeTypes->setColumnWidthMode( 1, QListView::Manual ); 00082 } 00083 if ( visuals & Patterns ) 00084 d->lvMimeTypes->addColumn( i18n("Patterns") ); 00085 00086 d->lvMimeTypes->setRootIsDecorated( true ); 00087 00088 loadMimeTypes( selMimeTypes ); 00089 00090 if (visuals & KMimeTypeChooser::EditButton) 00091 { 00092 QHBox *btns = new QHBox( this ); 00093 ((QBoxLayout*)btns->layout())->addStretch(1); 00094 d->btnEditMimeType = new QPushButton( i18n("&Edit..."), btns ); 00095 00096 connect( d->btnEditMimeType, SIGNAL(clicked()), this, SLOT(editMimeType()) ); 00097 d->btnEditMimeType->setEnabled( false ); 00098 connect( d->lvMimeTypes, SIGNAL( doubleClicked ( QListViewItem * )), 00099 this, SLOT( editMimeType())); 00100 connect( d->lvMimeTypes, SIGNAL(currentChanged(QListViewItem*)), 00101 this, SLOT(slotCurrentChanged(QListViewItem*)) ); 00102 00103 QWhatsThis::add( d->btnEditMimeType, i18n( 00104 "Click this button to display the familiar KDE mime type editor.") ); 00105 } 00106 } 00107 00108 void KMimeTypeChooser::loadMimeTypes( const QStringList &_selectedMimeTypes ) 00109 { 00110 QStringList selMimeTypes; 00111 00112 if ( !_selectedMimeTypes.isEmpty() ) 00113 selMimeTypes = _selectedMimeTypes; 00114 else 00115 selMimeTypes = mimeTypes(); 00116 00117 d->lvMimeTypes->clear(); 00118 00119 QMap<QString,QListViewItem*> groups; 00120 // thanks to kdebase/kcontrol/filetypes/filetypesview 00121 KMimeType::List mimetypes = KMimeType::allMimeTypes(); 00122 QValueListIterator<KMimeType::Ptr> it(mimetypes.begin()); 00123 00124 QListViewItem *groupItem; 00125 bool agroupisopen = false; 00126 QListViewItem *idefault = 0; //open this, if all other fails 00127 QListViewItem *firstChecked = 0; // make this one visible after the loop 00128 00129 for (; it != mimetypes.end(); ++it) 00130 { 00131 QString mimetype = (*it)->name(); 00132 int index = mimetype.find("/"); 00133 QString maj = mimetype.left(index); 00134 00135 if ( d->groups.count() && !d->groups.contains( maj ) ) 00136 continue; 00137 00138 QString min = mimetype.right(mimetype.length() - (index+1)); 00139 00140 QMapIterator<QString,QListViewItem*> mit = groups.find( maj ); 00141 if ( mit == groups.end() ) 00142 { 00143 groupItem = new QListViewItem( d->lvMimeTypes, maj ); 00144 groups.insert( maj, groupItem ); 00145 if ( maj == d->defaultgroup ) 00146 idefault = groupItem; 00147 } 00148 else 00149 groupItem = mit.data(); 00150 00151 QCheckListItem *item = new QCheckListItem( groupItem, min, QCheckListItem::CheckBox ); 00152 item->setPixmap( 0, SmallIcon( (*it)->icon(QString::null,false) ) ); 00153 00154 int cl = 1; 00155 00156 if ( d->visuals & Comments ) 00157 { 00158 item->setText( cl, (*it)->comment(QString::null, false) ); 00159 cl++; 00160 } 00161 00162 if ( d->visuals & Patterns ) 00163 item->setText( cl, (*it)->patterns().join("; ") ); 00164 00165 if ( selMimeTypes.contains(mimetype) ) 00166 { 00167 item->setOn( true ); 00168 groupItem->setOpen( true ); 00169 agroupisopen = true; 00170 if ( !firstChecked ) 00171 firstChecked = item; 00172 } 00173 } 00174 00175 if ( firstChecked ) 00176 d->lvMimeTypes->ensureItemVisible( firstChecked ); 00177 00178 if ( !agroupisopen && idefault ) 00179 { 00180 idefault->setOpen( true ); 00181 d->lvMimeTypes->ensureItemVisible( idefault ); 00182 } 00183 } 00184 00185 void KMimeTypeChooser::editMimeType() 00186 { 00187 if ( !(d->lvMimeTypes->currentItem() && (d->lvMimeTypes->currentItem())->parent()) ) 00188 return; 00189 QString mt = (d->lvMimeTypes->currentItem()->parent())->text( 0 ) + "/" + (d->lvMimeTypes->currentItem())->text( 0 ); 00190 // thanks to libkonq/konq_operations.cc 00191 connect( KSycoca::self(), SIGNAL(databaseChanged()), 00192 this, SLOT(slotSycocaDatabaseChanged()) ); 00193 QString keditfiletype = QString::fromLatin1("keditfiletype"); 00194 KRun::runCommand( keditfiletype 00195 + " --parent " + QString::number( topLevelWidget()->winId()) 00196 + " " + KProcess::quote(mt), 00197 keditfiletype, keditfiletype /*unused*/); 00198 } 00199 00200 void KMimeTypeChooser::slotCurrentChanged(QListViewItem* i) 00201 { 00202 if ( d->btnEditMimeType ) 00203 d->btnEditMimeType->setEnabled( i->parent() ); 00204 } 00205 00206 void KMimeTypeChooser::slotSycocaDatabaseChanged() 00207 { 00208 if ( KSycoca::self()->isChanged("mime") ) 00209 loadMimeTypes(); 00210 } 00211 00212 QStringList KMimeTypeChooser::mimeTypes() const 00213 { 00214 QStringList l; 00215 QListViewItemIterator it( d->lvMimeTypes ); 00216 for (; it.current(); ++it) 00217 { 00218 if ( it.current()->parent() && ((QCheckListItem*)it.current())->isOn() ) 00219 l << it.current()->parent()->text(0) + "/" + it.current()->text(0); // FIXME uncecked, should be Ok unless someone changes mimetypes during this! 00220 } 00221 return l; 00222 } 00223 00224 QStringList KMimeTypeChooser::patterns() const 00225 { 00226 QStringList l; 00227 KMimeType::Ptr p; 00228 QString defMT = KMimeType::defaultMimeType(); 00229 QListViewItemIterator it( d->lvMimeTypes ); 00230 for (; it.current(); ++it) 00231 { 00232 if ( it.current()->parent() && ((QCheckListItem*)it.current())->isOn() ) 00233 { 00234 p = KMimeType::mimeType( it.current()->parent()->text(0) + "/" + it.current()->text(0) ); 00235 if ( p->name() != defMT ) 00236 l += p->patterns(); 00237 } 00238 } 00239 return l; 00240 } 00241 //END 00242 00243 //BEGIN KMimeTypeChooserDialog 00244 KMimeTypeChooserDialog::KMimeTypeChooserDialog( 00245 const QString &caption, 00246 const QString& text, 00247 const QStringList &selMimeTypes, 00248 const QString &defaultGroup, 00249 const QStringList &groupsToShow, 00250 int visuals, 00251 QWidget *parent, const char *name ) 00252 : KDialogBase(parent, name, true, caption, Cancel|Ok, Ok) 00253 { 00254 m_chooser = new KMimeTypeChooser( text, selMimeTypes, 00255 defaultGroup, groupsToShow, visuals, 00256 this, "chooser" ); 00257 setMainWidget(m_chooser); 00258 00259 KConfigGroup group( KGlobal::config(), "KMimeTypeChooserDialog"); 00260 resize( group.readSizeEntry("size", new QSize(400,300)) ); 00261 } 00262 00263 KMimeTypeChooserDialog::KMimeTypeChooserDialog( 00264 const QString &caption, 00265 const QString& text, 00266 const QStringList &selMimeTypes, 00267 const QString &defaultGroup, 00268 QWidget *parent, const char *name ) 00269 : KDialogBase(parent, name, true, caption, Cancel|Ok, Ok) 00270 { 00271 m_chooser = new KMimeTypeChooser( text, selMimeTypes, 00272 defaultGroup, QStringList(), 00273 KMimeTypeChooser::Comments|KMimeTypeChooser::Patterns|KMimeTypeChooser::EditButton, 00274 this, "chooser" ); 00275 setMainWidget(m_chooser); 00276 00277 KConfigGroup group( KGlobal::config(), "KMimeTypeChooserDialog"); 00278 resize( group.readSizeEntry("size", new QSize(400,300)) ); 00279 } 00280 00281 00282 KMimeTypeChooserDialog::~KMimeTypeChooserDialog() 00283 { 00284 KConfigGroup group( KGlobal::config(), "KMimeTypeChooserDialog"); 00285 group.writeEntry("size", size()); 00286 } 00287 00288 //END KMimeTypeChooserDialog 00289 00290 // kate: space-indent on; indent-width 2; replace-tabs on; 00291 #include "kmimetypechooser.moc"
KDE Logo
This file is part of the documentation for kio Library Version 3.3.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Oct 17 11:29:27 2004 by doxygen 1.3.8 written by Dimitri van Heesch, © 1997-2003