]> git.0d.be Git - empathy.git/blobdiff - src/empathy-chat-window.c
Don't warn before leaving disconnected chatrooms
[empathy.git] / src / empathy-chat-window.c
index 46f76466b2d838b9ea945a7e9b98a72572453138..634968593d9b6826a003bb17bbde695503e2184e 100644 (file)
@@ -212,6 +212,161 @@ chat_window_find_chat (EmpathyChat *chat)
        return NULL;
 }
 
+static void
+remove_all_chats (EmpathyChatWindow *window)
+{
+       EmpathyChatWindowPriv *priv;
+
+       priv = GET_PRIV (window);
+       g_object_ref (window);
+
+       while (priv->chats) {
+               empathy_chat_window_remove_chat (window, priv->chats->data);
+       }
+
+       g_object_unref (window);
+}
+
+static void
+confirm_close_response_cb (GtkWidget *dialog,
+                           int response,
+                           EmpathyChatWindow *window)
+{
+       EmpathyChat *chat;
+
+       chat = g_object_get_data (G_OBJECT (dialog), "chat");
+
+       gtk_widget_destroy (dialog);
+
+       if (response != GTK_RESPONSE_ACCEPT)
+               return;
+
+       if (chat != NULL) {
+               empathy_chat_window_remove_chat (window, chat);
+       } else {
+               remove_all_chats (window);
+       }
+}
+
+static void
+confirm_close (EmpathyChatWindow *window,
+               gboolean close_window,
+               guint n_rooms,
+               EmpathyChat *chat)
+{
+       EmpathyChatWindowPriv *priv;
+       GtkWidget *dialog;
+       gchar *primary, *secondary;
+
+       g_return_if_fail (n_rooms > 0);
+
+       if (n_rooms > 1) {
+               g_return_if_fail (chat == NULL);
+       } else {
+               g_return_if_fail (chat != NULL);
+       }
+
+       priv = GET_PRIV (window);
+
+       /* If there are no chats in this window, how could we possibly have got
+        * here?
+        */
+       g_return_if_fail (priv->chats != NULL);
+
+       /* Treat closing a window which only has one tab exactly like closing
+        * that tab.
+        */
+       if (close_window && priv->chats->next == NULL) {
+               close_window = FALSE;
+               chat = priv->chats->data;
+       }
+
+       if (close_window) {
+               primary = g_strdup (_("Close this window?"));
+
+               if (n_rooms == 1) {
+                       gchar *chat_name = empathy_chat_dup_name (chat);
+                       secondary = g_strdup_printf (
+                               _("Closing this window will leave %s. You will "
+                                 "not receive any further messages until you "
+                                 "rejoin it."),
+                               chat_name);
+                       g_free (chat_name);
+               } else {
+                       secondary = g_strdup_printf (
+                               /* Note to translators: the number of chats will
+                                * always be at least 2.
+                                */
+                               ngettext (
+                                       "Closing this window will leave a chat room. You will "
+                                       "not receive any further messages until you rejoin it.",
+                                       "Closing this window will leave %u chat rooms. You will "
+                                       "not receive any further messages until you rejoin them.",
+                                       n_rooms),
+                               n_rooms);
+               }
+       } else {
+               gchar *chat_name = empathy_chat_dup_name (chat);
+               primary = g_strdup_printf (_("Leave %s?"), chat_name);
+               secondary = g_strdup (_("You will not receive any further messages from this chat "
+                                       "room until you rejoin it."));
+               g_free (chat_name);
+       }
+
+       dialog = gtk_message_dialog_new (
+               GTK_WINDOW (priv->dialog),
+               GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
+               GTK_MESSAGE_WARNING,
+               GTK_BUTTONS_CANCEL,
+               "%s", primary);
+
+       gtk_window_set_title (GTK_WINDOW (dialog), "");
+       g_object_set (dialog, "secondary-text", secondary, NULL);
+
+       g_free (primary);
+       g_free (secondary);
+
+       gtk_dialog_add_button (GTK_DIALOG (dialog),
+               close_window ? _("Close window") : _("Leave room"),
+               GTK_RESPONSE_ACCEPT);
+       gtk_dialog_set_default_response (GTK_DIALOG (dialog),
+               GTK_RESPONSE_ACCEPT);
+
+       if (!close_window) {
+               g_object_set_data (G_OBJECT (dialog), "chat", chat);
+       }
+
+       g_signal_connect (dialog, "response",
+               G_CALLBACK (confirm_close_response_cb), window);
+
+       gtk_window_present (GTK_WINDOW (dialog));
+}
+
+/* Returns TRUE if we should check if the user really wants to leave.  If it's
+ * a multi-user chat, and it has a TpChat (so there's an underlying channel, so
+ * the user is actually in the room as opposed to having been kicked or gone
+ * offline or something), then we should check.
+ */
+static gboolean
+chat_needs_close_confirmation (EmpathyChat *chat)
+{
+       return (empathy_chat_is_room (chat)
+               && empathy_chat_get_tp_chat (chat) != NULL);
+}
+
+static void
+maybe_close_chat (EmpathyChatWindow *window,
+                  EmpathyChat *chat)
+{
+       g_return_if_fail (chat != NULL);
+
+       if (chat_needs_close_confirmation (chat)) {
+               confirm_close (window, FALSE, 1, chat);
+       } else {
+               empathy_chat_window_remove_chat (window, chat);
+       }
+}
+
 static void
 chat_window_close_clicked_cb (GtkAction   *action,
                              EmpathyChat *chat)
