File: udisksopticaldisc.cpp

package info (click to toggle)
cantata 3.4.0.ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,428 kB
  • sloc: cpp: 109,584; perl: 1,366; xml: 722; python: 139; lex: 110; sh: 105; yacc: 78; makefile: 8
file content (263 lines) | stat: -rw-r--r-- 8,657 bytes parent folder | download
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
    Copyright 2010 Michael Zanetti <mzanetti@kde.org>
    Copyright 2010, 2011 Lukas Tinkl <ltinkl@redhat.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) version 3, or any
    later version accepted by the membership of KDE e.V. (or its
    successor approved by the membership of KDE e.V.), which shall
    act as a proxy defined in Section 6 of version 3 of the license.

    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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <QFile>
#include <QMap>
#include <QMutexLocker>

#include "soliddefs_p.h"
#include "udisksopticaldisc.h"

typedef QMap<QString, Solid::OpticalDisc::ContentTypes> ContentTypesCache;
SOLID_GLOBAL_STATIC(ContentTypesCache, cache)
SOLID_GLOBAL_STATIC(QMutex, cacheLock)

// inspired by http://cgit.freedesktop.org/hal/tree/hald/linux/probing/probe-volume.c
static Solid::OpticalDisc::ContentType advancedDiscDetect(const QString& device_file)
{
	/* the discs block size */
	unsigned short bs;
	/* the path table size */
	unsigned short ts;
	/* the path table location (in blocks) */
	unsigned int tl;
	/* length of the directory name in current path table entry */
	unsigned char len_di = 0;
	/* the number of the parent directory's path table entry */
	unsigned int parent = 0;
	/* filename for the current path table entry */
	char dirname[256];
	/* our position into the path table */
	int pos = 0;
	/* the path table record we're on */
	int curr_record = 1;

	Solid::OpticalDisc::ContentType result = Solid::OpticalDisc::NoContent;

	int fd = open(QFile::encodeName(device_file), O_RDONLY);

	/* read the block size */
	lseek(fd, 0x8080, SEEK_CUR);
	if (read(fd, &bs, 2) != 2) {
		qDebug("Advanced probing on %s failed while reading block size", qPrintable(device_file));
		goto out;
	}

	/* read in size of path table */
	lseek(fd, 2, SEEK_CUR);
	if (read(fd, &ts, 2) != 2) {
		qDebug("Advanced probing on %s failed while reading path table size", qPrintable(device_file));
		goto out;
	}

	/* read in which block path table is in */
	lseek(fd, 6, SEEK_CUR);
	if (read(fd, &tl, 4) != 4) {
		qDebug("Advanced probing on %s failed while reading path table block", qPrintable(device_file));
		goto out;
	}

	/* seek to the path table */
	lseek(fd, bs * tl, SEEK_SET);

	/* loop through the path table entries */
	while (pos < ts) {
		/* get the length of the filename of the current entry */
		if (read(fd, &len_di, 1) != 1) {
			qDebug("Advanced probing on %s failed, cannot read more entries", qPrintable(device_file));
			break;
		}

		/* get the record number of this entry's parent
           i'm pretty sure that the 1st entry is always the top directory */
		lseek(fd, 5, SEEK_CUR);
		if (read(fd, &parent, 2) != 2) {
			qDebug("Advanced probing on %s failed, couldn't read parent entry", qPrintable(device_file));
			break;
		}

		/* read the name */
		if (read(fd, dirname, len_di) != len_di) {
			qDebug("Advanced probing on %s failed, couldn't read the entry name", qPrintable(device_file));
			break;
		}
		dirname[len_di] = 0;

		/* if we found a folder that has the root as a parent, and the directory name matches
           one of the special directories then set the properties accordingly */
		if (parent == 1) {
			if (!strcasecmp(dirname, "VIDEO_TS")) {
				qDebug("Disc in %s is a Video DVD", qPrintable(device_file));
				result = Solid::OpticalDisc::VideoDvd;
				break;
			}
			else if (!strcasecmp(dirname, "BDMV")) {
				qDebug("Disc in %s is a Blu-ray video disc", qPrintable(device_file));
				result = Solid::OpticalDisc::VideoBluRay;
				break;
			}
			else if (!strcasecmp(dirname, "VCD")) {
				qDebug("Disc in %s is a Video CD", qPrintable(device_file));
				result = Solid::OpticalDisc::VideoCd;
				break;
			}
			else if (!strcasecmp(dirname, "SVCD")) {
				qDebug("Disc in %s is a Super Video CD", qPrintable(device_file));
				result = Solid::OpticalDisc::SuperVideoCd;
				break;
			}
		}

		/* all path table entries are padded to be even,
           so if this is an odd-length table, seek a byte to fix it */
		if (len_di % 2 == 1) {
			lseek(fd, 1, SEEK_CUR);
			pos++;
		}

		/* update our position */
		pos += 8 + len_di;
		curr_record++;
	}

	close(fd);
	return result;

out:
	/* go back to the start of the file */
	lseek(fd, 0, SEEK_SET);
	close(fd);
	return result;
}

