summaryrefslogtreecommitdiff
path: root/src/nautilus-notification-delete.c
blob: 70b93d3826e756254c6614d823a47a385d32237c (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* nautilus-notification-delete.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 <glib/gi18n.h>

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

struct _NautilusNotificationDeletePrivate
{
  guint n_items;
  guint timeout_id;
  gchar *file_name;
  NautilusWindow *window;
};

G_DEFINE_TYPE_WITH_PRIVATE (NautilusNotificationDelete, nautilus_notification_delete, GTK_TYPE_GRID)

enum {
  PROP_0,
  PROP_N_ITEMS,
  PROP_FILE_NAME,
  PROP_WINDOW,
  LAST_PROP
};

enum
{
  NOTIFICATION_TIMEOUT = 10
};

static void
nautilus_notification_delete_remove_timeout (NautilusNotificationDelete *self)
{
  if (self->priv->timeout_id != 0)
    {
      g_source_remove (self->priv->timeout_id);
      self->priv->timeout_id = 0;
    }
}

static void
nautilus_notification_delete_destroy (NautilusNotificationDelete *self)
{
  nautilus_notification_delete_remove_timeout (self);
  gtk_widget_destroy (GTK_WIDGET (self));
}

static gboolean
nautilus_notification_delete_on_timeout (gpointer user_data)
{
  NautilusNotificationDelete *self = NAUTILUS_NOTIFICATION_DELETE (user_data);

  self->priv->timeout_id = 0;
  gtk_widget_destroy (GTK_WIDGET (self));

  return G_SOURCE_REMOVE;
}

static void
nautilus_notification_delete_undo_clicked (NautilusNotificationDelete *self)
{
  nautilus_notification_delete_remove_timeout (self);
  /* Notification manager will destroy all notifications after the undo
   * state changes. So no need to destroy the notification it now */
  nautilus_file_undo_manager_undo (GTK_WINDOW (self->priv->window));
}

static void
nautilus_notification_delete_constructed (GObject *object)
{
  NautilusNotificationDelete *self = NAUTILUS_NOTIFICATION_DELETE (object);
  GtkWidget *close;
  gchar *label;
  GtkWidget *label_widget;
  GtkWidget *undo;

  G_OBJECT_CLASS (nautilus_notification_delete_parent_class)->constructed (object);

  if (self->priv->n_items == 1) {
    if (self->priv->file_name != NULL)
      label = g_strdup_printf (_("%s deleted"), self->priv->file_name);
    else
      label = g_strdup (_("File deleted"));
  } else {
    label = g_strdup_printf (_("%d files deleted"),
                             self->priv->n_items);
  }
  label_widget = gtk_label_new (label);
  gtk_widget_set_halign (label_widget, GTK_ALIGN_START);
  gtk_container_add (GTK_CONTAINER (self), label_widget);
  gtk_widget_set_margin_end (label_widget, 30);

  undo = gtk_button_new_with_label (_("Undo"));
  gtk_widget_set_valign (undo, GTK_ALIGN_CENTER);
  gtk_container_add (GTK_CONTAINER (self), undo);
  gtk_widget_set_margin_end (undo, 6);
  g_signal_connect_swapped (undo, "clicked", G_CALLBACK (nautilus_notification_delete_undo_clicked), self);

  close = gtk_button_new_from_icon_name ("window-close-symbolic", GTK_ICON_SIZE_BUTTON);
  gtk_widget_set_valign (close, GTK_ALIGN_CENTER);
  gtk_button_set_focus_on_click (GTK_BUTTON (close), FALSE);
  gtk_button_set_relief (GTK_BUTTON (close), GTK_RELIEF_NONE);
  gtk_container_add (GTK_CONTAINER (self), close);
  g_signal_connect_swapped (close, "clicked", G_CALLBACK (nautilus_notification_delete_destroy), self);

  self->priv->timeout_id = g_timeout_add_seconds_full (G_PRIORITY_DEFAULT,
                                                       NOTIFICATION_TIMEOUT,
                                                       nautilus_notification_delete_on_timeout,
                                                       g_object_ref (self),
                                                       g_object_unref);

  g_free (label);
}

NautilusNotificationDelete *
nautilus_notification_delete_new (NautilusWindow *window,
                                  guint           n_items,
                                  gchar          *file_name)
{
  g_assert (NAUTILUS_IS_WINDOW (window));

  return g_object_new (NAUTILUS_TYPE_NOTIFICATION_DELETE,
                       "window", window,
                       "n-items", n_items,
                       "file-name", file_name,
                       "column-spacing", 0,
                       "margin-start", 12,
                       "margin-end", 4,
                       NULL);
}

static void
nautilus_notification_delete_set_property (GObject      *object,
                                           guint         prop_id,
                                           const GValue *value,
                                           GParamSpec   *pspec)
{
  NautilusNotificationDelete *self = NAUTILUS_NOTIFICATION_DELETE (object);

  switch (prop_id)
    {
    case PROP_N_ITEMS:
      self->priv->n_items = g_value_get_int (value);
      break;

    case PROP_FILE_NAME:
      if (self->priv->file_name != NULL)
        g_free (self->priv->file_name);
      self->priv->file_name = g_strdup (g_value_get_string (value));
      break;

    case PROP_WINDOW:
      self->priv->window = NAUTILUS_WINDOW (g_value_get_pointer (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
nautilus_notification_delete_finalize (GObject *object)
{
  NautilusNotificationDelete *self = NAUTILUS_NOTIFICATION_DELETE (object);

  if (self->priv->file_name != NULL)
    g_free (self->priv->file_name);

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

static void
nautilus_notification_delete_class_init (NautilusNotificationDeleteClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->set_property = nautilus_notification_delete_set_property;
  object_class->constructed = nautilus_notification_delete_constructed;
  object_class->finalize= nautilus_notification_delete_finalize;

  g_object_class_install_property (object_class,
                                   PROP_N_ITEMS,
                                   g_param_spec_int ("n-items",
                                                     "Number of deleted items",
                                                     "Number of items that are being deleted",
                                                     1,
                                                     G_MAXINT,
                                                     1,
                                                     G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
  g_object_class_install_property (object_class,
                                   PROP_FILE_NAME,
                                   g_param_spec_string ("file-name",
                                                        "Name of deleted file",
                                                        "Nane of the deleted file if it was only one file",
                                                        "",
                                                        G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
  g_object_class_install_property (object_class,
                                   PROP_WINDOW,
                                   g_param_spec_pointer ("window",
                                                         "Window associated",
                                                         "The window that contains the notification",
                                                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
}

static void
nautilus_notification_delete_init (NautilusNotificationDelete *self)
{
  self->priv = nautilus_notification_delete_get_instance_private (self);
}