empathy-chatroom.c 16.6 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
	TpAccount *account;
35 36 37
	gchar     *room;
	gchar     *name;
	gboolean   auto_connect;
38 39
	gboolean favorite;
	EmpathyTpChat *tp_chat;
40 41 42 43
	gchar *subject;
	guint members_count;
	gboolean invite_only;
	gboolean need_password;
44
	gboolean always_urgent;
45 46
} EmpathyChatroomPriv;

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

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,
64 65
	PROP_FAVORITE,
	PROP_TP_CHAT,
66 67 68
	PROP_SUBJECT,
	PROP_MEMBERS_COUNT,
	PROP_NEED_PASSWORD,
69 70
	PROP_INVITE_ONLY,
	PROP_ALWAYS_URGENT,
71 72
};

73
G_DEFINE_TYPE (EmpathyChatroom, empathy_chatroom, G_TYPE_OBJECT);
74 75

static void
76
empathy_chatroom_class_init (EmpathyChatroomClass *klass)
77 78 79 80 81 82 83 84 85 86 87 88
{
	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",
89
							      TP_TYPE_ACCOUNT,
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
							      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));

116 117 118 119 120 121 122 123
	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));
124

125 126 127 128 129 130 131 132
	g_object_class_install_property (object_class,
					 PROP_ALWAYS_URGENT,
					 g_param_spec_boolean ("always_urgent",
							       "Always Urgent",
							       "TRUE if every message should be considered urgent",
							       FALSE,
							       G_PARAM_READWRITE));

133 134 135 136 137 138 139 140
	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));

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
  g_object_class_install_property (object_class,
      PROP_SUBJECT,
      g_param_spec_string ("subject",
        "Subject",
        "The chatroom's subject",
        "",
        G_PARAM_READWRITE |
        G_PARAM_CONSTRUCT |
        G_PARAM_STATIC_NAME |
        G_PARAM_STATIC_NICK |
        G_PARAM_STATIC_BLURB));

  g_object_class_install_property (object_class,
      PROP_MEMBERS_COUNT,
      g_param_spec_uint ("members-count",
        "Members count",
        "The chatroom's members count",
        0,
        G_MAXUINT,
        0,
        G_PARAM_READWRITE |
        G_PARAM_CONSTRUCT |
        G_PARAM_STATIC_NAME |
        G_PARAM_STATIC_NICK |
        G_PARAM_STATIC_BLURB));

  g_object_class_install_property (object_class,
      PROP_INVITE_ONLY,
      g_param_spec_boolean ("invite-only",
        "Invite Only",
        "The chatroom is invite only",
        FALSE,
        G_PARAM_READWRITE |
        G_PARAM_CONSTRUCT |
        G_PARAM_STATIC_NAME |
        G_PARAM_STATIC_NICK |
        G_PARAM_STATIC_BLURB));

  g_object_class_install_property (object_class,
      PROP_NEED_PASSWORD,
      g_param_spec_boolean ("need-password",
        "Password Needed",
        "The chatroom is password protected",
        FALSE,
        G_PARAM_READWRITE |
        G_PARAM_CONSTRUCT |
        G_PARAM_STATIC_NAME |
        G_PARAM_STATIC_NICK |
        G_PARAM_STATIC_BLURB));

191
	g_type_class_add_private (object_class, sizeof (EmpathyChatroomPriv));
192 193 194
}

static void
195
empathy_chatroom_init (EmpathyChatroom *chatroom)
196
{
197 198 199 200
	EmpathyChatroomPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chatroom,
		EMPATHY_TYPE_CHATROOM, EmpathyChatroomPriv);

	chatroom->priv = priv;
201 202 203 204 205
}

static void
chatroom_finalize (GObject *object)
{
206
	EmpathyChatroomPriv *priv;
207 208 209

	priv = GET_PRIV (object);

210 211 212
	if (priv->tp_chat != NULL)
		g_object_unref (priv->tp_chat);

213 214 215
	g_object_unref (priv->account);
	g_free (priv->room);
	g_free (priv->name);
216
	g_free (priv->subject);
217

218
	(G_OBJECT_CLASS (empathy_chatroom_parent_class)->finalize) (object);
219 220 221 222 223 224 225 226
}

