1
/* smplayer, GUI front-end for mplayer.
2
Copyright (C) 2006-2008 Ricardo Villalba <rvm@escomposlinux.org>
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.
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.
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
21
#include <QApplication>
31
#include <QLibraryInfo>
34
#include <windows.h> // For the screensaver stuff
46
class Sleeper : public QThread
49
static void sleep(unsigned long secs) {QThread::sleep(secs);}
50
static void msleep(unsigned long msecs) {
51
//qDebug("sleeping...");
52
QThread::msleep(msecs);
55
static void usleep(unsigned long usecs) {QThread::usleep(usecs);}
61
QString Helper::app_path;
62
QString Helper::ini_path;
64
void Helper::setAppPath(QString path) {
68
QString Helper::appPath() {
72
QString Helper::dataPath() {
74
QString path = QString(DATA_PATH);
84
QString Helper::translationPath() {
85
#ifdef TRANSLATION_PATH
86
QString path = QString(TRANSLATION_PATH);
90
return appPath() + "/translations";
92
return appPath() + "/translations";
96
QString Helper::docPath() {
98
QString path = QString(DOC_PATH);
102
return appPath() + "/docs";
104
return appPath() + "/docs";
108
QString Helper::confPath() {
110
QString path = QString(CONF_PATH);
120
QString Helper::themesPath() {
122
QString path = QString(THEMES_PATH);
126
return appPath() + "/themes";
128
return appPath() + "/themes";
132
QString Helper::shortcutsPath() {
133
#ifdef SHORTCUTS_PATH
134
QString path = QString(SHORTCUTS_PATH);
138
return appPath() + "/shortcuts";
140
return appPath() + "/shortcuts";
144
QString Helper::qtTranslationPath() {
145
return QLibraryInfo::location(QLibraryInfo::TranslationsPath);
148
QString Helper::doc(QString file, QString locale) {
149
if (locale.isEmpty()) {
150
locale = QLocale::system().name();
153
QString f = docPath() + "/" + locale + "/" + file;
154
qDebug("Helper:doc: checking '%s'", f.toUtf8().data());
155
if (QFile::exists(f)) return f;
157
if (locale.indexOf(QRegExp("_[A-Z]+")) != -1) {
158
locale.replace(QRegExp("_[A-Z]+"), "");
159
f = docPath() + "/" + locale + "/" + file;
160
qDebug("Helper:doc: checking '%s'", f.toUtf8().data());
161
if (QFile::exists(f)) return f;
164
f = docPath() + "/en/" + file;
168
QString Helper::appHomePath() {
169
return QDir::homePath() + "/.smplayer";
172
void Helper::setIniPath(QString path) {
176
QString Helper::iniPath() {
177
if (!ini_path.isEmpty()) {
180
if (QFile::exists(appHomePath())) return appHomePath();
185
QString Helper::filenameForPref(const QString & filename) {
186
QString s = filename;
187
s = s.replace('/', '_');
188
s = s.replace('\\', '_');
189
s = s.replace(':', '_');
190
s = s.replace('.', '_');
191
s = s.replace(' ', '_');
193
QFileInfo fi(filename);
195
s += "_" + QString::number( fi.size() );
201
QString Helper::dvdForPref(const QString & dvd_id, int title) {
202
return QString("DVD_%1_%2").arg(dvd_id).arg(title);
206
void Helper::addLog(QString s) {
210
QString Helper::log() {
214
QString Helper::formatTime(int secs) {
216
int hours = (int) t / 3600;
218
int minutes = (int) t / 60;
223
return tf.sprintf("%02d:%02d:%02d",hours,minutes,seconds);
226
QString Helper::timeForJumps(int secs) {
227
int minutes = (int) secs / 60;
228
int seconds = secs % 60;
232
return QObject::tr("1 second");
234
return QObject::tr("%1 seconds").arg(seconds);
239
return QObject::tr("1 minute");
242
return QObject::tr("1 minute and 1 second");
244
return QObject::tr("1 minute and %1 seconds").arg(seconds);
247
return QObject::tr("%1 minutes").arg(minutes);
250
return QObject::tr("%1 minutes and 1 second").arg(minutes);
252
return QObject::tr("%1 minutes and %2 seconds").arg(minutes)
259
// This function has been copied (and modified a little bit) from Scribus (program under GPL license):
260
// http://docs.scribus.net/devel/util_8cpp-source.html#l00112
261
QString Helper::shortPathName(QString long_path) {
262
if ((QSysInfo::WindowsVersion >= QSysInfo::WV_NT) && (QFile::exists(long_path))) {
263
QString short_path = long_path;
265
const int max_path = 4096;
266
WCHAR shortName[max_path];
268
QString nativePath = QDir::convertSeparators(long_path);
269
int ret = GetShortPathNameW((LPCWSTR) nativePath.utf16(), shortName, max_path);
270
if (ret != ERROR_INVALID_PARAMETER && ret < MAX_PATH)
271
short_path = QString::fromUtf16((const ushort*) shortName);
280
void Helper::setScreensaverEnabled(bool b) {
281
qDebug("Helper::setScreensaverEnabled: %d", b);
284
// Activate screensaver
285
SystemParametersInfo( SPI_SETSCREENSAVEACTIVE, true, 0, SPIF_SENDWININICHANGE);
286
SystemParametersInfo( SPI_SETLOWPOWERACTIVE, 1, NULL, 0);
287
SystemParametersInfo( SPI_SETPOWEROFFACTIVE, 1, NULL, 0);
289
SystemParametersInfo( SPI_SETSCREENSAVEACTIVE, false, 0, SPIF_SENDWININICHANGE);
290
SystemParametersInfo( SPI_SETLOWPOWERACTIVE, 0, NULL, 0);
291
SystemParametersInfo( SPI_SETPOWEROFFACTIVE, 0, NULL, 0);
297
void Helper::msleep(int ms) {
299
//qDebug("Helper::msleep: %d (using usleep)", ms);
302
//qDebug("Helper::msleep: %d (using QThread::msleep)", ms);
303
Sleeper::msleep( ms );
307
QString Helper::colorToRRGGBBAA(unsigned int color) {
312
return s.sprintf("%02x%02x%02x00", c.red(), c.green(), c.blue() );
315
QString Helper::colorToRRGGBB(unsigned int color) {
320
return s.sprintf("%02x%02x%02x", c.red(), c.green(), c.blue() );
323
QString Helper::colorToRGB(unsigned int color) {
328
return s.sprintf("0x%02x%02x%02x", c.blue(), c.green(), c.red() );
331
void Helper::setForegroundColor(QWidget * w, const QColor & color) {
332
QPalette p = w->palette();
333
p.setColor(w->foregroundRole(), color);
337
void Helper::setBackgroundColor(QWidget * w, const QColor & color) {
338
QPalette p = w->palette();
339
p.setColor(w->backgroundRole(), color);
343
QString Helper::changeSlashes(QString filename) {
344
// Only change if file exists (it's a local file)
345
if (QFileInfo(filename).exists())
346
return filename.replace('/', '\\');
351
QString Helper::dvdSplitFolder(QString dvd_url) {
352
qDebug("Helper::dvdSplitFolder: '%s'", dvd_url.toUtf8().data());
353
QRegExp s("^dvd://(\\d+):(.*)", Qt::CaseInsensitive);
354
if (s.indexIn(dvd_url)!=-1) {
357
return QString::null;
361
int Helper::dvdSplitTitle(QString dvd_url) {
362
qDebug("Helper::dvdSplitTitle: '%s'", dvd_url.toUtf8().data());
363
QRegExp s("^dvd://(\\d+)(.*)", Qt::CaseInsensitive);
364
if (s.indexIn(dvd_url)!=-1) {
365
return s.cap(1).toInt();
372
bool Helper::directoryContainsDVD(QString directory) {
373
//qDebug("Helper::directoryContainsDVD: '%s'", directory.latin1());
376
QStringList l = dir.entryList();
378
for (int n=0; n < l.count(); n++) {
379
//qDebug(" * entry %d: '%s'", n, l[n].toUtf8().data());
380
if (l[n].toLower() == "video_ts") valid = TRUE;
386
#if COLOR_OUTPUT_SUPPORT
387
QString Helper::stripColorsTags(QString s) {
388
QRegExp rx_console_colors("\033\\[\\d\\d?;\\d\\d?m");
389
int removePos = rx_console_colors.lastIndexIn(s);
390
removePos += rx_console_colors.matchedLength();
391
return s.remove(0, removePos);