Blob Blame History Raw
From 8de2e9b4bc53e6c097897142bad223c100d36292 Mon Sep 17 00:00:00 2001
From: Bryan Quigley <gquigs@gmail.com>
Date: Mon, 4 Aug 2014 12:26:00 -0400
Subject: [PATCH 090/137] fdo#81552 Fail nicely if avahi doesn't let
 libreoffice publish items

Needed some minor changes for 4-2.

Reviewed-on: https://gerrit.libreoffice.org/10735
Reviewed-by: Michael Stahl <mstahl@redhat.com>
Tested-by: Michael Stahl <mstahl@redhat.com>
(cherry picked from commit 3c57701cf0a169bd8d1893d1b2271d48b8072147)

Change-Id: Ie264a032a71bda336158e18bd2b14c569f23f42d
Reviewed-on: https://gerrit.libreoffice.org/10822
Reviewed-by: Michael Stahl <mstahl@redhat.com>
Tested-by: Michael Stahl <mstahl@redhat.com>
---
 sd/source/ui/remotecontrol/AvahiNetworkService.cxx | 28 ++++++++++++++--------
 1 file changed, 18 insertions(+), 10 deletions(-)

diff --git a/sd/source/ui/remotecontrol/AvahiNetworkService.cxx b/sd/source/ui/remotecontrol/AvahiNetworkService.cxx
index 04d8e57..43ee31b 100644
--- a/sd/source/ui/remotecontrol/AvahiNetworkService.cxx
+++ b/sd/source/ui/remotecontrol/AvahiNetworkService.cxx
@@ -35,7 +35,7 @@ static AvahiThreadedPoll *threaded_poll = NULL;
 static AvahiEntryGroup *group = NULL;
 static AvahiNetworkService *avahiService = NULL;
 
-static void create_services(AvahiClient *c);
+static bool create_services(AvahiClient *c);
 
 static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, AVAHI_GCC_UNUSED void *userdata) {
     assert(g == group || group == NULL);
@@ -78,16 +78,19 @@ static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state,
     }
 }
 
-static void create_services(AvahiClient *c) {
+static bool create_services(AvahiClient *c) {
     assert(c);
 
     /* If this is the first time we're called, let's create a new
      * entry group if necessary */
+    if(!client)
+        return false;
 
     if (!group)
         if (!(group = avahi_entry_group_new(c, entry_group_callback, NULL))) {
             fprintf(stderr, "avahi_entry_group_new() failed: %s\n", avahi_strerror(avahi_client_errno(c)));
             avahiService->clear();
+            return false;
         }
 
     /* If the group is empty (either because it was just created, or
@@ -113,22 +116,23 @@ static void create_services(AvahiClient *c) {
 
                 avahi_entry_group_reset(group);
 
-                create_services(c);
-                return;
+                return create_services(c);
             }
 
             fprintf(stderr, "Failed to add _impressremote._tcp service: %s\n", avahi_strerror(ret));
             avahiService->clear();
+            return false;
         }
 
         /* Tell the server to register the service */
         if ((ret = avahi_entry_group_commit(group)) < 0) {
             fprintf(stderr, "Failed to commit entry group: %s\n", avahi_strerror(ret));
             avahiService->clear();
+            return false;
         }
     }
 
-    return;
+    return true; //Services we're already created
 }
 
 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
@@ -174,7 +178,8 @@ void AvahiNetworkService::setup() {
      return;
    }
 
-   create_services(client);
+   if(!create_services(client))
+        return;
 
    /* Finally, start the event loop thread */
    if (avahi_threaded_poll_start(threaded_poll) < 0) {
@@ -184,8 +189,11 @@ void AvahiNetworkService::setup() {
 }
 
 void AvahiNetworkService::clear() {
-  /* Call this when the app shuts down */
-  avahi_threaded_poll_stop(threaded_poll);
-  avahi_client_free(client);
-  avahi_threaded_poll_free(threaded_poll);
+    /* Call this when the app shuts down */
+        if(threaded_poll)
+            avahi_threaded_poll_stop(threaded_poll);
+        if(client)
+            avahi_client_free(client);
+        if(threaded_poll)
+            avahi_threaded_poll_free(threaded_poll);
 }
-- 
1.9.3