| 1 | /***************************************************************************** |
| 2 | ** QNapi |
| 3 | ** Copyright (C) 2008-2017 Piotr KrzemiĆski <[email protected]> |
| 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 "subtitlematcher.h" |
| 16 | #include <QDir> |
| 17 | |
| 18 | SubtitleMatcher::SubtitleMatcher( |
| 19 | bool _noBackup, bool _isPostProcessingEnabled, QString _ppSubFormat, |
| 20 | QString _ppSubExtension, bool _changePermissions, |
| 21 | QString _changePermissionsTo, |
| 22 | const QSharedPointer<const SubtitleFormatsRegistry>& |
| 23 | subtitleFormatsRegistry) |
| 24 | : noBackup(_noBackup), |
| 25 | isPostProcessingEnabled(_isPostProcessingEnabled), |
| 26 | ppSubFormat(_ppSubFormat), |
| 27 | ppSubExtension(_ppSubExtension), |
| 28 | changePermissions(_changePermissions), |
| 29 | changePermissionsTo(_changePermissionsTo), |
| 30 | subtitleFormatsRegistry(subtitleFormatsRegistry) {} |
| 31 | |
| 32 | bool SubtitleMatcher::matchSubtitles(QString subtitlesTmpFilePath, |
| 33 | QString targetMovieFilePath) const { |
| 34 | QFileInfo subtitlesTmpFileInfo(subtitlesTmpFilePath); |
| 35 | |
| 36 | if (!subtitlesTmpFileInfo.exists()) return false; |
| 37 | |
| 38 | QString targetExtension = selectTargetExtension(subtitlesTmpFileInfo); |
| 39 | |
| 40 | QString targetSubtitlesFilePath = |
| 41 | constructSubtitlePath(targetMovieFilePath, targetExtension); |
| 42 | |
| 43 | if (!isWritablePath(targetSubtitlesFilePath)) return false; |
| 44 | |
| 45 | removeOrCopy(targetMovieFilePath, targetSubtitlesFilePath); |
| 46 | |
| 47 | bool result = false; |
| 48 | |
| 49 | #ifdef Q_OS_WIN |
| 50 | // Pod windowsem, aby "wyczyscic" atrybuty pliku, tworzymy go na nowo |
| 51 | result = dryCopy(subtitlesTmpFilePath, targetSubtitlesFilePath); |
| 52 | #else |
| 53 | // pod innymi OS-ami wystarczy skopiowac plik |
| 54 | result = QFile::copy(subtitlesTmpFilePath, targetSubtitlesFilePath); |
| 55 | #endif |
| 56 | |
| 57 | if (changePermissions) { |
| 58 | fixFilePermissions(targetSubtitlesFilePath, changePermissionsTo); |
| 59 | } |
| 60 | |
| 61 | return result; |
| 62 | } |
| 63 | |
| 64 | QString SubtitleMatcher::selectTargetExtension( |
| 65 | QFileInfo subtitlesTmpFileInfo) const { |
| 66 | QString targetExtension = subtitlesTmpFileInfo.suffix(); |
| 67 | |
| 68 | if (isPostProcessingEnabled) { |
| 69 | if (!ppSubFormat.isEmpty() && ppSubExtension.isEmpty()) { |
| 70 | targetExtension = |
| 71 | subtitleFormatsRegistry->select(ppSubFormat)->defaultExtension(); |
| 72 | } else if (!ppSubExtension.isEmpty()) { |
| 73 | targetExtension = ppSubExtension; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return targetExtension; |
| 78 | } |
| 79 | |
| 80 | QString SubtitleMatcher::constructSubtitlePath(QString targetMovieFilePath, |
| 81 | QString targetExtension, |
| 82 | QString baseSuffix) const { |
| 83 | QFileInfo targetMovieFileInfo(targetMovieFilePath); |
| 84 | return targetMovieFileInfo.path() + QDir::separator() + |
| 85 | targetMovieFileInfo.completeBaseName() + baseSuffix + "." + |
| 86 | targetExtension; |
| 87 | } |
| 88 | |
| 89 | bool SubtitleMatcher::isWritablePath(QString path) const { |
| 90 | QFileInfo pathFileInfo(path); |
| 91 | return QFileInfo(pathFileInfo.absolutePath()).isWritable(); |
| 92 | } |
| 93 | |
| 94 | void SubtitleMatcher::removeOrCopy(QString targetMoviefilePath, |
| 95 | QString targetSubtitlesFilePath) const { |
| 96 | if (QFile::exists(targetSubtitlesFilePath)) { |
| 97 | if (!noBackup) { |
| 98 | QFileInfo targetSubtitlesFileInfo(targetSubtitlesFilePath); |
| 99 | QString newName = constructSubtitlePath( |
| 100 | targetMoviefilePath, targetSubtitlesFileInfo.suffix(), tr("_copy")); |
| 101 | |
| 102 | if (QFile::exists(newName)) QFile::remove(newName); |
| 103 | |
| 104 | QFile::rename(targetSubtitlesFilePath, newName); |
| 105 | } else |
| 106 | QFile::remove(targetSubtitlesFilePath); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | bool SubtitleMatcher::dryCopy(QString srcFilePath, QString dstFilePath) const { |
| 111 | QFile dstFile(dstFilePath), srcFile(srcFilePath); |
| 112 | bool result = false; |
| 113 | |
| 114 | if (!dstFile.open(QIODevice::WriteOnly) || |
| 115 | !srcFile.open(QIODevice::ReadOnly)) { |
| 116 | dstFile.close(); |
| 117 | } else { |
| 118 | result = dstFile.write(srcFile.readAll()) > 0; |
| 119 | srcFile.close(); |
| 120 | dstFile.close(); |
| 121 | } |
| 122 | |
| 123 | return result; |
| 124 | } |
| 125 | |
| 126 | void SubtitleMatcher::fixFilePermissions(QString targetSubtitlesFilePath, |
| 127 | QString changePermissionsTo) const { |
| 128 | bool validPermissions; |
| 129 | int permInt = changePermissionsTo.toInt(&validPermissions, 8); |
| 130 | |
| 131 | if (validPermissions) { |
| 132 | int perm = 0; |
| 133 | perm |= (permInt & 0700) << 2; |
| 134 | perm |= (permInt & 0070) << 1; |
| 135 | perm |= (permInt & 0007); |
| 136 | QFile::setPermissions(targetSubtitlesFilePath, QFile::Permissions(perm)); |
| 137 | } |
| 138 | } |