- support for configurable refresh delay (first part) (FATE #301991)
[platform/upstream/libzypp.git] / zypp / ZConfig.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/ZConfig.cc
10  *
11 */
12 #include <iostream>
13 #include "zypp/base/Logger.h"
14 #include "zypp/base/InputStream.h"
15
16 #include "zypp/ZConfig.h"
17 #include "zypp/ZYppFactory.h"
18 #include "zypp/PathInfo.h"
19 #include "zypp/parser/IniDict.h"
20
21 using namespace std;
22 using namespace zypp::filesystem;
23 using namespace zypp::parser;
24
25 ///////////////////////////////////////////////////////////////////
26 namespace zypp
27 { /////////////////////////////////////////////////////////////////
28
29   ///////////////////////////////////////////////////////////////////
30   //
31   //    CLASS NAME : ZConfig::Impl
32   //
33   /** ZConfig implementation.
34   */
35   class ZConfig::Impl
36   {
37     public:
38       Impl()
39         : repo_add_probe(false),
40           repo_refresh_delay(0),
41           autolock_untrustedvendor(false)
42       {
43         MIL << "ZConfig singleton created." << endl;
44         Pathname confpath("/etc/zypp/zypp.conf");
45         if ( PathInfo(confpath).isExist())
46         {
47           InputStream is(confpath);
48           dict.read(is);
49         }
50         else
51         {
52           MIL << "No /etc/zypp/zypp.conf" << endl;
53         }
54         
55         for ( IniDict::section_const_iterator sit = dict.sectionsBegin(); 
56               sit != dict.sectionsEnd();
57               ++sit )
58         {
59           string section(*sit);
60           //MIL << section << endl;
61           for ( IniDict::entry_const_iterator it = dict.entriesBegin(*sit); 
62                 it != dict.entriesEnd(*sit);
63                 ++it )
64           {
65             string entry(it->first);
66             string value(it->second);
67             // DBG << (*it).first << "=" << (*it).second << endl;
68             if ( section == "main" )
69             {
70               if ( entry == "arch" )
71               {
72                 cfg_arch = Arch(value);
73               }
74               else if ( entry == "metadatadir" )
75               {
76                 cfg_metadata_path = Pathname(value);
77               }
78               else if ( entry == "reposdir" )
79               {
80                 cfg_known_repos_path = Pathname(value);
81               }
82               else if ( entry == "cachedir" )
83               {
84                 cfg_cache_path = Pathname(value);
85               }
86               else if ( entry == "repo.add.probe" )
87               {
88                 repo_add_probe = (value == "1");
89               }
90               else if ( entry == "repo.refresh.delay" )
91               {
92                 str::strtonum(value, repo_refresh_delay);
93               }
94             }
95             else if ( section == "locking" )
96             {
97               autolock_untrustedvendor = ( value == "1" );
98             }
99             
100           }
101         }
102       
103       }
104
105       ~Impl()
106       {}
107       
108     public:
109     parser::IniDict dict;
110     
111     Arch cfg_arch;
112     
113     Pathname cfg_metadata_path;
114     Pathname cfg_cache_path;
115     Pathname cfg_known_repos_path;
116     
117     bool repo_add_probe;
118     unsigned repo_refresh_delay;
119     
120     // [locking]
121     bool autolock_untrustedvendor;
122     
123   };
124   ///////////////////////////////////////////////////////////////////
125
126   ///////////////////////////////////////////////////////////////////
127   //
128   //    METHOD NAME : ZConfig::instance
129   //    METHOD TYPE : ZConfig &
130   //
131   ZConfig & ZConfig::instance()
132   {
133     static ZConfig _instance; // The singleton
134     return _instance;
135   }
136
137   ///////////////////////////////////////////////////////////////////
138   //
139   //    METHOD NAME : ZConfig::ZConfig
140   //    METHOD TYPE : Ctor
141   //
142   ZConfig::ZConfig()
143   : _pimpl( new Impl )
144   {
145     
146   }
147
148   ///////////////////////////////////////////////////////////////////
149   //
150   //    METHOD NAME : ZConfig::~ZConfig
151   //    METHOD TYPE : Dtor
152   //
153   ZConfig::~ZConfig( )
154   {}
155
156   ///////////////////////////////////////////////////////////////////
157 #warning change methods to use the singleton
158
159   ///////////////////////////////////////////////////////////////////
160   //
161   //    METHOD NAME : ZConfig::systemArchitecture
162   //    METHOD TYPE : Arch
163   //
164   Arch ZConfig::systemArchitecture() const
165   {
166     return ( (_pimpl->cfg_arch == Arch()) ?
167         getZYpp()->architecture() : _pimpl->cfg_arch );
168   }
169
170   ///////////////////////////////////////////////////////////////////
171   //
172   //    METHOD NAME : ZConfig::defaultTextLocale
173   //    METHOD TYPE : Locale
174   //
175   Locale ZConfig::textLocale() const
176   {
177     return getZYpp()->getTextLocale();
178   }
179
180   Pathname ZConfig::repoMetadataPath() const
181   {
182     return ( _pimpl->cfg_metadata_path.empty() 
183         ? Pathname("/var/cache/zypp/raw") : _pimpl->cfg_metadata_path );
184   }
185
186   Pathname ZConfig::repoCachePath() const
187   {
188     return ( _pimpl->cfg_cache_path.empty() 
189         ? Pathname("/var/cache/zypp") : _pimpl->cfg_cache_path );
190   }
191
192   Pathname ZConfig::knownReposPath() const
193   {
194     return ( _pimpl->cfg_known_repos_path.empty() 
195         ? Pathname("/etc/zypp/repos.d") : _pimpl->cfg_known_repos_path );
196   }
197
198   const std::string & ZConfig::cacheDBSplitJoinSeparator() const
199   {
200     static std::string s("!@$");
201     return s;
202   }
203
204   bool ZConfig::repo_add_probe() const
205   {
206     return _pimpl->repo_add_probe;
207   }
208
209   unsigned ZConfig::repo_refresh_delay() const
210   {
211     return _pimpl->repo_refresh_delay;
212   }
213
214   bool ZConfig::autolock_untrustedvendor() const
215   {
216     return _pimpl->autolock_untrustedvendor;
217   }
218   
219   /////////////////////////////////////////////////////////////////
220 } // namespace zypp
221 ///////////////////////////////////////////////////////////////////