latexila-utils.c 16.4 KB
Newer Older
1
/*
2
 * This file is part of GNOME LaTeX.
3 4 5 6 7 8
 *
 * From gedit-utils.c:
 * Copyright (C) 1998, 1999 - Alex Roberts, Evan Lawrence
 * Copyright (C) 2000, 2002 - Chema Celorio, Paolo Maggi
 * Copyright (C) 2003-2005 - Paolo Maggi
 *
9
 * Copyright (C) 2014, 2015, 2017, 2018 - Sébastien Wilmet <swilmet@gnome.org>
10
 *
11
 * GNOME LaTeX is free software: you can redistribute it and/or modify
12 13 14 15
 * 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.
 *
16
 * GNOME LaTeX is distributed in the hope that it will be useful,
17 18 19 20 21
 * 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
22
 * along with GNOME LaTeX.  If not, see <http://www.gnu.org/licenses/>.
23 24 25 26 27
 */

/**
 * SECTION:utils
 * @title: LatexilaUtils
28
 * @short_description: Utility functions
29
 *
30
 * Various utility functions.
31 32 33 34
 */

#include "latexila-utils.h"
#include <string.h>
35
#include "dh-dconf-migration.h"
Sébastien Wilmet's avatar
Sébastien Wilmet committed
36
#include "latexila-synctex.h"
37

38 39 40
static gint
get_extension_position (const gchar *filename)
{
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
	const gchar *pos;
	gint length;

	if (filename == NULL)
	{
		return 0;
	}

	length = strlen (filename);
	pos = filename + length;
	g_assert (pos[0] == '\0');

	while (TRUE)
	{
		pos = g_utf8_find_prev_char (filename, pos);

		if (pos == NULL || pos[0] == '/')
		{
			break;
		}

		if (pos[0] == '.')
		{
			return pos - filename;
		}
	}

	return length;
69 70 71 72 73 74 75 76 77 78 79
}

/**
 * latexila_utils_get_shortname:
 * @filename: a filename.
 *
 * Returns: the @filename without its extension. Free with g_free().
 */
gchar *
latexila_utils_get_shortname (const gchar *filename)
{
80
	return g_strndup (filename, get_extension_position (filename));
81 82
}

Sébastien Wilmet's avatar
Sébastien Wilmet committed
83 84 85 86 87 88 89 90 91 92
/**
 * latexila_utils_get_extension:
 * @filename: a filename.
 *
 * Returns: the @filename's extension with the dot, in lowercase. Free with
 * g_free().
 */
gchar *
latexila_utils_get_extension (const gchar *filename)
{
93 94 95
	gint pos = get_extension_position (filename);

	return g_ascii_strdown (filename + pos, -1);
Sébastien Wilmet's avatar
Sébastien Wilmet committed
96 97
}

98 99 100 101 102 103 104 105 106 107 108 109 110 111
/**
 * latexila_utils_replace_home_dir_with_tilde:
 * @filename: the filename.
 *
 * Replaces the home directory with a tilde, if the home directory is present in
 * the @filename.
 *
 * This function comes from gedit.
 *
 * Returns: the new filename. Free with g_free().
 */
gchar *
latexila_utils_replace_home_dir_with_tilde (const gchar *filename)
{
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
	gchar *tmp;
	gchar *home;

	g_return_val_if_fail (filename != NULL, NULL);

	/* Note that g_get_home_dir returns a const string */
	tmp = (gchar *) g_get_home_dir ();

	if (tmp == NULL)
	{
		return g_strdup (filename);
	}

	home = g_filename_to_utf8 (tmp, -1, NULL, NULL, NULL);
	if (home == NULL)
	{
		return g_strdup (filename);
	}

	if (strcmp (filename, home) == 0)
	{
		g_free (home);
		return g_strdup ("~");
	}

	tmp = home;
	home = g_strdup_printf ("%s/", tmp);
	g_free (tmp);

	if (g_str_has_prefix (filename, home))
	{
		gchar *res = g_strdup_printf ("~/%s", filename + strlen (home));
		g_free (home);
		return res;
	}

	g_free (home);
	return g_strdup (filename);
150
}
151 152 153 154

