1
/* smplayer, GUI front-end for mplayer.
2
Copyright (C) 2006-2009 Ricardo Villalba <rvm@escomposlinux.org>
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation; either version 2 of the License, or
7
(at your option) any later version.
9
This program 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
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#include "subtracks.h"
21
#include "mediasettings.h"
24
SubTracks::SubTracks() {
29
SubTracks::~SubTracks() {
32
void SubTracks::clear() {
36
void SubTracks::add( SubData::Type t, int ID ) {
44
void SubTracks::list() {
45
for (unsigned int n=0; n < subs.count(); n++) {
46
qDebug("SubTracks::list: item %d: type: %d ID: %d lang: '%s' name: '%s' filename: '%s'",
47
n, subs[n].type(), subs[n].ID(), subs[n].lang().toUtf8().data(),
48
subs[n].name().toUtf8().data(), subs[n].filename().toUtf8().data() );
52
void SubTracks::listNames() {
53
for (unsigned int n=0; n < subs.count(); n++) {
54
qDebug("SubTracks::list: item %d: '%s'",
55
n, subs[n].displayName().toUtf8().data() );
59
int SubTracks::numItems() {
63
bool SubTracks::existsItemAt(int n) {
64
return ((n > 0) && (n < numItems()));
67
int SubTracks::findLang(QString expr) {
68
qDebug( "SubTracks::findLang: '%s'", expr.toUtf8().data());
73
for (int n=0; n < numItems(); n++) {
74
qDebug("SubTracks::findLang: lang #%d '%s'", n,
75
subs[n].lang().toUtf8().data());
76
if (rx.indexIn( subs[n].lang() ) > -1) {
77
qDebug("SubTracks::findLang: found preferred lang!");
86
// Return first subtitle or the user preferred (if found)
87
// or none if there's no subtitles
88
int SubTracks::selectOne(QString preferred_lang, int default_sub) {
89
int sub = MediaSettings::SubNone;
92
sub = 0; // First subtitle
93
if (existsItemAt(default_sub)) {
97
// Check if one of the subtitles is the user preferred.
98
if (!preferred_lang.isEmpty()) {
99
int res = findLang( preferred_lang );
100
if (res != -1) sub = res;
106
int SubTracks::find( SubData::Type t, int ID ) {
107
for (unsigned int n=0; n < subs.count(); n++) {
108
if ( ( subs[n].type() == t ) && ( subs[n].ID() == ID ) ) {
112
qDebug("SubTracks::find: item type: %d, ID: %d doesn't exist", t, ID);
116
SubData SubTracks::findItem( SubData::Type t, int ID ) {
125
SubData SubTracks::itemAt( int n ) {
126
if (n >= 0 && n < subs.count()) {
129
qWarning("SubTracks::itemAt: %d out of range!", n);
130
qWarning("SubTracks::itemAt: returning an empty sub to avoid a crash");
131
qWarning("SubTracks::itemAt: this shouldn't happen, report a bug if you see this");
138
bool SubTracks::changeLang( SubData::Type t, int ID, QString lang ) {
140
if (f == -1) return false;
142
subs[f].setLang(lang);
146
bool SubTracks::changeName( SubData::Type t, int ID, QString name ) {
148
if (f == -1) return false;
150
subs[f].setName(name);
154
bool SubTracks::changeFilename( SubData::Type t, int ID, QString filename ) {
156
if (f == -1) return false;
158
subs[f].setFilename(filename);
162
SubTracks::ParseResult SubTracks::parse(QString text) {
163
qDebug("SubTracks::parse: '%s'", text.toUtf8().data());
165
ParseResult result = SubtitleUnchanged;
167
QRegExp rx_subtitle("^ID_(SUBTITLE|FILE_SUB|VOBSUB)_ID=(\\d+)");
168
QRegExp rx_sid("^ID_(SID|VSID)_(\\d+)_(LANG|NAME)=(.*)");
169
QRegExp rx_subtitle_file("^ID_FILE_SUB_FILENAME=(.*)");
171
if (rx_subtitle.indexIn(text) > -1) {
172
int ID = rx_subtitle.cap(2).toInt();
173
QString type = rx_subtitle.cap(1);
176
if (type == "FILE_SUB") t = SubData::File;
178
if (type == "VOBSUB") t = SubData::Vob;
182
if (find(t, ID) > -1) {
183
qWarning("SubTracks::parse: subtitle type: %d, ID: %d already exists!", t, ID);
187
result = SubtitleAdded;
191
if (rx_sid.indexIn(text) > -1) {
192
int ID = rx_sid.cap(2).toInt();
193
QString value = rx_sid.cap(4);
194
QString attr = rx_sid.cap(3);
195
QString type = rx_sid.cap(1);
197
SubData::Type t = SubData::Sub;
198
if (type == "VSID") t = SubData::Vob;
200
if (find(t, ID) == -1) {
201
qWarning("SubTracks::parse: subtitle type: %d, ID: %d doesn't exist!", t, ID);
204
changeName(t,ID, value);
206
changeLang(t,ID, value);
208
result = SubtitleChanged;
212
if (rx_subtitle_file.indexIn(text) > -1) {
213
QString file = rx_subtitle_file.cap(1);
214
if ( subs.count() > 0 ) {
215
int last = subs.count() -1;
216
if (subs[last].type() == SubData::File) {
217
subs[last].setFilename( file );
219
result = SubtitleChanged;
228
void SubTracks::test() {
229
process("ID_SUBTITLE_ID=0");
230
process("ID_SID_0_NAME=Arabic");
231
process("ID_SID_0_LANG=ara");
232
process("ID_SUBTITLE_ID=1");
233
process("ID_SID_1_NAME=Catalan");
234
process("ID_SID_1_LANG=cat");
236
process("ID_VOBSUB_ID=0");
237
process("ID_VSID_0_LANG=en");
238
process("ID_VOBSUB_ID=1");
239
process("ID_VSID_1_LANG=fr");
241
process("ID_FILE_SUB_ID=1");
242
process("ID_FILE_SUB_FILENAME=./lost313_es.sub");