empathy-chatroom.c 10.3 KB
Newer Older
1 2
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
3
 * Copyright (C) 2007-2008 Collabora Ltd.
4
 *
5 6 7 8
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11 12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15 16 17
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 19 20 21 22 23 24 25 26
 *
 * Authors: Xavier Claessens <xclaesse@gmail.com>
 */

#include "config.h"

#include <string.h>

#include <glib.h>
27
#include <telepathy-glib/util.h>
28

29 30
#include "empathy-chatroom.h"
#include "empathy-utils.h"
31

32 33
#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatroom)
typedef struct {
34 35 36 37
	McAccount *account;
	gchar     *room;
	gchar     *name;
	gboolean   auto_connect;
38 39
	gboolean favorite;
	EmpathyTpChat *tp_chat;
40 41
} EmpathyChatroomPriv;

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

static void chatroom_finalize          (GObject             *object);
static void chatroom_get_property      (GObject             *object,
					guint                param_id,
					GValue              *value,
					GParamSpec          *pspec);
static void chatroom_set_property      (GObject             *object,
					guint                param_id,
					const GValue        *value,
					GParamSpec          *pspec);

enum {
	PROP_0,
	PROP_ACCOUNT,
	PROP_ROOM,
	PROP_NAME,
	PROP_AUTO_CONNECT,
59 60
	PROP_FAVORITE,
	PROP_TP_CHAT,
61 62
};

63
G_DEFINE_TYPE (EmpathyChatroom, empathy_chatroom, G_TYPE_OBJECT);
64 65

static void
66
empathy_chatroom_class_init (EmpathyChatroomClass *klass)
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
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->finalize     = chatroom_finalize;
	object_class->get_property = chatroom_get_property;
	object_class->set_property = chatroom_set_property;

	g_object_class_install_property (object_class,
					 PROP_ACCOUNT,
					 g_param_spec_object ("account",
							      "Chatroom Account",
							      "The account associated with an chatroom",
							      MC_TYPE_ACCOUNT,
							      G_PARAM_READWRITE));

	g_object_class_install_property (object_class,
					 PROP_ROOM,
					 g_param_spec_string ("room",
							      "Chatroom Room",
							      "Chatroom represented as 'room@server'",
							      NULL,
							      G_PARAM_READWRITE));

	g_object_class_install_property (object_class,
					 PROP_NAME,
					 g_param_spec_string ("name",
							      "Chatroom Name",
							      "Chatroom name",
							      NULL,
							      G_PARAM_READWRITE));

	g_object_class_install_property (object_class,
					 PROP_AUTO_CONNECT,
					 g_param_spec_boolean ("auto_connect",
							       "Chatroom Auto Connect",
							       "Connect on startup",
							       FALSE,
							       G_PARAM_READWRITE));

106 107 108 109 110 111 112 113 114 115 116 117
  g_object_class_install_property (object_class,
      PROP_FAVORITE,
      g_param_spec_boolean ("favorite",
        "Favorite",
        "TRUE if the chatroom is in user's favorite list",
        FALSE,
        G_PARAM_READWRITE |
        G_PARAM_CONSTRUCT |
        G_PARAM_STATIC_NAME |
        G_PARAM_STATIC_NICK |
        G_PARAM_STATIC_BLURB));

118 119 120 121 122 123 124 125
	g_object_class_install_property (object_class,
					 PROP_TP_CHAT,
					 g_param_spec_object ("tp-chat",
							      "Chatroom channel wrapper",
							      "The wrapper for the chatroom channel if there is one",
							      EMPATHY_TYPE_TP_CHAT,
							      G_PARAM_READWRITE));

126
	g_type_class_add_private (object_class, sizeof (EmpathyChatroomPriv));
127 128 129
}

static void
130
empathy_chatroom_init (EmpathyChatroom *chatroom)
131
{
132 133 134 135
	EmpathyChatroomPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chatroom,
		EMPATHY_TYPE_CHATROOM, EmpathyChatroomPriv);

	chatroom->priv = priv;
136 137 138 139 140
}

