~dmxe/lifeograph/1.0

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
/***********************************************************************************

    Copyright (C) 2010 Ahmet Öztürk (aoz_2@yahoo.com)

    This file is part of Lifeograph.

    Lifeograph 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.

    Lifeograph 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 Lifeograph.  If not, see <http://www.gnu.org/licenses/>.

***********************************************************************************/


#include "settings.hpp"

#include <fstream>
#include <glib/gstdio.h>    // g_mkdir()


using namespace LIFEO;


Settings::Settings( void )
:   autologout( true ), idletime( IDLETIME_DEFAULT ), show_formatting_toolbar( true ),
    diary_extension( EXTENSION_DEFAULT ), flag_maiden_voyage( false ),
    width( WIDTH_DEFAULT ), height( HEIGHT_DEFAULT ),
    state_maximized( false ), position_x( POSITION_NOTSET ), position_y( POSITION_NOTSET ),
    position_pane( PANEPOS_DEFAULT ), position_pane_tags( PANEPOS_TAGS_DEFAULT )
{
}

bool
Settings::read( void )
{
    m_path = Glib::getenv( "LIFEOGRAPH_CONFIG_FILE" );
    if( m_path.empty() )
    {
        flag_maiden_voyage = true; // maiden voyage is not used when custom config path is given
        m_path = Glib::build_filename( Glib::get_user_config_dir(), "lifeograph", "lifeo.conf" );
    }

    std::ifstream file( m_path.c_str() );

    if( ! file.is_open() )
    {
        PRINT_DEBUG( "Configuration file does not exist!" );
        return false;
    }
    else
        flag_maiden_voyage = false;

    reset();

    std::string line;

    while( getline( file, line ) )
    {
        if( line.size() < 3 )
            continue;

        switch( line[ 0 ] )
        {
            case 'R':
                recentfiles.insert( line.substr( 2 ) );
                break;
            // TODO: position & size settings may go to the diary
            // TODO: check if these are within screen boundaries
            case 'H':
                height = convert_string( line.substr( 2 ) );
                break;
            case 'W':
                width = convert_string( line.substr( 2 ) );
                break;
            case 'S':
                state_maximized = ( line[ 2 ] == 'm' || line[ 2 ] == 'M' );
                break;
            case 'X':
                position_x = convert_string( line.substr( 2 ) );
                break;
            case 'Y':
                position_y = convert_string( line.substr( 2 ) );
                break;
            case 'P':   // uppercase
                position_pane = convert_string( line.substr( 2 ) );
                break;
            case 'p':   // lowercase
                position_pane_tags =  convert_string( line.substr( 2 ) );
                // zero width is not accepted by Gtk::Paned
                if( position_pane_tags == 0 )
                    position_pane_tags = 1;
                break;
            case 'L':
                autologout = ( line[ 2 ] == 'y' || line[ 2 ] == 'Y' );
                break;
            case 'I':
                idletime = convert_string( line.substr( 2 ) );
                if( idletime < IDLETIME_MIN || idletime > IDLETIME_MAX )
                    idletime = IDLETIME_DEFAULT;
                break;
            case 'F':
                show_formatting_toolbar = ( line[ 2 ] == 'y' || line[ 2 ] == 'Y' );
                break;
            case 'E':
                // if only a dot (.) no extension will be added automatically
                diary_extension = line.substr( 2 );
                if( diary_extension[ 0 ] != '.' )
                    diary_extension.insert( 0, "." );
                break;

            case 0:     // empty line
            case '#':   // comment
                break;
            default:
                print_info( "unrecognized option:\n" + line );
                break;
        }
    }

    file.close();
    return true;
}

bool
Settings::write( void )
{
    if( ! Glib::file_test( m_path, Glib::FILE_TEST_EXISTS ) )
    {
        if( ! Glib::file_test( Glib::path_get_dirname( m_path ), Glib::FILE_TEST_IS_DIR ) )
            g_mkdir_with_parents( Glib::path_get_dirname( m_path ).c_str(), 0700 );
    }

    std::ofstream file( m_path.c_str(), std::ios::out | std::ios::trunc );

    if( ! file.is_open() )
    {
        print_error( "Failed to save configuration!" );
        return false;
    }

    file << "W " << width;
    file << "\nH " << height;
    file << "\nX " << position_x;
    file << "\nY " << position_y;
    file << "\nS " << ( state_maximized ? 'm' : 'u' );
    file << "\nP " << position_pane;
    file << "\np " << position_pane_tags;

    // RECENT FILES
    for( ListPaths::iterator iter = recentfiles.begin();
         iter != recentfiles.end(); ++iter )
        file << "\nR " << *iter;

    file << "\nL " << ( autologout ? 'y' : 'n' );
    file << "\nI " << idletime;
    file << "\nF " << ( show_formatting_toolbar ? 'y' : 'n' );

    if( diary_extension != EXTENSION_DEFAULT )
        file << "\nE " << diary_extension;

    file.close();
    return true;
}

void
Settings::reset( void )
{
    recentfiles.clear();
    autologout = true;
    idletime = IDLETIME_DEFAULT;
    show_formatting_toolbar = true;
    diary_extension = EXTENSION_DEFAULT;
}

// maybe later:
//bool
//Settings::get_bool_opt( int i )
//{
//    if( i >= m_bool_opts.size() )
//        throw LIFEO::Error( "Requested option is out of bounds" );
//    else
//        return m_bool_opts[ i ];
//}