2 This file is part of KMail.
3 Copyright (c) 2007 Till Adam <adam@kde.org>
5 KMail is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License, version 2, as
7 published by the Free Software Foundation.
9 KMail is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 In addition, as a special exception, the copyright holders give
19 permission to link the code of this program with any edition of
20 the Qt library by Trolltech AS, Norway (or with modified versions
21 of Qt that use the same license as Qt), and distribute linked
22 combinations including the two. You must obey the GNU General
23 Public License in all respects for all of the code used other than
24 Qt. If you modify this file, you may extend this exception to
25 your version of the file, but you are not obligated to do so. If
26 you do not wish to do so, delete this exception statement from
30 #include "filterimporterexporter.h"
31 #include "filterimporterexporter_p.h"
33 #include "filteraction.h"
34 #include "mailfilter.h"
38 #include <kfiledialog.h>
39 #include <kmessagebox.h>
40 #include <kpushbutton.h>
41 #include <messageviewer/autoqpointer.h>
42 #include <messageviewer/util.h>
44 #include <QtCore/QRegExp>
45 #include <QtGui/QListWidget>
46 #include <QtGui/QVBoxLayout>
48 using namespace MailCommon
;
50 FilterSelectionDialog::FilterSelectionDialog( QWidget
*parent
)
53 setObjectName( "filterselection" );
55 setCaption( i18n( "Select Filters" ) );
56 setButtons( Ok
| Cancel
);
57 setDefaultButton( Ok
);
58 showButtonSeparator( true );
60 QVBoxLayout
* const top
= new QVBoxLayout( mainWidget() );
61 filtersListWidget
= new QListWidget();
62 top
->addWidget( filtersListWidget
);
63 filtersListWidget
->setAlternatingRowColors( true );
64 filtersListWidget
->setSortingEnabled( false );
65 filtersListWidget
->setSelectionMode( QAbstractItemView::NoSelection
);
67 QHBoxLayout
* const buttonLayout
= new QHBoxLayout();
68 top
->addLayout( buttonLayout
);
69 selectAllButton
= new KPushButton( i18n( "Select All" ) );
70 buttonLayout
->addWidget( selectAllButton
);
71 unselectAllButton
= new KPushButton( i18n( "Unselect All" ) );
72 buttonLayout
->addWidget( unselectAllButton
);
74 connect( selectAllButton
, SIGNAL(clicked()), this, SLOT(slotSelectAllButton()) );
75 connect( unselectAllButton
, SIGNAL(clicked()), this, SLOT(slotUnselectAllButton()) );
80 FilterSelectionDialog::~FilterSelectionDialog()
84 void FilterSelectionDialog::setFilters( const QList
<MailFilter
*> &filters
)
86 if ( filters
.isEmpty() ) {
87 enableButtonOk( false );
91 originalFilters
= filters
;
92 filtersListWidget
->clear();
94 foreach ( const MailFilter
*filter
, filters
) {
95 QListWidgetItem
*item
= new QListWidgetItem( filter
->name(), filtersListWidget
);
96 item
->setFlags( Qt::ItemIsUserCheckable
| Qt::ItemIsEnabled
);
97 item
->setCheckState( Qt::Checked
);
101 QList
<MailFilter
*> FilterSelectionDialog::selectedFilters() const
103 QList
<MailFilter
*> filters
;
105 const int filterCount
= filtersListWidget
->count();
106 for ( int i
= 0; i
< filterCount
; ++i
) {
107 const QListWidgetItem
*item
= filtersListWidget
->item( i
);
108 if ( item
->checkState() == Qt::Checked
)
109 filters
<< originalFilters
[ i
];
115 void FilterSelectionDialog::slotUnselectAllButton()
117 const int filterCount
= filtersListWidget
->count();
118 for ( int i
= 0; i
< filterCount
; ++i
) {
119 QListWidgetItem
* const item
= filtersListWidget
->item( i
);
120 item
->setCheckState( Qt::Unchecked
);
124 void FilterSelectionDialog::slotSelectAllButton()
126 const int filterCount
= filtersListWidget
->count();
127 for ( int i
= 0; i
< filterCount
; ++i
) {
128 QListWidgetItem
* const item
= filtersListWidget
->item( i
);
129 item
->setCheckState( Qt::Checked
);
133 QList
<MailFilter
*> FilterImporterExporter::readFiltersFromConfig( const KSharedConfig::Ptr config
)
135 const KConfigGroup group
= config
->group( "General" );
137 const int numFilters
= group
.readEntry( "filters", 0 );
139 QList
<MailFilter
*> filters
;
140 for ( int i
= 0; i
< numFilters
; ++i
) {
141 const QString groupName
= QString::fromLatin1( "Filter #%1" ).arg( i
);
143 const KConfigGroup group
= config
->group( groupName
);
144 MailFilter
*filter
= new MailFilter( group
);
146 if ( filter
->isEmpty() ) {
148 kDebug() << "Filter" << filter
->asString() << "is empty!";
152 filters
.append( filter
);
158 void FilterImporterExporter::writeFiltersToConfig( const QList
<MailFilter
*> &filters
, KSharedConfig::Ptr config
)
160 // first, delete all filter groups:
161 const QStringList filterGroups
=
162 config
->groupList().filter( QRegExp( "Filter #\\d+" ) );
164 foreach ( const QString
&group
, filterGroups
)
165 config
->deleteGroup( group
);
168 foreach ( const MailFilter
*filter
, filters
) {
169 if ( !filter
->isEmpty() ) {
170 const QString groupName
= QString::fromLatin1( "Filter #%1" ).arg( i
);
172 KConfigGroup group
= config
->group( groupName
);
173 filter
->writeConfig( group
);
178 KConfigGroup group
= config
->group( "General" );
179 group
.writeEntry( "filters", i
);
184 class FilterImporterExporter::Private
187 Private( QWidget
*parent
)
196 FilterImporterExporter::FilterImporterExporter( QWidget
*parent
)
197 : d( new Private( parent
) )
201 FilterImporterExporter::~FilterImporterExporter()
206 QList
<MailFilter
*> FilterImporterExporter::importFilters()
208 const QString fileName
= KFileDialog::getOpenFileName( QDir::homePath(), QString(),
209 d
->mParent
, i18n( "Import Filters" ) );
210 if ( fileName
.isEmpty() )
211 return QList
<MailFilter
*>(); // cancel
214 QFile
file( fileName
);
215 if ( !file
.open( QIODevice::ReadOnly
) ) {
216 KMessageBox::error( d
->mParent
,
217 i18n( "The selected file is not readable. "
218 "Your file access permissions might be insufficient.") );
219 return QList
<MailFilter
*>();
223 const KSharedConfig::Ptr config
= KSharedConfig::openConfig( fileName
);
224 const QList
<MailFilter
*> imported
= readFiltersFromConfig( config
);
226 FilterSelectionDialog
dlg( d
->mParent
);
227 dlg
.setFilters( imported
);
229 return (dlg
.exec() == QDialog::Accepted
? dlg
.selectedFilters() : QList
<MailFilter
*>());
232 void FilterImporterExporter::exportFilters( const QList
<MailFilter
*> &filters
)
234 const KUrl saveUrl
= KFileDialog::getSaveUrl( QDir::homePath(), QString(),
235 d
->mParent
, i18n( "Export Filters" ) );
237 if ( saveUrl
.isEmpty() || !MessageViewer::Util::checkOverwrite( saveUrl
, d
->mParent
) )
240 KSharedConfig::Ptr config
= KSharedConfig::openConfig( saveUrl
.toLocalFile() );
241 MessageViewer::AutoQPointer
<FilterSelectionDialog
> dlg( new FilterSelectionDialog( d
->mParent
) );
242 dlg
->setFilters( filters
);
243 if ( dlg
->exec() == QDialog::Accepted
&& dlg
)
244 writeFiltersToConfig( dlg
->selectedFilters(), config
);
247 #include "filterimporterexporter_p.moc"