view plugins/timestamp.c @ 10787:c932d96a7c69

[gaim-migrate @ 12428] " Autocorrect in gaim... um... sucks. The problem is that the correction only happens AFTER you send a message. This means that you don't actually know what message you are sending until it is sent. Horrible. My new patch works AS YOU TYPE with the following rules: * You must be typing, not pasting in text. If you paste in the and there is a rule to correct the to the, it won't be corrected. This is because it would be impossible to go back and check all the text. * Check whole words ONLY. Partial words don't count. * If the dictionary word has capital letters in it, match only words with that capitalization. (FPT -> FTP doesn't match ftp.) * If the dictionary word doesn't have capital letters, preserve the capitalization the user used. (The code assumes three possible capitalization schemes: ALLCAPS, lowercase, and Proper.) Unless the solution has capital letters. In that case, force the capitalization used in the solution. * A word may have quote characters in it and still be a word. * If the user immediately hits the backspace key after a correction, undo the correction." --xkan " This patch is based off xkahn's work available at: http://sf.net/support/tracker.php?aid=300235 Most of the changes are his. I just changed his formatting to match the rest of Gaim more closely. Also, I made a couple coding style changes. The two biggest changes I made were: 1) I added some hash stuff to load_config. This removes duplicates. 2) I imported my list of text replacement strings as the default list in addition to xkahn's list. My list consisted primarily of the autocorrect entries from my copy of OpenOffice.org Writer. There were a few things I've added by hand. (For those familiar with my comments in #gaim, this list does NOT include anything like a pwn -> own replacement. If people want to talk like that, I'm not going to stop them. This list should be useful to anyone.) I also fixed a memory leak in load_config. A GtkListStore stores its own copy of strings, so there is no need to g_strdup them when appending to the GtkListStore. When I get a chance, I need to make it give an error if you try to set a duplicate "You type" word in the preferences. Otherwise, this patch should take care of everything. I would recommend considering this patch over xkahn's because it's a direct superset of his patches. However, I want to be very clear that most of the changes in this patch are his and he deserves the credit for them. " --rlaager committer: Tailor Script <tailor@pidgin.im>
author Luke Schierer <lschiere@pidgin.im>
date Thu, 07 Apr 2005 14:35:25 +0000
parents bb6fea81e770
children 50224ac8184d
line wrap: on
line source

/*
 * Gaim - iChat-like timestamps
 *
 * Copyright (C) 2002-2003, Sean Egan
 * Copyright (C) 2003, Chris J. Friesen <Darth_Sebulba04@yahoo.com>
 *
 * 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 "conversation.h"
#include "debug.h"
#include "prefs.h"
#include "signals.h"
#include "version.h"

#include "gtkimhtml.h"
#include "gtkplugin.h"
#include "gtkutils.h"

#define TIMESTAMP_PLUGIN_ID "gtk-timestamp"

/* Set the default to 5 minutes. */
static int interval = 5 * 60 * 1000;

static GSList *timestamp_timeouts = NULL;

static gboolean
do_timestamp(gpointer data)
{
	GaimConversation *c = (GaimConversation *)data;
	GaimGtkConversation *conv = GAIM_GTK_CONVERSATION(c);
	GtkTextIter iter;
	char mdate[7];
	int is_conversation_active;
	time_t tim = time(NULL);

	if (!g_list_find(gaim_get_conversations(), c))
		return FALSE;

	/* is_conversation_active is true if an im has been displayed since the last timestamp */
	is_conversation_active = GPOINTER_TO_INT(gaim_conversation_get_data(c, "timestamp-conv-active"));

	if (is_conversation_active){
		int y, height;
		GdkRectangle rect;
		gboolean scroll = TRUE;
		GtkWidget *imhtml = conv->imhtml;
		GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(imhtml));
		gtk_text_buffer_get_end_iter(buffer, &iter);
		gaim_conversation_set_data(c, "timestamp-conv-active", GINT_TO_POINTER(FALSE));
		strftime(mdate, sizeof(mdate), "\n%H:%M", localtime(&tim));
		gtk_text_view_get_visible_rect(GTK_TEXT_VIEW(imhtml), &rect);
		gtk_text_view_get_line_yrange(GTK_TEXT_VIEW(imhtml), &iter, &y, &height);
		if(((y + height) - (rect.y + rect.height)) > height
		   && gtk_text_buffer_get_char_count(buffer)){
			scroll = FALSE;
		}
		gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, mdate, strlen(mdate), "TIMESTAMP", NULL);
		if (scroll)
			gtk_imhtml_scroll_to_end(GTK_IMHTML(imhtml));
	}
	else
		gaim_conversation_set_data(c, "timestamp-enabled", GINT_TO_POINTER(FALSE));

	return TRUE;
}


