~ubuntu-branches/ubuntu/wily/smplayer/wily

« back to all changes in this revision

Viewing changes to src/subtracks.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Maia Kozheva
  • Date: 2009-03-31 23:05:43 UTC
  • mto: (1.1.9 upstream) (3.1.2 squeeze)
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: james.westby@ubuntu.com-20090331230543-nsklbxenl2hf2n6h
ImportĀ upstreamĀ versionĀ 0.6.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  smplayer, GUI front-end for mplayer.
 
2
    Copyright (C) 2006-2009 Ricardo Villalba <rvm@escomposlinux.org>
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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
 
17
*/
 
18
 
 
19
 
 
20
#include "subtracks.h"
 
21
#include "mediasettings.h"
 
22
#include <QRegExp>
 
23
 
 
24
SubTracks::SubTracks() {
 
25
        index = 0;
 
26
}
 
27
 
 
28
 
 
29
SubTracks::~SubTracks() {
 
30
}
 
31
 
 
32
void SubTracks::clear() {
 
33
        subs.clear();
 
34
}
 
35
 
 
36
void SubTracks::add( SubData::Type t, int ID ) {
 
37
        SubData d;
 
38
        d.setType(t);
 
39
        d.setID(ID);
 
40
 
 
41
        subs.append(d);
 
42
}
 
43
 
 
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() );
 
49
        }
 
50
}
 
51
 
 
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() );
 
56
        }
 
57
}
 
58
 
 
59
int SubTracks::numItems() {
 
60
        return subs.count();
 
61
}
 
62
 
 
63
bool SubTracks::existsItemAt(int n) {
 
64
        return ((n > 0) && (n < numItems()));
 
65
}
 
66
 
 
67
int SubTracks::findLang(QString expr) {
 
68
        qDebug( "SubTracks::findLang: '%s'", expr.toUtf8().data());
 
69
        QRegExp rx( expr );
 
70
 
 
71
        int res_id = -1;
 
72
 
 
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!");
 
78
                        res_id = n;
 
79
                        break;  
 
80
                }
 
81
        }
 
82
 
 
83
        return res_id;
 
84
}
 
85
 
 
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; 
 
90
 
 
91
        if (numItems() > 0) {
 
92
                sub = 0; // First subtitle
 
93
                if (existsItemAt(default_sub)) {
 
94
                        sub = default_sub;
 
95
                }
 
96
 
 
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;
 
101
                }
 
102
        }
 
103
        return sub;
 
104
}
 
105
 
 
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 ) ) {
 
109
                        return n;
 
110
                }
 
111
        }
 
112
        qDebug("SubTracks::find: item type: %d, ID: %d doesn't exist", t, ID);
 
113
        return -1;
 
114
}
 
115
 
 
116
SubData SubTracks::findItem( SubData::Type t, int ID ) {
 
117
        SubData sub;
 
118
        int n = find(t,ID);
 
119
        if ( n != -1 ) 
 
120
                return subs[n];
 
121
        else
 
122
                return sub;
 
123
}
 
124
 
 
125
SubData SubTracks::itemAt( int n ) {
 
126
        if (n >= 0 && n < subs.count()) {
 
127
                return subs[n];
 
128
        } else {
 
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");
 
132
 
 
133
                SubData empty_sub;
 
134
                return empty_sub;
 
135
        }
 
136
}
 
137
 
 
138
bool SubTracks::changeLang( SubData::Type t, int ID, QString lang ) {
 
139
        int f = find(t,ID);
 
140
        if (f == -1) return false;
 
141
 
 
142
        subs[f].setLang(lang);
 
143
        return true;
 
144
}
 
145
 
 
146
bool SubTracks::changeName( SubData::Type t, int ID, QString name ) {
 
147
        int f = find(t,ID);
 
148
        if (f == -1) return false;
 
149
 
 
150
        subs[f].setName(name);
 
151
        return true;
 
152
}
 
153
 
 
154
bool SubTracks::changeFilename( SubData::Type t, int ID, QString filename ) {
 
155
        int f = find(t,ID);
 
156
        if (f == -1) return false;
 
157
 
 
158
        subs[f].setFilename(filename);
 
159
        return true;
 
160
}
 
161
 
 
162
SubTracks::ParseResult SubTracks::parse(QString text) {
 
163
        qDebug("SubTracks::parse: '%s'", text.toUtf8().data());
 
164
 
 
165
        ParseResult result = SubtitleUnchanged;
 
166
 
 
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=(.*)");
 
170
 
 
171
        if (rx_subtitle.indexIn(text) > -1) {
 
172
                int ID = rx_subtitle.cap(2).toInt();
 
173
                QString type = rx_subtitle.cap(1);
 
174
 
 
175
                SubData::Type t;
 
176
                if (type == "FILE_SUB") t = SubData::File;
 
177
                else
 
178
                if (type == "VOBSUB") t = SubData::Vob;
 
179
                else
 
180
                        t = SubData::Sub;
 
181
 
 
182
                if (find(t, ID) > -1) {
 
183
                        qWarning("SubTracks::parse: subtitle type: %d, ID: %d already exists!", t, ID);
 
184
                } else {
 
185
                        add(t,ID);
 
186
 
 
187
                        result = SubtitleAdded;
 
188
                }       
 
189
        }
 
190
        else
 
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);
 
196
 
 
197
                SubData::Type t = SubData::Sub;
 
198
                if (type == "VSID") t = SubData::Vob;
 
199
 
 
200
                if (find(t, ID) == -1) {
 
201
                        qWarning("SubTracks::parse: subtitle type: %d, ID: %d doesn't exist!", t, ID);
 
202
                } else {
 
203
                        if (attr=="NAME")
 
204
                                changeName(t,ID, value);
 
205
                        else
 
206
                                changeLang(t,ID, value);
 
207
 
 
208
                        result = SubtitleChanged;
 
209
                }
 
210
        }
 
211
        else
 
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 );
 
218
 
 
219
                                result = SubtitleChanged;
 
220
                        }
 
221
                }
 
222
        }
 
223
 
 
224
        return result;
 
225
}
 
226
 
 
227
/*
 
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");
 
235
 
 
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");
 
240
 
 
241
        process("ID_FILE_SUB_ID=1");
 
242
        process("ID_FILE_SUB_FILENAME=./lost313_es.sub");
 
243
 
 
244
        list();
 
245
        listNames();
 
246
}
 
247
*/