MediaCIFS: get rid of dependency to zypp::Target
[platform/upstream/libzypp.git] / zypp / target / hal / HalContext.cc
index 70f8bc8..c80367d 100644 (file)
@@ -6,20 +6,43 @@
 |                         /_____||_| |_| |_|                           |
 |                                                                      |
 \---------------------------------------------------------------------*/
-/** \file zypp/media/Hal.cc
+/** \file zypp/target/hal/HalContext.cc
  *
  *  \brief Hardware abstaction layer library wrapper implementation.
  */
-#ifndef FAKE_HAL // disables zypp's HAL dependency
+#include <zypp/target/hal/HalException.h>
+//////////////////////////////////////////////////////////////////////
+namespace zypp
+{ ////////////////////////////////////////////////////////////////////
+  ////////////////////////////////////////////////////////////////////
+  namespace target
+  { //////////////////////////////////////////////////////////////////
+    //////////////////////////////////////////////////////////////////
+    namespace hal
+    { ////////////////////////////////////////////////////////////////
+      NoHalException::NoHalException()
+        : Exception(_("Sorry, but this version of libzypp was built without HAL support."))
+      {}
+      ////////////////////////////////////////////////////////////////
+    } // namespace hal
+    //////////////////////////////////////////////////////////////////
+    //////////////////////////////////////////////////////////////////
+  } // namespace target
+  ////////////////////////////////////////////////////////////////////
+  ////////////////////////////////////////////////////////////////////
+} // namespace zypp
+//////////////////////////////////////////////////////////////////////
+
+#ifndef NO_HAL // disables zypp's HAL dependency
 
 #include <zypp/target/hal/HalContext.h>
 #include <zypp/thread/Mutex.h>
 #include <zypp/thread/MutexLock.h>
 #include <zypp/base/NonCopyable.h>
 #include <zypp/base/Logger.h>
+#include <zypp/base/String.h>
+#include <zypp/base/Gettext.h>
 
-#include <dbus/dbus-glib-lowlevel.h>
-#include <dbus/dbus-glib.h>
 #include <hal/libhal.h>
 #include <hal/libhal-storage.h>
 
@@ -70,11 +93,15 @@ namespace zypp
             return dbus_error_is_set(&error);
           }
 
-          inline HalException halException() const
+          inline HalException halException(const std::string &msg = std::string()) const
           {
-            if( error.name != NULL && error.message != NULL) {
+            if( isSet() && error.name != NULL && error.message != NULL) {
               return HalException(error.name, error.message);
-            } else {
+            }
+            else if( !msg.empty()) {
+              return HalException(msg);
+            }
+            else {
               return HalException();
             }
           }
@@ -87,7 +114,7 @@ namespace zypp
         {
           if( !h)
           {
-            ZYPP_THROW(HalException("HalContext not connected"));
+            ZYPP_THROW(HalException(_("HalContext not connected")));
           }
         }
 
@@ -97,7 +124,7 @@ namespace zypp
         {
           if( !d)
           {
-            ZYPP_THROW(HalException("HalDrive not initialized"));
+            ZYPP_THROW(HalException(_("HalDrive not initialized")));
           }
         }
 
@@ -107,7 +134,7 @@ namespace zypp
         {
           if( !v)
           {
-            ZYPP_THROW(HalException("HalVolume not initialized"));
+            ZYPP_THROW(HalException(_("HalVolume not initialized")));
           }
         }
 
@@ -121,6 +148,8 @@ namespace zypp
       {
         if(!e_name.empty() && !e_msg.empty())
           return str << msg() << ": " << e_msg << " (" << e_name << ")";
+        else if(!e_msg.empty())
+          return str << msg() << ": " << e_msg;
         else
           return str << msg();
       }
@@ -129,11 +158,12 @@ namespace zypp
       class HalContext_Impl
       {
       public:
-        HalContext_Impl(bool monitorable = false);
+        HalContext_Impl();
         ~HalContext_Impl();
 
         DBusConnection *conn;
         LibHalContext  *hctx;
+        bool            pcon; // private connection
       };
 
 
@@ -183,29 +213,33 @@ namespace zypp
 
 
       ////////////////////////////////////////////////////////////////
-      HalContext_Impl::HalContext_Impl(bool monitorable)
+      HalContext_Impl::HalContext_Impl()
         : conn(NULL)
         , hctx(NULL)
+        , pcon(false) // we allways use shared connections at the moment
       {
         HalError err;
 
-        conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err.error);
+        if( pcon)
+          conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err.error);
+        else
+          conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err.error);
         if( !conn) {
-          ZYPP_THROW(err.halException());
+          ZYPP_THROW(err.halException(
+            _("Unable to create dbus connection")
+          ));
         }
 
