1 /*****************************************************************************
2 ** QNapi
3 ** Copyright (C) 2008-2017 Piotr Krzemiński <pio.krzeminski@gmail.com>
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
11 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
12 **
13 *****************************************************************************/
14 
15 #include "engines/subtitledownloadengine.h"
16 #include <QDir>
17 #include <QFlags>
18 #include "subconvert/subtitleformatsregistry.h"
19 
SubtitleDownloadEngine(const QString & tmpPath)20 SubtitleDownloadEngine::SubtitleDownloadEngine(const QString& tmpPath)
21     : tmpPath(tmpPath) {}
22 
setMoviePath(const QString & path)23 void SubtitleDownloadEngine::setMoviePath(const QString& path) { movie = path; }
24 
moviePath()25 QString SubtitleDownloadEngine::moviePath() { return movie; }
26 
clearSubtitlesList()27 void SubtitleDownloadEngine::clearSubtitlesList() {
28   foreach (SubtitleInfo s, subtitlesList) {
29     if (QFile::exists(s.sourceLocation)) QFile::remove(s.sourceLocation);
30   }
31   subtitlesList.clear();
32 }
33 
resolveById(QUuid id)34 Maybe<SubtitleInfo> SubtitleDownloadEngine::resolveById(QUuid id) {
35   foreach (SubtitleInfo s, subtitlesList) {
36     if (s.id == id) return just(s);
37   }
38   return nothing();
39 }
40 
updateSubtitleInfo(const SubtitleInfo & si)41 void SubtitleDownloadEngine::updateSubtitleInfo(const SubtitleInfo& si) {
42   for (int i = 0; i < subtitlesList.size(); ++i) {
43     if (subtitlesList[i].id == si.id) {
44       subtitlesList[i] = si;
45       break;
46     }
47   }
48 }
49 
generateTmpFileName() const50 QString SubtitleDownloadEngine::generateTmpFileName() const {
51   static bool gen_inited;
52   if (!gen_inited) {
53     qsrand(time(0));
54     gen_inited = true;
55   }
56   return QString("QNapi.%1.tmp").arg(qrand());
57 }
58 
generateTmpPath() const59 QString SubtitleDownloadEngine::generateTmpPath() const {
60   QString newTmpFilePath =
61       QString("%1/%2").arg(tmpPath).arg(generateTmpFileName());
62   return QDir::toNativeSeparators(newTmpFilePath);
63 }
64