1*bb02e0acSNicolas Bonnefon /* 2*bb02e0acSNicolas Bonnefon * Copyright (C) 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 #ifndef PERSISTENTINFO_H 21*bb02e0acSNicolas Bonnefon #define PERSISTENTINFO_H 22*bb02e0acSNicolas Bonnefon 23*bb02e0acSNicolas Bonnefon #include <QSettings> 24*bb02e0acSNicolas Bonnefon #include <QHash> 25*bb02e0acSNicolas Bonnefon 26*bb02e0acSNicolas Bonnefon class Persistable; 27*bb02e0acSNicolas Bonnefon 28*bb02e0acSNicolas Bonnefon // Singleton class managing the saving of persistent data to permanent storage 29*bb02e0acSNicolas Bonnefon // Clients must implement Persistable and register with this object, they can 30*bb02e0acSNicolas Bonnefon // then be saved/loaded. 31*bb02e0acSNicolas Bonnefon class PersistentInfo { 32*bb02e0acSNicolas Bonnefon public: 33*bb02e0acSNicolas Bonnefon // Initialise the storage backend for the Persistable, migrating the settings 34*bb02e0acSNicolas Bonnefon // if needed. Must be called before any other function. 35*bb02e0acSNicolas Bonnefon void migrateAndInit(); 36*bb02e0acSNicolas Bonnefon // Register a Persistable 37*bb02e0acSNicolas Bonnefon void registerPersistable( Persistable* object, const QString& name ); 38*bb02e0acSNicolas Bonnefon // Get a Persistable (or NULL if it doesn't exist) 39*bb02e0acSNicolas Bonnefon Persistable* getPersistable( const QString& name ); 40*bb02e0acSNicolas Bonnefon // Save a persistable to its permanent storage 41*bb02e0acSNicolas Bonnefon void save( const QString& name ); 42*bb02e0acSNicolas Bonnefon // Retrieve a persistable from permanent storage 43*bb02e0acSNicolas Bonnefon void retrieve( const QString& name ); 44*bb02e0acSNicolas Bonnefon 45*bb02e0acSNicolas Bonnefon private: 46*bb02e0acSNicolas Bonnefon // Can't be constructed or copied (singleton) 47*bb02e0acSNicolas Bonnefon PersistentInfo(); 48*bb02e0acSNicolas Bonnefon PersistentInfo( const PersistentInfo& ); 49*bb02e0acSNicolas Bonnefon ~PersistentInfo(); 50*bb02e0acSNicolas Bonnefon 51*bb02e0acSNicolas Bonnefon // Has migrateAndInit() been called? 52*bb02e0acSNicolas Bonnefon bool initialised_; 53*bb02e0acSNicolas Bonnefon 54*bb02e0acSNicolas Bonnefon // List of persistables 55*bb02e0acSNicolas Bonnefon QHash<QString, Persistable*> objectList_; 56*bb02e0acSNicolas Bonnefon 57*bb02e0acSNicolas Bonnefon // Qt setting object 58*bb02e0acSNicolas Bonnefon QSettings* settings_; 59*bb02e0acSNicolas Bonnefon 60*bb02e0acSNicolas Bonnefon // allow this function to create one instance 61*bb02e0acSNicolas Bonnefon friend PersistentInfo& GetPersistentInfo(); 62*bb02e0acSNicolas Bonnefon }; 63*bb02e0acSNicolas Bonnefon 64*bb02e0acSNicolas Bonnefon PersistentInfo& GetPersistentInfo(); 65*bb02e0acSNicolas Bonnefon 66*bb02e0acSNicolas Bonnefon // Global function used to get an object from the PersistentInfo store 67*bb02e0acSNicolas Bonnefon template<typename T> 68*bb02e0acSNicolas Bonnefon T& Persistent( const char* name ) 69*bb02e0acSNicolas Bonnefon { 70*bb02e0acSNicolas Bonnefon Persistable* p = GetPersistentInfo().getPersistable( QString( name ) ); 71*bb02e0acSNicolas Bonnefon return dynamic_cast<T&>(*p); 72*bb02e0acSNicolas Bonnefon } 73*bb02e0acSNicolas Bonnefon 74*bb02e0acSNicolas Bonnefon #endif 75