static gboolean
timestamp_displaying_conv_msg(GaimAccount *account, GaimConversation *conv,
							  char **buffer, void *data)
{
	int is_timestamp_enabled;

	if (!g_list_find(gaim_get_conversations(), conv))
		return FALSE;

	/* set to true, since there has been an im since the last timestamp */
	gaim_conversation_set_data(conv, "timestamp-conv-active", GINT_TO_POINTER(TRUE));

	is_timestamp_enabled = GPOINTER_TO_INT(gaim_conversation_get_data(conv, "timestamp-enabled"));

	if (!is_timestamp_enabled){
		gaim_conversation_set_data(conv, "timestamp-enabled", GINT_TO_POINTER(TRUE));
		do_timestamp((gpointer)conv);
	}

	return FALSE;
}

static gboolean
timestamp_receiving_msg(GaimAccount *account, char **sender, char **buffer,
						GaimConversation *conv, int *flags, void *data)
{
	g_return_val_if_fail(conv != NULL, FALSE);

	return timestamp_displaying_conv_msg(account, conv, buffer, data);
}


static void timestamp_new_convo(GaimConversation *conv)
{
	GaimGtkConversation *c = GAIM_GTK_CONVERSATION(conv);

	if (!g_list_find(gaim_get_conversations(), conv))
		return;

	gtk_imhtml_show_comments(GTK_IMHTML(c->imhtml), FALSE);

	/*
	This if statement stops conversations that have already been initialized for timestamps
	from being reinitialized.  This prevents every active conversation from immediately being spammed
	with a new timestamp when the user modifies the timer interval.
	*/
	if (!gaim_conversation_get_data(conv, "timestamp-initialized")){
		GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(c->imhtml));
		gaim_conversation_set_data(conv, "timestamp-initialized", GINT_TO_POINTER(TRUE));
		gaim_conversation_set_data(conv, "timestamp-enabled", GINT_TO_POINTER(TRUE));
		gaim_conversation_set_data(conv, "timestamp-conv-active", GINT_TO_POINTER(TRUE));
		gtk_text_buffer_create_tag (buffer, "TIMESTAMP", "foreground", "#888888", "justification", GTK_JUSTIFY_CENTER, 
					    "weight", PANGO_WEIGHT_BOLD, NULL);
		do_timestamp(conv);
	}

	timestamp_timeouts = g_slist_append(timestamp_timeouts,
			GINT_TO_POINTER(g_timeout_add(interval, do_timestamp, conv)));
}


static void destroy_timer_list()
{
	GSList *to;

	for (to = timestamp_timeouts; to != NULL; to = to->next)
		g_source_remove(GPOINTER_TO_INT(to->data));

	g_slist_free(timestamp_timeouts);

	timestamp_timeouts = NULL;
}

static void init_timer_list()
{
	GList *cnvs;
	GaimConversation *c;

	if (timestamp_timeouts != NULL)
		destroy_timer_list();

	for (cnvs = gaim_get_conversations(); cnvs != NULL; cnvs = cnvs->next) {
		c = cnvs->data;
		timestamp_new_convo(c);
	}
}



static void set_timestamp(GtkWidget *button, GtkWidget *spinner) {
	int tm;

	tm = 0;

	tm = CLAMP(gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spinner)), 1, G_MAXINT);
	gaim_debug(GAIM_DEBUG_MISC, "timestamp", "setting time to %d mins\n", tm);

	tm = tm * 60 * 1000;

	interval = tm;
	gaim_prefs_set_int("/plugins/gtk/timestamp/interval", interval);

	destroy_timer_list();
	init_timer_list();
}

