synced-tabs-dialog.c 11.3 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 © 2017 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 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
 *
 *  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 "synced-tabs-dialog.h"

#include "ephy-embed-prefs.h"
#include "ephy-embed-shell.h"
#include "ephy-favicon-helpers.h"
#include "ephy-shell.h"

#include <json-glib/json-glib.h>

#define PIXBUF_MISSING_PATH "/org/gnome/epiphany/missing-thumbnail.png"

struct _SyncedTabsDialog {
  GtkDialog parent_instance;

  EphyOpenTabsManager *manager;

  WebKitFaviconDatabase *database;
  GdkPixbuf *pixbuf_root;
  GdkPixbuf *pixbuf_missing;

  GtkTreeModel *treestore;
  GtkWidget *treeview;
};

G_DEFINE_TYPE (SyncedTabsDialog, synced_tabs_dialog, GTK_TYPE_DIALOG)

enum {
  ICON_COLUMN,
  TITLE_COLUMN,
  URL_COLUMN
};

enum {
  PROP_0,
  PROP_OPEN_TABS_MANAGER,
  LAST_PROP
};

static GParamSpec *obj_properties[LAST_PROP];

typedef struct {
  SyncedTabsDialog *dialog;
  char             *title;
  char             *url;
  guint             parent_index;
} PopulateRowAsyncData;

static PopulateRowAsyncData *
populate_row_async_data_new (SyncedTabsDialog *dialog,
                             const char       *title,
                             const char       *url,
                             guint             parent_index)
{
  PopulateRowAsyncData *data;

  data = g_slice_new (PopulateRowAsyncData);
  data->dialog = g_object_ref (dialog);
  data->title = g_strdup (title);
  data->url = g_strdup (url);
  data->parent_index = parent_index;

  return data;
}

static void
populate_row_async_data_free (PopulateRowAsyncData *data)
{
  g_object_unref (data->dialog);
  g_free (data->title);
  g_free (data->url);
  g_slice_free (PopulateRowAsyncData, data);
}

static void
treeview_row_activated_cb (GtkTreeView       *view,
                           GtkTreePath       *path,
                           GtkTreeViewColumn *column,
                           gpointer           user_data)
{
  EphyShell *shell;
  EphyEmbed *embed;
  GtkWindow *window;
  GtkTreeModel *model;
  GtkTreeIter iter;
  char *url;
  char *path_str;

  /* No action on top-level rows. */
  if (gtk_tree_path_get_depth (path) == 1)
    return;

  /* No action on local tabs, i.e. children of first top-level row. */
  path_str = gtk_tree_path_to_string (path);
  if (g_str_has_prefix (path_str, "0:"))
    goto out;

  model = gtk_tree_view_get_model (view);
  gtk_tree_model_get_iter (model, &iter, path);
  gtk_tree_model_get (model, &iter, URL_COLUMN, &url, -1);

  shell = ephy_shell_get_default ();
  window = gtk_application_get_active_window (GTK_APPLICATION (shell));
  embed = ephy_shell_new_tab (shell, EPHY_WINDOW (window),
125
                              NULL, EPHY_NEW_TAB_JUMP);
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
  ephy_web_view_load_url (ephy_embed_get_web_view (embed), url);

  g_free (url);
out:
  g_free (path_str);
}

static void
synced_tabs_dialog_favicon_loaded_cb (GObject      *source,
                                      GAsyncResult *result,
                                      gpointer      user_data)
{
  WebKitFaviconDatabase *database = WEBKIT_FAVICON_DATABASE (source);
  PopulateRowAsyncData *data = (PopulateRowAsyncData *)user_data;
  cairo_surface_t *surface;
  GdkPixbuf *favicon = NULL;
  GtkTreeIter parent_iter;
  char *escaped_url;

  surface = webkit_favicon_database_get_favicon_finish (database, result, NULL);
  if (surface) {
    favicon = ephy_pixbuf_get_from_surface_scaled (surface, FAVICON_SIZE, FAVICON_SIZE);
    cairo_surface_destroy (surface);
  }

  gtk_tree_model_get_iter_first (data->dialog->treestore, &parent_iter);
  for (guint i = 0; i < data->parent_index; i++)
    gtk_tree_model_iter_next (data->dialog->treestore, &parent_iter);

  favicon = favicon ? favicon : data->dialog->pixbuf_missing;
  escaped_url = g_markup_escape_text (data->url, -1);
  gtk_tree_store_insert_with_values (GTK_TREE_STORE (data->dialog->treestore),
                                     NULL, &parent_iter, -1,
                                     ICON_COLUMN, favicon,
                                     TITLE_COLUMN, data->title,
                                     URL_COLUMN, escaped_url,
                                     -1);

  g_free (escaped_url);
  populate_row_async_data_free (data);
}

static void
synced_tabs_dialog_populate_from_record (SyncedTabsDialog   *dialog,
                                         EphyOpenTabsRecord *record,
                                         gboolean            is_local,
                                         guint               index)
{
  PopulateRowAsyncData *data;
  JsonArray *url_history;
176
  GList *tabs;
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
  const char *title;
  const char *url;

  g_assert (EPHY_IS_SYNCED_TABS_DIALOG (dialog));
  g_assert (EPHY_IS_OPEN_TABS_RECORD (record));

  if (is_local)
    title = _("Local Tabs");
  else
    title = ephy_open_tabs_record_get_client_name (record);

  /* Insert top-level row. */
  gtk_tree_store_insert_with_values (GTK_TREE_STORE (dialog->treestore),
                                     NULL, NULL, -1,
                                     ICON_COLUMN, dialog->pixbuf_root,
                                     TITLE_COLUMN, title,
                                     URL_COLUMN, NULL,
                                     -1);

  tabs = ephy_open_tabs_record_get_tabs (record);
197
  for (GList *l = tabs; l && l->data; l = l->next) {
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    title = json_object_get_string_member (l->data, "title");
    url_history = json_object_get_array_member (l->data, "urlHistory");
    url = json_array_get_string_element (url_history, 0);

    data = populate_row_async_data_new (dialog, title, url, index);
    webkit_favicon_database_get_favicon (dialog->database, url, NULL,
                                         synced_tabs_dialog_favicon_loaded_cb,
                                         data);
  }
}

