gimphelp.c 28.3 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3 4
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimphelp.c
5 6
 * Copyright (C) 1999-2004 Michael Natterer <mitch@gimp.org>
 *                         Henrik Brix Andersen <brix@gimp.org>
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 3 of the License, or
11 12 13 14 15 16 17 18
 * (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
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21

22 23
#include "config.h"

24
#include <string.h>
25

26
#include <gegl.h>
27 28
#include <gtk/gtk.h>

29
#include "libgimpbase/gimpbase.h"
30 31
#include "libgimpwidgets/gimpwidgets.h"

Michael Natterer's avatar
Michael Natterer committed
32
#include "widgets-types.h"
33

34 35
#include "config/gimpguiconfig.h"

36
#include "core/gimp.h"
37
#include "core/gimpparamspecs.h"
38 39
#include "core/gimpprogress.h"
#include "core/gimp-utils.h"
40

41
#include "pdb/gimppdb.h"
42
#include "pdb/gimpprocedure.h"
43

44
#include "plug-in/gimpplugin.h"
45
#include "plug-in/gimppluginmanager-help-domain.h"
46
#include "plug-in/gimptemporaryprocedure.h"
Michael Natterer's avatar
Michael Natterer committed
47

48
#include "gimphelp.h"
49
#include "gimphelp-ids.h"
50
#include "gimplanguagecombobox.h"
Jehan's avatar
Jehan committed
51
#include "gimplanguagestore-parser.h"
52
#include "gimpmessagebox.h"
53
#include "gimpmessagedialog.h"
54
#include "gimpmessagedialog.h"
55
#include "gimpwidgets-utils.h"
Michael Natterer's avatar
Michael Natterer committed
56

Michael Natterer's avatar
Michael Natterer committed
57
#include "gimp-log.h"
58
#include "gimp-intl.h"
59

60 61 62 63 64

typedef struct _GimpIdleHelp GimpIdleHelp;

struct _GimpIdleHelp
{
65 66 67 68 69
  Gimp         *gimp;
  GimpProgress *progress;
  gchar        *help_domain;
  gchar        *help_locales;
  gchar        *help_id;
Jehan's avatar
Jehan committed
70 71

  GtkDialog    *query_dialog;
72 73
};

Michael Natterer's avatar
Michael Natterer committed
74

75
/*  local function prototypes  */
Michael Natterer's avatar
Michael Natterer committed
76

77 78
static gboolean   gimp_idle_help          (GimpIdleHelp  *idle_help);
static void       gimp_idle_help_free     (GimpIdleHelp  *idle_help);
79

80
static gboolean   gimp_help_browser       (Gimp          *gimp,
81
                                           GimpProgress  *progress);
82
static void       gimp_help_browser_error (Gimp          *gimp,
83
                                           GimpProgress  *progress,
84 85 86 87 88
                                           const gchar   *title,
                                           const gchar   *primary,
                                           const gchar   *text);

static void       gimp_help_call          (Gimp          *gimp,
89
                                           GimpProgress  *progress,
90 91 92 93
                                           const gchar   *procedure_name,
                                           const gchar   *help_domain,
                                           const gchar   *help_locales,
                                           const gchar   *help_id);
94

95 96 97 98 99 100
static gint       gimp_help_get_help_domains         (Gimp    *gimp,
                                                      gchar ***domain_names,
                                                      gchar ***domain_uris);
static gchar    * gimp_help_get_default_domain_uri   (Gimp    *gimp);
static gchar    * gimp_help_get_locales              (Gimp    *gimp);

101
static GFile    * gimp_help_get_user_manual_basedir  (void);
102

Jehan's avatar
Jehan committed
103
static void       gimp_help_query_alt_user_manual    (GimpIdleHelp *idle_help);
104

Jehan's avatar
Jehan committed
105 106
static void       gimp_help_language_combo_changed   (GtkComboBox  *combo,
                                                      GimpIdleHelp *idle_help);
Michael Natterer's avatar
Michael Natterer committed
107

108
/*  public functions  */
109

110
void
111 112 113 114
gimp_help_show (Gimp         *gimp,
                GimpProgress *progress,
                const gchar  *help_domain,
                const gchar  *help_id)
115
{
116 117
  GimpGuiConfig *config;

118
  g_return_if_fail (GIMP_IS_GIMP (gimp));
119
  g_return_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress));
