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

« back to all changes in this revision

Viewing changes to src/corelib/subtracks.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Matvey Kozhev
  • Date: 2008-05-30 12:52:19 UTC
  • mto: (1.1.7 upstream) (3.1.1 lenny)
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: james.westby@ubuntu.com-20080530125219-ky15vano4ps1ppnk
ImportĀ upstreamĀ versionĀ 0.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  smplayer, GUI front-end for mplayer.
 
2
    Copyright (C) 2006-2008 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
        return subs[n];
 
127
}
 
128
 
 
129
bool SubTracks::changeLang( SubData::Type t, int ID, QString lang ) {
 
130
        int f = find(t,ID);
 
131
        if (f == -1) return false;
 
132
 
 
133
        subs[f].setLang(lang);
 
134
        return true;
 
135
}
 
136
 
 
137
bool SubTracks::changeName( SubData::Type t, int ID, QString name ) {
 
138
        int f = find(t,ID);
 
139
        if (f == -1) return false;
 
140
 
 
141
        subs[f].setName(name);
 
142
        return true;
 
143
}
 
144
 
 
145
bool SubTracks::changeFilename( SubData::Type t, int ID, QString filename ) {
 
146
        int f = find(t,ID);
 
147
        if (f == -1) return false;
 
148
 
 
149
        subs[f].setFilename(filename);
 
150
        return true;
 
151
}
 
152
 
 
153
void SubTracks::process(QString text) {
 
154
        qDebug("SubTracks::process: '%s'", text.toUtf8().data());
 
155
 
 
156
        QRegExp rx_subtitle("^ID_(SUBTITLE|FILE_SUB|VOBSUB)_ID=(\\d+)");
 
157
        QRegExp rx_sid("^ID_(SID|VSID)_(\\d+)_(LANG|NAME)=(.*)");
 
158
        QRegExp rx_subtitle_file("^ID_FILE_SUB_FILENAME=(.*)");
 
159
 
 
160
        if (rx_subtitle.indexIn(text) > -1) {
 
161
                int ID = rx_subtitle.cap(2).toInt();
 
162
                QString type = rx_subtitle.cap(1);
 
163
 
 
164
                SubData::Type t;
 
165
                if (type == "FILE_SUB") t = SubData::File;
 
166
                else
 
167
                if (type == "VOBSUB") t = SubData::Vob;
 
168
                else
 
169
                        t = SubData::Sub;
 
170
 
 
171
                if (find(t, ID) > -1) {
 
172
                        qWarning("SubTracks::process: subtitle type: %d, ID: %d already exists!", t, ID);
 
173
                } else {
 
174
                        add(t,ID);
 
175
                }       
 
176
        }
 
177
        else
 
178
        if (rx_sid.indexIn(text) > -1) {
 
179
                int ID = rx_sid.cap(2).toInt();
 
180
                QString value = rx_sid.cap(4);
 
181
                QString attr = rx_sid.cap(3);
 
182
                QString type = rx_sid.cap(1);
 
183
 
 
184
                SubData::Type t = SubData::Sub;
 
185
                if (type == "VSID") t = SubData::Vob;
 
186
 
 
187
                if (find(t, ID) == -1) {
 
188
                        qWarning("SubTracks::process: subtitle type: %d, ID: %d doesn't exist!", t, ID);
 
189
                } else {
 
190
                        if (attr=="NAME")
 
191
                                changeName(t,ID, value);
 
192
                        else
 
193
                                changeLang(t,ID, value);
 
194
                }       
 
195
        }
 
196
        else
 
197
        if (rx_subtitle_file.indexIn(text) > -1) {
 
198
                QString file = rx_subtitle_file.cap(1);
 
199
                if ( subs.count() > 0 ) {
 
200
                        int last = subs.count() -1;
 
201
                        if (subs[last].type() == SubData::File) {
 
202
                                subs[last].setFilename( file );
 
203
                        }
 
204
                }
 
205
        }
 
206
}
 
207
 
 
208
/*
 
209
void SubTracks::test() {
 
210
        process("ID_SUBTITLE_ID=0");
 
211
        process("ID_SID_0_NAME=Arabic");
 
212
        process("ID_SID_0_LANG=ara");
 
213
        process("ID_SUBTITLE_ID=1");
 
214
        process("ID_SID_1_NAME=Catalan");
 
215
        process("ID_SID_1_LANG=cat");
 
216
 
 
217
        process("ID_VOBSUB_ID=0");
 
218
        process("ID_VSID_0_LANG=en");
 
219
        process("ID_VOBSUB_ID=1");
 
220
        process("ID_VSID_1_LANG=fr");
 
221
 
 
222
        process("ID_FILE_SUB_ID=1");
 
223
        process("ID_FILE_SUB_FILENAME=./lost313_es.sub");
 
224
 
 
225
        list();
 
226
        listNames();
 
227
}
 
228
*/