summaryrefslogtreecommitdiff
path: root/utils/playlists/playlist2ttl.c
blob: 2bb7f1ff68041edb3c1332ed0b2319c5e427c623 (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
/*
 * Copyright (C) 2009, Nokia <ivan.frade@nokia.com>
 *
 * 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 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.h>
#include <gio/gio.h>

#include <totem-pl-parser.h>

static gchar **filenames = NULL;

typedef struct {
	gint   tracks ;
	gchar *playlist;
} PlaylistData;

static GOptionEntry entries[] = {
	{ G_OPTION_REMAINING, 0, G_OPTION_FLAG_FILENAME, G_OPTION_ARG_FILENAME_ARRAY, &filenames,
	  "FILE",
	  NULL
	},
	{ NULL }
};

static void
print_header (void)
{
	g_print ("@prefix nmo: <http://www.semanticdesktop.org/ontologies/2007/03/22/nmo#>.\n");
	g_print ("@prefix nfo: <http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#>.\n");
	g_print ("@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.\n");
	g_print ("\n");
}

static void
print_playlist_entry (const gchar *uri)
{
	g_print ("<%s> a nmm:Playlist .\n\n", uri);
}

static void
entry_parsed (TotemPlParser *parser, 
              const gchar   *uri, 
              GHashTable    *metadata, 
              gpointer       user_data)
{
	PlaylistData *playlist_data = (PlaylistData*) user_data;

	playlist_data->tracks += 1;

	g_print ("<%s> nfo:hasMediaFileListEntry [ \n",
	         playlist_data->playlist);
	g_print ("\t a nfo:MediaFileListEntry ; \n");
	g_print ("\t nfo:listPosition %d ; \n", playlist_data->tracks);
	g_print ("\t nfo:entryUrl \"%s\" ] .\n\n", uri);
}

int
main (int argc, char **argv)
{
	GFile *file;
	GOptionContext *context = NULL;
	gchar *uri;
	PlaylistData playlist_data = { 0, NULL};
	TotemPlParser *pl;
	TotemPlParserResult result;
	GError *error = NULL;

	context = g_option_context_new ("- Parse a playlist and show info");

	g_option_context_add_main_entries (context, entries, NULL);
	g_option_context_parse (context, &argc, &argv, NULL);

	if (!g_option_context_parse (context, &argc, &argv, &error) || !filenames) {
		gchar *help;

		g_printerr ("%s\n\n", "Playlist filename is mandatory");

		help = g_option_context_get_help (context, TRUE, NULL);
		g_option_context_free (context);
		g_printerr ("%s", help);
		g_free (help);

		return -1;
	}

	file = g_file_new_for_commandline_arg (filenames[0]);
	uri = g_file_get_uri (file);

	print_header ();
	print_playlist_entry (uri);
	playlist_data.playlist = uri;

	pl = totem_pl_parser_new ();

	g_object_set (pl, "recurse", FALSE, "disable-unsafe", TRUE, NULL);
	g_signal_connect (G_OBJECT (pl), "entry-parsed", G_CALLBACK (entry_parsed), &playlist_data);

	result = totem_pl_parser_parse (pl, uri, FALSE);

	switch (result) {
	case TOTEM_PL_PARSER_RESULT_SUCCESS:
		break;
	case TOTEM_PL_PARSER_RESULT_IGNORED:
		g_print ("Error: Ignored (%s)\n", uri);
		break;
	case TOTEM_PL_PARSER_RESULT_ERROR:
		g_print ("Error: Failed parsing (%s)\n", uri);
		break;
	case TOTEM_PL_PARSER_RESULT_UNHANDLED:
		g_print ("Error: Unhandled type (%s)\n", uri);
		break;
	default:
		g_print ("Undefined result!?!?!");
	}

	g_object_unref (pl);

	return 0;
}