120

121 122 123
  config = GIMP_GUI_CONFIG (gimp->config);

  if (config->use_help)
124
    {
125
      GimpIdleHelp *idle_help = g_slice_new0 (GimpIdleHelp);
126

127 128
      idle_help->gimp     = gimp;
      idle_help->progress = progress;
129

130
      if (help_domain && strlen (help_domain))
131
        idle_help->help_domain = g_strdup (help_domain);
132

133
      idle_help->help_locales = gimp_help_get_locales (gimp);
134

135
      if (help_id && strlen (help_id))
136
        idle_help->help_id = g_strdup (help_id);
137

138 139 140
      GIMP_LOG (HELP, "request for help-id '%s' from help-domain '%s'",
                help_id     ? help_id      : "(null)",
                help_domain ? help_domain  : "(null)");
141

142
      g_idle_add ((GSourceFunc) gimp_idle_help, idle_help);
143
    }
144 145
}

146 147 148 149 150 151 152 153 154 155 156
gboolean
gimp_help_browser_is_installed (Gimp *gimp)
{
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), FALSE);

  if (gimp_pdb_lookup_procedure (gimp->pdb, "extension-gimp-help-browser"))
    return TRUE;

  return FALSE;
}

157 158
gboolean
gimp_help_user_manual_is_installed (Gimp *gimp)
159
{
160
  GFile    *basedir;
161 162 163 164
  gboolean  found = FALSE;

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), FALSE);

165 166 167 168
  /*  if GIMP2_HELP_URI is set, assume that the manual can be found there  */
  if (g_getenv ("GIMP2_HELP_URI"))
    return TRUE;

169 170
  basedir = gimp_help_get_user_manual_basedir ();

171 172
  if (g_file_query_file_type (basedir, G_FILE_QUERY_INFO_NONE, NULL) ==
      G_FILE_TYPE_DIRECTORY)
173 174 175 176 177 178 179 180
    {
      gchar       *locales = gimp_help_get_locales (gimp);
      const gchar *s       = locales;
      const gchar *p;

      for (p = strchr (s, ':'); p && !found; p = strchr (s, ':'))
        {
          gchar *locale = g_strndup (s, p - s);
181 182
          GFile *file1  = g_file_get_child (basedir, locale);
          GFile *file2  = g_file_get_child (file1, "gimp-help.xml");
183

184 185
          found = (g_file_query_file_type (file2, G_FILE_QUERY_INFO_NONE,
                                           NULL) == G_FILE_TYPE_REGULAR);
186

187 188
          g_object_unref (file1);
          g_object_unref (file2);
189 190 191 192 193 194
          g_free (locale);

          s = p + 1;
        }

      g_free (locales);
195 196 197

      if (! found)
        {
198 199
          GFile *file1  = g_file_get_child (basedir, "en");
          GFile *file2  = g_file_get_child (file1, "gimp-help.xml");
200

201 202
          found = (g_file_query_file_type (file2, G_FILE_QUERY_INFO_NONE,
                                           NULL) == G_FILE_TYPE_REGULAR);
203

204 205
          g_object_unref (file1);
          g_object_unref (file2);
206
        }
207 208
    }

209
  g_object_unref (basedir);
210 211

  return found;
212 213
}

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
void
gimp_help_user_manual_changed (Gimp *gimp)
{
  GimpProcedure *procedure;

  g_return_if_fail (GIMP_IS_GIMP (gimp));

  /*  Check if a help parser is running  */
  procedure = gimp_pdb_lookup_procedure (gimp->pdb, "extension-gimp-help-temp");

  if (GIMP_IS_TEMPORARY_PROCEDURE (procedure))
    {
      gimp_plug_in_close (GIMP_TEMPORARY_PROCEDURE (procedure)->plug_in, TRUE);
    }
}

