From 97e0438e1af1619cd130e6a371803a2aa05ec858 Mon Sep 17 00:00:00 2001
From: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
Date: Mon, 17 Mar 2008 15:29:52 -0400
Subject: [PATCH] Client error handling - Add a dbus signal so that daemon
 could notify the client

Does nothing for now
---
 sflphone-gtk/src/dbus.c                     | 11 +++++++++++
 src/audio/audiolayer.cpp                    |  9 +++++++--
 src/dbus/configurationmanager-glue.h        | 13 +++++++++++++
 src/dbus/configurationmanager-introspec.xml |  5 +++++
 src/managerimpl.cpp                         | 21 +++++++++++++++++++++
 src/managerimpl.h                           |  5 +++++
 6 files changed, 62 insertions(+), 2 deletions(-)

diff --git a/sflphone-gtk/src/dbus.c b/sflphone-gtk/src/dbus.c
index 68c2261377..db01375dbb 100644
--- a/sflphone-gtk/src/dbus.c
+++ b/sflphone-gtk/src/dbus.c
@@ -150,6 +150,13 @@ accounts_changed_cb (DBusGProxy *proxy,
   config_window_fill_account_list();
 }
 
+static void  
+error_alert(DBusGProxy *proxy,
+                  void * foo  )
+{
+  g_print ("Error notifying \n");
+}
+
 gboolean 
 dbus_connect ()
 {
@@ -244,6 +251,10 @@ dbus_connect ()
   dbus_g_proxy_connect_signal (configurationManagerProxy,
     "accountsChanged", G_CALLBACK(accounts_changed_cb), NULL, NULL);
    
+  dbus_g_proxy_add_signal (configurationManagerProxy, 
+    "errorAlert", G_TYPE_INVALID);
+  dbus_g_proxy_connect_signal (configurationManagerProxy,
+    "errorAlert", G_CALLBACK(error_alert), NULL, NULL);
   return TRUE;
 }
 
