File: tepl-tab-loading.c

package info (click to toggle)
libgedit-tepl 6.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,936 kB
  • sloc: ansic: 18,685; xml: 759; sh: 20; makefile: 9
file content (98 lines) | stat: -rw-r--r-- 2,450 bytes parent folder | download | duplicates (5)
1
2
3
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
/* SPDX-FileCopyrightText: 2020 - Sébastien Wilmet <swilmet@gnome.org>
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#include "config.h"
#include "tepl-tab-loading.h"
#include <glib/gi18n-lib.h>
#include "tepl-file-loader.h"
#include "tepl-info-bar.h"

static void
load_file_cb (GObject      *source_object,
	      GAsyncResult *result,
	      gpointer      user_data)
{
	TeplFileLoader *loader = TEPL_FILE_LOADER (source_object);
	TeplTab *tab = TEPL_TAB (user_data);
	GError *error = NULL;

	if (tepl_file_loader_load_finish (loader, result, &error))
	{
		TeplBuffer *buffer;
		TeplFile *file;

		buffer = tepl_tab_get_buffer (tab);
		file = tepl_buffer_get_file (buffer);
		tepl_file_add_uri_to_recent_manager (file);
	}

	if (error != NULL &&
	    !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
	{
		TeplInfoBar *info_bar;

		info_bar = tepl_info_bar_new_simple (GTK_MESSAGE_ERROR,
						     _("Error when loading the file."),
						     error->message);

		tepl_tab_add_info_bar (tab, GTK_INFO_BAR (info_bar));
		gtk_widget_show (GTK_WIDGET (info_bar));
	}

	g_clear_error (&error);
	g_object_unref (loader);
	g_object_unref (tab);
}

/**
 * tepl_tab_load_file:
 * @tab: a #TeplTab.
 * @location: a #GFile.
 *
 * Unconditionally loads a file in @tab, regardless if there are unsaved changes
 * in the #GtkTextBuffer. The previous buffer content is lost.
 *
 * This function is asynchronous, there is no way to know when the file loading
 * is finished.
 *
 * Since: 4.0
 */
void
tepl_tab_load_file (TeplTab *tab,
		    GFile   *location)
{
	TeplBuffer *buffer;
	TeplFile *file;
	TeplFileLoader *loader;
	GCancellable *cancellable;

	g_return_if_fail (TEPL_IS_TAB (tab));
	g_return_if_fail (G_IS_FILE (location));

	buffer = tepl_tab_get_buffer (tab);
	file = tepl_buffer_get_file (buffer);

	tepl_file_set_location (file, location);
	loader = tepl_file_loader_new (buffer, file);

	cancellable = g_cancellable_new ();

	/* If there is a request to destroy the tab, it's pointless to continue
	 * loading the file. So, cancel the operation when the tab is destroyed,
	 * to free up resources for other operations.
	 */
	g_signal_connect_object (tab,
				 "destroy",
				 G_CALLBACK (g_cancellable_cancel),
				 cancellable,
				 G_CONNECT_SWAPPED);

	tepl_file_loader_load_async (loader,
				     G_PRIORITY_DEFAULT,
				     cancellable,
				     load_file_cb,
				     g_object_ref (tab));

	g_object_unref (cancellable);
}