230 231 232 233 234 235 236 237 238 239 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
GList *
gimp_help_get_installed_languages (void)
{
  GList *manuals = NULL;
  GFile *basedir;

  /*  if GIMP2_HELP_URI is set, assume that the manual can be found there  */
  if (g_getenv ("GIMP2_HELP_URI"))
    basedir = g_file_new_for_uri (g_getenv ("GIMP2_HELP_URI"));
  else
    basedir = gimp_help_get_user_manual_basedir ();

  if (g_file_query_file_type (basedir, G_FILE_QUERY_INFO_NONE, NULL) ==
      G_FILE_TYPE_DIRECTORY)
    {
      GFileEnumerator *enumerator;

      enumerator = g_file_enumerate_children (basedir,
                                              G_FILE_ATTRIBUTE_STANDARD_NAME ","
                                              G_FILE_ATTRIBUTE_STANDARD_TYPE,
                                              G_FILE_QUERY_INFO_NONE,
                                              NULL, NULL);

      if (enumerator)
        {
          GFileInfo *info;

          while ((info = g_file_enumerator_next_file (enumerator,
                                                      NULL, NULL)))
            {
              if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
                {
                  GFile *locale_dir;
                  GFile *file;

                  locale_dir = g_file_enumerator_get_child (enumerator, info);
                  file  = g_file_get_child (locale_dir, "gimp-help.xml");
                  if (g_file_query_file_type (file, G_FILE_QUERY_INFO_NONE,
                                              NULL) == G_FILE_TYPE_REGULAR)
                    {
                      manuals = g_list_prepend (manuals,
                                                g_strdup (g_file_info_get_name (info)));
                    }
                  g_object_unref (locale_dir);
                  g_object_unref (file);
                }
              g_object_unref (info);
            }
          g_object_unref (enumerator);
        }
    }
  g_object_unref (basedir);

  return manuals;
}
Michael Natterer's avatar
Michael Natterer committed
285 286

/*  private functions  */
287

288
static gboolean
289
gimp_idle_help (GimpIdleHelp *idle_help)
290
{
291 292
  GimpGuiConfig *config         = GIMP_GUI_CONFIG (idle_help->gimp->config);
  const gchar   *procedure_name = NULL;
293

294 295 296 297
  if (! idle_help->help_domain       &&
      ! config->user_manual_online   &&
      ! gimp_help_user_manual_is_installed (idle_help->gimp))
    {
Jehan's avatar
Jehan committed
298 299
      /*  The user manual is not installed locally, propose alternative
       *  manuals (other installed languages or online version).
300
       */
Jehan's avatar
Jehan committed
301
      gimp_help_query_alt_user_manual (idle_help);
302 303 304 305

      return FALSE;
    }

306
  if (config->help_browser == GIMP_HELP_BROWSER_GIMP)
307
    {
308
      if (gimp_help_browser (idle_help->gimp, idle_help->progress))
309
        procedure_name = "extension-gimp-help-browser-temp";
310
    }
311

312 313
  if (config->help_browser == GIMP_HELP_BROWSER_WEB_BROWSER)
    {
314
      /*  FIXME: should check for procedure availability  */
315
      procedure_name = "plug-in-web-browser";
316 317
    }

318
  if (procedure_name)
319
    gimp_help_call (idle_help->gimp,
320
                    idle_help->progress,
321
                    procedure_name,
322 323 324
                    idle_help->help_domain,
                    idle_help->help_locales,
                    idle_help->help_id);
325

326 327 328 329 330 331 332 333
  gimp_idle_help_free (idle_help);

  return FALSE;
}

static void
gimp_idle_help_free (GimpIdleHelp *idle_help)
{
334
  g_free (idle_help->help_domain);
335
  g_free (idle_help->help_locales);
336
  g_free (idle_help->help_id);
337 338

  g_slice_free (GimpIdleHelp, idle_help);
339
}
340

Sven Neumann's avatar
Sven Neumann committed
341
static gboolean
342
gimp_help_browser (Gimp         *gimp,
343
                   GimpProgress *progress)
