# kio Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# 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 KMimeTypeChooser::~KMimeTypeChooser()
    00109 {
    00110   delete d;
    00111 }
    00112
    00113 void KMimeTypeChooser::loadMimeTypes( const QStringList &_selectedMimeTypes )
    00114 {
    00115   QStringList selMimeTypes;
    00116
    00117   if ( !_selectedMimeTypes.isEmpty() )
    00118     selMimeTypes = _selectedMimeTypes;
    00119   else
    00120     selMimeTypes = mimeTypes();
    00121
    00122   d->lvMimeTypes->clear();
    00123
    00124   QMap<QString,QListViewItem*> groups;
    00125   // thanks to kdebase/kcontrol/filetypes/filetypesview
    00126   KMimeType::List mimetypes = KMimeType::allMimeTypes();
    00127   QValueListIterator<KMimeType::Ptr> it(mimetypes.begin());
    00128
    00129   QListViewItem *groupItem;
    00130   bool agroupisopen = false;
    00131   QListViewItem *idefault = 0; //open this, if all other fails
    00132   QListViewItem *firstChecked = 0; // make this one visible after the loop
    00133
    00134   for (; it != mimetypes.end(); ++it)
    00135   {
    00136     QString mimetype = (*it)->name();
    00137     int index = mimetype.find("/");
    00138     QString maj = mimetype.left(index);
    00139
    00140     if ( d->groups.count() && !d->groups.contains( maj ) )
    00141       continue;
    00142
    00143     QString min = mimetype.right(mimetype.length() - (index+1));
    00144
    00145     QMapIterator<QString,QListViewItem*> mit = groups.find( maj );
    00146     if ( mit == groups.end() )
    00147     {
    00148       groupItem = new QListViewItem( d->lvMimeTypes, maj );
    00149       groups.insert( maj, groupItem );
    00150        if ( maj == d->defaultgroup )
    00151          idefault = groupItem;
    00152     }
    00153     else
    00154         groupItem = mit.data();
    00155
    00156     QCheckListItem *item = new QCheckListItem( groupItem, min, QCheckListItem::CheckBox );
    00157     item->setPixmap( 0, SmallIcon( (*it)->icon(QString::null,false) ) );
    00158
    00159     int cl = 1;
    00160
    00161     if ( d->visuals & Comments )
    00162     {
    00163       item->setText( cl, (*it)->comment(QString::null, false) );
    00164       cl++;
    00165     }
    00166
    00167     if ( d->visuals & Patterns )
    00168       item->setText( cl, (*it)->patterns().join("; ") );
    00169
    00170     if ( selMimeTypes.contains(mimetype) )
    00171     {
    00172       item->setOn( true );
    00173       groupItem->setOpen( true );
    00174       agroupisopen = true;
    00175       if ( !firstChecked )
    00176         firstChecked = item;
    00177     }
    00178   }
    00179
    00180   if ( firstChecked )
    00181     d->lvMimeTypes->ensureItemVisible( firstChecked );
    00182
    00183   if ( !agroupisopen && idefault )
    00184   {
    00185     idefault->setOpen( true );
    00186     d->lvMimeTypes->ensureItemVisible( idefault );
    00187   }
    00188 }
    00189
    00190 void KMimeTypeChooser::editMimeType()
    00191 {
    00192   if ( !(d->lvMimeTypes->currentItem() && (d->lvMimeTypes->currentItem())->parent()) )
    00193     return;
    00194   QString mt = (d->lvMimeTypes->currentItem()->parent())->text( 0 ) + "/" + (d->lvMimeTypes->currentItem())->text( 0 );
    00195   // thanks to libkonq/konq_operations.cc
    00196   connect( KSycoca::self(), SIGNAL(databaseChanged()),
    00197            this, SLOT(slotSycocaDatabaseChanged()) );
    00198   QString keditfiletype = QString::fromLatin1("keditfiletype");
    00199   KRun::runCommand( keditfiletype
    00200                     + " --parent " + QString::number( (ulong)topLevelWidget()->winId())
    00201                     + " " + KProcess::quote(mt),
    00202                     keditfiletype, keditfiletype /*unused*/);
    00203 }
    00204
    00205 void KMimeTypeChooser::slotCurrentChanged(QListViewItem* i)
    00206 {
    00207   if ( d->btnEditMimeType )
    00208     d->btnEditMimeType->setEnabled( i->parent() );
    00209 }
    00210
    00211 void KMimeTypeChooser::slotSycocaDatabaseChanged()
    00212 {
    00213   if ( KSycoca::self()->isChanged("mime") )
    00214     loadMimeTypes();
    00215 }
    00216
    00217 QStringList KMimeTypeChooser::mimeTypes() const
    00218 {
    00219   QStringList l;
    00220   QListViewItemIterator it( d->lvMimeTypes );
    00221   for (; it.current(); ++it)
    00222   {
    00223     if ( it.current()->parent() && ((QCheckListItem*)it.current())->isOn() )
    00224       l << it.current()->parent()->text(0) + "/" + it.current()->text(0); // FIXME uncecked, should be Ok unless someone changes mimetypes during this!
    00225   }
    00226   return l;
    00227 }
    00228
    00229 QStringList KMimeTypeChooser::patterns() const
    00230 {
    00231   QStringList l;
    00232   KMimeType::Ptr p;
    00233   QString defMT = KMimeType::defaultMimeType();
    00234   QListViewItemIterator it( d->lvMimeTypes );
    00235   for (; it.current(); ++it)
    00236   {
    00237     if ( it.current()->parent() && ((QCheckListItem*)it.current())->isOn() )
    00238     {
    00239       p = KMimeType::mimeType( it.current()->parent()->text(0) + "/" + it.current()->text(0) );
    00240       if ( p->name() != defMT )
    00241         l += p->patterns();
    00242     }
    00243   }
    00244   return l;
    00245 }
    00246 //END
    00247
    00248 //BEGIN KMimeTypeChooserDialog
    00249 KMimeTypeChooserDialog::KMimeTypeChooserDialog(
    00250                          const QString &caption,
    00251                          const QString& text,
    00252                          const QStringList &selMimeTypes,
    00253                          const QString &defaultGroup,
    00254                          const QStringList &groupsToShow,
    00255                          int visuals,
    00256                          QWidget *parent, const char *name )
    00257     : KDialogBase(parent, name, true, caption, Cancel|Ok, Ok)
    00258 {
    00259   m_chooser = new KMimeTypeChooser( text, selMimeTypes,
    00260                                   defaultGroup, groupsToShow, visuals,
    00261                                   this, "chooser" );
    00262   setMainWidget(m_chooser);
    00263
    00264   KConfigGroup group( KGlobal::config(), "KMimeTypeChooserDialog");
    00265   resize( group.readSizeEntry("size", new QSize(400,300)) );
    00266 }
    00267
    00268 KMimeTypeChooserDialog::KMimeTypeChooserDialog(
    00269                          const QString &caption,
    00270                          const QString& text,
    00271                          const QStringList &selMimeTypes,
    00272                          const QString &defaultGroup,
    00273                          QWidget *parent, const char *name )
    00274     : KDialogBase(parent, name, true, caption, Cancel|Ok, Ok)
    00275 {
    00276   m_chooser = new KMimeTypeChooser( text, selMimeTypes,
    00277                                   defaultGroup, QStringList(),
    00278                                   KMimeTypeChooser::Comments|KMimeTypeChooser::Patterns|KMimeTypeChooser::EditButton,
    00279                                   this, "chooser" );
    00280   setMainWidget(m_chooser);
    00281
    00282   KConfigGroup group( KGlobal::config(), "KMimeTypeChooserDialog");
    00283   resize( group.readSizeEntry("size", new QSize(400,300)) );
    00284 }
    00285
    00286
    00287 KMimeTypeChooserDialog::~KMimeTypeChooserDialog()
    00288 {
    00289   KConfigGroup group( KGlobal::config(), "KMimeTypeChooserDialog");
    00290   group.writeEntry("size", size());
    00291 }
    00292
    00293 //END KMimeTypeChooserDialog
    00294
    00295 // kate: space-indent on; indent-width 2; replace-tabs on;
    00296 #include "kmimetypechooser.moc"

This file is part of the documentation for kio Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:24:00 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003