1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
/* This file is part of the KDE libraries
Copyright (C) 2001 - 2004 Anders Lund <anders@alweb.dk>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "kmimetypechooser.h"
#include <kconfig.h>
#include <kiconloader.h>
#include <klistview.h>
#include <klocale.h>
#include <kmimetype.h>
#include <kprocess.h>
#include <krun.h>
#include <ksycoca.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqlineedit.h>
#include <tqpushbutton.h>
#include <tqwhatsthis.h>
//BEGIN KMimeTypeChooserPrivate
class KMimeTypeChooserPrivate
{
public:
KListView *lvMimeTypes;
TQPushButton *btnEditMimeType;
TQString defaultgroup;
TQStringList groups;
int visuals;
};
//END
//BEGIN KMimeTypeChooser
KMimeTypeChooser::KMimeTypeChooser( const TQString &text,
const TQStringList &selMimeTypes,
const TQString &defaultGroup,
const TQStringList &groupsToShow,
int visuals,
TQWidget *parent,
const char *name )
: TQVBox( parent, name )
{
d = new KMimeTypeChooserPrivate();
d->lvMimeTypes = 0;
d->btnEditMimeType = 0;
d->defaultgroup = defaultGroup;
d->groups = groupsToShow;
d->visuals = visuals;
setSpacing( KDialogBase::spacingHint() );
if ( !text.isEmpty() )
{
new TQLabel( text, this );
}
d->lvMimeTypes = new KListView( this );
d->lvMimeTypes->addColumn( i18n("Mime Type") );
// d->lvMimeTypes->setColumnWidthMode( 0, TQListView::Manual );
if ( visuals & Comments )
{
d->lvMimeTypes->addColumn( i18n("Comment") );
d->lvMimeTypes->setColumnWidthMode( 1, TQListView::Manual );
}
if ( visuals & Patterns )
d->lvMimeTypes->addColumn( i18n("Patterns") );
d->lvMimeTypes->setRootIsDecorated( true );
loadMimeTypes( selMimeTypes );
if (visuals & KMimeTypeChooser::EditButton)
{
TQHBox *btns = new TQHBox( this );
((TQBoxLayout*)btns->layout())->addStretch(1);
d->btnEditMimeType = new TQPushButton( i18n("&Edit..."), btns );
connect( d->btnEditMimeType, TQT_SIGNAL(clicked()), this, TQT_SLOT(editMimeType()) );
d->btnEditMimeType->setEnabled( false );
connect( d->lvMimeTypes, TQT_SIGNAL( doubleClicked ( TQListViewItem * )),
this, TQT_SLOT( editMimeType()));
connect( d->lvMimeTypes, TQT_SIGNAL(currentChanged(TQListViewItem*)),
this, TQT_SLOT(slotCurrentChanged(TQListViewItem*)) );
TQWhatsThis::add( d->btnEditMimeType, i18n(
"Click this button to display the familiar TDE mime type editor.") );
}
}
KMimeTypeChooser::~KMimeTypeChooser()
{
delete d;
}
void KMimeTypeChooser::loadMimeTypes( const TQStringList &_selectedMimeTypes )
{
TQStringList selMimeTypes;
if ( !_selectedMimeTypes.isEmpty() )
selMimeTypes = _selectedMimeTypes;
else
selMimeTypes = mimeTypes();
d->lvMimeTypes->clear();
TQMap<TQString,TQListViewItem*> groups;
// thanks to tdebase/kcontrol/filetypes/filetypesview
KMimeType::List mimetypes = KMimeType::allMimeTypes();
TQValueListIterator<KMimeType::Ptr> it(mimetypes.begin());
TQListViewItem *groupItem;
bool agroupisopen = false;
TQListViewItem *idefault = 0; //open this, if all other fails
TQListViewItem *firstChecked = 0; // make this one visible after the loop
for (; it != mimetypes.end(); ++it)
{
TQString mimetype = (*it)->name();
int index = mimetype.find("/");
TQString maj = mimetype.left(index);
if ( d->groups.count() && !d->groups.contains( maj ) )
continue;
TQString min = mimetype.right(mimetype.length() - (index+1));
TQMapIterator<TQString,TQListViewItem*> mit = groups.find( maj );
if ( mit == groups.end() )
{
groupItem = new TQListViewItem( d->lvMimeTypes, maj );
groups.insert( maj, groupItem );
if ( maj == d->defaultgroup )
idefault = groupItem;
}
else
groupItem = mit.data();
TQCheckListItem *item = new TQCheckListItem( groupItem, min, TQCheckListItem::CheckBox );
item->setPixmap( 0, SmallIcon( (*it)->icon(TQString::null,false) ) );
int cl = 1;
if ( d->visuals & Comments )
{
item->setText( cl, (*it)->comment(TQString::null, false) );
cl++;
}
if ( d->visuals & Patterns )
item->setText( cl, (*it)->patterns().join("; ") );
if ( selMimeTypes.contains(mimetype) )
{
item->setOn( true );
groupItem->setOpen( true );
agroupisopen = true;
if ( !firstChecked )
firstChecked = item;
}
}
if ( firstChecked )
d->lvMimeTypes->ensureItemVisible( firstChecked );
if ( !agroupisopen && idefault )
{
idefault->setOpen( true );
d->lvMimeTypes->ensureItemVisible( idefault );
}
}
void KMimeTypeChooser::editMimeType()
{
if ( !(d->lvMimeTypes->currentItem() && (d->lvMimeTypes->currentItem())->parent()) )
return;
TQString mt = (d->lvMimeTypes->currentItem()->parent())->text( 0 ) + "/" + (d->lvMimeTypes->currentItem())->text( 0 );
// thanks to libkonq/konq_operations.cc
connect( KSycoca::self(), TQT_SIGNAL(databaseChanged()),
this, TQT_SLOT(slotSycocaDatabaseChanged()) );
TQString keditfiletype = TQString::fromLatin1("keditfiletype");
KRun::runCommand( keditfiletype
+ " --parent " + TQString::number( (ulong)topLevelWidget()->winId())
+ " " + TDEProcess::quote(mt),
keditfiletype, keditfiletype /*unused*/);
}
void KMimeTypeChooser::slotCurrentChanged(TQListViewItem* i)
{
if ( d->btnEditMimeType )
d->btnEditMimeType->setEnabled( i->parent() );
}
void KMimeTypeChooser::slotSycocaDatabaseChanged()
{
if ( KSycoca::self()->isChanged("mime") )
loadMimeTypes();
}
TQStringList KMimeTypeChooser::mimeTypes() const
{
TQStringList l;
TQListViewItemIterator it( d->lvMimeTypes );
for (; it.current(); ++it)
{
if ( it.current()->parent() && ((TQCheckListItem*)it.current())->isOn() )
l << it.current()->parent()->text(0) + "/" + it.current()->text(0); // FIXME uncecked, should be Ok unless someone changes mimetypes during this!
}
return l;
}
TQStringList KMimeTypeChooser::patterns() const
{
TQStringList l;
KMimeType::Ptr p;
TQString defMT = KMimeType::defaultMimeType();
TQListViewItemIterator it( d->lvMimeTypes );
for (; it.current(); ++it)
{
if ( it.current()->parent() && ((TQCheckListItem*)it.current())->isOn() )
{
p = KMimeType::mimeType( it.current()->parent()->text(0) + "/" + it.current()->text(0) );
if ( p->name() != defMT )
l += p->patterns();
}
}
return l;
}
//END
//BEGIN KMimeTypeChooserDialog
KMimeTypeChooserDialog::KMimeTypeChooserDialog(
const TQString &caption,
const TQString& text,
const TQStringList &selMimeTypes,
const TQString &defaultGroup,
const TQStringList &groupsToShow,
int visuals,
TQWidget *parent, const char *name )
: KDialogBase(parent, name, true, caption, Cancel|Ok, Ok)
{
m_chooser = new KMimeTypeChooser( text, selMimeTypes,
defaultGroup, groupsToShow, visuals,
this, "chooser" );
setMainWidget(m_chooser);
KConfigGroup group( TDEGlobal::config(), "KMimeTypeChooserDialog");
TQSize defaultSize( 400, 300 );
resize( group.readSizeEntry("size", &defaultSize) );
}
KMimeTypeChooserDialog::KMimeTypeChooserDialog(
const TQString &caption,
const TQString& text,
const TQStringList &selMimeTypes,
const TQString &defaultGroup,
TQWidget *parent, const char *name )
: KDialogBase(parent, name, true, caption, Cancel|Ok, Ok)
{
m_chooser = new KMimeTypeChooser( text, selMimeTypes,
defaultGroup, TQStringList(),
KMimeTypeChooser::Comments|KMimeTypeChooser::Patterns|KMimeTypeChooser::EditButton,
this, "chooser" );
setMainWidget(m_chooser);
KConfigGroup group( TDEGlobal::config(), "KMimeTypeChooserDialog");
TQSize defaultSize( 400, 300 );
resize( group.readSizeEntry("size", &defaultSize) );
}
KMimeTypeChooserDialog::~KMimeTypeChooserDialog()
{
KConfigGroup group( TDEGlobal::config(), "KMimeTypeChooserDialog");
group.writeEntry("size", size());
}
//END KMimeTypeChooserDialog
// kate: space-indent on; indent-width 2; replace-tabs on;
#include "kmimetypechooser.moc"
|