static void
chatroom_finalize (GObject *object)
{
141
	EmpathyChatroomPriv *priv;
142 143 144

	priv = GET_PRIV (object);

145 146 147
	if (priv->tp_chat != NULL)
		g_object_unref (priv->tp_chat);

148 149 150 151
	g_object_unref (priv->account);
	g_free (priv->room);
	g_free (priv->name);

152
	(G_OBJECT_CLASS (empathy_chatroom_parent_class)->finalize) (object);
153 154 155 156 157 158 159 160
}

static void
chatroom_get_property (GObject    *object,
		       guint       param_id,
		       GValue     *value,
		       GParamSpec *pspec)
{
161
	EmpathyChatroomPriv *priv;
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

	priv = GET_PRIV (object);

	switch (param_id) {
	case PROP_ACCOUNT:
		g_value_set_object (value, priv->account);
		break;
	case PROP_ROOM:
		g_value_set_string (value, priv->room);
		break;
	case PROP_NAME:
		g_value_set_string (value, priv->name);
		break;
	case PROP_AUTO_CONNECT:
		g_value_set_boolean (value, priv->auto_connect);
		break;
178 179 180
  case PROP_FAVORITE:
    g_value_set_boolean (value, priv->favorite);
    break;
181 182 183
	case PROP_TP_CHAT:
		g_value_set_object (value, priv->tp_chat);
		break;
184 185 186 187 188 189 190 191 192 193 194 195
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	};
}

static void
chatroom_set_property (GObject      *object,
		       guint         param_id,
		       const GValue *value,
		       GParamSpec   *pspec)
{
196
	EmpathyChatroomPriv *priv;
197 198 199 200 201

	priv = GET_PRIV (object);

	switch (param_id) {
	case PROP_ACCOUNT:
202
		empathy_chatroom_set_account (EMPATHY_CHATROOM (object),
203 204 205
					     g_value_get_object (value));
		break;
	case PROP_ROOM:
206
		empathy_chatroom_set_room (EMPATHY_CHATROOM (object),
207 208 209
					  g_value_get_string (value));
		break;
	case PROP_NAME:
210
		empathy_chatroom_set_name (EMPATHY_CHATROOM (object),
211 212 213
					  g_value_get_string (value));
		break;
	case PROP_AUTO_CONNECT:
214
		empathy_chatroom_set_auto_connect (EMPATHY_CHATROOM (object),
215 216
						  g_value_get_boolean (value));
		break;
217 218
  case PROP_FAVORITE:
    priv->favorite = g_value_get_boolean (value);
219 220 221 222 223
    if (!priv->favorite)
      {
        empathy_chatroom_set_auto_connect (EMPATHY_CHATROOM (object),
            FALSE);
      }
224
    break;
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
	case PROP_TP_CHAT: {
		GObject *chat = g_value_dup_object (value);

		if (chat == (GObject *) priv->tp_chat)
			break;

		g_assert (chat == NULL || priv->tp_chat == NULL);

		if (priv->tp_chat != NULL) {
			g_object_unref (priv->tp_chat);
			priv->tp_chat = NULL;
		} else {
			priv->tp_chat = EMPATHY_TP_CHAT (chat);
		}
		break;
	}
241 242 243 244 245 246
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	};
}

247
EmpathyChatroom *
Xavier Claessens's avatar
Xavier Claessens committed
248
empathy_chatroom_new (McAccount *account)
249 250 251
{
	g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);

252
	return g_object_new (EMPATHY_TYPE_CHATROOM,
253 254 255 256
			     "account", account,
			     NULL);
}

257 258
EmpathyChatroom *
empathy_chatroom_new_full (McAccount   *account,
259 260 261 262 263 264 265
			  const gchar *room,
			  const gchar *name,
			  gboolean     auto_connect)
{
	g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
	g_return_val_if_fail (room != NULL, NULL);

266
	return g_object_new (EMPATHY_TYPE_CHATROOM,
267 268 269 270 271 272 273 274
			     "account", account,
			     "room", room,
			     "name", name,
			     "auto_connect", auto_connect,
			     NULL);
}

McAccount *
275
empathy_chatroom_get_account (EmpathyChatroom *chatroom)
276
{
277
	EmpathyChatroomPriv *priv;
278

279
	g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom), NULL);
280 281 282 283 284 285

	priv = GET_PRIV (chatroom);
	return priv->account;
}

