source: smplayer/trunk/src/helper.cpp@ 178

Last change on this file since 178 was 178, checked in by Silvan Scherrer, 9 years ago

smplayer: always add the app path to a executable search too

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