Initial attempt at i18n support using gettext
[synfig.git] / synfig-studio / trunk / src / synfigapp / settings.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file settings.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include <fstream>
34 #include <iostream>
35 #include "settings.h"
36 #include <synfig/general.h>
37
38 #include "general.h"
39
40 #endif
41
42 /* === U S I N G =========================================================== */
43
44 using namespace std;
45 using namespace etl;
46 using namespace synfig;
47 using namespace synfigapp;
48
49 /* === M A C R O S ========================================================= */
50
51 /* === G L O B A L S ======================================================= */
52
53 /* === P R O C E D U R E S ================================================= */
54
55 /* === M E T H O D S ======================================================= */
56
57 Settings::Settings()
58 {
59 }
60
61 Settings::~Settings()
62 {
63 }
64
65 synfig::String
66 Settings::get_value(const synfig::String& key)const
67 {
68         synfig::String value;
69         if(!get_value(key,value))
70                 return synfig::String();
71         return value;
72 }
73
74 void
75 Settings::add_domain(Settings* domain, const synfig::String& name)
76 {
77         domain_map[name]=domain;
78 }
79
80 void
81 Settings::remove_domain(const synfig::String& name)
82 {
83         domain_map.erase(name);
84 }
85
86 bool
87 Settings::get_value(const synfig::String& key, synfig::String& value)const
88 {
89         // Search for the value in any children domains
90         DomainMap::const_iterator iter;
91         for(iter=domain_map.begin();iter!=domain_map.end();++iter)
92         {
93                 // if we have a domain hit
94                 if(key.size()>iter->first.size() && String(key.begin(),key.begin()+iter->first.size())==iter->first)
95                 {
96                         synfig::String key_(key.begin()+iter->first.size()+1,key.end());
97
98                         // If the domain has it, then we have got a hit
99                         if(iter->second->get_value(key_,value))
100                                 return true;
101                 }
102         }
103
104         // Search for the value in our simple map
105         if(simple_value_map.count(key))
106         {
107                 value=simple_value_map.find(key)->second;
108                 return true;
109         }
110
111         // key not found
112         return false;
113 }
114
115 bool
116 Settings::set_value(const synfig::String& key,const synfig::String& value)
117 {
118         // Search for the key in any children domains
119         DomainMap::iterator iter;
120         for(iter=domain_map.begin();iter!=domain_map.end();++iter)
121         {
122                 // if we have a domain hit
123                 if(key.size()>iter->first.size() && String(key.begin(),key.begin()+iter->first.size())==iter->first)
124                 {
125                         synfig::String key_(key.begin()+iter->first.size()+1,key.end());
126
127                         return iter->second->set_value(key_,value);
128                 }
129         }
130
131         simple_value_map[key]=value;
132         return true;
133 }
134
135 //! Compare two key names, putting pref.* keys first
136 static bool
137 compare_pref_first (synfig::String first, synfig::String second)
138 {
139         return  first.substr(0, 5) == "pref."
140                         ?       second.substr(0, 5) == "pref."
141                                 ?       first < second
142                                 :       true
143                         :       second.substr(0, 5) == "pref."
144                                 ?       false
145                                 :       first < second;
146 }
147
148 Settings::KeyList
149 Settings::get_key_list()const
150 {
151         KeyList key_list;
152
153         // Get keys from the domains
154         {
155                 DomainMap::const_iterator iter;
156                 for(iter=domain_map.begin();iter!=domain_map.end();++iter)
157                 {
158                         KeyList sub_key_list(iter->second->get_key_list());
159                         KeyList::iterator key_iter;
160                         for(key_iter=sub_key_list.begin();key_iter!=sub_key_list.end();++key_iter)
161                                 key_list.push_back(iter->first+'.'+*key_iter);
162                 }
163         }
164
165         // Get keys from the simple variables
166         {
167                 ValueBaseMap::const_iterator iter;
168                 for(iter=simple_value_map.begin();iter!=simple_value_map.end();++iter)
169                         key_list.push_back(iter->first);
170         }
171
172         // Sort the keys
173         // We make sure the 'pref.*' keys come first to fix bug 1694393,
174         // where windows were being created before the parameter
175         // specifying which window manager hint to use had been loaded
176         key_list.sort(compare_pref_first);
177
178         return key_list;
179 }
180
181 bool
182 Settings::save_to_file(const synfig::String& filename)const
183 {
184         synfig::String tmp_filename(filename+".TMP");
185
186         try
187         {
188                 std::ofstream file(tmp_filename.c_str());
189
190                 if(!file)return false;
191
192                 KeyList key_list(get_key_list());
193
194                 // Save the keys
195                 {
196                         KeyList::const_iterator iter;
197                         for(iter=key_list.begin();iter!=key_list.end();++iter)
198                         {
199                                 if(!file)return false;
200                                 String ret = get_value(*iter);
201                                 if (ret != String()) file<<*iter<<'='<<ret<<endl;
202                         }
203                 }
204
205                 if(!file)
206                         return false;
207         }catch(...) { return false; }
208
209 #ifdef _WIN32
210         char old_file[80]="sif.XXXXXXXX";
211         mktemp(old_file);
212         rename(filename.c_str(),old_file);
213         if(rename(tmp_filename.c_str(),filename.c_str())!=0)
214         {
215                 rename(old_file,tmp_filename.c_str());
216                 return false;
217         }
218         remove(old_file);
219 #else
220         if(rename(tmp_filename.c_str(),filename.c_str())!=0)
221                 return false;
222 #endif
223
224         return true;
225 }
226
227 bool
228 Settings::load_from_file(const synfig::String& filename)
229 {
230         std::ifstream file(filename.c_str());
231         if(!file)
232                 return false;
233         while(file)
234         {
235                 std::string line;
236                 getline(file,line);
237                 if(!line.empty() && ((line[0]>='a' && line[0]<='z')||(line[0]>='A' && line[0]<='Z')))
238                 {
239                         std::string::iterator equal(find(line.begin(),line.end(),'='));
240                         if(equal==line.end())
241                                 continue;
242                         std::string key(line.begin(),equal);
243                         std::string value(equal+1,line.end());
244
245                         //synfig::info("Settings::load_from_file(): Trying Key \"%s\" with a value of \"%s\".",key.c_str(),value.c_str());
246                         try{
247                         if(!set_value(key,value))
248                                 synfig::warning("Settings::load_from_file(): Key \"%s\" with a value of \"%s\" was rejected.",key.c_str(),value.c_str());
249                         }
250                         catch(...)
251                         {
252                                 synfig::error("Settings::load_from_file(): Attempt to set key \"%s\" with a value of \"%s\" has thrown an exception.",key.c_str(),value.c_str());
253                                 throw;
254                         }
255                 }
256         }
257         return true;
258 }