| 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"cxx . . $ `<file>/libqnapi/src/subtitlematcher.h`/ |
| 16 | #include <QDir> |
| 17 | |
| 18 | SubtitleMatchercxx . . $ SubtitleMatcher#::SubtitleMatchercxx . . $ SubtitleMatcher#SubtitleMatcher(328065b54377f352).( |
| 19 | bool _noBackup, bool _isPostProcessingEnabled, QStringcxx . . $ QString# _ppSubFormat, |
| 20 | QStringcxx . . $ QString# _ppSubExtension, bool _changePermissions, |
| 21 | QStringcxx . . $ QString# _changePermissionsTo, |
| 22 | const QSharedPointercxx . . $ QSharedPointer#<const SubtitleFormatsRegistrycxx . . $ SubtitleFormatsRegistry#>& |
| 23 | subtitleFormatsRegistry) |
| 24 | : noBackupcxx . . $ SubtitleMatcher#noBackup.(_noBackup), |
| 25 | isPostProcessingEnabledcxx . . $ SubtitleMatcher#isPostProcessingEnabled.(_isPostProcessingEnabled), |
| 26 | ppSubFormatcxx . . $ SubtitleMatcher#ppSubFormat.(_ppSubFormat), |
| 27 | ppSubExtensioncxx . . $ SubtitleMatcher#ppSubExtension.(_ppSubExtension), |
| 28 | changePermissionscxx . . $ SubtitleMatcher#changePermissions.(_changePermissions), |
| 29 | changePermissionsTocxx . . $ SubtitleMatcher#changePermissionsTo.(_changePermissionsTo), |
| 30 | subtitleFormatsRegistrycxx . . $ SubtitleMatcher#subtitleFormatsRegistry.(subtitleFormatsRegistry) {} |
| 31 | |
| 32 | bool SubtitleMatchercxx . . $ SubtitleMatcher#::matchSubtitlescxx . . $ SubtitleMatcher#matchSubtitles(128312153f60fc8d).(QStringcxx . . $ QString# subtitlesTmpFilePath, |
| 33 | QStringcxx . . $ QString# targetMovieFilePath) const { |
| 34 | QFileInfocxx . . $ QFileInfo# subtitlesTmpFileInfocxx . . $ QFileInfo#QFileInfo(3520e46b33348bf6).(subtitlesTmpFilePath); |
| 35 | |
| 36 | if (!subtitlesTmpFileInfo.existscxx . . $ QFileInfo#exists(50ce9a9e25b4a850).()) return false; |
| 37 | |
| 38 | QStringcxx . . $ QString# targetExtension = selectTargetExtensioncxx . . $ QString#QString(e1217c004a86c950).(subtitlesTmpFileInfocxx . . $ QFileInfo#QFileInfo(208c3155c9413262).); |
| 39 | |
| 40 | QStringcxx . . $ QString# targetSubtitlesFilePath = |
| 41 | constructSubtitlePathcxx . . $ QString#QString(e1217c004a86c950).(targetMovieFilePathcxx . . $ QString#QString(3520e46b33348bf6)., targetExtensioncxx . . $ QString#QString(3520e46b33348bf6).); |
| 42 | |
| 43 | if (!isWritablePathcxx . . $ SubtitleMatcher#isWritablePath(ce784287ea2249e0).(targetSubtitlesFilePathcxx . . $ QString#QString(3520e46b33348bf6).)) return false; |
| 44 | |
| 45 | removeOrCopycxx . . $ SubtitleMatcher#removeOrCopy(68677a49fc42faad).(targetMovieFilePathcxx . . $ QString#QString(3520e46b33348bf6)., targetSubtitlesFilePathcxx . . $ QString#QString(3520e46b33348bf6).); |
| 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 = QFilecxx . . $ QFile#::copycxx . . $ QFile#copy(edf9e4f73813982f).(subtitlesTmpFilePath, targetSubtitlesFilePath); |
| 55 | #endif |
| 56 | |
| 57 | if (changePermissionscxx . . $ SubtitleMatcher#changePermissions.) { |
| 58 | fixFilePermissionscxx . . $ SubtitleMatcher#fixFilePermissions(68677a49fc42faad).(targetSubtitlesFilePathcxx . . $ QString#QString(3520e46b33348bf6)., changePermissionsTocxx . . $ QString#QString(3520e46b33348bf6).); |
| 59 | } |
| 60 | |
| 61 | return result; |
| 62 | } |
| 63 | |
| 64 | QStringcxx . . $ QString# SubtitleMatchercxx . . $ SubtitleMatcher#::selectTargetExtensioncxx . . $ SubtitleMatcher#selectTargetExtension(4ace3cc0cf22035b).( |
| 65 | QFileInfocxx . . $ QFileInfo# subtitlesTmpFileInfo) const { |
| 66 | QStringcxx . . $ QString# targetExtension = subtitlesTmpFileInfocxx . . $ QString#QString(e1217c004a86c950)..suffixcxx . . $ QFileInfo#suffix(6ef7b6c27b00b43d).(); |
| 67 | |
| 68 | if (isPostProcessingEnabledcxx . . $ SubtitleMatcher#isPostProcessingEnabled.) { |
| 69 | if (!ppSubFormatcxx . . $ SubtitleMatcher#ppSubFormat..isEmptycxx . . $ QString#isEmpty(50ce9a9e25b4a850).() && ppSubExtensioncxx . . $ SubtitleMatcher#ppSubExtension..isEmptycxx . . $ QString#isEmpty(50ce9a9e25b4a850).()) { |
| 70 | targetExtension =cxx . . $ QString#`operator=`(7b76e6691bca1027). |
| 71 | subtitleFormatsRegistrycxx . . $ SubtitleMatcher#subtitleFormatsRegistry.->cxx . . $ QSharedPointer#`operator->`(5a2a78a048fb49a8).selectcxx . . $ SubtitleFormatsRegistry#select(849396506c5f6260).(ppSubFormatcxx . . $ SubtitleMatcher#ppSubFormat.)->cxx . . $ QSharedPointer#`operator->`(5a2a78a048fb49a8).defaultExtensioncxx . . $ SubtitleFormat#defaultExtension(6ef7b6c27b00b43d).(); |
| 72 | } else if (!ppSubExtensioncxx . . $ SubtitleMatcher#ppSubExtension..isEmptycxx . . $ QString#isEmpty(50ce9a9e25b4a850).()) { |
| 73 | targetExtension =cxx . . $ QString#`operator=`(6a89d0b7f4d2681c). ppSubExtensioncxx . . $ SubtitleMatcher#ppSubExtension.; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return targetExtensioncxx . . $ QString#QString(e1217c004a86c950).; |
| 78 | } |
| 79 | |
| 80 | QStringcxx . . $ QString# SubtitleMatchercxx . . $ SubtitleMatcher#::constructSubtitlePathcxx . . $ SubtitleMatcher#constructSubtitlePath(4895135db7c39a2b).(QStringcxx . . $ QString# targetMovieFilePath, |
| 81 | QStringcxx . . $ QString# targetExtension, |
| 82 | QStringcxx . . $ QString# baseSuffix) const { |
| 83 | QFileInfocxx . . $ QFileInfo# targetMovieFileInfocxx . . $ QFileInfo#QFileInfo(3520e46b33348bf6).(targetMovieFilePath); |
| 84 | return targetMovieFileInfocxx . . $ QString#QString(3520e46b33348bf6)..pathcxx . . $ QFileInfo#path(6ef7b6c27b00b43d).() +cxx . . $ operator+(eab1ea5b6c36e9b2). QDircxx . . $ QDir#::separatorcxx . . $ QDir#separator(44eb9225326fcbf5).() +cxx . . $ operator+(fab9b4f80031f353). |
| 85 | targetMovieFileInfo.completeBaseNamecxx . . $ QFileInfo#completeBaseName(6ef7b6c27b00b43d).() +cxx . . $ operator+(fab9b4f80031f353). baseSuffix +cxx . . $ operator+(3d22f08efddf8107). "." +cxx . . $ operator+(fab9b4f80031f353). |
| 86 | targetExtension; |
| 87 | } |
| 88 | |
| 89 | bool SubtitleMatchercxx . . $ SubtitleMatcher#::isWritablePathcxx . . $ SubtitleMatcher#isWritablePath(ce784287ea2249e0).(QStringcxx . . $ QString# path) const { |
| 90 | QFileInfocxx . . $ QFileInfo# pathFileInfocxx . . $ QFileInfo#QFileInfo(3520e46b33348bf6).(path); |
| 91 | return QFileInfocxx . . $ QFileInfo#(pathFileInfo.absolutePathcxx . . $ QFileInfo#absolutePath(6ef7b6c27b00b43d).()).isWritablecxx . . $ QFileInfo#isWritable(50ce9a9e25b4a850).(); |
| 92 | } |
| 93 | |
| 94 | void SubtitleMatchercxx . . $ SubtitleMatcher#::removeOrCopycxx . . $ SubtitleMatcher#removeOrCopy(68677a49fc42faad).(QStringcxx . . $ QString# targetMoviefilePath, |
| 95 | QStringcxx . . $ QString# targetSubtitlesFilePath) const { |
| 96 | if (QFilecxx . . $ QFile#::existscxx . . $ QFile#exists(e1defb592416720d).(targetSubtitlesFilePath)) { |
| 97 | if (!noBackupcxx . . $ SubtitleMatcher#noBackup.) { |
| 98 | QFileInfocxx . . $ QFileInfo# targetSubtitlesFileInfocxx . . $ QFileInfo#QFileInfo(3520e46b33348bf6).(targetSubtitlesFilePath); |
| 99 | QStringcxx . . $ QString# newName = constructSubtitlePathcxx . . $ QString#QString(e1217c004a86c950).( |
| 100 | targetMoviefilePathcxx . . $ QString#QString(3520e46b33348bf6)., targetSubtitlesFileInfocxx . . $ QString#QString(e1217c004a86c950)..suffixcxx . . $ QFileInfo#suffix(6ef7b6c27b00b43d).(), trcxx . . $ QString#QString(e1217c004a86c950).("_copy")); |
| 101 | |
| 102 | if (QFilecxx . . $ QFile#::existscxx . . $ QFile#exists(e1defb592416720d).(newName)) QFilecxx . . $ QFile#::removecxx . . $ QFile#remove(e1defb592416720d).(newName); |
| 103 | |
| 104 | QFilecxx . . $ QFile#::renamecxx . . $ QFile#rename(edf9e4f73813982f).(targetSubtitlesFilePath, newName); |
| 105 | } else |
| 106 | QFilecxx . . $ QFile#::removecxx . . $ QFile#remove(e1defb592416720d).(targetSubtitlesFilePath); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | bool SubtitleMatchercxx . . $ SubtitleMatcher#::dryCopycxx . . $ SubtitleMatcher#dryCopy(128312153f60fc8d).(QStringcxx . . $ QString# srcFilePath, QStringcxx . . $ QString# dstFilePath) const { |
| 111 | QFilecxx . . $ QFile# dstFilecxx . . $ QFile#QFile(3520e46b33348bf6).(dstFilePath), srcFilecxx . . $ QFile#QFile(3520e46b33348bf6).(srcFilePath); |
| 112 | bool result = false; |
| 113 | |
| 114 | if (!dstFile.opencxx . . $ QFile#open(dca164408be960c8).(QIODevicecxx . . $ QFlags#QFlags(9b289cee16747614).::WriteOnlycxx . . $ QIODevice#WriteOnly.) || |
| 115 | !srcFile.opencxx . . $ QFile#open(dca164408be960c8).(QIODevicecxx . . $ QFlags#QFlags(9b289cee16747614).::ReadOnlycxx . . $ QIODevice#ReadOnly.)) { |
| 116 | dstFile.closecxx . . $ QFileDevice#close(49f6e7a06ebc5aa8).(); |
| 117 | } else { |
| 118 | result = dstFile.writecxx . . $ QIODevice#write(4ee2750a0be83987).(srcFile.readAllcxx . . $ QIODevice#readAll(b3c286cec66e09f4).()) > 0; |
| 119 | srcFile.closecxx . . $ QFileDevice#close(49f6e7a06ebc5aa8).(); |
| 120 | dstFile.closecxx . . $ QFileDevice#close(49f6e7a06ebc5aa8).(); |
| 121 | } |
| 122 | |
| 123 | return result; |
| 124 | } |
| 125 | |
| 126 | void SubtitleMatchercxx . . $ SubtitleMatcher#::fixFilePermissionscxx . . $ SubtitleMatcher#fixFilePermissions(68677a49fc42faad).(QStringcxx . . $ QString# targetSubtitlesFilePath, |
| 127 | QStringcxx . . $ QString# changePermissionsTo) const { |
| 128 | bool validPermissions; |
| 129 | int permInt = changePermissionsTo.toIntcxx . . $ QString#toInt(d7972accae11ee1f).(&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 | QFilecxx . . $ QFile#::setPermissionscxx . . $ QFile#setPermissions(8c341457b00c996b).(targetSubtitlesFilePath, QFilecxx . . $ QFile#::Permissionscxx . . $ QFileDevice#Permissions#(permcxx . . $ QFlag#QFlag(d4f767463ce0a6b3).)); |
| 137 | } |
| 138 | } |