- Implemented several hal get/set/removeDeviceProperty wrappers
authorMarius Tomaschewski <mt@suse.de>
Fri, 31 Mar 2006 11:30:52 +0000 (11:30 +0000)
committerMarius Tomaschewski <mt@suse.de>
Fri, 31 Mar 2006 11:30:52 +0000 (11:30 +0000)
- Improved HalException to allow to fetch HAL/DBUS error componets

package/libzypp.changes
zypp/target/hal/HalContext.cc
zypp/target/hal/HalContext.h
zypp/target/hal/HalException.h

index cc68a719a3639f2ff5455d9cb9aa4059308a4086..e43ed9e02d86b5f0f78e65173f3d3393dde50d6b 100644 (file)
@@ -1,3 +1,10 @@
+-------------------------------------------------------------------
+Fri Mar 31 13:27:09 CEST 2006 - mt@suse.de
+
+- Implemented several hal get/set/removeDeviceProperty wrappers
+- Improved HalException to allow to fetch HAL/DBUS error componets
+- rev 2830
+
 -------------------------------------------------------------------
 Fri Mar 31 12:44:25 CEST 2006 - kkaempf@suse.de
 
index e67fb274a2d9ee97c400b6ef2d38c2b84235352d..e88fe3195b2b712e6450f517e75bed5ff227eb99 100644 (file)
@@ -65,14 +65,17 @@ namespace zypp
           HalError()  { dbus_error_init(&error); }
           ~HalError() { dbus_error_free(&error); }
 
-          inline std::string toString() const
+          inline bool         isSet() const
+          {
+            return dbus_error_is_set(&error);
+          }
+
+          inline HalException halException() const
           {
             if( error.name != NULL && error.message != NULL) {
-              return std::string(error.name) +
-                     std::string(": ")       +
-                     std::string(error.message);
+              return HalException(error.name, error.message);
             } else {
-              return std::string();
+              return HalException();
             }
           }
         };
@@ -112,6 +115,15 @@ namespace zypp
       } // anonymous
       ////////////////////////////////////////////////////////////////
 
+      ////////////////////////////////////////////////////////////////
+      std::ostream &
+      HalException::dumpOn( std::ostream & str ) const
+      {
+        if(!e_name.empty() && !e_msg.empty())
+          return str << msg() << ": " << e_msg << " (" << e_name << ")";
+        else
+          return str << msg();
+      }
 
       ////////////////////////////////////////////////////////////////
       class HalContext_Impl
@@ -179,7 +191,7 @@ namespace zypp
 
         conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err.error);
         if( !conn) {
-          ZYPP_THROW(HalException(err.toString()));
+          ZYPP_THROW(err.halException());
         }
 
         if( monitorable)
@@ -220,7 +232,7 @@ namespace zypp
           dbus_connection_unref(conn);
           conn = NULL;
 
-          ZYPP_THROW(HalException(err.toString()));
+          ZYPP_THROW(err.halException());
         }
       }
 
@@ -323,7 +335,7 @@ namespace zypp
         names = libhal_get_all_devices( h_impl->hctx, &count, &err.error);
         if( !names)
         {
-          ZYPP_THROW(HalException(err.toString()));
+          ZYPP_THROW(err.halException());
         }
 
         std::vector<std::string> ret(names, names + count);
@@ -390,7 +402,7 @@ namespace zypp
                                                  &count, &err.error);
         if( !names)
         {
-          ZYPP_THROW(HalException(err.toString()));
+          ZYPP_THROW(err.halException());
         }
 
         std::vector<std::string> ret(names, names + count);
@@ -398,6 +410,258 @@ namespace zypp
         return ret;
       }
 
