~ubuntu-branches/ubuntu/quantal/zaz/quantal

« back to all changes in this revision

Viewing changes to src/profile.cpp

  • Committer: Package Import Robot
  • Author(s): Miriam Ruiz
  • Date: 2011-11-16 02:28:10 UTC
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: package-import@ubuntu.com-20111116022810-a7maz8kv5zfaz7p1
Tags: upstream-1.0.0~dfsg1
ImportĀ upstreamĀ versionĀ 1.0.0~dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Zaz
 
3
 * Copyright (C) Remigiusz Dybka 2009 <remigiusz.dybka@gmail.com>
 
4
 *
 
5
 Zaz is free software: you can redistribute it and/or modify it
 
6
 under the terms of the GNU General Public License as published by the
 
7
 Free Software Foundation, either version 3 of the License, or
 
8
 (at your option) any later version.
 
9
 
 
10
 Zaz is distributed in the hope that it will be useful, but
 
11
 WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
13
 See the GNU General Public License for more details.
 
14
 
 
15
 You should have received a copy of the GNU General Public License along
 
16
 with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 */
 
18
 
 
19
#include "settings.h"
 
20
#include "profile.h"
 
21
#include "common.h"
 
22
#include "error.h"
 
23
#include <cstring>
 
24
#include <cstdlib>
 
25
#include <iostream>
 
26
#include <fstream>
 
27
#include <sstream>
 
28
 
 
29
Profile profile;
 
30
 
 
31
Profile::Profile(string _name)
 
32
        : name(_name)
 
33
{
 
34
    Strip(name);
 
35
    bool migrate = false;
 
36
 
 
37
    if (name.empty())
 
38
    {
 
39
        name = getDefaultProfileName();
 
40
        migrate = true;
 
41
    }
 
42
 
 
43
    fileName = Scenes::Settings::getDefaultDirectory() + SEPARATOR + name + ".profile";
 
44
 
 
45
    ifstream phile;
 
46
#ifdef WIN32
 
47
    phile.open(Scenes::Settings::W32_GetFileName(fileName).c_str());
 
48
#else
 
49
    phile.open(fileName.c_str());
 
50
#endif
 
51
 
 
52
    if (!phile)
 
53
    {
 
54
        // if we create a new default profile, let's read in level progress
 
55
        // from the settings file
 
56
        if (migrate)
 
57
        {
 
58
            Scenes::Settings sets;
 
59
            for (int f = 1; f < 100; f++)
 
60
            {
 
61
                stringstream n;
 
62
                n << "level" << f << "_completed";
 
63
                if (sets.getb(n.str() ,false))
 
64
                {
 
65
                    n.str("");
 
66
                    n << MIGRATE_SET << ":" << f << ":completed";
 
67
                    setb(n.str(), true);
 
68
                }
 
69
            }
 
70
        }
 
71
 
 
72
        return;
 
73
    }
 
74
 
 
75
    string cfgline;
 
76
 
 
77
    while (getline(phile, cfgline))
 
78
    {
 
79
        int col = cfgline.find_last_of(":");
 
80
 
 
81
        if (col == -1)
 
82
            continue; // format error
 
83
 
 
84
        string name = cfgline.substr(0, col);
 
85
        string value = cfgline.substr(col + 1);
 
86
 
 
87
        Strip(name);
 
88
        Strip(value);
 
89
 
 
90
        cfg[name] = value;
 
91
    }
 
92
 
 
93
    phile.close();
 
94
}
 
95
 
 
96
void Profile::Save()
 
97
{
 
98
    ofstream phile;
 
99
 
 
100
#ifdef WIN32
 
101
    phile.open(Scenes::Settings::W32_CreateFile(fileName).c_str());
 
102
#else
 
103
    phile.open(fileName.c_str());
 
104
#endif
 
105
 
 
106
    if (!phile)
 
107
    {
 
108
        ERR("Could not save profile in " + fileName);
 
109
    }
 
110
 
 
111
    map<string, string>::iterator iter;
 
112
 
 
113
    for (iter = cfg.begin(); iter != cfg.end(); ++iter)
 
114
    {
 
115
        string cfgline = iter->first;
 
116
        cfgline.append(":");
 
117
        cfgline.append(iter->second);
 
118
        cfgline.append("\n");
 
119
 
 
120
        phile << cfgline;
 
121
    }
 
122
 
 
123
    phile.close();
 
124
}
 
125
 
 
126
const string Profile::get(const string name, const string defval)
 
127
{
 
128
    if (!cfg.count(name))
 
129
        cfg[name] = defval;
 
130
 
 
131
    return cfg[name];
 
132
}
 
133
 
 
134
void Profile::set(const string name, const string value)
 
135
{
 
136
    cfg[name] = value;
 
137
}
 
138
 
 
139
bool Profile::getb(const string name, bool defval)
 
140
{
 
141
    if (!cfg.count(name))
 
142
    {
 
143
        string val = defval?"TRUE":"FALSE";
 
144
        cfg[name] = val;
 
145
    }
 
146
 
 
147
    bool ret = false;
 
148
 
 
149
    char buff[1024];
 
150
    //char buff[strlen(cfg[name].c_str()) + 1];
 
151
    strcpy(buff, cfg[name].c_str());
 
152
    int s = strlen(buff);
 
153
 
 
154
    for (int f = 0; f < s; f++)
 
155
    {
 
156
        buff[f] = toupper(buff[f]);
 
157
    }
 
158
 
 
159
    if (!strcmp(buff, "TRUE"))
 
160
    {
 
161
        return true;
 
162
    }
 
163
 
 
164
    return ret;
 
165
}
 
166
 
 
167
void Profile::setb(const string name, bool value)
 
168
{
 
169
    string val = value?"TRUE":"FALSE";
 
170
    cfg[name] = val;
 
171
}
 
172
 
 
173
string Profile::getDefaultProfileName()
 
174
{
 
175
#ifndef WIN32
 
176
    char *r = getenv(ENVUSERNAME);
 
177
    string n;
 
178
    if (r)
 
179
    {
 
180
        n = r;
 
181
    }
 
182
    else
 
183
    {
 
184
        n = _("Anonymous");
 
185
    }
 
186
 
 
187
    return n;
 
188
#else
 
189
    wchar_t wu[256];
 
190
 
 
191
    DWORD n = 256;
 
192
    if (GetUserNameW(wu, &n))
 
193
    {
 
194
        char userName[n * 2];
 
195
        if (WideCharToMultiByte(CP_UTF8, 0, wu, -1, userName, MAX_PATH * 2, NULL, NULL))
 
196
            return string(userName);
 
197
    }
 
198
 
 
199
    return string(_("Anonymous"));
 
200
#endif
 
201
}