using namespace Solid::Backends::UDisks;

OpticalDisc::OpticalDisc(UDisksDevice* device)
	: UDisksStorageVolume(device), m_needsReprobe(true), m_cachedContent(Solid::OpticalDisc::NoContent)
{
	connect(device, SIGNAL(changed()), this, SLOT(slotChanged()));
}

OpticalDisc::~OpticalDisc()
{
}

qulonglong OpticalDisc::capacity() const
{
	return m_device->prop("DeviceSize").toULongLong();
}

bool OpticalDisc::isRewritable() const
{
	// the hard way, udisks has no notion of a disc "rewritability"
	const QString mediaType = m_device->prop("DriveMedia").toString();
	return mediaType == "optical_cd_rw" || mediaType == "optical_dvd_rw" || mediaType == "optical_dvd_ram" || mediaType == "optical_dvd_plus_rw" || mediaType == "optical_dvd_plus_rw_dl" || mediaType == "optical_bd_re" || mediaType == "optical_hddvd_rw";// TODO check completeness
}

bool OpticalDisc::isBlank() const
{
	return m_device->prop("OpticalDiscIsBlank").toBool();
}

bool OpticalDisc::isAppendable() const
{
	return m_device->prop("OpticalDiscIsAppendable").toBool();
}

Solid::OpticalDisc::DiscType OpticalDisc::discType() const
{
	const QString discType = m_device->prop("DriveMedia").toString();

	QMap<Solid::OpticalDisc::DiscType, QString> map;
	map[Solid::OpticalDisc::CdRom] = "optical_cd";
	map[Solid::OpticalDisc::CdRecordable] = "optical_cd_r";
	map[Solid::OpticalDisc::CdRewritable] = "optical_cd_rw";
	map[Solid::OpticalDisc::DvdRom] = "optical_dvd";
	map[Solid::OpticalDisc::DvdRecordable] = "optical_dvd_r";
	map[Solid::OpticalDisc::DvdRewritable] = "optical_dvd_rw";
	map[Solid::OpticalDisc::DvdRam] = "optical_dvd_ram";
	map[Solid::OpticalDisc::DvdPlusRecordable] = "optical_dvd_plus_r";
	map[Solid::OpticalDisc::DvdPlusRewritable] = "optical_dvd_plus_rw";
	map[Solid::OpticalDisc::DvdPlusRecordableDuallayer] = "optical_dvd_plus_r_dl";
	map[Solid::OpticalDisc::DvdPlusRewritableDuallayer] = "optical_dvd_plus_rw_dl";
	map[Solid::OpticalDisc::BluRayRom] = "optical_bd";
	map[Solid::OpticalDisc::BluRayRecordable] = "optical_bd_r";
	map[Solid::OpticalDisc::BluRayRewritable] = "optical_bd_re";
	map[Solid::OpticalDisc::HdDvdRom] = "optical_hddvd";
	map[Solid::OpticalDisc::HdDvdRecordable] = "optical_hddvd_r";
	map[Solid::OpticalDisc::HdDvdRewritable] = "optical_hddvd_rw";
	// TODO add these to Solid
	//map[Solid::OpticalDisc::MagnetoOptical] ="optical_mo";
	//map[Solid::OpticalDisc::MountRainer] ="optical_mrw";
	//map[Solid::OpticalDisc::MountRainerWritable] ="optical_mrw_w";

	return map.key(discType, Solid::OpticalDisc::UnknownDiscType);
}

Solid::OpticalDisc::ContentTypes OpticalDisc::availableContent() const
{
	if (isBlank()) {
		m_needsReprobe = false;
		return Solid::OpticalDisc::NoContent;
	}

	if (m_needsReprobe) {
		QMutexLocker lock(cacheLock);

		QString deviceFile = m_device->prop("DeviceFile").toString();

		if (cache->contains(deviceFile)) {
			m_cachedContent = cache->value(deviceFile);
			m_needsReprobe = false;
			return m_cachedContent;
		}

		m_cachedContent = Solid::OpticalDisc::NoContent;
		bool hasData = m_device->prop("OpticalDiscNumTracks").toInt() > 0 && m_device->prop("OpticalDiscNumTracks").toInt() > m_device->prop("OpticalDiscNumAudioTracks").toInt();
		bool hasAudio = m_device->prop("OpticalDiscNumAudioTracks").toInt() > 0;

		if (hasData) {
			m_cachedContent |= Solid::OpticalDisc::Data;
			m_cachedContent |= advancedDiscDetect(deviceFile);
		}
		if (hasAudio)
			m_cachedContent |= Solid::OpticalDisc::Audio;

		m_needsReprobe = false;

		cache->insert(deviceFile, m_cachedContent);
	}

	return m_cachedContent;
}

void OpticalDisc::slotChanged()
{
	QMutexLocker lock(cacheLock);
	m_needsReprobe = true;
	m_cachedContent = Solid::OpticalDisc::NoContent;
	cache->remove(m_device->prop("DeviceFile").toString());
}