From 9d82186be12d5c73bf1ec7c4c4316fdd1f181641 Mon Sep 17 00:00:00 2001 From: Marius Tomaschewski Date: Fri, 31 Mar 2006 11:30:52 +0000 Subject: [PATCH] - Implemented several hal get/set/removeDeviceProperty wrappers - Improved HalException to allow to fetch HAL/DBUS error componets --- package/libzypp.changes | 7 + zypp/target/hal/HalContext.cc | 284 +++++++++++++++++++++++++++++++++++++++-- zypp/target/hal/HalContext.h | 51 +++++++- zypp/target/hal/HalException.h | 40 +++++- 4 files changed, 368 insertions(+), 14 deletions(-) diff --git a/package/libzypp.changes b/package/libzypp.changes index cc68a71..e43ed9e 100644 --- a/package/libzypp.changes +++ b/package/libzypp.changes @@ -1,4 +1,11 @@ ------------------------------------------------------------------- +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 - honor subscription status of catalogs (#162350) diff --git a/zypp/target/hal/HalContext.cc b/zypp/target/hal/HalContext.cc index e67fb27..e88fe31 100644 --- a/zypp/target/hal/HalContext.cc +++ b/zypp/target/hal/HalContext.cc @@ -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 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 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 ret(1, getTypeName()); std::string key; diff --git a/zypp/target/hal/HalContext.h b/zypp/target/hal/HalContext.h index 6a5cd6f..4d78684 100644 --- a/zypp/target/hal/HalContext.h +++ b/zypp/target/hal/HalContext.h @@ -17,7 +17,7 @@ #include #include #include - +#include ////////////////////////////////////////////////////////////////////// namespace zypp @@ -111,6 +111,55 @@ namespace zypp std::vector 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); diff --git a/zypp/target/hal/HalException.h b/zypp/target/hal/HalException.h index d44272e..6807b4e 100644 --- a/zypp/target/hal/HalException.h +++ b/zypp/target/hal/HalException.h @@ -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; }; -- 2.7.4