~ubuntu-branches/ubuntu/precise/stellarium/precise

« back to all changes in this revision

Viewing changes to src/StelSkyCultureMgr.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cédric Delfosse
  • Date: 2008-05-19 21:28:23 UTC
  • mfrom: (3.1.5 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080519212823-m5nfiuntxstxzxj7
Tags: 0.9.1-4
Add libxcursor-dev, libxfixes-dev, libxinerama-dev, libqt4-opengl-dev to
build-deps (Closes: #479906)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Stellarium
 
3
 * Copyright (C) 2006 Fabien Chereau
 
4
 * 
 
5
 * This program is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU General Public License
 
7
 * as published by the Free Software Foundation; either version 2
 
8
 * of the License, or (at your option) any later version.
 
9
 * 
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 * 
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
18
 */
 
19
 
 
20
#include "StelSkyCultureMgr.hpp"
 
21
#include "StelFileMgr.hpp"
 
22
#include "Translator.hpp"
 
23
#include "InitParser.hpp"
 
24
#include "StelApp.hpp"
 
25
 
 
26
#include <iostream>
 
27
#include <fstream>
 
28
#include <dirent.h>
 
29
#include <cassert>
 
30
#include <QDebug>
 
31
 
 
32
StelSkyCultureMgr::StelSkyCultureMgr()
 
33
{
 
34
        QSet<QString> cultureDirNames;
 
35
        StelFileMgr& fileMan(StelApp::getInstance().getFileMgr());
 
36
        
 
37
        try
 
38
        {
 
39
                cultureDirNames = fileMan.listContents("skycultures",StelFileMgr::DIRECTORY);
 
40
        }
 
41
        catch(exception& e)
 
42
        {
 
43
                cerr << "ERROR while trying list sky cultures:" << e.what() << endl;    
 
44
        }
 
45
        
 
46
        for(QSet<QString>::iterator dir=cultureDirNames.begin(); dir!=cultureDirNames.end(); dir++)
 
47
        {
 
48
                try
 
49
                {
 
50
                        InitParser pd;
 
51
                        pd.load(fileMan.findFile("skycultures/" + *dir + "/info.ini"));
 
52
                        dirToNameEnglish[*dir] = pd.get_str("info:name");
 
53
                }
 
54
                catch (exception& e)
 
55
                {
 
56
                        qWarning() << "WARNING: unable to successfully read info.ini file from skyculture dir" << *dir;
 
57
                }
 
58
        }       
 
59
}
 
60
 
 
61
 
 
62
StelSkyCultureMgr::~StelSkyCultureMgr()
 
63
{
 
64
}
 
65
 
 
66
 
 
67
//! Init itself from a config file.
 
68
void StelSkyCultureMgr::init(const InitParser& conf)
 
69
{
 
70
        QString tmp = conf.get_str("localization", "sky_culture", "western").c_str();
 
71
        setSkyCultureDir(tmp);
 
72
}
 
73
 
 
74
//! Set the current sky culture from the passed directory
 
75
bool StelSkyCultureMgr::setSkyCultureDir(const QString& cultureDir)
 
76
{
 
77
        // make sure culture definition exists before attempting or will die
 
78
        if(directoryToSkyCultureEnglish(cultureDir) == "")
 
79
        {
 
80
                qWarning() << "Invalid sky culture directory: " << cultureDir;
 
81
                return false;
 
82
        }
 
83
        skyCultureDir = cultureDir;
 
84
        StelApp::getInstance().updateSkyCulture();
 
85
        return true;
 
86
}
 
87
 
 
88
//! returns newline delimited list of human readable culture names in english
 
89
string StelSkyCultureMgr::getSkyCultureListEnglish(void)
 
90
{
 
91
        string cultures;
 
92
        for ( map<QString, string>::const_iterator iter = dirToNameEnglish.begin(); iter != dirToNameEnglish.end(); ++iter )
 
93
        {
 
94
                cultures += iter->second + "\n";
 
95
        }
 
96
        return cultures;
 
97
}
 
98
 
 
99
//! returns newline delimited list of human readable culture names translated to current locale
 
100
wstring StelSkyCultureMgr::getSkyCultureListI18(void)
 
101
{
 
102
        wstring cultures;
 
103
        for ( map<QString, string>::const_iterator iter = dirToNameEnglish.begin(); iter != dirToNameEnglish.end(); ++iter )
 
104
        {
 
105
                if (iter != dirToNameEnglish.begin()) cultures += L"\n";
 
106
                cultures += _(iter->second);
 
107
        }
 
108
        //wcout << cultures << endl;
 
109
        return cultures;
 
110
}
 
111
 
 
112
//! returns newline delimited hash of human readable culture names translated to current locale
 
113
//! and the directory names
 
114
wstring StelSkyCultureMgr::getSkyCultureHash(void) const
 
115
{
 
116
        wstring cultures;
 
117
        for ( map<QString, string>::const_iterator iter = dirToNameEnglish.begin(); iter != dirToNameEnglish.end(); ++iter )
 
118
        {
 
119
                
 
120
                // weed out invalid hash entries from invalid culture lookups in hash
 
121
                // TODO how to keep hash clean in the first place
 
122
                if(iter->second == "") continue;
 
123
                
 
124
                cultures += _(iter->second);
 
125
                cultures += wstring(L"\n") + (iter->first).toStdWString() + L"\n";
 
126
        }
 
127
        // wcout << cultures << endl;
 
128
        return cultures;
 
129
}
 
130
 
 
131
wstring StelSkyCultureMgr::getSkyCulture() const
 
132
{
 
133
        return directoryToSkyCultureI18(skyCultureDir);
 
134
}
 
135
 
 
136
 
 
137
string StelSkyCultureMgr::directoryToSkyCultureEnglish(const QString& directory)
 
138
{
 
139
        return dirToNameEnglish[directory];
 
140
}
 
141
 
 
142
wstring StelSkyCultureMgr::directoryToSkyCultureI18(const QString& directory) const
 
143
{
 
144
        map<QString, string>::const_iterator i = dirToNameEnglish.find(directory);
 
145
        if (i==dirToNameEnglish.end())
 
146
        {
 
147
                qWarning() << "WARNING: StelSkyCultureMgr::directoryToSkyCultureI18(\""
 
148
                     << directory << "\"): could not find directory";
 
149
                return L"";
 
150
        }
 
151
        else
 
152
                return _(i->second);
 
153
}
 
154
 
 
155
QString StelSkyCultureMgr::skyCultureToDirectory(const wstring& cultureName)
 
156
{
 
157
        for ( map<QString, string>::const_iterator iter = dirToNameEnglish.begin(); iter != dirToNameEnglish.end(); ++iter )
 
158
        {
 
159
                if (_(iter->second) == cultureName) return iter->first;
 
160
        }
 
161
        return "";
 
162
}