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 #include <QDataStream> 24 25 #include "log.h" 26 #include "filterset.h" 27 28 const int FilterSet::FILTERSET_VERSION = 1; 29 30 Qt::CaseSensitivity getCaseSensivity( bool ignoreCase ) 31 { 32 return ignoreCase ? Qt::CaseInsensitive : Qt::CaseSensitive; 33 } 34 35 Filter::Filter() 36 { 37 } 38 39 Filter::Filter(const QString& pattern, bool ignoreCase, 40 const QString& foreColorName, const QString& backColorName ) : 41 regexp_( pattern, getCaseSensivity( ignoreCase ) ), 42 foreColorName_( foreColorName ), 43 backColorName_( backColorName ), enabled_( true ) 44 { 45 LOG(logDEBUG) << "New Filter, fore: " << foreColorName_.toStdString() 46 << " back: " << backColorName_.toStdString(); 47 } 48 49 QString Filter::pattern() const 50 { 51 return regexp_.pattern(); 52 } 53 54 void Filter::setPattern( const QString& pattern ) 55 { 56 regexp_.setPattern( pattern ); 57 } 58 59 bool Filter::ignoreCase() const 60 { 61 return regexp_.caseSensitivity() == Qt::CaseInsensitive; 62 } 63 64 void Filter::setIgnoreCase( bool ignoreCase ) 65 { 66 regexp_.setCaseSensitivity( getCaseSensivity(ignoreCase) ); 67 } 68 69 const QString& Filter::foreColorName() const 70 { 71 return foreColorName_; 72 } 73 74 void Filter::setForeColor( const QString& foreColorName ) 75 { 76 foreColorName_ = foreColorName; 77 } 78 79 const QString& Filter::backColorName() const 80 { 81 return backColorName_; 82 } 83 84 void Filter::setBackColor( const QString& backColorName ) 85 { 86 backColorName_ = backColorName; 87 } 88 89 int Filter::indexIn( const QString& string ) const 90 { 91 return regexp_.indexIn( string ); 92 } 93 94 // 95 // Operators for serialization 96 // 97 98 QDataStream& operator<<( QDataStream& out, const Filter& object ) 99 { 100 LOG(logDEBUG) << "<<operator from Filter"; 101 out << object.regexp_; 102 out << object.foreColorName_; 103 out << object.backColorName_; 104 105 return out; 106 } 107 108 QDataStream& operator>>( QDataStream& in, Filter& object ) 109 { 110 LOG(logDEBUG) << ">>operator from Filter"; 111 in >> object.regexp_; 112 in >> object.foreColorName_; 113 in >> object.backColorName_; 114 115 return in; 116 } 117 118 119 // Default constructor 120 FilterSet::FilterSet() 121 { 122 qRegisterMetaTypeStreamOperators<Filter>( "Filter" ); 123 qRegisterMetaTypeStreamOperators<FilterSet>( "FilterSet" ); 124 qRegisterMetaTypeStreamOperators<FilterSet::FilterList>( "FilterSet::FilterList" ); 125 } 126 127 bool FilterSet::matchLine( const QString& line, 128 QColor* foreColor, QColor* backColor ) const 129 { 130 for ( QList<Filter>::const_iterator i = filterList.constBegin(); 131 i != filterList.constEnd(); i++ ) { 132 if ( i->indexIn( line ) != -1 ) { 133 foreColor->setNamedColor( i->foreColorName() ); 134 backColor->setNamedColor( i->backColorName() ); 135 return true; 136 } 137 } 138 139 return false; 140 } 141 142 // 143 // Operators for serialization 144 // 145 146 QDataStream& operator<<( QDataStream& out, const FilterSet& object ) 147 { 148 LOG(logDEBUG) << "<<operator from FilterSet"; 149 out << object.filterList; 150 151 return out; 152 } 153 154 QDataStream& operator>>( QDataStream& in, FilterSet& object ) 155 { 156 LOG(logDEBUG) << ">>operator from FilterSet"; 157 in >> object.filterList; 158 159 return in; 160 } 161 162 // 163 // Persistable virtual functions implementation 164 // 165 166 void Filter::saveToStorage( QSettings& settings ) const 167 { 168 LOG(logDEBUG) << "Filter::saveToStorage"; 169 170 settings.setValue( "regexp", regexp_.pattern() ); 171 settings.setValue( "ignore_case", regexp_.caseSensitivity() == Qt::CaseInsensitive); 172 settings.setValue( "fore_colour", foreColorName_ ); 173 settings.setValue( "back_colour", backColorName_ ); 174 } 175 176 void Filter::retrieveFromStorage( QSettings& settings ) 177 { 178 LOG(logDEBUG) << "Filter::retrieveFromStorage"; 179 180 regexp_ = QRegExp( settings.value( "regexp" ).toString(), 181 getCaseSensivity( settings.value( "ignore_case", false ).toBool() ) ); 182 foreColorName_ = settings.value( "fore_colour" ).toString(); 183 backColorName_ = settings.value( "back_colour" ).toString(); 184 } 185 186 void FilterSet::saveToStorage( QSettings& settings ) const 187 { 188 LOG(logDEBUG) << "FilterSet::saveToStorage"; 189 190 settings.beginGroup( "FilterSet" ); 191 // Remove everything in case the array is shorter than the previous one 192 settings.remove(""); 193 settings.setValue( "version", FILTERSET_VERSION ); 194 settings.beginWriteArray( "filters" ); 195 for (int i = 0; i < filterList.size(); ++i) { 196 settings.setArrayIndex(i); 197 filterList[i].saveToStorage( settings ); 198 } 199 settings.endArray(); 200 settings.endGroup(); 201 } 202 203 void FilterSet::retrieveFromStorage( QSettings& settings ) 204 { 205 LOG(logDEBUG) << "FilterSet::retrieveFromStorage"; 206 207 filterList.clear(); 208 209 if ( settings.contains( "FilterSet/version" ) ) { 210 settings.beginGroup( "FilterSet" ); 211 if ( settings.value( "version" ) == FILTERSET_VERSION ) { 212 int size = settings.beginReadArray( "filters" ); 213 for (int i = 0; i < size; ++i) { 214 settings.setArrayIndex(i); 215 Filter filter; 216 filter.retrieveFromStorage( settings ); 217 filterList.append( filter ); 218 } 219 settings.endArray(); 220 } 221 else { 222 LOG(logERROR) << "Unknown version of FilterSet, ignoring it..."; 223 } 224 settings.endGroup(); 225 } 226 else { 227 LOG(logWARNING) << "Trying to import legacy (<=0.8.2) filters..."; 228 FilterSet tmp_filter_set = 229 settings.value( "filterSet" ).value<FilterSet>(); 230 *this = tmp_filter_set; 231 LOG(logWARNING) << "...imported filterset: " 232 << filterList.count() << " elements"; 233 // Remove the old key once migration is done 234 settings.remove( "filterSet" ); 235 // And replace it with the new one 236 saveToStorage( settings ); 237 settings.sync(); 238 } 239 } 240