| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#include "lcb-cbd.h" |
| 10 |
#include "gio/gio.h" |
| 11 |
#include "lcb-dbus.h" |
| 12 |
#include "lcb-main.h" |
| 13 |
#include "lcb-util.h" |
| 14 |
|
| 15 |
#include <glib/gstdio.h> |
| 16 |
|
| 17 |
typedef struct { |
| 18 |
GMainLoop *loop; |
| 19 |
GAsyncResult *res; |
| 20 |
} LcbSyncData; |
| 21 |
|
| 22 |
|
| 23 |
static void |
| 24 |
on_open_messages_sync (GObject *object, |
| 25 |
GAsyncResult *res, |
| 26 |
gpointer user_data) |
| 27 |
{ |
| 28 |
LcbSyncData *data = user_data; |
| 29 |
data->res = g_object_ref (res); |
| 30 |
|
| 31 |
g_main_loop_quit (data->loop); |
| 32 |
} |
| 33 |
|
| 34 |
static GBytes * |
| 35 |
on_open_messages_get_bytes (GDBusProxy *proxy, |
| 36 |
GAsyncResult *result, |
| 37 |
GError **error) |
| 38 |
{ |
| 39 |
g_autoptr (GUnixFDList) fd_list = NULL; |
| 40 |
g_autoptr (GVariant) var_tuple = NULL; |
| 41 |
g_autoptr (GVariant) var_handle = NULL; |
| 42 |
g_autoptr (GMappedFile) file = NULL; |
| 43 |
gint32 fd_handle_index; |
| 44 |
g_autofd int fd = -1; |
| 45 |
|
| 46 |
var_tuple = g_dbus_proxy_call_with_unix_fd_list_finish (G_DBUS_PROXY (proxy), |
| 47 |
&fd_list, |
| 48 |
result, |
| 49 |
error); |
| 50 |
if (!var_tuple || !fd_list) |
| 51 |
return NULL; |
| 52 |
|
| 53 |
var_handle = g_variant_get_child_value (var_tuple, 0); |
| 54 |
fd_handle_index = g_variant_get_handle (var_handle); |
| 55 |
|
| 56 |
|
| 57 |
g_assert (fd_handle_index == 0); |
| 58 |
g_assert (g_unix_fd_list_get_length (fd_list) == 1); |
| 59 |
|
| 60 |
fd = g_unix_fd_list_get (fd_list, fd_handle_index, error); |
| 61 |
if (fd == -1) |
| 62 |
return NULL; |
| 63 |
|
| 64 |
file = g_mapped_file_new_from_fd (fd, FALSE, error); |
| 65 |
if (!file) |
| 66 |
return NULL; |
| 67 |
|
| 68 |
return g_mapped_file_get_bytes (file); |
| 69 |
} |
| 70 |
|
| 71 |
static void |
| 72 |
on_open_messages (GObject *proxy, |
| 73 |
GAsyncResult *result, |
| 74 |
gpointer user_data) |
| 75 |
{ |
| 76 |
g_autoptr (GBytes) bytes = NULL; |
| 77 |
g_autoptr (GTask) task = NULL; |
| 78 |
g_autoptr (GError) error = NULL; |
| 79 |
GListModel *messages; |
| 80 |
|
| 81 |
g_assert (G_IS_TASK (user_data)); |
| 82 |
|
| 83 |
task = user_data; |
| 84 |
|
| 85 |
bytes = on_open_messages_get_bytes (G_DBUS_PROXY (proxy), result, &error); |
| 86 |
if (error) { |
| 87 |
g_task_return_error (task, g_steal_pointer (&error)); |
| 88 |
return; |
| 89 |
} |
| 90 |
messages = lcb_util_load_messages_from_bytes (bytes, &error); |
| 91 |
if (!messages) { |
| 92 |
g_task_return_error (task, g_steal_pointer (&error)); |
| 93 |
return; |
| 94 |
} |
| 95 |
g_task_return_pointer (task, messages, g_object_unref); |
| 96 |
} |
| 97 |
|
| 98 |
void |
| 99 |
lcb_cbd_get_messages (GCancellable *cancellable, |
| 100 |
GAsyncReadyCallback callback, |
| 101 |
gpointer user_data) |
| 102 |
{ |
| 103 |
g_autoptr (GTask) task = NULL; |
| 104 |
LcbDBusCbd *proxy = lcb_get_proxy (); |
| 105 |
|
| 106 |
g_return_if_fail (LCB_DBUS_IS_CBD (proxy)); |
| 107 |
|
| 108 |
task = g_task_new (NULL, cancellable, callback, user_data); |
| 109 |
g_task_set_source_tag (task, lcb_cbd_get_messages); |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
g_dbus_proxy_call_with_unix_fd_list (G_DBUS_PROXY (proxy), |
| 115 |
"OpenMessages", |
| 116 |
NULL, |
| 117 |
G_DBUS_CALL_FLAGS_NONE, |
| 118 |
-1, |
| 119 |
NULL, |
| 120 |
cancellable, |
| 121 |
on_open_messages, |
| 122 |
g_steal_pointer (&task)); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
GListModel * |
| 135 |
lcb_cbd_get_messages_finish (GAsyncResult *result, GError **error) |
| 136 |
{ |
| 137 |
GTask *task; |
| 138 |
|
| 139 |
g_return_val_if_fail (G_IS_TASK (result), NULL); |
| 140 |
|
| 141 |
task = G_TASK (result); |
| 142 |
g_assert (g_task_get_source_tag (task) == lcb_cbd_get_messages); |
| 143 |
return g_task_propagate_pointer (task, error); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
GListModel * |
| 155 |
lcb_cbd_get_messages_sync (GError **error) |
| 156 |
{ |
| 157 |
g_autoptr (GMainContext) context = g_main_context_new (); |
| 158 |
g_autoptr (GMainLoop) loop = NULL; |
| 159 |
GListModel *messages; |
| 160 |
LcbSyncData data; |
| 161 |
|
| 162 |
g_main_context_push_thread_default (context); |
| 163 |
loop = g_main_loop_new (context, FALSE); |
| 164 |
|
| 165 |
data = (LcbSyncData) { |
| 166 |
.loop = loop, |
| 167 |
.res = NULL, |
| 168 |
}; |
| 169 |
|
| 170 |
lcb_cbd_get_messages (NULL, on_open_messages_sync, &data); |
| 171 |
g_main_loop_run (data.loop); |
| 172 |
|
| 173 |
messages = lcb_cbd_get_messages_finish (data.res, error); |
| 174 |
|
| 175 |
if (data.res) |
| 176 |
g_object_unref (data.res); |
| 177 |
g_main_context_pop_thread_default (context); |
| 178 |
|
| 179 |
return messages; |
| 180 |
} |