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 TQt library by Trolltech AS, Norway (or with modified versions 00021 of TQt that use the same license as TQt), 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 TQt. 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 <tdeconfig.h> 00037 #include <kdebug.h> 00038 #include <tdefiledialog.h> 00039 #include <kdialogbase.h> 00040 #include <tdelistview.h> 00041 #include <kpushbutton.h> 00042 00043 #include <tqregexp.h> 00044 #include <tqlayout.h> 00045 00046 00047 using namespace KMail; 00048 00049 FilterSelectionDialog::FilterSelectionDialog( TQWidget * parent ) 00050 :KDialogBase( parent, "filterselection", true, i18n("Select Filters"), Ok|Cancel, Ok, true ), 00051 wasCancelled( false ) 00052 { 00053 TQWidget *w = new TQWidget( this ); 00054 TQVBoxLayout *top = new TQVBoxLayout( w ); 00055 00056 filtersListView = new TDEListView( w ); 00057 top->addWidget( filtersListView ); 00058 setMainWidget(w); 00059 filtersListView->setSorting( -1 ); 00060 filtersListView->setSelectionMode( TQListView::NoSelection ); 00061 filtersListView->addColumn( i18n("Filters"), 300 ); 00062 filtersListView->setFullWidth( true ); 00063 TQHBoxLayout *buttonLayout = new TQHBoxLayout( 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, TQT_SIGNAL( clicked() ), this, TQT_SLOT( slotSelectAllButton() ) ); 00070 connect( unselectAllButton, TQT_SIGNAL( clicked() ), this, TQT_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 TQValueList<KMFilter*>& filters ) 00090 { 00091 if ( filters.isEmpty() ) 00092 { 00093 enableButtonOK( false ); 00094 return; 00095 } 00096 originalFilters = filters; 00097 filtersListView->clear(); 00098 TQValueListConstIterator<KMFilter*> it = filters.constEnd(); 00099 while ( it != filters.constBegin() ) { 00100 --it; 00101 KMFilter* filter = *it; 00102 TQCheckListItem* item = new TQCheckListItem( filtersListView, filter->name(), TQCheckListItem::CheckBox ); 00103 item->setOn( true ); 00104 } 00105 } 00106 00107 TQValueList<KMFilter*> FilterSelectionDialog::selectedFilters() const 00108 { 00109 TQValueList<KMFilter*> filters; 00110 TQListViewItemIterator it( filtersListView ); 00111 int i = 0; 00112 while( it.current() ) { 00113 TQCheckListItem* item = static_cast<TQCheckListItem*>( 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 TQListViewItemIterator it( filtersListView ); 00124 while( it.current() ) { 00125 TQCheckListItem* item = static_cast<TQCheckListItem*>( it.current() ); 00126 item->setOn( false ); 00127 ++it; 00128 } 00129 } 00130 00131 void FilterSelectionDialog::slotSelectAllButton() 00132 { 00133 TQListViewItemIterator it( filtersListView ); 00134 while( it.current() ) { 00135 TQCheckListItem* item = static_cast<TQCheckListItem*>( it.current() ); 00136 item->setOn( true ); 00137 ++it; 00138 } 00139 } 00140 00141 /* static */ 00142 TQValueList<KMFilter*> FilterImporterExporter::readFiltersFromConfig( TDEConfig* config, bool bPopFilter ) 00143 { 00144 TDEConfigGroupSaver 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 TQValueList<KMFilter*> filters; 00152 for ( int i=0 ; i < numFilters ; ++i ) { 00153 TQString grpName; 00154 grpName.sprintf("%s #%d", (bPopFilter ? "PopFilter" : "Filter") , i); 00155 TDEConfigGroupSaver 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 TQValueList<KMFilter*>& filters, TDEConfig* config, bool bPopFilter ) 00172 { 00173 // first, delete all groups: 00174 TQStringList filterGroups = 00175 config->groupList().grep( TQRegExp( bPopFilter ? "PopFilter #\\d+" : "Filter #\\d+" ) ); 00176 for ( TQStringList::Iterator it = filterGroups.begin() ; 00177 it != filterGroups.end() ; ++it ) 00178 config->deleteGroup( *it ); 00179 00180 int i = 0; 00181 for ( TQValueListConstIterator<KMFilter*> it = filters.constBegin() ; 00182 it != filters.constEnd() ; ++it ) { 00183 if ( !(*it)->isEmpty() ) { 00184 TQString grpName; 00185 if ( bPopFilter ) 00186 grpName.sprintf("PopFilter #%d", i); 00187 else 00188 grpName.sprintf("Filter #%d", i); 00189 TDEConfigGroupSaver saver(config, grpName); 00190 (*it)->writeConfig(config); 00191 ++i; 00192 } 00193 } 00194 TDEConfigGroupSaver saver(config, "General"); 00195 if (bPopFilter) 00196 config->writeEntry("popfilters", i); 00197 else 00198 config->writeEntry("filters", i); 00199 } 00200 00201 00202 FilterImporterExporter::FilterImporterExporter( TQWidget* parent, bool popFilter ) 00203 :mParent( parent), mPopFilter( popFilter ) 00204 { 00205 } 00206 00207 FilterImporterExporter::~FilterImporterExporter() 00208 { 00209 } 00210 00211 TQValueList<KMFilter*> FilterImporterExporter::importFilters() 00212 { 00213 TQString fileName = KFileDialog::getOpenFileName( TQDir::homeDirPath(), TQString(), mParent, i18n("Import Filters") ); 00214 if ( fileName.isEmpty() ) 00215 return TQValueList<KMFilter*>(); // cancel 00216 00217 { // scoping 00218 TQFile 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 TQValueList<KMFilter*>(); 00222 } 00223 } 00224 00225 TDEConfig config( fileName ); 00226 TQValueList<KMFilter*> imported = readFiltersFromConfig( &config, mPopFilter ); 00227 FilterSelectionDialog dlg( mParent ); 00228 dlg.setFilters( imported ); 00229 dlg.exec(); 00230 return dlg.cancelled() ? TQValueList<KMFilter*>() : dlg.selectedFilters(); 00231 } 00232 00233 void FilterImporterExporter::exportFilters(const TQValueList<KMFilter*> & filters ) 00234 { 00235 KURL saveUrl = KFileDialog::getSaveURL( TQDir::homeDirPath(), TQString(), mParent, i18n("Export Filters") ); 00236 00237 if ( saveUrl.isEmpty() || !Util::checkOverwrite( saveUrl, mParent ) ) 00238 return; 00239 00240 TDEConfig 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"