1/* This file is part of the KDE libraries
2 Copyright (C) Stephan Kulow <coolo@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "kfilefiltercombo.h"
21
22#include <kdebug.h>
23#include <klocale.h>
24#include <kmimetype.h>
25#include <config-kfile.h>
26#include <QtCore/QEvent>
27#include <QtGui/QLineEdit>
28
29class KFileFilterCombo::Private
30{
31public:
32 Private( KFileFilterCombo *_parent )
33 : parent(_parent),
34 hasAllSupportedFiles(false),
35 isMimeFilter(false),
36 defaultFilter(i18n("*|All Files"))
37 {
38 }
39
40 void _k_slotFilterChanged();
41
42 KFileFilterCombo *parent;
43 // when we have more than 3 mimefilters and no default-filter,
44 // we don't show the comments of all mimefilters in one line,
45 // instead we show "All supported files". We have to translate
46 // that back to the list of mimefilters in currentFilter() tho.
47 bool hasAllSupportedFiles;
48 // true when setMimeFilter was called
49 bool isMimeFilter;
50 QString lastFilter;
51 QString defaultFilter;
52
53 QStringList m_filters;
54 bool m_allTypes;
55};
56
57KFileFilterCombo::KFileFilterCombo( QWidget *parent)
58 : KComboBox(true, parent), d( new Private(this) )
59{
60 setTrapReturnKey( true );
61 setInsertPolicy(QComboBox::NoInsert);
62 connect( this, SIGNAL(activated(int)), this, SIGNAL(filterChanged()));
63 connect( this, SIGNAL(returnPressed()), this, SIGNAL(filterChanged()));
64 connect( this, SIGNAL(filterChanged()), SLOT(_k_slotFilterChanged()));
65 d->m_allTypes = false;
66}
67
68KFileFilterCombo::~KFileFilterCombo()
69{
70 delete d;
71}
72
73void KFileFilterCombo::setFilter(const QString& filter)
74{
75 clear();
76 d->m_filters.clear();
77 d->hasAllSupportedFiles = false;
78
79 if (!filter.isEmpty()) {
80 QString tmp = filter;
81 int index = tmp.indexOf('\n');
82 while (index > 0) {
83 d->m_filters.append(tmp.left(index));
84 tmp = tmp.mid(index + 1);
85 index = tmp.indexOf('\n');
86 }
87 d->m_filters.append(tmp);
88 }
89 else
90 d->m_filters.append( d->defaultFilter );
91
92 QStringList::ConstIterator it;
93 QStringList::ConstIterator end(d->m_filters.constEnd());
94 for (it = d->m_filters.constBegin(); it != end; ++it) {
95 int tab = (*it).indexOf('|');
96 addItem((tab < 0) ? *it :
97 (*it).mid(tab + 1));
98 }
99
100 d->lastFilter = currentText();
101 d->isMimeFilter = false;
102}
103
104QString KFileFilterCombo::currentFilter() const
105{
106 QString f = currentText();
107 if (f == itemText(currentIndex())) { // user didn't edit the text
108 f = d->m_filters.value(currentIndex());
109 if ( d->isMimeFilter || (currentIndex() == 0 && d->hasAllSupportedFiles) ) {
110 return f; // we have a mimetype as filter
111 }
112 }
113
114 int tab = f.indexOf('|');
115 if (tab < 0)
116 return f;
117 else
118 return f.left(tab);
119}
120
121bool KFileFilterCombo::showsAllTypes() const
122{
123 return d->m_allTypes;
124}
125
126QStringList KFileFilterCombo::filters() const
127{
128 return d->m_filters;
129}
130
131void KFileFilterCombo::setCurrentFilter( const QString& filter )
132{
133 setCurrentIndex(d->m_filters.indexOf(filter));
134 filterChanged();
135}
136
137void KFileFilterCombo::setMimeFilter( const QStringList& types,
138 const QString& defaultType )
139{
140 clear();
141 d->m_filters.clear();
142 QString delim = QLatin1String(", ");
143 d->hasAllSupportedFiles = false;
144 bool hasAllFilesFilter = false;
145
146 d->m_allTypes = defaultType.isEmpty() && (types.count() > 1);
147
148 QString allComments, allTypes;
149 for(QStringList::ConstIterator it = types.begin(); it != types.end(); ++it)
150 {
151 kDebug(kfile_area) << *it;
152 KMimeType::Ptr type = KMimeType::mimeType( *it );
153
154 if (!type) {
155 kDebug(kfile_area) << "Could not create mimetype!\n";
156 continue;
157 }
158
159 if ( type->name().startsWith( QLatin1String( "all/" ) ) ) {
160 hasAllFilesFilter = true;
161 continue;
162 }
163
164 if ( d->m_allTypes && it != types.begin() ) {
165 allComments += delim;
166 allTypes += ' ';
167 }
168
169 d->m_filters.append( type->name() );
170 if ( d->m_allTypes )
171 {
172 allTypes += type->name();
173 allComments += type->comment();
174 }
175 addItem( type->comment() );
176 if ( type->name() == defaultType )
177 setCurrentIndex( count() - 1 );
178 }
179
180 if ( d->m_allTypes )
181 {
182 if ( count() <= 3 ) // show the mime-comments of at max 3 types
183 insertItem(0, allComments);
184 else {
185 insertItem(0, i18n("All Supported Files"));
186 d->hasAllSupportedFiles = true;
187 }
188 setCurrentIndex( 0 );
189
190 d->m_filters.prepend( allTypes );
191 }
192
193 if ( hasAllFilesFilter ) {
194 addItem(i18n("All Files"));
195 d->m_filters.append( QLatin1String("all/allfiles") );
196 }
197
198 d->lastFilter = currentText();
199 d->isMimeFilter = true;
200}
201
202void KFileFilterCombo::Private::_k_slotFilterChanged()
203{
204 lastFilter = parent->currentText();
205}
206
207bool KFileFilterCombo::eventFilter( QObject *o, QEvent *e )
208{
209 if ( o == lineEdit() && e->type() == QEvent::FocusOut ) {
210 if ( currentText() != d->lastFilter )
211 emit filterChanged();
212 }
213
214 return KComboBox::eventFilter( o, e );
215}
216
217void KFileFilterCombo::setDefaultFilter( const QString& filter )
218{
219 d->defaultFilter = filter;
220}
221
222QString KFileFilterCombo::defaultFilter() const
223{
224 return d->defaultFilter;
225}
226
227bool KFileFilterCombo::isMimeFilter() const
228{
229 return d->isMimeFilter;
230}
231
232#include "kfilefiltercombo.moc"
233