static void
synced_tabs_dialog_populate_model (SyncedTabsDialog *dialog)
{
  EphyOpenTabsRecord *record;
213
  GList *remotes;
214 215 216 217 218 219 220 221
  guint index = 0;

  /* Insert local tabs. */
  record = ephy_open_tabs_manager_get_local_tabs (dialog->manager);
  synced_tabs_dialog_populate_from_record (dialog, record, TRUE, index++);

  /* Insert remote tabs. */
  remotes = ephy_open_tabs_manager_get_remote_tabs (dialog->manager);
222
  for (GList *l = remotes; l && l->data; l = l->next)
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    synced_tabs_dialog_populate_from_record (dialog, l->data, FALSE, index++);

  g_object_unref (record);
}

static void
synced_tabs_dialog_set_property (GObject      *object,
                                 guint         prop_id,
                                 const GValue *value,
                                 GParamSpec   *pspec)
{
  SyncedTabsDialog *dialog = EPHY_SYNCED_TABS_DIALOG (object);

  switch (prop_id) {
    case PROP_OPEN_TABS_MANAGER:
238 239
      if (dialog->manager)
        g_object_unref (dialog->manager);
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
      dialog->manager = g_object_ref (g_value_get_object (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  }
}

static void
synced_tabs_dialog_get_property (GObject    *object,
                                 guint       prop_id,
                                 GValue     *value,
                                 GParamSpec *pspec)
{
  SyncedTabsDialog *dialog = EPHY_SYNCED_TABS_DIALOG (object);

  switch (prop_id) {
    case PROP_OPEN_TABS_MANAGER:
      g_value_set_object (value, dialog->manager);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  }
}

static void
synced_tabs_dialog_constructed (GObject *object)
{
  SyncedTabsDialog *dialog = EPHY_SYNCED_TABS_DIALOG (object);

  G_OBJECT_CLASS (synced_tabs_dialog_parent_class)->constructed (object);

  synced_tabs_dialog_populate_model (dialog);
}

static void
synced_tabs_dialog_dispose (GObject *object)
{
  SyncedTabsDialog *dialog = EPHY_SYNCED_TABS_DIALOG (object);

  g_clear_object (&dialog->manager);
  g_clear_object (&dialog->pixbuf_root);
  g_clear_object (&dialog->pixbuf_missing);

  G_OBJECT_CLASS (synced_tabs_dialog_parent_class)->dispose (object);
}

static void
synced_tabs_dialog_class_init (SyncedTabsDialogClass *klass)
{
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->set_property = synced_tabs_dialog_set_property;
  object_class->get_property = synced_tabs_dialog_get_property;
  object_class->constructed = synced_tabs_dialog_constructed;
  object_class->dispose = synced_tabs_dialog_dispose;

  obj_properties[PROP_OPEN_TABS_MANAGER] =
    g_param_spec_object ("open-tabs-manager",
                         "Open tabs manager",
                         "Open Tabs Manager",
                         EPHY_TYPE_OPEN_TABS_MANAGER,
                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (object_class, LAST_PROP, obj_properties);

  gtk_widget_class_set_template_from_resource (widget_class,
                                               "/org/gnome/epiphany/gtk/synced-tabs-dialog.ui");

  gtk_widget_class_bind_template_child (widget_class, SyncedTabsDialog, treestore);
  gtk_widget_class_bind_template_child (widget_class, SyncedTabsDialog, treeview);
  gtk_widget_class_bind_template_callback (widget_class, treeview_row_activated_cb);
}

static void
synced_tabs_dialog_init (SyncedTabsDialog *dialog)
{
  WebKitWebContext *context;
  GdkPixbuf *pixbuf;
  GError *error = NULL;

  gtk_widget_init_template (GTK_WIDGET (dialog));

  gtk_tree_view_set_tooltip_column (GTK_TREE_VIEW (dialog->treeview), URL_COLUMN);

  context = ephy_embed_shell_get_web_context (ephy_embed_shell_get_default ());
  dialog->database = webkit_web_context_get_favicon_database (context);

  dialog->pixbuf_root = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
                                                  "computer-symbolic",
                                                  FAVICON_SIZE, 0, &error);
  if (error) {
    g_warning ("Failed to build pixbuf from theme icon: %s", error->message);
    g_error_free (error);
    error = NULL;
  }

  pixbuf = gdk_pixbuf_new_from_resource (PIXBUF_MISSING_PATH, &error);
  if (pixbuf) {
    dialog->pixbuf_missing = gdk_pixbuf_scale_simple (pixbuf,
                                                      FAVICON_SIZE, FAVICON_SIZE,
                                                      GDK_INTERP_BILINEAR);
    g_object_unref (pixbuf);
  } else {
    g_warning ("Failed to build pixbuf from resource: %s", error->message);
    g_error_free (error);
  }
}

SyncedTabsDialog *
synced_tabs_dialog_new (EphyOpenTabsManager *manager)
{
  return EPHY_SYNCED_TABS_DIALOG (g_object_new (EPHY_TYPE_SYNCED_TABS_DIALOG,
                                                "use-header-bar", TRUE,
                                                "open-tabs-manager", manager,
                                                NULL));
}