edbus: Add edbus_private_connection_get()
authorJosé Roberto de Souza <zehortigoza@profusion.mobi>
Tue, 11 Dec 2012 19:50:20 +0000 (19:50 +0000)
committerLucas De Marchi <lucas.demarchi@profusion.mobi>
Tue, 11 Dec 2012 19:50:20 +0000 (19:50 +0000)
Patch by: José Roberto de Souza  <zehortigoza@profusion.mobi>

SVN revision: 80691

legacy/edbus/src/lib/edbus_connection.h
legacy/edbus/src/lib/edbus_core.c

index 811854d..928b137 100644 (file)
@@ -18,8 +18,9 @@ typedef enum
 #define EDBUS_TIMEOUT_INFINITE ((int) 0x7fffffff)
 
 /**
- * @brief Establish a connection to bus and integrate it with the ecore main
- * loop.
+ * Establish a connection to bus and integrate it with the ecore main
+ * loop, if already exist a connection of given type return it and increase
+ * reference.
  *
  * @param type type of connection e.g EDBUS_CONNECTION_TYPE_SESSION,
  * EDBUS_CONNECTION_TYPE_SYSTEM or EDBUS_CONNECTION_TYPE_STARTER
@@ -29,6 +30,17 @@ typedef enum
 EAPI EDBus_Connection *edbus_connection_get(EDBus_Connection_Type type);
 
 /**
+ * Always create and establish a new connection to bus and integrate it with
+ * the ecore main loop.
+ *
+ * @param type type of connection e.g EDBUS_CONNECTION_TYPE_SESSION,
+ * EDBUS_CONNECTION_TYPE_SYSTEM or EDBUS_CONNECTION_TYPE_STARTER
+ *
+ * @return connection with bus
+ */
+EAPI EDBus_Connection *edbus_private_connection_get(EDBus_Connection_Type type);
+
+/**
  * @brief Increment connection reference count.
  *
  * @param conn The given EDBus_Connection object to reference
index c7618bb..cfbba04 100644 (file)
@@ -921,25 +921,14 @@ edbus_connection_setup(EDBus_Connection *conn)
                       conn);
 }
 
-EAPI EDBus_Connection *
-edbus_connection_get(EDBus_Connection_Type type)
+static EDBus_Connection *
+_connection_get(EDBus_Connection_Type type)
 {
    EDBus_Connection *conn;
    DBusError err;
 
-   DBG("Getting connection with type %d", type);
-
-   if ((type < EDBUS_CONNECTION_TYPE_SESSION) ||
-       (type > EDBUS_CONNECTION_TYPE_STARTER))
-     return NULL;
-
-   conn = shared_connections[type - 1];
-   if (conn)
-     {
-        DBG("Connection with type %d exists at %p; reffing and returning",
-           type, conn);
-        return edbus_connection_ref(conn);
-     }
+   EINA_SAFETY_ON_FALSE_RETURN_VAL((type < EDBUS_CONNECTION_TYPE_LAST) &&
+                                   (type > EDBUS_CONNECTION_TYPE_UNKNOWN), NULL);
 
    conn = calloc(1, sizeof(EDBus_Connection));
    EINA_SAFETY_ON_NULL_RETURN_VAL(conn, NULL);
@@ -952,14 +941,11 @@ edbus_connection_get(EDBus_Connection_Type type)
         ERR("Error connecting to bus: %s", err.message);
         return NULL;
      }
-   edbus_connection_setup(conn);
 
+   edbus_connection_setup(conn);
    conn->type = type;
-   shared_connections[type - 1] = conn;
-
    conn->refcount = 1;
    EINA_MAGIC_SET(conn, EDBUS_CONNECTION_MAGIC);
-
    conn->names = eina_hash_string_superfast_new(NULL);
 
    DBG("Returned new connection at %p", conn);
@@ -967,6 +953,33 @@ edbus_connection_get(EDBus_Connection_Type type)
 }
 
 EAPI EDBus_Connection *
+edbus_private_connection_get(EDBus_Connection_Type type)
+{
+   return _connection_get(type);
+}
+
+EAPI EDBus_Connection *
+edbus_connection_get(EDBus_Connection_Type type)
+{
+   EDBus_Connection *conn;
+
+   DBG("Getting connection with type %d", type);
+   conn = shared_connections[type - 1];
+   if (conn)
+     {
+        DBG("Connection with type %d exists at %p; reffing and returning",
+            type, conn);
+        return edbus_connection_ref(conn);
+     }
+
+   conn = _connection_get(type);
+   EINA_SAFETY_ON_NULL_RETURN_VAL(conn, NULL);
+   shared_connections[type - 1] = conn;
+
+   return conn;
+}
+
+EAPI EDBus_Connection *
 edbus_connection_ref(EDBus_Connection *conn)
 {
    EDBUS_CONNECTION_CHECK_RETVAL(conn, NULL);
@@ -1072,7 +1085,8 @@ _edbus_connection_unref(EDBus_Connection *conn)
    edbus_data_del_all(&conn->data);
 
    if (conn->idler) ecore_idler_del(conn->idler);
-   shared_connections[conn->type - 1] = NULL;
+   if (shared_connections[conn->type - 1] == conn)
+     shared_connections[conn->type - 1] = NULL;
 
    free(conn);
 }