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