Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / synfigstudio_0_61_07 / 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 #endif
39
40 /* === U S I N G =========================================================== */
41
42 using namespace std;
43 using namespace etl;
44 using namespace synfig;
45 using namespace synfigapp;
46
47 /* === M A C R O S ========================================================= */
48
49 /* === G L O B A L S ======================================================= */
50
51 /* === P R O C E D U R E S ================================================= */
52
53 /* === M E T H O D S ======================================================= */
54
55 Settings::Settings()
56 {
57 }
58
59 Settings::~Settings()
60 {
61 }
62
63 synfig::String
64 Settings::get_value(const synfig::String& key)const
65 {
66         synfig::String value;
67         if(!get_value(key,value))
68                 return synfig::String();
69         return value;
70 }
71
72 void
73 Settings::add_domain(Settings* domain, const synfig::String& name)
74 {
75         domain_map[name]=domain;
76 }
77
78 void
79 Settings::remove_domain(const synfig::String& name)
80 {
81         domain_map.erase(name);
82 }
83
84 bool
85 Settings::get_value(const synfig::String& key, synfig::String& value)const
86 {
87         // Search for the value in any children domains
88         DomainMap::const_iterator iter;
89         for(iter=domain_map.begin();iter!=domain_map.end();++iter)
90         {
91                 // if we have a domain hit
92                 if(key.size()>iter->first.size() && String(key.begin(),key.begin()+iter->first.size())==iter->first)
93                 {
94                         synfig::String key_(key.begin()+iter->first.size()+1,key.end());
95
96                         // If the domain has it, then we have got a hit
97                         if(iter->second->get_value(key_,value))
98                                 return true;
99                 }
100         }
101
102         // Search for the value in our simple map
103         if(simple_value_map.count(key))
104         {
105                 value=simple_value_map.find(key)->second;
106                 return true;
107         }
108
109         // key not found
110         return false;
111 }
112
113 bool
114 Settings::set_value(const synfig::String& key,const synfig::String& value)
115 {
116         // Search for the key in any children domains
117         DomainMap::iterator iter;
118         for(iter=domain_map.begin();iter!=domain_map.end();++iter)
119         {
120                 // if we have a domain hit
121                 if(key.size()>iter->first.size() && String(key.begin(),key.begin()+iter->first.size())==iter->first)
122                 {
123                         synfig::String key_(key.begin()+iter->first.size()+1,key.end());
124
125                         return iter->second->set_value(key_,value);
126                 }
127         }
128
129         simple_value_map[key]=value;
130         return true;
131 }
132
133 //! Compare two key names, putting pref.* keys first
134 static bool
135 compare_pref_first (synfig::String first, synfig::String second)
136 {
137         return  first.substr(0, 5) == "pref."
138                         ?       second.substr(0, 5) == "pref."
139                                 ?       first < second
140                                 :       true
141                         :       second.substr(0, 5) == "pref."
142                                 ?       false
143                                 :       first < second;
144 }
145
146 Settings::KeyList
147 Settings::get_key_list()const
148 {
149         KeyList key_list;
150
151         // Get keys from the domains
152         {
153                 DomainMap::const_iterator iter;
154                 for(iter=domain_map.begin();iter!=domain_map.end();++iter)
155                 {
156                         KeyList sub_key_list(iter->second->get_key_list());
157                         KeyList::iterator key_iter;
158                         for(key_iter=sub_key_list.begin();key_iter!=sub_key_list.end();++key_iter)
159                                 key_list.push_back(iter->first+'.'+*key_iter);
160                 }
161         }
162
163         // Get keys from the simple variables
164         {
165                 ValueBaseMap::const_iterator iter;
166                 for(iter=simple_value_map.begin();iter!=simple_value_map.end();++iter)
167                         key_list.push_back(iter->first);
168         }
169
170         // Sort the keys
171         // We make sure the 'pref.*' keys come first to fix bug 1694393,
172         // where windows were being created before the parameter
173         // specifying which window manager hint to use had been loaded
174         key_list.sort(compare_pref_first);
175
176         return key_list;
177 }
178
179 bool
180 Settings::save_to_file(const synfig::String& filename)const
181 {
182         synfig::String tmp_filename(filename+".TMP");
183
184         try
185         {
186                 std::ofstream file(tmp_filename.c_str());
187
188                 if(!file)return false;
189
190                 KeyList key_list(get_key_list());
191
192                 // Save the keys
193                 {
194                         KeyList::const_iterator iter;
195                         for(iter=key_list.begin();iter!=key_list.end();++iter)
196                         {
197                                 if(!file)return false;
198                                 String ret = get_value(*iter);
199                                 if (ret != String()) file<<*iter<<'='<<ret<<endl;
200                         }
201                 }
202
203                 if(!file)
204                         return false;
205         }catch(...) { return false; }
206
207 #ifdef _WIN32
208         char old_file[80]="sif.XXXXXXXX";
209         mktemp(old_file);
210         rename(filename.c_str(),old_file);
211         if(rename(tmp_filename.c_str(),filename.c_str())!=0)
212         {
213                 rename(old_file,tmp_filename.c_str());
214                 return false;
215         }
216         remove(old_file);
217 #else
218         if(rename(tmp_filename.c_str(),filename.c_str())!=0)
219                 return false;
220 #endif
221
222         return true;
223 }
224
225 bool
226 Settings::load_from_file(const synfig::String& filename)
227 {
228         std::ifstream file(filename.c_str());
229         if(!file)
230                 return false;
231         while(file)
232         {
233                 std::string line;
234                 getline(file,line);
235                 if(!line.empty() && ((line[0]>='a' && line[0]<='z')||(line[0]>='A' && line[0]<='Z')))
236                 {
237                         std::string::iterator equal(find(line.begin(),line.end(),'='));
238                         if(equal==line.end())
239                                 continue;
240                         std::string key(line.begin(),equal);
241                         std::string value(equal+1,line.end());
242
243                         //synfig::info("Settings::load_from_file(): Trying Key \"%s\" with a value of \"%s\".",key.c_str(),value.c_str());
244                         try{
245                         if(!set_value(key,value))
246                                 synfig::warning("Settings::load_from_file(): Key \"%s\" with a value of \"%s\" was rejected.",key.c_str(),value.c_str());
247                         }
248                         catch(...)
249                         {
250                                 synfig::error("Settings::load_from_file(): Attept to set key \"%s\" with a value of \"%s\" has thrown an exception.",key.c_str(),value.c_str());
251                                 throw;
252                         }
253                 }
254         }
255         return true;
256 }