ephy-notification.c 7.02 KB
Newer Older
1
2
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
Gabriel Ivascu's avatar
Gabriel Ivascu committed
3
 *  Copyright © 2016 Gabriel Ivascu <gabrielivascu@gnome.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 *
 *  This file is part of Epiphany.
 *
 *  Epiphany 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.
 *
 *  Epiphany 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 Epiphany.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"
#include "ephy-notification.h"

#include "ephy-notification-container.h"

struct _EphyNotification {
27
  GtkBin parent_instance;
Michael Catanzaro's avatar
Michael Catanzaro committed
28
29

  GtkWidget *grid;
30
31
32

  GtkWidget *head;
  GtkWidget *body;
Michael Catanzaro's avatar
Michael Catanzaro committed
33
  GtkWidget *close_button;
34

35
36
  char *head_msg;
  char *body_msg;
37
38
39
40
41
42
43
44
};

enum {
  PROP_0,
  PROP_HEAD,
  PROP_BODY
};

45
46
47
48
49
50
51
enum {
  CLOSE,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

52
G_DEFINE_TYPE (EphyNotification, ephy_notification, GTK_TYPE_BIN);
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

static void
ephy_notification_constructed (GObject *object)
{
  EphyNotification *self = EPHY_NOTIFICATION (object);

  G_OBJECT_CLASS (ephy_notification_parent_class)->constructed (object);

  gtk_label_set_text (GTK_LABEL (self->head), self->head_msg);
  gtk_label_set_text (GTK_LABEL (self->body), self->body_msg);
}

static void
ephy_notification_finalize (GObject *object)
{
  EphyNotification *self = EPHY_NOTIFICATION (object);

  g_free (self->head_msg);
  g_free (self->body_msg);

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

static void
ephy_notification_set_property (GObject      *object,
                                guint         prop_id,
                                const GValue *value,
                                GParamSpec   *pspec)
{
  EphyNotification *self = EPHY_NOTIFICATION (object);

  switch (prop_id) {
    case PROP_HEAD:
      self->head_msg = g_value_dup_string (value);
      break;
    case PROP_BODY:
      self->body_msg = g_value_dup_string (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
ephy_notification_get_property (GObject    *object,
                                guint       prop_id,
                                GValue     *value,
                                GParamSpec *pspec)
{
  EphyNotification *self = EPHY_NOTIFICATION (object);

  switch (prop_id) {
    case PROP_HEAD:
      g_value_set_string (value, self->head_msg);
      break;
    case PROP_BODY:
      g_value_set_string (value, self->body_msg);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

Michael Catanzaro's avatar
Michael Catanzaro committed
118
119
120
121
static void
close_button_clicked_cb (GtkButton        *button,
                         EphyNotification *self)
{
122
  g_signal_emit (self, signals[CLOSE], 0);
Michael Catanzaro's avatar
Michael Catanzaro committed
123
124
}

125
126
127
static void
ephy_notification_init (EphyNotification *self)
{
Michael Catanzaro's avatar
Michael Catanzaro committed
128
129
130
131
132
133
134
135
  GtkWidget *image;
  GtkStyleContext *context;

  self->grid = gtk_grid_new ();
  context = gtk_widget_get_style_context (self->grid);
  gtk_style_context_add_class (context, "app-notification");
  gtk_container_add (GTK_CONTAINER (self), self->grid);

136
  self->head = gtk_label_new (NULL);
137
  gtk_label_set_line_wrap (GTK_LABEL (self->head), TRUE);
138
  gtk_label_set_xalign (GTK_LABEL (self->head), 0);
139
  gtk_widget_set_hexpand (self->head, TRUE);
Michael Catanzaro's avatar
Michael Catanzaro committed
140
  gtk_grid_attach (GTK_GRID (self->grid), self->head, 0, 0, 1, 1);
141
142

  self->body = gtk_label_new (NULL);
143
  gtk_label_set_line_wrap (GTK_LABEL (self->body), TRUE);
144
  gtk_label_set_xalign (GTK_LABEL (self->body), 0);
145
  gtk_widget_set_hexpand (self->body, TRUE);
Michael Catanzaro's avatar
Michael Catanzaro committed
146
147
148
  gtk_grid_attach (GTK_GRID (self->grid), self->body, 0, 1, 1, 1);

  self->close_button = gtk_button_new ();
149
150
151
152
153
  gtk_widget_set_focus_on_click (self->close_button, FALSE);
  gtk_widget_set_margin_top (self->close_button, 6);
  gtk_widget_set_margin_bottom (self->close_button, 6);
  gtk_widget_set_margin_start (self->close_button, 6);
  gtk_widget_set_margin_end (self->close_button, 6);
154
  gtk_style_context_add_class (gtk_widget_get_style_context (self->close_button), "flat");
Michael Catanzaro's avatar
Michael Catanzaro committed
155
156
157
158
159
160
161
162
163
  gtk_grid_attach (GTK_GRID (self->grid), self->close_button, 1, 0, 1, 2);

  image = gtk_image_new_from_icon_name ("window-close-symbolic", GTK_ICON_SIZE_BUTTON);
  gtk_button_set_image (GTK_BUTTON (self->close_button), image);

  g_signal_connect (self->close_button,
                    "clicked",
                    G_CALLBACK (close_button_clicked_cb),
                    self);
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
}

static void
ephy_notification_class_init (EphyNotificationClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->constructed = ephy_notification_constructed;
  object_class->finalize = ephy_notification_finalize;
  object_class->set_property = ephy_notification_set_property;
  object_class->get_property = ephy_notification_get_property;

  g_object_class_install_property (object_class,
                                   PROP_HEAD,
                                   g_param_spec_string ("head",
                                                        "Head",
                                                        "The notification head",
                                                        "",
182
                                                        G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
183
184
185
186
187
188
  g_object_class_install_property (object_class,
                                   PROP_BODY,
                                   g_param_spec_string ("body",
                                                        "Body",
                                                        "The notification body",
                                                        "",
189
                                                        G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
190
191
192
193
194
195
196

  signals[CLOSE] =
    g_signal_new ("close",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
                  0, NULL, NULL, NULL,
                  G_TYPE_NONE, 0);
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
}

EphyNotification *
ephy_notification_new (const char *head,
                       const char *body)
{
  return g_object_new (EPHY_TYPE_NOTIFICATION,
                       "head", head,
                       "body", body,
                       NULL);
}

void
ephy_notification_show (EphyNotification *self)
{
Michael Catanzaro's avatar
Michael Catanzaro committed
212
  g_assert (EPHY_IS_NOTIFICATION (self));
213
214
215
216

  ephy_notification_container_add_notification (ephy_notification_container_get_default (),
                                                GTK_WIDGET (self));
}
217
218
219
220
221
222
223

gboolean
ephy_notification_is_duplicate (EphyNotification *notification_a,
                                EphyNotification *notification_b)
{
  return g_strcmp0 (notification_a->head_msg, notification_b->head_msg) == 0 && g_strcmp0 (notification_a->body_msg, notification_b->body_msg) == 0;
}