1bb02e0acSNicolas Bonnefon /* 2bb02e0acSNicolas Bonnefon * Copyright (C) 2009, 2010, 2011 Nicolas Bonnefon and other contributors 3bb02e0acSNicolas Bonnefon * 4bb02e0acSNicolas Bonnefon * This file is part of glogg. 5bb02e0acSNicolas Bonnefon * 6bb02e0acSNicolas Bonnefon * glogg is free software: you can redistribute it and/or modify 7bb02e0acSNicolas Bonnefon * it under the terms of the GNU General Public License as published by 8bb02e0acSNicolas Bonnefon * the Free Software Foundation, either version 3 of the License, or 9bb02e0acSNicolas Bonnefon * (at your option) any later version. 10bb02e0acSNicolas Bonnefon * 11bb02e0acSNicolas Bonnefon * glogg is distributed in the hope that it will be useful, 12bb02e0acSNicolas Bonnefon * but WITHOUT ANY WARRANTY; without even the implied warranty of 13bb02e0acSNicolas Bonnefon * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14bb02e0acSNicolas Bonnefon * GNU General Public License for more details. 15bb02e0acSNicolas Bonnefon * 16bb02e0acSNicolas Bonnefon * You should have received a copy of the GNU General Public License 17bb02e0acSNicolas Bonnefon * along with glogg. If not, see <http://www.gnu.org/licenses/>. 18bb02e0acSNicolas Bonnefon */ 19bb02e0acSNicolas Bonnefon 20bb02e0acSNicolas Bonnefon // This file implements classes Filter and FilterSet 21bb02e0acSNicolas Bonnefon 22bb02e0acSNicolas Bonnefon #include <QSettings> 231a30cd4dSJonathan Wakely #include <QDataStream> 24bb02e0acSNicolas Bonnefon 25bb02e0acSNicolas Bonnefon #include "log.h" 26bb02e0acSNicolas Bonnefon #include "filterset.h" 27bb02e0acSNicolas Bonnefon 28bb02e0acSNicolas Bonnefon const int FilterSet::FILTERSET_VERSION = 1; 29bb02e0acSNicolas Bonnefon 30*b9c9a815SAnton Filimonov Qt::CaseSensitivity getCaseSensivity( bool ignoreCase ) 31*b9c9a815SAnton Filimonov { 32*b9c9a815SAnton Filimonov return ignoreCase ? Qt::CaseInsensitive : Qt::CaseSensitive; 33*b9c9a815SAnton Filimonov } 34*b9c9a815SAnton Filimonov 35bb02e0acSNicolas Bonnefon Filter::Filter() 36bb02e0acSNicolas Bonnefon { 37bb02e0acSNicolas Bonnefon } 38bb02e0acSNicolas Bonnefon 39*b9c9a815SAnton Filimonov Filter::Filter(const QString& pattern, bool ignoreCase, 40bb02e0acSNicolas Bonnefon const QString& foreColorName, const QString& backColorName ) : 41*b9c9a815SAnton Filimonov regexp_( pattern, getCaseSensivity( ignoreCase ) ), 42*b9c9a815SAnton Filimonov foreColorName_( foreColorName ), 43bb02e0acSNicolas Bonnefon backColorName_( backColorName ), enabled_( true ) 44bb02e0acSNicolas Bonnefon { 45bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "New Filter, fore: " << foreColorName_.toStdString() 46bb02e0acSNicolas Bonnefon << " back: " << backColorName_.toStdString(); 47bb02e0acSNicolas Bonnefon } 48bb02e0acSNicolas Bonnefon 49bb02e0acSNicolas Bonnefon QString Filter::pattern() const 50bb02e0acSNicolas Bonnefon { 51bb02e0acSNicolas Bonnefon return regexp_.pattern(); 52bb02e0acSNicolas Bonnefon } 53bb02e0acSNicolas Bonnefon 54bb02e0acSNicolas Bonnefon void Filter::setPattern( const QString& pattern ) 55bb02e0acSNicolas Bonnefon { 56bb02e0acSNicolas Bonnefon regexp_.setPattern( pattern ); 57bb02e0acSNicolas Bonnefon } 58bb02e0acSNicolas Bonnefon 59*b9c9a815SAnton Filimonov bool Filter::ignoreCase() const 60*b9c9a815SAnton Filimonov { 61*b9c9a815SAnton Filimonov return regexp_.caseSensitivity() == Qt::CaseInsensitive; 62*b9c9a815SAnton Filimonov } 63*b9c9a815SAnton Filimonov 64*b9c9a815SAnton Filimonov void Filter::setIgnoreCase( bool ignoreCase ) 65*b9c9a815SAnton Filimonov { 66*b9c9a815SAnton Filimonov regexp_.setCaseSensitivity( getCaseSensivity(ignoreCase) ); 67*b9c9a815SAnton Filimonov } 68*b9c9a815SAnton Filimonov 69bb02e0acSNicolas Bonnefon const QString& Filter::foreColorName() const 70bb02e0acSNicolas Bonnefon { 71bb02e0acSNicolas Bonnefon return foreColorName_; 72bb02e0acSNicolas Bonnefon } 73bb02e0acSNicolas Bonnefon 74bb02e0acSNicolas Bonnefon void Filter::setForeColor( const QString& foreColorName ) 75bb02e0acSNicolas Bonnefon { 76bb02e0acSNicolas Bonnefon foreColorName_ = foreColorName; 77bb02e0acSNicolas Bonnefon } 78bb02e0acSNicolas Bonnefon 79bb02e0acSNicolas Bonnefon const QString& Filter::backColorName() const 80bb02e0acSNicolas Bonnefon { 81bb02e0acSNicolas Bonnefon return backColorName_; 82bb02e0acSNicolas Bonnefon } 83bb02e0acSNicolas Bonnefon 84bb02e0acSNicolas Bonnefon void Filter::setBackColor( const QString& backColorName ) 85bb02e0acSNicolas Bonnefon { 86bb02e0acSNicolas Bonnefon backColorName_ = backColorName; 87bb02e0acSNicolas Bonnefon } 88bb02e0acSNicolas Bonnefon 89bb02e0acSNicolas Bonnefon int Filter::indexIn( const QString& string ) const 90bb02e0acSNicolas Bonnefon { 91bb02e0acSNicolas Bonnefon return regexp_.indexIn( string ); 92bb02e0acSNicolas Bonnefon } 93bb02e0acSNicolas Bonnefon 94bb02e0acSNicolas Bonnefon // 95bb02e0acSNicolas Bonnefon // Operators for serialization 96bb02e0acSNicolas Bonnefon // 97bb02e0acSNicolas Bonnefon 98bb02e0acSNicolas Bonnefon QDataStream& operator<<( QDataStream& out, const Filter& object ) 99bb02e0acSNicolas Bonnefon { 100bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "<<operator from Filter"; 101bb02e0acSNicolas Bonnefon out << object.regexp_; 102bb02e0acSNicolas Bonnefon out << object.foreColorName_; 103bb02e0acSNicolas Bonnefon out << object.backColorName_; 104bb02e0acSNicolas Bonnefon 105bb02e0acSNicolas Bonnefon return out; 106bb02e0acSNicolas Bonnefon } 107bb02e0acSNicolas Bonnefon 108bb02e0acSNicolas Bonnefon QDataStream& operator>>( QDataStream& in, Filter& object ) 109bb02e0acSNicolas Bonnefon { 110bb02e0acSNicolas Bonnefon LOG(logDEBUG) << ">>operator from Filter"; 111bb02e0acSNicolas Bonnefon in >> object.regexp_; 112bb02e0acSNicolas Bonnefon in >> object.foreColorName_; 113bb02e0acSNicolas Bonnefon in >> object.backColorName_; 114bb02e0acSNicolas Bonnefon 115bb02e0acSNicolas Bonnefon return in; 116bb02e0acSNicolas Bonnefon } 117bb02e0acSNicolas Bonnefon 118bb02e0acSNicolas Bonnefon 119bb02e0acSNicolas Bonnefon // Default constructor 120bb02e0acSNicolas Bonnefon FilterSet::FilterSet() 121bb02e0acSNicolas Bonnefon { 122bb02e0acSNicolas Bonnefon qRegisterMetaTypeStreamOperators<Filter>( "Filter" ); 123bb02e0acSNicolas Bonnefon qRegisterMetaTypeStreamOperators<FilterSet>( "FilterSet" ); 124bb02e0acSNicolas Bonnefon qRegisterMetaTypeStreamOperators<FilterSet::FilterList>( "FilterSet::FilterList" ); 125bb02e0acSNicolas Bonnefon } 126bb02e0acSNicolas Bonnefon 127bb02e0acSNicolas Bonnefon bool FilterSet::matchLine( const QString& line, 128bb02e0acSNicolas Bonnefon QColor* foreColor, QColor* backColor ) const 129bb02e0acSNicolas Bonnefon { 130bb02e0acSNicolas Bonnefon for ( QList<Filter>::const_iterator i = filterList.constBegin(); 131bb02e0acSNicolas Bonnefon i != filterList.constEnd(); i++ ) { 132bb02e0acSNicolas Bonnefon if ( i->indexIn( line ) != -1 ) { 133bb02e0acSNicolas Bonnefon foreColor->setNamedColor( i->foreColorName() ); 134bb02e0acSNicolas Bonnefon backColor->setNamedColor( i->backColorName() ); 135bb02e0acSNicolas Bonnefon return true; 136bb02e0acSNicolas Bonnefon } 137bb02e0acSNicolas Bonnefon } 138bb02e0acSNicolas Bonnefon 139bb02e0acSNicolas Bonnefon return false; 140bb02e0acSNicolas Bonnefon } 141bb02e0acSNicolas Bonnefon 142bb02e0acSNicolas Bonnefon // 143bb02e0acSNicolas Bonnefon // Operators for serialization 144bb02e0acSNicolas Bonnefon // 145bb02e0acSNicolas Bonnefon 146bb02e0acSNicolas Bonnefon QDataStream& operator<<( QDataStream& out, const FilterSet& object ) 147bb02e0acSNicolas Bonnefon { 148bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "<<operator from FilterSet"; 149bb02e0acSNicolas Bonnefon out << object.filterList; 150bb02e0acSNicolas Bonnefon 151bb02e0acSNicolas Bonnefon return out; 152bb02e0acSNicolas Bonnefon } 153bb02e0acSNicolas Bonnefon 154bb02e0acSNicolas Bonnefon QDataStream& operator>>( QDataStream& in, FilterSet& object ) 155bb02e0acSNicolas Bonnefon { 156bb02e0acSNicolas Bonnefon LOG(logDEBUG) << ">>operator from FilterSet"; 157bb02e0acSNicolas Bonnefon in >> object.filterList; 158bb02e0acSNicolas Bonnefon 159bb02e0acSNicolas Bonnefon return in; 160bb02e0acSNicolas Bonnefon } 161bb02e0acSNicolas Bonnefon 162bb02e0acSNicolas Bonnefon // 163bb02e0acSNicolas Bonnefon // Persistable virtual functions implementation 164bb02e0acSNicolas Bonnefon // 165bb02e0acSNicolas Bonnefon 166bb02e0acSNicolas Bonnefon void Filter::saveToStorage( QSettings& settings ) const 167bb02e0acSNicolas Bonnefon { 168bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "Filter::saveToStorage"; 169bb02e0acSNicolas Bonnefon 170bb02e0acSNicolas Bonnefon settings.setValue( "regexp", regexp_.pattern() ); 171*b9c9a815SAnton Filimonov settings.setValue( "ignore_case", regexp_.caseSensitivity() == Qt::CaseInsensitive); 172bb02e0acSNicolas Bonnefon settings.setValue( "fore_colour", foreColorName_ ); 173bb02e0acSNicolas Bonnefon settings.setValue( "back_colour", backColorName_ ); 174bb02e0acSNicolas Bonnefon } 175bb02e0acSNicolas Bonnefon 176bb02e0acSNicolas Bonnefon void Filter::retrieveFromStorage( QSettings& settings ) 177bb02e0acSNicolas Bonnefon { 178bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "Filter::retrieveFromStorage"; 179bb02e0acSNicolas Bonnefon 180*b9c9a815SAnton Filimonov regexp_ = QRegExp( settings.value( "regexp" ).toString(), 181*b9c9a815SAnton Filimonov getCaseSensivity( settings.value( "ignore_case", false ).toBool() ) ); 182bb02e0acSNicolas Bonnefon foreColorName_ = settings.value( "fore_colour" ).toString(); 183bb02e0acSNicolas Bonnefon backColorName_ = settings.value( "back_colour" ).toString(); 184bb02e0acSNicolas Bonnefon } 185bb02e0acSNicolas Bonnefon 186bb02e0acSNicolas Bonnefon void FilterSet::saveToStorage( QSettings& settings ) const 187bb02e0acSNicolas Bonnefon { 188bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "FilterSet::saveToStorage"; 189bb02e0acSNicolas Bonnefon 190bb02e0acSNicolas Bonnefon settings.beginGroup( "FilterSet" ); 191bb02e0acSNicolas Bonnefon // Remove everything in case the array is shorter than the previous one 192bb02e0acSNicolas Bonnefon settings.remove(""); 193bb02e0acSNicolas Bonnefon settings.setValue( "version", FILTERSET_VERSION ); 194bb02e0acSNicolas Bonnefon settings.beginWriteArray( "filters" ); 195bb02e0acSNicolas Bonnefon for (int i = 0; i < filterList.size(); ++i) { 196bb02e0acSNicolas Bonnefon settings.setArrayIndex(i); 197bb02e0acSNicolas Bonnefon filterList[i].saveToStorage( settings ); 198bb02e0acSNicolas Bonnefon } 199bb02e0acSNicolas Bonnefon settings.endArray(); 200bb02e0acSNicolas Bonnefon settings.endGroup(); 201bb02e0acSNicolas Bonnefon } 202bb02e0acSNicolas Bonnefon 203bb02e0acSNicolas Bonnefon void FilterSet::retrieveFromStorage( QSettings& settings ) 204bb02e0acSNicolas Bonnefon { 205bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "FilterSet::retrieveFromStorage"; 206bb02e0acSNicolas Bonnefon 207bb02e0acSNicolas Bonnefon filterList.clear(); 208bb02e0acSNicolas Bonnefon 209bb02e0acSNicolas Bonnefon if ( settings.contains( "FilterSet/version" ) ) { 210bb02e0acSNicolas Bonnefon settings.beginGroup( "FilterSet" ); 211bb02e0acSNicolas Bonnefon if ( settings.value( "version" ) == FILTERSET_VERSION ) { 212bb02e0acSNicolas Bonnefon int size = settings.beginReadArray( "filters" ); 213bb02e0acSNicolas Bonnefon for (int i = 0; i < size; ++i) { 214bb02e0acSNicolas Bonnefon settings.setArrayIndex(i); 215bb02e0acSNicolas Bonnefon Filter filter; 216bb02e0acSNicolas Bonnefon filter.retrieveFromStorage( settings ); 217bb02e0acSNicolas Bonnefon filterList.append( filter ); 218bb02e0acSNicolas Bonnefon } 219bb02e0acSNicolas Bonnefon settings.endArray(); 220bb02e0acSNicolas Bonnefon } 221bb02e0acSNicolas Bonnefon else { 222bb02e0acSNicolas Bonnefon LOG(logERROR) << "Unknown version of FilterSet, ignoring it..."; 223bb02e0acSNicolas Bonnefon } 224bb02e0acSNicolas Bonnefon settings.endGroup(); 225bb02e0acSNicolas Bonnefon } 226bb02e0acSNicolas Bonnefon else { 227bb02e0acSNicolas Bonnefon LOG(logWARNING) << "Trying to import legacy (<=0.8.2) filters..."; 228bb02e0acSNicolas Bonnefon FilterSet tmp_filter_set = 229bb02e0acSNicolas Bonnefon settings.value( "filterSet" ).value<FilterSet>(); 230bb02e0acSNicolas Bonnefon *this = tmp_filter_set; 231bb02e0acSNicolas Bonnefon LOG(logWARNING) << "...imported filterset: " 232bb02e0acSNicolas Bonnefon << filterList.count() << " elements"; 233bb02e0acSNicolas Bonnefon // Remove the old key once migration is done 234bb02e0acSNicolas Bonnefon settings.remove( "filterSet" ); 235bb02e0acSNicolas Bonnefon // And replace it with the new one 236bb02e0acSNicolas Bonnefon saveToStorage( settings ); 237bb02e0acSNicolas Bonnefon settings.sync(); 238bb02e0acSNicolas Bonnefon } 239bb02e0acSNicolas Bonnefon } 240