void
286
empathy_chatroom_set_account (EmpathyChatroom *chatroom,
287 288
			     McAccount      *account)
{
289
	EmpathyChatroomPriv *priv;
290

291
	g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
	g_return_if_fail (MC_IS_ACCOUNT (account));

	priv = GET_PRIV (chatroom);

	if (account == priv->account) {
		return;
	}
	if (priv->account) {
		g_object_unref (priv->account);
	}
	priv->account = g_object_ref (account);

	g_object_notify (G_OBJECT (chatroom), "account");
}

const gchar *
308
empathy_chatroom_get_room (EmpathyChatroom *chatroom)
309
{
310
	EmpathyChatroomPriv *priv;
311

312
	g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom), NULL);
313 314 315 316 317 318

	priv = GET_PRIV (chatroom);
	return priv->room;
}

void
319
empathy_chatroom_set_room (EmpathyChatroom *chatroom,
320 321
			  const gchar    *room)
{
322
	EmpathyChatroomPriv *priv;
323

324
	g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));
325 326 327 328 329 330 331 332 333 334 335
	g_return_if_fail (room != NULL);

	priv = GET_PRIV (chatroom);

	g_free (priv->room);
	priv->room = g_strdup (room);

	g_object_notify (G_OBJECT (chatroom), "room");
}

const gchar *
336
empathy_chatroom_get_name (EmpathyChatroom *chatroom)
337
{
338
	EmpathyChatroomPriv *priv;
339

340
	g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom), NULL);
341 342 343

	priv = GET_PRIV (chatroom);
	
344
	if (EMP_STR_EMPTY (priv->name)) {
345 346 347 348 349 350 351
		return priv->room;
	}
	
	return priv->name;
}

void
352
empathy_chatroom_set_name (EmpathyChatroom *chatroom,
353 354
			  const gchar    *name)
{
355
	EmpathyChatroomPriv *priv;
356

357
	g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));
358 359 360 361

	priv = GET_PRIV (chatroom);

	g_free (priv->name);
362 363 364 365
	priv->name = NULL;
	if (name) {
		priv->name = g_strdup (name);
	}
366 367 368 369 370

	g_object_notify (G_OBJECT (chatroom), "name");
}

gboolean
371
empathy_chatroom_get_auto_connect (EmpathyChatroom *chatroom)
372
{
373
	EmpathyChatroomPriv *priv;
374

375
	g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom), FALSE);
376 377 378 379 380 381

	priv = GET_PRIV (chatroom);
	return priv->auto_connect;
}

void
382
empathy_chatroom_set_auto_connect (EmpathyChatroom *chatroom,
383 384
				  gboolean        auto_connect)
{
385
	EmpathyChatroomPriv *priv;
386

387
	g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));
388 389 390 391 392

	priv = GET_PRIV (chatroom);

	priv->auto_connect = auto_connect;

393 394 395 396
  if (priv->auto_connect)
    {
      /* auto_connect implies favorite */
      priv->favorite = TRUE;
397
      g_object_notify (G_OBJECT (chatroom), "favorite");
398 399
    }

400 401 402 403
	g_object_notify (G_OBJECT (chatroom), "auto-connect");
}

gboolean
404
empathy_chatroom_equal (gconstpointer v1,
405 406 407 408 409 410 411
		       gconstpointer v2)
{
	McAccount   *account_a;
	McAccount   *account_b;
	const gchar *room_a;
	const gchar *room_b;

412 413
	g_return_val_if_fail (EMPATHY_IS_CHATROOM (v1), FALSE);
	g_return_val_if_fail (EMPATHY_IS_CHATROOM (v2), FALSE);
414

415 416
	account_a = empathy_chatroom_get_account (EMPATHY_CHATROOM (v1));
	account_b = empathy_chatroom_get_account (EMPATHY_CHATROOM (v2));
417

418 419
	room_a = empathy_chatroom_get_room (EMPATHY_CHATROOM (v1));
	room_b = empathy_chatroom_get_room (EMPATHY_CHATROOM (v2));
420

421 422
	return empathy_account_equal (account_a, account_b) && !tp_strdiff (room_a,
      room_b);
423
}
424 425 426 427 428 429 430 431 432 433 434

EmpathyTpChat *
empathy_chatroom_get_tp_chat (EmpathyChatroom *chatroom) {
	EmpathyChatroomPriv *priv;

	g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom), NULL);

	priv = GET_PRIV (chatroom);

	return priv->tp_chat;
}