~ubuntu-branches/ubuntu/wily/smplayer/wily

« back to all changes in this revision

Viewing changes to src/corelib/helper.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Maia Kozheva
  • Date: 2009-03-31 23:05:43 UTC
  • mfrom: (1.2.2 upstream)
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: james.westby@ubuntu.com-20090331230543-0h2hfwpwlu9opbv2
* New upstream release. (Closes: #523791)
  - Reworked subtitle font preferences. (Closes: #503295)
  - No longer installs qt_fr.qm. (Closes: #486314)
* debian/control:
  - Bumped Standards-Version to 3.8.1.
  - Changed maintainer name (still the same person and GPG key).
  - Changed section to video.
  - Build-depend on zlib1g-dev for findsubtitles.
  - Require Qt >= 4.3 per readme.
  - Added ${misc:Depends}.
  - Make smplayer-translations depend on smplayer and smplayer recommend
    smplayer-translations, not the other way round. (Closes: #489375)
* debian/copyright:
  - Significantly expanded per-file with new upstream authors.
* debian/rules:
  - Make make use correct uic in install.
  - Clean svn_revision.
  - Removed get-orig-source - not needed with uscan --repack.
* debian/patches/01_gl_translation.patch:
  - Added patch to fix lrelease error on smplayer_gl.ts.
* Added debian/README.source for simple-patchsys.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  smplayer, GUI front-end for mplayer.
2
 
    Copyright (C) 2006-2008 Ricardo Villalba <rvm@escomposlinux.org>
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 <QRegExp>
25
 
#include <QDir>
26
 
#include <QTextCodec>
27
 
#include <QWidget>
28
 
#include <QLocale>
29
 
#include "config.h"
30
 
 
31
 
#include <QLibraryInfo>
32
 
 
33
 
#ifdef Q_OS_WIN
34
 
#include <windows.h> // For the screensaver stuff
35
 
#endif
36
 
 
37
 
 
38
 
#if EXTERNAL_SLEEP
39
 
#include <unistd.h>
40
 
#else
41
 
#include <qthread.h>
42
 
#endif
43
 
 
44
 
 
45
 
#if !EXTERNAL_SLEEP
46
 
class Sleeper : public QThread
47
 
{
48
 
public:
49
 
        static void sleep(unsigned long secs) {QThread::sleep(secs);}
50
 
        static void msleep(unsigned long msecs) {
51
 
                //qDebug("sleeping...");
52
 
                QThread::msleep(msecs);
53
 
                //qDebug("finished");
54
 
        }
55
 
        static void usleep(unsigned long usecs) {QThread::usleep(usecs);}
56
 
};
57
 
#endif
58
 
 
59
 
 
60
 
QString Helper::logs;
61
 
QString Helper::app_path;
62
 
QString Helper::ini_path;
63
 
 
64
 
void Helper::setAppPath(QString path) {
65
 
        app_path = path;
66
 
}
67
 
 
68
 
QString Helper::appPath() {
69
 
        return app_path;
70
 
}
71
 
 
72
 
QString Helper::dataPath() {
73
 
#ifdef DATA_PATH
74
 
        QString path = QString(DATA_PATH);
75
 
        if (!path.isEmpty())
76
 
                return path;
77
 
        else
78
 
                return appPath();
79
 
#else
80
 
        return appPath();
81
 
#endif
82
 
}
83
 
 
84
 
QString Helper::translationPath() {
85
 
#ifdef TRANSLATION_PATH
86
 
        QString path = QString(TRANSLATION_PATH);
87
 
        if (!path.isEmpty())
88
 
                return path;
89
 
        else
90
 
                return appPath() + "/translations";
91
 
#else
92
 
        return appPath() + "/translations";
93
 
#endif
94
 
}
95
 
 
96
 
QString Helper::docPath() {
97
 
#ifdef DOC_PATH
98
 
        QString path = QString(DOC_PATH);
99
 
        if (!path.isEmpty())
100
 
                return path;
101
 
        else
102
 
                return appPath() + "/docs";
103
 
#else
104
 
        return appPath() + "/docs";
105
 
#endif
106
 
}
107
 
 
108
 
QString Helper::confPath() {
109
 
#ifdef CONF_PATH
110
 
        QString path = QString(CONF_PATH);
111
 
        if (!path.isEmpty())
112
 
                return path;
113
 
        else
114
 
                return appPath();
115
 
#else
116
 
        return appPath();
117
 
#endif
118
 
}
119
 
 
120
 
QString Helper::themesPath() {
121
 
#ifdef THEMES_PATH
122
 
        QString path = QString(THEMES_PATH);
123
 
        if (!path.isEmpty())
124
 
                return path;
125
 
        else
126
 
                return appPath() + "/themes";
127
 
#else
128
 
        return appPath() + "/themes";
129
 
#endif
130
 
}
131
 
 
132
 
QString Helper::shortcutsPath() {
133
 
#ifdef SHORTCUTS_PATH
134
 
        QString path = QString(SHORTCUTS_PATH);
135
 
        if (!path.isEmpty())
136
 
                return path;
137
 
        else
138
 
                return appPath() + "/shortcuts";
139
 
#else
140
 
        return appPath() + "/shortcuts";
141
 
#endif
142
 
}
143
 
 
144
 
QString Helper::qtTranslationPath() {
145
 
        return QLibraryInfo::location(QLibraryInfo::TranslationsPath);
146
 
}
147
 
 
148
 
QString Helper::doc(QString file, QString locale) {
149
 
        if (locale.isEmpty()) {
150
 
                locale = QLocale::system().name();
151
 
        }
152
 
 
153
 
        QString f = docPath() + "/" + locale + "/" + file;
154
 
        qDebug("Helper:doc: checking '%s'", f.toUtf8().data());
155
 
        if (QFile::exists(f)) return f;
156
 
 
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;
162
 
        }
163
 
 
164
 
        f = docPath() + "/en/" + file;
165
 
        return f;
166
 
}
167
 
 
168
 
QString Helper::appHomePath() {
169
 
        return QDir::homePath() + "/.smplayer";
170
 
}
171
 
 
172
 
void Helper::setIniPath(QString path) {
173
 
        ini_path = path;
174
 
}
175
 
 
176
 
QString Helper::iniPath() {
177
 
        if (!ini_path.isEmpty()) {
178
 
                return ini_path;
179
 
        } else {
180
 
                if (QFile::exists(appHomePath())) return appHomePath();
181
 
        }
182
 
        return "";
183
 
}
184
 
 
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(' ', '_');
192
 
 
193
 
        QFileInfo fi(filename);
194
 
        if (fi.exists()) {
195
 
                s += "_" + QString::number( fi.size() );
196
 
        }
197
 
 
198
 
        return s;       
199
 
}
200
 
 
201
 
QString Helper::dvdForPref(const QString & dvd_id, int title) {
202
 
        return  QString("DVD_%1_%2").arg(dvd_id).arg(title);
203
 
}
204
 
 
205
 
 
206
 
void Helper::addLog(QString s) {
207
 
        logs += s + "\n";
208
 
}
209
 
 
210
 
QString Helper::log() { 
211
 
        return logs; 
212
 
}
213
 
 
214
 
QString Helper::formatTime(int secs) {
215
 
        int t = secs;
216
 
    int hours = (int) t / 3600;
217
 
    t -= hours*3600;
218
 
    int minutes = (int) t / 60;
219
 
    t -= minutes*60;
220
 
    int seconds = t;
221
 
 
222
 
    QString tf;
223
 
    return tf.sprintf("%02d:%02d:%02d",hours,minutes,seconds);
224
 
}
225
 
 
226
 
QString Helper::timeForJumps(int secs) {
227
 
    int minutes = (int) secs / 60;
228
 
        int seconds = secs % 60;
229
 
 
230
 
        if (minutes==0) {
231
 
                if (seconds==1) 
232
 
                        return QObject::tr("1 second");
233
 
                else
234
 
                        return QObject::tr("%1 seconds").arg(seconds);
235
 
        }
236
 
        else {
237
 
                if (minutes==1) {
238
 
                        if (seconds==0) 
239
 
                                return QObject::tr("1 minute");
240
 
                        else
241
 
                        if (seconds==1) 
242
 
                                return QObject::tr("1 minute and 1 second");
243
 
                        else
244
 
                                return QObject::tr("1 minute and %1 seconds").arg(seconds);
245
 
                } else {
246
 
                        if (seconds==0) 
247
 
                                return QObject::tr("%1 minutes").arg(minutes);
248
 
                        else
249
 
                        if (seconds==1) 
250
 
                                return QObject::tr("%1 minutes and 1 second").arg(minutes);
251
 
                        else
252
 
                                return QObject::tr("%1 minutes and %2 seconds").arg(minutes)
253
 
                                                                   .arg(seconds);
254
 
                }
255
 
        }
256
 
}
257
 
 
258
 
#ifdef Q_OS_WIN
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;
264
 
 
265
 
                const int max_path = 4096;
266
 
                WCHAR shortName[max_path];
267
 
 
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);
272
 
 
273
 
                return short_path;
274
 
        } else {
275
 
                return long_path;
276
 
        }
