kmail

filterimporterexporter.cpp

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 #include <kpushbutton.h>
00042 
00043 #include <qregexp.h>
00044 #include <qlayout.h>
00045 
00046 
00047 using namespace KMail;
00048 
00049 FilterSelectionDialog::FilterSelectionDialog( QWidget * parent )
00050   :KDialogBase( parent, "filterselection", true, i18n("Select Filters"), Ok|Cancel, Ok, true ),
00051    wasCancelled( false )
00052 {
00053   QWidget *w = new QWidget( this );
00054   QVBoxLayout *top = new QVBoxLayout( w );
00055 
00056   filtersListView = new KListView( w );
00057   top->addWidget( filtersListView );
00058   setMainWidget(w);
00059   filtersListView->setSorting( -1 );
00060   filtersListView->setSelectionMode( QListView::NoSelection );
00061   filtersListView->addColumn( i18n("Filters"), 300 );
00062   filtersListView->setFullWidth( true );
00063   QHBoxLayout *buttonLayout = new QHBoxLayout( this );
00064   top->addLayout( buttonLayout );
00065   selectAllButton = new KPushButton( i18n( "Select All" ), w );
00066   buttonLayout->addWidget( selectAllButton );
00067   unselectAllButton = new KPushButton( i18n( "Unselect All" ), w );
00068   buttonLayout->addWidget( unselectAllButton );
00069   connect( selectAllButton, SIGNAL( clicked() ), this, SLOT( slotSelectAllButton() ) );
00070   connect( unselectAllButton, SIGNAL( clicked() ), this, SLOT( slotUnselectAllButton() ) );
00071   resize( 300, 350 );
00072 }
00073 
00074 FilterSelectionDialog::~FilterSelectionDialog()
00075 {
00076 }
00077 
00078 void FilterSelectionDialog::slotCancel()
00079 {
00080   wasCancelled = true;
00081   KDialogBase::slotCancel();
00082 }
00083 
00084 bool FilterSelectionDialog::cancelled()
00085 {
00086   return wasCancelled;
00087 }
00088 
00089 void FilterSelectionDialog::setFilters( const QValueList<KMFilter*>& filters )
00090 {
00091   if ( filters.isEmpty() )
00092   {
00093     enableButtonOK( false );
00094     return;
00095   }
00096   originalFilters = filters;
00097   filtersListView->clear();
00098   QValueListConstIterator<KMFilter*> it = filters.constEnd();
00099   while ( it != filters.constBegin() ) {
00100     --it;
00101     KMFilter* filter = *it;
00102     QCheckListItem* item = new QCheckListItem( filtersListView, filter->name(), QCheckListItem::CheckBox );
00103     item->setOn( true );
00104   }
00105 }
00106 
00107 QValueList<KMFilter*> FilterSelectionDialog::selectedFilters() const
00108 {
00109   QValueList<KMFilter*> filters;
00110   QListViewItemIterator it( filtersListView );
00111   int i = 0;
00112   while( it.current() ) {
00113     QCheckListItem* item = static_cast<QCheckListItem*>( it.current() );
00114     if ( item->isOn() )
00115       filters << originalFilters[i];
00116     ++i; ++it;
00117   }
00118   return filters;
00119 }
00120 
00121 void FilterSelectionDialog::slotUnselectAllButton()
00122 {
00123   QListViewItemIterator it( filtersListView );
00124   while( it.current() ) {
00125     QCheckListItem* item = static_cast<QCheckListItem*>( it.current() );
00126     item->setOn( false );
00127     ++it;
00128   }
00129 }
00130 
00131 void FilterSelectionDialog::slotSelectAllButton()
00132 {
00133   QListViewItemIterator it( filtersListView );
00134   while( it.current() ) {
00135     QCheckListItem* item = static_cast<QCheckListItem*>( it.current() );
00136     item->setOn( true );
00137     ++it;
00138   }
00139 }
00140 
00141 /* static */
00142 QValueList<KMFilter*> FilterImporterExporter::readFiltersFromConfig( KConfig* config, bool bPopFilter )
00143 {
00144     KConfigGroupSaver saver(config, "General");
00145     int numFilters = 0;
00146     if (bPopFilter)
00147       numFilters = config->readNumEntry("popfilters",0);
00148     else
00149       numFilters = config->readNumEntry("filters",0);
00150 
00151     QValueList<KMFilter*> filters;
00152     for ( int i=0 ; i < numFilters ; ++i ) {
00153       QString grpName;
00154       grpName.sprintf("%s #%d", (bPopFilter ? "PopFilter" : "Filter") , i);
00155       KConfigGroupSaver saver(config, grpName);
00156       KMFilter * filter = new KMFilter(config, bPopFilter);
00157       filter->purify();
00158       if ( filter->isEmpty() ) {
00159     #ifndef NDEBUG
00160         kdDebug(5006) << "KMFilter::readConfig: filter\n" << filter->asString()
00161           << "is empty!" << endl;
00162     #endif
00163         delete filter;
00164       } else
00165         filters.append(filter);
00166     }
00167     return filters;
00168 }
00169 
00170 /* static */
00171 void FilterImporterExporter::writeFiltersToConfig( const QValueList<KMFilter*>& filters, KConfig* config, bool bPopFilter )
00172 {
00173     // first, delete all groups:
00174     QStringList filterGroups =
00175       config->groupList().grep( QRegExp( bPopFilter ? "PopFilter #\\d+" : "Filter #\\d+" ) );
00176     for ( QStringList::Iterator it = filterGroups.begin() ;
00177           it != filterGroups.end() ; ++it )
00178       config->deleteGroup( *it );
00179 
00180     int i = 0;
00181     for ( QValueListConstIterator<KMFilter*> it = filters.constBegin() ;
00182           it != filters.constEnd() ; ++it ) {
00183       if ( !(*it)->isEmpty() ) {
00184         QString grpName;
00185         if ( bPopFilter )
00186           grpName.sprintf("PopFilter #%d", i);
00187         else
00188           grpName.sprintf("Filter #%d", i);
00189         KConfigGroupSaver saver(config, grpName);
00190         (*it)->writeConfig(config);
00191         ++i;
00192       }
00193     }
00194     KConfigGroupSaver saver(config, "General");
00195     if (bPopFilter)
00196       config->writeEntry("popfilters", i);
00197     else
00198       config->writeEntry("filters", i);
00199 }
00200 
00201 
00202 FilterImporterExporter::FilterImporterExporter( QWidget* parent, bool popFilter )
00203 :mParent( parent), mPopFilter( popFilter )
00204 {
00205 }
00206 
00207 FilterImporterExporter::~FilterImporterExporter()
00208 {
00209 }
00210 
00211 QValueList<KMFilter*> FilterImporterExporter::importFilters()
00212 {
00213     QString fileName = KFileDialog::getOpenFileName( QDir::homeDirPath(), QString::null, mParent, i18n("Import Filters") );
00214     if ( fileName.isEmpty() )
00215         return QValueList<KMFilter*>(); // cancel
00216 
00217     { // scoping
00218         QFile f( fileName );
00219         if ( !f.open( IO_ReadOnly ) ) {
00220             KMessageBox::error( mParent, i18n("The selected file is not readable. Your file access permissions might be insufficient.") );
00221             return QValueList<KMFilter*>();
00222         }
00223     }
00224 
00225     KConfig config( fileName );
00226     QValueList<KMFilter*> imported = readFiltersFromConfig( &config, mPopFilter );
00227     FilterSelectionDialog dlg( mParent );
00228     dlg.setFilters( imported );
00229     dlg.exec();
00230     return dlg.cancelled() ? QValueList<KMFilter*>() : dlg.selectedFilters();
00231 }
00232 
00233 void FilterImporterExporter::exportFilters(const QValueList<KMFilter*> & filters )
00234 {
00235     KURL saveUrl = KFileDialog::getSaveURL( QDir::homeDirPath(), QString::null, mParent, i18n("Export Filters") );
00236 
00237     if ( saveUrl.isEmpty() || !Util::checkOverwrite( saveUrl, mParent ) )
00238       return;
00239 
00240     KConfig config( saveUrl.path() );
00241     FilterSelectionDialog dlg( mParent );
00242     dlg.setFilters( filters );
00243     dlg.exec();
00244     if ( !dlg.cancelled() )
00245         writeFiltersToConfig( dlg.selectedFilters(), &config, mPopFilter );
00246 }
00247 
00248 #include "filterimporterexporter.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys