1*bb02e0acSNicolas Bonnefon /* 2*bb02e0acSNicolas Bonnefon * Copyright (C) 2009, 2010, 2011 Nicolas Bonnefon and other contributors 3*bb02e0acSNicolas Bonnefon * 4*bb02e0acSNicolas Bonnefon * This file is part of glogg. 5*bb02e0acSNicolas Bonnefon * 6*bb02e0acSNicolas Bonnefon * glogg is free software: you can redistribute it and/or modify 7*bb02e0acSNicolas Bonnefon * it under the terms of the GNU General Public License as published by 8*bb02e0acSNicolas Bonnefon * the Free Software Foundation, either version 3 of the License, or 9*bb02e0acSNicolas Bonnefon * (at your option) any later version. 10*bb02e0acSNicolas Bonnefon * 11*bb02e0acSNicolas Bonnefon * glogg is distributed in the hope that it will be useful, 12*bb02e0acSNicolas Bonnefon * but WITHOUT ANY WARRANTY; without even the implied warranty of 13*bb02e0acSNicolas Bonnefon * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*bb02e0acSNicolas Bonnefon * GNU General Public License for more details. 15*bb02e0acSNicolas Bonnefon * 16*bb02e0acSNicolas Bonnefon * You should have received a copy of the GNU General Public License 17*bb02e0acSNicolas Bonnefon * along with glogg. If not, see <http://www.gnu.org/licenses/>. 18*bb02e0acSNicolas Bonnefon */ 19*bb02e0acSNicolas Bonnefon 20*bb02e0acSNicolas Bonnefon // This file implements class SavedSearch 21*bb02e0acSNicolas Bonnefon 22*bb02e0acSNicolas Bonnefon #include <QSettings> 23*bb02e0acSNicolas Bonnefon 24*bb02e0acSNicolas Bonnefon #include "log.h" 25*bb02e0acSNicolas Bonnefon #include "savedsearches.h" 26*bb02e0acSNicolas Bonnefon 27*bb02e0acSNicolas Bonnefon const int SavedSearches::SAVEDSEARCHES_VERSION = 1; 28*bb02e0acSNicolas Bonnefon const int SavedSearches::maxNumberOfRecentSearches = 50; 29*bb02e0acSNicolas Bonnefon 30*bb02e0acSNicolas Bonnefon SavedSearches::SavedSearches() : savedSearches_() 31*bb02e0acSNicolas Bonnefon { 32*bb02e0acSNicolas Bonnefon qRegisterMetaTypeStreamOperators<SavedSearches>( "SavedSearches" ); 33*bb02e0acSNicolas Bonnefon } 34*bb02e0acSNicolas Bonnefon 35*bb02e0acSNicolas Bonnefon void SavedSearches::addRecent( const QString& text ) 36*bb02e0acSNicolas Bonnefon { 37*bb02e0acSNicolas Bonnefon // We're not interested in blank lines 38*bb02e0acSNicolas Bonnefon if ( text.isEmpty() ) 39*bb02e0acSNicolas Bonnefon return; 40*bb02e0acSNicolas Bonnefon 41*bb02e0acSNicolas Bonnefon // Remove any copy of the about to be added text 42*bb02e0acSNicolas Bonnefon savedSearches_.removeAll( text ); 43*bb02e0acSNicolas Bonnefon 44*bb02e0acSNicolas Bonnefon // Add at the front 45*bb02e0acSNicolas Bonnefon savedSearches_.push_front( text ); 46*bb02e0acSNicolas Bonnefon 47*bb02e0acSNicolas Bonnefon // Trim the list if it's too long 48*bb02e0acSNicolas Bonnefon while (savedSearches_.size() > maxNumberOfRecentSearches) 49*bb02e0acSNicolas Bonnefon savedSearches_.pop_back(); 50*bb02e0acSNicolas Bonnefon } 51*bb02e0acSNicolas Bonnefon 52*bb02e0acSNicolas Bonnefon QStringList SavedSearches::recentSearches() const 53*bb02e0acSNicolas Bonnefon { 54*bb02e0acSNicolas Bonnefon return savedSearches_; 55*bb02e0acSNicolas Bonnefon } 56*bb02e0acSNicolas Bonnefon 57*bb02e0acSNicolas Bonnefon // 58*bb02e0acSNicolas Bonnefon // Operators for serialization 59*bb02e0acSNicolas Bonnefon // 60*bb02e0acSNicolas Bonnefon 61*bb02e0acSNicolas Bonnefon QDataStream& operator<<( QDataStream& out, const SavedSearches& object ) 62*bb02e0acSNicolas Bonnefon { 63*bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "<<operator from SavedSearches"; 64*bb02e0acSNicolas Bonnefon 65*bb02e0acSNicolas Bonnefon out << object.savedSearches_; 66*bb02e0acSNicolas Bonnefon 67*bb02e0acSNicolas Bonnefon return out; 68*bb02e0acSNicolas Bonnefon } 69*bb02e0acSNicolas Bonnefon 70*bb02e0acSNicolas Bonnefon QDataStream& operator>>( QDataStream& in, SavedSearches& object ) 71*bb02e0acSNicolas Bonnefon { 72*bb02e0acSNicolas Bonnefon LOG(logDEBUG) << ">>operator from SavedSearches"; 73*bb02e0acSNicolas Bonnefon 74*bb02e0acSNicolas Bonnefon in >> object.savedSearches_; 75*bb02e0acSNicolas Bonnefon 76*bb02e0acSNicolas Bonnefon return in; 77*bb02e0acSNicolas Bonnefon } 78*bb02e0acSNicolas Bonnefon 79*bb02e0acSNicolas Bonnefon // 80*bb02e0acSNicolas Bonnefon // Persistable virtual functions implementation 81*bb02e0acSNicolas Bonnefon // 82*bb02e0acSNicolas Bonnefon 83*bb02e0acSNicolas Bonnefon void SavedSearches::saveToStorage( QSettings& settings ) const 84*bb02e0acSNicolas Bonnefon { 85*bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "SavedSearches::saveToStorage"; 86*bb02e0acSNicolas Bonnefon 87*bb02e0acSNicolas Bonnefon settings.beginGroup( "SavedSearches" ); 88*bb02e0acSNicolas Bonnefon // Remove everything in case the array is shorter than the previous one 89*bb02e0acSNicolas Bonnefon settings.remove(""); 90*bb02e0acSNicolas Bonnefon settings.setValue( "version", SAVEDSEARCHES_VERSION ); 91*bb02e0acSNicolas Bonnefon settings.beginWriteArray( "searchHistory" ); 92*bb02e0acSNicolas Bonnefon for (int i = 0; i < savedSearches_.size(); ++i) { 93*bb02e0acSNicolas Bonnefon settings.setArrayIndex( i ); 94*bb02e0acSNicolas Bonnefon settings.setValue( "string", savedSearches_.at( i ) ); 95*bb02e0acSNicolas Bonnefon } 96*bb02e0acSNicolas Bonnefon settings.endArray(); 97*bb02e0acSNicolas Bonnefon settings.endGroup(); 98*bb02e0acSNicolas Bonnefon } 99*bb02e0acSNicolas Bonnefon 100*bb02e0acSNicolas Bonnefon void SavedSearches::retrieveFromStorage( QSettings& settings ) 101*bb02e0acSNicolas Bonnefon { 102*bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "SavedSearches::retrieveFromStorage"; 103*bb02e0acSNicolas Bonnefon 104*bb02e0acSNicolas Bonnefon savedSearches_.clear(); 105*bb02e0acSNicolas Bonnefon 106*bb02e0acSNicolas Bonnefon if ( settings.contains( "SavedSearches/version" ) ) { 107*bb02e0acSNicolas Bonnefon // Unserialise the "new style" stored history 108*bb02e0acSNicolas Bonnefon settings.beginGroup( "SavedSearches" ); 109*bb02e0acSNicolas Bonnefon if ( settings.value( "version" ) == SAVEDSEARCHES_VERSION ) { 110*bb02e0acSNicolas Bonnefon int size = settings.beginReadArray( "searchHistory" ); 111*bb02e0acSNicolas Bonnefon for (int i = 0; i < size; ++i) { 112*bb02e0acSNicolas Bonnefon settings.setArrayIndex(i); 113*bb02e0acSNicolas Bonnefon QString search = settings.value( "string" ).toString(); 114*bb02e0acSNicolas Bonnefon savedSearches_.append( search ); 115*bb02e0acSNicolas Bonnefon } 116*bb02e0acSNicolas Bonnefon settings.endArray(); 117*bb02e0acSNicolas Bonnefon } 118*bb02e0acSNicolas Bonnefon else { 119*bb02e0acSNicolas Bonnefon LOG(logERROR) << "Unknown version of FilterSet, ignoring it..."; 120*bb02e0acSNicolas Bonnefon } 121*bb02e0acSNicolas Bonnefon settings.endGroup(); 122*bb02e0acSNicolas Bonnefon } 123*bb02e0acSNicolas Bonnefon else { 124*bb02e0acSNicolas Bonnefon LOG(logWARNING) << "Trying to import legacy (<=0.8.2) saved searches..."; 125*bb02e0acSNicolas Bonnefon SavedSearches tmp_saved_searches = 126*bb02e0acSNicolas Bonnefon settings.value( "savedSearches" ).value<SavedSearches>(); 127*bb02e0acSNicolas Bonnefon *this = tmp_saved_searches; 128*bb02e0acSNicolas Bonnefon LOG(logWARNING) << "...imported searches: " 129*bb02e0acSNicolas Bonnefon << savedSearches_.count() << " elements"; 130*bb02e0acSNicolas Bonnefon // Remove the old key once migration is done 131*bb02e0acSNicolas Bonnefon settings.remove( "savedSearches" ); 132*bb02e0acSNicolas Bonnefon // And replace it with the new one 133*bb02e0acSNicolas Bonnefon saveToStorage( settings ); 134*bb02e0acSNicolas Bonnefon settings.sync(); 135*bb02e0acSNicolas Bonnefon } 136*bb02e0acSNicolas Bonnefon } 137