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