kmail

filterimporterexporter.cpp
1 /*
2  This file is part of KMail.
3  Copyright (c) 2007 Till Adam <adam@kde.org>
4 
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.
8 
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.
13 
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
17 
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 TQt library by Trolltech AS, Norway (or with modified versions
21  of TQt that use the same license as TQt), 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  TQt. 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
27  your version.
28 */
29 
30 #include "filterimporterexporter.h"
31 
32 #include "kmfilter.h"
33 #include "kmfilteraction.h"
34 #include "util.h"
35 
36 #include <tdeconfig.h>
37 #include <kdebug.h>
38 #include <tdefiledialog.h>
39 #include <kdialogbase.h>
40 #include <tdelistview.h>
41 #include <kpushbutton.h>
42 
43 #include <tqregexp.h>
44 #include <tqlayout.h>
45 
46 
47 using namespace KMail;
48 
49 FilterSelectionDialog::FilterSelectionDialog( TQWidget * parent )
50  :KDialogBase( parent, "filterselection", true, i18n("Select Filters"), Ok|Cancel, Ok, true ),
51  wasCancelled( false )
52 {
53  TQWidget *w = new TQWidget( this );
54  TQVBoxLayout *top = new TQVBoxLayout( w );
55 
56  filtersListView = new TDEListView( w );
57  top->addWidget( filtersListView );
58  setMainWidget(w);
59  filtersListView->setSorting( -1 );
60  filtersListView->setSelectionMode( TQListView::NoSelection );
61  filtersListView->addColumn( i18n("Filters"), 300 );
62  filtersListView->setFullWidth( true );
63  TQHBoxLayout *buttonLayout = new TQHBoxLayout( this );
64  top->addLayout( buttonLayout );
65  selectAllButton = new KPushButton( i18n( "Select All" ), w );
66  buttonLayout->addWidget( selectAllButton );
67  unselectAllButton = new KPushButton( i18n( "Unselect All" ), w );
68  buttonLayout->addWidget( unselectAllButton );
69  connect( selectAllButton, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotSelectAllButton() ) );
70  connect( unselectAllButton, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotUnselectAllButton() ) );
71  resize( 300, 350 );
72 }
73 
74 FilterSelectionDialog::~FilterSelectionDialog()
75 {
76 }
77 
78 void FilterSelectionDialog::slotCancel()
79 {
80  wasCancelled = true;
81  KDialogBase::slotCancel();
82 }
83 
84 bool FilterSelectionDialog::cancelled()
85 {
86  return wasCancelled;
87 }
88 
89 void FilterSelectionDialog::setFilters( const TQValueList<KMFilter*>& filters )
90 {
91  if ( filters.isEmpty() )
92  {
93  enableButtonOK( false );
94  return;
95  }
96  originalFilters = filters;
97  filtersListView->clear();
98  TQValueListConstIterator<KMFilter*> it = filters.constEnd();
99  while ( it != filters.constBegin() ) {
100  --it;
101  KMFilter* filter = *it;
102  TQCheckListItem* item = new TQCheckListItem( filtersListView, filter->name(), TQCheckListItem::CheckBox );
103  item->setOn( true );
104  }
105 }
106 
107 TQValueList<KMFilter*> FilterSelectionDialog::selectedFilters() const
108 {
109  TQValueList<KMFilter*> filters;
110  TQListViewItemIterator it( filtersListView );
111  int i = 0;
112  while( it.current() ) {
113  TQCheckListItem* item = static_cast<TQCheckListItem*>( it.current() );
114  if ( item->isOn() )
115  filters << originalFilters[i];
116  ++i; ++it;
117  }
118  return filters;
119 }
120 
121 void FilterSelectionDialog::slotUnselectAllButton()
122 {
123  TQListViewItemIterator it( filtersListView );
124  while( it.current() ) {
125  TQCheckListItem* item = static_cast<TQCheckListItem*>( it.current() );
126  item->setOn( false );
127  ++it;
128  }
129 }
130 
131 void FilterSelectionDialog::slotSelectAllButton()
132 {
133  TQListViewItemIterator it( filtersListView );
134  while( it.current() ) {
135  TQCheckListItem* item = static_cast<TQCheckListItem*>( it.current() );
136  item->setOn( true );
137  ++it;
138  }
139 }
140 
141 /* static */
142 TQValueList<KMFilter*> FilterImporterExporter::readFiltersFromConfig( TDEConfig* config, bool bPopFilter )
143 {
144  TDEConfigGroupSaver saver(config, "General");
145  int numFilters = 0;
146  if (bPopFilter)
147  numFilters = config->readNumEntry("popfilters",0);
148  else
149  numFilters = config->readNumEntry("filters",0);
150 
151  TQValueList<KMFilter*> filters;
152  for ( int i=0 ; i < numFilters ; ++i ) {
153  TQString grpName;
154  grpName.sprintf("%s #%d", (bPopFilter ? "PopFilter" : "Filter") , i);
155  TDEConfigGroupSaver saver(config, grpName);
156  KMFilter * filter = new KMFilter(config, bPopFilter);
157  filter->purify();
158  if ( filter->isEmpty() ) {
159  #ifndef NDEBUG
160  kdDebug(5006) << "KMFilter::readConfig: filter\n" << filter->asString()
161  << "is empty!" << endl;
162  #endif
163  delete filter;
164  } else
165  filters.append(filter);
166  }
167  return filters;
168 }
169 
170 /* static */
171 void FilterImporterExporter::writeFiltersToConfig( const TQValueList<KMFilter*>& filters, TDEConfig* config, bool bPopFilter )
172 {
173  // first, delete all groups:
174  TQStringList filterGroups =
175  config->groupList().grep( TQRegExp( bPopFilter ? "PopFilter #\\d+" : "Filter #\\d+" ) );
176  for ( TQStringList::Iterator it = filterGroups.begin() ;
177  it != filterGroups.end() ; ++it )
178  config->deleteGroup( *it );
179 
180  int i = 0;
181  for ( TQValueListConstIterator<KMFilter*> it = filters.constBegin() ;
182  it != filters.constEnd() ; ++it ) {
183  if ( !(*it)->isEmpty() ) {
184  TQString grpName;
185  if ( bPopFilter )
186  grpName.sprintf("PopFilter #%d", i);
187  else
188  grpName.sprintf("Filter #%d", i);
189  TDEConfigGroupSaver saver(config, grpName);
190  (*it)->writeConfig(config);
191  ++i;
192  }
193  }
194  TDEConfigGroupSaver saver(config, "General");
195  if (bPopFilter)
196  config->writeEntry("popfilters", i);
197  else
198  config->writeEntry("filters", i);
199 }
200 
201 
202 FilterImporterExporter::FilterImporterExporter( TQWidget* parent, bool popFilter )
203 :mParent( parent), mPopFilter( popFilter )
204 {
205 }
206 
207 FilterImporterExporter::~FilterImporterExporter()
208 {
209 }
210 
212 {
213  TQString fileName = KFileDialog::getOpenFileName( TQDir::homeDirPath(), TQString(), mParent, i18n("Import Filters") );
214  if ( fileName.isEmpty() )
215  return TQValueList<KMFilter*>(); // cancel
216 
217  { // scoping
218  TQFile f( fileName );
219  if ( !f.open( IO_ReadOnly ) ) {
220  KMessageBox::error( mParent, i18n("The selected file is not readable. Your file access permissions might be insufficient.") );
221  return TQValueList<KMFilter*>();
222  }
223  }
224 
225  TDEConfig config( fileName );
226  TQValueList<KMFilter*> imported = readFiltersFromConfig( &config, mPopFilter );
227  FilterSelectionDialog dlg( mParent );
228  dlg.setFilters( imported );
229  dlg.exec();
230  return dlg.cancelled() ? TQValueList<KMFilter*>() : dlg.selectedFilters();
231 }
232 
233 void FilterImporterExporter::exportFilters(const TQValueList<KMFilter*> & filters )
234 {
235  KURL saveUrl = KFileDialog::getSaveURL( TQDir::homeDirPath(), TQString(), mParent, i18n("Export Filters") );
236 
237  if ( saveUrl.isEmpty() || !Util::checkOverwrite( saveUrl, mParent ) )
238  return;
239 
240  TDEConfig config( saveUrl.path() );
241  FilterSelectionDialog dlg( mParent );
242  dlg.setFilters( filters );
243  dlg.exec();
244  if ( !dlg.cancelled() )
245  writeFiltersToConfig( dlg.selectedFilters(), &config, mPopFilter );
246 }
247 
248 #include "filterimporterexporter.moc"
void exportFilters(const TQValueList< KMFilter * > &)
Export the given filter rules to a file which is asked from the user.
TQValueList< KMFilter * > importFilters()
Import filters.
folderdiaquotatab.h
Definition: aboutdata.cpp:40
@ Ok
The user rights/ACL have been fetched from the server sucessfully.
Definition: acljobs.h:66