1 /* 2 * Copyright (C) 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 RecentFiles 21 22 #include <QSettings> 23 #include <QFile> 24 25 #include "log.h" 26 #include "recentfiles.h" 27 28 const int RecentFiles::RECENTFILES_VERSION = 1; 29 const int RecentFiles::MAX_NUMBER_OF_FILES = 10; 30 31 RecentFiles::RecentFiles() : recentFiles_() 32 { 33 } 34 35 void RecentFiles::addRecent( const QString& text ) 36 { 37 // First prune non existent files 38 QMutableStringListIterator i(recentFiles_); 39 while ( i.hasNext() ) { 40 if ( !QFile::exists(i.next()) ) 41 i.remove(); 42 } 43 44 // Remove any copy of the about to be added filename 45 recentFiles_.removeAll( text ); 46 47 // Add at the front 48 recentFiles_.push_front( text ); 49 50 // Trim the list if it's too long 51 while ( recentFiles_.size() > MAX_NUMBER_OF_FILES ) 52 recentFiles_.pop_back(); 53 } 54 55 QStringList RecentFiles::recentFiles() const 56 { 57 return recentFiles_; 58 } 59 60 // 61 // Persistable virtual functions implementation 62 // 63 64 void RecentFiles::saveToStorage( QSettings& settings ) const 65 { 66 LOG(logDEBUG) << "RecentFiles::saveToStorage"; 67 68 settings.beginGroup( "RecentFiles" ); 69 settings.setValue( "version", RECENTFILES_VERSION ); 70 settings.beginWriteArray( "filesHistory" ); 71 for (int i = 0; i < recentFiles_.size(); ++i) { 72 settings.setArrayIndex( i ); 73 settings.setValue( "name", recentFiles_.at( i ) ); 74 } 75 settings.endArray(); 76 settings.endGroup(); 77 } 78 79 void RecentFiles::retrieveFromStorage( QSettings& settings ) 80 { 81 LOG(logDEBUG) << "RecentFiles::retrieveFromStorage"; 82 83 recentFiles_.clear(); 84 85 if ( settings.contains( "RecentFiles/version" ) ) { 86 // Unserialise the "new style" stored history 87 settings.beginGroup( "RecentFiles" ); 88 if ( settings.value( "version" ) == RECENTFILES_VERSION ) { 89 int size = settings.beginReadArray( "filesHistory" ); 90 for (int i = 0; i < size; ++i) { 91 settings.setArrayIndex(i); 92 QString search = settings.value( "name" ).toString(); 93 recentFiles_.append( search ); 94 } 95 settings.endArray(); 96 } 97 else { 98 LOG(logERROR) << "Unknown version of FilterSet, ignoring it..."; 99 } 100 settings.endGroup(); 101 } 102 } 103