summaryrefslogtreecommitdiff
path: root/src/tidbit-dbus.c
blob: 7fee5182a70a98b4efae7dc9bd796ca2d28520d0 (plain)
1
2
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
/* Tidbit
 * Copyright (C) 2010 Charlie Brej tidbit@brej.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3.0 of the License, or (at your option) any later version.
 *
 * 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
 *
 */

#include <glib.h>
#include <dbus/dbus-glib.h>
#include "main.h"
#include "tidbit-dbus.h"
#include "tidbit-dbus-glue.h"
#include "tidbit-database.h"
#include "tidbit-record.h"

G_DEFINE_TYPE(TidbitDbus, tidbit_dbus, G_TYPE_OBJECT)

static void
tidbit_dbus_finalize (GObject *object)
{
 G_OBJECT_CLASS (tidbit_dbus_parent_class)->finalize (object);
 TidbitDbus *tidbit_dbus = TIDBIT_DBUS (object);
 tidbit_database_unref(tidbit_dbus->database);
}

static void
tidbit_dbus_class_init (TidbitDbusClass *klass)
{
 GObjectClass *object_class;
 object_class = G_OBJECT_CLASS (klass);
 object_class->finalize = tidbit_dbus_finalize;
}

static void
tidbit_dbus_init (TidbitDbus *object)
{
 TidbitDbus *tidbit_dbus = TIDBIT_DBUS (object);
 tidbit_dbus->database = NULL;
}

static void on_disconnect_cb (DBusGProxy *proxy)
{
 main_quit ();
}

void tidbit_dbus_setup (PtrTidbitDatabase database)
{
 guint   result;
 GError *error = NULL;
 DBusGConnection *connection;
 DBusGProxy *proxy;
 GObject *object;
 
 dbus_g_thread_init ();
 connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
 proxy = dbus_g_proxy_new_for_name (connection, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
 dbus_connection_set_exit_on_disconnect (dbus_g_connection_get_connection (connection), FALSE);
 org_freedesktop_DBus_request_name (proxy, DBUS_TIDBIT, DBUS_NAME_FLAG_DO_NOT_QUEUE, &result, &error);
 g_signal_connect (G_OBJECT (proxy), "destroy", G_CALLBACK (on_disconnect_cb), NULL);

 object = g_object_new (TYPE_TIDBIT_DBUS, NULL);
 TidbitDbus *tidbit_dbus = TIDBIT_DBUS (object);
 dbus_g_object_type_install_info (TYPE_TIDBIT_DBUS, &dbus_glib_tidbit_dbus_object_info);
 dbus_g_connection_register_g_object (connection, DBUS_TIDBIT_PATH, object);
 tidbit_dbus->database = database;
 tidbit_dbus->proxy = proxy;
 tidbit_database_ref(database);
}

gboolean tidbit_dbus_insert (TidbitDbus *object, gchar *record, gboolean *success, gpointer userdata)
{
 TidbitDbus *tidbit_dbus = TIDBIT_DBUS (object);
 PtrTidbitRecord new_record = tidbit_record_from_raw (record);     //FIXME check record is ok
 tidbit_database_insert (tidbit_dbus->database, new_record);
 tidbit_record_unref (new_record);
 *success = TRUE;
 return TRUE;
}

gboolean tidbit_dbus_fetch (TidbitDbus *object, gchar *signature, gchar **reply_record, gpointer userdata)
{
 TidbitDbus *tidbit_dbus = TIDBIT_DBUS (object);
 PtrTidbitGuid guid = tidbit_guid_new (signature);
 PtrTidbitRecord record = tidbit_database_fetch (tidbit_dbus->database, guid);
 tidbit_guid_unref (guid);
 if (record) {
    *reply_record = g_strdup(record->raw);
    tidbit_record_unref (record);
    }
 return TRUE;
}

static void guid_group_to_string_array_func (PtrTidbitGuid guid, gpointer user_data)
{
 char ***guids_array = (char***) user_data;                     //This is rather horrid. 
 **guids_array = g_strdup(tidbit_guid_get_string (guid));
 (*guids_array)++;
}

#define DBUS_STRUCT_STRING_STRING_INT (dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INVALID))

gboolean tidbit_dbus_query (TidbitDbus *object, gchar *table_name, GPtrArray *elements, char ***guids, gpointer userdata)
{
 TidbitDbus *tidbit_dbus = TIDBIT_DBUS (object);
 PtrTidbitQuery query = tidbit_query_new (table_name);
 
 GValue *dbus_value = g_new0 (GValue, 1);
 g_value_init (dbus_value, DBUS_STRUCT_STRING_STRING_INT);

 for (int i=0; i < elements->len; i++){
    g_value_set_boxed (dbus_value, g_ptr_array_index (elements, i));
    char* key;
    char* value;
    guint32 type;
    dbus_g_type_struct_get (dbus_value, 0, &key, 1, &value, 2, &type, G_MAXUINT);
    tidbit_query_add_exp (query, key, value, type);
    }
 g_free (dbus_value);
 PtrTidbitGuidGroup guid_group = tidbit_database_query (tidbit_dbus->database, query);
 int count = tidbit_guid_group_get_count (guid_group);
 char **guids_array = g_new(char*, count+1);
 guids_array[count] = NULL;
 *guids = guids_array;
 tidbit_guid_group_foreach (guid_group, guid_group_to_string_array_func, &guids_array);
 return TRUE;
}