summaryrefslogtreecommitdiff
path: root/src/plugins/open-directory/totem-open-directory.c
blob: 3106be9bcb9e8f5920213f150b7841764d849bae (plain)
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
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
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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
 * Copyright (C) Matthieu Gautier 2015 <dev@mgautier.fr>
 *
 * 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 Library General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
 */

#include "config.h"

#include <glib/gi18n-lib.h>
#include <libpeas/peas-extension-base.h>
#include <libpeas/peas-object-module.h>
#include <libpeas/peas-activatable.h>

#include "totem-plugin.h"

#define TOTEM_TYPE_OPEN_DIRECTORY_PLUGIN		(totem_open_directory_plugin_get_type ())
#define TOTEM_OPEN_DIRECTORY_PLUGIN(o)			(G_TYPE_CHECK_INSTANCE_CAST ((o), TOTEM_TYPE_OPEN_DIRECTORY_PLUGIN, TotemOpenDirectoryPlugin))

typedef struct {
	PeasExtensionBase parent;

	TotemObject *totem;
	char        *mrl;
	GSimpleAction *action;
} TotemOpenDirectoryPlugin;

TOTEM_PLUGIN_REGISTER(TOTEM_TYPE_OPEN_DIRECTORY_PLUGIN, TotemOpenDirectoryPlugin, totem_open_directory_plugin)

static char *
get_notification_id (void)
{
	return g_strdup_printf ("%s_TIME%ld",
				"totem",
				g_get_monotonic_time () / G_TIME_SPAN_SECOND);
}

static void
totem_open_directory_plugin_open (GSimpleAction       *action,
			     GVariant            *parameter,
			     TotemOpenDirectoryPlugin *pi)
{


	GError *error = NULL;
	GDBusProxy *proxy;
	gchar* notification_id;
	GVariantBuilder *builder;
	GVariant *dbus_arguments;

	g_assert (pi->mrl != NULL);

	proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
					       G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
					       NULL, /* GDBusInterfaceInfo */
					       "org.freedesktop.FileManager1",
					       "/org/freedesktop/FileManager1",
					       "org.freedesktop.FileManager1",
					       NULL, /* GCancellable */
					       &error);
	if (proxy == NULL) {
		g_warning ("Could not contact file manager: %s", error->message);
		g_error_free (error);
		return;
	}

	notification_id = get_notification_id();

	builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
	g_variant_builder_add (builder, "s", pi->mrl);
	dbus_arguments = g_variant_new ("(ass)", builder, notification_id);
	g_variant_builder_unref (builder);
	g_free(notification_id);

	if (g_dbus_proxy_call_sync (proxy,
				"ShowItems", dbus_arguments,
				G_DBUS_CALL_FLAGS_NONE,
				-1, NULL, &error) == FALSE) {
		g_warning ("Could not get file manager to display file: %s", error->message);
		g_error_free (error);
	}

	g_object_unref (proxy);
}

static void
totem_open_directory_file_closed (TotemObject *totem,
				 TotemOpenDirectoryPlugin *pi)
{
	g_clear_pointer (&pi->mrl, g_free);

	g_simple_action_set_enabled (G_SIMPLE_ACTION (pi->action), FALSE);
}

static gboolean
scheme_is_supported (const char *scheme)
{
	const gchar * const *schemes;
	guint i;

	if (!scheme)
		return FALSE;

	if (g_str_equal (scheme, "http") ||
	    g_str_equal (scheme, "https"))
		return FALSE;

	schemes = g_vfs_get_supported_uri_schemes (g_vfs_get_default ());
	for (i = 0; schemes[i] != NULL; i++) {
		if (g_str_equal (schemes[i], scheme))
			return TRUE;
	}
	return FALSE;
}

static void
totem_open_directory_file_opened (TotemObject              *totem,
				  const char               *mrl,
				  TotemOpenDirectoryPlugin *pi)
{
	char *scheme;

	g_clear_pointer (&pi->mrl, g_free);

	if (mrl == NULL)
		return;

	scheme = g_uri_parse_scheme (mrl);
	if (!scheme_is_supported (scheme)) {
		g_debug ("Not enabling open-directory as scheme for '%s' not supported", mrl);
		g_free (scheme);
		return;
	}
	g_free (scheme);

	g_simple_action_set_enabled (G_SIMPLE_ACTION (pi->action), TRUE);
	pi->mrl = g_strdup (mrl);
}

static void
impl_activate (PeasActivatable *plugin)
{
	TotemOpenDirectoryPlugin *pi = TOTEM_OPEN_DIRECTORY_PLUGIN (plugin);
	GMenu *menu;
	GMenuItem *item;
	char *mrl;

	/* FIXME: This plugin will stop working if the file is outside
	 * what the Flatpak has access to
	if (g_file_test ("/.flatpak-info", G_FILE_TEST_EXISTS))
		return; */

	pi->totem = g_object_get_data (G_OBJECT (plugin), "object");

	g_signal_connect (pi->totem,
			  "file-opened",
			  G_CALLBACK (totem_open_directory_file_opened),
			  plugin);
	g_signal_connect (pi->totem,
			  "file-closed",
			  G_CALLBACK (totem_open_directory_file_closed),
			  plugin);

	pi->action = g_simple_action_new ("open-dir", NULL);
	g_signal_connect (G_OBJECT (pi->action), "activate",
			  G_CALLBACK (totem_open_directory_plugin_open), plugin);
	g_action_map_add_action (G_ACTION_MAP (pi->totem), G_ACTION (pi->action));
	g_simple_action_set_enabled (G_SIMPLE_ACTION (pi->action), FALSE);

	/* add UI */
	menu = totem_object_get_menu_section (pi->totem, "opendirectory-placeholder");
	item = g_menu_item_new (_("Open Containing Folder"), "app.open-dir");
	g_menu_append_item (G_MENU (menu), item);

	mrl = totem_object_get_current_mrl (pi->totem);
	totem_open_directory_file_opened (pi->totem, mrl, pi);
	g_free (mrl);
}

static void
impl_deactivate (PeasActivatable *plugin)
{
	TotemOpenDirectoryPlugin *pi = TOTEM_OPEN_DIRECTORY_PLUGIN (plugin);

	g_signal_handlers_disconnect_by_func (pi->totem, totem_open_directory_file_opened, plugin);
	g_signal_handlers_disconnect_by_func (pi->totem, totem_open_directory_file_closed, plugin);

	totem_object_empty_menu_section (pi->totem, "opendirectory-placeholder");

	pi->totem = NULL;

	g_clear_pointer (&pi->mrl, g_free);
}