1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3  *  Copyright © 2016 Iulian-Gabriel Radu <iulian.radu67@gmail.com>
4  *
5  *  This file is part of Epiphany.
6  *
7  *  Epiphany is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  Epiphany is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with Epiphany.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "config.h"
22 
23 #include "ephy-add-bookmark-popover.h"
24 
25 #include "ephy-bookmark-properties.h"
26 #include "ephy-bookmarks-manager.h"
27 #include "ephy-embed-container.h"
28 #include "ephy-location-entry.h"
29 #include "ephy-shell.h"
30 #include "ephy-sync-utils.h"
31 
32 struct _EphyAddBookmarkPopover {
33   GtkPopover parent_instance;
34 
35   char *address;
36 
37   GtkWidget *grid;
38   GtkWidget *relative_to;
39   GtkWindow *window;
40 };
41 
42 G_DEFINE_TYPE (EphyAddBookmarkPopover, ephy_add_bookmark_popover, GTK_TYPE_POPOVER)
43 
44 enum {
45   PROP_0,
46   PROP_RELATIVE_TO,
47   PROP_WINDOW,
48   LAST_PROP
49 };
50 
51 enum signalsEnum {
52   UPDATE_STATE,
53   LAST_SIGNAL
54 };
55 
56 static gint signals[LAST_SIGNAL] = { 0 };
57 
58 static GParamSpec *obj_properties[LAST_PROP];
59 
60 static void
ephy_bookmarks_popover_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)61 ephy_bookmarks_popover_set_property (GObject      *object,
62                                      guint         prop_id,
63                                      const GValue *value,
64                                      GParamSpec   *pspec)
65 {
66   EphyAddBookmarkPopover *self = EPHY_ADD_BOOKMARK_POPOVER (object);
67 
68   switch (prop_id) {
69     case PROP_RELATIVE_TO:
70       self->relative_to = g_value_get_object (value);
71       break;
72     case PROP_WINDOW:
73       self->window = g_value_get_object (value);
74       break;
75     default:
76       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
77   }
78 }
79 
80 static void
ephy_add_bookmark_popover_finalize(GObject * object)81 ephy_add_bookmark_popover_finalize (GObject *object)
82 {
83   EphyAddBookmarkPopover *self = EPHY_ADD_BOOKMARK_POPOVER (object);
84 
85   if (self->address)
86     g_free (self->address);
87 
88   G_OBJECT_CLASS (ephy_add_bookmark_popover_parent_class)->finalize (object);
89 }
90 
91 static void
ephy_add_bookmark_popover_constructed(GObject * object)92 ephy_add_bookmark_popover_constructed (GObject *object)
93 {
94   EphyAddBookmarkPopover *self = EPHY_ADD_BOOKMARK_POPOVER (object);
95 
96   G_OBJECT_CLASS (ephy_add_bookmark_popover_parent_class)->constructed (object);
97 
98   gtk_popover_set_relative_to (GTK_POPOVER (self), self->relative_to);
99 }
100 
101 static void
ephy_add_bookmark_popover_class_init(EphyAddBookmarkPopoverClass * klass)102 ephy_add_bookmark_popover_class_init (EphyAddBookmarkPopoverClass *klass)
103 {
104   GObjectClass *object_class = G_OBJECT_CLASS (klass);
105 
106   object_class->set_property = ephy_bookmarks_popover_set_property;
107   object_class->finalize = ephy_add_bookmark_popover_finalize;
108   object_class->constructed = ephy_add_bookmark_popover_constructed;
109 
110   obj_properties[PROP_RELATIVE_TO] =
111     g_param_spec_object ("relative-to",
112                          "A GtkWidget object",
113                          "The popover's parent widget",
114                          GTK_TYPE_WIDGET,
115                          G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
116   obj_properties[PROP_WINDOW] =
117     g_param_spec_object ("window",
118                          "A GtkWidget object",
119                          "The popover's parent window",
120                          GTK_TYPE_WIDGET,
121                          G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
122 
123   g_object_class_install_properties (object_class, LAST_PROP, obj_properties);
124 
125   /**
126    * EphAddBookmarkPopover::update-state:
127    * @entry: the object on which the signal is emitted
128    *
129    * Emitted when the bookmark state changes
130    *
131    */
132   signals[UPDATE_STATE] = g_signal_new ("update-state", G_OBJECT_CLASS_TYPE (klass),
133                                         G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST,
134                                         0, NULL, NULL, NULL,
135                                         G_TYPE_NONE,
136                                         1,
137                                         G_TYPE_INT);
138 }
139 
140 static void
ephy_add_bookmark_popover_notify_visible_cb(GtkPopover * popover,GParamSpec * param,gpointer user_data)141 ephy_add_bookmark_popover_notify_visible_cb (GtkPopover *popover,
142                                              GParamSpec *param,
143                                              gpointer    user_data)
144 {
145   EphyAddBookmarkPopover *self;
146   EphyBookmarksManager *manager;
147 
148   g_assert (EPHY_IS_ADD_BOOKMARK_POPOVER (popover));
149 
150   if (gtk_widget_get_visible (GTK_WIDGET (popover)))
151     return;
152 
153   self = EPHY_ADD_BOOKMARK_POPOVER (popover);
154   manager = ephy_shell_get_bookmarks_manager (ephy_shell_get_default ());
155 
156   ephy_bookmarks_manager_save (manager,
157                                ephy_bookmarks_manager_save_warn_on_error_cancellable (manager),
158                                ephy_bookmarks_manager_save_warn_on_error_cb,
159                                NULL);
160 
161   g_clear_pointer (&self->address, g_free);
162   g_clear_pointer (&self->grid, gtk_widget_destroy);
163 }
164 
165 static void
ephy_add_bookmark_popover_init(EphyAddBookmarkPopover * self)166 ephy_add_bookmark_popover_init (EphyAddBookmarkPopover *self)
167 {
168   g_signal_connect (self, "notify::visible",
169                     G_CALLBACK (ephy_add_bookmark_popover_notify_visible_cb),
170                     NULL);
171 }
172 
173 GtkWidget *
ephy_add_bookmark_popover_new(GtkWidget * relative_to,GtkWidget * window)174 ephy_add_bookmark_popover_new (GtkWidget *relative_to,
175                                GtkWidget *window)
176 {
177   return g_object_new (EPHY_TYPE_ADD_BOOKMARK_POPOVER,
178                        "relative-to", relative_to,
179                        "window", window,
180                        NULL);
181 }
182 
183 /**
184  * update_bookmarked_status_cb:
185  * @bookmark: an #EphyBookmark object
186  * @header_bar: an #EphyHeaderBar widget
187  *
188  * Remove bookmarked status if the @bookmark was removed.
189  *
190  **/
191 static void
ephy_add_bookmark_popover_update_bookmarked_status_cb(EphyAddBookmarkPopover * self,EphyBookmark * bookmark,EphyBookmarksManager * manager)192 ephy_add_bookmark_popover_update_bookmarked_status_cb (EphyAddBookmarkPopover *self,
193                                                        EphyBookmark           *bookmark,
194                                                        EphyBookmarksManager   *manager)
195 {
196   EphyEmbed *embed;
197   EphyWebView *view;
198   const char *address;
199 
200   g_assert (EPHY_IS_ADD_BOOKMARK_POPOVER (self));
201   g_assert (EPHY_IS_BOOKMARK (bookmark));
202   g_assert (EPHY_IS_BOOKMARKS_MANAGER (manager));
203 
204   embed = ephy_embed_container_get_active_child (EPHY_EMBED_CONTAINER (self->window));
205   view = ephy_embed_get_web_view (embed);
206 
207   address = ephy_web_view_get_address (view);
208 
209   if (g_strcmp0 (ephy_bookmark_get_url (bookmark), address) == 0)
210     g_signal_emit (self, signals[UPDATE_STATE], 0, EPHY_BOOKMARK_ICON_EMPTY);
211 
212   ephy_bookmarks_manager_save (manager,
213                                ephy_bookmarks_manager_save_warn_on_error_cancellable (manager),
214                                ephy_bookmarks_manager_save_warn_on_error_cb,
215                                NULL);
216 
217   gtk_popover_popdown (GTK_POPOVER (self));
218 }
219 
220 void
ephy_add_bookmark_popover_show(EphyAddBookmarkPopover * self)221 ephy_add_bookmark_popover_show (EphyAddBookmarkPopover *self)
222 {
223   EphyBookmarksManager *manager;
224   EphyBookmark *bookmark;
225   EphyEmbed *embed;
226   const char *address;
227 
228   manager = ephy_shell_get_bookmarks_manager (ephy_shell_get_default ());
229   embed = ephy_embed_container_get_active_child (EPHY_EMBED_CONTAINER (self->window));
230 
231   address = ephy_web_view_get_address (ephy_embed_get_web_view (embed));
232 
233   bookmark = ephy_bookmarks_manager_get_bookmark_by_url (manager, address);
234   if (!bookmark) {
235     g_autofree char *id = NULL;
236     g_autoptr (EphyBookmark) new_bookmark = NULL;
237 
238     id = ephy_bookmark_generate_random_id ();
239     new_bookmark = ephy_bookmark_new (address,
240                                       ephy_embed_get_title (embed),
241                                       g_sequence_new (g_free),
242                                       id);
243 
244     ephy_bookmarks_manager_add_bookmark (manager, new_bookmark);
245     g_signal_emit (self, signals[UPDATE_STATE], 0, EPHY_BOOKMARK_ICON_BOOKMARKED);
246 
247     bookmark = new_bookmark;
248   }
249 
250   g_signal_connect_object (manager, "bookmark-removed",
251                            G_CALLBACK (ephy_add_bookmark_popover_update_bookmarked_status_cb),
252                            self,
253                            G_CONNECT_SWAPPED);
254 
255   self->grid = ephy_bookmark_properties_new (bookmark,
256                                              EPHY_BOOKMARK_PROPERTIES_TYPE_POPOVER,
257                                              GTK_WIDGET (self));
258   gtk_container_add (GTK_CONTAINER (self), self->grid);
259   gtk_popover_set_default_widget (GTK_POPOVER (self),
260                                   ephy_bookmark_properties_get_add_tag_button (EPHY_BOOKMARK_PROPERTIES (self->grid)));
261 
262   g_free (self->address);
263   self->address = g_strdup (address);
264 
265   gtk_popover_popup (GTK_POPOVER (self));
266 }
267