+      // --------------------------------------------------------------
+      bool
+      HalContext::getDevicePropertyBool  (const std::string &udi,
+                                          const std::string &key) const
+      {
+        MutexLock  lock(g_Mutex);
+        VERIFY_CONTEXT(h_impl);
+
+        HalError      err;
+        dbus_bool_t   ret;
+
+        ret = libhal_device_get_property_bool  (h_impl->hctx,
+                                                udi.c_str(),
+                                                key.c_str(),
+                                                &err.error);
+        if( err.isSet())
+        {
+          ZYPP_THROW(err.halException());
+        }
+        return ret;
+      }
+
+      // --------------------------------------------------------------
+      int32_t
+      HalContext::getDevicePropertyInt32 (const std::string &udi,
+                                          const std::string &key) const
+      {
+        MutexLock  lock(g_Mutex);
+        VERIFY_CONTEXT(h_impl);
+
+        HalError      err;
+        dbus_int32_t  ret;
+
+        ret = libhal_device_get_property_int   (h_impl->hctx,
+                                                udi.c_str(),
+                                                key.c_str(),
+                                                &err.error);
+        if( err.isSet())
+        {
+          ZYPP_THROW(err.halException());
+        }
+        return ret;
+      }
+
+      // --------------------------------------------------------------
+      uint64_t
+      HalContext::getDevicePropertyUInt64(const std::string &udi,
+                                          const std::string &key) const
+      {
+        MutexLock  lock(g_Mutex);
+        VERIFY_CONTEXT(h_impl);
+
+        HalError      err;
+        dbus_uint64_t ret;
+
+        ret = libhal_device_get_property_uint64(h_impl->hctx,
+                                                udi.c_str(),
+                                                key.c_str(),
+                                                &err.error);
+        if( err.isSet())
+        {
+          ZYPP_THROW(err.halException());
+        }
+        return ret;
+      }
+
+      // --------------------------------------------------------------
+      double
+      HalContext::getDevicePropertyDouble(const std::string &udi,
+                                          const std::string &key) const
+      {
+        MutexLock  lock(g_Mutex);
+        VERIFY_CONTEXT(h_impl);
+
+        HalError      err;
+        double        ret;
+
+        ret = libhal_device_get_property_bool  (h_impl->hctx,
+                                                udi.c_str(),
+                                                key.c_str(),
+                                                &err.error);
+        if( err.isSet())
+        {
+          ZYPP_THROW(err.halException());
+        }
+        return ret;
+      }
+
+
+      // --------------------------------------------------------------
+      std::string
+      HalContext::getDevicePropertyString(const std::string &udi,
+                                          const std::string &key) const
+      {
+        MutexLock  lock(g_Mutex);
+        VERIFY_CONTEXT(h_impl);
+
+        HalError      err;
+        std::string   ret;
+        char         *ptr;
+
+        ptr = libhal_device_get_property_string(h_impl->hctx,
+                                                udi.c_str(),
+                                                key.c_str(),
+                                                &err.error);
+        if( err.isSet())
+        {
+          ZYPP_THROW(err.halException());
+        }
+        if( ptr != NULL)
+        {
+          ret = ptr;
+          free(ptr);
+        }
+        return ret;
+      }
+
+      // --------------------------------------------------------------
+      void
+      HalContext::setDevicePropertyBool  (const std::string &udi,
+                                          const std::string &key,
+                                          bool               value)
+      {
+        MutexLock  lock(g_Mutex);
+        VERIFY_CONTEXT(h_impl);
+
+        HalError      err;
+        dbus_bool_t   ret;
+        
+        ret = libhal_device_set_property_bool  (h_impl->hctx,
+                                                udi.c_str(),
+                                                key.c_str(),
+                                                value ? 1 : 0,
+                                                &err.error);
+        if( !ret)
+        {
+          ZYPP_THROW(err.halException());
+        }
+      }
+
+      // --------------------------------------------------------------
+      void
+      HalContext::setDevicePropertyInt32 (const std::string &udi,
+                                          const std::string &key,
+                                          int32_t            value)
+      {
+        MutexLock  lock(g_Mutex);
+        VERIFY_CONTEXT(h_impl);
+
+        HalError      err;
+        dbus_bool_t   ret;
+
+        ret = libhal_device_set_property_int   (h_impl->hctx,
+                                                udi.c_str(),
+                                                key.c_str(),
+                                                value,
+                                                &err.error);
+        if( !ret)
+        {
+          ZYPP_THROW(err.halException());
+        }
+      }
+
+      // --------------------------------------------------------------
+      void
+      HalContext::setDevicePropertyUInt64(const std::string &udi,
+                                          const std::string &key,
+                                          uint64_t           value)
+      {
+        MutexLock  lock(g_Mutex);
+        VERIFY_CONTEXT(h_impl);
+
+        HalError      err;
+        dbus_bool_t   ret;
+        
+        ret = libhal_device_set_property_uint64(h_impl->hctx,
+                                                udi.c_str(),
+                                                key.c_str(),
+                                                value,
+                                                &err.error);
+        if( !ret)
+        {
+          ZYPP_THROW(err.halException());
+        }
+      }
+
+      // --------------------------------------------------------------
+      void
+      HalContext::setDevicePropertyDouble(const std::string &udi,
+                                          const std::string &key,
+                                          double             value)
+      {
+        MutexLock  lock(g_Mutex);
+        VERIFY_CONTEXT(h_impl);
+
+        HalError      err;
+        dbus_bool_t   ret;
+        
+        ret = libhal_device_set_property_double(h_impl->hctx,
+                                                udi.c_str(),
+                                                key.c_str(),
+                                                value,
+                                                &err.error);
+        if( !ret)
+        {
+          ZYPP_THROW(err.halException());
+        }
+      }
+
+      // --------------------------------------------------------------
+      void
+      HalContext::setDevicePropertyString(const std::string &udi,
+                                          const std::string &key,
+                                          const std::string &value)
+      {
+        MutexLock  lock(g_Mutex);
+        VERIFY_CONTEXT(h_impl);
+
+        HalError      err;
+        dbus_bool_t   ret;
+        
+        ret = libhal_device_set_property_string(h_impl->hctx,
+                                                udi.c_str(),
+                                                key.c_str(),
+                                                value.c_str(),
+                                                &err.error);
+        if( !ret)
+        {
+          ZYPP_THROW(err.halException());
+        }
+      }
+
+      // --------------------------------------------------------------
+      void
+      HalContext::removeDeviceProperty(const std::string &udi,
+                                       const std::string &key)
+      {
+        MutexLock  lock(g_Mutex);
+        VERIFY_CONTEXT(h_impl);
+
+        HalError      err;
+        dbus_bool_t   ret;
+
+        ret = libhal_device_remove_property(h_impl->hctx,
+                                            udi.c_str(),
+                                            key.c_str(),
+                                            &err.error);
+        if( !ret)
+        {
+          ZYPP_THROW(err.halException());
+        }
+      }
 
       ////////////////////////////////////////////////////////////////
       HalDrive::HalDrive()