diff --git a/src/audio/audiolayer.cpp b/src/audio/audiolayer.cpp
index 55c43f9354..49d01cebc4 100644
--- a/src/audio/audiolayer.cpp
+++ b/src/audio/audiolayer.cpp
@@ -298,6 +298,7 @@ AudioLayer::isCaptureActive(void) {
   bool 
 AudioLayer::open_device(std::string pcm_p, std::string pcm_c, int flag)
 {
+  std::stringstream errMsg;
   int err;
   snd_pcm_hw_params_t* hwParams = NULL;
   snd_pcm_sw_params_t *swparams = NULL;
@@ -314,7 +315,9 @@ AudioLayer::open_device(std::string pcm_p, std::string pcm_c, int flag)
   {
     _debugAlsa(" Opening capture device %s\n", pcm_c.c_str());
     if(err = snd_pcm_open(&_CaptureHandle,  pcm_c.c_str(),  SND_PCM_STREAM_CAPTURE, 0) < 0){
-      _debugAlsa(" Error while opening capture device %s (%s)\n", pcm_c.c_str(), snd_strerror(err));
+      errMsg << " Error while opening capture device " << pcm_c.c_str() << " (" <<  snd_strerror(err) << ")";
+      _debugAlsa(" %s\n", errMsg.str().c_str());
+      setErrorMessage( errMsg.str() );
       return false;
     }
 
@@ -339,7 +342,9 @@ AudioLayer::open_device(std::string pcm_p, std::string pcm_c, int flag)
 
     _debugAlsa(" Opening playback device %s\n", pcm_p.c_str());
     if(err = snd_pcm_open(&_PlaybackHandle, pcm_p.c_str(),  SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK ) < 0){
-      _debugAlsa(" Error while opening playback device %s (%s)\n", pcm_p.c_str(), snd_strerror(err));
+      errMsg << " Error while opening playback device " << pcm_p.c_str() << " (" <<  snd_strerror(err) << ")";
+      _debugAlsa(" %s\n", errMsg.str().c_str());
+      setErrorMessage( errMsg.str() );
       return false;
     }
     if( err = snd_pcm_hw_params_malloc( &hwParams ) < 0 ) {
diff --git a/src/dbus/configurationmanager-glue.h b/src/dbus/configurationmanager-glue.h
index 811dac37ad..8fcd2b22e3 100644
--- a/src/dbus/configurationmanager-glue.h
+++ b/src/dbus/configurationmanager-glue.h
@@ -228,6 +228,11 @@ public:
         {
             { 0, 0, 0 }
         };
+        static ::DBus::IntrospectedArgument errorAlert_args[] = 
+        {
+            { "errMsg", "s", false },
+            { 0, 0, 0 }
+        };
         static ::DBus::IntrospectedMethod ConfigurationManager_methods[] = 
         {
             { "getAccountDetails", getAccountDetails_args },
@@ -268,6 +273,7 @@ public:
         {
             { "parametersChanged", parametersChanged_args },
             { "accountsChanged", accountsChanged_args },
+            { "errorAlert", errorAlert_args },
             { 0, 0 }
         };
         static ::DBus::IntrospectedProperty ConfigurationManager_properties[] = 
@@ -344,6 +350,13 @@ public:
         ::DBus::SignalMessage sig("accountsChanged");
         emit_signal(sig);
     }
+    void errorAlert( const ::DBus::String& arg1 )
+    {
+        ::DBus::SignalMessage sig("errorAlert");
+        ::DBus::MessageIter wi = sig.writer();
+        wi << arg1;
+        emit_signal(sig);
+    }
 
 private:
 
diff --git a/src/dbus/configurationmanager-introspec.xml b/src/dbus/configurationmanager-introspec.xml
index 2ea00b91c3..39fd2c01db 100644
--- a/src/dbus/configurationmanager-introspec.xml
+++ b/src/dbus/configurationmanager-introspec.xml
@@ -148,5 +148,10 @@
     
     <signal name="accountsChanged">
     </signal>
+
+    <signal name="errorAlert">
+      <arg type="s" name="errMsg" direction="out"/>
+    </signal>
+
   </interface>
 </node>
diff --git a/src/managerimpl.cpp b/src/managerimpl.cpp
index 6a213943f1..7121d35b0f 100644
--- a/src/managerimpl.cpp
+++ b/src/managerimpl.cpp
@@ -1258,12 +1258,15 @@ ManagerImpl::getOutputAudioPluginList(void)
 ManagerImpl::setInputAudioPlugin(const std::string& audioPlugin)
 {
   _debug("Set input audio plugin\n");
+  _audiodriver -> setErrorMessage( "" );
   _audiodriver -> openDevice( _audiodriver -> getIndexIn(),
       _audiodriver -> getIndexOut(),
       _audiodriver -> getSampleRate(),
       _audiodriver -> getFrameSize(),
       SFL_PCM_CAPTURE,
       audioPlugin);
+  if( _audiodriver -> getErrorMessage() != "")
+    notifyErrClient( _audiodriver -> getErrorMessage() );
 }
 
 /**
@@ -1273,12 +1276,15 @@ ManagerImpl::setInputAudioPlugin(const std::string& audioPlugin)
 ManagerImpl::setOutputAudioPlugin(const std::string& audioPlugin)
 {
   _debug("Set output audio plugin\n");
+  _audiodriver -> setErrorMessage( "" );
   _audiodriver -> openDevice( _audiodriver -> getIndexIn(),
       _audiodriver -> getIndexOut(),
       _audiodriver -> getSampleRate(),
       _audiodriver -> getFrameSize(),
       SFL_PCM_PLAYBACK,
       audioPlugin);
+  if( _audiodriver -> getErrorMessage() != "")
+    notifyErrClient( _audiodriver -> getErrorMessage() );
   // set config
   setConfig( AUDIO , ALSA_PLUGIN , audioPlugin );
 }
@@ -1300,12 +1306,15 @@ ManagerImpl::getAudioOutputDeviceList(void)
 ManagerImpl::setAudioOutputDevice(const int index)
 {
   _debug("Set audio output device: %i\n", index);
+  _audiodriver -> setErrorMessage( "" );
   _audiodriver->openDevice(_audiodriver->getIndexIn(), 
       index, 
       _audiodriver->getSampleRate(), 
       _audiodriver->getFrameSize(), 
       SFL_PCM_PLAYBACK,
       _audiodriver->getAudioPlugin());
+  if( _audiodriver -> getErrorMessage() != "")
+    notifyErrClient( _audiodriver -> getErrorMessage() );
   // set config
   setConfig( AUDIO , ALSA_CARD_ID_OUT , index );
 }
@@ -1327,12 +1336,15 @@ ManagerImpl::getAudioInputDeviceList(void)
 ManagerImpl::setAudioInputDevice(const int index)
 {
   _debug("Set audio input device %i\n", index);
+  _audiodriver -> setErrorMessage( "" );
   _audiodriver->openDevice(index, 
       _audiodriver->getIndexOut(), 
       _audiodriver->getSampleRate(), 
       _audiodriver->getFrameSize(), 
       SFL_PCM_CAPTURE,
       _audiodriver->getAudioPlugin());
+  if( _audiodriver -> getErrorMessage() != "")
+    notifyErrClient( _audiodriver -> getErrorMessage() );
   // set config
   setConfig( AUDIO , ALSA_CARD_ID_IN , index );
 }
@@ -1403,6 +1415,13 @@ ManagerImpl::setRingtoneChoice( const std::string& tone )
   setConfig( AUDIO , RING_CHOICE , tone ); 
 }
 
+void
+ManagerImpl::notifyErrClient( const std::string& errMsg )
+{
+  _debug("Call notifyErrClient: %s\n" , errMsg.c_str());
+  if( _dbus ) _dbus -> getConfigurationManager() -> errorAlert( errMsg );
+}
+
   int
 ManagerImpl::getAudioDeviceIndex(const std::string name)
 {
@@ -1468,6 +1487,8 @@ ManagerImpl::selectAudioDriver (void)
   _debugInit(" AudioLayer Opening Device");
   _audiodriver->setErrorMessage("");
   _audiodriver->openDevice( numCardIn , numCardOut, sampleRate, frameSize, SFL_PCM_BOTH, alsaPlugin ); 
+  if( _audiodriver -> getErrorMessage() != "")
+    notifyErrClient( _audiodriver -> getErrorMessage());
 }
 
 /**
diff --git a/src/managerimpl.h b/src/managerimpl.h
index d10e28e407..6d01ab20e7 100644
--- a/src/managerimpl.h
+++ b/src/managerimpl.h
@@ -363,6 +363,11 @@ public:
    */
   void setDefaultAccount(const AccountID& accountID);
 
+  /*
+   * Notify the client that an error occured
+   * @param errMsg The error message that should popup on the client side
+   */
+  void notifyErrClient( const std::string& errMsg );
 
   bool getConfigAll(const std::string& sequenceId);
   bool getConfig(const std::string& section, const std::string& name, TokenList& arg);
-- 
GitLab