File: persistentinfo.cpp

package info (click to toggle)
glogg 0.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 832 kB
  • sloc: cpp: 6,649; sh: 66; perl: 32; sed: 25; makefile: 11
file content (117 lines) | stat: -rw-r--r-- 3,216 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Copyright (C) 2011 Nicolas Bonnefon and other contributors
 *
 * This file is part of glogg.
 *
 * glogg is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * glogg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with glogg.  If not, see <http://www.gnu.org/licenses/>.
 */

// Implements PersistentInfo, a singleton class which store/retrieve objects
// to persistent storage.

#include "persistentinfo.h"

#include <cassert>
#include <QStringList>

#include "log.h"
#include "persistable.h"

PersistentInfo::PersistentInfo()
{
    settings_    = NULL;
    initialised_ = false;
}

PersistentInfo::~PersistentInfo()
{
    if ( initialised_ )
        delete settings_;
}

void PersistentInfo::migrateAndInit()
{
    assert( initialised_ == false );
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
    // On Windows, we use .ini files and import from the registry if no
    // .ini file is found (glogg <= 0.9 used the registry).

    // This store the config file in %appdata%
    settings_ = new QSettings( QSettings::IniFormat,
            QSettings::UserScope, "glogg", "glogg" );

    if ( settings_->childKeys().count() == 0 ) {
        LOG(logWARNING) << "INI file empty, trying to import from registry";
        QSettings registry( "glogg", "glogg" );
        foreach ( QString key, registry.allKeys() ) {
            settings_->setValue( key, registry.value( key ) );
        }
    }
#else
    // We use default Qt storage on proper OSes
    settings_ = new QSettings( "glogg", "glogg" );
#endif
    initialised_ = true;
}

void PersistentInfo::registerPersistable( Persistable* object,
        const QString& name )
{
    assert( initialised_ );

    objectList_.insert( name, object );
}

Persistable* PersistentInfo::getPersistable( const QString& name )
{
    assert( initialised_ );

    Persistable* object = objectList_.value( name, NULL );

    return object;
}

void PersistentInfo::save( const QString& name )
{
    assert( initialised_ );

    if ( objectList_.contains( name ) )
        objectList_.value( name )->saveToStorage( *settings_ );
    else
        LOG(logERROR) << "Unregistered persistable " << name.toStdString();

    // Sync to ensure it is propagated to other processes
    settings_->sync();
}

void PersistentInfo::retrieve( const QString& name )
{
    assert( initialised_ );

    // Sync to ensure it has been propagated from other processes
    settings_->sync();

    if ( objectList_.contains( name ) )
        objectList_.value( name )->retrieveFromStorage( *settings_ );
    else
        LOG(logERROR) << "Unregistered persistable " << name.toStdString();
}

// Friend function to construct/get the singleton
PersistentInfo& GetPersistentInfo()
{
    static PersistentInfo pInfo;
    return pInfo;
}