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