~dmxe/lifeograph/1.0

489.1.1 by Ahmet Öztürk
removed gconf dependency; implemented a very basic file based configuration storage system
1
/***********************************************************************************
2
1587 by Ahmet Öztürk
code cleanup: tab to space transition
3
    Copyright (C) 2010 Ahmet Öztürk (aoz_2@yahoo.com)
4
5
    This file is part of Lifeograph.
6
7
    Lifeograph is free software: you can redistribute it and/or modify
8
    it under the terms of the GNU General Public License as published by
9
    the Free Software Foundation, either version 3 of the License, or
10
    (at your option) any later version.
11
12
    Lifeograph is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
    GNU General Public License for more details.
16
17
    You should have received a copy of the GNU General Public License
18
    along with Lifeograph.  If not, see <http://www.gnu.org/licenses/>.
489.1.1 by Ahmet Öztürk
removed gconf dependency; implemented a very basic file based configuration storage system
19
20
***********************************************************************************/
21
22
558 by Ahmet Öztürk
renamed Config class to Settings to avoid confusion
23
#include "settings.hpp"
489.1.1 by Ahmet Öztürk
removed gconf dependency; implemented a very basic file based configuration storage system
24
25
#include <fstream>
1587 by Ahmet Öztürk
code cleanup: tab to space transition
26
#include <glib/gstdio.h>    // g_mkdir()
489.1.1 by Ahmet Öztürk
removed gconf dependency; implemented a very basic file based configuration storage system
27
28
29
using namespace LIFEO;
30
31
558 by Ahmet Öztürk
renamed Config class to Settings to avoid confusion
32
Settings::Settings( void )
1473 by Ahmet Öztürk
preserve maximized state of the window
33
:   autologout( true ), idletime( IDLETIME_DEFAULT ), show_formatting_toolbar( true ),
1638 by Ahmet Öztürk
cleaned up first run handling; now config files specified through env variables are created if they do not exist; default window height set to 300
34
    diary_extension( EXTENSION_DEFAULT ), flag_maiden_voyage( false ),
35
    width( WIDTH_DEFAULT ), height( HEIGHT_DEFAULT ),
1473 by Ahmet Öztürk
preserve maximized state of the window
36
    state_maximized( false ), position_x( POSITION_NOTSET ), position_y( POSITION_NOTSET ),
37
    position_pane( PANEPOS_DEFAULT ), position_pane_tags( PANEPOS_TAGS_DEFAULT )
