kaddressbook Library API Documentation

filter.cpp

00001 /*                                                                      
00002     This file is part of KAddressBook.                                  
00003     Copyright (c) 2002 Mike Pilone <mpilone@slac.com>                   
00004                                                                         
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or   
00008     (at your option) any later version.                                 
00009                                                                         
00010     This program is distributed in the hope that it will be useful,     
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of      
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the        
00013     GNU General Public License for more details.                        
00014                                                                         
00015     You should have received a copy of the GNU General Public License   
00016     along with this program; if not, write to the Free Software         
00017     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.           
00018                                                                         
00019     As a special exception, permission is given to link this program    
00020     with any edition of Qt, and distribute the resulting executable,    
00021     without including the source code for Qt in the source distribution.
00022 */                                                                      
00023 
00024 #include <kconfig.h>
00025 #include <kdebug.h>
00026 
00027 #include "filter.h"
00028 
00029 Filter::Filter()
00030 {
00031   mName = QString::null;
00032   mEnabled = true;
00033   mMatchRule = Matching;
00034 }
00035 
00036 Filter::Filter(const QString &name)
00037 {
00038   mName = name;
00039   mEnabled = true;
00040   mMatchRule = Matching;
00041 }
00042 
00043 Filter::Filter(const Filter &copyFrom)
00044 {
00045   (*this) = copyFrom;
00046 }
00047 
00048 Filter::~Filter()
00049 {
00050 }
00051 
00052 Filter &Filter::operator=(const Filter &copyFrom)
00053 {
00054   if (this == &copyFrom)
00055     return *this;
00056     
00057   mName = copyFrom.mName;
00058   mEnabled = copyFrom.mEnabled;
00059   mMatchRule = copyFrom.mMatchRule;
00060   mCategoryList = copyFrom.mCategoryList;
00061   
00062   return *this;
00063 }
00064 
00065 void Filter::apply(KABC::Addressee::List &addresseeList)
00066 {
00067   KABC::Addressee::List::Iterator iter;
00068   for (iter = addresseeList.begin(); iter != addresseeList.end(); )
00069   {
00070     if (filterAddressee(*iter))
00071       ++iter;
00072     else
00073       iter = addresseeList.erase(iter);
00074   }
00075 }
00076 
00077 bool Filter::filterAddressee(const KABC::Addressee &a)
00078 {
00079   QStringList::Iterator iter;
00080   iter = mCategoryList.begin();
00081   // empty filter always matches
00082 
00083   if ( iter == mCategoryList.end() )
00084     return true;
00085 
00086   for (; iter != mCategoryList.end(); ++iter ) {
00087     if ( a.hasCategory( *iter ) )
00088       return ( mMatchRule == Matching );
00089   }
00090   
00091   return !( mMatchRule == Matching );
00092 }
00093 
00094 void Filter::save(KConfig *config)
00095 {
00096   config->writeEntry("Name", mName);
00097   config->writeEntry("Enabled", mEnabled);
00098   config->writeEntry("Categories", mCategoryList);
00099   config->writeEntry("MatchRule", (int)mMatchRule);
00100 }
00101 
00102 void Filter::restore(KConfig *config)
00103 {
00104   mName = config->readEntry("Name", "<internal error>");
00105   mEnabled = config->readBoolEntry("Enabled", true);
00106   mCategoryList = config->readListEntry("Categories");
00107   mMatchRule = (MatchRule)config->readNumEntry("MatchRule", Matching);
00108 }
00109 
00110 void Filter::save(KConfig *config, QString baseGroup, 
00111                      Filter::List &list)
00112 {
00113   {
00114     KConfigGroupSaver s(config, baseGroup);
00115 
00116     // remove the old filters
00117     uint count = config->readNumEntry( "Count" );
00118     for ( uint i = 0; i < count; ++i )
00119       config->deleteGroup( QString("%1_%2").arg(baseGroup).arg(i) );
00120 
00121     config->writeEntry("Count", list.count());
00122   }
00123   
00124   int index = 0;
00125   Filter::List::Iterator iter;
00126   for (iter = list.begin(); iter != list.end(); ++iter)
00127   {
00128     {
00129       KConfigGroupSaver s(config, QString("%1_%2").arg(baseGroup).arg(index));
00130       (*iter).save(config);
00131     }
00132     index++;
00133   }
00134 }
00135                      
00136 Filter::List Filter::restore(KConfig *config, QString baseGroup)
00137 {
00138   Filter::List list;
00139   int count = 0;
00140   Filter f;
00141   
00142   {
00143     KConfigGroupSaver s(config, baseGroup);
00144     count = config->readNumEntry("Count", 0);
00145   }
00146   
00147   for (int i = 0; i < count; i++)
00148   {
00149     {
00150       KConfigGroupSaver s(config, QString("%1_%2").arg(baseGroup).arg(i));
00151       f.restore(config);
00152     }
00153     list.append(f);
00154   }
00155   
00156   return list;
00157 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sat Oct 18 02:47:09 2003 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001