344
{
345
  static gboolean  busy = FALSE;
346
  GimpProcedure   *procedure;
347 348 349 350 351 352

  if (busy)
    return TRUE;

  busy = TRUE;

353
  /*  Check if a help browser is already running  */
354 355
  procedure = gimp_pdb_lookup_procedure (gimp->pdb,
                                         "extension-gimp-help-browser-temp");
356

357
  if (! procedure)
358
    {
359 360 361 362 363
      GimpValueArray *args         = NULL;
      gint            n_domains    = 0;
      gchar         **help_domains = NULL;
      gchar         **help_uris    = NULL;
      GError         *error        = NULL;
364

365 366
      procedure = gimp_pdb_lookup_procedure (gimp->pdb,
                                             "extension-gimp-help-browser");
367

368
      if (! procedure)
369
        {
370 371 372
          gimp_help_browser_error (gimp, progress,
                                   _("Help browser is missing"),
                                   _("The GIMP help browser is not available."),
373
                                   _("The GIMP help browser plug-in appears "
374
                                     "to be missing from your installation. "
375 376
                                     "You may instead use the web browser "
                                     "for reading the help pages."));
377 378
          busy = FALSE;

379 380
          return FALSE;
        }
381

382
      n_domains = gimp_help_get_help_domains (gimp, &help_domains, &help_uris);
383

384
      args = gimp_procedure_get_arguments (procedure);
385
      gimp_value_array_truncate (args, 5);
386

387 388 389 390 391 392 393 394 395 396
      g_value_set_int             (gimp_value_array_index (args, 0),
                                   GIMP_RUN_INTERACTIVE);
      g_value_set_int             (gimp_value_array_index (args, 1),
                                   n_domains);
      gimp_value_take_stringarray (gimp_value_array_index (args, 2),
                                   help_domains, n_domains);
      g_value_set_int             (gimp_value_array_index (args, 3),
                                   n_domains);
      gimp_value_take_stringarray (gimp_value_array_index (args, 4),
                                   help_uris, n_domains);
397

398 399
      gimp_procedure_execute_async (procedure, gimp,
                                    gimp_get_user_context (gimp),
400
                                    NULL, args, NULL, &error);
401

402
      gimp_value_array_unref (args);
403 404 405

      if (error)
        {
406
          gimp_message_literal (gimp, G_OBJECT (progress), GIMP_MESSAGE_ERROR,
407
                                error->message);
408 409 410
          g_error_free (error);
        }
     }
411

412
  /*  Check if the help browser started properly  */
413 414
  procedure = gimp_pdb_lookup_procedure (gimp->pdb,
                                         "extension-gimp-help-browser-temp");
415

416
  if (! procedure)
417
    {
418
      gimp_help_browser_error (gimp, progress,
419
                               _("Help browser doesn't start"),
420 421
                               _("Could not start the GIMP help browser "
                                 "plug-in."),
422 423
                               _("You may instead use the web browser "
                                 "for reading the help pages."));
424 425
      busy = FALSE;

426
      return FALSE;
427
    }
Sven Neumann's avatar
Sven Neumann committed
428

429 430
  busy = FALSE;

Sven Neumann's avatar
Sven Neumann committed
431
  return TRUE;
432 433
}

434
static void
435
gimp_help_browser_error (Gimp         *gimp,
436
                         GimpProgress *progress,
437 438 439
                         const gchar  *title,
                         const gchar  *primary,
                         const gchar  *text)
440 441 442
{
  GtkWidget *dialog;

443
  dialog = gimp_message_dialog_new (title, GIMP_ICON_HELP_USER_MANUAL,
444 445
                                    NULL, 0,
                                    NULL, NULL,
446

447
                                    _("_Cancel"),          GTK_RESPONSE_CANCEL,
448
                                    _("Use _Web Browser"), GTK_RESPONSE_OK,
449

450
                                    NULL);
451

452
  gimp_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
453 454 455 456
                                           GTK_RESPONSE_OK,
                                           GTK_RESPONSE_CANCEL,
                                           -1);