489.1.1 by Ahmet Öztürk
removed gconf dependency; implemented a very basic file based configuration storage system
38
{
39
}
40
41
bool
558 by Ahmet Öztürk
renamed Config class to Settings to avoid confusion
42
Settings::read( void )
489.1.1 by Ahmet Öztürk
removed gconf dependency; implemented a very basic file based configuration storage system
43
{
1503 by Ahmet Öztürk
added support for using multiple configuration files via setting LIFEOGRAPH_CONFIG_FILE variable in the environment
44
    m_path = Glib::getenv( "LIFEOGRAPH_CONFIG_FILE" );
45
    if( m_path.empty() )
1638 by Ahmet Öztürk
cleaned up first run handling; now config files specified through env variables are created if they do not exist; default window height set to 300
46
    {
47
        flag_maiden_voyage = true; // maiden voyage is not used when custom config path is given
48
        m_path = Glib::build_filename( Glib::get_user_config_dir(), "lifeograph", "lifeo.conf" );
49
    }
1503 by Ahmet Öztürk
added support for using multiple configuration files via setting LIFEOGRAPH_CONFIG_FILE variable in the environment
50
51
    std::ifstream file( m_path.c_str() );
52
53
    if( ! file.is_open() )
54
    {
1638 by Ahmet Öztürk
cleaned up first run handling; now config files specified through env variables are created if they do not exist; default window height set to 300
55
        PRINT_DEBUG( "Configuration file does not exist!" );
1503 by Ahmet Öztürk
added support for using multiple configuration files via setting LIFEOGRAPH_CONFIG_FILE variable in the environment
56
        return false;
57
    }
1638 by Ahmet Öztürk
cleaned up first run handling; now config files specified through env variables are created if they do not exist; default window height set to 300
58
    else
59
        flag_maiden_voyage = false;
1204 by Ahmet Öztürk
add .diary extension to the new diary files. a different extension can be selected or this feature can be disabled via a hidden option in config file
60
1587 by Ahmet Öztürk
code cleanup: tab to space transition
61
    reset();
62
63
    std::string line;
64
65
    while( getline( file, line ) )
66
    {
67
        if( line.size() < 3 )
68
            continue;
69
70
        switch( line[ 0 ] )
71
        {
72
            case 'R':
73
                recentfiles.insert( line.substr( 2 ) );
74
                break;
75
            // TODO: position & size settings may go to the diary
76
            // TODO: check if these are within screen boundaries
77
            case 'H':
78
                height = convert_string( line.substr( 2 ) );
79
                break;
80
            case 'W':
81
                width = convert_string( line.substr( 2 ) );
82
                break;
1473 by Ahmet Öztürk
preserve maximized state of the window
83
            case 'S':
84
                state_maximized = ( line[ 2 ] == 'm' || line[ 2 ] == 'M' );
85
                break;
1587 by Ahmet Öztürk
code cleanup: tab to space transition
86
            case 'X':
87
                position_x = convert_string( line.substr( 2 ) );
88
                break;
89
            case 'Y':
90
                position_y = convert_string( line.substr( 2 ) );
91
                break;
92
            case 'P':   // uppercase
93
                position_pane = convert_string( line.substr( 2 ) );
94
                break;
95
            case 'p':   // lowercase
96
                position_pane_tags =  convert_string( line.substr( 2 ) );
97
                // zero width is not accepted by Gtk::Paned
98
                if( position_pane_tags == 0 )
99
                    position_pane_tags = 1;
100
                break;
101
            case 'L':
102
                autologout = ( line[ 2 ] == 'y' || line[ 2 ] == 'Y' );
103
                break;
104
            case 'I':
105
                idletime = convert_string( line.substr( 2 ) );
106
                if( idletime < IDLETIME_MIN || idletime > IDLETIME_MAX )
107
                    idletime = IDLETIME_DEFAULT;
108
                break;
109
            case 'F':
110
                show_formatting_toolbar = ( line[ 2 ] == 'y' || line[ 2 ] == 'Y' );
111
                break;
112
            case 'E':
113
                // if only a dot (.) no extension will be added automatically
1204 by Ahmet Öztürk
add .diary extension to the new diary files. a different extension can be selected or this feature can be disabled via a hidden option in config file
114
                diary_extension = line.substr( 2 );
115
                if( diary_extension[ 0 ] != '.' )
116
                    diary_extension.insert( 0, "." );
117
                break;
489.1.3 by Ahmet Öztürk
removed configuration variables in Lifeograph and FlebuttonRecent and had them use Config class members
118
1587 by Ahmet Öztürk
code cleanup: tab to space transition
119
            case 0:     // empty line
120
            case '#':   // comment
121
                break;
122
            default:
123
                print_info( "unrecognized option:\n" + line );
124
                break;
125
        }
126
    }
489.1.1 by Ahmet Öztürk
removed gconf dependency; implemented a very basic file based configuration storage system
127
1587 by Ahmet Öztürk
code cleanup: tab to space transition
128
    file.close();
129
    return true;
489.1.1 by Ahmet Öztürk
removed gconf dependency; implemented a very basic file based configuration storage system
130
}
131
132
bool
558 by Ahmet Öztürk
renamed Config class to Settings to avoid confusion
133
Settings::write( void )
489.1.1 by Ahmet Öztürk
removed gconf dependency; implemented a very basic file based configuration storage system
134
{
1638 by Ahmet Öztürk
cleaned up first run handling; now config files specified through env variables are created if they do not exist; default window height set to 300
135
    if( ! Glib::file_test( m_path, Glib::FILE_TEST_EXISTS ) )
1503 by Ahmet Öztürk
added support for using multiple configuration files via setting LIFEOGRAPH_CONFIG_FILE variable in the environment
136
    {
1638 by Ahmet Öztürk
cleaned up first run handling; now config files specified through env variables are created if they do not exist; default window height set to 300
137
        if( ! Glib::file_test( Glib::path_get_dirname( m_path ), Glib::FILE_TEST_IS_DIR ) )
138
            g_mkdir_with_parents( Glib::path_get_dirname( m_path ).c_str(), 0700 );
1503 by Ahmet Öztürk
added support for using multiple configuration files via setting LIFEOGRAPH_CONFIG_FILE variable in the environment
139
    }
140
141
    std::ofstream file( m_path.c_str(), std::ios::out | std::ios::trunc );
142
143
    if( ! file.is_open() )
144
    {
145
        print_error( "Failed to save configuration!" );
146
        return false;
147
    }
489.1.1 by Ahmet Öztürk
removed gconf dependency; implemented a very basic file based configuration storage system
148
1587 by Ahmet Öztürk
code cleanup: tab to space transition
149
    file << "W " << width;
150
    file << "\nH " << height;
151
    file << "\nX " << position_x;
152
    file << "\nY " << position_y;
153
    file << "\nS " << ( state_maximized ? 'm' : 'u' );
154
    file << "\nP " << position_pane;
155
    file << "\np " << position_pane_tags;
156
157
    // RECENT FILES
158
    for( ListPaths::iterator iter = recentfiles.begin();
159
         iter != recentfiles.end(); ++iter )
160
        file << "\nR " << *iter;
161
162
    file << "\nL " << ( autologout ? 'y' : 'n' );
163
    file << "\nI " << idletime;
164
    file << "\nF " << ( show_formatting_toolbar ? 'y' : 'n' );
489.1.1 by Ahmet Öztürk
removed gconf dependency; implemented a very basic file based configuration storage system
165
1204 by Ahmet Öztürk
add .diary extension to the new diary files. a different extension can be selected or this feature can be disabled via a hidden option in config file
166
    if( diary_extension != EXTENSION_DEFAULT )
167
        file << "\nE " << diary_extension;
168
1587 by Ahmet Öztürk
code cleanup: tab to space transition
169
    file.close();
170
    return true;
489.1.1 by Ahmet Öztürk
removed gconf dependency; implemented a very basic file based configuration storage system
171
}
172
173
void
558 by Ahmet Öztürk
renamed Config class to Settings to avoid confusion
174
Settings::reset( void )
489.1.1 by Ahmet Öztürk
removed gconf dependency; implemented a very basic file based configuration storage system
175
{
1204 by Ahmet Öztürk
add .diary extension to the new diary files. a different extension can be selected or this feature can be disabled via a hidden option in config file
176
    recentfiles.clear();
177
    autologout = true;
178
    idletime = IDLETIME_DEFAULT;
179
    show_formatting_toolbar = true;
180
    diary_extension = EXTENSION_DEFAULT;
489.1.1 by Ahmet Öztürk
removed gconf dependency; implemented a very basic file based configuration storage system
181
}
182
1204 by Ahmet Öztürk
add .diary extension to the new diary files. a different extension can be selected or this feature can be disabled via a hidden option in config file
183
// maybe later:
184
//bool
185
//Settings::get_bool_opt( int i )
186
//{
187
//    if( i >= m_bool_opts.size() )
188
//        throw LIFEO::Error( "Requested option is out of bounds" );
189
//    else
190
//        return m_bool_opts[ i ];
191
//}