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