static GtkWidget *
get_config_frame(GaimPlugin *plugin)
{
	GtkWidget *ret;
	GtkWidget *frame, *label;
	GtkWidget *vbox, *hbox;
	GtkAdjustment *adj;
	GtkWidget *spinner, *button;

	ret = gtk_vbox_new(FALSE, 18);
	gtk_container_set_border_width (GTK_CONTAINER (ret), 12);

	frame = gaim_gtk_make_frame(ret, _("iChat Timestamp"));
	vbox = gtk_vbox_new(FALSE, 5);
	gtk_container_add(GTK_CONTAINER(frame), vbox);

	hbox = gtk_hbox_new(FALSE, 5);
	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);

	label = gtk_label_new(_("Delay"));
	gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 5);

	adj = (GtkAdjustment *)gtk_adjustment_new(interval/(60*1000), 1, G_MAXINT, 1, 0, 0);
	spinner = gtk_spin_button_new(adj, 0, 0);
	gtk_box_pack_start(GTK_BOX(hbox), spinner, TRUE, TRUE, 0);

	label = gtk_label_new(_("minutes."));
	gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 5);

	hbox = gtk_hbox_new(TRUE, 5);
	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);

	button = gtk_button_new_with_mnemonic(_("_Apply"));
	gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 5);
	g_signal_connect(G_OBJECT(button), "clicked",
					 G_CALLBACK(set_timestamp), spinner);

	gtk_widget_show_all(ret);
	return ret;
}

static gboolean
plugin_load(GaimPlugin *plugin)
{
	void *conv_handle = gaim_conversations_get_handle();

	init_timer_list();

	gaim_signal_connect(conv_handle, "conversation-created",
					plugin, GAIM_CALLBACK(timestamp_new_convo), NULL);

	/* record IM display events for each conversation */
	gaim_signal_connect(conv_handle, "receiving-im-msg",
					plugin, GAIM_CALLBACK(timestamp_receiving_msg), NULL);
	gaim_signal_connect(conv_handle, "displaying-im-msg",
					plugin, GAIM_CALLBACK(timestamp_displaying_conv_msg), NULL);

	interval = gaim_prefs_get_int("/plugins/gtk/timestamp/interval");

	return TRUE;
}



static gboolean
plugin_unload(GaimPlugin *plugin)
{
	GList *cnvs;

	destroy_timer_list();

	for (cnvs = gaim_get_conversations(); cnvs != NULL; cnvs = cnvs->next) {
		GaimConversation *c = cnvs->data;
		GaimGtkConversation *conv = GAIM_GTK_CONVERSATION(c);
		gtk_imhtml_show_comments(GTK_IMHTML(conv->imhtml), TRUE);
	}

	return TRUE;
}

static GaimGtkPluginUiInfo ui_info =
{
	get_config_frame
};

static GaimPluginInfo info =
{
	GAIM_PLUGIN_MAGIC,
	GAIM_MAJOR_VERSION,
	GAIM_MINOR_VERSION,
	GAIM_PLUGIN_STANDARD,                             /**< type           */
	GAIM_GTK_PLUGIN_TYPE,                             /**< ui_requirement */
	0,                                                /**< flags          */
	NULL,                                             /**< dependencies   */
	GAIM_PRIORITY_DEFAULT,                            /**< priority       */

	TIMESTAMP_PLUGIN_ID,                              /**< id             */
	N_("Timestamp"),                                  /**< name           */
	VERSION,                                          /**< version        */
	                                                  /**  summary        */
	N_("Adds iChat-style timestamps to conversations every N minutes."),
	                                                  /**  description    */
	N_("Adds iChat-style timestamps to conversations every N minutes."),
	"Sean Egan <bj91704@binghamton.edu>",             /**< author         */
	GAIM_WEBSITE,                                     /**< homepage       */

	plugin_load,                                      /**< load           */
	plugin_unload,                                    /**< unload         */
	NULL,                                             /**< destroy        */

	&ui_info,                                         /**< ui_info        */
	NULL,                                             /**< extra_info     */
	NULL,
	NULL
};

static void
init_plugin(GaimPlugin *plugin)
{
	gaim_prefs_add_none("/plugins/gtk/timestamp");
	gaim_prefs_add_int("/plugins/gtk/timestamp/interval", interval);
}

GAIM_INIT_PLUGIN(interval, init_plugin, info)