277
 
}
278
 
 
279
 
/*
280
 
void Helper::setScreensaverEnabled(bool b) {
281
 
        qDebug("Helper::setScreensaverEnabled: %d", b);
282
 
 
283
 
        if (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);
288
 
        } else {
289
 
                SystemParametersInfo( SPI_SETSCREENSAVEACTIVE, false, 0, SPIF_SENDWININICHANGE);
290
 
                SystemParametersInfo( SPI_SETLOWPOWERACTIVE, 0, NULL, 0);
291
 
                SystemParametersInfo( SPI_SETPOWEROFFACTIVE, 0, NULL, 0);
292
 
        }
293
 
}
294
 
*/
295
 
#endif
296
 
 
297
 
void Helper::msleep(int ms) {
298
 
#if EXTERNAL_SLEEP
299
 
        //qDebug("Helper::msleep: %d (using usleep)", ms);
300
 
        usleep(ms*1000);
301
 
#else
302
 
        //qDebug("Helper::msleep: %d (using QThread::msleep)", ms);
303
 
        Sleeper::msleep( ms );
304
 
#endif
305
 
}
306
 
 
307
 
QString Helper::colorToRRGGBBAA(unsigned int color) {
308
 
        QColor c;
309
 
        c.setRgb( color );
310
 
 
311
 
        QString s;
312
 
        return s.sprintf("%02x%02x%02x00", c.red(), c.green(), c.blue() );
313
 
}
314
 
 
315
 
