diff --git a/mythtv/libs/libmythtv/textsubtitleparser.cpp b/mythtv/libs/libmythtv/textsubtitleparser.cpp
index 28265c6..3e64e2e 100644
a
|
b
|
public: |
99 | 99 | { |
100 | 100 | m_remoteFile = NULL; |
101 | 101 | m_localFile = new QFile(filename); |
102 | | if (!m_localFile->open(QIODevice::ReadOnly | QIODevice::Text)) |
| 102 | if (!m_localFile->open(QIODevice::ReadOnly)) |
103 | 103 | { |
104 | 104 | delete m_localFile; |
105 | 105 | m_localFile = NULL; |
… |
… |
void TextSubtitleParser::LoadSubtitles(const QString &fileName, |
318 | 318 | LOG(VB_VBI, LOG_INFO, |
319 | 319 | QString("Finished reading %1 subtitle bytes (requested %2)") |
320 | 320 | .arg(numread).arg(new_len)); |
321 | | subtitle_t *loaded_subs = sub_read_file(&sub_data); |
322 | | if (!loaded_subs) |
| 321 | |
| 322 | // try to determine the text codec |
| 323 | QByteArray test(sub_data.rbuffer_text, sub_data.rbuffer_len); |
| 324 | QTextCodec *textCodec = QTextCodec::codecForUtfText(test, NULL); |
| 325 | if (!textCodec) |
| 326 | { |
| 327 | LOG(VB_VBI, LOG_WARNING, "Failed to autodetect a UTF encoding."); |
| 328 | QString codec = gCoreContext->GetSetting("SubtitleCodec", ""); |
| 329 | if (!codec.isEmpty()) |
| 330 | textCodec = QTextCodec::codecForName(codec.toLatin1()); |
| 331 | if (!textCodec) |
| 332 | textCodec = QTextCodec::codecForName("utf-8"); |
| 333 | if (!textCodec) |
| 334 | { |
| 335 | LOG(VB_VBI, LOG_ERR, |
| 336 | QString("Failed to find codec for subtitle file '%1'") |
| 337 | .arg(fileName)); |
| 338 | return; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | LOG(VB_VBI, LOG_INFO, QString("Opened subtitle file '%1' with codec '%2'") |
| 343 | .arg(fileName).arg(textCodec->name().constData())); |
| 344 | |
| 345 | // load the entire subtitle file, converting to unicode as we go |
| 346 | QScopedPointer<QTextDecoder> dec(textCodec->makeDecoder(/*QTextCodec::IgnoreHeader*/)); |
| 347 | QString data = dec->toUnicode(sub_data.rbuffer_text, sub_data.rbuffer_len); |
| 348 | if (data.isEmpty()) |
323 | 349 | { |
324 | | delete[] sub_data.rbuffer_text; |
| 350 | LOG(VB_VBI, LOG_WARNING, |
| 351 | QString("Data loaded from subtitle file '%1' is empty.") |
| 352 | .arg(fileName)); |
325 | 353 | return; |
326 | 354 | } |
327 | 355 | |
328 | | target.SetFrameBasedTiming(!sub_data.uses_time); |
329 | | target.Clear(); |
| 356 | // convert back to utf-8 for parsing |
| 357 | QByteArray ba = data.toUtf8(); |
| 358 | //delete[] sub_data.rbuffer_text; |
| 359 | sub_data.rbuffer_text = (char*)ba.data(); |
| 360 | sub_data.rbuffer_len = ba.size(); |
| 361 | LOG(VB_VBI, LOG_INFO, QString("UTF-8 subs: %1").arg(QString(sub_data.rbuffer_text).replace(QChar(0), "NUL"))); |
330 | 362 | |
331 | | QTextCodec *textCodec = NULL; |
332 | | QString codec = gCoreContext->GetSetting("SubtitleCodec", ""); |
333 | | if (!codec.isEmpty()) |
334 | | textCodec = QTextCodec::codecForName(codec.toLatin1()); |
335 | | if (!textCodec) |
336 | | textCodec = QTextCodec::codecForName("utf-8"); |
337 | | if (!textCodec) |
| 363 | subtitle_t *loaded_subs = sub_read_file(&sub_data); |
| 364 | if (!loaded_subs) |
338 | 365 | { |
339 | | delete[] sub_data.rbuffer_text; |
| 366 | //delete[] sub_data.rbuffer_text; |
| 367 | LOG(VB_VBI, LOG_ERR, QString("Failed to read subtitles from '%1'") |
| 368 | .arg(fileName)); |
340 | 369 | return; |
341 | 370 | } |
342 | 371 | |
343 | | QTextDecoder *dec = textCodec->makeDecoder(); |
| 372 | LOG(VB_VBI, LOG_INFO, QString("Found %1 subtitles in file '%2'") |
| 373 | .arg(sub_data.num).arg(fileName)); |
| 374 | target.SetFrameBasedTiming(!sub_data.uses_time); |
| 375 | target.Clear(); |
| 376 | |
| 377 | // convert the subtitles to our own format, free the original structures |
| 378 | // and convert back to unicode |
| 379 | textCodec = QTextCodec::codecForName("utf-8"); |
| 380 | if (textCodec) |
| 381 | dec.reset(textCodec->makeDecoder()); |
344 | 382 | |
345 | | // convert the subtitles to our own format and free the original structures |
346 | 383 | for (int sub_i = 0; sub_i < sub_data.num; ++sub_i) |
347 | 384 | { |
348 | 385 | const subtitle_t *sub = &loaded_subs[sub_i]; |
… |
… |
void TextSubtitleParser::LoadSubtitles(const QString &fileName, |
357 | 394 | for (int line = 0; line < sub->lines; ++line) |
358 | 395 | { |
359 | 396 | const char *subLine = sub->text[line]; |
360 | | QString str = dec->toUnicode(subLine, strlen(subLine)); |
| 397 | QString str; |
| 398 | if (textCodec) |
| 399 | str = dec->toUnicode(subLine, strlen(subLine)); |
| 400 | else |
| 401 | str = QString(subLine); |
361 | 402 | newsub.textLines.push_back(str); |
362 | 403 | |
363 | 404 | free(sub->text[line]); |
… |
… |
void TextSubtitleParser::LoadSubtitles(const QString &fileName, |
365 | 406 | target.AddSubtitle(newsub); |
366 | 407 | } |
367 | 408 | |
368 | | delete dec; |
| 409 | //delete dec; |
369 | 410 | // textCodec object is managed by Qt, do not delete... |
370 | 411 | |
371 | 412 | free(loaded_subs); |
372 | | delete[] sub_data.rbuffer_text; |
| 413 | //delete[] sub_data.rbuffer_text; |
373 | 414 | |
374 | 415 | target.SetLastLoaded(); |
375 | 416 | } |