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 // Implements PersistentInfo, a singleton class which store/retrieve objects 21*bb02e0acSNicolas Bonnefon // to persistent storage. 22*bb02e0acSNicolas Bonnefon 23*bb02e0acSNicolas Bonnefon #include "persistentinfo.h" 24*bb02e0acSNicolas Bonnefon 25*bb02e0acSNicolas Bonnefon #include <cassert> 26*bb02e0acSNicolas Bonnefon #include <QStringList> 27*bb02e0acSNicolas Bonnefon 28*bb02e0acSNicolas Bonnefon #include "log.h" 29*bb02e0acSNicolas Bonnefon #include "persistable.h" 30*bb02e0acSNicolas Bonnefon 31*bb02e0acSNicolas Bonnefon PersistentInfo::PersistentInfo() 32*bb02e0acSNicolas Bonnefon { 33*bb02e0acSNicolas Bonnefon settings_ = NULL; 34*bb02e0acSNicolas Bonnefon initialised_ = false; 35*bb02e0acSNicolas Bonnefon } 36*bb02e0acSNicolas Bonnefon 37*bb02e0acSNicolas Bonnefon PersistentInfo::~PersistentInfo() 38*bb02e0acSNicolas Bonnefon { 39*bb02e0acSNicolas Bonnefon if ( initialised_ ) 40*bb02e0acSNicolas Bonnefon delete settings_; 41*bb02e0acSNicolas Bonnefon } 42*bb02e0acSNicolas Bonnefon 43*bb02e0acSNicolas Bonnefon void PersistentInfo::migrateAndInit() 44*bb02e0acSNicolas Bonnefon { 45*bb02e0acSNicolas Bonnefon assert( initialised_ == false ); 46*bb02e0acSNicolas Bonnefon #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) 47*bb02e0acSNicolas Bonnefon // On Windows, we use .ini files and import from the registry if no 48*bb02e0acSNicolas Bonnefon // .ini file is found (glogg <= 0.9 used the registry). 49*bb02e0acSNicolas Bonnefon 50*bb02e0acSNicolas Bonnefon // This store the config file in %appdata% 51*bb02e0acSNicolas Bonnefon settings_ = new QSettings( QSettings::IniFormat, 52*bb02e0acSNicolas Bonnefon QSettings::UserScope, "glogg", "glogg" ); 53*bb02e0acSNicolas Bonnefon 54*bb02e0acSNicolas Bonnefon if ( settings_->childKeys().count() == 0 ) { 55*bb02e0acSNicolas Bonnefon LOG(logWARNING) << "INI file empty, trying to import from registry"; 56*bb02e0acSNicolas Bonnefon QSettings registry( "glogg", "glogg" ); 57*bb02e0acSNicolas Bonnefon foreach ( QString key, registry.allKeys() ) { 58*bb02e0acSNicolas Bonnefon settings_->setValue( key, registry.value( key ) ); 59*bb02e0acSNicolas Bonnefon } 60*bb02e0acSNicolas Bonnefon } 61*bb02e0acSNicolas Bonnefon #else 62*bb02e0acSNicolas Bonnefon // We use default Qt storage on proper OSes 63*bb02e0acSNicolas Bonnefon settings_ = new QSettings( "glogg", "glogg" ); 64*bb02e0acSNicolas Bonnefon #endif 65*bb02e0acSNicolas Bonnefon initialised_ = true; 66*bb02e0acSNicolas Bonnefon } 67*bb02e0acSNicolas Bonnefon 68*bb02e0acSNicolas Bonnefon void PersistentInfo::registerPersistable( Persistable* object, 69*bb02e0acSNicolas Bonnefon const QString& name ) 70*bb02e0acSNicolas Bonnefon { 71*bb02e0acSNicolas Bonnefon assert( initialised_ ); 72*bb02e0acSNicolas Bonnefon 73*bb02e0acSNicolas Bonnefon objectList_.insert( name, object ); 74*bb02e0acSNicolas Bonnefon } 75*bb02e0acSNicolas Bonnefon 76*bb02e0acSNicolas Bonnefon Persistable* PersistentInfo::getPersistable( const QString& name ) 77*bb02e0acSNicolas Bonnefon { 78*bb02e0acSNicolas Bonnefon assert( initialised_ ); 79*bb02e0acSNicolas Bonnefon 80*bb02e0acSNicolas Bonnefon Persistable* object = objectList_.value( name, NULL ); 81*bb02e0acSNicolas Bonnefon 82*bb02e0acSNicolas Bonnefon return object; 83*bb02e0acSNicolas Bonnefon } 84*bb02e0acSNicolas Bonnefon 85*bb02e0acSNicolas Bonnefon void PersistentInfo::save( const QString& name ) 86*bb02e0acSNicolas Bonnefon { 87*bb02e0acSNicolas Bonnefon assert( initialised_ ); 88*bb02e0acSNicolas Bonnefon 89*bb02e0acSNicolas Bonnefon if ( objectList_.contains( name ) ) 90*bb02e0acSNicolas Bonnefon objectList_.value( name )->saveToStorage( *settings_ ); 91*bb02e0acSNicolas Bonnefon else 92*bb02e0acSNicolas Bonnefon LOG(logERROR) << "Unregistered persistable " << name.toStdString(); 93*bb02e0acSNicolas Bonnefon 94*bb02e0acSNicolas Bonnefon // Sync to ensure it is propagated to other processes 95*bb02e0acSNicolas Bonnefon settings_->sync(); 96*bb02e0acSNicolas Bonnefon } 97*bb02e0acSNicolas Bonnefon 98*bb02e0acSNicolas Bonnefon void PersistentInfo::retrieve( const QString& name ) 99*bb02e0acSNicolas Bonnefon { 100*bb02e0acSNicolas Bonnefon assert( initialised_ ); 101*bb02e0acSNicolas Bonnefon 102*bb02e0acSNicolas Bonnefon // Sync to ensure it has been propagated from other processes 103*bb02e0acSNicolas Bonnefon settings_->sync(); 104*bb02e0acSNicolas Bonnefon 105*bb02e0acSNicolas Bonnefon if ( objectList_.contains( name ) ) 106*bb02e0acSNicolas Bonnefon objectList_.value( name )->retrieveFromStorage( *settings_ ); 107*bb02e0acSNicolas Bonnefon else 108*bb02e0acSNicolas Bonnefon LOG(logERROR) << "Unregistered persistable " << name.toStdString(); 109*bb02e0acSNicolas Bonnefon } 110*bb02e0acSNicolas Bonnefon 111*bb02e0acSNicolas Bonnefon // Friend function to construct/get the singleton 112*bb02e0acSNicolas Bonnefon PersistentInfo& GetPersistentInfo() 113*bb02e0acSNicolas Bonnefon { 114*bb02e0acSNicolas Bonnefon static PersistentInfo pInfo; 115*bb02e0acSNicolas Bonnefon return pInfo; 116*bb02e0acSNicolas Bonnefon } 117*bb02e0acSNicolas Bonnefon 118