457 458
  if (progress)
    {
459
      guint32 window_id = gimp_progress_get_window_id (progress);
460

461 462
      if (window_id)
        gimp_window_set_transient_for (GTK_WINDOW (dialog), window_id);
463 464
    }

465
  gimp_message_box_set_primary_text (GIMP_MESSAGE_DIALOG (dialog)->box,
466 467
                                     "%s", primary);
  gimp_message_box_set_text (GIMP_MESSAGE_DIALOG (dialog)->box, "%s", text);
468 469

  if (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK)
470 471 472 473 474
    {
      g_object_set (gimp->config,
                    "help-browser", GIMP_HELP_BROWSER_WEB_BROWSER,
                    NULL);
    }
475 476 477 478

  gtk_widget_destroy (dialog);
}

479
static void
480 481 482 483 484 485
gimp_help_call (Gimp         *gimp,
                GimpProgress *progress,
                const gchar  *procedure_name,
                const gchar  *help_domain,
                const gchar  *help_locales,
                const gchar  *help_id)
486
{
487
  GimpProcedure *procedure;
488

489 490 491
  /*  Special case the help browser  */
  if (! strcmp (procedure_name, "extension-gimp-help-browser-temp"))
    {
492 493
      GimpValueArray *return_vals;
      GError         *error = NULL;
494

Michael Natterer's avatar
Michael Natterer committed
495 496 497 498 499 500
      GIMP_LOG (HELP, "Calling help via %s: %s %s %s",
                procedure_name,
                help_domain  ? help_domain  : "(null)",
                help_locales ? help_locales : "(null)",
                help_id      ? help_id      : "(null)");

501 502 503
      return_vals =
        gimp_pdb_execute_procedure_by_name (gimp->pdb,
                                            gimp_get_user_context (gimp),
504
                                            progress, &error,
505 506 507 508 509
                                            procedure_name,
                                            G_TYPE_STRING, help_domain,
                                            G_TYPE_STRING, help_locales,
                                            G_TYPE_STRING, help_id,
                                            G_TYPE_NONE);
510

511
      gimp_value_array_unref (return_vals);
512

513 514
      if (error)
        {
515
          gimp_message_literal (gimp, NULL, GIMP_MESSAGE_ERROR, error->message);
516 517 518
          g_error_free (error);
        }

519 520 521
      return;
    }

522
  /*  Check if a help parser is already running  */
523
  procedure = gimp_pdb_lookup_procedure (gimp->pdb, "extension-gimp-help-temp");
Sven Neumann's avatar
Sven Neumann committed
524

525
  if (! procedure)
526
    {
527 528 529 530 531
      GimpValueArray  *args         = NULL;
      gint             n_domains    = 0;
      gchar          **help_domains = NULL;
      gchar          **help_uris    = NULL;
      GError          *error        = NULL;
532

533
      procedure = gimp_pdb_lookup_procedure (gimp->pdb, "extension-gimp-help");
534

535
      if (! procedure)
536 537
        /*  FIXME: error msg  */
        return;
538

539
      n_domains = gimp_help_get_help_domains (gimp, &help_domains, &help_uris);
540

541
      args = gimp_procedure_get_arguments (procedure);
542
      gimp_value_array_truncate (args, 4);
543

544 545 546 547 548 549 550 551
      g_value_set_int             (gimp_value_array_index (args, 0),
                                   n_domains);
      gimp_value_take_stringarray (gimp_value_array_index (args, 1),
                                   help_domains, n_domains);
      g_value_set_int             (gimp_value_array_index (args, 2),
                                   n_domains);
      gimp_value_take_stringarray (gimp_value_array_index (args, 3),
                                   help_uris, n_domains);
552

553
      gimp_procedure_execute_async (procedure, gimp,
554 555
                                    gimp_get_user_context (gimp), progress,
                                    args, NULL, &error);
556

557
      gimp_value_array_unref (args);
558 559 560

      if (error)
        {
561
          gimp_message_literal (gimp, NULL, GIMP_MESSAGE_ERROR, error->message);
562 563
          g_error_free (error);
        }
564
    }
565 566

  /*  Check if the help parser started properly  */
567
  procedure = gimp_pdb_lookup_procedure (gimp->pdb, "extension-gimp-help-temp");
568

569
  if (procedure)
570
    {
571 572
      GimpValueArray *return_vals;
      GError         *error = NULL;
573

Michael Natterer's avatar
Michael Natterer committed
574 575 576 577 578
      GIMP_LOG (HELP, "Calling help via %s: %s %s %s",
                procedure_name,
                help_domain  ? help_domain  : "(null)",
                help_locales ? help_locales : "(null)",
                help_id      ? help_id      : "(null)");
579

580 581 582
      return_vals =
        gimp_pdb_execute_procedure_by_name (gimp->pdb,
                                            gimp_get_user_context (gimp),
583
                                            progress, &error,
584 585 586 587 588 589
                                            "extension-gimp-help-temp",
                                            G_TYPE_STRING, procedure_name,
                                            G_TYPE_STRING, help_domain,
                                            G_TYPE_STRING, help_locales,
                                            G_TYPE_STRING, help_id,
                                            G_TYPE_NONE);
590

591
      gimp_value_array_unref (return_vals);
592 593 594

      if (error)
        {
595
          gimp_message_literal (gimp, NULL, GIMP_MESSAGE_ERROR, error->message);
596 597
          g_error_free (error);
        }
598
    }
599
}
600