static void
chatroom_get_property (GObject    *object,
		       guint       param_id,
		       GValue     *value,
		       GParamSpec *pspec)
{
227
	EmpathyChatroomPriv *priv;
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

	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;
244 245 246
	case PROP_FAVORITE:
		g_value_set_boolean (value, priv->favorite);
		break;
247 248 249
	case PROP_ALWAYS_URGENT:
		g_value_set_boolean (value, priv->always_urgent);
		break;
250 251 252
	case PROP_TP_CHAT:
		g_value_set_object (value, priv->tp_chat);
		break;
253 254 255 256 257 258 259 260 261 262 263 264
  case PROP_SUBJECT:
    g_value_set_string (value, priv->subject);
    break;
  case PROP_MEMBERS_COUNT:
    g_value_set_uint (value, priv->members_count);
    break;
  case PROP_INVITE_ONLY:
    g_value_set_boolean (value, priv->invite_only);
    break;
  case PROP_NEED_PASSWORD:
    g_value_set_boolean (value, priv->need_password);
    break;
265 266 267 268 269 270 271 272 273 274 275 276 277 278
	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)
{
	switch (param_id) {
	case PROP_ACCOUNT:
279
		empathy_chatroom_set_account (EMPATHY_CHATROOM (object),
280 281 282
					     g_value_get_object (value));
		break;
	case PROP_ROOM:
283
		empathy_chatroom_set_room (EMPATHY_CHATROOM (object),
284 285 286
					  g_value_get_string (value));
		break;
	case PROP_NAME:
287
		empathy_chatroom_set_name (EMPATHY_CHATROOM (object),
288 289 290
					  g_value_get_string (value));
		break;
	case PROP_AUTO_CONNECT:
291
		empathy_chatroom_set_auto_connect (EMPATHY_CHATROOM (object),
292 293
						  g_value_get_boolean (value));
		break;
294 295 296 297
	case PROP_FAVORITE:
		empathy_chatroom_set_favorite (EMPATHY_CHATROOM (object),
					       g_value_get_boolean (value));
		break;
298 299 300 301
	case PROP_ALWAYS_URGENT:
		empathy_chatroom_set_always_urgent (EMPATHY_CHATROOM (object),
					       g_value_get_boolean (value));
		break;
302 303 304
	case PROP_TP_CHAT:
		empathy_chatroom_set_tp_chat (EMPATHY_CHATROOM (object),
					      g_value_get_object (value));
305
		break;
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
  case PROP_SUBJECT:
    empathy_chatroom_set_subject (EMPATHY_CHATROOM (object),
        g_value_get_string (value));
    break;
  case PROP_MEMBERS_COUNT:
    empathy_chatroom_set_members_count (EMPATHY_CHATROOM (object),
        g_value_get_uint (value));
    break;
  case PROP_NEED_PASSWORD:
    empathy_chatroom_set_need_password (EMPATHY_CHATROOM (object),
        g_value_get_boolean (value));
    break;
  case PROP_INVITE_ONLY:
    empathy_chatroom_set_invite_only (EMPATHY_CHATROOM (object),
        g_value_get_boolean (value));
    break;
322 323 324 325 326 327
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	};
}

328
EmpathyChatroom *
329
empathy_chatroom_new (TpAccount *account)
330
{
331
	return g_object_new (EMPATHY_TYPE_CHATROOM,
332 333 334 335
			     "account", account,
			     NULL);
}

336
EmpathyChatroom *
337
empathy_chatroom_new_full (TpAccount *account,
338 339 340 341 342 343
			  const gchar *room,
			  const gchar *name,
			  gboolean     auto_connect)
{
	g_return_val_if_fail (room != NULL, NULL);

344
	return g_object_new (EMPATHY_TYPE_CHATROOM,
345 346 347 348 349 350 351
			     "account", account,
			     "room", room,
			     "name", name,
			     "auto_connect", auto_connect,
			     NULL);
}

