- Added translations marks to hal, url and mutex exceptions (#23771)
[platform/upstream/libzypp.git] / zypp / target / hal / HalContext.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/target/hal/HalContext.cc
10  *
11  *  \brief Hardware abstaction layer library wrapper implementation.
12  */
13 #ifndef FAKE_HAL // disables zypp's HAL dependency
14
15 #include <zypp/target/hal/HalContext.h>
16 #include <zypp/thread/Mutex.h>
17 #include <zypp/thread/MutexLock.h>
18 #include <zypp/base/NonCopyable.h>
19 #include <zypp/base/Logger.h>
20 #include <zypp/base/String.h>
21 #include <zypp/base/Gettext.h>
22
23 #include <dbus/dbus-glib-lowlevel.h>
24 #include <dbus/dbus-glib.h>
25 #include <hal/libhal.h>
26 #include <hal/libhal-storage.h>
27
28 #include <iostream>
29
30 using namespace std;
31
32 //////////////////////////////////////////////////////////////////////
33 namespace zypp
34 { ////////////////////////////////////////////////////////////////////
35   ////////////////////////////////////////////////////////////////////
36   namespace target
37   { //////////////////////////////////////////////////////////////////
38     //////////////////////////////////////////////////////////////////
39     namespace hal
40     { ////////////////////////////////////////////////////////////////
41
42       using zypp::thread::Mutex;
43       using zypp::thread::MutexLock;
44
45       ////////////////////////////////////////////////////////////////
46       namespace // anonymous
47       { //////////////////////////////////////////////////////////////
48
49
50         //////////////////////////////////////////////////////////////
51         // STATIC
52         /**
53          ** hmm... currently a global one..
54         */
55         static Mutex g_Mutex;
56
57
58         //////////////////////////////////////////////////////////////
59         /**
60          * Internal hal (dbus ) error helper class.
61          */
62         class HalError
63         {
64         public:
65           DBusError error;
66
67           HalError()  { dbus_error_init(&error); }
68           ~HalError() { dbus_error_free(&error); }
69
70           inline bool         isSet() const
71           {
72             return dbus_error_is_set(&error);
73           }
74
75           inline HalException halException(const std::string &msg = std::string()) const
76           {
77             if( isSet() && error.name != NULL && error.message != NULL) {
78               return HalException(error.name, error.message);
79             }
80             else if( !msg.empty()) {
81               return HalException(msg);
82             }
83             else {
84               return HalException();
85             }
86           }
87         };
88
89
90         // -----------------------------------------------------------
91         inline void
92         VERIFY_CONTEXT(const zypp::RW_pointer<HalContext_Impl> &h)
93         {
94           if( !h)
95           {
96             ZYPP_THROW(HalException(_("HalContext not connected")));
97           }
98         }
99
100         // -----------------------------------------------------------
101         inline void
102         VERIFY_DRIVE(const zypp::RW_pointer<HalDrive_Impl> &d)
103         {
104           if( !d)
105           {
106             ZYPP_THROW(HalException(_("HalDrive not initialized")));
107           }
108         }
109
110         // -----------------------------------------------------------
111         inline void
112         VERIFY_VOLUME(const zypp::RW_pointer<HalVolume_Impl> &v)
113         {
114           if( !v)
115           {
116             ZYPP_THROW(HalException(_("HalVolume not initialized")));
117           }
118         }
119
120         //////////////////////////////////////////////////////////////
121       } // anonymous
122       ////////////////////////////////////////////////////////////////
123
124       ////////////////////////////////////////////////////////////////
125       std::ostream &
126       HalException::dumpOn( std::ostream & str ) const
127       {
128         if(!e_name.empty() && !e_msg.empty())
129           return str << msg() << ": " << e_msg << " (" << e_name << ")";
130         else if(!e_msg.empty())
131           return str << msg() << ": " << e_msg;
132         else
133           return str << msg();
134       }
135
136       ////////////////////////////////////////////////////////////////
137       class HalContext_Impl
138       {
139       public:
140         HalContext_Impl(bool monitorable = false);
141         ~HalContext_Impl();
142
143         DBusConnection *conn;
144         LibHalContext  *hctx;
145         bool            pcon; // private connection
146       };
147
148
149       ////////////////////////////////////////////////////////////////
150       class HalDrive_Impl
151       {
152       public:
153         zypp::RW_pointer<HalContext_Impl>  hal;
154         LibHalDrive                       *drv;
155
156         HalDrive_Impl()
157           : hal(), drv(NULL)
158         {
159         }
160
161         HalDrive_Impl(const zypp::RW_pointer<HalContext_Impl> &r,
162                       LibHalDrive *d)
163           : hal(r), drv(d)
164         {
165         }
166
167         ~HalDrive_Impl()
168         {
169           if( drv)
170             libhal_drive_free(drv);
171         }
172       };
173
174
175       ////////////////////////////////////////////////////////////////
176       class HalVolume_Impl
177       {
178       public:
179         LibHalVolume *vol;
180
181         HalVolume_Impl(LibHalVolume *v=NULL)
182           : vol(v)
183         {
184         }
185
186         ~HalVolume_Impl()
187         {
188           if( vol)
189             libhal_volume_free(vol);
190         }
191       };
192
193
194       ////////////////////////////////////////////////////////////////
195       HalContext_Impl::HalContext_Impl(bool monitorable)
196         : conn(NULL)
197         , hctx(NULL)
198         , pcon(false) // we allways use shared connections at the moment
199       {
200         HalError err;
201
202         if( pcon)
203           conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err.error);
204         else
205           conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err.error);
206         if( !conn) {
207           ZYPP_THROW(err.halException(
208             _("Unable to create dbus connection")
209           ));
210         }
211
212         if( monitorable)
213           dbus_connection_setup_with_g_main(conn, NULL);
214
215         hctx = libhal_ctx_new();
216         if( !hctx)
217         {
218           if( pcon)
219               dbus_connection_close(conn);
220           dbus_connection_unref(conn);
221           conn = NULL;
222
223           ZYPP_THROW(HalException(
224             _("libhal_ctx_new: Can't create libhal context")
225           ));
226         }
227
228         if( !libhal_ctx_set_dbus_connection(hctx, conn))
229         {
230           libhal_ctx_free(hctx);
231           hctx = NULL;
232
233           if( pcon)
234             dbus_connection_close(conn);
235           dbus_connection_unref(conn);
236           conn = NULL;
237
238           ZYPP_THROW(HalException(
239             _("libhal_set_dbus_connection: Can't set dbus connection")
240           ));
241         }
242
243         if( !libhal_ctx_init(hctx, &err.error))
244         {
245           libhal_ctx_free(hctx);
246           hctx = NULL;
247
248           if( pcon)
249             dbus_connection_close(conn);
250           dbus_connection_unref(conn);
251           conn = NULL;
252
253           ZYPP_THROW(err.halException(
254             _("Unable to initalize HAL context -- hald not running?")
255           ));
256         }
257       }
258
259       // -------------------------------------------------------------
260       HalContext_Impl::~HalContext_Impl()
261       {
262         if( hctx)
263         {
264           HalError err;
265           libhal_ctx_shutdown(hctx, &err.error);
266           libhal_ctx_free( hctx);
267         }
268         if( conn)
269         {
270           if( pcon)
271             dbus_connection_close(conn);
272           dbus_connection_unref(conn);
273         }
274       }
275
276
277       ////////////////////////////////////////////////////////////////
278       HalContext::HalContext(bool autoconnect)
279         : h_impl( NULL)
280       {
281         MutexLock lock(g_Mutex);
282
283         if( autoconnect)
284           h_impl.reset( new HalContext_Impl());
285       }
286
287       // -------------------------------------------------------------
288       HalContext::HalContext(const HalContext &context)
289         : h_impl( NULL)
290       {
291         MutexLock lock(g_Mutex);
292
293         zypp::RW_pointer<HalContext_Impl>(context.h_impl).swap(h_impl);
294       }
295
296       // -------------------------------------------------------------
297       HalContext::HalContext(bool autoconnect, bool monitorable)
298         : h_impl( NULL)
299       {
300         MutexLock lock(g_Mutex);
301
302         if( autoconnect)
303           h_impl.reset( new HalContext_Impl(monitorable));
304       }
305
306       // -------------------------------------------------------------
307       HalContext::~HalContext()
308       {
309         MutexLock  lock(g_Mutex);
310
311         h_impl.reset();
312       }
313
314       // --------------------------------------------------------------
315       HalContext &
316       HalContext::operator=(const HalContext &context)
317       {
318         MutexLock  lock(g_Mutex);
319
320         if( this == &context)
321           return *this;
322
323         zypp::RW_pointer<HalContext_Impl>(context.h_impl).swap(h_impl);
324         return *this;
325       }
326
327       // --------------------------------------------------------------
328       HalContext::operator HalContext::bool_type() const
329       {
330         MutexLock  lock(g_Mutex);
331
332         return h_impl;
333       }
334
335       // --------------------------------------------------------------
336       void
337       HalContext::connect()
338       {
339         MutexLock lock(g_Mutex);
340
341         if( !h_impl)
342           h_impl.reset( new HalContext_Impl());
343       }
344
345       // --------------------------------------------------------------
346       std::vector<std::string>
347       HalContext::getAllDevices() const
348       {
349         MutexLock  lock(g_Mutex);
350         VERIFY_CONTEXT(h_impl);
351
352         HalError   err;
353         char     **names;
354         int        count = 0;
355
356         names = libhal_get_all_devices( h_impl->hctx, &count, &err.error);
357         if( !names)
358         {
359           ZYPP_THROW(err.halException());
360         }
361
362         std::vector<std::string> ret(names, names + count);
363         libhal_free_string_array(names);
364         return ret;
365       }
366
367       // --------------------------------------------------------------
368       HalDrive
369       HalContext::getDriveFromUDI(const std::string &udi) const
370       {
371         MutexLock  lock(g_Mutex);
372         VERIFY_CONTEXT(h_impl);
373
374         LibHalDrive *drv = libhal_drive_from_udi(h_impl->hctx, udi.c_str());
375         if( drv != NULL)
376           return HalDrive(new HalDrive_Impl( h_impl, drv));
377         else
378           return HalDrive();
379       }
380
381       // --------------------------------------------------------------
382       HalVolume
383       HalContext::getVolumeFromUDI(const std::string &udi) const
384       {
385         MutexLock  lock(g_Mutex);
386         VERIFY_CONTEXT(h_impl);
387
388         LibHalVolume *vol = libhal_volume_from_udi(h_impl->hctx, udi.c_str());
389         if( vol)
390           return HalVolume( new HalVolume_Impl(vol));
391         else
392           return HalVolume();
393       }
394
395       // --------------------------------------------------------------
396       HalVolume
397       HalContext::getVolumeFromDeviceFile(const std::string &device_file) const
398       {
399         MutexLock  lock(g_Mutex);
400         VERIFY_CONTEXT(h_impl);
401
402         LibHalVolume *vol = libhal_volume_from_device_file(h_impl->hctx,
403                                                            device_file.c_str());
404         if( vol)
405           return HalVolume( new HalVolume_Impl(vol));
406         else
407           return HalVolume();
408       }
409
410       // --------------------------------------------------------------
411       std::vector<std::string>
412       HalContext::findDevicesByCapability(const std::string &capability) const
413       {
414         MutexLock  lock(g_Mutex);
415         VERIFY_CONTEXT(h_impl);
416
417         HalError   err;
418         char     **names;
419         int        count = 0;
420
421         names = libhal_find_device_by_capability(h_impl->hctx,
422                                                  capability.c_str(),
423                                                  &count, &err.error);
424         if( !names)
425         {
426           ZYPP_THROW(err.halException());
427         }
428
429         std::vector<std::string> ret(names, names + count);
430         libhal_free_string_array(names);
431         return ret;
432       }
433
434       // --------------------------------------------------------------
435       bool
436       HalContext::getDevicePropertyBool  (const std::string &udi,
437                                           const std::string &key) const
438       {
439         MutexLock  lock(g_Mutex);
440         VERIFY_CONTEXT(h_impl);
441
442         HalError      err;
443         dbus_bool_t   ret;
444
445         ret = libhal_device_get_property_bool  (h_impl->hctx,
446                                                 udi.c_str(),
447                                                 key.c_str(),
448                                                 &err.error);
449         if( err.isSet())
450         {
451           ZYPP_THROW(err.halException());
452         }
453         return ret;
454       }
455
456       // --------------------------------------------------------------
457       int32_t
458       HalContext::getDevicePropertyInt32 (const std::string &udi,
459                                           const std::string &key) const
460       {
461         MutexLock  lock(g_Mutex);
462         VERIFY_CONTEXT(h_impl);
463
464         HalError      err;
465         dbus_int32_t  ret;
466
467         ret = libhal_device_get_property_int   (h_impl->hctx,
468                                                 udi.c_str(),
469                                                 key.c_str(),
470                                                 &err.error);
471         if( err.isSet())
472         {
473           ZYPP_THROW(err.halException());
474         }
475         return ret;
476       }
477
478       // --------------------------------------------------------------
479       uint64_t
480       HalContext::getDevicePropertyUInt64(const std::string &udi,
481                                           const std::string &key) const
482       {
483         MutexLock  lock(g_Mutex);
484         VERIFY_CONTEXT(h_impl);
485
486         HalError      err;
487         dbus_uint64_t ret;
488
489         ret = libhal_device_get_property_uint64(h_impl->hctx,
490                                                 udi.c_str(),
491                                                 key.c_str(),
492                                                 &err.error);
493         if( err.isSet())
494         {
495           ZYPP_THROW(err.halException());
496         }
497         return ret;
498       }
499
500       // --------------------------------------------------------------
501       double
502       HalContext::getDevicePropertyDouble(const std::string &udi,
503                                           const std::string &key) const
504       {
505         MutexLock  lock(g_Mutex);
506         VERIFY_CONTEXT(h_impl);
507
508         HalError      err;
509         double        ret;
510
511         ret = libhal_device_get_property_bool  (h_impl->hctx,
512                                                 udi.c_str(),
513                                                 key.c_str(),
514                                                 &err.error);
515         if( err.isSet())
516         {
517           ZYPP_THROW(err.halException());
518         }
519         return ret;
520       }
521
522
523       // --------------------------------------------------------------
524       std::string
525       HalContext::getDevicePropertyString(const std::string &udi,
526                                           const std::string &key) const
527       {
528         MutexLock  lock(g_Mutex);
529         VERIFY_CONTEXT(h_impl);
530
531         HalError      err;
532         std::string   ret;
533         char         *ptr;
534
535         ptr = libhal_device_get_property_string(h_impl->hctx,
536                                                 udi.c_str(),
537                                                 key.c_str(),
538                                                 &err.error);
539         if( err.isSet())
540         {
541           ZYPP_THROW(err.halException());
542         }
543         if( ptr != NULL)
544         {
545           ret = ptr;
546           free(ptr);
547         }
548         return ret;
549       }
550
551       // --------------------------------------------------------------
552       void
553       HalContext::setDevicePropertyBool  (const std::string &udi,
554                                           const std::string &key,
555                                           bool               value)
556       {
557         MutexLock  lock(g_Mutex);
558         VERIFY_CONTEXT(h_impl);
559
560         HalError      err;
561         dbus_bool_t   ret;
562
563         ret = libhal_device_set_property_bool  (h_impl->hctx,
564                                                 udi.c_str(),
565                                                 key.c_str(),
566                                                 value ? 1 : 0,
567                                                 &err.error);
568         if( !ret)
569         {
570           ZYPP_THROW(err.halException());
571         }
572       }
573
574       // --------------------------------------------------------------
575       void
576       HalContext::setDevicePropertyInt32 (const std::string &udi,
577                                           const std::string &key,
578                                           int32_t            value)
579       {
580         MutexLock  lock(g_Mutex);
581         VERIFY_CONTEXT(h_impl);
582
583         HalError      err;
584         dbus_bool_t   ret;
585
586         ret = libhal_device_set_property_int   (h_impl->hctx,
587                                                 udi.c_str(),
588                                                 key.c_str(),
589                                                 value,
590                                                 &err.error);
591         if( !ret)
592         {
593           ZYPP_THROW(err.halException());
594         }
595       }
596
597       // --------------------------------------------------------------
598       void
599       HalContext::setDevicePropertyUInt64(const std::string &udi,
600                                           const std::string &key,
601                                           uint64_t           value)
602       {
603         MutexLock  lock(g_Mutex);
604         VERIFY_CONTEXT(h_impl);
605
606         HalError      err;
607         dbus_bool_t   ret;
608
609         ret = libhal_device_set_property_uint64(h_impl->hctx,
610                                                 udi.c_str(),
611                                                 key.c_str(),
612                                                 value,
613                                                 &err.error);
614         if( !ret)
615         {
616           ZYPP_THROW(err.halException());
617         }
618       }
619
620       // --------------------------------------------------------------
621       void
622       HalContext::setDevicePropertyDouble(const std::string &udi,
623                                           const std::string &key,
624                                           double             value)
625       {
626         MutexLock  lock(g_Mutex);
627         VERIFY_CONTEXT(h_impl);
628
629         HalError      err;
630         dbus_bool_t   ret;
631
632         ret = libhal_device_set_property_double(h_impl->hctx,
633                                                 udi.c_str(),
634                                                 key.c_str(),
635                                                 value,
636                                                 &err.error);
637         if( !ret)
638         {
639           ZYPP_THROW(err.halException());
640         }
641       }
642
643       // --------------------------------------------------------------
644       void
645       HalContext::setDevicePropertyString(const std::string &udi,
646                                           const std::string &key,
647                                           const std::string &value)
648       {
649         MutexLock  lock(g_Mutex);
650         VERIFY_CONTEXT(h_impl);
651
652         HalError      err;
653         dbus_bool_t   ret;
654
655         ret = libhal_device_set_property_string(h_impl->hctx,
656                                                 udi.c_str(),
657                                                 key.c_str(),
658                                                 value.c_str(),
659                                                 &err.error);
660         if( !ret)
661         {
662           ZYPP_THROW(err.halException());
663         }
664       }
665
666       // --------------------------------------------------------------
667       void
668       HalContext::removeDeviceProperty(const std::string &udi,
669                                        const std::string &key)
670       {
671         MutexLock  lock(g_Mutex);
672         VERIFY_CONTEXT(h_impl);
673
674         HalError      err;
675         dbus_bool_t   ret;
676
677         ret = libhal_device_remove_property(h_impl->hctx,
678                                             udi.c_str(),
679                                             key.c_str(),
680                                             &err.error);
681         if( !ret)
682         {
683           ZYPP_THROW(err.halException());
684         }
685       }
686
687       ////////////////////////////////////////////////////////////////
688       HalDrive::HalDrive()
689         : d_impl( NULL)
690       {
691       }
692
693       // --------------------------------------------------------------
694       HalDrive::HalDrive(HalDrive_Impl *impl)
695         : d_impl( NULL)
696       {
697         MutexLock  lock(g_Mutex);
698
699         d_impl.reset(impl);
700       }
701
702       // --------------------------------------------------------------
703       HalDrive::HalDrive(const HalDrive &drive)
704         : d_impl( NULL)
705       {
706         MutexLock  lock(g_Mutex);
707
708         zypp::RW_pointer<HalDrive_Impl>(drive.d_impl).swap(d_impl);
709       }
710
711       // --------------------------------------------------------------
712       HalDrive::~HalDrive()
713       {
714         MutexLock  lock(g_Mutex);
715
716         d_impl.reset();
717       }
718
719       // --------------------------------------------------------------
720       HalDrive &
721       HalDrive::operator=(const HalDrive &drive)
722       {
723         MutexLock  lock(g_Mutex);
724
725         if( this == &drive)
726           return *this;
727
728         zypp::RW_pointer<HalDrive_Impl>(drive.d_impl).swap(d_impl);
729         return *this;
730       }
731
732       // --------------------------------------------------------------
733       HalDrive::operator HalDrive::bool_type() const
734       {
735         MutexLock  lock(g_Mutex);
736
737         return d_impl;
738       }
739
740       // --------------------------------------------------------------
741       std::string
742       HalDrive::getUDI() const
743       {
744         MutexLock  lock(g_Mutex);
745         VERIFY_DRIVE(d_impl);
746
747         const char *ptr = libhal_drive_get_udi(d_impl->drv);
748         return std::string(ptr ? ptr : "");
749       }
750
751       // --------------------------------------------------------------
752       std::string
753       HalDrive::getTypeName() const
754       {
755         MutexLock  lock(g_Mutex);
756         VERIFY_DRIVE(d_impl);
757
758         const char *ptr = libhal_drive_get_type_textual(d_impl->drv);
759         return std::string(ptr ? ptr : "");
760       }
761
762       // --------------------------------------------------------------
763       std::string
764       HalDrive::getDeviceFile() const
765       {
766         MutexLock  lock(g_Mutex);
767         VERIFY_DRIVE(d_impl);
768
769         return std::string(libhal_drive_get_device_file(d_impl->drv));
770       }
771
772       // --------------------------------------------------------------
773       unsigned int
774       HalDrive::getDeviceMajor() const
775       {
776         MutexLock  lock(g_Mutex);
777         VERIFY_DRIVE(d_impl);
778
779         return libhal_drive_get_device_major(d_impl->drv);
780       }
781
782       // --------------------------------------------------------------
783       unsigned int
784       HalDrive::getDeviceMinor() const
785       {
786         MutexLock  lock(g_Mutex);
787         VERIFY_DRIVE(d_impl);
788
789         return libhal_drive_get_device_minor(d_impl->drv);
790       }
791
792       // --------------------------------------------------------------
793       bool
794       HalDrive::usesRemovableMedia() const
795       {
796         MutexLock  lock(g_Mutex);
797         VERIFY_DRIVE(d_impl);
798
799         return libhal_drive_uses_removable_media(d_impl->drv);
800       }
801
802       // --------------------------------------------------------------
803       std::vector<std::string>
804       HalDrive::getCdromCapabilityNames() const
805       {
806         MutexLock  lock(g_Mutex);
807         VERIFY_DRIVE(d_impl);
808
809         std::vector<std::string> ret;
810         LibHalDriveCdromCaps     caps;
811
812         /*
813         ** FIXME: there is no textual variant :-(
814         **        using property key names...
815         */
816         caps = libhal_drive_get_cdrom_caps(d_impl->drv);
817
818         if(caps & LIBHAL_DRIVE_CDROM_CAPS_CDROM)
819           ret.push_back("cdrom");
820         if(caps & LIBHAL_DRIVE_CDROM_CAPS_CDR)
821           ret.push_back("cdr");
822         if(caps & LIBHAL_DRIVE_CDROM_CAPS_CDRW)
823           ret.push_back("cdrw");
824         if(caps & LIBHAL_DRIVE_CDROM_CAPS_DVDRAM)
825           ret.push_back("dvdram");
826         if(caps & LIBHAL_DRIVE_CDROM_CAPS_DVDROM)
827           ret.push_back("dvd");
828         if(caps & LIBHAL_DRIVE_CDROM_CAPS_DVDR)
829           ret.push_back("dvdr");
830         if(caps & LIBHAL_DRIVE_CDROM_CAPS_DVDRW)
831           ret.push_back("dvdrw");
832         if(caps & LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSR)
833           ret.push_back("dvdplusr");
834         if(caps & LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSRW)
835           ret.push_back("dvdplusrw");
836         if(caps & LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSRDL)
837           ret.push_back("dvdplusrdl");
838
839         return ret;
840
841 #if 0
842         if( libhal_drive_get_type(d_impl->drv) != LIBHAL_DRIVE_TYPE_CDROM)
843           ZYPP_THROW(HalException(_("Not a CDROM drive")));
844
845         /*
846         ** FIXME: we use property keys matching
847         **          "storage.cdrom.cd*"
848         **          "storage.cdrom.dvd*"
849         ** but this may print other bool keys,
850         ** that are not CDROM caps.
851         */
852         LibHalPropertySet         *props;
853         HalError                   err;
854
855         props = libhal_device_get_all_properties(d_impl->hal->hctx,
856                                                  getUDI().c_str(),
857                                                  &err.error);
858         if( !props)
859           ZYPP_THROW(err.halException());
860
861         std::vector<std::string>   ret(1, getTypeName());
862         std::string                key;
863         std::string                dvd("storage.cdrom.dvd");
864         std::string                cd ("storage.cdrom.cd");
865
866         LibHalPropertySetIterator  it;
867         for(libhal_psi_init(&it, props);
868             libhal_psi_has_more(&it);
869             libhal_psi_next(&it))
870         {
871           if( libhal_psi_get_type(&it) == LIBHAL_PROPERTY_TYPE_BOOLEAN &&
872               libhal_psi_get_bool(&it))
873           {
874             key = libhal_psi_get_key(&it);
875             if( key.compare(0, cd.size(), cd) == 0)
876             {
877               ret.push_back(key.substr(sizeof("storage.cdrom.")-1));
878             }
879             else
880             if( key.compare(0, dvd.size(), dvd) == 0)
881             {
882               ret.push_back(key.substr(sizeof("storage.cdrom.")-1));
883             }
884           }
885         }
886         libhal_free_property_set(props);
887
888         return ret;
889 #endif
890       }
891
892       // --------------------------------------------------------------
893       std::vector<std::string>
894       HalDrive::findAllVolumes() const
895       {
896         MutexLock  lock(g_Mutex);
897         VERIFY_DRIVE(d_impl);
898
899         char     **names;
900         int        count = 0;
901
902         names = libhal_drive_find_all_volumes(d_impl->hal->hctx,
903                                               d_impl->drv,
904                                               &count);
905
906         std::vector<std::string> ret;
907         ret.assign(names, names + count);
908         libhal_free_string_array(names);
909         return ret;
910       }
911
912
913       ////////////////////////////////////////////////////////////////
914       HalVolume::HalVolume()
915         : v_impl( NULL)
916       {}
917
918       HalVolume::HalVolume(HalVolume_Impl *impl)
919         : v_impl( NULL)
920       {
921         MutexLock  lock(g_Mutex);
922
923         v_impl.reset(impl);
924       }
925
926       // --------------------------------------------------------------
927       HalVolume::HalVolume(const HalVolume &volume)
928         : v_impl( NULL)
929       {
930         MutexLock  lock(g_Mutex);
931
932         zypp::RW_pointer<HalVolume_Impl>(volume.v_impl).swap(v_impl);
933       }
934
935       // --------------------------------------------------------------
936       HalVolume::~HalVolume()
937       {
938         MutexLock  lock(g_Mutex);
939
940         v_impl.reset();
941       }
942
943       // --------------------------------------------------------------
944       HalVolume &
945       HalVolume::operator=(const HalVolume &volume)
946       {
947         MutexLock  lock(g_Mutex);
948
949         if( this == &volume)
950           return *this;
951
952         zypp::RW_pointer<HalVolume_Impl>(volume.v_impl).swap(v_impl);
953         return *this;
954       }
955
956       // --------------------------------------------------------------
957       HalVolume::operator HalVolume::bool_type() const
958       {
959         MutexLock  lock(g_Mutex);
960
961         return v_impl;
962       }
963
964       // --------------------------------------------------------------
965       std::string
966       HalVolume::getUDI() const
967       {
968         MutexLock  lock(g_Mutex);
969         VERIFY_VOLUME(v_impl);
970
971         const char *ptr = libhal_volume_get_udi(v_impl->vol);
972         return std::string(ptr ? ptr : "");
973       }
974
975       // --------------------------------------------------------------
976       std::string
977       HalVolume::getDeviceFile() const
978       {
979         MutexLock  lock(g_Mutex);
980         VERIFY_VOLUME(v_impl);
981
982         return std::string(libhal_volume_get_device_file(v_impl->vol));
983       }
984
985       // --------------------------------------------------------------
986       unsigned int
987       HalVolume::getDeviceMajor() const
988       {
989         MutexLock  lock(g_Mutex);
990         VERIFY_VOLUME(v_impl);
991
992         return libhal_volume_get_device_major(v_impl->vol);
993       }
994
995       // --------------------------------------------------------------
996       unsigned int
997       HalVolume::getDeviceMinor() const
998       {
999         MutexLock  lock(g_Mutex);
1000         VERIFY_VOLUME(v_impl);
1001
1002         return libhal_volume_get_device_minor(v_impl->vol);
1003       }
1004
1005       // --------------------------------------------------------------
1006       bool
1007       HalVolume::isDisc() const
1008       {
1009         MutexLock  lock(g_Mutex);
1010         VERIFY_VOLUME(v_impl);
1011
1012         return libhal_volume_is_disc(v_impl->vol);
1013       }
1014
1015       // --------------------------------------------------------------
1016       bool
1017       HalVolume::isPartition() const
1018       {
1019         MutexLock  lock(g_Mutex);
1020         VERIFY_VOLUME(v_impl);
1021
1022         return libhal_volume_is_partition(v_impl->vol);
1023       }
1024
1025       // --------------------------------------------------------------
1026       bool
1027       HalVolume::isMounted() const
1028       {
1029         MutexLock  lock(g_Mutex);
1030         VERIFY_VOLUME(v_impl);
1031
1032         return libhal_volume_is_mounted(v_impl->vol);
1033       }
1034
1035       // --------------------------------------------------------------
1036       std::string
1037       HalVolume::getFSType() const
1038       {
1039         MutexLock  lock(g_Mutex);
1040         VERIFY_VOLUME(v_impl);
1041
1042         return std::string( libhal_volume_get_fstype(v_impl->vol));
1043       }
1044
1045       // --------------------------------------------------------------
1046       std::string
1047       HalVolume::getFSUsage() const
1048       {
1049         MutexLock  lock(g_Mutex);
1050         VERIFY_VOLUME(v_impl);
1051
1052         LibHalVolumeUsage usage( libhal_volume_get_fsusage(v_impl->vol));
1053         std::string       ret;
1054         switch( usage)
1055         {
1056           case  LIBHAL_VOLUME_USAGE_MOUNTABLE_FILESYSTEM:
1057             ret = "filesystem";
1058           break;
1059           case LIBHAL_VOLUME_USAGE_PARTITION_TABLE:
1060             ret = "partitiontable";
1061           break;
1062           case LIBHAL_VOLUME_USAGE_RAID_MEMBER:
1063             return "raid";
1064           break;
1065           case LIBHAL_VOLUME_USAGE_CRYPTO:
1066             ret = "crypto";
1067           break;
1068           case LIBHAL_VOLUME_USAGE_UNKNOWN:
1069           default:
1070           break;
1071         }
1072         return ret;
1073       }
1074
1075       // --------------------------------------------------------------
1076       std::string
1077       HalVolume::getMountPoint() const
1078       {
1079         VERIFY_VOLUME(v_impl);
1080
1081         return std::string( libhal_volume_get_mount_point(v_impl->vol));
1082       }
1083
1084
1085       ////////////////////////////////////////////////////////////////
1086     } // namespace hal
1087     //////////////////////////////////////////////////////////////////
1088     //////////////////////////////////////////////////////////////////
1089   } // namespace target
1090   ////////////////////////////////////////////////////////////////////
1091   ////////////////////////////////////////////////////////////////////
1092 } // namespace zypp
1093 //////////////////////////////////////////////////////////////////////
1094 #else // FAKE_HAL
1095 #include <zypp/target/hal/HalContext.h>
1096 #include <zypp/target/hal/HalException.h>
1097 namespace zypp
1098 { ////////////////////////////////////////////////////////////////////
1099   ////////////////////////////////////////////////////////////////////
1100   namespace target
1101   { //////////////////////////////////////////////////////////////////
1102     //////////////////////////////////////////////////////////////////
1103     namespace hal
1104     { ////////////////////////////////////////////////////////////////
1105
1106       std::ostream &
1107       HalException::dumpOn( std::ostream & str ) const
1108       { return str; }
1109
1110       // --------------------------------------------------------------
1111       class HalContext_Impl
1112       {};
1113       class HalDrive_Impl
1114       {};
1115       class HalVolume_Impl
1116       {};
1117
1118       // --------------------------------------------------------------
1119       HalContext::HalContext(bool)
1120       {}
1121       HalContext::~HalContext()
1122       {}
1123       HalContext &
1124       HalContext::operator=(const HalContext &)
1125       { return *this; }
1126       HalContext::operator HalContext::bool_type() const
1127       { return 0; }
1128       void
1129       HalContext::connect()
1130       {}
1131       std::vector<std::string>
1132       HalContext::getAllDevices() const
1133       { return std::vector<std::string>(); }
1134       HalDrive
1135       HalContext::getDriveFromUDI(const std::string &) const
1136       { return HalDrive(); }
1137       HalVolume
1138       HalContext::getVolumeFromUDI(const std::string &) const
1139       { return HalVolume(); }
1140       HalVolume
1141       HalContext::getVolumeFromDeviceFile(const std::string &) const
1142       { return HalVolume(); }
1143       std::vector<std::string>
1144       HalContext::findDevicesByCapability(const std::string &) const
1145       { return std::vector<std::string>(); }
1146       bool
1147       HalContext::getDevicePropertyBool(const std::string &, const std::string &) const
1148       { return false; }
1149       void
1150       HalContext::setDevicePropertyBool  (const std::string &, const std::string &, bool value)
1151       {}
1152       void
1153       HalContext::removeDeviceProperty(const std::string &, const std::string &)
1154       {}
1155       std::string
1156       HalContext::getDevicePropertyString(const std::string &, const std::string &) const
1157       { return ""; }
1158       // --------------------------------------------------------------
1159       HalDrive::HalDrive()
1160       {}
1161       HalDrive::~HalDrive()
1162       {}
1163       HalDrive &
1164       HalDrive::operator=(const HalDrive &)
1165       { return *this; }
1166       HalDrive::operator HalDrive::bool_type() const
1167       { return 0; }
1168       std::string
1169       HalDrive::getUDI() const
1170       { return std::string(); }
1171       std::string
1172       HalDrive::getTypeName() const
1173       { return std::string(); }
1174       std::string
1175       HalDrive::getDeviceFile() const
1176       { return std::string(); }
1177       unsigned int
1178       HalDrive::getDeviceMinor() const
1179       { return 0; }
1180       unsigned int
1181       HalDrive::getDeviceMajor() const
1182       { return 0; }
1183       bool
1184       HalDrive::usesRemovableMedia() const
1185       { return false; }
1186       std::vector<std::string>
1187       HalDrive::getCdromCapabilityNames() const
1188       { return std::vector<std::string>(); }
1189       std::vector<std::string>
1190       HalDrive::findAllVolumes() const
1191       { return std::vector<std::string>(); }
1192
1193       // --------------------------------------------------------------
1194       HalVolume::HalVolume()
1195       {}
1196       HalVolume::~HalVolume()
1197       {}
1198       HalVolume &
1199       HalVolume::operator=(const HalVolume &)
1200       { return *this; }
1201       HalVolume::operator HalVolume::bool_type() const
1202       { return 0; }
1203       std::string
1204       HalVolume::getUDI() const
1205       { return std::string(); }
1206       std::string
1207       HalVolume::getDeviceFile() const
1208       { return std::string(); }
1209       unsigned int
1210       HalVolume::getDeviceMinor() const
1211       { return 0; }
1212       unsigned int
1213       HalVolume::getDeviceMajor() const
1214       { return 0; }
1215       bool
1216       HalVolume::isDisc() const
1217       { return false; }
1218       bool
1219       HalVolume::isPartition() const
1220       { return false; }
1221       bool
1222       HalVolume::isMounted() const
1223       { return false; }
1224       std::string
1225       HalVolume::getFSType() const
1226       { return std::string(); }
1227       std::string
1228       HalVolume::getFSUsage() const
1229       { return std::string(); }
1230       std::string
1231       HalVolume::getMountPoint() const
1232       { return std::string(); }
1233
1234       ////////////////////////////////////////////////////////////////
1235     } // namespace hal
1236     //////////////////////////////////////////////////////////////////
1237     //////////////////////////////////////////////////////////////////
1238   } // namespace target
1239   ////////////////////////////////////////////////////////////////////
1240   ////////////////////////////////////////////////////////////////////
1241 } // namespace zypp
1242 //////////////////////////////////////////////////////////////////////
1243 #endif // FAKE_HAL
1244
1245 /*
1246 ** vim: set ts=2 sts=2 sw=2 ai et:
1247 */