601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
static gint
gimp_help_get_help_domains (Gimp    *gimp,
                            gchar ***domain_names,
                            gchar ***domain_uris)
{
  gchar **plug_in_domains = NULL;
  gchar **plug_in_uris    = NULL;
  gint    i, n_domains;

  n_domains = gimp_plug_in_manager_get_help_domains (gimp->plug_in_manager,
                                                     &plug_in_domains,
                                                     &plug_in_uris);

  *domain_names = g_new0 (gchar *, n_domains + 1);
  *domain_uris  = g_new0 (gchar *, n_domains + 1);

617
  (*domain_names)[0] = g_strdup ("https://www.gimp.org/help");
618
  (*domain_uris)[0]  = gimp_help_get_default_domain_uri (gimp);
619 620 621

  for (i = 0; i < n_domains; i++)
    {
622 623
      (*domain_names)[i + 1] = plug_in_domains[i];
      (*domain_uris)[i + 1]  = plug_in_uris[i];
624 625 626 627 628 629 630 631 632 633 634 635
    }

  g_free (plug_in_domains);
  g_free (plug_in_uris);

  return n_domains + 1;
}

static gchar *
gimp_help_get_default_domain_uri (Gimp *gimp)
{
  GimpGuiConfig *config = GIMP_GUI_CONFIG (gimp->config);
636
  GFile         *dir;
637 638 639 640 641 642 643 644
  gchar         *uri;

  if (g_getenv ("GIMP2_HELP_URI"))
    return g_strdup (g_getenv ("GIMP2_HELP_URI"));

  if (config->user_manual_online)
    return g_strdup (config->user_manual_online_uri);

645
  dir = gimp_help_get_user_manual_basedir ();
646 647
  uri = g_file_get_uri (dir);
  g_object_unref (dir);
648 649 650 651

  return uri;
}

