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