map.c 15.6 KB
Newer Older
Keith Preston's avatar
Keith Preston committed
1
/* GEOCLUE_MAP - A DBus api and wrapper for getting geography pictures
2
 * Copyright (C) 2006-2007 by Garmin Ltd. or its subsidiaries
Keith Preston's avatar
Keith Preston committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * 
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2 as published by the Free Software Foundation; 
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#define DBUS_API_SUBJECT_TO_CHANGE 

#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <dbus/dbus.h>

#include <map.h>
#include <map_client_glue.h>
#include <stdio.h>
28
#include <stdlib.h>
Keith Preston's avatar
Keith Preston committed
29 30
#include <map_signal_marshal.h>

31
#include <geoclue_master_client_glue.h>
Keith Preston's avatar
Keith Preston committed
32
#include <config.h>
33

34
#define GEOCLUE_MAP_DBUS_INTERFACE   "org.freedesktop.geoclue.map"   
35
 
36 37 38
#define GEOCLUE_MASTER_DBUS_SERVICE     "org.freedesktop.geoclue.master"
#define GEOCLUE_MASTER_DBUS_PATH        "/org/freedesktop/geoclue/master"
#define GEOCLUE_MASTER_DBUS_INTERFACE   "org.freedesktop.geoclue.master" 
39

Keith Preston's avatar
Keith Preston committed
40 41 42 43 44 45 46 47
        
static  DBusGConnection*        geoclue_map_connection =   NULL;
static  DBusGProxy*             geoclue_map_proxy      =   NULL;
static  GEOCLUE_MAP_CALLBACK         callbackfunction  =   NULL;
static  void*                   userdatastore     =   NULL; 


   
48
void geoclue_map_get_map_finished(void* userdata, GEOCLUE_MAP_RETURNCODE returncode, GArray* geoclue_map_buffer, gchar* buffer_mime_type)
Keith Preston's avatar
Keith Preston committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
{   
   // printf("\n\n\nGet Map Finished returned \n\n\n\n");
   if(returncode == GEOCLUE_MAP_SUCCESS)
   {
        if(callbackfunction != NULL)
            callbackfunction( returncode, geoclue_map_buffer, buffer_mime_type , userdatastore );           
   }
   else
   {
        g_printerr("GEOCLUE_MAPping get geoclue_map error return code %d\n", returncode);
   }
}



GEOCLUE_MAP_RETURNCODE geoclue_map_version(int* major, int* minor, int* micro)
{
    if(geoclue_map_connection == NULL)
        return GEOCLUE_MAP_NOT_INITIALIZED;  
    if(geoclue_map_proxy == NULL)
        return GEOCLUE_MAP_NOT_INITIALIZED;  
                                   
    GError* error = NULL;
72
    org_freedesktop_geoclue_map_version ( geoclue_map_proxy, major, minor, micro, &error );
Keith Preston's avatar
Keith Preston committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    if( error != NULL )
    {
        g_printerr ("Error getting version: %s\n", error->message);
        g_error_free (error);  
        return GEOCLUE_MAP_DBUS_ERROR;        
    }
    return GEOCLUE_MAP_SUCCESS;              
}

GEOCLUE_MAP_RETURNCODE geoclue_map_service_provider(char** name)
{
    if(geoclue_map_connection == NULL)
        return GEOCLUE_MAP_NOT_INITIALIZED;  
    if(geoclue_map_proxy == NULL)
        return GEOCLUE_MAP_NOT_INITIALIZED;  
                                   
    GError* error = NULL;
90
    org_freedesktop_geoclue_map_service_provider( geoclue_map_proxy, name, &error );
Keith Preston's avatar
Keith Preston committed
91 92 93 94 95 96 97 98
    if( error != NULL )
    {
        g_printerr ("Error getting service provider: %s\n", error->message);
        g_error_free (error);  
        return GEOCLUE_MAP_DBUS_ERROR;        
    }
    return GEOCLUE_MAP_SUCCESS;              
}
99 100 101 102

GEOCLUE_MAP_RETURNCODE geoclue_map_init_specific(char* service, char* path)
{
    GError* error = NULL;
Keith Preston's avatar
Keith Preston committed
103
    geoclue_map_connection = dbus_g_bus_get (GEOCLUE_DBUS_BUS, &error);
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    if (geoclue_map_connection == NULL)
    {
        g_printerr ("GEOCLUE_MAP failed to open connection to bus: %s\n", error->message);
        g_error_free (error);
        return GEOCLUE_MAP_DBUS_ERROR;
    }
    
    
    geoclue_map_proxy = dbus_g_proxy_new_for_name (geoclue_map_connection,
                                                    service,
                                                    path,
                                                    GEOCLUE_MAP_DBUS_INTERFACE);
                   
    dbus_g_object_register_marshaller ( _geoclue_map_VOID__INT_BOXED_STRING,
                                        G_TYPE_NONE,
                                        G_TYPE_INT, DBUS_TYPE_G_UCHAR_ARRAY, G_TYPE_STRING, G_TYPE_INVALID);
    dbus_g_proxy_add_signal (geoclue_map_proxy,
                             "get_map_finished",
                             G_TYPE_INT, DBUS_TYPE_G_UCHAR_ARRAY, G_TYPE_STRING, G_TYPE_INVALID);
    dbus_g_proxy_connect_signal (geoclue_map_proxy,
                                 "get_map_finished",
                                 (GCallback) geoclue_map_get_map_finished,
                                 (gpointer)NULL,
                                 (GClosureNotify) NULL);

    callbackfunction    = NULL;
    userdatastore       = NULL;  
    return GEOCLUE_MAP_SUCCESS;        
}

GEOCLUE_MAP_RETURNCODE geoclue_map_get_all_providers(char*** OUT_service, char*** OUT_path, char*** OUT_desc)
{
    GError* error = NULL;
        
    if (geoclue_map_connection == NULL)
    {
Keith Preston's avatar
Keith Preston committed
140
        geoclue_map_connection = dbus_g_bus_get (GEOCLUE_DBUS_BUS, &error);
141 142 143 144 145 146 147 148 149
    }
    

    
    DBusGProxy* master = dbus_g_proxy_new_for_name (geoclue_map_connection,
                                                    GEOCLUE_MASTER_DBUS_SERVICE,
                                                    GEOCLUE_MASTER_DBUS_PATH,
                                                    GEOCLUE_MASTER_DBUS_INTERFACE);   

150
    org_freedesktop_geoclue_master_get_all_position_providers (master, OUT_service, OUT_path, OUT_desc, &error);
151 152 153 154 155 156 157 158 159
    if( error != NULL )
    {
        g_printerr ("Error getting all position provider: %s\n", error->message);
        g_error_free (error);  
        return GEOCLUE_MAP_DBUS_ERROR;        
    }   
    
    return GEOCLUE_MAP_SUCCESS;     
}
Keith Preston's avatar
Keith Preston committed
160 161 162 163 164 165
   
   
   
GEOCLUE_MAP_RETURNCODE geoclue_map_init()
{
    GError* error = NULL;
Keith Preston's avatar
Keith Preston committed
166
    geoclue_map_connection = dbus_g_bus_get (GEOCLUE_DBUS_BUS, &error);
Keith Preston's avatar
Keith Preston committed
167 168 169 170 171 172
    if (geoclue_map_connection == NULL)
    {
        g_printerr ("GEOCLUE_MAP failed to open connection to bus: %s\n", error->message);
        g_error_free (error);
        return GEOCLUE_MAP_DBUS_ERROR;
    }
173 174 175 176 177 178 179 180 181 182 183 184 185
    
    
    
     
    DBusGProxy* master = dbus_g_proxy_new_for_name (geoclue_map_connection,
                                                    GEOCLUE_MASTER_DBUS_SERVICE,
                                                    GEOCLUE_MASTER_DBUS_PATH,
                                                    GEOCLUE_MASTER_DBUS_INTERFACE);   
    
   
    char* service;
    char* path;
    char* desc;
186
    org_freedesktop_geoclue_master_get_default_map_provider (master, &service, &path, &desc, &error);
187 188 189 190 191 192 193 194
    if( error != NULL )
    {
        g_printerr ("Error getting default map provider: %s\n", error->message);
        g_error_free (error);  
        return GEOCLUE_MAP_DBUS_ERROR;        
    }   
    
    printf(" Getting Service %s and path %s\n",service, path);
Keith Preston's avatar
Keith Preston committed
195
    geoclue_map_proxy = dbus_g_proxy_new_for_name (geoclue_map_connection,
196 197
                                                    service,
                                                    path,
Keith Preston's avatar
Keith Preston committed
198
                                                    GEOCLUE_MAP_DBUS_INTERFACE);
199 200 201 202
    
    free(service);
    free(path); 
    free(desc); 
Keith Preston's avatar
Keith Preston committed
203 204 205 206 207
                                    
    dbus_g_object_register_marshaller ( _geoclue_map_VOID__INT_BOXED_STRING,
                                        G_TYPE_NONE,
                                        G_TYPE_INT, DBUS_TYPE_G_UCHAR_ARRAY, G_TYPE_STRING, G_TYPE_INVALID);
    dbus_g_proxy_add_signal (geoclue_map_proxy,
208
                             "get_map_finished",
Keith Preston's avatar
Keith Preston committed
209 210
                             G_TYPE_INT, DBUS_TYPE_G_UCHAR_ARRAY, G_TYPE_STRING, G_TYPE_INVALID);
    dbus_g_proxy_connect_signal (geoclue_map_proxy,
211 212
                                 "get_map_finished",
                                 (GCallback) geoclue_map_get_map_finished,
Keith Preston's avatar
Keith Preston committed
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
                                 (gpointer)NULL,
                                 (GClosureNotify) NULL);

    callbackfunction    = NULL;
    userdatastore       = NULL;  
    return GEOCLUE_MAP_SUCCESS;        
}

 
GEOCLUE_MAP_RETURNCODE geoclue_map_close()
{
    g_object_unref (geoclue_map_proxy);
    geoclue_map_proxy = NULL;
    geoclue_map_connection = NULL;
    callbackfunction    = NULL;
    userdatastore       = NULL;
    return GEOCLUE_MAP_SUCCESS;   
}



GEOCLUE_MAP_RETURNCODE geoclue_map_get_map (const gdouble IN_latitude, const gdouble IN_longitude, const gint IN_width, const gint IN_height, const gint IN_zoom, GEOCLUE_MAP_CALLBACK func, void* userdata)
{
    if(geoclue_map_connection == NULL)
        return GEOCLUE_MAP_NOT_INITIALIZED;  
    if(geoclue_map_proxy == NULL)
        return GEOCLUE_MAP_NOT_INITIALIZED;     
    //g_print("GEOCLUE_MAPping!\n");
    GError* error = NULL;
    int return_code;
243
    if(!org_freedesktop_geoclue_map_get_map (geoclue_map_proxy, IN_latitude, IN_longitude, IN_width, IN_height, IN_zoom, &return_code, &error))
Keith Preston's avatar
Keith Preston committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    {
        if(error != NULL)
        {
            g_printerr(" Error in geoclue_map async dbus call %s\n", error->message);
            g_error_free (error);  
        }               
    }
    callbackfunction    = func;
    userdatastore       = userdata;
    //g_print("GEOCLUE_MAPping OUT!\n");    
    
    return  GEOCLUE_MAP_SUCCESS;
}

GEOCLUE_MAP_RETURNCODE geoclue_map_max_zoom(int* max_zoom)
{
    if(geoclue_map_connection == NULL)
        return GEOCLUE_MAP_NOT_INITIALIZED;  
    if(geoclue_map_proxy == NULL)
        return GEOCLUE_MAP_NOT_INITIALIZED;     
    GError* error = NULL;
265
    if(!org_freedesktop_geoclue_map_max_zoom(geoclue_map_proxy, max_zoom, &error))
Keith Preston's avatar
Keith Preston committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
    {
        if(error != NULL)
        {
            g_printerr(" Error in geoclue_map async dbus call %s\n", error->message);
            g_error_free (error);  
        } 
        return  GEOCLUE_MAP_DBUS_ERROR;              
    }
    return  GEOCLUE_MAP_SUCCESS;
}
    
GEOCLUE_MAP_RETURNCODE geoclue_map_min_zoom(int* min_zoom)
{
    if(geoclue_map_connection == NULL)
        return GEOCLUE_MAP_NOT_INITIALIZED;  
    if(geoclue_map_proxy == NULL)
        return GEOCLUE_MAP_NOT_INITIALIZED;     
    GError* error = NULL;
284
    if(!org_freedesktop_geoclue_map_min_zoom(geoclue_map_proxy, min_zoom, &error))
Keith Preston's avatar
Keith Preston committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
    {
        if(error != NULL)
        {
            g_printerr(" Error in geoclue_map async dbus call %s\n", error->message);
            g_error_free (error);  
        } 
        return  GEOCLUE_MAP_DBUS_ERROR;              
    }
    return  GEOCLUE_MAP_SUCCESS;
}


GEOCLUE_MAP_RETURNCODE geoclue_map_max_height(int* max_height)
{
    if(geoclue_map_connection == NULL)
        return GEOCLUE_MAP_NOT_INITIALIZED;  
    if(geoclue_map_proxy == NULL)
        return GEOCLUE_MAP_NOT_INITIALIZED;     
    GError* error = NULL;
304
    if(!org_freedesktop_geoclue_map_max_height(geoclue_map_proxy, max_height, &error))
Keith Preston's avatar
Keith Preston committed
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
    {
        if(error != NULL)
        {
            g_printerr(" Error in geoclue_map async dbus call %s\n", error->message);
            g_error_free (error);  
        } 
        return  GEOCLUE_MAP_DBUS_ERROR;              
    }
    return  GEOCLUE_MAP_SUCCESS;
}

GEOCLUE_MAP_RETURNCODE geoclue_map_min_height(int* min_height)
{
    if(geoclue_map_connection == NULL)
        return GEOCLUE_MAP_NOT_INITIALIZED;  
    if(geoclue_map_proxy == NULL)
        return GEOCLUE_MAP_NOT_INITIALIZED;     
    GError* error = NULL;
323
    if(!org_freedesktop_geoclue_map_min_height(geoclue_map_proxy, min_height, &error))
Keith Preston's avatar
Keith Preston committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
    {
        if(error != NULL)
        {
            g_printerr(" Error in geoclue_map async dbus call %s\n", error->message);
            g_error_free (error);  
        } 
        return  GEOCLUE_MAP_DBUS_ERROR;              
    }
    return  GEOCLUE_MAP_SUCCESS;
}

GEOCLUE_MAP_RETURNCODE geoclue_map_max_width(int* max_width)
{
    if(geoclue_map_connection == NULL)
        return GEOCLUE_MAP_NOT_INITIALIZED;  
    if(geoclue_map_proxy == NULL)
        return GEOCLUE_MAP_NOT_INITIALIZED;     
    GError* error = NULL;
342
    if(!org_freedesktop_geoclue_map_max_width(geoclue_map_proxy, max_width, &error))
Keith Preston's avatar
Keith Preston committed
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
    {
        if(error != NULL)
        {
            g_printerr(" Error in geoclue_map async dbus call %s\n", error->message);
            g_error_free (error);  
        } 
        return  GEOCLUE_MAP_DBUS_ERROR;              
    }
    return  GEOCLUE_MAP_SUCCESS;
}

GEOCLUE_MAP_RETURNCODE geoclue_map_min_width(int* min_width)
{
    if(geoclue_map_connection == NULL)
        return GEOCLUE_MAP_NOT_INITIALIZED;  
    if(geoclue_map_proxy == NULL)
        return GEOCLUE_MAP_NOT_INITIALIZED;     
    GError* error = NULL;
361
    if(!org_freedesktop_geoclue_map_min_width(geoclue_map_proxy, min_width, &error))
Keith Preston's avatar
Keith Preston committed
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
    {
        if(error != NULL)
        {
            g_printerr(" Error in geoclue_map async dbus call %s\n", error->message);
            g_error_free (error);  
        } 
        return  GEOCLUE_MAP_DBUS_ERROR;              
    }
    return  GEOCLUE_MAP_SUCCESS;
}
    
    

GEOCLUE_MAP_RETURNCODE geoclue_map_latlong_to_offset(const gdouble IN_latitude, const gdouble IN_longitude,  const gint IN_zoom, const gdouble IN_center_latitude, const gdouble IN_center_longitude, int* OUT_x_offset, int* OUT_y_offset)
{
    if(geoclue_map_connection == NULL)
        return GEOCLUE_MAP_NOT_INITIALIZED;  
    if(geoclue_map_proxy == NULL)
        return GEOCLUE_MAP_NOT_INITIALIZED;     
    GError* error = NULL;
382
    if(!org_freedesktop_geoclue_map_latlong_to_offset(geoclue_map_proxy, IN_latitude, IN_longitude, IN_zoom, IN_center_latitude,   IN_center_longitude, OUT_x_offset, OUT_y_offset, &error))
Keith Preston's avatar
Keith Preston committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
    {
        if(error != NULL)
        {
            g_printerr(" Error in geoclue_map async dbus call %s\n", error->message);
            g_error_free (error);  
        } 
        return  GEOCLUE_MAP_DBUS_ERROR;              
    }
    return  GEOCLUE_MAP_SUCCESS;
}

GEOCLUE_MAP_RETURNCODE geoclue_map_offset_to_latlong(const int IN_x_offset,const int IN_y_offset, const gint IN_zoom, const gdouble IN_center_latitude, const gdouble IN_center_longitude, gdouble* OUT_latitude, gdouble* OUT_longitude )
{
    if(geoclue_map_connection == NULL)
        return GEOCLUE_MAP_NOT_INITIALIZED;  
    if(geoclue_map_proxy == NULL)
        return GEOCLUE_MAP_NOT_INITIALIZED;     
    GError* error = NULL;
    printf("offset to long\n");
    
403
    if(!org_freedesktop_geoclue_map_offset_to_latlong(geoclue_map_proxy, IN_x_offset, IN_y_offset, IN_zoom, IN_center_latitude,   IN_center_longitude, OUT_latitude, OUT_longitude, &error))
Keith Preston's avatar
Keith Preston committed
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
    {
        if(error != NULL)
        {
            g_printerr(" Error in geoclue_map async dbus call %s\n", error->message);
            g_error_free (error);  
        } 
        return  GEOCLUE_MAP_DBUS_ERROR;              
    }
    return  GEOCLUE_MAP_SUCCESS;    
    
}


GEOCLUE_MAP_RETURNCODE geoclue_map_find_zoom_level (const gdouble IN_latitude_top_left, const gdouble IN_longitude_top_left, const gdouble IN_latitude_bottom_right, const gdouble IN_longitude_bottom_right, const gint IN_width, const gint IN_height, gint* OUT_zoom)
{
    
    if(geoclue_map_connection == NULL)
        return GEOCLUE_MAP_NOT_INITIALIZED;  
    if(geoclue_map_proxy == NULL)
        return GEOCLUE_MAP_NOT_INITIALIZED;     
    GError* error = NULL;
425
    if(!org_freedesktop_geoclue_map_find_zoom_level (geoclue_map_proxy, IN_latitude_top_left, IN_longitude_top_left, IN_latitude_bottom_right, IN_width, IN_height, IN_longitude_bottom_right, OUT_zoom, &error))
Keith Preston's avatar
Keith Preston committed
426 427 428 429 430 431 432 433 434 435 436 437 438 439
    {
        if(error != NULL)
        {
            g_printerr(" Error in geoclue_map async dbus call %s\n", error->message);
            g_error_free (error);  
        } 
        return  GEOCLUE_MAP_DBUS_ERROR;              
    }
    return  GEOCLUE_MAP_SUCCESS;    
}