QString Helper::colorToRRGGBB(unsigned int color) {
316
 
        QColor c;
317
 
        c.setRgb( color );
318
 
 
319
 
        QString s;
320
 
        return s.sprintf("%02x%02x%02x", c.red(), c.green(), c.blue() );
321
 
}
322
 
 
323
 
QString Helper::colorToRGB(unsigned int color) {
324
 
        QColor c;
325
 
        c.setRgb( color );
326
 
 
327
 
        QString s;
328
 
        return s.sprintf("0x%02x%02x%02x", c.blue(), c.green(), c.red() );
329
 
}
330
 
 
331
 
void Helper::setForegroundColor(QWidget * w, const QColor & color) {
332
 
        QPalette p = w->palette(); 
333
 
        p.setColor(w->foregroundRole(), color); 
334
 
        w->setPalette(p);
335
 
}
336
 
 
337
 
void Helper::setBackgroundColor(QWidget * w, const QColor & color) {
338
 
        QPalette p = w->palette(); 
339
 
        p.setColor(w->backgroundRole(), color); 
340
 
        w->setPalette(p);
341
 
}
342
 
 
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('/', '\\');
347
 
        else
348
 
                return filename;
349
 
}
350
 
 
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) {
355
 
                return s.cap(2);
356
 
        } else {
357
 
                return QString::null;
358
 
        }
359
 
}
360
 
 
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();
366
 
        } else {
367
 
                return -1;
368
 
        }
369
 
}
370
 
 
371
 
 
372
 
bool Helper::directoryContainsDVD(QString directory) {
373
 
        //qDebug("Helper::directoryContainsDVD: '%s'", directory.latin1());
374
 
 
375
 
        QDir dir(directory);
376
 
        QStringList l = dir.entryList();
377
 
        bool valid = FALSE;
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;
381
 
        }
382
 
 
383
 
        return valid;
384
 
}
385
 
 
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);
392
 
}
393
 
#endif
394