kmail

filterimporterexporter.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of KMail.
00003     Copyright (c) 2007 Till Adam <adam@kde.org>
00004 
00005     KMail is free software; you can redistribute it and/or modify it
00006     under the terms of the GNU General Public License, version 2, as
00007     published by the Free Software Foundation.
00008 
00009     KMail is distributed in the hope that it will be useful, but
00010     WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     General Public License for more details.
00013 
00014     You should have received a copy of the GNU General Public License
00015     along with this program; if not, write to the Free Software
00016     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00017 
00018     In addition, as a special exception, the copyright holders give
00019     permission to link the code of this program with any edition of
00020     the Qt library by Trolltech AS, Norway (or with modified versions
00021     of Qt that use the same license as Qt), and distribute linked
00022     combinations including the two.  You must obey the GNU General
00023     Public License in all respects for all of the code used other than
00024     Qt.  If you modify this file, you may extend this exception to
00025     your version of the file, but you are not obligated to do so.  If
00026     you do not wish to do so, delete this exception statement from
00027     your version.
00028 */
00029 
00030 #include "filterimporterexporter.h"
00031 
00032 #include "kmfilter.h"
00033 #include "kmfilteraction.h"
00034 #include "util.h"
00035 
00036 #include <kconfig.h>
00037 #include <kdebug.h>
00038 #include <kfiledialog.h>
00039 #include <kdialogbase.h>
00040 #include <klistview.h>
00041 
00042 #include <qregexp.h>
00043 
00044 
00045 using namespace KMail;
00046 
00047 class FilterSelectionDialog : public KDialogBase
00048 {
00049 public:
00050     FilterSelectionDialog( QWidget * parent = 0 )
00051         :KDialogBase( parent, "filterselection", true, i18n("Select Filters"), Ok|Cancel, Ok, true ),
00052         wasCancelled( false )
00053     {
00054         filtersListView = new KListView( this );
00055         setMainWidget(filtersListView);
00056         filtersListView->setSorting( -1 );
00057         filtersListView->setSelectionMode( QListView::NoSelection );
00058         filtersListView->addColumn( i18n("Filters"), 300 );
00059         filtersListView->setFullWidth( true );
00060         resize( 300, 350 );
00061     }
00062 
00063     virtual ~FilterSelectionDialog()
00064     {
00065     }
00066     
00067     virtual void slotCancel()
00068     {
00069         wasCancelled = true;
00070         KDialogBase::slotCancel();
00071     }
00072     
00073     bool cancelled()
00074     {
00075         return wasCancelled;
00076     }
00077 
00078     void setFilters( const QValueList<KMFilter*>& filters )
00079     {
00080         originalFilters = filters;
00081         filtersListView->clear();
00082         QValueListConstIterator<KMFilter*> it = filters.constEnd();
00083         while ( it != filters.constBegin() ) {
00084             --it;
00085             KMFilter* filter = *it;
00086             QCheckListItem* item = new QCheckListItem( filtersListView, filter->name(), QCheckListItem::CheckBox );
00087             item->setOn( true );
00088         }
00089     }
00090     
00091     QValueList<KMFilter*> selectedFilters() const
00092     {
00093         QValueList<KMFilter*> filters;
00094         QListViewItemIterator it( filtersListView );
00095         int i = 0;
00096         while( it.current() ) {
00097             QCheckListItem* item = static_cast<QCheckListItem*>( it.current() );
00098             if ( item->isOn() )
00099                 filters << originalFilters[i];
00100             ++i; ++it;
00101         }
00102         return filters;
00103     }
00104 private:
00105     KListView *filtersListView;
00106     QValueList<KMFilter*> originalFilters;
00107     bool wasCancelled;
00108 };
00109 
00110 /* static */
00111 QValueList<KMFilter*> FilterImporterExporter::readFiltersFromConfig( KConfig* config, bool bPopFilter )
00112 {
00113     KConfigGroupSaver saver(config, "General");
00114     int numFilters = 0;
00115     if (bPopFilter)
00116       numFilters = config->readNumEntry("popfilters",0);
00117     else
00118       numFilters = config->readNumEntry("filters",0);
00119     
00120     QValueList<KMFilter*> filters;
00121     for ( int i=0 ; i < numFilters ; ++i ) {
00122       QString grpName;
00123       grpName.sprintf("%s #%d", (bPopFilter ? "PopFilter" : "Filter") , i);
00124       KConfigGroupSaver saver(config, grpName);
00125       KMFilter * filter = new KMFilter(config, bPopFilter);
00126       filter->purify();
00127       if ( filter->isEmpty() ) {
00128     #ifndef NDEBUG
00129         kdDebug(5006) << "KMFilter::readConfig: filter\n" << filter->asString()
00130           << "is empty!" << endl;
00131     #endif
00132         delete filter;
00133       } else
00134         filters.append(filter);
00135     }
00136     return filters;
00137 }
00138 
00139 /* static */ 
00140 void FilterImporterExporter::writeFiltersToConfig( const QValueList<KMFilter*>& filters, KConfig* config, bool bPopFilter )
00141 {
00142     // first, delete all groups:
00143     QStringList filterGroups =
00144       config->groupList().grep( QRegExp( bPopFilter ? "PopFilter #\\d+" : "Filter #\\d+" ) );
00145     for ( QStringList::Iterator it = filterGroups.begin() ;
00146           it != filterGroups.end() ; ++it )
00147       config->deleteGroup( *it );
00148     
00149     int i = 0;
00150     for ( QValueListConstIterator<KMFilter*> it = filters.constBegin() ;
00151           it != filters.constEnd() ; ++it ) {
00152       if ( !(*it)->isEmpty() ) {
00153         QString grpName;
00154         if ( bPopFilter )
00155           grpName.sprintf("PopFilter #%d", i);
00156         else
00157           grpName.sprintf("Filter #%d", i);
00158         KConfigGroupSaver saver(config, grpName);
00159         (*it)->writeConfig(config);
00160         ++i;
00161       }
00162     }
00163     KConfigGroupSaver saver(config, "General");
00164     if (bPopFilter)
00165       config->writeEntry("popfilters", i);
00166     else
00167       config->writeEntry("filters", i);
00168 }
00169 
00170 
00171 FilterImporterExporter::FilterImporterExporter( QWidget* parent, bool popFilter )
00172 :mParent( parent), mPopFilter( popFilter )
00173 {
00174 }
00175 
00176 FilterImporterExporter::~FilterImporterExporter()
00177 {
00178 }
00179 
00180 QValueList<KMFilter*> FilterImporterExporter::importFilters()
00181 {
00182     QString fileName = KFileDialog::getOpenFileName( QDir::homeDirPath(), QString::null, mParent, i18n("Import Filters") );
00183     if ( fileName.isEmpty() ) 
00184         return QValueList<KMFilter*>(); // cancel
00185     
00186     { // scoping
00187         QFile f( fileName );
00188         if ( !f.open( IO_ReadOnly ) ) {
00189             KMessageBox::error( mParent, i18n("The selected file is not readable. Your file access permissions might be insufficient.") );
00190             return QValueList<KMFilter*>();
00191         }
00192     }
00193     
00194     KConfig config( fileName );
00195     QValueList<KMFilter*> imported = readFiltersFromConfig( &config, mPopFilter );
00196     FilterSelectionDialog dlg( mParent );
00197     dlg.setFilters( imported );
00198     dlg.exec();
00199     return dlg.cancelled() ? QValueList<KMFilter*>() : dlg.selectedFilters();
00200 }
00201 
00202 void FilterImporterExporter::exportFilters(const QValueList<KMFilter*> & filters )
00203 {
00204     KURL saveUrl = KFileDialog::getSaveURL( QDir::homeDirPath(), QString::null, mParent, i18n("Export Filters") );
00205     
00206     if ( saveUrl.isEmpty() || !Util::checkOverwrite( saveUrl, mParent ) )
00207       return;
00208     
00209     KConfig config( saveUrl.path() );
00210     FilterSelectionDialog dlg( mParent );
00211     dlg.setFilters( filters );
00212     dlg.exec();
00213     if ( !dlg.cancelled() )
00214         writeFiltersToConfig( dlg.selectedFilters(), &config, mPopFilter );
00215 }
00216