/*
Reminder by Dale Swanson November 16th, 2009
A plugin for the program Pidgin that provides a repeating chime as a reminder of unviewed messages.
*/

#define PURPLE_PLUGINS

#include <glib.h>

#ifndef G_GNUC_NULL_TERMINATED
#  if __GNUC__ >= 4
#    define G_GNUC_NULL_TERMINATED __attribute__((__sentinel__))
#  else
#    define G_GNUC_NULL_TERMINATED
#  endif /* __GNUC__ >= 4 */
#endif /* G_GNUC_NULL_TERMINATED */

#include "plugin.h"
#include "version.h"
#include "sound.h"
#include "signals.h"
#include "gtkconv.h"

int viewed = 0; //a flag to store if the conversation window has been viewed or not
int waiting = 0; //a flag to store if there is a message waiting
int delaycount = 0; //used to delay the first chime

gboolean chime()
{//makes the sound
	PurpleConversation *conv = NULL;
	delaycount = delaycount + 1; //delay the first chime, it won't sound until delaycount > 10 (or whatever you make it)
	if (viewed == 1) return FALSE;//if the viewed flag has been set don't sound the chime
	if (delaycount >= 10) purple_sound_play_event(PURPLE_SOUND_CHAT_NICK, purple_conversation_get_account(conv));//makes the actual sound
	//using chat nick since it isn't used by me for anything else, possibly changed later
	
	return TRUE;
  }

void notify(PurpleConversation *conv)
{//time to turn on the sound
	viewed = 0;//the window hasn't been viewed
    if (waiting == 0) purple_timeout_add_seconds(5, chime, 0);//if this is the first message then add the chime
	waiting = 1;//now there is a message waiting
}

void unnotify(PurpleConversation *conv)
{//time to turn off the sound
	viewed = 1;
	waiting = 0;
	delaycount = 0;
	chime(); //now that we set the flags we call chime so that it will return false
}

gboolean message_displayed(PurpleAccount *account, const char *who, char *message, PurpleConversation *conv, PurpleMessageFlags flags)
{//a new message received so we must notify
	if (flags == 2) notify(conv); // received message notify
	if (flags == 1) unnotify(conv); //sent message unnotify
	//away recv flags type delay 2 16404 1 1024
	//recv recv flags type delay 2 2 1 1024
	//send recv flags type delay 2 1 1 1024
	return FALSE;
}

int attach_signals(PurpleConversation *conv)
{//ataches GDK signals
	PidginConversation *gtkconv = NULL;

	gtkconv = PIDGIN_CONVERSATION(conv);
	//focus the convo, entry = text entry box, inhtml = actual convo
	g_signal_connect(G_OBJECT(gtkconv->entry), "focus-in-event", G_CALLBACK(unnotify), conv);
	g_signal_connect(G_OBJECT(gtkconv->imhtml), "focus-in-event", G_CALLBACK(unnotify), conv);
	//click a mouse button on window
	g_signal_connect(G_OBJECT(gtkconv->entry), "button-press-event", G_CALLBACK(unnotify), conv);
	g_signal_connect(G_OBJECT(gtkconv->imhtml), "button-press-event", G_CALLBACK(unnotify), conv);
	//press a keyboard key
	g_signal_connect(G_OBJECT(gtkconv->entry), "key-press-event", G_CALLBACK(unnotify), conv);
	return 0;
}

static gboolean plugin_load(PurplePlugin *plugin)
{
	void *purple_conv_handle = purple_conversations_get_handle(); //used for the purple signals
	void *pidgin_conv_handle = pidgin_conversations_get_handle(); //used for pidgin signals

	purple_signal_connect(purple_conv_handle, "conversation-created", plugin,
	                    PURPLE_CALLBACK(attach_signals), NULL); //when a new im window appears we can attach the GTK signals
	purple_signal_connect(pidgin_conv_handle, "displayed-im-msg", plugin, //pidgin signals
	                    PURPLE_CALLBACK(message_displayed), NULL); //works new im message
    purple_signal_connect(purple_conv_handle, "deleting-conversation", plugin, //purple signals
                        PURPLE_CALLBACK(unnotify), NULL); //works close im window
	return TRUE;
}

static PurplePluginInfo info = {
    PURPLE_PLUGIN_MAGIC,
    PURPLE_MAJOR_VERSION,
    PURPLE_MINOR_VERSION,
    PURPLE_PLUGIN_STANDARD,
    NULL,
    0,
    NULL,
    PURPLE_PRIORITY_DEFAULT,

    "core-daleswanson-reminder",
    "Reminder",
    "0.1",

    "Provides a repeating chime as a reminder of a new IM.",          
    "A plugin for the program Pidgin that provides a repeating chime as a reminder of unviewed messages.",          
    "Dale Swanson",                          
    "http://daleswanson.org <crazysteve138@hotmail.com>",     
    
    plugin_load,                   
    NULL,                          
    NULL,                          
	
    NULL,                          
    NULL,                          
    NULL,                        
    NULL,                   
    NULL,                          
    NULL,                          
    NULL,                          
    NULL                           
};                               

static void                        
init_plugin(PurplePlugin *plugin)
{                                  
}

PURPLE_INIT_PLUGIN(reminder, init_plugin, info)
