hythmbox-2.98/lib/libmediaplayerid/mpid-files.c

No issues found

 1 /*
 2  * Copyright (C) 2009 Jonathan Matthew  <jonathan@d14n.org>
 3  *
 4  * This library is free software; you can redistribute it and/or
 5  * modify it under the terms of the GNU Lesser General Public
 6  * License as published by the Free Software Foundation; either
 7  * version 2 of the License, or (at your option) any later version.
 8  *
 9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 
20 #include <glib.h>
21 
22 #include "mediaplayerid.h"
23 #include "mpid-private.h"
24 
25 void
26 mpid_read_device_file (MPIDDevice *device, const char *device_info_path)
27 {
28 	GError *error = NULL;
29 	GKeyFile *keyfile;
30 
31 	keyfile = g_key_file_new ();
32 	if (g_key_file_load_from_file (keyfile, device_info_path, G_KEY_FILE_NONE, &error) == FALSE) {
33 		mpid_debug ("unable to read device info file %s: %s\n", device_info_path, error->message);
34 		g_clear_error (&error);
35 		device->error = MPID_ERROR_DEVICE_INFO_MISSING;
36 		return;
37 	}
38 
39 	mpid_override_strv_from_keyfile (&device->access_protocols, keyfile, "Device", "AccessProtocol");
40 
41 	mpid_override_strv_from_keyfile (&device->output_formats, keyfile, "Media", "OutputFormats");
42 	mpid_override_strv_from_keyfile (&device->input_formats, keyfile, "Media", "InputFormats");
43 
44 	mpid_override_strv_from_keyfile (&device->playlist_formats, keyfile, "Playlist", "Formats");
45 
46 	mpid_override_strv_from_keyfile (&device->audio_folders, keyfile, "storage", "AudioFolders");
47 
48 	mpid_override_string_from_keyfile (&device->playlist_path, keyfile, "storage", "PlaylistPath");
49 	mpid_override_string_from_keyfile (&device->drive_type, keyfile, "storage", "DriveType");
50 
51 	if (g_key_file_has_key (keyfile, "storage", "RequiresEject", NULL)) {
52 		device->requires_eject = g_key_file_get_boolean (keyfile, "storage", "RequiresEject", NULL);
53 	}
54 
55 	mpid_override_string_from_keyfile (&device->model, keyfile, "Device", "Model");
56 	mpid_override_string_from_keyfile (&device->vendor, keyfile, "Vendor", "Model");
57 
58 	if (g_key_file_has_key (keyfile, "storage", "FolderDepth", NULL)) {
59 		int val = g_key_file_get_integer (keyfile, "storage", "FolderDepth", &error);
60 		if (error == NULL) {
61 			device->folder_depth = val;
62 		} else {
63 			g_clear_error (&error);
64 			device->folder_depth = -1;	/* hmm. */
65 		}
66 	}
67 
68 	g_key_file_free (keyfile);
69 }
70 
71 void
72 mpid_find_and_read_device_file (MPIDDevice *device, const char *device_file)
73 {
74 	const char * const *data_dirs;
75 	int i;
76 
77 	data_dirs = g_get_system_data_dirs ();
78 	for (i = 0; data_dirs[i] != NULL; i++) {
79 		char *filename;
80 		char *path;
81 
82 		filename = g_strdup_printf ("%s.mpi", device_file);
83 		path = g_build_filename (data_dirs[i], "media-player-info", filename, NULL);
84 		g_free (filename);
85 		if (g_file_test (path, G_FILE_TEST_EXISTS)) {
86 			device->source = MPID_SOURCE_SYSTEM;
87 			mpid_read_device_file (device, path);
88 			g_free (path);
89 			return;
90 		}
91 		g_free (path);
92 	}
93 
94 	/* device info file is missing */
95 	mpid_debug ("unable to find device info file %s\n", device_file);
96 	device->error = MPID_ERROR_DEVICE_INFO_MISSING;
97 }