xref: /glogg/src/savedsearches.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 class SavedSearch
21 
22 #include <QSettings>
23 #include <QDataStream>
24 
25 #include "log.h"
26 #include "savedsearches.h"
27 
28 const int SavedSearches::SAVEDSEARCHES_VERSION = 1;
29 const int SavedSearches::maxNumberOfRecentSearches = 50;
30 
31 SavedSearches::SavedSearches() : savedSearches_()
32 {
33     qRegisterMetaTypeStreamOperators<SavedSearches>( "SavedSearches" );
34 }
35 
36 void SavedSearches::addRecent( const QString& text )
37 {
38     // We're not interested in blank lines
39     if ( text.isEmpty() )
40         return;
41 
42     // Remove any copy of the about to be added text
43     savedSearches_.removeAll( text );
44 
45     // Add at the front
46     savedSearches_.push_front( text );
47 
48     // Trim the list if it's too long
49     while (savedSearches_.size() > maxNumberOfRecentSearches)
50         savedSearches_.pop_back();
51 }
52 
53 QStringList SavedSearches::recentSearches() const
54 {
55     return savedSearches_;
56 }
57 
58 //
59 // Operators for serialization
60 //
61 
62 QDataStream& operator<<( QDataStream& out, const SavedSearches& object )
63 {
64     LOG(logDEBUG) << "<<operator from SavedSearches";
65 
66     out << object.savedSearches_;
67 
68     return out;
69 }
70 
71 QDataStream& operator>>( QDataStream& in, SavedSearches& object )
72 {
73     LOG(logDEBUG) << ">>operator from SavedSearches";
74 
75     in >> object.savedSearches_;
76 
77     return in;
78 }
79 
80 //
81 // Persistable virtual functions implementation
82 //
83 
84 void SavedSearches::saveToStorage( QSettings& settings ) const
85 {
86     LOG(logDEBUG) << "SavedSearches::saveToStorage";
87 
88     settings.beginGroup( "SavedSearches" );
89     // Remove everything in case the array is shorter than the previous one
90     settings.remove("");
91     settings.setValue( "version", SAVEDSEARCHES_VERSION );
92     settings.beginWriteArray( "searchHistory" );
93     for (int i = 0; i < savedSearches_.size(); ++i) {
94         settings.setArrayIndex( i );
95         settings.setValue( "string", savedSearches_.at( i ) );
96     }
97     settings.endArray();
98     settings.endGroup();
99 }
100 
101 void SavedSearches::retrieveFromStorage( QSettings& settings )
102 {
103     LOG(logDEBUG) << "SavedSearches::retrieveFromStorage";
104 
105     savedSearches_.clear();
106 
107     if ( settings.contains( "SavedSearches/version" ) ) {
108         // Unserialise the "new style" stored history
109         settings.beginGroup( "SavedSearches" );
110         if ( settings.value( "version" ) == SAVEDSEARCHES_VERSION ) {
111             int size = settings.beginReadArray( "searchHistory" );
112             for (int i = 0; i < size; ++i) {
113                 settings.setArrayIndex(i);
114                 QString search = settings.value( "string" ).toString();
115                 savedSearches_.append( search );
116             }
117             settings.endArray();
118         }
119         else {
120             LOG(logERROR) << "Unknown version of FilterSet, ignoring it...";
121         }
122         settings.endGroup();
123     }
124     else {
125         LOG(logWARNING) << "Trying to import legacy (<=0.8.2) saved searches...";
126         SavedSearches tmp_saved_searches =
127             settings.value( "savedSearches" ).value<SavedSearches>();
128         *this = tmp_saved_searches;
129         LOG(logWARNING) << "...imported searches: "
130             << savedSearches_.count() << " elements";
131         // Remove the old key once migration is done
132         settings.remove( "savedSearches" );
133         // And replace it with the new one
134         saveToStorage( settings );
135         settings.sync();
136     }
137 }
138