Mercurial > pidgin
changeset 11417:a9ec436535bc
[gaim-migrate @ 13654]
- Fixed up for dbus
- Receives MM specific messages.
- Handshaking protocol complete.
NOTE: stuff is commented for testing. Will be fixed shortly.
committer: Tailor Script <tailor@pidgin.im>
author | Christian Muise <christian.muise@gmail.com> |
---|---|
date | Fri, 02 Sep 2005 00:40:22 +0000 |
parents | f0b84eb8f0cb |
children | 29aaa7d29015 |
files | plugins/musicmessaging/musicmessaging.c |
diffstat | 1 files changed, 253 insertions(+), 115 deletions(-) [+] |
line wrap: on
line diff
--- a/plugins/musicmessaging/musicmessaging.c Thu Sep 01 22:45:07 2005 +0000 +++ b/plugins/musicmessaging/musicmessaging.c Fri Sep 02 00:40:22 2005 +0000 @@ -44,56 +44,6 @@ #define MUSICMESSAGING_START_MSG "A music messaging session has been requested. Please click the MM icon to accept." #define MUSICMESSAGING_CONFIRM_MSG "Music messaging session confirmed." -/* Globals */ -/* List of sessions */ -GList *conversations; - -/* Pointer to this plugin */ -GaimPlugin *plugin_pointer; - -/* Define types needed for DBus */ -DBusGConnection *connection; -DBusGProxy *proxy; -#define DBUS_SERVICE_GSCORE "org.gscore.GScoreService" -#define DBUS_PATH_GSCORE "/org/gscore/GScoreObject" -#define DBUS_INTERFACE_GSCORE "org.gscore.GScoreInterface" - -/* Define the functions to export for use with DBus */ -DBUS_EXPORT void music_messaging_change_request (const char *command, const char *parameters); -DBUS_EXPORT void music_messaging_change_confirmed (const char *command, const char *parameters); -DBUS_EXPORT void music_messaging_change_failed (const char *id, const char *command, const char *parameters); -DBUS_EXPORT void music_messaging_done_session (void); - -/* This file has been generated by the #dbus-analize-functions.py - script. It contains dbus wrappers for the four functions declared - above. */ -#include "music-messaging-bindings.c" - -/* Exported functions */ -void music_messaging_change_request(const char *command, const char *parameters) -{ - gaim_notify_message(plugin_pointer, GAIM_NOTIFY_MSG_INFO, command, - parameters, NULL, NULL, NULL); -} - -void music_messaging_change_confirmed(const char *command, const char *parameters) -{ - gaim_notify_message(plugin_pointer, GAIM_NOTIFY_MSG_INFO, command, - parameters, NULL, NULL, NULL); -} - -void music_messaging_change_failed(const char *id, const char *command, const char *parameters) -{ - gaim_notify_message(plugin_pointer, GAIM_NOTIFY_MSG_INFO, command, - parameters, NULL, NULL, NULL); -} - -void music_messaging_done_session(void) -{ - gaim_notify_message(plugin_pointer, GAIM_NOTIFY_MSG_INFO, "Session", - "Session Complete", NULL, NULL, NULL); -} - typedef struct { GaimConversation *conv; /* pointer to the conversation */ GtkWidget *seperator; /* seperator in the conversation */ @@ -115,30 +65,178 @@ static void conv_destroyed(GaimConversation *conv); static gboolean intercept_sent(GaimAccount *account, GaimConversation *conv, char **message, void* pData); static gboolean intercept_received(GaimAccount *account, char **sender, char **message, GaimConversation *conv, int *flags); +static gboolean send_change_request (const int session, const char *id, const char *command, const char *parameters); +static gboolean send_change_confirmed (const int session, const char *command, const char *parameters); +static void session_end (MMConversation *mmconv); + +/* Globals */ +/* List of sessions */ +GList *conversations; + +/* Pointer to this plugin */ +GaimPlugin *plugin_pointer; + +/* Define types needed for DBus */ +DBusGConnection *connection; +DBusGProxy *proxy; +#define DBUS_SERVICE_GSCORE "org.gscore.GScoreService" +#define DBUS_PATH_GSCORE "/org/gscore/GScoreObject" +#define DBUS_INTERFACE_GSCORE "org.gscore.GScoreInterface" + +/* Define the functions to export for use with DBus */ +DBUS_EXPORT void music_messaging_change_request (const int session, const char *command, const char *parameters); +DBUS_EXPORT void music_messaging_change_confirmed (const int session, const char *command, const char *parameters); +DBUS_EXPORT void music_messaging_change_failed (const int session, const char *id, const char *command, const char *parameters); +DBUS_EXPORT void music_messaging_done_session (const int session); + +/* This file has been generated by the #dbus-analize-functions.py + script. It contains dbus wrappers for the four functions declared + above. */ +#include "music-messaging-bindings.c" + +/* Exported functions */ +void music_messaging_change_request(const int session, const char *command, const char *parameters) +{ + + MMConversation *mmconv = (MMConversation *)g_list_nth_data(conversations, session); + + if (mmconv->started) + { + if (mmconv->originator) + { + char *name = (mmconv->conv)->name; + send_change_request (session, name, command, parameters); + } else + { + GString *to_send = g_string_new(""); + g_string_append_printf(to_send, "##MM## request %s %s##MM##", command, parameters); + + gaim_conv_im_send(GAIM_CONV_IM(mmconv->conv), to_send->str); + + gaim_debug_misc("Sent request: %s\n", to_send->str); + } + } + +} + +void music_messaging_change_confirmed(const int session, const char *command, const char *parameters) +{ + gaim_notify_message(plugin_pointer, GAIM_NOTIFY_MSG_INFO, command, + parameters, NULL, NULL, NULL); + + MMConversation *mmconv = (MMConversation *)g_list_nth_data(conversations, session); + + if (mmconv->started) + { + if (mmconv->originator) + { + GString *to_send = g_string_new(""); + g_string_append_printf(to_send, "##MM## confirm %s %s##MM##", command, parameters); + + gaim_conv_im_send(GAIM_CONV_IM(mmconv->conv), to_send->str); + send_change_confirmed(session, command, parameters); + } else + { + /* Do nothing. If they aren't the originator, then they can't confirm. */ + } + } + +} + +void music_messaging_change_failed(const int session, const char *id, const char *command, const char *parameters) +{ + gaim_notify_message(plugin_pointer, GAIM_NOTIFY_MSG_INFO, command, + parameters, NULL, NULL, NULL); + + MMConversation *mmconv = (MMConversation *)g_list_nth_data(conversations, session); + + if (mmconv->started) + { + if (mmconv->originator) + { + GString *to_send = g_string_new(""); + g_string_append_printf(to_send, "##MM## failed %s %s %s##MM##", id, command, parameters); + + gaim_conv_im_send(GAIM_CONV_IM(mmconv->conv), to_send->str); + } else + { + /* Do nothing. If they aren't the originator, then they can't confirm. */ + } + } +} + +void music_messaging_done_session(const int session) +{ + gaim_notify_message(plugin_pointer, GAIM_NOTIFY_MSG_INFO, "Session", + "Session Complete", NULL, NULL, NULL); + + MMConversation *mmconv = (MMConversation *)g_list_nth_data(conversations, session); + + session_end(mmconv); +} /* DBus commands that can be sent to the editor */ +G_BEGIN_DECLS +DBusConnection *gaim_dbus_get_connection(void); +G_END_DECLS -void send_change_request (const char *id, const char *command, const char *parameters) +static gboolean send_change_request (const int session, const char *id, const char *command, const char *parameters) { - dbus_g_proxy_call_no_reply(proxy, "GscoreChangeRequest", - G_TYPE_STRING, &id, - G_TYPE_STRING, &command, - G_TYPE_STRING, ¶meters, - G_TYPE_INVALID); + DBusMessage *message; + + /* Create the signal we need */ + message = dbus_message_new_signal (DBUS_PATH_GAIM, DBUS_INTERFACE_GAIM, "GscoreChangeRequest"); + + /* Append the string "Ping!" to the signal */ + dbus_message_append_args (message, + DBUS_TYPE_INT32, &session, + DBUS_TYPE_STRING, &id, + DBUS_TYPE_STRING, &command, + DBUS_TYPE_STRING, ¶meters, + DBUS_TYPE_INVALID); + + /* Send the signal */ + dbus_connection_send (gaim_dbus_get_connection(), message, NULL); + + /* Free the signal now we have finished with it */ + dbus_message_unref (message); + + /* Tell the user we sent a signal */ + g_printerr("Sent change request signal: %d %s %s %s\n", session, id, command, parameters); + + return TRUE; } -void send_change_confirmed (const char *command, const char *parameters) +static gboolean send_change_confirmed (const int session, const char *command, const char *parameters) { - dbus_g_proxy_call_no_reply(proxy, "GscoreChangeConfirmed", - G_TYPE_STRING, &command, - G_TYPE_STRING, ¶meters, - G_TYPE_INVALID); + DBusMessage *message; + + /* Create the signal we need */ + message = dbus_message_new_signal (DBUS_PATH_GAIM, DBUS_INTERFACE_GAIM, "GscoreChangeConfirmed"); + + /* Append the string "Ping!" to the signal */ + dbus_message_append_args (message, + DBUS_TYPE_INT32, &session, + DBUS_TYPE_STRING, &command, + DBUS_TYPE_STRING, ¶meters, + DBUS_TYPE_INVALID); + + /* Send the signal */ + dbus_connection_send (gaim_dbus_get_connection(), message, NULL); + + /* Free the signal now we have finished with it */ + dbus_message_unref (message); + + /* Tell the user we sent a signal */ + g_printerr("Sent change confirmed signal.\n"); + + return TRUE; } -static MMConversation* -mmconv_from_conv(GaimConversation *conv) +static int +mmconv_from_conv_loc(GaimConversation *conv) { MMConversation *mmconv_current = NULL; guint i; @@ -148,37 +246,16 @@ mmconv_current = (MMConversation *)g_list_nth_data(conversations, i); if (conv == mmconv_current->conv) { - return mmconv_current; + return i; } } - return NULL; + return -1; } -static gboolean -init_mm_dbus_connection() +static MMConversation* +mmconv_from_conv(GaimConversation *conv) { - GError *error = NULL; - g_type_init (); - - connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error); - if (!connection) - { - g_printerr ("Failed to open connection to bus: %s\n", error->message); - g_error_free (error); - return FALSE; - } - - proxy = dbus_g_proxy_new_for_name (connection, - DBUS_SERVICE_GSCORE, - DBUS_PATH_GSCORE, - DBUS_INTERFACE_GSCORE); - if (!proxy) - { - g_printerr("Failed to create the proxy: %s\n", error->message); - return FALSE; - } - - return TRUE; + return (MMConversation *)g_list_nth_data(conversations, mmconv_from_conv_loc(conv)); } static gboolean @@ -188,8 +265,6 @@ code wouldn't know about our functions. */ GAIM_DBUS_REGISTER_BINDINGS(plugin); - /* We need to initialize the dbus stuff */ - init_mm_dbus_connection(); gaim_notify_message(plugin, GAIM_NOTIFY_MSG_INFO, "Welcome", "Welcome to music messaging.", NULL, NULL, NULL); @@ -210,7 +285,7 @@ plugin, GAIM_CALLBACK(conv_destroyed), NULL); /* Listen for sending/receiving messages to replace tags */ - gaim_signal_connect(conv_list_handle, "writing-im-msg", + gaim_signal_connect(conv_list_handle, "displaying-im-msg", plugin, GAIM_CALLBACK(intercept_sent), NULL); gaim_signal_connect(conv_list_handle, "receiving-im-msg", plugin, GAIM_CALLBACK(intercept_received), NULL); @@ -238,37 +313,32 @@ static gboolean intercept_sent(GaimAccount *account, GaimConversation *conv, char **message, void* pData) { - GaimConnection *connection = gaim_conversation_get_gc(conv); - const char *convName = gaim_conversation_get_name(conv); if (0 == strncmp(*message, MUSICMESSAGING_PREFIX, strlen(MUSICMESSAGING_PREFIX))) { gaim_debug_misc("gaim-musicmessaging", "Sent MM Message: %s\n", *message); - serv_send_im(connection, convName, *message, GAIM_MESSAGE_SEND); message = 0; } else if (0 == strncmp(*message, MUSICMESSAGING_START_MSG, strlen(MUSICMESSAGING_START_MSG))) { gaim_debug_misc("gaim-musicmessaging", "Sent MM request.\n"); - serv_send_im(connection, convName, *message, GAIM_MESSAGE_SEND); message = 0; } else if (0 == strncmp(*message, MUSICMESSAGING_CONFIRM_MSG, strlen(MUSICMESSAGING_CONFIRM_MSG))) { gaim_debug_misc("gaim-musicmessaging", "Sent MM confirm.\n"); - serv_send_im(connection, convName, *message, GAIM_MESSAGE_SEND); message = 0; } else if (0 == strncmp(*message, "test1", strlen("test1"))) { gaim_debug_misc("gaim-musicmessaging", "\n\nTEST 1\n\n"); - send_change_request("test-id", "test-command", "test-parameters"); + send_change_request(0, "test-id", "test-command", "test-parameters"); return FALSE; } else if (0 == strncmp(*message, "test2", strlen("test2"))) { gaim_debug_misc("gaim-musicmessaging", "\n\nTEST 2\n\n"); - send_change_confirmed("test-command", "test-parameters"); + send_change_confirmed(1, "test-command", "test-parameters"); return FALSE; } else @@ -287,10 +357,64 @@ gaim_debug_misc("gaim-musicmessaging", "Intercepted: %s\n", *message); if (strstr(*message, MUSICMESSAGING_PREFIX)) { - gaim_debug_misc("gaim-musicmessaging", "Received MM Message: %s\n", - strtok(strstr(*message, MUSICMESSAGING_PREFIX), "<")); + char *parsed_message = strtok(strstr(*message, MUSICMESSAGING_PREFIX), "<"); + gaim_debug_misc("gaim-musicmessaging", "Received an MM Message: %s\n", parsed_message); - /* DEAL WITH A MM MESSAGE */ + if (mmconv->started) + { + if (strstr(parsed_message, "request")) + { + if (mmconv->originator) + { + gaim_debug_misc("gaim-musicmessaging", "Sending request to gscore.\n"); + + int session = mmconv_from_conv_loc(conv); + char *id = (mmconv->conv)->name; + + /* Get past the first two terms - '##MM##' and 'request' */ + strtok(parsed_message, " "); /* '##MM##' */ + strtok(NULL, " "); /* 'request' */ + + char *command = strtok(NULL, " "); + char *parameters = strtok(NULL, "#"); + + send_change_request (session, id, command, parameters); + + } + } else if (strstr(parsed_message, "confirm")) + { + if (!mmconv->originator) + { + gaim_debug_misc("gaim-musicmessaging", "Sending confirmation to gscore.\n"); + + int session = mmconv_from_conv_loc(conv); + + /* Get past the first two terms - '##MM##' and 'confirm' */ + strtok(parsed_message, " "); /* '##MM##' */ + strtok(NULL, " "); /* 'confirm' */ + + char *command = strtok(NULL, " "); + char *parameters = strtok(NULL, "#"); + + send_change_confirmed (session, command, parameters); + } + } else if (strstr(parsed_message, "failed")) + { + /* Get past the first two terms - '##MM##' and 'confirm' */ + strtok(parsed_message, " "); /* '##MM##' */ + strtok(NULL, " "); /* 'failed' */ + + char *id = strtok(NULL, " "); + char *command = strtok(NULL, " "); + /* char *parameters = strtok(NULL, "#"); DONT NEED PARAMETERS */ + + if ((mmconv->conv)->name == id) + { + gaim_notify_message(plugin_pointer, GAIM_NOTIFY_MSG_ERROR, "Music Messaging", + "There was a conflict in running the command:", command, NULL, NULL); + } + } + } message = 0; } @@ -345,25 +469,32 @@ return TRUE; } +static void session_end (MMConversation *mmconv) +{ + mmconv->started = FALSE; + mmconv->originator = FALSE; + mmconv->requested = FALSE; + kill_editor(mmconv); +} + static void music_button_toggled (GtkWidget *widget, gpointer data) { + MMConversation *mmconv = mmconv_from_conv(((MMConversation *) data)->conv); if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget))) { - if (((MMConversation *) data)->requested) - { - start_session((MMConversation *) data); - send_request_confirmed((MMConversation *) data); - } + //if (((MMConversation *) data)->requested) + //{ + if (!mmconv){ g_printerr("DEBUG: About to bust\n"); } + start_session(mmconv); + send_request_confirmed(mmconv); + /*} else { ((MMConversation *) data)->originator = TRUE; send_request((MMConversation *) data); - } + }*/ } else { - ((MMConversation *)data)->started = FALSE; - ((MMConversation *)data)->originator = FALSE; - ((MMConversation *)data)->requested = FALSE; - kill_editor((MMConversation *) data); + session_end((MMConversation *)data); } } @@ -377,10 +508,17 @@ static void run_editor (MMConversation *mmconv) { GError *spawn_error = NULL; - gchar * args[2]; + gchar * args[4]; args[0] = (gchar *)gaim_prefs_get_string("/plugins/gtk/musicmessaging/editor_path"); - args[1] = NULL; - if (!(g_spawn_async (".", args, NULL, 12, NULL, NULL, &(mmconv->pid), &spawn_error))) + + args[1] = "-session_id"; + GString *session_id = g_string_new(""); + g_string_sprintfa(session_id, "%d", mmconv_from_conv_loc(mmconv->conv)); + args[2] = session_id->str; + + args[3] = NULL; + + if (!(g_spawn_async (".", args, NULL, 4, NULL, NULL, &(mmconv->pid), &spawn_error))) { gaim_notify_error(plugin_pointer, "Error Running Editor", "The following error has occured:", spawn_error->message);