-        if( monitorable)
-          dbus_connection_setup_with_g_main(conn, NULL);
-
         hctx = libhal_ctx_new();
         if( !hctx)
         {
-          dbus_connection_disconnect(conn);
+          if( pcon)
+              dbus_connection_close(conn);
           dbus_connection_unref(conn);
           conn = NULL;
 
           ZYPP_THROW(HalException(
-            "libhal_ctx_new: Can't create libhal context"
+            _("libhal_ctx_new: Can't create libhal context")
           ));
         }
 
@@ -214,12 +248,13 @@ namespace zypp
           libhal_ctx_free(hctx);
           hctx = NULL;
 
-          dbus_connection_disconnect(conn);
+          if( pcon)
+            dbus_connection_close(conn);
           dbus_connection_unref(conn);
           conn = NULL;
 
           ZYPP_THROW(HalException(
-            "libhal_set_dbus_connection: Can't set dbus connection"
+            _("libhal_set_dbus_connection: Can't set dbus connection")
           ));
         }
 
@@ -228,11 +263,14 @@ namespace zypp
           libhal_ctx_free(hctx);
           hctx = NULL;
 
-          dbus_connection_disconnect(conn);
+          if( pcon)
+            dbus_connection_close(conn);
           dbus_connection_unref(conn);
           conn = NULL;
 
-          ZYPP_THROW(err.halException());
+          ZYPP_THROW(err.halException(
+            _("Unable to initalize HAL context -- hald not running?")
+          ));
         }
       }
 
@@ -247,7 +285,8 @@ namespace zypp
         }
         if( conn)
         {
-          dbus_connection_close(conn);
+          if( pcon)
+            dbus_connection_close(conn);
           dbus_connection_unref(conn);
         }
       }
@@ -273,16 +312,6 @@ namespace zypp
       }
 
       // -------------------------------------------------------------
-      HalContext::HalContext(bool autoconnect, bool monitorable)
-        : h_impl( NULL)
-      {
-        MutexLock lock(g_Mutex);
-
-        if( autoconnect)
-          h_impl.reset( new HalContext_Impl(monitorable));
-      }
-
-      // -------------------------------------------------------------
       HalContext::~HalContext()
       {
         MutexLock  lock(g_Mutex);
@@ -819,7 +848,7 @@ namespace zypp
 
 #if 0
         if( libhal_drive_get_type(d_impl->drv) != LIBHAL_DRIVE_TYPE_CDROM)
-          ZYPP_THROW(HalException("Not a CDROM drive"));
+          ZYPP_THROW(HalException(_("Not a CDROM drive")));
 
         /*
         ** FIXME: we use property keys matching
@@ -1070,7 +1099,7 @@ namespace zypp
   ////////////////////////////////////////////////////////////////////
 } // namespace zypp
 //////////////////////////////////////////////////////////////////////
-#else // FAKE_HAL
+#else // NO_HAL
 #include <zypp/target/hal/HalContext.h>
 #include <zypp/target/hal/HalException.h>
 namespace zypp
@@ -1096,7 +1125,7 @@ namespace zypp
 
       // --------------------------------------------------------------
       HalContext::HalContext(bool)
-      {}
+      { ZYPP_THROW( NoHalException() ); }
       HalContext::~HalContext()
       {}
       HalContext &
@@ -1136,7 +1165,7 @@ namespace zypp
       { return ""; }
       // --------------------------------------------------------------
       HalDrive::HalDrive()
-      {}
+      { ZYPP_THROW( NoHalException() ); }
       HalDrive::~HalDrive()
       {}
       HalDrive &
@@ -1171,7 +1200,7 @@ namespace zypp
 
       // --------------------------------------------------------------
       HalVolume::HalVolume()
-      {}
+      { ZYPP_THROW( NoHalException() ); }
       HalVolume::~HalVolume()
       {}
       HalVolume &
@@ -1219,7 +1248,7 @@ namespace zypp
   ////////////////////////////////////////////////////////////////////
 } // namespace zypp
 //////////////////////////////////////////////////////////////////////
-#endif // FAKE_HAL
+#endif // NO_HAL
 
 /*
 ** vim: set ts=2 sts=2 sw=2 ai et: