acquire_internal.c

Go to the documentation of this file.
00001 /*
00002  EasyDbus: DBUS Binding Library.
00003  Copyright (C) 2007  Daniele Rondina aka Ge@@eu, geaaru@gmail.com 
00004 
00005  This program is free software; you can redistribute it and/or
00006  modify it under the terms of the GNU General Public License
00007  as published by the Free Software Foundation; either version 2
00008  of the License, or (at your option) any later version.
00009 
00010  This program is distributed in the hope that it will be useful,
00011  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  GNU General Public License for more details.
00014 
00015  You should have received a copy of the GNU General Public License
00016  along with this program; if not, write to the Free Software
00017  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018 
00019  Filename:  acquire_internal.c
00020  
00021  Description:  
00022  
00023  Version:  1.0
00024  Created:  09/17/07 17:04:41 CEST
00025  Revision:  none
00026  
00027  Author:   Daniele Rondina aka Ge@@ru , geaaru@gmail.com 
00028  License:  GPL 2.0
00029 */
00030 
00036 #include <stdlib.h>
00037 #include <string.h>
00038 // easydbus includes
00039 #include "easydbus-core.h"
00040 #include "acquire_internal.h"
00041 #include "debug.h"
00042 #include "utils.h"
00043 
00056 int
00057 easydbus_get_address_serverSession (struct EasyDbus_conn *data,
00058                                     char *serviceName)
00059 {
00060    DBusError err;
00061    struct EasyDbus_core *core = (struct EasyDbus_core *) data;
00062    int result = -1;
00063    char *new_service_name = NULL;
00064 
00065    dbus_error_init (&err);
00066 
00067    if (easydbus_connect_to_bus (core, &err, DBUS_BUS_SESSION))
00068       goto error_acquire;
00069 
00070 // if (dbus_bus_register (core->conn, &err) == FALSE) {
00071 //    EasyDbusDebug ("Error %s from dbus_bus_register: %s",
00072 //          err.name,
00073 //          err.message);
00074 //    goto error_acquire;
00075 // }
00076 
00077    result = easydbus_acquire_name (core, serviceName,
00078                                    data->flags | EASYDBUS_CONN_DO_NOT_QUEUE,
00079                                    &err);
00080    if (result == -1)
00081       goto error_acquire;
00082    if (result == EASYDBUS_CONN_NAME_SERVICE_EXISTS) {
00083       // probably this isn't needed because without this flags
00084       // return value is EASYDBUS_CONN_NAME_SERVICE_IN_QUEUE
00085       if (data->flags & EASYDBUS_CONN_NAME_SERVICE_RENAMED) {
00086          new_service_name = easydbus_found_new_name
00087             (core, serviceName, data->flags, &err);
00088          if (new_service_name == NULL)
00089             goto error_acquire;
00090          result = EASYDBUS_CONN_NAME_SERVICE_RENAMED;
00091       }
00092       else
00093          goto error_acquire;
00094    }
00095 
00096    data->registered_ondbus = 1;
00097 
00098    if (new_service_name != NULL)
00099       core->service_name = new_service_name;
00100    else if (easydbus_save_serviceName (core, serviceName)) {
00101       EasyDbusDebug ("Error on save service name");
00102       goto error_acquire;
00103    }
00104 
00105    if (data->flags & EASYDBUS_CONN_NAME_UNIQUE)
00106       if (easydbus_set_unique_nameID (core, core->service_name))
00107          goto error_acquire;
00108 
00109    dbus_error_free (&err);
00110    return result;
00111 
00112  error_acquire:
00113    if (data->registered_ondbus)
00114       dbus_bus_release_name (core->conn, 
00115                              core->service_name, &err);
00116 
00117    if (core->conn != NULL)
00118       dbus_connection_unref (core->conn);
00119 
00120    dbus_error_free (&err);
00121 
00122    return -1;
00123 }
00124 
00133 inline int
00134 easydbus_set_unique_nameID (struct EasyDbus_core *core, 
00135                             char *nameId)
00136 {
00137    EasyDbusDebug ("DBUS unique name = %s",
00138                   dbus_bus_get_unique_name (core->conn));
00139 
00140    if (!dbus_bus_set_unique_name (core->conn, nameId))
00141       return -1;
00142 
00143    EasyDbusDebug ("DBUS unique name = %s",
00144                   dbus_bus_get_unique_name (core->conn));
00145 
00146    return 0;
00147 }
00148 
00161 inline int 
00162 easydbus_get_address_onsession (struct EasyDbus_conn *data, 
00163                                 char *serviceName)
00164 {
00165    struct EasyDbus_core *core = (struct EasyDbus_core *) data;
00166    DBusError err;
00167    int result = -1;
00168    char *new_service_name = NULL;
00169 
00170    dbus_error_init (&err);
00171 
00172    /* connect to system bus */
00173    if (easydbus_connect_to_bus (core, &err, DBUS_BUS_SESSION))
00174       goto error_acquire;
00175 
00176    result = easydbus_acquire_name (core, serviceName, 
00177                                    data->flags, &err);
00178    if (result == -1)
00179       goto error_acquire;
00180 
00181    if (result == EASYDBUS_CONN_NAME_SERVICE_EXISTS &&
00182        // probably this isn't needed because without this flags
00183        // return value is EASYDBUS_CONN_NAME_SERVICE_IN_QUEUE
00184        (data->flags & EASYDBUS_CONN_DO_NOT_QUEUE)) {
00185       if (data->flags & EASYDBUS_CONN_NAME_SERVICE_RENAMED) {
00186          new_service_name = easydbus_found_new_name
00187             (core, serviceName, data->flags, &err);
00188          if (new_service_name == NULL)
00189             goto error_acquire;
00190          result = EASYDBUS_CONN_NAME_SERVICE_RENAMED;
00191       }
00192       else
00193          goto error_acquire;
00194    }
00195 
00196    data->registered_ondbus = 1;
00197 
00198    if (new_service_name != NULL)
00199       core->service_name = new_service_name;
00200    else if (easydbus_save_serviceName (core, serviceName)) {
00201       EasyDbusDebug ("Error on save service name");
00202       goto error_acquire;
00203    }
00204    
00205    if (data->flags & EASYDBUS_CONN_NAME_UNIQUE)
00206       if (easydbus_set_unique_nameID (core, core->service_name))
00207          goto error_acquire;
00208 
00209    EasyDbusDebug ("Acquire operation terminated");
00210 
00211    dbus_error_free (&err);
00212    return result;
00213 
00214  error_acquire:
00215    if (data->registered_ondbus)
00216       dbus_bus_release_name (core->conn, 
00217                              core->service_name, &err);
00218 
00219    if (core->conn != NULL)
00220       dbus_connection_unref (core->conn);
00221 
00222    dbus_error_free (&err);
00223 
00224    return result;
00225 }
00226 
00236 int 
00237 easydbus_connect_to_bus (struct EasyDbus_core *core,
00238                          DBusError * err, 
00239                          DBusBusType type_bus) 
00240 {
00241    /* connect to system bus */
00242    core->conn = dbus_bus_get (type_bus, err);
00243    if (!core->conn) {
00244       EasyDbusDebug ("Error %s from dbus_bus_get: %s",
00245                      err->name, err->message);
00246       return -1;
00247    }
00248 
00249    EasyDbusDebug ("Connected and registered to bus daemon");
00250    /* do not allw libdbus to exit on connection failure ( This
00251     * may around random exit () on SIGPIPE errors) */
00252    dbus_connection_set_exit_on_disconnect (core->conn, FALSE);
00253    return 0;
00254 }
00255 
00268 inline int 
00269 easydbus_get_address_onsystem (struct EasyDbus_conn *data, 
00270                                char *serviceName)
00271 {
00272    struct EasyDbus_core *core = (struct EasyDbus_core *) data;
00273    DBusError err;
00274    int result = -1;
00275    char *new_service_name = NULL;
00276 
00277    dbus_error_init (&err);
00278 
00279    if (easydbus_connect_to_bus (core, &err, DBUS_BUS_SYSTEM))
00280       goto error_acquire;
00281 
00282    result = easydbus_acquire_name (core, serviceName, data->flags, 
00283                                    &err);
00284    if (result == -1)
00285       goto error_acquire;
00286    if (result == EASYDBUS_CONN_NAME_SERVICE_EXISTS &&
00287        // probably this isn't needed because without this flags
00288        // return value is EASYDBUS_CONN_NAME_SERVICE_IN_QUEUE
00289        (data->flags & EASYDBUS_CONN_DO_NOT_QUEUE)) {
00290       if (data->flags & EASYDBUS_CONN_NAME_SERVICE_RENAMED) {
00291          new_service_name = easydbus_found_new_name
00292             (core, serviceName, data->flags, &err);
00293          if (new_service_name == NULL)
00294             goto error_acquire;
00295          result = EASYDBUS_CONN_NAME_SERVICE_RENAMED;
00296       }
00297       else
00298          goto error_acquire;
00299    }
00300 
00301    data->registered_ondbus = 1;
00302 
00303    if (new_service_name != NULL)
00304       core->service_name = new_service_name;
00305    else if (easydbus_save_serviceName (core, serviceName)) {
00306       EasyDbusDebug ("Error on save service name");
00307       goto error_acquire;
00308    }
00309 
00310    if (data->flags & EASYDBUS_CONN_NAME_UNIQUE)
00311       if (easydbus_set_unique_nameID (core, core->service_name))
00312          goto error_acquire;
00313 
00314    dbus_error_free (&err);
00315    return result;
00316 
00317  error_acquire:
00318    if (data->registered_ondbus)
00319       dbus_bus_release_name (core->conn, 
00320                              core->service_name, &err);
00321 
00322    if (core->conn != NULL)
00323       dbus_connection_unref (core->conn);
00324 
00325    dbus_error_free (&err);
00326 
00327    return -1;
00328 }
00329 
00330 
00339 void
00340 close_dbus_connection (EasyDbus_conn *data)
00341 {
00342    struct EasyDbus_core *core = (struct EasyDbus_core *) data;
00343 
00344    dbus_connection_close (core->conn);
00345 }
00346 
00347 // vim: ts=3 shiftwidth=3 expandtab

Generated on Thu Apr 10 10:00:17 2008 for EasyDbus-0.2 by  doxygen 1.5.4