~ubuntu-branches/debian/sid/smplayer/sid

« back to all changes in this revision

Viewing changes to src/helper.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Maia Kozheva
  • Date: 2009-03-31 23:05:43 UTC
  • mto: (1.1.9 upstream) (3.1.2 squeeze)
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: james.westby@ubuntu.com-20090331230543-nsklbxenl2hf2n6h
Tags: upstream-0.6.7
ImportĀ upstreamĀ versionĀ 0.6.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  smplayer, GUI front-end for mplayer.
 
2
    Copyright (C) 2006-2009 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 <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
 
41
class Sleeper : public QThread
 
42
{
 
43
public:
 
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
/*
 
55
QString Helper::dvdForPref(const QString & dvd_id, int title) {
 
56
        return  QString("DVD_%1_%2").arg(dvd_id).arg(title);
 
57
}
 
58
*/
 
59
 
 
60
QString 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
 
 
72
QString Helper::timeForJumps(int secs) {
 
73
    int minutes = (int) secs / 60;
 
74
        int seconds = secs % 60;
 
75
 
 
76
        if (minutes==0) {
 
77
                return QObject::tr("%1 second(s)", "", seconds).arg(seconds);
 
78
        } else {
 
79
                if (seconds==0) 
 
80
                        return QObject::tr("%1 minute(s)", "", minutes).arg(minutes);
 
81
                else {
 
82
                        QString m = QObject::tr("%1 minute(s)", "", minutes).arg(minutes);
 
83
                        QString s = QObject::tr("%1 second(s)", "", seconds).arg(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
 
92
QString 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
/*
 
111
void 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
 
 
128
void 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
 
 
138
QString 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
 
 
146
bool 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
 
 
160
int 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
 
 
178
QString 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
 
 
198
QStringList Helper::searchForConsecutiveFiles(const QString & initial_file) {
 
199
        qDebug("Helper::searchForConsecutiveFiles: initial_file: '%s'", initial_file.toUtf8().constData());
 
200
 
 
201
        QStringList files_to_add;
 
202
 
 
203
        QFileInfo fi(initial_file);
 
204
        QString basename = fi.completeBaseName();
 
205
        QString extension = fi.suffix();
 
206
        QString path = fi.absolutePath();
 
207
 
 
208
        QRegExp rx("^.*(\\d+)");
 
209
 
 
210
        if ( rx.indexIn(basename) > -1) {
 
211
                int digits = rx.cap(1).length();
 
212
                int current_number = rx.cap(1).toInt();
 
213
 
 
214
                //qDebug("Helper::searchForConsecutiveFiles: filename ends with a number (%s)", rx.cap(1).toUtf8().constData());
 
215
                qDebug("Helper::searchForConsecutiveFiles: filename ends with a number (%d)", current_number);
 
216
                qDebug("Helper::searchForConsecutiveFiles: trying to find consecutive files");
 
217
 
 
218
                QString template_name = path + "/" + basename.left(basename.length() - digits);
 
219
                //qDebug("BaseGui::newMediaLoaded: name without digits: '%s'", template_name.toUtf8().constData());
 
220
 
 
221
                current_number++;
 
222
                QString next_name = template_name + QString("%1").arg(current_number, digits, 10, QLatin1Char('0')) +"."+ extension;
 
223
                qDebug("Helper::searchForConsecutiveFiles: looking for '%s'", next_name.toUtf8().constData());
 
224
 
 
225
                while (QFile::exists(next_name)) {
 
226
                        qDebug("Helper::searchForConsecutiveFiles: '%s' exists, added to the list", next_name.toUtf8().constData());
 
227
                        files_to_add.append(next_name);
 
228
                        current_number++;
 
229
                        next_name = template_name + QString("%1").arg(current_number, digits, 10, QLatin1Char('0')) +"."+ extension;
 
230
                        qDebug("Helper::searchForConsecutiveFiles: looking for '%s'", next_name.toUtf8().constData());
 
231
                }
 
232
        }
 
233
 
 
234
        return files_to_add;
 
235
}