/**
 * latexila_utils_register_icons:
 *
155
 * Register the GNOME LaTeX icons to the #GtkIconTheme as built-in icons. For
156
 * example the icon located at data/images/stock-icons/badbox.png in the
157
 * GNOME LaTeX git repository will be available with the icon name "badbox". The
158
 * "stock-icons" directory name is for historical reasons and should be changed
159
 * when stock icons are no longer used in GNOME LaTeX.
160
 *
161 162
 * TODO: prefix icon names with "glatex-", so the badbox.png would be
 * "glatex-badbox".
163 164 165 166
 */
void
latexila_utils_register_icons (void)
{
167 168
	gtk_icon_theme_add_resource_path (gtk_icon_theme_get_default (),
					  "/org/gnome/gnome-latex/stock-icons/");
169
}
170

171 172 173 174 175 176 177 178 179
/**
 * latexila_utils_get_pixbuf_from_icon_name:
 * @icon_name: an icon name.
 * @icon_size: an icon size.
 *
 * Returns: (nullable) (transfer full): the corresponding #GdkPixbuf.
 */
GdkPixbuf *
latexila_utils_get_pixbuf_from_icon_name (const gchar *icon_name,
180
					  GtkIconSize  icon_size)
181
{
182 183 184
	gint size;
	GdkPixbuf *pixbuf;
	GError *error = NULL;
185

186
	gtk_icon_size_lookup (icon_size, &size, NULL);
187

188 189 190 191 192
	pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
					   icon_name,
					   size,
					   0,
					   &error);
193

194 195 196 197 198
	if (error != NULL)
	{
		g_warning ("Error when loading icon \"%s\": %s", icon_name, error->message);
		g_error_free (error);
	}
199

200
	return pixbuf;
201 202
}

203 204 205 206 207 208 209 210 211 212 213 214
/**
 * latexila_utils_str_replace:
 * @string: a string
 * @search: the search string
 * @replacement: the replacement string
 *
 * Replaces all occurences of @search by @replacement.
 *
 * Returns: A newly allocated string with the replacements. Free with g_free().
 */
gchar *
latexila_utils_str_replace (const gchar *string,
215 216
			    const gchar *search,
			    const gchar *replacement)
217
{
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
	gchar **chunks;
	gchar *ret;

	g_return_val_if_fail (string != NULL, NULL);
	g_return_val_if_fail (search != NULL, NULL);
	g_return_val_if_fail (replacement != NULL, NULL);

	chunks = g_strsplit (string, search, -1);
	if (chunks != NULL && chunks[0] != NULL)
	{
		ret = g_strjoinv (replacement, chunks);
	}
	else
	{
		ret = g_strdup (string);
	}

	g_strfreev (chunks);
	return ret;
237
}
Sébastien Wilmet's avatar
Sébastien Wilmet committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251

/**
 * latexila_utils_file_query_exists_async:
 * @file: a #GFile.
 * @cancellable: a #GCancellable.
 * @callback: the callback to call when the operation is finished.
 * @user_data: the data to pass to the callback function.
 *
 * The asynchronous version of g_file_query_exists(). When the operation is
 * finished, @callback will be called. You can then call
 * latexila_utils_file_query_exists_finish() to get the result of the operation.
 */
void
latexila_utils_file_query_exists_async (GFile               *file,
252 253 254
					GCancellable        *cancellable,
					GAsyncReadyCallback  callback,
					gpointer             user_data)
Sébastien Wilmet's avatar
Sébastien Wilmet committed
255
{
256 257 258 259 260 261 262
	g_file_query_info_async (file,
				 G_FILE_ATTRIBUTE_STANDARD_TYPE,
				 G_FILE_QUERY_INFO_NONE,
				 G_PRIORITY_DEFAULT,
				 cancellable,
				 callback,
				 user_data);
Sébastien Wilmet's avatar
Sébastien Wilmet committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
}

/**
 * latexila_utils_file_query_exists_finish:
 * @file: a #GFile.
 * @result: a #GAsyncResult.
 *
 * Finishes the operation started with latexila_utils_file_query_exists_async().
 * There is no output #GError parameter, so you should check if the operation
 * has been cancelled (in which case %FALSE will be returned).
 *
 * Returns: %TRUE if the file exists and the operation hasn't been cancelled,
 * %FALSE otherwise.
 */
gboolean
latexila_utils_file_query_exists_finish (GFile        *file,
279
					 GAsyncResult *result)
