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

Last change on this file since 128 was 128, checked in by Silvan Scherrer, 13 years ago

SMPlayer: trunk update to latest svn

  • Property svn:eol-style set to LF
File size: 7.4 KB
Line 
1/* smplayer, GUI front-end for mplayer.
2 Copyright (C) 2006-2012 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 "config.h"
28
29#ifdef Q_OS_WIN
30#include <windows.h> // For the screensaver stuff
31#endif
32
33#if EXTERNAL_SLEEP
34#include <unistd.h>
35#else
36#include <qthread.h>
37#endif
38
39
40#if !EXTERNAL_SLEEP
41class Sleeper : public QThread
42{
43public:
44 static void sleep(unsigned long secs) {QThread::sleep(secs);}
45 static void msleep(unsigned long msecs) {
46 //qDebug("sleeping...");
47 QThread::msleep(msecs);
48 //qDebug("finished");
49 }
50 static void usleep(unsigned long usecs) {QThread::usleep(usecs);}
51};
52#endif
53
54/*
55QString Helper::dvdForPref(const QString & dvd_id, int title) {
56 return QString("DVD_%1_%2").arg(dvd_id).arg(title);
57}
58*/
59
60QString Helper::formatTime(int secs) {
61 int t = secs;
62 int hours = (int) t / 3600;
63 t -= hours*3600;
64 int minutes = (int) t / 60;
65 t -= minutes*60;
66 int seconds = t;
67
68 QString tf;
69 return tf.sprintf("%02d:%02d:%02d",hours,minutes,seconds);
70}
71
72QString Helper::timeForJumps(int secs) {
73 int minutes = (int) secs / 60;
74 int seconds = secs % 60;
75
76 if (minutes==0) {
77 return QObject::tr("%n second(s)", "", seconds);
78 } else {
79 if (seconds==0)
80 return QObject::tr("%n minute(s)", "", minutes);
81 else {
82 QString m = QObject::tr("%n minute(s)", "", minutes);
83 QString s = QObject::tr("%n second(s)", "", seconds);
84 return QObject::tr("%1 and %2").arg(m).arg(s);
85 }
86 }
87}
88
89#ifdef Q_OS_WIN
90// This function has been copied (and modified a little bit) from Scribus (program under GPL license):
91// http://docs.scribus.net/devel/util_8cpp-source.html#l00112
92QString Helper::shortPathName(QString long_path) {
93 if ((QSysInfo::WindowsVersion >= QSysInfo::WV_NT) && (QFile::exists(long_path))) {
94 QString short_path = long_path;
95
96 const int max_path = 4096;
97 WCHAR shortName[max_path];
98
99 QString nativePath = QDir::convertSeparators(long_path);
100 int ret = GetShortPathNameW((LPCWSTR) nativePath.utf16(), shortName, max_path);
101 if (ret != ERROR_INVALID_PARAMETER && ret < MAX_PATH)
102 short_path = QString::fromUtf16((const ushort*) shortName);
103
104 return short_path;
105 } else {
106 return long_path;
107 }
108}
109
110/*
111void Helper::setScreensaverEnabled(bool b) {
112 qDebug("Helper::setScreensaverEnabled: %d", b);
113
114 if (b) {
115 // Activate screensaver
116 SystemParametersInfo( SPI_SETSCREENSAVEACTIVE, true, 0, SPIF_SENDWININICHANGE);
117 SystemParametersInfo( SPI_SETLOWPOWERACTIVE, 1, NULL, 0);
118 SystemParametersInfo( SPI_SETPOWEROFFACTIVE, 1, NULL, 0);
119 } else {
120 SystemParametersInfo( SPI_SETSCREENSAVEACTIVE, false, 0, SPIF_SENDWININICHANGE);
121 SystemParametersInfo( SPI_SETLOWPOWERACTIVE, 0, NULL, 0);
122 SystemParametersInfo( SPI_SETPOWEROFFACTIVE, 0, NULL, 0);
123 }
124}
125*/
126#endif
127
128void Helper::msleep(int ms) {
129#if EXTERNAL_SLEEP
130 //qDebug("Helper::msleep: %d (using usleep)", ms);
131 usleep(ms*1000);
132#else
133 //qDebug("Helper::msleep: %d (using QThread::msleep)", ms);
134 Sleeper::msleep( ms );
135#endif
136}
137
138QString Helper::changeSlashes(QString filename) {
139 // Only change if file exists (it's a local file)
140 if (QFileInfo(filename).exists())
141 return filename.replace('/', '\\');
142 else
143 return filename;
144}
145
146bool Helper::directoryContainsDVD(QString directory) {
147 //qDebug("Helper::directoryContainsDVD: '%s'", directory.latin1());
148
149 QDir dir(directory);
150 QStringList l = dir.entryList();
151 bool valid = FALSE;
152 for (int n=0; n < l.count(); n++) {
153 //qDebug(" * entry %d: '%s'", n, l[n].toUtf8().data());
154 if (l[n].toLower() == "video_ts") valid = TRUE;
155 }
156
157 return valid;
158}
159
160int Helper::qtVersion() {
161 QRegExp rx("(\\d+)\\.(\\d+)\\.(\\d+)");
162 QString v(qVersion());
163
164 int r = 0;
165
166 if (rx.indexIn(v) > -1) {
167 int n1 = rx.cap(1).toInt();
168 int n2 = rx.cap(2).toInt();
169 int n3 = rx.cap(3).toInt();
170 r = n1 * 1000 + n2 * 100 + n3;
171 }
172
173 qDebug("Helper::qtVersion: %d", r);
174
175 return r;
176}
177
178QString Helper::equalizerListToString(AudioEqualizerList values) {
179 double v0 = (double) values[0].toInt() / 10;
180 double v1 = (double) values[1].toInt() / 10;
181 double v2 = (double) values[2].toInt() / 10;
182 double v3 = (double) values[3].toInt() / 10;
183 double v4 = (double) values[4].toInt() / 10;
184 double v5 = (double) values[5].toInt() / 10;
185 double v6 = (double) values[6].toInt() / 10;
186 double v7 = (double) values[7].toInt() / 10;
187 double v8 = (double) values[8].toInt() / 10;
188 double v9 = (double) values[9].toInt() / 10;
189 QString s = QString::number(v0) + ":" + QString::number(v1) + ":" +
190 QString::number(v2) + ":" + QString::number(v3) + ":" +
191 QString::number(v4) + ":" + QString::number(v5) + ":" +
192 QString::number(v6) + ":" + QString::number(v7) + ":" +
193 QString::number(v8) + ":" + QString::number(v9);
194
195 return s;
196}
197
198QStringList Helper::searchForConsecutiveFiles(const QString & initial_file) {
199 qDebug("Helper::searchForConsecutiveFiles: initial_file: '%s'", initial_file.toUtf8().constData());
200
201 QStringList files_to_add;
202 QStringList matching_files;
203
204 QFileInfo fi(initial_file);
205 QString basename = fi.completeBaseName();
206 QString extension = fi.suffix();
207 QString path = fi.absolutePath();
208
209 QDir dir(path);
210 dir.setFilter(QDir::Files);
211 dir.setSorting(QDir::Name);
212
213 QRegExp rx("(\\d+)");
214
215 int digits;
216 int current_number;
217 int pos = 0;
218 QString next_name;
219 bool next_found = false;
220 qDebug("Helper::searchForConsecutiveFiles: trying to find consecutive files");
221 while ( ( pos = rx.indexIn(basename, pos) ) != -1 ) {
222 qDebug("Helper::searchForConsecutiveFiles: captured: %s",rx.cap(1).toUtf8().constData());
223 digits = rx.cap(1).length();
224 current_number = rx.cap(1).toInt() + 1;
225 next_name = basename.left(pos) + QString("%1").arg(current_number, digits, 10, QLatin1Char('0'));
226 next_name.replace(QRegExp("([\\[\\]?*])"), "[\\1]");
227 next_name += "*." + extension;
228 qDebug("Helper::searchForConsecutiveFiles: next name = %s",next_name.toUtf8().constData());
229 matching_files = dir.entryList((QStringList)next_name);
230
231 if ( !matching_files.isEmpty() ) {
232 next_found = true;
233 break;
234 }
235 qDebug("Helper::searchForConsecutiveFiles: pos = %d",pos);
236 pos += digits;
237 }
238
239 if (next_found) {
240 qDebug("Helper::searchForConsecutiveFiles: adding consecutive files");
241 while ( !matching_files.isEmpty() ) {
242 qDebug("Helper::searchForConsecutiveFiles: '%s' exists, added to the list", matching_files[0].toUtf8().constData());
243 files_to_add << path + "/" + matching_files[0];
244 current_number++;
245 next_name = basename.left(pos) + QString("%1").arg(current_number, digits, 10, QLatin1Char('0'));
246 next_name.replace(QRegExp("([\\[\\]?*])"), "[\\1]");
247 next_name += "*." + extension;
248 matching_files = dir.entryList((QStringList)next_name);
249 qDebug("Helper::searchForConsecutiveFiles: looking for '%s'", next_name.toUtf8().constData());
250 }
251 }
252
253 return files_to_add;
254}
Note: See TracBrowser for help on using the repository browser.