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

Last change on this file since 165 was 165, checked in by Silvan Scherrer, 11 years ago

SMPlayer: update trunk to latest 0.8.7

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