Update Slovak translation
[empathy-mirror.git] / libempathy / empathy-chatroom.c
blobb186b2dd2aef8f4a63095413af0bd007f0a60c2d
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
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>
22 #include "config.h"
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)
30 typedef struct {
31 TpAccount *account;
32 gchar *room;
33 gchar *name;
34 gboolean auto_connect;
35 gboolean favorite;
36 EmpathyTpChat *tp_chat;
37 gchar *subject;
38 guint members_count;
39 gboolean invite_only;
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,
47 guint param_id,
48 GValue *value,
49 GParamSpec *pspec);
50 static void chatroom_set_property (GObject *object,
51 guint param_id,
52 const GValue *value,
53 GParamSpec *pspec);
55 enum {
56 PROP_0,
57 PROP_ACCOUNT,
58 PROP_ROOM,
59 PROP_NAME,
60 PROP_AUTO_CONNECT,
61 PROP_FAVORITE,
62 PROP_TP_CHAT,
63 PROP_SUBJECT,
64 PROP_MEMBERS_COUNT,
65 PROP_NEED_PASSWORD,
66 PROP_INVITE_ONLY,
67 PROP_ALWAYS_URGENT,
70 G_DEFINE_TYPE (EmpathyChatroom, empathy_chatroom, G_TYPE_OBJECT);
72 static void
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,
82 PROP_ACCOUNT,
83 g_param_spec_object ("account",
84 "Chatroom Account",
85 "The account associated with an chatroom",
86 TP_TYPE_ACCOUNT,
87 G_PARAM_READWRITE));
89 g_object_class_install_property (object_class,
90 PROP_ROOM,
91 g_param_spec_string ("room",
92 "Chatroom Room",
93 "Chatroom represented as 'room@server'",
94 NULL,
95 G_PARAM_READWRITE));
97 g_object_class_install_property (object_class,
98 PROP_NAME,
99 g_param_spec_string ("name",
100 "Chatroom Name",
101 "Chatroom name",
102 NULL,
103 G_PARAM_READWRITE));
105 g_object_class_install_property (object_class,
106 PROP_AUTO_CONNECT,
107 g_param_spec_boolean ("auto_connect",
108 "Chatroom Auto Connect",
109 "Connect on startup",
110 FALSE,
111 G_PARAM_READWRITE));
113 g_object_class_install_property (object_class,
114 PROP_FAVORITE,
115 g_param_spec_boolean ("favorite",
116 "Favorite",
117 "TRUE if the chatroom is in user's favorite list",
118 FALSE,
119 G_PARAM_READWRITE |
120 G_PARAM_CONSTRUCT));
122 g_object_class_install_property (object_class,
123 PROP_ALWAYS_URGENT,
124 g_param_spec_boolean ("always_urgent",
125 "Always Urgent",
126 "TRUE if every message should be considered urgent",
127 FALSE,
128 G_PARAM_READWRITE));
130 g_object_class_install_property (object_class,
131 PROP_TP_CHAT,
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,
136 G_PARAM_READWRITE));
138 g_object_class_install_property (object_class,
139 PROP_SUBJECT,
140 g_param_spec_string ("subject",
141 "Subject",
142 "The chatroom's subject",
144 G_PARAM_READWRITE |
145 G_PARAM_CONSTRUCT |
146 G_PARAM_STATIC_NAME |
147 G_PARAM_STATIC_NICK |
148 G_PARAM_STATIC_BLURB));
150 g_object_class_install_property (object_class,
151 PROP_MEMBERS_COUNT,
152 g_param_spec_uint ("members-count",
153 "Members count",
154 "The chatroom's members count",
156 G_MAXUINT,
158 G_PARAM_READWRITE |
159 G_PARAM_CONSTRUCT |
160 G_PARAM_STATIC_NAME |
161 G_PARAM_STATIC_NICK |
162 G_PARAM_STATIC_BLURB));
164 g_object_class_install_property (object_class,
165 PROP_INVITE_ONLY,
166 g_param_spec_boolean ("invite-only",
167 "Invite Only",
168 "The chatroom is invite only",
169 FALSE,
170 G_PARAM_READWRITE |
171 G_PARAM_CONSTRUCT |
172 G_PARAM_STATIC_NAME |
173 G_PARAM_STATIC_NICK |
174 G_PARAM_STATIC_BLURB));
176 g_object_class_install_property (object_class,
177 PROP_NEED_PASSWORD,
178 g_param_spec_boolean ("need-password",
179 "Password Needed",
180 "The chatroom is password protected",
181 FALSE,
182 G_PARAM_READWRITE |
183 G_PARAM_CONSTRUCT |
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));
191 static void
192 empathy_chatroom_init (EmpathyChatroom *chatroom)
194 EmpathyChatroomPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chatroom,
195 EMPATHY_TYPE_CHATROOM, EmpathyChatroomPriv);
197 chatroom->priv = priv;
200 static void
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);
211 g_free (priv->room);
212 g_free (priv->name);
213 g_free (priv->subject);
215 (G_OBJECT_CLASS (empathy_chatroom_parent_class)->finalize) (object);
218 static void
219 chatroom_get_property (GObject *object,
220 guint param_id,
221 GValue *value,
222 GParamSpec *pspec)
224 EmpathyChatroomPriv *priv;
226 priv = GET_PRIV (object);
228 switch (param_id) {
229 case PROP_ACCOUNT:
230 g_value_set_object (value, priv->account);
231 break;
232 case PROP_ROOM:
233 g_value_set_string (value, priv->room);
234 break;
235 case PROP_NAME:
236 g_value_set_string (value, priv->name);
237 break;
238 case PROP_AUTO_CONNECT:
239 g_value_set_boolean (value, priv->auto_connect);
240 break;
241 case PROP_FAVORITE:
242 g_value_set_boolean (value, priv->favorite);
243 break;
244 case PROP_ALWAYS_URGENT:
245 g_value_set_boolean (value, priv->always_urgent);
246 break;
247 case PROP_TP_CHAT:
248 g_value_set_object (value, priv->tp_chat);
249 break;
250 case PROP_SUBJECT:
251 g_value_set_string (value, priv->subject);
252 break;
253 case PROP_MEMBERS_COUNT:
254 g_value_set_uint (value, priv->members_count);
255 break;
256 case PROP_INVITE_ONLY:
257 g_value_set_boolean (value, priv->invite_only);
258 break;
259 case PROP_NEED_PASSWORD:
260 g_value_set_boolean (value, priv->need_password);
261 break;
262 default:
263 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
264 break;
268 static void
269 chatroom_set_property (GObject *object,
270 guint param_id,
271 const GValue *value,
272 GParamSpec *pspec)
274 switch (param_id) {
275 case PROP_ACCOUNT:
276 empathy_chatroom_set_account (EMPATHY_CHATROOM (object),
277 g_value_get_object (value));
278 break;
279 case PROP_ROOM:
280 empathy_chatroom_set_room (EMPATHY_CHATROOM (object),
281 g_value_get_string (value));
282 break;
283 case PROP_NAME:
284 empathy_chatroom_set_name (EMPATHY_CHATROOM (object),
285 g_value_get_string (value));
286 break;
287 case PROP_AUTO_CONNECT:
288 empathy_chatroom_set_auto_connect (EMPATHY_CHATROOM (object),
289 g_value_get_boolean (value));
290 break;
291 case PROP_FAVORITE:
292 empathy_chatroom_set_favorite (EMPATHY_CHATROOM (object),
293 g_value_get_boolean (value));
294 break;
295 case PROP_ALWAYS_URGENT:
296 empathy_chatroom_set_always_urgent (EMPATHY_CHATROOM (object),
297 g_value_get_boolean (value));
298 break;
299 case PROP_TP_CHAT:
300 empathy_chatroom_set_tp_chat (EMPATHY_CHATROOM (object),
301 g_value_get_object (value));
302 break;
303 case PROP_SUBJECT:
304 empathy_chatroom_set_subject (EMPATHY_CHATROOM (object),
305 g_value_get_string (value));
306 break;
307 case PROP_MEMBERS_COUNT:
308 empathy_chatroom_set_members_count (EMPATHY_CHATROOM (object),
309 g_value_get_uint (value));
310 break;
311 case PROP_NEED_PASSWORD:
312 empathy_chatroom_set_need_password (EMPATHY_CHATROOM (object),
313 g_value_get_boolean (value));
314 break;
315 case PROP_INVITE_ONLY:
316 empathy_chatroom_set_invite_only (EMPATHY_CHATROOM (object),
317 g_value_get_boolean (value));
318 break;
319 default:
320 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
321 break;
325 EmpathyChatroom *
326 empathy_chatroom_new (TpAccount *account)
328 return g_object_new (EMPATHY_TYPE_CHATROOM,
329 "account", account,
330 NULL);
333 EmpathyChatroom *
334 empathy_chatroom_new_full (TpAccount *account,
335 const gchar *room,
336 const gchar *name,
337 gboolean auto_connect)
339 g_return_val_if_fail (room != NULL, NULL);
341 return g_object_new (EMPATHY_TYPE_CHATROOM,
342 "account", account,
343 "room", room,
344 "name", name,
345 "auto_connect", auto_connect,
346 NULL);
349 TpAccount *
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;
360 void
361 empathy_chatroom_set_account (EmpathyChatroom *chatroom,
362 TpAccount *account)
364 EmpathyChatroomPriv *priv;
366 g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));
368 priv = GET_PRIV (chatroom);
370 if (account == priv->account) {
371 return;
373 if (priv->account) {
374 g_object_unref (priv->account);
376 priv->account = g_object_ref (account);
378 g_object_notify (G_OBJECT (chatroom), "account");
381 const gchar *
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);
389 return priv->room;
392 void
393 empathy_chatroom_set_room (EmpathyChatroom *chatroom,
394 const gchar *room)
396 EmpathyChatroomPriv *priv;
398 g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));
399 g_return_if_fail (room != NULL);
401 priv = GET_PRIV (chatroom);
403 g_free (priv->room);
404 priv->room = g_strdup (room);
406 g_object_notify (G_OBJECT (chatroom), "room");
409 const gchar *
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)) {
419 return priv->room;
422 return priv->name;
425 void
426 empathy_chatroom_set_name (EmpathyChatroom *chatroom,
427 const gchar *name)
429 EmpathyChatroomPriv *priv;
431 g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));
433 priv = GET_PRIV (chatroom);
435 g_free (priv->name);
436 priv->name = NULL;
437 if (name) {
438 priv->name = g_strdup (name);
441 g_object_notify (G_OBJECT (chatroom), "name");
444 gboolean
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;
455 void
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");
476 gboolean
477 empathy_chatroom_equal (gconstpointer v1,
478 gconstpointer v2)
480 TpAccount *account_a;
481 TpAccount *account_b;
482 const gchar *room_a;
483 const gchar *room_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);
497 EmpathyTpChat *
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;
509 const gchar *
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;
521 void
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;
534 if (subject)
535 priv->subject = g_strdup (subject);
537 g_object_notify (G_OBJECT (chatroom), "subject");
540 guint
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;
552 void
553 empathy_chatroom_set_members_count (EmpathyChatroom *chatroom,
554 guint count)
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");
567 gboolean
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;
579 void
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");
594 gboolean
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;
606 void
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");
621 void
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) {
633 return;
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");
644 gboolean
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;
656 void
657 empathy_chatroom_set_favorite (EmpathyChatroom *chatroom,
658 gboolean favorite)
660 EmpathyChatroomPriv *priv;
662 g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));
664 priv = GET_PRIV (chatroom);
666 if (priv->favorite == favorite) {
667 return;
670 priv->favorite = favorite;
671 if (!priv->favorite) {
672 empathy_chatroom_set_auto_connect (chatroom, FALSE);
674 g_object_notify (G_OBJECT (chatroom), "favorite");
677 gboolean
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;
689 void
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)
700 return;
702 priv->always_urgent = always_urgent;
703 g_object_notify (G_OBJECT (chatroom), "always_urgent");