summaryrefslogtreecommitdiff
path: root/src/nautilus-notification-manager.c
blob: 28b124a58ab1e792655d4b2bf6fd10c198a2f5be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
/* nautilus-notification-manager.c
 *
 * Copyright (C) 2015 Carlos Soriano <csoriano@gnome.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "nautilus-notification-manager.h"
#include "nautilus-file-undo-manager.h"

struct _NautilusNotificationManagerPrivate
{
  GtkWidget *grid;
};

G_DEFINE_TYPE_WITH_PRIVATE (NautilusNotificationManager, nautilus_notification_manager, GD_TYPE_NOTIFICATION)

NautilusNotificationManager *
nautilus_notification_manager_new (void)
{
  return g_object_new (NAUTILUS_TYPE_NOTIFICATION_MANAGER,
                       "show-close-button", FALSE,
                       "timeout", -1,
                       NULL);
}

void
nautilus_notification_manager_add_notification (NautilusNotificationManager *self,
                                                GtkWidget                   *notification)
{
  gtk_container_add (GTK_CONTAINER (self->priv->grid), notification);
  gtk_widget_show_all (GTK_WIDGET (self));
}

static void
nautilus_notification_manager_remove (NautilusNotificationManager *self)
{
  gtk_widget_hide (GTK_WIDGET (self));
  gtk_container_foreach (GTK_CONTAINER (self->priv->grid),
                         (GtkCallback) gtk_widget_destroy,
                         NULL);
}

static void
nautilus_notification_manager_undo_manager_changed (NautilusFileUndoManager     *undo_manager,
                                                    NautilusNotificationManager *self)
{
  nautilus_notification_manager_remove (self);
}

static void
nautilus_notification_manager_finalize (GObject *object)
{
  NautilusNotificationManager *self = NAUTILUS_NOTIFICATION_MANAGER (object);

  g_signal_handlers_disconnect_by_func (nautilus_file_undo_manager_get (),
                                        G_CALLBACK (nautilus_notification_manager_undo_manager_changed),
                                        self);

  G_OBJECT_CLASS (nautilus_notification_manager_parent_class)->finalize (object);
}

static void
nautilus_notification_manager_class_init (NautilusNotificationManagerClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);

  object_class->finalize = nautilus_notification_manager_finalize;
}

static void
nautilus_notification_manager_init (NautilusNotificationManager *self)
{
  self->priv = nautilus_notification_manager_get_instance_private (self);

  gtk_widget_set_halign (GTK_WIDGET (self), GTK_ALIGN_CENTER);
  gtk_widget_set_valign (GTK_WIDGET (self), GTK_ALIGN_START);

  self->priv->grid = gtk_grid_new ();
  gtk_orientable_set_orientation (GTK_ORIENTABLE (self->priv->grid),
                                  GTK_ORIENTATION_VERTICAL);
  gtk_grid_set_row_spacing (GTK_GRID (self->priv->grid), 6);
  gtk_container_add (GTK_CONTAINER (self), self->priv->grid);

  g_signal_connect_swapped (self->priv->grid, "remove",
                            G_CALLBACK (nautilus_notification_manager_remove), self);
  /* Only support one undo operation at once. So if undo state changes,
   * remove all notifications and the new one will be added to the appropiate
   * notification manager
   */
  g_signal_connect_object (nautilus_file_undo_manager_get (), "undo-changed",
                           G_CALLBACK (nautilus_notification_manager_undo_manager_changed), self, 0);
}