/*
 * Pidgin - Internet Messenger
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * Pidgin is the legal property of its developers, whose names are too numerous
 * to list here. Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * 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, see <https://www.gnu.org/licenses/>.
 */

#include "pidgin/pidginaccountfilterconnected.h"

#include <purple.h>

struct _PidginAccountFilterConnected {
	GtkFilter parent;
};

/******************************************************************************
 * Callbacks
 *****************************************************************************/
static void
pidgin_account_filter_connected_changed(G_GNUC_UNUSED PurpleAccountManager *manager,
                                        PurpleAccount *account,
                                        gpointer data)
{
	GtkFilterChange change = GTK_FILTER_CHANGE_LESS_STRICT;

	if(purple_account_get_connected(account)) {
		change = GTK_FILTER_CHANGE_LESS_STRICT;
	} else {
		change = GTK_FILTER_CHANGE_MORE_STRICT;
	}

	gtk_filter_changed(GTK_FILTER(data), change);
}

/******************************************************************************
 * GtkFilter Implementation
 *****************************************************************************/
static GtkFilterMatch
pidgin_account_filter_connected_get_strictness(G_GNUC_UNUSED GtkFilter *self) {
	return GTK_FILTER_MATCH_SOME;
}

static gboolean
pidgin_account_filter_connected_match(G_GNUC_UNUSED GtkFilter *self,
                                      gpointer item)
{
	gboolean ret = FALSE;

	if(PURPLE_IS_ACCOUNT(item)) {
		ret = purple_account_get_connected(PURPLE_ACCOUNT(item));
	}

	return ret;
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
G_DEFINE_FINAL_TYPE(PidginAccountFilterConnected,
                    pidgin_account_filter_connected, GTK_TYPE_FILTER)

static void
pidgin_account_filter_connected_init(PidginAccountFilterConnected *filter) {
	PurpleAccountManager *manager = NULL;

	manager = purple_core_get_account_manager(purple_core_get_default());
	g_signal_connect_object(manager, "account-connected",
	                        G_CALLBACK(pidgin_account_filter_connected_changed),
	                        filter, G_CONNECT_DEFAULT);
	g_signal_connect_object(manager, "account-disconnected",
	                        G_CALLBACK(pidgin_account_filter_connected_changed),
	                        filter, G_CONNECT_DEFAULT);
}

static void
pidgin_account_filter_connected_class_init(PidginAccountFilterConnectedClass *klass) {
	GtkFilterClass *filter_class = GTK_FILTER_CLASS(klass);

	filter_class->get_strictness = pidgin_account_filter_connected_get_strictness;
	filter_class->match = pidgin_account_filter_connected_match;
}

/******************************************************************************
 * API
 *****************************************************************************/
GtkFilter *
pidgin_account_filter_connected_new(void) {
	return g_object_new(PIDGIN_TYPE_ACCOUNT_FILTER_CONNECTED, NULL);
}
