📄 musicmessaging.c
字号:
/* * Music messaging plugin for Purple * * Copyright (C) 2005 Christian Muise. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program 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 * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA. */#include "internal.h"#include "pidgin.h"#include "conversation.h"#include "gtkconv.h"#include "gtkplugin.h"#include "gtkutils.h"#include "notify.h"#include "version.h"#include "debug.h"#define DBUS_API_SUBJECT_TO_CHANGE#include <dbus/dbus.h>#include "dbus-maybe.h"#include "dbus-bindings.h"#include "dbus-server.h"#include "dbus-purple.h"#define MUSICMESSAGING_PLUGIN_ID "gtk-hazure-musicmessaging"#define MUSICMESSAGING_PREFIX "##MM##"#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.")typedef struct { PurpleConversation *conv; /* pointer to the conversation */ GtkWidget *seperator; /* seperator in the conversation */ GtkWidget *button; /* button in the conversation */ GPid pid; /* the pid of the score editor */ gboolean started; /* session has started and editor run */ gboolean originator; /* started the mm session */ gboolean requested; /* received a request to start a session */ } MMConversation;static gboolean start_session(MMConversation *mmconv);static void run_editor(MMConversation *mmconv);static void kill_editor(MMConversation *mmconv);static void add_button (MMConversation *mmconv);static void remove_widget (GtkWidget *button);static void init_conversation (PurpleConversation *conv);static void conv_destroyed(PurpleConversation *conv);static gboolean intercept_sent(PurpleAccount *account, const char *who, char **message, void* pData);static gboolean intercept_received(PurpleAccount *account, char **sender, char **message, PurpleConversation *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 */PurplePlugin *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); purple_conv_im_send(PURPLE_CONV_IM(mmconv->conv), to_send->str); purple_debug_misc("Sent request: %s\n", to_send->str); } } }void music_messaging_change_confirmed(const int session, const char *command, const char *parameters){ 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); purple_conv_im_send(PURPLE_CONV_IM(mmconv->conv), to_send->str); } 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){ MMConversation *mmconv = (MMConversation *)g_list_nth_data(conversations, session); purple_notify_message(plugin_pointer, PURPLE_NOTIFY_MSG_INFO, command, parameters, NULL, NULL, NULL); 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); purple_conv_im_send(PURPLE_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){ MMConversation *mmconv = (MMConversation *)g_list_nth_data(conversations, session); purple_notify_message(plugin_pointer, PURPLE_NOTIFY_MSG_INFO, "Session", "Session Complete", NULL, NULL, NULL); session_end(mmconv);}/* DBus commands that can be sent to the editor */G_BEGIN_DECLSDBusConnection *purple_dbus_get_connection(void);G_END_DECLSstatic gboolean send_change_request (const int session, const char *id, const char *command, const char *parameters){ DBusMessage *message; /* Create the signal we need */ message = dbus_message_new_signal (DBUS_PATH_PURPLE, DBUS_INTERFACE_PURPLE, "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 (purple_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;}static gboolean send_change_confirmed (const int session, const char *command, const char *parameters){ DBusMessage *message; /* Create the signal we need */ message = dbus_message_new_signal (DBUS_PATH_PURPLE, DBUS_INTERFACE_PURPLE, "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 (purple_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 intmmconv_from_conv_loc(PurpleConversation *conv){ GList *l; MMConversation *mmconv_current = NULL; guint i; i = 0; for (l = conversations; l != NULL; l = l->next) { mmconv_current = l->data; if (conv == mmconv_current->conv) { return i; } i++; } return -1;}static MMConversation*mmconv_from_conv(PurpleConversation *conv){ return (MMConversation *)g_list_nth_data(conversations, mmconv_from_conv_loc(conv));}static gbooleanplugin_load(PurplePlugin *plugin) { void *conv_list_handle; PURPLE_DBUS_RETURN_FALSE_IF_DISABLED(plugin); /* First, we have to register our four exported functions with the main purple dbus loop. Without this statement, the purple dbus code wouldn't know about our functions. */ PURPLE_DBUS_REGISTER_BINDINGS(plugin); /* Keep the plugin for reference (needed for notify's) */ plugin_pointer = plugin; /* Add the button to all the current conversations */ purple_conversation_foreach (init_conversation); /* Listen for any new conversations */ conv_list_handle = purple_conversations_get_handle(); purple_signal_connect(conv_list_handle, "conversation-created", plugin, PURPLE_CALLBACK(init_conversation), NULL); /* Listen for conversations that are ending */ purple_signal_connect(conv_list_handle, "deleting-conversation", plugin, PURPLE_CALLBACK(conv_destroyed), NULL); /* Listen for sending/receiving messages to replace tags */ purple_signal_connect(conv_list_handle, "sending-im-msg", plugin, PURPLE_CALLBACK(intercept_sent), NULL); purple_signal_connect(conv_list_handle, "receiving-im-msg", plugin, PURPLE_CALLBACK(intercept_received), NULL); return TRUE;}static gbooleanplugin_unload(PurplePlugin *plugin) { MMConversation *mmconv = NULL; while (conversations != NULL) { mmconv = conversations->data; conv_destroyed(mmconv->conv); } return TRUE;}static gbooleanintercept_sent(PurpleAccount *account, const char *who, char **message, void* pData){ if (0 == strncmp(*message, MUSICMESSAGING_PREFIX, strlen(MUSICMESSAGING_PREFIX))) { purple_debug_misc("purple-musicmessaging", "Sent MM Message: %s\n", *message); message = 0; } else if (0 == strncmp(*message, MUSICMESSAGING_START_MSG, strlen(MUSICMESSAGING_START_MSG))) { purple_debug_misc("purple-musicmessaging", "Sent MM request.\n"); return FALSE; } else if (0 == strncmp(*message, MUSICMESSAGING_CONFIRM_MSG, strlen(MUSICMESSAGING_CONFIRM_MSG))) { purple_debug_misc("purple-musicmessaging", "Sent MM confirm.\n"); return FALSE; } else if (0 == strncmp(*message, "test1", strlen("test1"))) { purple_debug_misc("purple-musicmessaging", "\n\nTEST 1\n\n"); send_change_request(0, "test-id", "test-command", "test-parameters"); return FALSE; } else if (0 == strncmp(*message, "test2", strlen("test2"))) { purple_debug_misc("purple-musicmessaging", "\n\nTEST 2\n\n"); send_change_confirmed(1, "test-command", "test-parameters"); return FALSE; } else { return FALSE; /* Do nothing...procceed as normal */ } return TRUE;}static gbooleanintercept_received(PurpleAccount *account, char **sender, char **message, PurpleConversation *conv, int *flags)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -