yelp-application.c 28.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * Copyright (C) 2010 Shaun McCance <shaunm@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 2 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
16
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
17 18 19 20 21 22 23 24
 *
 * Author: Shaun McCance <shaunm@gnome.org>
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

25 26 27 28
#define G_SETTINGS_ENABLE_BACKEND

#include <gio/gio.h>
#include <gio/gsettingsbackend.h>
29
#include <glib/gi18n.h>
30
#include <gtk/gtk.h>
31
#ifdef GDK_WINDOWING_X11
32
#include <gdk/gdkx.h>
33
#endif
34
#include <stdlib.h>
35

36
#include "yelp-bookmarks.h"
37
#include "yelp-settings.h"
38 39
#include "yelp-view.h"

40
#include "yelp-application.h"
41
#include "yelp-window.h"
42

43
#define DEFAULT_URI "help:gnome-help"
44

45 46
static gboolean editor_mode = FALSE;

47 48 49 50 51 52 53 54 55 56 57
G_GNUC_NORETURN static gboolean
option_version_cb (const gchar *option_name,
	           const gchar *value,
	           gpointer     data,
	           GError     **error)
{
	g_print ("%s %s\n", PACKAGE, VERSION);

	exit (0);
}

58 59
static const GOptionEntry entries[] = {
    {"editor-mode", 0, 0, G_OPTION_ARG_NONE, &editor_mode, N_("Turn on editor mode"), NULL},
60
    { "version", 0, G_OPTION_FLAG_NO_ARG | G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, option_version_cb, NULL, NULL },
61 62 63
    { NULL }
};

64 65 66
typedef struct _YelpApplicationLoad YelpApplicationLoad;
struct _YelpApplicationLoad {
    YelpApplication *app;
67
    guint32 timestamp;
68
    gboolean new;
69
    gboolean fallback_help_list;
70 71
};

72 73 74
static void          yelp_application_iface_init       (YelpBookmarksInterface *iface);
static void          yelp_application_dispose          (GObject                *object);
static void          yelp_application_finalize         (GObject                *object);
75

76 77 78
static gboolean      yelp_application_cmdline          (GApplication          *app,
                                                        gchar               ***arguments,
                                                        gint                  *exit_status);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
79
static void          yelp_application_startup          (GApplication          *app);
80 81
static int           yelp_application_command_line     (GApplication          *app,
                                                        GApplicationCommandLine *cmdline);
82 83
static void          application_uri_resolved          (YelpUri               *uri,
                                                        YelpApplicationLoad   *data);
84 85 86
static gboolean      application_window_deleted        (YelpWindow            *window,
                                                        GdkEvent              *event,
                                                        YelpApplication       *app);
Shaun McCance's avatar
Shaun McCance committed
87 88
GSettings *          application_get_doc_settings      (YelpApplication       *app,
                                                        const gchar           *doc_uri);
89 90
static void          application_adjust_font           (GAction               *action,
                                                        GVariant              *parameter,
91 92
                                                        YelpApplication       *app);
static void          application_set_font_sensitivity  (YelpApplication       *app);
93

94 95 96
static void          bookmarks_changed                 (GSettings             *settings,
                                                        const gchar           *key,
                                                        YelpApplication       *app);
97 98 99
static gboolean      window_resized                    (YelpWindow            *window,
                                                        YelpApplication       *app);

Allison Karlitskaya's avatar
Allison Karlitskaya committed
100
G_DEFINE_TYPE_WITH_CODE (YelpApplication, yelp_application, GTK_TYPE_APPLICATION,
101 102
                         G_IMPLEMENT_INTERFACE (YELP_TYPE_BOOKMARKS,
                                                yelp_application_iface_init))
103 104 105 106 107 108
#define GET_PRIV(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), YELP_TYPE_APPLICATION, YelpApplicationPrivate))

typedef struct _YelpApplicationPrivate YelpApplicationPrivate;
struct _YelpApplicationPrivate {
    GSList *windows;
    GHashTable *windows_by_document;
109

110
    GPropertyAction  *show_cursor_action;
111 112
    GSimpleAction    *larger_text_action;
    GSimpleAction    *smaller_text_action;
113

114
    GSettingsBackend *backend;
115
    GSettings *gsettings;
116
    GHashTable *docsettings;
117 118 119 120 121
};

static void
yelp_application_init (YelpApplication *app)
{
122 123 124 125
    YelpApplicationPrivate *priv = GET_PRIV (app);
    priv->docsettings = g_hash_table_new_full (g_str_hash, g_str_equal,
                                               (GDestroyNotify) g_free,
                                               (GDestroyNotify) g_object_unref);
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
    gtk_application_set_accels_for_action (GTK_APPLICATION (app),
                                           "app.yelp-application-show-cursor",
                                           (const gchar*[]) {"F7", NULL});
    gtk_application_set_accels_for_action (GTK_APPLICATION (app),
                                           "app.yelp-application-larger-text",
                                           (const gchar*[]) {"<Control>plus", NULL});
    gtk_application_set_accels_for_action (GTK_APPLICATION (app),
                                           "app.yelp-application-smaller-text",
                                           (const gchar*[]) {"<Control>minus", NULL});

    gtk_application_set_accels_for_action (GTK_APPLICATION (app),
                                           "win.yelp-window-find", (const gchar*[]) {"<Control>F", NULL});
    gtk_application_set_accels_for_action (GTK_APPLICATION (app),
                                           "win.yelp-window-search", (const gchar*[]) {"<Control>S", NULL});
    gtk_application_set_accels_for_action (GTK_APPLICATION (app),
                                           "win.yelp-window-new", (const gchar*[]) {"<Control>N", NULL});
    gtk_application_set_accels_for_action (GTK_APPLICATION (app),
                                           "win.yelp-window-close", (const gchar*[]) {"<Control>W", NULL});
    gtk_application_set_accels_for_action (GTK_APPLICATION (app),
                                           "win.yelp-window-ctrll", (const gchar*[]) {"<Control>L", NULL});
    gtk_application_set_accels_for_action (GTK_APPLICATION (app),
                                           "win.yelp-view-print", (const gchar*[]) {"<Control>P", NULL});

    gtk_application_set_accels_for_action (GTK_APPLICATION (app),
                                           "win.yelp-view-go-back",
                                           (const gchar*[]) {"<Alt>Left", NULL});
    gtk_application_set_accels_for_action (GTK_APPLICATION (app),
                                           "win.yelp-view-go-forward",
                                           (const gchar*[]) {"<Alt>Right", NULL});
    gtk_application_set_accels_for_action (GTK_APPLICATION (app),
                                           "win.yelp-view-go-previous",
                                           (const gchar*[]) {"<Control>Page_Up", NULL});
    gtk_application_set_accels_for_action (GTK_APPLICATION (app),
                                           "win.yelp-view-go-next",
                                           (const gchar*[]) {"<Control>Page_Down", NULL});
162 163 164 165 166
}

static void
yelp_application_class_init (YelpApplicationClass *klass)
{
Allison Karlitskaya's avatar
Allison Karlitskaya committed
167
    GApplicationClass *application_class = G_APPLICATION_CLASS (klass);
168 169
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

170
    application_class->local_command_line = yelp_application_cmdline;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
171
    application_class->startup = yelp_application_startup;
172
    application_class->command_line = yelp_application_command_line;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
173

174 175 176 177 178 179
    object_class->dispose = yelp_application_dispose;
    object_class->finalize = yelp_application_finalize;

    g_type_class_add_private (klass, sizeof (YelpApplicationPrivate));
}

180 181 182 183 184 185 186 187
static void
yelp_application_iface_init (YelpBookmarksInterface *iface)
{
    iface->add_bookmark = yelp_application_add_bookmark;
    iface->remove_bookmark = yelp_application_remove_bookmark;
    iface->is_bookmarked = yelp_application_is_bookmarked;
}

188 189 190 191 192
static void
yelp_application_dispose (GObject *object)
{
    YelpApplicationPrivate *priv = GET_PRIV (object);

193 194 195 196 197
    if (priv->show_cursor_action) {
        g_object_unref (priv->show_cursor_action);
        priv->show_cursor_action = NULL;
    }

198 199 200 201 202 203 204 205 206 207
    if (priv->larger_text_action) {
        g_object_unref (priv->larger_text_action);
        priv->larger_text_action = NULL;
    }

    if (priv->smaller_text_action) {
        g_object_unref (priv->smaller_text_action);
        priv->smaller_text_action = NULL;
    }

208 209 210 211 212
    if (priv->gsettings) {
        g_object_unref (priv->gsettings);
        priv->gsettings = NULL;
    }

213 214 215 216 217 218
    G_OBJECT_CLASS (yelp_application_parent_class)->dispose (object);
}

static void
yelp_application_finalize (GObject *object)
{
219 220 221
    YelpApplicationPrivate *priv = GET_PRIV (object);

    g_hash_table_destroy (priv->windows_by_document);
222
    g_hash_table_destroy (priv->docsettings);
223

224 225 226 227
    G_OBJECT_CLASS (yelp_application_parent_class)->finalize (object);
}


228 229 230 231 232
static gboolean
yelp_application_cmdline (GApplication     *app,
                          gchar          ***arguments,
                          gint             *exit_status)
{
233 234 235
    GOptionContext *context;
    gint argc = g_strv_length (*arguments);
    gint i;
236

237
    context = g_option_context_new (NULL);
238
    g_option_context_add_group (context, gtk_get_option_group (FALSE));
239 240 241 242 243 244 245 246 247 248 249
    g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
    g_option_context_parse (context, &argc, arguments, NULL);

    for (i = 1; i < argc; i++) {
        if (!strchr ((*arguments)[i], ':') && !((*arguments)[i][0] == '/')) {
            GFile *base, *new;
            gchar *cur, *newuri;
            cur = g_get_current_dir ();
            base = g_file_new_for_path (cur);
            new = g_file_resolve_relative_path (base, (*arguments)[i]);
            newuri = g_file_get_uri (new);
250
            g_free ((*arguments)[i]);
251 252 253 254
            (*arguments)[i] = newuri;
            g_free (cur);
            g_object_unref (new);
            g_object_unref (base);
255 256 257 258 259 260 261
        }
    }

    return G_APPLICATION_CLASS (yelp_application_parent_class)
        ->local_command_line (app, arguments, exit_status);
}

262
static void
Allison Karlitskaya's avatar
Allison Karlitskaya committed
263
yelp_application_startup (GApplication *application)
264
{
Allison Karlitskaya's avatar
Allison Karlitskaya committed
265
    YelpApplication *app = YELP_APPLICATION (application);
266
    YelpApplicationPrivate *priv = GET_PRIV (app);
267
    gchar *keyfile;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
268
    YelpSettings *settings;
269

270
    g_set_application_name (_("Help"));
Allison Karlitskaya's avatar
Allison Karlitskaya committed
271 272

    /* chain up */
273
    G_APPLICATION_CLASS (yelp_application_parent_class)->startup (application);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
274 275

    settings = yelp_settings_get_default ();
276
    if (editor_mode)
277
        yelp_settings_set_editor_mode (settings, TRUE);
278 279 280 281
    priv->windows_by_document = g_hash_table_new_full (g_str_hash,
                                                       g_str_equal,
                                                       g_free,
                                                       NULL);
282 283
    /* Use a config file for per-document settings, because
       Ryan asked me to. */
284
    keyfile = g_build_filename (g_get_user_config_dir (), "yelp", "yelp.cfg", NULL);
285 286
    priv->backend = g_keyfile_settings_backend_new (keyfile, "/org/gnome/yelp/", "yelp");
    g_free (keyfile);
287

288 289 290
    /* But the main settings are in dconf */
    priv->gsettings = g_settings_new ("org.gnome.yelp");

291 292 293
    g_settings_bind (priv->gsettings, "show-cursor",
                     settings, "show-text-cursor",
                     G_SETTINGS_BIND_DEFAULT);
294 295 296 297
    priv->show_cursor_action = g_property_action_new ("yelp-application-show-cursor",
                                                      settings, "show-text-cursor");
    g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (priv->show_cursor_action));

298 299 300 301
    g_settings_bind (priv->gsettings, "font-adjustment",
                     settings, "font-adjustment",
                     G_SETTINGS_BIND_DEFAULT);

302 303 304 305 306 307 308 309 310 311 312 313 314
    priv->larger_text_action = g_simple_action_new ("yelp-application-larger-text", NULL);
    g_signal_connect (priv->larger_text_action,
                      "activate",
                      G_CALLBACK (application_adjust_font),
                      app);
    g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (priv->larger_text_action));

    priv->smaller_text_action = g_simple_action_new ("yelp-application-smaller-text", NULL);
    g_signal_connect (priv->smaller_text_action,
                      "activate",
                      G_CALLBACK (application_adjust_font),
                      app);
    g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (priv->smaller_text_action));
315

316 317 318
    application_set_font_sensitivity (app);
}

319 320
/******************************************************************************/

321
static void
322 323
application_adjust_font (GAction         *action,
                         GVariant        *parameter,
324 325 326 327
                         YelpApplication *app)
{
    YelpApplicationPrivate *priv = GET_PRIV (app);
    gint adjustment = g_settings_get_int (priv->gsettings, "font-adjustment");
328
    gint adjust = g_str_equal (g_action_get_name (action), "yelp-application-larger-text") ? 1 : -1;
329 330

    adjustment += adjust;
331
    g_settings_set_int (priv->gsettings, "font-adjustment", adjustment);
332

333 334
    application_set_font_sensitivity (app);
}
335

336 337 338 339 340 341 342 343 344 345 346
static void
application_set_font_sensitivity (YelpApplication *app)
{
    YelpApplicationPrivate *priv = GET_PRIV (app);
    YelpSettings *settings = yelp_settings_get_default ();
    GParamSpec *spec = g_object_class_find_property ((GObjectClass *) YELP_SETTINGS_GET_CLASS (settings),
                                                     "font-adjustment");
    gint adjustment = g_settings_get_int (priv->gsettings, "font-adjustment");
    if (!G_PARAM_SPEC_INT (spec)) {
        g_warning ("Expcected integer param spec for font-adjustment");
        return;
347
    }
348 349 350 351
    g_simple_action_set_enabled (priv->larger_text_action, 
                                 adjustment < ((GParamSpecInt *) spec)->maximum);
    g_simple_action_set_enabled (priv->smaller_text_action, 
                                 adjustment > ((GParamSpecInt *) spec)->minimum);
352 353
}

354 355 356 357 358 359
/******************************************************************************/

YelpApplication *
yelp_application_new (void)
{
    YelpApplication *app;
Matthias Clasen's avatar
Matthias Clasen committed
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
    char *app_id = NULL;
    char *yelp = NULL;

    if (g_file_test ("/.flatpak-info", G_FILE_TEST_EXISTS)) {
        GKeyFile *kf = g_key_file_new ();
        g_key_file_load_from_file (kf, "/.flatpak-info", G_KEY_FILE_NONE, NULL);
        if (g_key_file_has_group (kf, "Application"))
          app_id = g_key_file_get_string (kf, "Application", "name", NULL);
        else
          app_id = g_key_file_get_string (kf, "Runtime", "name", NULL);
        yelp = g_strconcat (app_id, ".Help", NULL);
        g_key_file_unref (kf);
    }
    else {
        yelp = g_strdup ("org.gnome.Yelp");
    }
376

Allison Karlitskaya's avatar
Allison Karlitskaya committed
377
    app = g_object_new (YELP_TYPE_APPLICATION,
Matthias Clasen's avatar
Matthias Clasen committed
378
                        "application-id", yelp,
379
                        "flags", G_APPLICATION_HANDLES_COMMAND_LINE,
Allison Karlitskaya's avatar
Allison Karlitskaya committed
380 381
                        "inactivity-timeout", 5000,
                        NULL);
Matthias Clasen's avatar
Matthias Clasen committed
382 383
    g_free (app_id);
    g_free (yelp);
384 385 386 387

    return app;
}

388
/* consumes the uri */
Allison Karlitskaya's avatar
Allison Karlitskaya committed
389 390 391
static void
open_uri (YelpApplication *app,
          YelpUri         *uri,
392 393
          gboolean         new_window,
          gboolean         fallback_help_list)
394
{
Allison Karlitskaya's avatar
Allison Karlitskaya committed
395 396 397 398
    YelpApplicationLoad *data;
    data = g_new (YelpApplicationLoad, 1);
    data->app = app;
    data->timestamp = gtk_get_current_event_time ();
399
    data->new = new_window;
400
    data->fallback_help_list = fallback_help_list;
401

Allison Karlitskaya's avatar
Allison Karlitskaya committed
402 403 404
    g_signal_connect (uri, "resolved",
                      G_CALLBACK (application_uri_resolved),
                      data);
405

Allison Karlitskaya's avatar
Allison Karlitskaya committed
406 407 408 409
    /* hold the app while resolving the uri so we don't exit while
     * in the middle of the load
     */
    g_application_hold (G_APPLICATION (app));
410

Allison Karlitskaya's avatar
Allison Karlitskaya committed
411 412
    yelp_uri_resolve (uri);
}
413 414


415 416 417
static int
yelp_application_command_line (GApplication            *application,
                               GApplicationCommandLine *cmdline)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
418 419
{
    YelpApplication *app = YELP_APPLICATION (application);
420 421
    gchar **argv;
    int i;
422

423
    argv = g_application_command_line_get_arguments (cmdline, NULL);
424

425
    if (argv[1] == NULL)
426
        open_uri (app, yelp_uri_new (DEFAULT_URI), FALSE, TRUE);
427

428
    for (i = 1; argv[i]; i++)
429
        open_uri (app, yelp_uri_new (argv[i]), FALSE, FALSE);
430 431

    g_strfreev (argv);
432

433
    return 0;
434 435
}

436 437 438 439
void
yelp_application_new_window (YelpApplication  *app,
                             const gchar      *uri)
{
440 441 442 443
    if (uri)
        open_uri (app, yelp_uri_new (uri), TRUE, FALSE);
    else
        open_uri (app, yelp_uri_new (DEFAULT_URI), TRUE, TRUE);
444 445 446 447 448 449
}

void
yelp_application_new_window_uri (YelpApplication  *app,
                                 YelpUri          *uri)
{
450
    open_uri (app, g_object_ref (uri), TRUE, FALSE);
451 452
}

453 454 455 456
static void
application_uri_resolved (YelpUri             *uri,
                          YelpApplicationLoad *data)
{
457
    YelpWindow *window;
458
    gchar *doc_uri;
459
    GdkWindow *gdk_window;
460
    YelpApplicationPrivate *priv = GET_PRIV (data->app);
461
    GFile *gfile;
462

Allison Karlitskaya's avatar
Allison Karlitskaya committed
463 464 465
    /* We held the application while resolving the URI, so unhold now. */
    g_application_release (G_APPLICATION (data->app));

466 467 468 469 470 471 472 473 474 475 476 477
    /* Get the GFile associated with the URI, or NULL if not available */
    gfile = yelp_uri_get_file (uri);
    if (gfile == NULL && data->fallback_help_list) {
        /* There is no file associated to the default uri, so we'll fallback
         * to help-list: if we're told to do so. */
        open_uri (data->app, yelp_uri_new ("help-list:"), data->new, FALSE);
        g_object_unref (uri);
        g_free (data);
        return;
    }
    g_clear_object (&gfile);

478 479
    doc_uri = yelp_uri_get_document_uri (uri);

480
    if (data->new || !doc_uri)
481 482 483
        window = NULL;
    else
        window = g_hash_table_lookup (priv->windows_by_document, doc_uri);
484 485

    if (window == NULL) {
486
        gint width, height;
Shaun McCance's avatar
Shaun McCance committed
487
        GSettings *settings = application_get_doc_settings (data->app, doc_uri);
488 489

        g_settings_get (settings, "geometry", "(ii)", &width, &height);
490
        window = yelp_window_new (data->app);
491
        gtk_window_set_default_size (GTK_WINDOW (window), width, height);
Shaun McCance's avatar
Shaun McCance committed
492
        g_signal_connect (window, "resized", G_CALLBACK (window_resized), data->app);
493
        priv->windows = g_slist_prepend (priv->windows, window);
494

495 496 497 498
        if (!data->new) {
            g_hash_table_insert (priv->windows_by_document, doc_uri, window);
            g_object_set_data (G_OBJECT (window), "doc_uri", doc_uri);
        }
499 500 501
        else {
            g_free (doc_uri);
        }
Allison Karlitskaya's avatar
Allison Karlitskaya committed
502

503 504
        g_signal_connect (window, "delete-event",
                          G_CALLBACK (application_window_deleted), data->app);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
505 506
        gtk_window_set_application (GTK_WINDOW (window),
                                    GTK_APPLICATION (data->app));
507 508 509 510 511
    }
    else {
        g_free (doc_uri);
    }

512
    yelp_window_load_uri (window, uri);
513

514
    gtk_widget_show_all (GTK_WIDGET (window));
515 516 517 518

    /* Metacity no longer does anything useful with gtk_window_present */
    gdk_window = gtk_widget_get_window (GTK_WIDGET (window));

519 520 521 522 523 524 525 526 527 528 529 530
#ifdef GDK_WINDOWING_X11
    if (GDK_IS_X11_WINDOW (gdk_window)){
        if (gdk_window)
            gdk_x11_window_move_to_current_desktop (gdk_window);

        /* Ensure we actually present the window when invoked from the command
         * line. This is somewhat evil, but the minor evil of Yelp stealing
         * focus (after you requested it) is outweighed for me by the major
         * evil of no help window appearing when you click Help.
         */
        if (data->timestamp == 0)
            data->timestamp = gdk_x11_get_server_time (gtk_widget_get_window (GTK_WIDGET (window)));
531

532 533 534 535 536
        gtk_window_present_with_time (GTK_WINDOW (window), data->timestamp);
    }
    else
#endif
        gtk_window_present (GTK_WINDOW (window));
537

Allison Karlitskaya's avatar
Allison Karlitskaya committed
538
    g_object_unref (uri);
539 540
    g_free (data);
}
541

542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
static gboolean
application_window_deleted (YelpWindow      *window,
                            GdkEvent        *event,
                            YelpApplication *app)
{
    gchar *doc_uri; /* owned by windows_by_document */
    YelpApplicationPrivate *priv = GET_PRIV (app);

    priv->windows = g_slist_remove (priv->windows, window);
    doc_uri = g_object_get_data (G_OBJECT (window), "doc_uri");
    if (doc_uri)
        g_hash_table_remove (priv->windows_by_document, doc_uri);

    return FALSE;
}

Shaun McCance's avatar
Shaun McCance committed
558 559 560 561 562 563
GSettings *
application_get_doc_settings (YelpApplication *app, const gchar *doc_uri)
{
    YelpApplicationPrivate *priv = GET_PRIV (app);
    GSettings *settings = g_hash_table_lookup (priv->docsettings, doc_uri);
    if (settings == NULL) {
564
        gchar *tmp, *key, *settings_path;
Shaun McCance's avatar
Shaun McCance committed
565
        tmp = g_uri_escape_string (doc_uri, "", FALSE);
566
        settings_path = g_strconcat ("/org/gnome/yelp/documents/", tmp, "/", NULL);
Shaun McCance's avatar
Shaun McCance committed
567
        g_free (tmp);
568 569 570
        if (priv->backend)
            settings = g_settings_new_with_backend_and_path ("org.gnome.yelp.documents",
                                                             priv->backend,
Shaun McCance's avatar
Shaun McCance committed
571 572
                                                             settings_path);
        else
573
            settings = g_settings_new_with_path ("org.gnome.yelp.documents",
Shaun McCance's avatar
Shaun McCance committed
574
                                                 settings_path);
575 576 577 578 579
        key = g_strdup (doc_uri);
        g_hash_table_insert (priv->docsettings, key, settings);
        g_object_set_data ((GObject *) settings, "doc_uri", key);
        g_signal_connect (settings, "changed::bookmarks",
                          G_CALLBACK (bookmarks_changed), app);
Shaun McCance's avatar
Shaun McCance committed
580 581 582 583 584
        g_free (settings_path);
    }
    return settings;
}

585 586
/******************************************************************************/

Shaun McCance's avatar
Shaun McCance committed
587
void
588
yelp_application_add_bookmark (YelpBookmarks     *bookmarks,
589 590
                               const gchar       *doc_uri,
                               const gchar       *page_id,
Shaun McCance's avatar
Shaun McCance committed
591 592 593 594
                               const gchar       *icon,
                               const gchar       *title)
{
    GSettings *settings;
595
    YelpApplication *app = YELP_APPLICATION (bookmarks);
Shaun McCance's avatar
Shaun McCance committed
596

597 598 599
    g_return_if_fail (page_id);
    g_return_if_fail (doc_uri);

Shaun McCance's avatar
Shaun McCance committed
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
    settings = application_get_doc_settings (app, doc_uri);

    if (settings) {
        GVariantBuilder builder;
        GVariantIter *iter;
        gchar *this_id, *this_icon, *this_title;
        gboolean broken = FALSE;
        g_settings_get (settings, "bookmarks", "a(sss)", &iter);
        g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(sss)"));
        while (g_variant_iter_loop (iter, "(&s&s&s)", &this_id, &this_icon, &this_title)) {
            if (g_str_equal (page_id, this_id)) {
                /* Already have this bookmark */
                broken = TRUE;
                break;
            }
            g_variant_builder_add (&builder, "(sss)", this_id, this_icon, this_title);
        }
        g_variant_iter_free (iter);

        if (!broken) {
            GVariant *value;
            g_variant_builder_add (&builder, "(sss)", page_id, icon, title);
            value = g_variant_builder_end (&builder);
            g_settings_set_value (settings, "bookmarks", value);
624 625 626 627
        }
    }
}

628
void
629
yelp_application_remove_bookmark (YelpBookmarks     *bookmarks,
630 631 632 633
                                  const gchar       *doc_uri,
                                  const gchar       *page_id)
{
    GSettings *settings;
634
    YelpApplication *app = YELP_APPLICATION (bookmarks);
635

636 637 638
    g_return_if_fail (page_id);
    g_return_if_fail (doc_uri);

639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
    settings = application_get_doc_settings (app, doc_uri);

    if (settings) {
        GVariantBuilder builder;
        GVariantIter *iter;
        gchar *this_id, *this_icon, *this_title;
        g_settings_get (settings, "bookmarks", "a(sss)", &iter);
        g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(sss)"));
        while (g_variant_iter_loop (iter, "(&s&s&s)", &this_id, &this_icon, &this_title)) {
            if (!g_str_equal (page_id, this_id))
                g_variant_builder_add (&builder, "(sss)", this_id, this_icon, this_title);
        }
        g_variant_iter_free (iter);

        g_settings_set_value (settings, "bookmarks", g_variant_builder_end (&builder));
    }
}

657
gboolean
658
yelp_application_is_bookmarked (YelpBookmarks     *bookmarks,
659 660 661
                                const gchar       *doc_uri,
                                const gchar       *page_id)
{
662
    GVariant *stored_bookmarks;
663 664 665 666
    GVariantIter *iter;
    gboolean ret = FALSE;
    gchar *this_id = NULL;
    GSettings *settings;
667
    YelpApplication *app = YELP_APPLICATION (bookmarks);
668

669 670
    g_return_val_if_fail (page_id, FALSE);
    g_return_val_if_fail (doc_uri, FALSE);
671

672 673 674 675
    settings = application_get_doc_settings (app, doc_uri);
    if (settings == NULL)
        return FALSE;

676
    stored_bookmarks = g_settings_get_value (settings, "bookmarks");
677 678 679 680 681 682 683 684 685
    g_settings_get (settings, "bookmarks", "a(sss)", &iter);
    while (g_variant_iter_loop (iter, "(&s&s&s)", &this_id, NULL, NULL)) {
        if (g_str_equal (page_id, this_id)) {
            ret = TRUE;
            break;
        }
    }

    g_variant_iter_free (iter);
686
    g_variant_unref (stored_bookmarks);
687 688 689
    return ret;
}

690 691 692 693 694 695 696 697 698
void
yelp_application_update_bookmarks (YelpApplication   *app,
                                   const gchar       *doc_uri,
                                   const gchar       *page_id,
                                   const gchar       *icon,
                                   const gchar       *title)
{
    GSettings *settings;

699 700 701
    g_return_if_fail (page_id);
    g_return_if_fail (doc_uri);

702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
    settings = application_get_doc_settings (app, doc_uri);

    if (settings) {
        GVariantBuilder builder;
        GVariantIter *iter;
        gchar *this_id, *this_icon, *this_title;
        gboolean updated = FALSE;
        g_settings_get (settings, "bookmarks", "a(sss)", &iter);
        g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(sss)"));
        while (g_variant_iter_loop (iter, "(&s&s&s)", &this_id, &this_icon, &this_title)) {
            if (g_str_equal (page_id, this_id)) {
                if (icon && !g_str_equal (icon, this_icon)) {
                    this_icon = (gchar *) icon;
                    updated = TRUE;
                }
                if (title && !g_str_equal (title, this_title)) {
                    this_title = (gchar *) title;
                    updated = TRUE;
                }
                if (!updated)
                    break;
            }
            g_variant_builder_add (&builder, "(sss)", this_id, this_icon, this_title);
        }
        g_variant_iter_free (iter);

728
        if (updated)
729 730
            g_settings_set_value (settings, "bookmarks",
                                  g_variant_builder_end (&builder));
731 732
        else
            g_variant_builder_clear (&builder);
Shaun McCance's avatar
Shaun McCance committed
733 734 735 736 737 738 739 740 741 742 743 744
    }
}

GVariant *
yelp_application_get_bookmarks (YelpApplication *app,
                                const gchar     *doc_uri)
{
    GSettings *settings = application_get_doc_settings (app, doc_uri);

    return g_settings_get_value (settings, "bookmarks");
}

745 746 747 748 749 750 751 752 753 754
static void
bookmarks_changed (GSettings       *settings,
                   const gchar     *key,
                   YelpApplication *app)
{
    const gchar *doc_uri = g_object_get_data ((GObject *) settings, "doc_uri");
    if (doc_uri)
        g_signal_emit_by_name (app, "bookmarks-changed", doc_uri);
}

755 756 757 758 759 760 761 762 763 764
static gboolean
window_resized (YelpWindow        *window,
                YelpApplication   *app)
{
    YelpApplicationPrivate *priv = GET_PRIV (app);
    YelpUri *uri;
    gchar *doc_uri;
    GSettings *settings;

    uri = yelp_window_get_uri (window);
765 766
    if (uri == NULL)
        return FALSE;
767
    doc_uri = yelp_uri_get_document_uri (uri);
768 769 770 771
    if (doc_uri == NULL) {
        g_object_unref (uri);
        return FALSE;
    }
772 773 774 775 776 777 778 779 780 781 782 783 784
    settings = g_hash_table_lookup (priv->docsettings, doc_uri);

    if (settings) {
        gint width, height;
        yelp_window_get_geometry (window, &width, &height);
        g_settings_set (settings, "geometry", "(ii)", width, height);
    }

    g_free (doc_uri);
    g_object_unref (uri);

    return FALSE;
}