summaryrefslogtreecommitdiff
path: root/libpurple/protocols/gg/servconn.c
blob: 1088d51582ba6402b9b8e9d89aa4483b75f6f55e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* purple
 *
 * Purple 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.
 *
 * Rewritten from scratch during Google Summer of Code 2012
 * by Tomek Wasilczyk (http://www.wasilczyk.pl).
 *
 * Previously implemented by:
 *  - Arkadiusz Miskiewicz <misiek@pld.org.pl> - first implementation (2001);
 *  - Bartosz Oler <bartosz@bzimage.us> - reimplemented during GSoC 2005;
 *  - Krzysztof Klinikowski <grommasher@gmail.com> - some parts (2009-2011).
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 */

#include "servconn.h"

#include "utils.h"

#define GGP_SERVCONN_HISTORY_PREF "/plugins/prpl/gg/server_history"
#define GGP_SERVCONN_HISTORY_MAXLEN 15

typedef struct
{
	GList *server_history;
	PurpleAccountOption *server_option;
} ggp_servconn_global_data;

static ggp_servconn_global_data global_data;

void ggp_servconn_setup(PurpleAccountOption *server_option)
{
	GList *extra;
	purple_prefs_add_string_list(GGP_SERVCONN_HISTORY_PREF, NULL);

	global_data.server_option = server_option;
	global_data.server_history =
	        purple_prefs_get_string_list(GGP_SERVCONN_HISTORY_PREF);
	extra = g_list_nth(global_data.server_history, GGP_SERVCONN_HISTORY_MAXLEN);
	if (extra != NULL) {
		/* Truncate the list to the maximum. */
		extra->prev->next = NULL;
		g_list_free_full(extra, g_free);
	}

	if(server_option != NULL) {
		purple_account_option_string_set_hints(global_data.server_option,
			ggp_servconn_get_servers());
	}
}

void ggp_servconn_cleanup(void)
{
	g_clear_list(&global_data.server_history, g_free);
}

void ggp_servconn_add_server(const gchar *server)
{
	GList *old_entry;

	old_entry = g_list_find_custom(global_data.server_history, server,
		(GCompareFunc)g_strcmp0);
	if (old_entry) {
		g_free(old_entry->data);
		global_data.server_history = g_list_delete_link(
			global_data.server_history, old_entry);
	}

	global_data.server_history = g_list_prepend(global_data.server_history,
		g_strdup(server));
	old_entry =
	        g_list_nth(global_data.server_history, GGP_SERVCONN_HISTORY_MAXLEN);
	if (old_entry != NULL) {
		/* Truncate the list to the maximum. */
		old_entry->prev->next = NULL;
		g_list_free_full(old_entry, g_free);
	}

	purple_prefs_set_string_list(GGP_SERVCONN_HISTORY_PREF,
	                             global_data.server_history);
	purple_account_option_string_set_hints(global_data.server_option,
		ggp_servconn_get_servers());
}

GSList *
ggp_servconn_get_servers(void)
{
	GSList *new_list = NULL;
	GList *it;

	it = g_list_first(global_data.server_history);
	while (it) {
		new_list = g_slist_append(new_list, g_strdup(it->data));
		it = g_list_next(it);
	}
	return new_list;
}

void
ggp_servconn_remote_disconnect(PurpleConnection *gc)
{
	purple_debug_info("gg", "Server remotely closes connection");
	purple_account_disconnect(purple_connection_get_account(gc));
}