File: configurationdata.cpp

package info (click to toggle)
fraqtive 0.4.3-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 988 kB
  • ctags: 1,249
  • sloc: cpp: 8,387; sh: 103; makefile: 45
file content (195 lines) | stat: -rw-r--r-- 5,002 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**************************************************************************
* This file is part of the Fraqtive program
* Copyright (C) 2004-2008 Michał Męciński
*
* This program 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 2 of the License, or
* (at your option) any later version.
**************************************************************************/

#include "configurationdata.h"

#if defined( Q_WS_WIN )
#ifndef _WIN32_IE
#define _WIN32_IE 0x0400
#endif
#include <shlobj.h>
#endif

#include <QDir>
#include <cstdlib>

ConfigurationData::ConfigurationData()
{
    QString homePath = QDir::homePath();

#if defined( Q_WS_WIN )
    QT_WA( {
        TCHAR appDataPath[ MAX_PATH ];
        if ( SHGetSpecialFolderPath( 0, appDataPath, CSIDL_APPDATA, FALSE ) )
            m_dataPath = QDir::fromNativeSeparators( QString::fromUtf16( (ushort*)appDataPath ) );
    } , {
        char appDataPath[ MAX_PATH ];
        if ( SHGetSpecialFolderPathA( 0, appDataPath, CSIDL_APPDATA, FALSE ) )
            m_dataPath = QDir::fromNativeSeparators( QString::fromLocal8Bit( appDataPath ) );
    } );
    if ( m_dataPath.isEmpty() )
        m_dataPath = homePath;
    m_dataPath += "/Fraqtive";
#else
    char* dataHome = getenv( "XDG_DATA_HOME" );
    if ( dataHome ) {
        if ( dataHome[ 0 ] == '/' )
            m_dataPath = dataHome;
        else
            m_dataPath = homePath + '/' + dataHome;
    } else {
        m_dataPath = homePath + "/.local/share";
    }
    m_dataPath += "/fraqtive";
#endif
}

ConfigurationData::~ConfigurationData()
{
}

bool ConfigurationData::contains( const QString& key ) const
{
    return m_data.contains( key );
}

void ConfigurationData::setValue( const QString& key, const QVariant& value )
{
    m_data[ key ] = value;
}

QVariant ConfigurationData::value( const QString& key, const QVariant& defaultValue /*= QVariant()*/ ) const
{
    QVariantMap::const_iterator it = m_data.find( key );
    if ( it == m_data.end() )
        return defaultValue;
    return it.value();
}

void ConfigurationData::readConfiguration()
{
    QFile file;
    QDataStream stream;

    if ( readFile( &file, &stream, "config.dat" ) )
        stream >> m_data;

    if ( readFile( &file, &stream, "bookmarks.dat" ) )
        stream >> m_bookmarks;

    if ( readFile( &file, &stream, "presets.dat" ) )
        stream >> m_userPresets;

    if ( readFile( &file, &stream, ":/data/presets.dat" ) )
        stream >> m_defaultPresets;
}

bool ConfigurationData::readFile( QFile* file, QDataStream* stream, const QString& fileName )
{
    QString path;
    if ( QDir::isRelativePath( fileName ) )
        path = locateDataFile( fileName );
    else
        path = fileName;

    file->close();
    file->setFileName( path );

    if ( !file->open( QIODevice::ReadOnly ) )
        return false;

    stream->setDevice( file );
    stream->setVersion( QDataStream::Qt_4_2 );

    qint32 version;
    *stream >> version;

    if ( version < 1 || version > 2 )
        return false;

    m_dataVersion = version;

    return true;
}

void ConfigurationData::writeConfiguration()
{
    QFile file;
    QDataStream stream;

    if ( writeFile( &file, &stream, "config.dat" ) )
        stream << m_data;

    if ( writeFile( &file, &stream, "bookmarks.dat" ) )
        stream << m_bookmarks;

    if ( writeFile( &file, &stream, "presets.dat" ) )
        stream << m_userPresets;
}

bool ConfigurationData::writeFile( QFile* file, QDataStream* stream, const QString& fileName )
{
    QString path;
    if ( QDir::isRelativePath( fileName ) )
        path = locateDataFile( fileName );
    else
        path = fileName;

    file->close();
    file->setFileName( path );

    if ( !file->open( QIODevice::WriteOnly ) )
        return false;

    stream->setDevice( file );
    stream->setVersion( QDataStream::Qt_4_2 );

    // increment version when adding / modifying fields
    m_dataVersion = 2;

    *stream << (qint32)m_dataVersion;

    return true;
}

QString ConfigurationData::locateDataFile( const QString& name )
{
    QString path = m_dataPath + '/' + name;

    checkAccess( path );

    return path;
}

bool ConfigurationData::checkAccess( const QString& path )
{
    QFileInfo fileInfo( path );

    if ( fileInfo.exists() )
        return fileInfo.isReadable();

    QDir dir = QDir::root();

    QStringList pathParts = path.split( '/', QString::SkipEmptyParts );

    for ( int i = 0; i < pathParts.size() - 1; i++ ) {
        const QString& part = pathParts.at( i );

        if ( dir.cd( part ) )
            continue;

        if ( dir.mkdir( part ) && dir.cd( part ) )
            continue;

        return false;
    }

    return true;
}