Sébastien Wilmet's avatar
Sébastien Wilmet committed
280
{
281
	GFileInfo *info = g_file_query_info_finish (file, result, NULL);
Sébastien Wilmet's avatar
Sébastien Wilmet committed
282

283 284 285 286 287
	if (info != NULL)
	{
		g_object_unref (info);
		return TRUE;
	}
Sébastien Wilmet's avatar
Sébastien Wilmet committed
288

289
	return FALSE;
Sébastien Wilmet's avatar
Sébastien Wilmet committed
290
}
Sébastien Wilmet's avatar
Sébastien Wilmet committed
291 292 293 294

static gboolean
default_document_viewer_is_evince (const gchar *uri)
{
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
	GFile *file;
	GAppInfo *app_info;
	const gchar *executable;
	gboolean ret;
	GError *error = NULL;

	file = g_file_new_for_uri (uri);
	app_info = g_file_query_default_handler (file, NULL, &error);
	g_object_unref (file);

	if (error != NULL)
	{
		g_warning ("Impossible to know if evince is the default document viewer: %s",
			   error->message);

		g_error_free (error);
		return FALSE;
	}

	executable = g_app_info_get_executable (app_info);
	ret = strstr (executable, "evince") != NULL;

	g_object_unref (app_info);
	return ret;
Sébastien Wilmet's avatar
Sébastien Wilmet committed
319 320 321 322
}

/**
 * latexila_utils_show_uri:
323
 * @widget: (nullable): the associated #GtkWidget, or %NULL.
324 325
 * @uri: the URI to show.
 * @timestamp: a timestamp.
Sébastien Wilmet's avatar
Sébastien Wilmet committed
326 327 328 329 330 331 332
 * @error: (out) (optional): a %NULL #GError, or %NULL.
 *
 * Shows the @uri. If the URI is a PDF file and if Evince is the default
 * document viewer, this function also connects the Evince window so the
 * backward search works (switch from the PDF to the source file).
 */
void
333
latexila_utils_show_uri (GtkWidget    *widget,
334 335 336
			 const gchar  *uri,
			 guint32       timestamp,
			 GError      **error)
Sébastien Wilmet's avatar
Sébastien Wilmet committed
337
{
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
	GtkWindow *parent = NULL;

	g_return_if_fail (widget == NULL || GTK_IS_WIDGET (widget));
	g_return_if_fail (uri != NULL);
	g_return_if_fail (error == NULL || *error == NULL);

	if (widget != NULL)
	{
		GtkWidget *toplevel;

		toplevel = gtk_widget_get_toplevel (widget);
		if (gtk_widget_is_toplevel (toplevel) &&
		    GTK_IS_WINDOW (toplevel))
		{
			parent = GTK_WINDOW (toplevel);
		}
	}

	if (gtk_show_uri_on_window (parent, uri, timestamp, error))
	{
		gchar *extension = latexila_utils_get_extension (uri);

		if (g_strcmp0 (extension, ".pdf") == 0 &&
		    default_document_viewer_is_evince (uri))
		{
			LatexilaSynctex *synctex = latexila_synctex_get_instance ();
			latexila_synctex_connect_evince_window (synctex, uri);
		}

		g_free (extension);
	}
Sébastien Wilmet's avatar
Sébastien Wilmet committed
369
}
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385

/**
 * latexila_utils_get_dialog_component:
 * @title: the title of the dialog component.
 * @widget: the widget displayed below the title.
 *
 * Gets a #GtkDialog component. When a dialog contains several components, or
 * logical groups, this function is useful to attach the @widget with a @title.
 * The title will be in bold, left-aligned, and the widget will have a left
 * margin.
 *
 * Returns: (transfer floating): the dialog component containing the @title and
 * the @widget.
 */
GtkWidget *
latexila_utils_get_dialog_component (const gchar *title,
386
				     GtkWidget   *widget)
387
{
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
	GtkContainer *grid;
	GtkWidget *label;
	gchar *markup;

	grid = GTK_CONTAINER (gtk_grid_new ());
	gtk_orientable_set_orientation (GTK_ORIENTABLE (grid), GTK_ORIENTATION_VERTICAL);
	gtk_grid_set_row_spacing (GTK_GRID (grid), 6);
	gtk_container_set_border_width (grid, 6);

	/* Title in bold, left-aligned. */
	label = gtk_label_new (NULL);
	markup = g_strdup_printf ("<b>%s</b>", title);
	gtk_label_set_markup (GTK_LABEL (label), markup);
	gtk_widget_set_halign (label, GTK_ALIGN_START);
	gtk_container_add (grid, label);

	/* Left margin for the widget. */
	gtk_widget_set_margin_start (widget, 12);
	gtk_container_add (grid, widget);

	g_free (markup);
	return GTK_WIDGET (grid);
410
}
411 412 413 414 415 416 417 418 419 420 421 422 423 424