@@ -219,7 +374,7 @@ chat_window_close_clicked_cb (GtkAction   *action,
        EmpathyChatWindow *window;
 
        window = chat_window_find_chat (chat);
-       empathy_chat_window_remove_chat (window, chat);
+       maybe_close_chat (window, chat);
 }
 
 static void
@@ -269,7 +424,7 @@ chat_window_create_label (EmpathyChatWindow *window,
        PangoAttribute       *attr;
 
        /* The spacing between the button and the label. */
-       hbox = gtk_hbox_new (FALSE, 0);
+       hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
 
        event_box = gtk_event_box_new ();
        gtk_event_box_set_visible_window (GTK_EVENT_BOX (event_box), FALSE);
@@ -295,7 +450,7 @@ chat_window_create_label (EmpathyChatWindow *window,
        status_image = gtk_image_new ();
 
        /* Spacing between the icon and label. */
-       event_box_hbox = gtk_hbox_new (FALSE, 0);
+       event_box_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
 
        gtk_box_pack_start (GTK_BOX (event_box_hbox), status_image, FALSE, FALSE, 0);
        gtk_box_pack_start (GTK_BOX (event_box_hbox), name_label, TRUE, TRUE, 0);
@@ -1027,7 +1182,7 @@ chat_window_close_activate_cb (GtkAction         *action,
 
        g_return_if_fail (priv->current_chat != NULL);
 
-       empathy_chat_window_remove_chat (window, priv->current_chat);
+       maybe_close_chat (window, priv->current_chat);
 }
 
 static void
@@ -1271,14 +1426,25 @@ chat_window_delete_event_cb (GtkWidget        *dialog,
                             EmpathyChatWindow *window)
 {
        EmpathyChatWindowPriv *priv = GET_PRIV (window);
+       EmpathyChat *chat = NULL;
+       guint n_rooms = 0;
+       GList *l;
 
        DEBUG ("Delete event received");
 
-       g_object_ref (window);
-       while (priv->chats) {
-               empathy_chat_window_remove_chat (window, priv->chats->data);
+       for (l = priv->chats; l != NULL; l = l->next) {
+               if (chat_needs_close_confirmation (l->data)) {
+                       chat = l->data;
+                       n_rooms++;
+               }
+       }
+
+       if (n_rooms > 0) {
+               confirm_close (window, TRUE, n_rooms,
+                       (n_rooms == 1 ? chat : NULL));
+       } else {
+               remove_all_chats (window);
        }
-       g_object_unref (window);
 
        return TRUE;
 }
@@ -1437,6 +1603,7 @@ static void
 chat_window_new_message_cb (EmpathyChat       *chat,
                            EmpathyMessage    *message,
                            gboolean pending,
+                           gboolean should_highlight,
                            EmpathyChatWindow *window)
 {
        EmpathyChatWindowPriv *priv;
@@ -1496,7 +1663,7 @@ chat_window_new_message_cb (EmpathyChat       *chat,
                if (chatroom != NULL && empathy_chatroom_is_always_urgent (chatroom)) {
                        needs_urgency = TRUE;
                } else {
-                       needs_urgency = empathy_message_should_highlight (message);
+                       needs_urgency = should_highlight;
                }
        } else {
                needs_urgency = TRUE;
@@ -2488,8 +2655,12 @@ empathy_chat_window_present_chat (EmpathyChat *chat,
        }
 
        empathy_chat_window_switch_to_chat (window, chat);
-       empathy_window_present_with_time (GTK_WINDOW (priv->dialog),
-         x_timestamp);
+
+       /* Don't use empathy_window_present_with_time () which would move the window
+        * to our current desktop but move to the window's desktop instead. This is
+        * more coherent with Shell's 'app is ready' notication which moves the view
+        * to the app desktop rather than moving the app itself. */
+       empathy_move_to_window_desktop (GTK_WINDOW (priv->dialog), x_timestamp);
 
        gtk_widget_grab_focus (chat->input_text_view);
 }