352
TpAccount *
353
empathy_chatroom_get_account (EmpathyChatroom *chatroom)
354
{
355
	EmpathyChatroomPriv *priv;
356

357
	g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom), NULL);
358 359 360 361 362 363

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

void
364
empathy_chatroom_set_account (EmpathyChatroom *chatroom,
365
			     TpAccount *account)
366
{
367
	EmpathyChatroomPriv *priv;
368

369
	g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384

	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 *
385
empathy_chatroom_get_room (EmpathyChatroom *chatroom)
386
{
387
	EmpathyChatroomPriv *priv;
388

389
	g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom), NULL);
390 391 392 393 394 395

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

void
396
empathy_chatroom_set_room (EmpathyChatroom *chatroom,
397 398
			  const gchar    *room)
{
399
	EmpathyChatroomPriv *priv;
400

401
	g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));
402 403 404 405 406 407 408 409 410 411 412
	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 *
413
empathy_chatroom_get_name (EmpathyChatroom *chatroom)
414
{
415
	EmpathyChatroomPriv *priv;
416

417
	g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom), NULL);
418 419

	priv = GET_PRIV (chatroom);
420

421
	if (EMP_STR_EMPTY (priv->name)) {
422 423
		return priv->room;
	}
424

425 426 427 428
	return priv->name;
}

void
429
empathy_chatroom_set_name (EmpathyChatroom *chatroom,
430 431
			  const gchar    *name)
{
432
	EmpathyChatroomPriv *priv;
433

434
	g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));
435 436 437 438

	priv = GET_PRIV (chatroom);

	g_free (priv->name);
439 440 441 442
	priv->name = NULL;
	if (name) {
		priv->name = g_strdup (name);
	}
443 444 445 446 447

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

gboolean
448
empathy_chatroom_get_auto_connect (EmpathyChatroom *chatroom)
449
{
450
	EmpathyChatroomPriv *priv;
451

452
	g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom), FALSE);
453 454 455 456 457 458

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

void
459
empathy_chatroom_set_auto_connect (EmpathyChatroom *chatroom,
460 461
				  gboolean        auto_connect)
{
462
	EmpathyChatroomPriv *priv;
463

464
	g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));
465 466 467 468 469

	priv = GET_PRIV (chatroom);

	priv->auto_connect = auto_connect;

470 471 472 473 474
	if (priv->auto_connect) {
		/* auto_connect implies favorite */
		priv->favorite = TRUE;
		g_object_notify (G_OBJECT (chatroom), "favorite");
	}
475

476 477 478 479
	g_object_notify (G_OBJECT (chatroom), "auto-connect");
}

gboolean
480
empathy_chatroom_equal (gconstpointer v1,
481 482
		       gconstpointer v2)
{
483 484
	TpAccount *account_a;
	TpAccount *account_b;
485 486 487
	const gchar *room_a;
	const gchar *room_b;

488 489
	g_return_val_if_fail (EMPATHY_IS_CHATROOM (v1), FALSE);
	g_return_val_if_fail (EMPATHY_IS_CHATROOM (v2), FALSE);
490

491 492
	account_a = empathy_chatroom_get_account (EMPATHY_CHATROOM (v1));
	account_b = empathy_chatroom_get_account (EMPATHY_CHATROOM (v2));
493

494 495
	room_a = empathy_chatroom_get_room (EMPATHY_CHATROOM (v1));
	room_b = empathy_chatroom_get_room (EMPATHY_CHATROOM (v2));
496

497
	return account_a == account_b && !tp_strdiff (room_a, room_b);
498
}
499 500

EmpathyTpChat *
501 502
empathy_chatroom_get_tp_chat (EmpathyChatroom *chatroom)
{
503 504 505 506 507 508 509 510
	EmpathyChatroomPriv *priv;

	g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom), NULL);

	priv = GET_PRIV (chatroom);

	return priv->tp_chat;
}
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623

const gchar *
empathy_chatroom_get_subject (EmpathyChatroom *chatroom)
{
  EmpathyChatroomPriv *priv;

  g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom), NULL);

  priv = GET_PRIV (chatroom);

  return priv->subject;
}