652
static gchar *
653
gimp_help_get_locales (Gimp *gimp)
654
{
655 656 657 658 659 660
  GimpGuiConfig  *config = GIMP_GUI_CONFIG (gimp->config);
  gchar         **names;
  gchar          *locales      = NULL;
  GList          *locales_list = NULL;
  GList          *iter;
  gint            i;
661

662 663 664
  if (config->help_locales && strlen (config->help_locales))
    return g_strdup (config->help_locales);

665 666 667 668 669 670 671 672 673 674 675
  /* Process locales. */
  names = (gchar **) g_get_language_names ();
  for (i = 0; names[i]; i++)
    {
      gchar *locale = g_strdup (names[i]);
      gchar *c;

      /* We don't care about encoding in context of our help system. */
      c = strchr (locale, '.');
      if (c)
        *c = '\0';
676 677

      /* We don't care about variants either. */
678 679 680
      c = strchr (locale, '@');
      if (c)
        *c = '\0';
681

682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
      /* Apparently some systems (i.e. Windows) would return a value as
       * IETF language tag, which is a different format from POSIX
       * locale; especially it would separate the lang and the region
       * with an hyphen instead of an underscore.
       * Actually the difference is much deeper, and IETF language tags
       * can have extended language subtags, a script subtag, variants,
       * moreover using different codes.
       * We'd actually need to look into this in details (TODO).
       * this dirty hack should do for easy translation at least (like
       * "en-GB" -> "en_GB).
       * Cf. bug 777754.
       */
      c = strchr (locale, '-');
      if (c)
        *c = '_';
697

698 699 700 701 702 703 704 705 706 707 708 709 710 711
      if (locale && *locale &&
          ! g_list_find_custom (locales_list, locale,
                                (GCompareFunc) g_strcmp0))
        {
          gchar *base;

          /* Adding this locale. */
          locales_list = g_list_prepend (locales_list, locale);

          /* Adding the base language as well. */
          base = strdup (locale);
          c = strchr (base, '_');
          if (c)
            *c = '\0';
712

713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
          if (base && *base &&
              ! g_list_find_custom (locales_list, base,
                                    (GCompareFunc) g_strcmp0))
            {
              locales_list = g_list_prepend (locales_list, base);
            }
          else
            {
              g_free (base);
            }
        }
      else
        {
          g_free (locale);
        }
    }
729

730 731 732 733 734 735 736 737 738 739 740 741 742
  locales_list = g_list_reverse (locales_list);

  /* Finally generate the colon-separated value. */
  if (locales_list)
    {
      locales = g_strdup (locales_list->data);
      for (iter = locales_list->next; iter; iter = iter->next)
        {
          gchar *temp = locales;
          locales = g_strconcat (temp, ":", iter->data, NULL);
          g_free (temp);
        }
    }
743

744 745 746
  g_list_free_full (locales_list, g_free);

  return locales;
747
}
748

749
static GFile *
750 751
gimp_help_get_user_manual_basedir (void)
{
752
  return gimp_data_directory_file ("help", NULL);
753
}
754 755 756 757 758 759 760 761 762 763 764 765 766

static void
gimp_help_query_online_response (GtkWidget    *dialog,
                                 gint          response,
                                 GimpIdleHelp *idle_help)
{
  gtk_widget_destroy (dialog);

  if (response == GTK_RESPONSE_ACCEPT)
    {
      g_object_set (idle_help->gimp->config,
                    "user-manual-online", TRUE,
                    NULL);
Jehan's avatar
Jehan committed
767 768 769 770 771 772 773
    }
  if (response != GTK_RESPONSE_YES)
    {
      g_object_set (idle_help->gimp->config,
                    "help-locales", "",
                    NULL);
    }
774

Jehan's avatar
Jehan committed
775 776 777
  if (response == GTK_RESPONSE_ACCEPT ||
      response == GTK_RESPONSE_YES)
    {
778 779 780 781 782 783 784 785 786 787
      gimp_help_show (idle_help->gimp,
                      idle_help->progress,
                      idle_help->help_domain,
                      idle_help->help_id);
    }

  gimp_idle_help_free (idle_help);
}