@@ -571,7 +835,7 @@ namespace zypp
                                                  getUDI().c_str(),
                                                  &err.error);
         if( !props)
-          ZYPP_THROW(HalException(err.toString()));
+          ZYPP_THROW(err.halException());
 
         std::vector<std::string>   ret(1, getTypeName());
         std::string                key;
index 6a5cd6f5d7ecff51313b32d0721548372dcb34b0..4d78684f7f1b6c4e59aa34bcf34357ea98201cfb 100644 (file)
@@ -17,7 +17,7 @@
 #include <zypp/base/PtrTypes.h>
 #include <string>
 #include <vector>
-
+#include <stdint.h>
 
 //////////////////////////////////////////////////////////////////////
 namespace zypp
@@ -111,6 +111,55 @@ namespace zypp
         std::vector<std::string>
         findDevicesByCapability(const std::string &capability) const;
 
+        bool
+        getDevicePropertyBool  (const std::string &udi,
+                                const std::string &key) const;
+
+        int32_t
+        getDevicePropertyInt32 (const std::string &udi,
+                                const std::string &key) const;
+
+        uint64_t
+        getDevicePropertyUInt64(const std::string &udi,
+                                const std::string &key) const;
+
+        double
+        getDevicePropertyDouble(const std::string &udi,
+                                const std::string &key) const;
+
+        std::string
+        getDevicePropertyString(const std::string &udi,
+                                const std::string &key) const;
+
+        void
+        setDevicePropertyBool  (const std::string &udi,
+                                const std::string &key,
+                                bool               value);
+
+        void
+        setDevicePropertyInt32 (const std::string &udi,
+                                const std::string &key,
+                                int32_t            value);
+
+        void
+        setDevicePropertyUInt64(const std::string &udi,
+                                const std::string &key,
+                                uint64_t           value);
+
+        void
+        setDevicePropertyDouble(const std::string &udi,
+                                const std::string &key,
+                                double             value);
+
+        void
+        setDevicePropertyString(const std::string &udi,
+                                const std::string &key,
+                                const std::string &value);
+
+        void
+        removeDeviceProperty(const std::string &udi,
+                             const std::string &key);
+
       private:
         //friend HalMonitor;
         HalContext(bool,bool);
index d44272e0dc82b30103801262d2de6e88e3004bca..6807b4e3176fcd14ef1efc1c6e75e07caade6e4a 100644 (file)
@@ -45,16 +45,50 @@ namespace zypp
           : zypp::Exception("Hal Exception")
         {}
 
-        /** Constructor taking hal error message.
+        /** Constructor taking complete hal error message.
+         * This constructor is used to generate custom error
+         * messages, in case, that no DBUS error is avaliable.
          * Use \ref ZYPP_THROW to throw exceptions.
          */
-        HalException(const std::string &msg)
-          : zypp::Exception(msg)
+        HalException(const std::string &msg_r)
+          : zypp::Exception("Hal Exception: " + msg_r)
+        {}
+
+        /** Constructor taking HAL (DBUS) error message components.
+         * Use \ref ZYPP_THROW to throw exceptions.
+         */
+        HalException(const std::string &err_name, const std::string &err_msg)
+          : zypp::Exception("Hal Exception")
+          , e_name(err_name)
+          , e_msg(err_msg)
         {}
 
         /** Destructor.
          */
         virtual ~HalException() throw() {};
+
+        /**
+         * \return The HAL (DBUS) error name component.
+         */
+        const std::string & errorName() const
+        {
+          return e_name;
+        }
+
+        /**
+         * \return The HAL (DBUS) error message component.
+         */
+        const std::string & errorMessage() const
+        {
+          return e_msg;
+        }
+
+      protected:
+        virtual std::ostream & dumpOn( std::ostream & str ) const;
+
+      private:
+        std::string e_name;
+        std::string e_msg;
       };