aboutsummaryrefslogtreecommitdiff
path: root/monitor.c
blob: 13214bfcef3639e64cdbc17d83c17b9495587a07 (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
206
207
208
209
210
211
212
#include "rodisc.h"

#include <gio/gio.h>

static GVolumeMonitor *monitor;

static bool guess_volume_type(GVolume *volume, const char **type)
{
	GIcon *icon = g_volume_get_icon(volume);
	g_return_val_if_fail(G_IS_THEMED_ICON(icon), false);
	GThemedIcon *ticon = G_THEMED_ICON(icon);
	
	const gchar * const * names = g_themed_icon_get_names(ticon);
	for (const gchar * const * name = names; *name; ++name) {
		if (g_str_has_prefix(*name, "media-optical")) {
			if (g_str_has_prefix(*name, "media-optical-dvd")) {
				*type = RODISC_TYPE_DVD;
			} else if (g_str_has_prefix(*name, "media-optical-cd")) {
				*type = RODISC_TYPE_CD;
			} else {
				*type = RODISC_TYPE_GENERIC;
			}
			return true;
		}
	}

	return false;
}

static bool set_disc_attrs(RODisc *disc, GVolume *volume)
{
	g_free(disc->label);

	disc->label = g_volume_get_identifier(volume, G_VOLUME_IDENTIFIER_KIND_LABEL);
	if (!disc->label || disc->label[0] == '\0') {
		g_free(disc->label);
		disc->label = g_volume_get_name(volume);
	}
	if (!disc->label || disc->label[0] == '\0') {
		g_free(disc->label);
		disc->label = g_strdup("Disc");
	}

	// Open file and seek to end to verify read permission and file size

	GError *error = NULL;
	GFileInputStream * stream = g_file_read(disc->file, NULL, &error);

	if (!stream) {
		g_warning("Could not open file '%s' for reading: %s",
		          disc->file_path, error->message);
		g_error_free(error);
		return false;
	}

	if (!g_seekable_seek(G_SEEKABLE(stream), 0, G_SEEK_END, NULL, &error)) {
		g_warning("Could not seek in file '%s' for obtaining file size: %s",
		          disc->file_path, error->message);
		g_error_free(error);
		return false;
	}

	disc->size = g_seekable_tell(G_SEEKABLE(stream));

	g_input_stream_close(G_INPUT_STREAM(stream), NULL, NULL);
	g_object_unref(stream);

	return true;
}

static void try_add_volume(GVolume *volume)
{
	const char * type;
	if (!guess_volume_type(volume, &type)) {
		// Ignore non-optical device
		return;
	}

	char *device_file_path = g_volume_get_identifier(volume, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
	if (!device_file_path) {
		return;
	}

	RODisc *disc = rodisc_new();

	char *basename = g_path_get_basename(device_file_path);

	disc->id = device_file_path;
	disc->uri = g_strconcat("/", basename, NULL);
	disc->file = g_file_new_for_path(device_file_path);
	disc->file_path = g_file_get_path(disc->file);
	disc->type = type;

	g_free(basename);

	if (set_disc_attrs(disc, volume)) {
		rodisc_export(disc);
	} else {
		rodisc_destroy(disc);
	}
}

static void try_update_volume(GVolume *volume, RODisc *disc)
{
	const char * type;
	if (!guess_volume_type(volume, &type)) {
		g_debug("no longer a optical media");
		rodisc_remove(disc);
		return;
	}

	disc->type = type;

	if (set_disc_attrs(disc, volume)) {
		rodisc_refresh(disc);
	} else {
		rodisc_remove(disc);
	}
}

static void monitor_volume_added_cb(GVolumeMonitor *volume_monitor, GVolume *volume,
                                    gpointer user_data)
{
	char *device_file_path = g_volume_get_identifier(volume, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
	if (!device_file_path) {
		return;
	}

	g_debug("Volume added: %s", device_file_path);

	RODisc *disc = rodisc_lookup(device_file_path);
	if (disc) {
		g_warning("Device already added: %s", device_file_path);
	} else {
		try_add_volume(volume);
	}

	g_free(device_file_path);
}

static void monitor_volume_changed_cb(GVolumeMonitor *volume_monitor, GVolume *volume,
                                      gpointer user_data)
{
	char *device_file_path = g_volume_get_identifier(volume, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
	if (!device_file_path) {
		return;
	}

	g_debug("Volume changed: %s", device_file_path);

	RODisc *disc = rodisc_lookup(device_file_path);
	if (disc) {
		try_update_volume(volume, disc);
	} else {
		try_add_volume(volume);
	}

	g_free(device_file_path);
}

static void monitor_volume_removed_cb(GVolumeMonitor *volume_monitor, GVolume *volume,
                                      gpointer user_data)
{
	char *device_file_path = g_volume_get_identifier(volume, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
	if (!device_file_path) {
		return;
	}

	g_debug("Volume removed: %s", device_file_path);

	RODisc *disc = rodisc_lookup(device_file_path);
	if (disc) {
		rodisc_remove(disc);
	}

	g_free(device_file_path);
}

static void enumerate_volumes()
{
	GList *volumes = g_volume_monitor_get_volumes(monitor);
	for (GList *l = volumes; l != NULL; l = l->next) {
		GVolume *volume = G_VOLUME(l->data);
		try_add_volume(volume);
	}
	g_list_free_full(volumes, g_object_unref);
}

bool monitor_start()
{
	monitor = g_volume_monitor_get();
	if (!monitor) {
		g_warning("Could not get volume monitor");
		return false;
	}

	g_signal_connect(monitor, "volume-added",
	                 G_CALLBACK(monitor_volume_added_cb), NULL);
	g_signal_connect(monitor, "volume-changed",
	                 G_CALLBACK(monitor_volume_changed_cb), NULL);
	g_signal_connect(monitor, "volume-removed",
	                 G_CALLBACK(monitor_volume_removed_cb), NULL);

	enumerate_volumes();

	return true;
}

void monitor_stop()
{
	g_object_unref(monitor);
}