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