1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2008 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
23 #include "empathy-chatroom.h"
25 #include <tp-account-widgets/tpaw-utils.h>
27 #include "empathy-utils.h"
29 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatroom)
34 gboolean auto_connect
;
36 EmpathyTpChat
*tp_chat
;
40 gboolean need_password
;
41 gboolean always_urgent
;
42 } EmpathyChatroomPriv
;
45 static void chatroom_finalize (GObject
*object
);
46 static void chatroom_get_property (GObject
*object
,
50 static void chatroom_set_property (GObject
*object
,
70 G_DEFINE_TYPE (EmpathyChatroom
, empathy_chatroom
, G_TYPE_OBJECT
);
73 empathy_chatroom_class_init (EmpathyChatroomClass
*klass
)
75 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
77 object_class
->finalize
= chatroom_finalize
;
78 object_class
->get_property
= chatroom_get_property
;
79 object_class
->set_property
= chatroom_set_property
;
81 g_object_class_install_property (object_class
,
83 g_param_spec_object ("account",
85 "The account associated with an chatroom",
89 g_object_class_install_property (object_class
,
91 g_param_spec_string ("room",
93 "Chatroom represented as 'room@server'",
97 g_object_class_install_property (object_class
,
99 g_param_spec_string ("name",
105 g_object_class_install_property (object_class
,
107 g_param_spec_boolean ("auto_connect",
108 "Chatroom Auto Connect",
109 "Connect on startup",
113 g_object_class_install_property (object_class
,
115 g_param_spec_boolean ("favorite",
117 "TRUE if the chatroom is in user's favorite list",
122 g_object_class_install_property (object_class
,
124 g_param_spec_boolean ("always_urgent",
126 "TRUE if every message should be considered urgent",
130 g_object_class_install_property (object_class
,
132 g_param_spec_object ("tp-chat",
133 "Chatroom channel wrapper",
134 "The wrapper for the chatroom channel if there is one",
135 EMPATHY_TYPE_TP_CHAT
,
138 g_object_class_install_property (object_class
,
140 g_param_spec_string ("subject",
142 "The chatroom's subject",
146 G_PARAM_STATIC_NAME
|
147 G_PARAM_STATIC_NICK
|
148 G_PARAM_STATIC_BLURB
));
150 g_object_class_install_property (object_class
,
152 g_param_spec_uint ("members-count",
154 "The chatroom's members count",
160 G_PARAM_STATIC_NAME
|
161 G_PARAM_STATIC_NICK
|
162 G_PARAM_STATIC_BLURB
));
164 g_object_class_install_property (object_class
,
166 g_param_spec_boolean ("invite-only",
168 "The chatroom is invite only",
172 G_PARAM_STATIC_NAME
|
173 G_PARAM_STATIC_NICK
|
174 G_PARAM_STATIC_BLURB
));
176 g_object_class_install_property (object_class
,
178 g_param_spec_boolean ("need-password",
180 "The chatroom is password protected",
184 G_PARAM_STATIC_NAME
|
185 G_PARAM_STATIC_NICK
|
186 G_PARAM_STATIC_BLURB
));
188 g_type_class_add_private (object_class
, sizeof (EmpathyChatroomPriv
));
192 empathy_chatroom_init (EmpathyChatroom
*chatroom
)
194 EmpathyChatroomPriv
*priv
= G_TYPE_INSTANCE_GET_PRIVATE (chatroom
,
195 EMPATHY_TYPE_CHATROOM
, EmpathyChatroomPriv
);
197 chatroom
->priv
= priv
;
201 chatroom_finalize (GObject
*object
)
203 EmpathyChatroomPriv
*priv
;
205 priv
= GET_PRIV (object
);
207 if (priv
->tp_chat
!= NULL
)
208 g_object_unref (priv
->tp_chat
);
210 g_object_unref (priv
->account
);
213 g_free (priv
->subject
);
215 (G_OBJECT_CLASS (empathy_chatroom_parent_class
)->finalize
) (object
);
219 chatroom_get_property (GObject
*object
,
224 EmpathyChatroomPriv
*priv
;
226 priv
= GET_PRIV (object
);
230 g_value_set_object (value
, priv
->account
);
233 g_value_set_string (value
, priv
->room
);
236 g_value_set_string (value
, priv
->name
);
238 case PROP_AUTO_CONNECT
:
239 g_value_set_boolean (value
, priv
->auto_connect
);
242 g_value_set_boolean (value
, priv
->favorite
);
244 case PROP_ALWAYS_URGENT
:
245 g_value_set_boolean (value
, priv
->always_urgent
);
248 g_value_set_object (value
, priv
->tp_chat
);
251 g_value_set_string (value
, priv
->subject
);
253 case PROP_MEMBERS_COUNT
:
254 g_value_set_uint (value
, priv
->members_count
);
256 case PROP_INVITE_ONLY
:
257 g_value_set_boolean (value
, priv
->invite_only
);
259 case PROP_NEED_PASSWORD
:
260 g_value_set_boolean (value
, priv
->need_password
);
263 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, pspec
);
269 chatroom_set_property (GObject
*object
,
276 empathy_chatroom_set_account (EMPATHY_CHATROOM (object
),
277 g_value_get_object (value
));
280 empathy_chatroom_set_room (EMPATHY_CHATROOM (object
),
281 g_value_get_string (value
));
284 empathy_chatroom_set_name (EMPATHY_CHATROOM (object
),
285 g_value_get_string (value
));
287 case PROP_AUTO_CONNECT
:
288 empathy_chatroom_set_auto_connect (EMPATHY_CHATROOM (object
),
289 g_value_get_boolean (value
));
292 empathy_chatroom_set_favorite (EMPATHY_CHATROOM (object
),
293 g_value_get_boolean (value
));
295 case PROP_ALWAYS_URGENT
:
296 empathy_chatroom_set_always_urgent (EMPATHY_CHATROOM (object
),
297 g_value_get_boolean (value
));
300 empathy_chatroom_set_tp_chat (EMPATHY_CHATROOM (object
),
301 g_value_get_object (value
));
304 empathy_chatroom_set_subject (EMPATHY_CHATROOM (object
),
305 g_value_get_string (value
));
307 case PROP_MEMBERS_COUNT
:
308 empathy_chatroom_set_members_count (EMPATHY_CHATROOM (object
),
309 g_value_get_uint (value
));
311 case PROP_NEED_PASSWORD
:
312 empathy_chatroom_set_need_password (EMPATHY_CHATROOM (object
),
313 g_value_get_boolean (value
));
315 case PROP_INVITE_ONLY
:
316 empathy_chatroom_set_invite_only (EMPATHY_CHATROOM (object
),
317 g_value_get_boolean (value
));
320 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, pspec
);
326 empathy_chatroom_new (TpAccount
*account
)
328 return g_object_new (EMPATHY_TYPE_CHATROOM
,
334 empathy_chatroom_new_full (TpAccount
*account
,
337 gboolean auto_connect
)
339 g_return_val_if_fail (room
!= NULL
, NULL
);
341 return g_object_new (EMPATHY_TYPE_CHATROOM
,
345 "auto_connect", auto_connect
,
350 empathy_chatroom_get_account (EmpathyChatroom
*chatroom
)
352 EmpathyChatroomPriv
*priv
;
354 g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom
), NULL
);
356 priv
= GET_PRIV (chatroom
);
357 return priv
->account
;
361 empathy_chatroom_set_account (EmpathyChatroom
*chatroom
,
364 EmpathyChatroomPriv
*priv
;
366 g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom
));
368 priv
= GET_PRIV (chatroom
);
370 if (account
== priv
->account
) {
374 g_object_unref (priv
->account
);
376 priv
->account
= g_object_ref (account
);
378 g_object_notify (G_OBJECT (chatroom
), "account");
382 empathy_chatroom_get_room (EmpathyChatroom
*chatroom
)
384 EmpathyChatroomPriv
*priv
;
386 g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom
), NULL
);
388 priv
= GET_PRIV (chatroom
);
393 empathy_chatroom_set_room (EmpathyChatroom
*chatroom
,
396 EmpathyChatroomPriv
*priv
;
398 g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom
));
399 g_return_if_fail (room
!= NULL
);
401 priv
= GET_PRIV (chatroom
);
404 priv
->room
= g_strdup (room
);
406 g_object_notify (G_OBJECT (chatroom
), "room");
410 empathy_chatroom_get_name (EmpathyChatroom
*chatroom
)
412 EmpathyChatroomPriv
*priv
;
414 g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom
), NULL
);
416 priv
= GET_PRIV (chatroom
);
418 if (TPAW_STR_EMPTY (priv
->name
)) {
426 empathy_chatroom_set_name (EmpathyChatroom
*chatroom
,
429 EmpathyChatroomPriv
*priv
;
431 g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom
));
433 priv
= GET_PRIV (chatroom
);
438 priv
->name
= g_strdup (name
);
441 g_object_notify (G_OBJECT (chatroom
), "name");
445 empathy_chatroom_get_auto_connect (EmpathyChatroom
*chatroom
)
447 EmpathyChatroomPriv
*priv
;
449 g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom
), FALSE
);
451 priv
= GET_PRIV (chatroom
);
452 return priv
->auto_connect
;
456 empathy_chatroom_set_auto_connect (EmpathyChatroom
*chatroom
,
457 gboolean auto_connect
)
459 EmpathyChatroomPriv
*priv
;
461 g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom
));
463 priv
= GET_PRIV (chatroom
);
465 priv
->auto_connect
= auto_connect
;
467 if (priv
->auto_connect
) {
468 /* auto_connect implies favorite */
469 priv
->favorite
= TRUE
;
470 g_object_notify (G_OBJECT (chatroom
), "favorite");
473 g_object_notify (G_OBJECT (chatroom
), "auto-connect");
477 empathy_chatroom_equal (gconstpointer v1
,
480 TpAccount
*account_a
;
481 TpAccount
*account_b
;
485 g_return_val_if_fail (EMPATHY_IS_CHATROOM (v1
), FALSE
);
486 g_return_val_if_fail (EMPATHY_IS_CHATROOM (v2
), FALSE
);
488 account_a
= empathy_chatroom_get_account (EMPATHY_CHATROOM (v1
));
489 account_b
= empathy_chatroom_get_account (EMPATHY_CHATROOM (v2
));
491 room_a
= empathy_chatroom_get_room (EMPATHY_CHATROOM (v1
));
492 room_b
= empathy_chatroom_get_room (EMPATHY_CHATROOM (v2
));
494 return account_a
== account_b
&& !tp_strdiff (room_a
, room_b
);
498 empathy_chatroom_get_tp_chat (EmpathyChatroom
*chatroom
)
500 EmpathyChatroomPriv
*priv
;
502 g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom
), NULL
);
504 priv
= GET_PRIV (chatroom
);
506 return priv
->tp_chat
;
510 empathy_chatroom_get_subject (EmpathyChatroom
*chatroom
)
512 EmpathyChatroomPriv
*priv
;
514 g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom
), NULL
);
516 priv
= GET_PRIV (chatroom
);
518 return priv
->subject
;
522 empathy_chatroom_set_subject (EmpathyChatroom
*chatroom
,
523 const gchar
*subject
)
525 EmpathyChatroomPriv
*priv
;
527 g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom
));
529 priv
= GET_PRIV (chatroom
);
531 g_free (priv
->subject
);
532 priv
->subject
= NULL
;
535 priv
->subject
= g_strdup (subject
);
537 g_object_notify (G_OBJECT (chatroom
), "subject");
541 empathy_chatroom_get_members_count (EmpathyChatroom
*chatroom
)
543 EmpathyChatroomPriv
*priv
;
545 g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom
), 0);
547 priv
= GET_PRIV (chatroom
);
549 return priv
->members_count
;
553 empathy_chatroom_set_members_count (EmpathyChatroom
*chatroom
,
556 EmpathyChatroomPriv
*priv
;
558 g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom
));
560 priv
= GET_PRIV (chatroom
);
562 priv
->members_count
= count
;
564 g_object_notify (G_OBJECT (chatroom
), "members-count");
568 empathy_chatroom_get_need_password (EmpathyChatroom
*chatroom
)
570 EmpathyChatroomPriv
*priv
;
572 g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom
), FALSE
);
574 priv
= GET_PRIV (chatroom
);
576 return priv
->need_password
;
580 empathy_chatroom_set_need_password (EmpathyChatroom
*chatroom
,
581 gboolean need_password
)
583 EmpathyChatroomPriv
*priv
;
585 g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom
));
587 priv
= GET_PRIV (chatroom
);
589 priv
->need_password
= need_password
;
591 g_object_notify (G_OBJECT (chatroom
), "need-password");
595 empathy_chatroom_get_invite_only (EmpathyChatroom
*chatroom
)
597 EmpathyChatroomPriv
*priv
;
599 g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom
), FALSE
);
601 priv
= GET_PRIV (chatroom
);
603 return priv
->invite_only
;
607 empathy_chatroom_set_invite_only (EmpathyChatroom
*chatroom
,
608 gboolean invite_only
)
610 EmpathyChatroomPriv
*priv
;
612 g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom
));
614 priv
= GET_PRIV (chatroom
);
616 priv
->invite_only
= invite_only
;
618 g_object_notify (G_OBJECT (chatroom
), "invite-only");
622 empathy_chatroom_set_tp_chat (EmpathyChatroom
*chatroom
,
623 EmpathyTpChat
*tp_chat
)
625 EmpathyChatroomPriv
*priv
;
627 g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom
));
628 g_return_if_fail (tp_chat
== NULL
|| EMPATHY_IS_TP_CHAT (tp_chat
));
630 priv
= GET_PRIV (chatroom
);
632 if (priv
->tp_chat
== tp_chat
) {
636 if (priv
->tp_chat
!= NULL
) {
637 g_object_unref (priv
->tp_chat
);
640 priv
->tp_chat
= tp_chat
? g_object_ref (tp_chat
) : NULL
;
641 g_object_notify (G_OBJECT (chatroom
), "tp-chat");
645 empathy_chatroom_is_favorite (EmpathyChatroom
*chatroom
)
647 EmpathyChatroomPriv
*priv
;
649 g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom
), FALSE
);
651 priv
= GET_PRIV (chatroom
);
653 return priv
->favorite
;
657 empathy_chatroom_set_favorite (EmpathyChatroom
*chatroom
,
660 EmpathyChatroomPriv
*priv
;
662 g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom
));
664 priv
= GET_PRIV (chatroom
);
666 if (priv
->favorite
== favorite
) {
670 priv
->favorite
= favorite
;
671 if (!priv
->favorite
) {
672 empathy_chatroom_set_auto_connect (chatroom
, FALSE
);
674 g_object_notify (G_OBJECT (chatroom
), "favorite");
678 empathy_chatroom_is_always_urgent (EmpathyChatroom
*chatroom
)
680 EmpathyChatroomPriv
*priv
;
682 g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom
), FALSE
);
684 priv
= GET_PRIV (chatroom
);
686 return priv
->always_urgent
;
690 empathy_chatroom_set_always_urgent (EmpathyChatroom
*chatroom
,
691 gboolean always_urgent
)
693 EmpathyChatroomPriv
*priv
;
695 g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom
));
697 priv
= GET_PRIV (chatroom
);
699 if (priv
->always_urgent
== always_urgent
)
702 priv
->always_urgent
= always_urgent
;
703 g_object_notify (G_OBJECT (chatroom
), "always_urgent");