summaryrefslogtreecommitdiff
path: root/grecord/src/mate-recorder.c
blob: 482d014d52a51ad7ffd92364351071994a82d5ea (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Iain Holmes <iain@prettypeople.org>
 *
 * Copyright 2002, 2003, 2004, 2005 Iain Holmes
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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 Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * 4th Februrary 2005: Christian Schaller: changed license to LGPL with 
 * permission of Iain Holmes, Ronald Bultje, Leontine Binchy (SUN), Johan Dahlin *  and Joe Marcus Clarke
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>

#include <glib/gi18n.h>
#include <mateconf/mateconf-client.h>
#include <gst/gst.h>

#include "gsr-window.h"

void gsr_quit (void);
void gsr_add_recent (gchar *filename);
GtkWidget * gsr_open_window (const char *filename);

extern void mate_media_profiles_init (MateConfClient *conf);
 
static GList *windows = NULL;

/* Also used in gsr-window.c as extern MateConfClient *mateconf_client */
MateConfClient *mateconf_client = NULL;

static gboolean
delete_event_cb (GSRWindow *window,
		 gpointer data)
{
	if (!gsr_window_is_saved (window) && !gsr_discard_confirmation_dialog (window, TRUE))
		return TRUE;

	return FALSE;
}

static void
window_destroyed (GtkWidget *window,
		  gpointer data)
{
	windows = g_list_remove (windows, window);

	if (windows == NULL) {
		gtk_main_quit ();
	}
}

void
gsr_quit (void) 
{
	GList *p;

	for (p = windows; p;) {
		GSRWindow *window = p->data;

		/* p is set here instead of in the for statement,
		   because by the time we get back to the loop,
		   p will be invalid */
		p = p->next;

		if (gsr_window_is_saved (window) || gsr_discard_confirmation_dialog (window, TRUE))
			gsr_window_close (window);
	}
}

void
gsr_add_recent (gchar *filename)
{
	GtkRecentData data;
	char *groups[] = { NULL, NULL };
	char *uri;

	memset (&data, 0, sizeof (data));

	uri = g_filename_to_uri (filename, NULL, NULL);
	if (uri == NULL)
		return;

	data.mime_type = g_content_type_guess (uri, NULL, 0, NULL);
	if (data.mime_type == NULL) {
		/* No mime-type means warnings, and it breaks when adding
		 * non-GIO supported URI schemes */
		g_free (uri);
		return;
	}

	/* It's a local file */
	data.display_name = g_filename_display_basename (data.display_name);
	groups[0] = "Totem";

	data.app_name = g_strdup (g_get_application_name ());
	data.app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
	data.groups = groups;
	gtk_recent_manager_add_full (gtk_recent_manager_get_default (),
				     uri, &data);

	g_free (data.display_name);
	g_free (data.mime_type);
	g_free (data.app_name);
	g_free (data.app_exec);

}

/* Also referenced from gsr-window.c */
gint gsr_sample_count = 1;

GtkWidget *
gsr_open_window (const char *filename)
{
	GtkWidget *window;
	char *utf8_name;
	char *name;

	if (filename == NULL) {
		/* Translator comment: untitled here implies that
		 * there is no active sound sample. Any newly
		 * recorded samples will be saved to disk with this
		 * name as default value. */
		if (gsr_sample_count == 1) {
			utf8_name = g_strdup (_("Untitled"));
		} else {
			utf8_name = g_strdup_printf (_("Untitled-%d"), gsr_sample_count);
		}
		name = g_filename_from_utf8 (utf8_name, -1, NULL, NULL, NULL);
		g_free (utf8_name);
		++gsr_sample_count;
	} else {
		name = g_strdup (filename);
	}

	window = GTK_WIDGET (gsr_window_new (name));
	g_free (name);

	g_signal_connect (G_OBJECT (window), "delete-event",
			  G_CALLBACK (delete_event_cb), NULL);

	g_signal_connect (G_OBJECT (window), "destroy",
			  G_CALLBACK (window_destroyed), NULL);

	windows = g_list_prepend (windows, window);
	gtk_widget_show (window);

	return window;
}

int
main (int argc,
      char **argv)
{
	gchar **filenames = NULL;
	/* this is necessary because someone apparently forgot to add a
	 * convenient way to get the remaining arguments to the MateProgram
	 * API when adding the GOption stuff to it ... */
	const GOptionEntry entries[] = {
		{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames,
		"Special option that collects any remaining arguments for us" },
		{ NULL, }
	};

	GOptionContext *ctx;
	GError *error = NULL;

	g_thread_init (NULL);

	/* Init gettext */
	bindtextdomain (GETTEXT_PACKAGE, MATELOCALEDIR);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
	textdomain (GETTEXT_PACKAGE);

	ctx = g_option_context_new ("mate-sound-recorder");
	/* Initializes gtk during option parsing */
	g_option_context_add_group (ctx, gtk_get_option_group (TRUE));
	g_option_context_add_group (ctx, gst_init_get_option_group ());
	g_option_context_add_main_entries (ctx, entries, GETTEXT_PACKAGE);

	if (!g_option_context_parse (ctx, &argc, &argv, &error)) {
		g_printerr ("Option parsing failed: %s\n", error->message);
		g_error_free (error);
		g_option_context_free (ctx);
		return EXIT_FAILURE;
	}

	g_option_context_free (ctx);
	gtk_window_set_default_icon_name ("mate-sound-recorder");

	/* use it like a singleton */
	mateconf_client = mateconf_client_get_default ();

	/* init mate-media-profiles */
	mate_media_profiles_init (mateconf_client);

	if (filenames != NULL && filenames[0] != NULL) {
		guint i, num;

		num = g_strv_length (filenames);
		for (i = 0; i < num; ++i) {
			gsr_open_window (filenames[i]);
		}
	} else {
			gsr_open_window (NULL);
	}

	if (filenames) {
		g_strfreev (filenames);
	}

	gtk_main ();

	return 0;
}