/**
 * latexila_utils_create_parent_directories:
 * @file: a file
 * @error: (out) (optional): a location to a %NULL #GError, or %NULL.
 *
 * Synchronously creates parent directories of @file, so that @file can be
 * saved.
 *
 * Returns: whether the directories are correctly created. %FALSE is returned on
 * error.
 */
gboolean
latexila_utils_create_parent_directories (GFile   *file,
425
					  GError **error)
426
{
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
	GFile *parent;
	GError *my_error = NULL;

	g_return_val_if_fail (G_IS_FILE (file), FALSE);
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

	parent = g_file_get_parent (file);

	if (parent == NULL)
	{
		return TRUE;
	}

	g_file_make_directory_with_parents (parent, NULL, &my_error);
	g_object_unref (parent);

	if (my_error != NULL)
	{
		if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
		{
			g_error_free (my_error);
			return TRUE;
		}
		else
		{
			g_propagate_error (error, my_error);
			return FALSE;
		}
	}

	return TRUE;
458
}
459 460 461 462 463 464 465 466 467 468 469 470 471

/**
 * latexila_utils_join_widgets:
 * @widget_top: the #GtkWidget at the top.
 * @widget_bottom: the #GtkWidget at the bottom.
 *
 * Joins two widgets vertically, with junction sides.
 *
 * Returns: (transfer floating): a #GtkContainer containing @widget_top and
 * @widget_bottom.
 */
GtkWidget *
latexila_utils_join_widgets (GtkWidget *widget_top,
472
			     GtkWidget *widget_bottom)
473
{
474 475
	GtkStyleContext *context;
	GtkBox *vbox;
476

477 478
	g_return_val_if_fail (GTK_IS_WIDGET (widget_top), NULL);
	g_return_val_if_fail (GTK_IS_WIDGET (widget_bottom), NULL);
479

480 481
	context = gtk_widget_get_style_context (widget_top);
	gtk_style_context_set_junction_sides (context, GTK_JUNCTION_BOTTOM);
482

483 484
	context = gtk_widget_get_style_context (widget_bottom);
	gtk_style_context_set_junction_sides (context, GTK_JUNCTION_TOP);
485

486 487 488
	vbox = GTK_BOX (gtk_box_new (GTK_ORIENTATION_VERTICAL, 0));
	gtk_box_pack_start (vbox, widget_top, TRUE, TRUE, 0);
	gtk_box_pack_start (vbox, widget_bottom, FALSE, FALSE, 0);
489

490
	return GTK_WIDGET (vbox);
491
}
492

493 494
static void
migrate_latexila_to_gnome_latex_gsettings (void)
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
{
	DhDconfMigration *migration;
	gint i;

	const gchar *keys[] =
	{
		"preferences/editor/auto-save",
		"preferences/editor/auto-save-interval",
		"preferences/editor/bracket-matching",
		"preferences/editor/create-backup-copy",
		"preferences/editor/display-line-numbers",
		"preferences/editor/editor-font",
		"preferences/editor/forget-no-tabs",
		"preferences/editor/highlight-current-line",
		"preferences/editor/highlight-misspelled-words",
		"preferences/editor/insert-spaces",
		"preferences/editor/reopen-files",
		"preferences/editor/scheme",
		"preferences/editor/spell-checking-language",
		"preferences/editor/tabs-size",
		"preferences/editor/use-default-font",
		"preferences/file-browser/current-directory",
		"preferences/file-browser/show-build-files",
		"preferences/file-browser/show-hidden-files",
		"preferences/latex/automatic-clean",
		"preferences/latex/clean-extensions",
		"preferences/latex/disabled-default-build-tools",
		"preferences/latex/enabled-default-build-tools",
		"preferences/latex/interactive-completion",
		"preferences/latex/interactive-completion-num",
		"preferences/latex/no-confirm-clean",
		"preferences/ui/bottom-panel-visible",
		"preferences/ui/edit-toolbar-visible",
		"preferences/ui/main-toolbar-visible",
		"preferences/ui/show-build-badboxes",
		"preferences/ui/show-build-warnings",
		"preferences/ui/side-panel-component",
		"preferences/ui/side-panel-visible",
		"state/dialogs/finance/last-shown-date",
		"state/dialogs/finance/remind-later",
		"state/window/documents",
		"state/window/side-panel-size",
		"state/window/size",
		"state/window/state",
		"state/window/structure-paned-position",
		"state/window/vertical-paned-position",
		NULL
	};

	migration = _dh_dconf_migration_new ();

	for (i = 0; keys[i] != NULL; i++)
	{
		const gchar *cur_key = keys[i];
		gchar *cur_glatex_key;
		gchar *cur_latexila_key;

		cur_glatex_key = g_strconcat ("/org/gnome/gnome-latex/", cur_key, NULL);
		cur_latexila_key = g_strconcat ("/org/gnome/latexila/", cur_key, NULL);

		_dh_dconf_migration_migrate_key (migration,
						 cur_glatex_key,
						 cur_latexila_key,
						 NULL);

		g_free (cur_glatex_key);
		g_free (cur_latexila_key);
	}

	_dh_dconf_migration_free (migration);
}
566

