00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
00037 class KMimeTypeChooserPrivate
00038 {
00039 public:
00040 KListView *lvMimeTypes;
00041 QPushButton *btnEditMimeType;
00042
00043 QString defaultgroup;
00044 QStringList groups;
00045 int visuals;
00046 };
00047
00048
00049
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
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
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;
00132 QListViewItem *firstChecked = 0;
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
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 );
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);
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
00247
00248
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 QSize defaultSize( 400, 300 );
00266 resize( group.readSizeEntry("size", &defaultSize) );
00267 }
00268
00269 KMimeTypeChooserDialog::KMimeTypeChooserDialog(
00270 const QString &caption,
00271 const QString& text,
00272 const QStringList &selMimeTypes,
00273 const QString &defaultGroup,
00274 QWidget *parent, const char *name )
00275 : KDialogBase(parent, name, true, caption, Cancel|Ok, Ok)
00276 {
00277 m_chooser = new KMimeTypeChooser( text, selMimeTypes,
00278 defaultGroup, QStringList(),
00279 KMimeTypeChooser::Comments|KMimeTypeChooser::Patterns|KMimeTypeChooser::EditButton,
00280 this, "chooser" );
00281 setMainWidget(m_chooser);
00282
00283 KConfigGroup group( KGlobal::config(), "KMimeTypeChooserDialog");
00284 QSize defaultSize( 400, 300 );
00285 resize( group.readSizeEntry("size", &defaultSize) );
00286 }
00287
00288
00289 KMimeTypeChooserDialog::~KMimeTypeChooserDialog()
00290 {
00291 KConfigGroup group( KGlobal::config(), "KMimeTypeChooserDialog");
00292 group.writeEntry("size", size());
00293 }
00294
00295
00296
00297
00298 #include "kmimetypechooser.moc"