#include <gtk/gtk.h>
#include <glade/glade.h>
-#include <libempathy/empathy-session.h>
#include <libempathy/empathy-contact-manager.h>
#include <libempathy/gossip-debug.h>
#include <libempathy/gossip-utils.h>
#define COMPOSING_STOP_TIMEOUT 5
struct _GossipChatPriv {
- EmpathyTpChat *tp_chat;
- GossipChatWindow *window;
-
- GtkTooltips *tooltips;
- guint composing_stop_timeout_id;
- gboolean sensitive;
- gchar *id;
+ EmpathyContactManager *manager;
+ EmpathyTpChat *tp_chat;
+ GossipChatWindow *window;
+
+ GtkTooltips *tooltips;
+ guint composing_stop_timeout_id;
+ gboolean sensitive;
+ gchar *id;
+ GSList *sent_messages;
+ gint sent_messages_index;
/* Used to automatically shrink a window that has temporarily
* grown due to long input.
*/
- gint padding_height;
- gint default_window_height;
- gint last_input_height;
- gboolean vscroll_visible;
+ gint padding_height;
+ gint default_window_height;
+ gint last_input_height;
+ gboolean vscroll_visible;
};
typedef struct {
static void chat_message_received_cb (EmpathyTpChat *tp_chat,
GossipMessage *message,
GossipChat *chat);
+void chat_sent_message_add (GossipChat *chat,
+ const gchar *str);
+const gchar * chat_sent_message_get_next (GossipChat *chat);
+const gchar * chat_sent_message_get_last (GossipChat *chat);
static gboolean chat_input_key_press_event_cb (GtkWidget *widget,
GdkEventKey *event,
GossipChat *chat);
static void chat_composing_stop (GossipChat *chat);
static void chat_composing_remove_timeout (GossipChat *chat);
static gboolean chat_composing_stop_timeout_cb (GossipChat *chat);
+static void chat_state_changed_cb (EmpathyTpChat *tp_chat,
+ GossipContact *contact,
+ TelepathyChannelChatState state,
+ GossipChat *chat);
enum {
COMPOSING,
priv = GET_PRIV (chat);
+ priv->manager = empathy_contact_manager_new ();
priv->tooltips = gtk_tooltips_new ();
-
priv->default_window_height = -1;
priv->vscroll_visible = FALSE;
priv->sensitive = TRUE;
+ priv->sent_messages = NULL;
+ priv->sent_messages_index = -1;
g_signal_connect (chat->input_text_view,
"key_press_event",
"changed",
G_CALLBACK (chat_input_text_buffer_changed_cb),
chat);
- g_signal_connect (GOSSIP_CHAT (chat)->view,
+ g_signal_connect (chat->view,
"focus_in_event",
G_CALLBACK (chat_text_view_focus_in_event_cb),
chat);
gossip_debug (DEBUG_DOMAIN, "Finalized: %p", object);
+ g_slist_foreach (priv->sent_messages, (GFunc) g_free, NULL);
+ g_slist_free (priv->sent_messages);
+
chat_composing_remove_timeout (chat);
- g_object_unref (GOSSIP_CHAT (object)->account);
+ g_object_unref (chat->account);
+ g_object_unref (priv->manager);
if (priv->tp_chat) {
g_object_unref (priv->tp_chat);
}
+ g_free (priv->id);
+
G_OBJECT_CLASS (gossip_chat_parent_class)->finalize (object);
}
priv = GET_PRIV (chat);
- if (msg == NULL || msg[0] == '\0') {
+ if (G_STR_EMPTY (msg)) {
return;
}
+ chat_sent_message_add (chat, msg);
+
if (g_str_has_prefix (msg, "/clear")) {
gossip_chat_view_clear (chat->view);
return;
g_signal_emit_by_name (chat, "new-message", message);
}
+void
+chat_sent_message_add (GossipChat *chat,
+ const gchar *str)
+{
+ GossipChatPriv *priv;
+ GSList *list;
+ GSList *item;
+
+ priv = GET_PRIV (chat);
+
+ /* Save the sent message in our repeat buffer */
+ list = priv->sent_messages;
+
+ /* Remove any other occurances of this msg */
+ while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
+ list = g_slist_remove_link (list, item);
+ g_free (item->data);
+ g_slist_free1 (item);
+ }
+
+ /* Trim the list to the last 10 items */
+ while (g_slist_length (list) > 10) {
+ item = g_slist_last (list);
+ if (item) {
+ list = g_slist_remove_link (list, item);
+ g_free (item->data);
+ g_slist_free1 (item);
+ }
+ }
+
+ /* Add new message */
+ list = g_slist_prepend (list, g_strdup (str));
+
+ /* Set list and reset the index */
+ priv->sent_messages = list;
+ priv->sent_messages_index = -1;
+}
+
+const gchar *
+chat_sent_message_get_next (GossipChat *chat)
+{
+ GossipChatPriv *priv;
+ gint max;
+
+ priv = GET_PRIV (chat);
+
+ if (!priv->sent_messages) {
+ gossip_debug (DEBUG_DOMAIN,
+ "No sent messages, next message is NULL");
+ return NULL;
+ }
+
+ max = g_slist_length (priv->sent_messages) - 1;
+
+ if (priv->sent_messages_index < max) {
+ priv->sent_messages_index++;
+ }
+
+ gossip_debug (DEBUG_DOMAIN,
+ "Returning next message index:%d",
+ priv->sent_messages_index);
+
+ return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
+}
+
+const gchar *
+chat_sent_message_get_last (GossipChat *chat)
+{
+ GossipChatPriv *priv;
+
+ g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
+
+ priv = GET_PRIV (chat);
+
+ if (!priv->sent_messages) {
+ gossip_debug (DEBUG_DOMAIN,
+ "No sent messages, last message is NULL");
+ return NULL;
+ }
+
+ if (priv->sent_messages_index >= 0) {
+ priv->sent_messages_index--;
+ }
+
+ gossip_debug (DEBUG_DOMAIN,
+ "Returning last message index:%d",
+ priv->sent_messages_index);
+
+ return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
+}
+
static gboolean
chat_input_key_press_event_cb (GtkWidget *widget,
GdkEventKey *event,
return TRUE;
}
+ /* Catch ctrl+up/down so we can traverse messages we sent */
+ if ((event->state & GDK_CONTROL_MASK) &&
+ (event->keyval == GDK_Up ||
+ event->keyval == GDK_Down)) {
+ GtkTextBuffer *buffer;
+ const gchar *str;
+
+ buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
+
+ if (event->keyval == GDK_Up) {
+ str = chat_sent_message_get_next (chat);
+ } else {
+ str = chat_sent_message_get_last (chat);
+ }
+
+ g_signal_handlers_block_by_func (buffer,
+ chat_input_text_buffer_changed_cb,
+ chat);
+ gtk_text_buffer_set_text (buffer, str ? str : "", -1);
+ g_signal_handlers_unblock_by_func (buffer,
+ chat_input_text_buffer_changed_cb,
+ chat);
+
+ return TRUE;
+ }
+
/* Catch enter but not ctrl/shift-enter */
if (IS_ENTER (event->keyval) && !(event->state & GDK_SHIFT_MASK)) {
GtkTextView *view;
}
text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
+
if (IS_ENTER (event->keyval) && (event->state & GDK_SHIFT_MASK)) {
/* Newline for shift-enter. */
return FALSE;
/* Just restart the timeout */
chat_composing_remove_timeout (chat);
} else {
- /* FIXME:
- gossip_session_send_composing (gossip_app_get_session (),
- priv->contact, TRUE);
- */
+ empathy_tp_chat_set_state (priv->tp_chat,
+ TP_CHANNEL_CHAT_STATE_COMPOSING);
}
priv->composing_stop_timeout_id = g_timeout_add (
priv = GET_PRIV (chat);
chat_composing_remove_timeout (chat);
- /* FIXME:
- gossip_session_send_composing (gossip_app_get_session (),
- priv->contact, FALSE);*/
+ empathy_tp_chat_set_state (priv->tp_chat,
+ TP_CHANNEL_CHAT_STATE_ACTIVE);
}
static void
priv = GET_PRIV (chat);
priv->composing_stop_timeout_id = 0;
- /* FIXME:
- gossip_session_send_composing (gossip_app_get_session (),
- priv->contact, FALSE);*/
+ empathy_tp_chat_set_state (priv->tp_chat,
+ TP_CHANNEL_CHAT_STATE_PAUSED);
return FALSE;
}
+static void
+chat_state_changed_cb (EmpathyTpChat *tp_chat,
+ GossipContact *contact,
+ TelepathyChannelChatState state,
+ GossipChat *chat)
+{
+ /* FIXME: not yet implemented */
+}
+
gboolean
gossip_chat_get_is_command (const gchar *str)
{
return NULL;
}
-GdkPixbuf *
-gossip_chat_get_status_pixbuf (GossipChat *chat)
+const gchar *
+gossip_chat_get_status_icon_name (GossipChat *chat)
{
g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
- if (GOSSIP_CHAT_GET_CLASS (chat)->get_status_pixbuf) {
- return GOSSIP_CHAT_GET_CLASS (chat)->get_status_pixbuf (chat);
+ if (GOSSIP_CHAT_GET_CLASS (chat)->get_status_icon_name) {
+ return GOSSIP_CHAT_GET_CLASS (chat)->get_status_icon_name (chat);
}
return NULL;
GossipContact *
gossip_chat_get_own_contact (GossipChat *chat)
{
- EmpathyContactManager *manager;
+ GossipChatPriv *priv;
g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
- manager = empathy_session_get_contact_manager ();
+ priv = GET_PRIV (chat);
- return empathy_contact_manager_get_own (manager, chat->account);
+ return empathy_contact_manager_get_own (priv->manager, chat->account);
}
GtkWidget *
g_signal_connect (tp_chat, "message-received",
G_CALLBACK (chat_message_received_cb),
chat);
+ g_signal_connect (tp_chat, "chat-state-changed",
+ G_CALLBACK (chat_state_changed_cb),
+ chat);
g_signal_connect (tp_chat, "destroy",
G_CALLBACK (chat_destroy_cb),
chat);
g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
- window = gossip_chat_get_window (GOSSIP_CHAT (chat));
+ window = gossip_chat_get_window (chat);
if (!window) {
return TRUE;
}