Remove most traces of glib.
[platform/upstream/libzypp.git] / zypp / target / hal / Hal.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/target/hal/Hal.cc
10  *
11 */
12 #include <iostream>
13
14 #undef ZYPP_BASE_LOGGER_LOGGROUP
15 #define ZYPP_BASE_LOGGER_LOGGROUP "HAL"
16 #include "zypp/base/Logger.h"
17
18 #include "zypp/target/hal/Hal.h"
19
20 #ifndef FAKE_HAL
21
22 #include <hal/libhal.h>
23
24 #endif
25
26 using std::endl;
27 using std::string;
28
29 ///////////////////////////////////////////////////////////////////
30 namespace zypp
31 { /////////////////////////////////////////////////////////////////
32   ///////////////////////////////////////////////////////////////////
33   namespace target
34   { /////////////////////////////////////////////////////////////////
35     ///////////////////////////////////////////////////////////////////
36     namespace hal
37     { /////////////////////////////////////////////////////////////////
38
39 #ifndef FAKE_HAL
40 ///////////////////////////////////////////////////////////////////
41 //
42 //      CLASS NAME : Hal::Impl
43 //
44 /** Hal implementation. */
45 struct Hal::Impl
46 {
47     /**
48      * pointer to dbus connection
49      */
50     DBusConnection *_connection;
51
52     /**
53      * pointer to complete hal context
54      */
55     LibHalContext *_context;
56
57     /**
58      * report HAL error and reset the error condition
59      */
60     void
61     report_error (const std::string & reason, DBusError & error) const
62     {
63         ERR << reason << ": " << string (error.name) << ": " << string(error.message);
64         dbus_error_init(&error);
65         return;
66     }
67
68     /** Ctor. */
69     Impl()
70     {
71         DBusError error;
72         dbus_error_init (&error);
73
74         _connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error);                   // get shared connection to DBUS 'socket'
75         //_connection = dbus_bus_get_private (DBUS_BUS_SYSTEM, &error);         // get private connection DBUS 'socket'
76         if (_connection) {
77             _context = libhal_ctx_new ();                                       // create empty HAL context
78             if (_context) {
79                 if (libhal_ctx_set_dbus_connection (_context, _connection)) {   // connect to HAL daemon via DBUS
80                     if (libhal_ctx_init (_context, &error)) {                   // fill HAL context
81                         return;
82                     } else {
83                         report_error ("libhal_ctx_init", error);
84                     }
85                 } else {
86                     report_error ("libhal_ctx_set_dbus_connection", error);
87                 }
88                 libhal_ctx_free (_context);                                     // clean up
89                 _context = NULL;
90             } else {
91                 report_error ("libhal_ctx_new: Can't create libhal context", error);
92             }
93             // dbus_connection_close (_connection);                             // call only if dbus_bus_get_private was used
94             dbus_connection_unref (_connection);
95             _connection = NULL;
96         } else {
97             report_error ("dbus_bus_get", error);
98         }
99     }
100
101
102     /** Dtor. */
103     ~Impl()
104     {
105         if (_context) {
106             libhal_ctx_free (_context);
107         }
108         if (_connection) {
109             // dbus_connection_close (_connection);                             // call only if dbus_bus_get_private was used
110             dbus_connection_unref (_connection);
111         }
112     }
113
114     /**
115      * query for HAL capability present
116      */
117
118     bool query( const std::string & cap_r ) const
119     { return query( cap_r, Rel::ANY, std::string() ); }
120
121     /**
122      * query for HAL capability having a specific value
123      */
124     bool  query( const std::string & cap_r,
125                Rel op_r,
126                const std::string & val_r ) const
127     {
128         DBusError error;
129         dbus_error_init (&error);
130
131         // ask HAL which devices provide the needed capability
132
133         bool result = false;
134
135         int device_count;
136         char **device_names = libhal_find_device_by_capability (_context, cap_r.c_str(), &device_count, &error);
137
138         if (device_names == NULL) {
139             report_error ("libhal_find_device_by_capability", error);
140             result = false;
141         }
142         else if (device_count > 0) {
143
144 #if 0           // once we get a capabilities value from HAL, we can compare it ...
145             if (value) {
146                 string lhs (value);
147                 int cmp = (lhs != rhs) ? ((lhs < rhs) ? -1 : 1) : 0;
148
149                 switch ( relation.inSwitch() )
150                 {
151                     case Rel::EQ_e:
152                         res = (cmp == 0);
153                         break;
154                     case Rel::NE_e:
155                         res = (cmp != 0);
156                         break;
157                     case Rel::LT_e:
158                         res = (cmp == -1);
159                         break;
160                     case Rel::LE_e:
161                         res = (cmp != 1);
162                         break;
163                     case Rel::GT_e:
164                         res = (cmp == 1);
165                         break;
166                     case Rel::GE_e:
167                         res = (cmp != -1);
168                         break;
169                     case Rel::ANY_e:
170                         res = true;
171                         break;
172                     case Rel::NONE_e:
173                         res = false;
174                         break;
175                     default:
176                         // We shouldn't get here.
177                         INT << "Unknown relational opertor '" << relation << "' treated as  'NONE'" << endl;
178                         break;
179                 }
180             }
181 #endif
182
183             result = true;
184
185         }
186
187         if (device_names != NULL)
188             libhal_free_string_array (device_names);
189
190         return result;
191     }
192
193   public:
194      /** Offer default Impl. */
195     static shared_ptr<Impl> nullimpl()
196     {
197         static shared_ptr<Impl> _nullimpl( new Impl );
198         return _nullimpl;
199     }
200
201 };  // struct Hal::Impl
202
203
204 #else // FAKE_HAL
205       struct Hal::Impl
206       {
207         bool query( const std::string & cap_r ) const
208         { return query( cap_r, Rel::ANY, std::string() ); }
209         bool  query( const std::string & cap_r,
210                      Rel op_r,
211                      const std::string & val_r ) const
212         { return false; }
213         /** Offer default Impl. */
214         static shared_ptr<Impl> nullimpl()
215         {
216           static shared_ptr<Impl> _nullimpl( new Impl );
217           return _nullimpl;
218         }
219       };
220 #endif
221
222 ///////////////////////////////////////////////////////////////////
223
224 /** \relates Hal::Impl Stream output
225      * And maybe std::ostream & operator<< Hal::Impl below too.
226      * return libhal version or something like that.
227  */
228 inline std::ostream & operator<<( std::ostream & str, const Hal::Impl & obj )
229 {
230   return str << "Hal::Impl";
231 }
232
233 ///////////////////////////////////////////////////////////////////
234 //
235 //      CLASS NAME : Hal
236 //
237 ///////////////////////////////////////////////////////////////////
238
239 ///////////////////////////////////////////////////////////////////
240 //
241 //      METHOD NAME : Hal::Hal
242 //      METHOD TYPE : Ctor
243 //
244 Hal::Hal()
245 : _pimpl( Impl::nullimpl() )
246 {}
247
248 ///////////////////////////////////////////////////////////////////
249 //
250 //      METHOD NAME : Hal::~Hal
251 //      METHOD TYPE : Dtor
252 //
253 Hal::~Hal()
254 {}
255
256 ///////////////////////////////////////////////////////////////////
257 //
258 //      METHOD NAME : Hal::instance
259 //      METHOD TYPE : Hal &
260 //
261 Hal & Hal::instance()
262 {
263   static Hal _singleton;
264   return _singleton;
265 }
266
267 ///////////////////////////////////////////////////////////////////
268 // Foreward to implenemtation
269 ///////////////////////////////////////////////////////////////////
270
271 bool Hal::query( const std::string & cap_r ) const
272 { return _pimpl->query( cap_r ); }
273
274 bool Hal::query( const std::string & cap_r,
275                  Rel op_r,
276                  const std::string & val_r ) const
277 { return _pimpl->query( cap_r, op_r, val_r ); }
278
279 /******************************************************************
280 **
281 **      FUNCTION NAME : operator<<
282 **      FUNCTION TYPE : std::ostream &
283 */
284 std::ostream & operator<<( std::ostream & str, const Hal & obj )
285 {
286   return str << *obj._pimpl;
287 }
288
289 /////////////////////////////////////////////////////////////////
290     } // namespace hal
291     ///////////////////////////////////////////////////////////////////
292     /////////////////////////////////////////////////////////////////
293   } // namespace target
294   ///////////////////////////////////////////////////////////////////
295   /////////////////////////////////////////////////////////////////
296 } // namespace zypp
297 ///////////////////////////////////////////////////////////////////