567
static void
568 569
migrate_latexila_to_gnome_latex_copy_file (GFile *latexila_file,
					   GFile *glatex_file)
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
{
	GError *error = NULL;

	latexila_utils_create_parent_directories (glatex_file, &error);
	if (error != NULL)
	{
		goto out;
	}

	g_file_copy (latexila_file,
		     glatex_file,
		     G_FILE_COPY_TARGET_DEFAULT_PERMS,
		     NULL, NULL, NULL,
		     &error);

585 586
	if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND) ||
	    g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS))
587 588 589 590
	{
		g_clear_error (&error);
	}

591 592 593
out:
	if (error != NULL)
	{
594
		g_warning ("Error when migrating LaTeXila to GNOME LaTeX user data file: %s",
595 596 597
			   error->message);
		g_clear_error (&error);
	}
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
}

static void
migrate_latexila_to_gnome_latex_most_used_symbols (void)
{
	GFile *latexila_file;
	GFile *glatex_file;

	latexila_file = g_file_new_build_filename (g_get_user_data_dir (),
						   "latexila",
						   "most_used_symbols.xml",
						   NULL);

	glatex_file = g_file_new_build_filename (g_get_user_data_dir (),
						 "gnome-latex",
						 "most_used_symbols.xml",
						 NULL);

	migrate_latexila_to_gnome_latex_copy_file (latexila_file, glatex_file);

	g_object_unref (latexila_file);
	g_object_unref (glatex_file);
}

static void
migrate_latexila_to_gnome_latex_projects (void)
{
	GFile *latexila_file;
	GFile *glatex_file;

	latexila_file = g_file_new_build_filename (g_get_user_data_dir (),
						   "latexila",
						   "projects.xml",
						   NULL);

	glatex_file = g_file_new_build_filename (g_get_user_data_dir (),
						 "gnome-latex",
						 "projects.xml",
						 NULL);

	migrate_latexila_to_gnome_latex_copy_file (latexila_file, glatex_file);
639 640 641 642 643

	g_object_unref (latexila_file);
	g_object_unref (glatex_file);
}

644 645 646
/**
 * latexila_utils_migrate_latexila_to_gnome_latex:
 *
647 648
 * Migrates the #GSettings values and user data/config files from LaTeXila to
 * GNOME LaTeX, so that users don't lose all their settings.
649 650 651 652
 */
void
latexila_utils_migrate_latexila_to_gnome_latex (void)
{
653 654 655 656 657 658 659 660
	GSettings *settings;

	settings = g_settings_new ("org.gnome.gnome-latex");

	if (!g_settings_get_boolean (settings, "latexila-to-gnome-latex-migration-done"))
	{
		migrate_latexila_to_gnome_latex_gsettings ();
		migrate_latexila_to_gnome_latex_most_used_symbols ();
661
		migrate_latexila_to_gnome_latex_projects ();
662 663 664 665 666

		g_settings_set_boolean (settings, "latexila-to-gnome-latex-migration-done", TRUE);
	}

	g_object_unref (settings);
667
}