void
empathy_chatroom_set_subject (EmpathyChatroom *chatroom,
                              const gchar *subject)
{
  EmpathyChatroomPriv *priv;

  g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));

  priv = GET_PRIV (chatroom);

  g_free (priv->subject);
  priv->subject = NULL;

  if (subject)
    priv->subject = g_strdup (subject);

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

guint
empathy_chatroom_get_members_count (EmpathyChatroom *chatroom)
{
  EmpathyChatroomPriv *priv;

  g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom), 0);

  priv = GET_PRIV (chatroom);

  return priv->members_count;
}

void
empathy_chatroom_set_members_count (EmpathyChatroom *chatroom,
                                    guint count)
{
  EmpathyChatroomPriv *priv;

  g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));

  priv = GET_PRIV (chatroom);

  priv->members_count = count;

  g_object_notify (G_OBJECT (chatroom), "members-count");
}

gboolean
empathy_chatroom_get_need_password (EmpathyChatroom *chatroom)
{
  EmpathyChatroomPriv *priv;

  g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom), FALSE);

  priv = GET_PRIV (chatroom);

  return priv->need_password;
}

void
empathy_chatroom_set_need_password (EmpathyChatroom *chatroom,
                                    gboolean need_password)
{
  EmpathyChatroomPriv *priv;

  g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));

  priv = GET_PRIV (chatroom);

  priv->need_password = need_password;

  g_object_notify (G_OBJECT (chatroom), "need-password");
}

gboolean
empathy_chatroom_get_invite_only (EmpathyChatroom *chatroom)
{
  EmpathyChatroomPriv *priv;

  g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom), FALSE);

  priv = GET_PRIV (chatroom);

  return priv->invite_only;
}

void
empathy_chatroom_set_invite_only (EmpathyChatroom *chatroom,
                                  gboolean invite_only)
{
  EmpathyChatroomPriv *priv;

  g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));

  priv = GET_PRIV (chatroom);

  priv->invite_only = invite_only;

  g_object_notify (G_OBJECT (chatroom), "invite-only");
}

624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
void
empathy_chatroom_set_tp_chat (EmpathyChatroom *chatroom,
			      EmpathyTpChat   *tp_chat)
{
	EmpathyChatroomPriv *priv;

	g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));
	g_return_if_fail (tp_chat == NULL || EMPATHY_IS_TP_CHAT (tp_chat));

	priv = GET_PRIV (chatroom);

	if (priv->tp_chat == tp_chat) {
		return;
	}

	if (priv->tp_chat != NULL) {
		g_object_unref (priv->tp_chat);
	}

	priv->tp_chat = tp_chat ? g_object_ref (tp_chat) : NULL;
	g_object_notify (G_OBJECT (chatroom), "tp-chat");
}

gboolean
empathy_chatroom_is_favorite (EmpathyChatroom *chatroom)
{
	EmpathyChatroomPriv *priv;

	g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom), FALSE);

	priv = GET_PRIV (chatroom);

	return priv->favorite;
}

void
empathy_chatroom_set_favorite (EmpathyChatroom *chatroom,
			       gboolean         favorite)
{
	EmpathyChatroomPriv *priv;

	g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));

	priv = GET_PRIV (chatroom);

	if (priv->favorite == favorite) {
		return;
	}

	priv->favorite = favorite;
	if (!priv->favorite) {
		empathy_chatroom_set_auto_connect (chatroom, FALSE);
	}
	g_object_notify (G_OBJECT (chatroom), "favorite");
}

680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
gboolean
empathy_chatroom_is_always_urgent (EmpathyChatroom *chatroom)
{
	EmpathyChatroomPriv *priv;

	g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom), FALSE);

	priv = GET_PRIV (chatroom);

	return priv->always_urgent;
}

void
empathy_chatroom_set_always_urgent (EmpathyChatroom *chatroom,
			       gboolean         always_urgent)
{
	EmpathyChatroomPriv *priv;

	g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));

	priv = GET_PRIV (chatroom);

	if (priv->always_urgent == always_urgent)
		return;

	priv->always_urgent = always_urgent;
	g_object_notify (G_OBJECT (chatroom), "always_urgent");
}