static void
Jehan's avatar
Jehan committed
788
gimp_help_query_alt_user_manual (GimpIdleHelp *idle_help)
789 790
{
  GtkWidget *dialog;
Jehan's avatar
Jehan committed
791
  GList     *manuals;
792 793

  dialog = gimp_message_dialog_new (_("GIMP user manual is missing"),
794
                                    GIMP_ICON_HELP_USER_MANUAL,
795
                                    NULL, 0, NULL, NULL,
Jehan's avatar
Jehan committed
796
                                    _("_Cancel"), GTK_RESPONSE_CANCEL,
797
                                    NULL);
Jehan's avatar
Jehan committed
798
  idle_help->query_dialog = GTK_DIALOG (dialog);
799 800 801

  if (idle_help->progress)
    {
802
      guint32 window_id = gimp_progress_get_window_id (idle_help->progress);
803

804 805
      if (window_id)
        gimp_window_set_transient_for (GTK_WINDOW (dialog), window_id);
806 807
    }

Jehan's avatar
Jehan committed
808 809 810 811 812
  gimp_message_box_set_primary_text (GIMP_MESSAGE_DIALOG (dialog)->box,
                                     _("The GIMP user manual is not installed "
                                       "in your language."));

  /* Add a list of available manuals instead, if any. */
813
  manuals = gimp_help_get_installed_languages ();
Jehan's avatar
Jehan committed
814 815
  if (manuals != NULL)
    {
816 817
      GtkWidget *lang_combo;

Jehan's avatar
Jehan committed
818 819 820 821 822
      /* Add an additional button. */
      gtk_dialog_add_button (GTK_DIALOG (dialog),
                             _("Read Selected _Language"),
                             GTK_RESPONSE_YES);
      /* And a dropdown list of available manuals. */
823 824
      lang_combo = gimp_language_combo_box_new (TRUE,
                                                _("Available manuals..."));
Jehan's avatar
Jehan committed
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
      gtk_combo_box_set_active (GTK_COMBO_BOX (lang_combo), 0);
      gtk_dialog_set_response_sensitive (idle_help->query_dialog,
                                         GTK_RESPONSE_YES, FALSE);
      g_signal_connect (lang_combo, "changed",
                        G_CALLBACK (gimp_help_language_combo_changed),
                        idle_help);
      gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
                          lang_combo, TRUE, TRUE, 0);
      gtk_widget_show (lang_combo);

      gimp_message_box_set_text (GIMP_MESSAGE_DIALOG (dialog)->box,
                                 _("You may either select a manual in another "
                                   "language or read the online version."));
    }
  else
    {
      gimp_message_box_set_text (GIMP_MESSAGE_DIALOG (dialog)->box,
                                 _("You may either install the additional help "
                                   "package or change your preferences to use "
                                   "the online version."));
    }
  gtk_dialog_add_button (GTK_DIALOG (dialog),
                         _("Read _Online"), GTK_RESPONSE_ACCEPT);
  gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
  if (manuals != NULL)
    {
851
      gimp_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
Jehan's avatar
Jehan committed
852 853 854 855 856 857 858 859
                                               GTK_RESPONSE_ACCEPT,
                                               GTK_RESPONSE_YES,
                                               GTK_RESPONSE_CANCEL,
                                               -1);
      g_list_free_full (manuals, g_free);
    }
  else
    {
860
      gimp_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
Jehan's avatar
Jehan committed
861 862 863 864
                                               GTK_RESPONSE_ACCEPT,
                                               GTK_RESPONSE_CANCEL,
                                               -1);
    }
865 866 867
  g_signal_connect (dialog, "response",
                    G_CALLBACK (gimp_help_query_online_response),
                    idle_help);
Jehan's avatar
Jehan committed
868 869
  gtk_widget_show (dialog);
}
870

Jehan's avatar
Jehan committed
871 872 873 874
static void
gimp_help_language_combo_changed (GtkComboBox  *combo,
                                  GimpIdleHelp *idle_help)
{
875 876
  gchar *help_locales = NULL;
  gchar *code;
Jehan's avatar
Jehan committed
877

878 879
  code = gimp_language_combo_box_get_code (GIMP_LANGUAGE_COMBO_BOX (combo));
  if (code && g_strcmp0 ("", code) != 0)
Jehan's avatar
Jehan committed
880
    {
881
      help_locales = g_strdup_printf ("%s:", code);
Jehan's avatar
Jehan committed
882 883 884 885 886 887 888 889
      gtk_dialog_set_response_sensitive (idle_help->query_dialog,
                                         GTK_RESPONSE_YES, TRUE);
    }
  else
    {
      gtk_dialog_set_response_sensitive (idle_help->query_dialog,
                                         GTK_RESPONSE_YES, FALSE);
    }
890 891 892 893 894 895 896
  g_object_set (idle_help->gimp->config,
                "help-locales", help_locales? help_locales : "",
                NULL);

  g_free (code);
  if (help_locales)
    g_free (help_locales);
897
}