]> git.saurik.com Git - apt.git/blob - apt-pkg/aptconfiguration.cc
merge with lp:~mvo/apt/apt-sid to get the complete fr
[apt.git] / apt-pkg / aptconfiguration.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* ######################################################################
4
5 Provide access methods to various configuration settings,
6 setup defaults and returns validate settings.
7
8 ##################################################################### */
9 /*}}}*/
10 // Include Files /*{{{*/
11 #include <apt-pkg/fileutl.h>
12 #include <apt-pkg/aptconfiguration.h>
13 #include <apt-pkg/configuration.h>
14
15 #include <vector>
16 #include <string>
17 #include <algorithm>
18 /*}}}*/
19 namespace APT {
20 // getCompressionTypes - Return Vector of usbale compressiontypes /*{{{*/
21 // ---------------------------------------------------------------------
22 /* return a vector of compression types in the prefered order. */
23 std::vector<std::string>
24 const Configuration::getCompressionTypes(bool const &Cached) {
25 static std::vector<std::string> types;
26 if (types.empty() == false) {
27 if (Cached == true)
28 return types;
29 else
30 types.clear();
31 }
32
33 // setup the defaults for the compressiontypes => method mapping
34 _config->CndSet("Acquire::CompressionTypes::bz2","bzip2");
35 _config->CndSet("Acquire::CompressionTypes::lzma","lzma");
36 _config->CndSet("Acquire::CompressionTypes::gz","gzip");
37
38 // Set default application paths to check for optional compression types
39 _config->CndSet("Dir::Bin::lzma", "/usr/bin/lzma");
40 _config->CndSet("Dir::Bin::bzip2", "/bin/bzip2");
41
42 // accept non-list order as override setting for config settings on commandline
43 std::string const overrideOrder = _config->Find("Acquire::CompressionTypes::Order","");
44 if (overrideOrder.empty() == false)
45 types.push_back(overrideOrder);
46
47 // load the order setting into our vector
48 std::vector<std::string> const order = _config->FindVector("Acquire::CompressionTypes::Order");
49 for (std::vector<std::string>::const_iterator o = order.begin();
50 o != order.end(); o++) {
51 if ((*o).empty() == true)
52 continue;
53 // ignore types we have no method ready to use
54 if (_config->Exists(string("Acquire::CompressionTypes::").append(*o)) == false)
55 continue;
56 // ignore types we have no app ready to use
57 string const appsetting = string("Dir::Bin::").append(*o);
58 if (_config->Exists(appsetting) == true) {
59 std::string const app = _config->FindFile(appsetting.c_str(), "");
60 if (app.empty() == false && FileExists(app) == false)
61 continue;
62 }
63 types.push_back(*o);
64 }
65
66 // move again over the option tree to add all missing compression types
67 ::Configuration::Item const *Types = _config->Tree("Acquire::CompressionTypes");
68 if (Types != 0)
69 Types = Types->Child;
70
71 for (; Types != 0; Types = Types->Next) {
72 if (Types->Tag == "Order" || Types->Tag.empty() == true)
73 continue;
74 // ignore types we already have in the vector
75 if (std::find(types.begin(),types.end(),Types->Tag) != types.end())
76 continue;
77 // ignore types we have no app ready to use
78 string const appsetting = string("Dir::Bin::").append(Types->Value);
79 if (appsetting.empty() == false && _config->Exists(appsetting) == true) {
80 std::string const app = _config->FindFile(appsetting.c_str(), "");
81 if (app.empty() == false && FileExists(app) == false)
82 continue;
83 }
84 types.push_back(Types->Tag);
85 }
86
87 return types;
88 }
89 /*}}}*/
90 // GetLanguages - Return Vector of Language Codes /*{{{*/
91 // ---------------------------------------------------------------------
92 /* return a vector of language codes in the prefered order.
93 the special word "environment" will be replaced with the long and the short
94 code of the local settings and it will be insured that this will not add
95 duplicates. So in an german local the setting "environment, de_DE, en, de"
96 will result in "de_DE, de, en".
97 The special word "none" is the stopcode for the not-All code vector */
98 std::vector<std::string> const Configuration::getLanguages(bool const &All,
99 bool const &Cached, char const * const Locale) {
100 using std::string;
101
102 // The detection is boring and has a lot of cornercases,
103 // so we cache the results to calculated it only once.
104 std::vector<string> static allCodes;
105 std::vector<string> static codes;
106
107 // we have something in the cache
108 if (codes.empty() == false || allCodes.empty() == false) {
109 if (Cached == true) {
110 if(All == true && allCodes.empty() == false)
111 return allCodes;
112 else
113 return codes;
114 } else {
115 allCodes.clear();
116 codes.clear();
117 }
118 }
119
120 // get the environment language code
121 // we extract both, a long and a short code and then we will
122 // check if we actually need both (rare) or if the short is enough
123 string const envMsg = string(Locale == 0 ? std::setlocale(LC_MESSAGES, NULL) : Locale);
124 size_t const lenShort = (envMsg.find('_') != string::npos) ? envMsg.find('_') : 2;
125 size_t const lenLong = (envMsg.find('.') != string::npos) ? envMsg.find('.') : (lenShort + 3);
126
127 string envLong = envMsg.substr(0,lenLong);
128 string const envShort = envLong.substr(0,lenShort);
129 bool envLongIncluded = true, envShortIncluded = false;
130
131 // first cornercase: LANG=C, so we use only "en" Translation
132 if (envLong == "C") {
133 codes.push_back("en");
134 return codes;
135 }
136
137 if (envLong != envShort) {
138 // to save the servers from unneeded queries, we only try also long codes
139 // for languages it is realistic to have a long code translation file...
140 char const *needLong[] = { "cs", "en", "pt", "sv", "zh", NULL };
141 for (char const **l = needLong; *l != NULL; l++)
142 if (envShort.compare(*l) == 0) {
143 envLongIncluded = false;
144 break;
145 }
146 }
147
148 // we don't add the long code, but we allow the user to do so
149 if (envLongIncluded == true)
150 envLong.clear();
151
152 // FIXME: Remove support for the old APT::Acquire::Translation
153 // it was undocumented and so it should be not very widthly used
154 string const oldAcquire = _config->Find("APT::Acquire::Translation","");
155 if (oldAcquire.empty() == false && oldAcquire != "environment") {
156 if (oldAcquire != "none")
157 codes.push_back(oldAcquire);
158 return codes;
159 }
160
161 // Support settings like Acquire::Translation=none on the command line to
162 // override the configuration settings vector of languages.
163 string const forceLang = _config->Find("Acquire::Languages","");
164 if (forceLang.empty() == false) {
165 if (forceLang == "environment") {
166 if (envLongIncluded == false)
167 codes.push_back(envLong);
168 if (envShortIncluded == false)
169 codes.push_back(envShort);
170 return codes;
171 } else if (forceLang != "none")
172 codes.push_back(forceLang);
173 return codes;
174 }
175
176 std::vector<string> const lang = _config->FindVector("Acquire::Languages");
177 // the default setting -> "environment, en"
178 if (lang.empty() == true) {
179 if (envLongIncluded == false)
180 codes.push_back(envLong);
181 if (envShortIncluded == false)
182 codes.push_back(envShort);
183 if (envShort != "en")
184 codes.push_back("en");
185 return codes;
186 }
187
188 // the configs define the order, so add the environment
189 // then needed and ensure the codes are not listed twice.
190 bool noneSeen = false;
191 for (std::vector<string>::const_iterator l = lang.begin();
192 l != lang.end(); l++) {
193 if (*l == "environment") {
194 if (envLongIncluded == true && envShortIncluded == true)
195 continue;
196 if (envLongIncluded == false) {
197 envLongIncluded = true;
198 if (noneSeen == false)
199 codes.push_back(envLong);
200 allCodes.push_back(envLong);
201 }
202 if (envShortIncluded == false) {
203 envShortIncluded = true;
204 if (noneSeen == false)
205 codes.push_back(envShort);
206 allCodes.push_back(envShort);
207 }
208 continue;
209 } else if (*l == "none") {
210 noneSeen = true;
211 continue;
212 } else if ((envLongIncluded == true && *l == envLong) ||
213 (envShortIncluded == true && *l == envShort))
214 continue;
215
216 if (noneSeen == false)
217 codes.push_back(*l);
218 allCodes.push_back(*l);
219 }
220 if (All == true)
221 return allCodes;
222 else
223 return codes;
224 }
225 /*}}}*/
226 }