[112] | 1 | /* smplayer, GUI front-end for mplayer.
|
---|
[188] | 2 | Copyright (C) 2006-2017 Ricardo Villalba <rvm@users.sourceforge.net>
|
---|
[112] | 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 | #include "helper.h"
|
---|
| 20 |
|
---|
| 21 | #include <QApplication>
|
---|
| 22 | #include <QFileInfo>
|
---|
| 23 | #include <QColor>
|
---|
| 24 | #include <QDir>
|
---|
| 25 | #include <QTextCodec>
|
---|
| 26 | #include <QWidget>
|
---|
[176] | 27 | #include <QDebug>
|
---|
[112] | 28 | #include "config.h"
|
---|
[165] | 29 | #include "extensions.h"
|
---|
[112] | 30 |
|
---|
| 31 | #ifdef Q_OS_WIN
|
---|
| 32 | #include <windows.h> // For the screensaver stuff
|
---|
| 33 | #endif
|
---|
| 34 |
|
---|
| 35 | #if EXTERNAL_SLEEP
|
---|
| 36 | #include <unistd.h>
|
---|
| 37 | #else
|
---|
| 38 | #include <qthread.h>
|
---|
| 39 | #endif
|
---|
| 40 |
|
---|
| 41 |
|
---|
| 42 | #if !EXTERNAL_SLEEP
|
---|
| 43 | class Sleeper : public QThread
|
---|
| 44 | {
|
---|
| 45 | public:
|
---|
| 46 | static void sleep(unsigned long secs) {QThread::sleep(secs);}
|
---|
| 47 | static void msleep(unsigned long msecs) {
|
---|
| 48 | //qDebug("sleeping...");
|
---|
| 49 | QThread::msleep(msecs);
|
---|
| 50 | //qDebug("finished");
|
---|
| 51 | }
|
---|
| 52 | static void usleep(unsigned long usecs) {QThread::usleep(usecs);}
|
---|
| 53 | };
|
---|
| 54 | #endif
|
---|
| 55 |
|
---|
| 56 | /*
|
---|
| 57 | QString Helper::dvdForPref(const QString & dvd_id, int title) {
|
---|
| 58 | return QString("DVD_%1_%2").arg(dvd_id).arg(title);
|
---|
| 59 | }
|
---|
| 60 | */
|
---|
| 61 |
|
---|
| 62 | QString Helper::formatTime(int secs) {
|
---|
[176] | 63 | bool negative = (secs < 0);
|
---|
[188] | 64 | secs = qAbs(secs);
|
---|
[176] | 65 |
|
---|
[112] | 66 | int t = secs;
|
---|
[176] | 67 | int hours = (int) t / 3600;
|
---|
| 68 | t -= hours*3600;
|
---|
| 69 | int minutes = (int) t / 60;
|
---|
| 70 | t -= minutes*60;
|
---|
| 71 | int seconds = t;
|
---|
[112] | 72 |
|
---|
[176] | 73 | //qDebug() << "Helper::formatTime:" << hours << ":" << minutes << ":" << seconds;
|
---|
| 74 |
|
---|
| 75 | return QString("%1%2:%3:%4").arg(negative ? "-" : "").arg(hours, 2, 10, QChar('0')).arg(minutes, 2, 10, QChar('0')).arg(seconds, 2, 10, QChar('0'));
|
---|
[112] | 76 | }
|
---|
| 77 |
|
---|
[188] | 78 | QString Helper::formatTime2(double secs) {
|
---|
| 79 | bool negative = (secs < 0);
|
---|
| 80 | secs = qAbs(secs);
|
---|
| 81 |
|
---|
| 82 | double t = secs;
|
---|
| 83 | int hours = (int) t / 3600;
|
---|
| 84 | t -= hours*3600;
|
---|
| 85 | int minutes = (int) t / 60;
|
---|
| 86 | t -= minutes*60;
|
---|
| 87 | int seconds = t;
|
---|
| 88 | t -= seconds;
|
---|
| 89 | int milliseconds = t*1000;
|
---|
| 90 |
|
---|
| 91 | //qDebug() << "Helper::formatTime: secs:" << secs << "=" << hours << ":" << minutes << ":" << seconds << "." << milliseconds;
|
---|
| 92 |
|
---|
| 93 | return QString("%1%2:%3:%4.%5").arg(negative ? "-" : "").arg(hours, 2, 10, QChar('0')).arg(minutes, 2, 10, QChar('0')).arg(seconds, 2, 10, QChar('0')).arg(milliseconds, 3, 10, QChar('0'));
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[112] | 96 | QString Helper::timeForJumps(int secs) {
|
---|
| 97 | int minutes = (int) secs / 60;
|
---|
| 98 | int seconds = secs % 60;
|
---|
| 99 |
|
---|
| 100 | if (minutes==0) {
|
---|
[124] | 101 | return QObject::tr("%n second(s)", "", seconds);
|
---|
[112] | 102 | } else {
|
---|
| 103 | if (seconds==0)
|
---|
[124] | 104 | return QObject::tr("%n minute(s)", "", minutes);
|
---|
[112] | 105 | else {
|
---|
[124] | 106 | QString m = QObject::tr("%n minute(s)", "", minutes);
|
---|
| 107 | QString s = QObject::tr("%n second(s)", "", seconds);
|
---|
[112] | 108 | return QObject::tr("%1 and %2").arg(m).arg(s);
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | #ifdef Q_OS_WIN
|
---|
| 114 | // This function has been copied (and modified a little bit) from Scribus (program under GPL license):
|
---|
| 115 | // http://docs.scribus.net/devel/util_8cpp-source.html#l00112
|
---|
| 116 | QString Helper::shortPathName(QString long_path) {
|
---|
[135] | 117 | if (QFile::exists(long_path)) {
|
---|
[112] | 118 | QString short_path = long_path;
|
---|
| 119 |
|
---|
| 120 | const int max_path = 4096;
|
---|
| 121 | WCHAR shortName[max_path];
|
---|
| 122 |
|
---|
[165] | 123 | QString nativePath = QDir::toNativeSeparators(long_path);
|
---|
[112] | 124 | int ret = GetShortPathNameW((LPCWSTR) nativePath.utf16(), shortName, max_path);
|
---|
| 125 | if (ret != ERROR_INVALID_PARAMETER && ret < MAX_PATH)
|
---|
| 126 | short_path = QString::fromUtf16((const ushort*) shortName);
|
---|
| 127 |
|
---|
| 128 | return short_path;
|
---|
| 129 | } else {
|
---|
| 130 | return long_path;
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | /*
|
---|
| 135 | void Helper::setScreensaverEnabled(bool b) {
|
---|
| 136 | qDebug("Helper::setScreensaverEnabled: %d", b);
|
---|
| 137 |
|
---|
| 138 | if (b) {
|
---|
| 139 | // Activate screensaver
|
---|
| 140 | SystemParametersInfo( SPI_SETSCREENSAVEACTIVE, true, 0, SPIF_SENDWININICHANGE);
|
---|
| 141 | SystemParametersInfo( SPI_SETLOWPOWERACTIVE, 1, NULL, 0);
|
---|
| 142 | SystemParametersInfo( SPI_SETPOWEROFFACTIVE, 1, NULL, 0);
|
---|
| 143 | } else {
|
---|
| 144 | SystemParametersInfo( SPI_SETSCREENSAVEACTIVE, false, 0, SPIF_SENDWININICHANGE);
|
---|
| 145 | SystemParametersInfo( SPI_SETLOWPOWERACTIVE, 0, NULL, 0);
|
---|
| 146 | SystemParametersInfo( SPI_SETPOWEROFFACTIVE, 0, NULL, 0);
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 | */
|
---|
| 150 | #endif
|
---|
| 151 |
|
---|
| 152 | void Helper::msleep(int ms) {
|
---|
| 153 | #if EXTERNAL_SLEEP
|
---|
| 154 | //qDebug("Helper::msleep: %d (using usleep)", ms);
|
---|
| 155 | usleep(ms*1000);
|
---|
| 156 | #else
|
---|
| 157 | //qDebug("Helper::msleep: %d (using QThread::msleep)", ms);
|
---|
| 158 | Sleeper::msleep( ms );
|
---|
| 159 | #endif
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | QString Helper::changeSlashes(QString filename) {
|
---|
| 163 | // Only change if file exists (it's a local file)
|
---|
| 164 | if (QFileInfo(filename).exists())
|
---|
| 165 | return filename.replace('/', '\\');
|
---|
| 166 | else
|
---|
| 167 | return filename;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | bool Helper::directoryContainsDVD(QString directory) {
|
---|
| 171 | //qDebug("Helper::directoryContainsDVD: '%s'", directory.latin1());
|
---|
| 172 |
|
---|
| 173 | QDir dir(directory);
|
---|
| 174 | QStringList l = dir.entryList();
|
---|
[165] | 175 | bool valid = false;
|
---|
[112] | 176 | for (int n=0; n < l.count(); n++) {
|
---|
| 177 | //qDebug(" * entry %d: '%s'", n, l[n].toUtf8().data());
|
---|
[165] | 178 | if (l[n].toLower() == "video_ts") valid = true;
|
---|
[112] | 179 | }
|
---|
| 180 |
|
---|
| 181 | return valid;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | int Helper::qtVersion() {
|
---|
| 185 | QRegExp rx("(\\d+)\\.(\\d+)\\.(\\d+)");
|
---|
| 186 | QString v(qVersion());
|
---|
| 187 |
|
---|
| 188 | int r = 0;
|
---|
| 189 |
|
---|
| 190 | if (rx.indexIn(v) > -1) {
|
---|
| 191 | int n1 = rx.cap(1).toInt();
|
---|
| 192 | int n2 = rx.cap(2).toInt();
|
---|
| 193 | int n3 = rx.cap(3).toInt();
|
---|
| 194 | r = n1 * 1000 + n2 * 100 + n3;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | qDebug("Helper::qtVersion: %d", r);
|
---|
| 198 |
|
---|
| 199 | return r;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | QString Helper::equalizerListToString(AudioEqualizerList values) {
|
---|
| 203 | double v0 = (double) values[0].toInt() / 10;
|
---|
| 204 | double v1 = (double) values[1].toInt() / 10;
|
---|
| 205 | double v2 = (double) values[2].toInt() / 10;
|
---|
| 206 | double v3 = (double) values[3].toInt() / 10;
|
---|
| 207 | double v4 = (double) values[4].toInt() / 10;
|
---|
| 208 | double v5 = (double) values[5].toInt() / 10;
|
---|
| 209 | double v6 = (double) values[6].toInt() / 10;
|
---|
| 210 | double v7 = (double) values[7].toInt() / 10;
|
---|
| 211 | double v8 = (double) values[8].toInt() / 10;
|
---|
| 212 | double v9 = (double) values[9].toInt() / 10;
|
---|
| 213 | QString s = QString::number(v0) + ":" + QString::number(v1) + ":" +
|
---|
| 214 | QString::number(v2) + ":" + QString::number(v3) + ":" +
|
---|
| 215 | QString::number(v4) + ":" + QString::number(v5) + ":" +
|
---|
| 216 | QString::number(v6) + ":" + QString::number(v7) + ":" +
|
---|
| 217 | QString::number(v8) + ":" + QString::number(v9);
|
---|
| 218 |
|
---|
| 219 | return s;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | QStringList Helper::searchForConsecutiveFiles(const QString & initial_file) {
|
---|
| 223 | qDebug("Helper::searchForConsecutiveFiles: initial_file: '%s'", initial_file.toUtf8().constData());
|
---|
| 224 |
|
---|
| 225 | QStringList files_to_add;
|
---|
[119] | 226 | QStringList matching_files;
|
---|
[112] | 227 |
|
---|
| 228 | QFileInfo fi(initial_file);
|
---|
| 229 | QString basename = fi.completeBaseName();
|
---|
| 230 | QString extension = fi.suffix();
|
---|
| 231 | QString path = fi.absolutePath();
|
---|
| 232 |
|
---|
[119] | 233 | QDir dir(path);
|
---|
| 234 | dir.setFilter(QDir::Files);
|
---|
| 235 | dir.setSorting(QDir::Name);
|
---|
[112] | 236 |
|
---|
[119] | 237 | QRegExp rx("(\\d+)");
|
---|
[112] | 238 |
|
---|
[119] | 239 | int digits;
|
---|
| 240 | int current_number;
|
---|
| 241 | int pos = 0;
|
---|
| 242 | QString next_name;
|
---|
| 243 | bool next_found = false;
|
---|
| 244 | qDebug("Helper::searchForConsecutiveFiles: trying to find consecutive files");
|
---|
| 245 | while ( ( pos = rx.indexIn(basename, pos) ) != -1 ) {
|
---|
[128] | 246 | qDebug("Helper::searchForConsecutiveFiles: captured: %s",rx.cap(1).toUtf8().constData());
|
---|
[119] | 247 | digits = rx.cap(1).length();
|
---|
| 248 | current_number = rx.cap(1).toInt() + 1;
|
---|
| 249 | next_name = basename.left(pos) + QString("%1").arg(current_number, digits, 10, QLatin1Char('0'));
|
---|
| 250 | next_name.replace(QRegExp("([\\[\\]?*])"), "[\\1]");
|
---|
| 251 | next_name += "*." + extension;
|
---|
[128] | 252 | qDebug("Helper::searchForConsecutiveFiles: next name = %s",next_name.toUtf8().constData());
|
---|
[119] | 253 | matching_files = dir.entryList((QStringList)next_name);
|
---|
[112] | 254 |
|
---|
[119] | 255 | if ( !matching_files.isEmpty() ) {
|
---|
| 256 | next_found = true;
|
---|
| 257 | break;
|
---|
| 258 | }
|
---|
| 259 | qDebug("Helper::searchForConsecutiveFiles: pos = %d",pos);
|
---|
| 260 | pos += digits;
|
---|
| 261 | }
|
---|
[112] | 262 |
|
---|
[119] | 263 | if (next_found) {
|
---|
| 264 | qDebug("Helper::searchForConsecutiveFiles: adding consecutive files");
|
---|
| 265 | while ( !matching_files.isEmpty() ) {
|
---|
| 266 | qDebug("Helper::searchForConsecutiveFiles: '%s' exists, added to the list", matching_files[0].toUtf8().constData());
|
---|
[176] | 267 | QString filename = path + "/" + matching_files[0];
|
---|
| 268 | #ifdef Q_OS_WIN
|
---|
| 269 | filename = QDir::toNativeSeparators(filename);
|
---|
| 270 | #endif
|
---|
| 271 | files_to_add << filename;
|
---|
[112] | 272 | current_number++;
|
---|
[119] | 273 | next_name = basename.left(pos) + QString("%1").arg(current_number, digits, 10, QLatin1Char('0'));
|
---|
| 274 | next_name.replace(QRegExp("([\\[\\]?*])"), "[\\1]");
|
---|
| 275 | next_name += "*." + extension;
|
---|
| 276 | matching_files = dir.entryList((QStringList)next_name);
|
---|
[112] | 277 | qDebug("Helper::searchForConsecutiveFiles: looking for '%s'", next_name.toUtf8().constData());
|
---|
| 278 | }
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | return files_to_add;
|
---|
| 282 | }
|
---|
[165] | 283 |
|
---|
| 284 | QStringList Helper::filesInDirectory(const QString & initial_file, const QStringList & filter) {
|
---|
| 285 | qDebug("Helper::filesInDirectory: initial_file: %s", initial_file.toUtf8().constData());
|
---|
| 286 | //qDebug() << "Helper::filesInDirectory: filter:" << filter;
|
---|
| 287 |
|
---|
| 288 | QFileInfo fi(initial_file);
|
---|
| 289 | QString current_file = fi.fileName();
|
---|
| 290 | QString path = fi.absolutePath();
|
---|
| 291 |
|
---|
| 292 | QDir d(path);
|
---|
| 293 | QStringList all_files = d.entryList(filter, QDir::Files);
|
---|
| 294 |
|
---|
| 295 | QStringList r;
|
---|
| 296 | for (int n = 0; n < all_files.count(); n++) {
|
---|
[176] | 297 | //if (all_files[n] != current_file) {
|
---|
[165] | 298 | QString s = path +"/" + all_files[n];
|
---|
[176] | 299 | #ifdef Q_OS_WIN
|
---|
| 300 | s = QDir::toNativeSeparators(s);
|
---|
| 301 | #endif
|
---|
[165] | 302 | r << s;
|
---|
[176] | 303 | //}
|
---|
[165] | 304 | }
|
---|
| 305 |
|
---|
| 306 | //qDebug() << "Helper::filesInDirectory: result:" << r;
|
---|
| 307 |
|
---|
| 308 | return r;
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | QStringList Helper::filesForPlaylist(const QString & initial_file, Preferences::AutoAddToPlaylistFilter filter) {
|
---|
| 312 | QStringList res;
|
---|
| 313 |
|
---|
| 314 | if (filter == Preferences::ConsecutiveFiles) {
|
---|
| 315 | res = searchForConsecutiveFiles(initial_file);
|
---|
| 316 | } else {
|
---|
| 317 | Extensions e;
|
---|
| 318 | QStringList exts;
|
---|
| 319 | switch (filter) {
|
---|
| 320 | case Preferences::VideoFiles: exts = e.video().forDirFilter(); break;
|
---|
| 321 | case Preferences::AudioFiles: exts = e.audio().forDirFilter(); break;
|
---|
| 322 | case Preferences::MultimediaFiles: exts = e.multimedia().forDirFilter(); break;
|
---|
| 323 | default: ;
|
---|
| 324 | }
|
---|
| 325 | if (!exts.isEmpty()) res = Helper::filesInDirectory(initial_file, exts);
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | return res;
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | #ifdef Q_OS_WIN
|
---|
| 332 | // Check for Windows shortcuts
|
---|
| 333 | QStringList Helper::resolveSymlinks(const QStringList & files) {
|
---|
| 334 | QStringList list = files;
|
---|
| 335 | for (int n=0; n < list.count(); n++) {
|
---|
| 336 | QFileInfo fi(list[n]);
|
---|
| 337 | if (fi.isSymLink()) {
|
---|
| 338 | list[n] = fi.symLinkTarget();
|
---|
| 339 | }
|
---|
| 340 | }
|
---|
| 341 | return list;
|
---|
| 342 | }
|
---|
| 343 | #endif
|
---|
[176] | 344 |
|
---|
| 345 | #ifndef Q_OS_WIN
|
---|
| 346 | QString Helper::findExecutable(const QString & name) {
|
---|
| 347 | QByteArray env = qgetenv("PATH");
|
---|
| 348 | #ifdef Q_OS_OS2
|
---|
| 349 | QString newName = name;
|
---|
| 350 | if (!newName.endsWith(".exe", Qt::CaseInsensitive))
|
---|
| 351 | newName.append(".exe");
|
---|
[178] | 352 | QStringList search_paths = QString::fromLocal8Bit(env.constData()).split(';', QString::SkipEmptyParts);
|
---|
| 353 | search_paths += qApp->applicationDirPath();
|
---|
| 354 | #else
|
---|
| 355 | QStringList search_paths = QString::fromLocal8Bit(env.constData()).split(':', QString::SkipEmptyParts);
|
---|
[176] | 356 | #endif
|
---|
| 357 | for (int n = 0; n < search_paths.count(); n++) {
|
---|
| 358 | #ifdef Q_OS_OS2
|
---|
| 359 | QString candidate = search_paths[n] + "/" + newName;
|
---|
| 360 | #else
|
---|
| 361 | QString candidate = search_paths[n] + "/" + name;
|
---|
| 362 | #endif
|
---|
| 363 | qDebug("Helper::findExecutable: candidate: %s", candidate.toUtf8().constData());
|
---|
| 364 | QFileInfo info(candidate);
|
---|
| 365 | if (info.isFile() && info.isExecutable()) {
|
---|
| 366 | qDebug("Helper::findExecutable: executable found: %s", candidate.toUtf8().constData());
|
---|
| 367 | return candidate;
|
---|
| 368 | }
|
---|
| 369 | }
|
---|
| 370 | return QString::null;
|
---|
| 371 | }
|
---|
| 372 | #endif
|
---|