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