summaryrefslogtreecommitdiff
path: root/src/mplayerversion.cpp
blob: 1f91fc3dbe3a905e1b44bc7ecd6a763009b55c66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*  smplayer, GUI front-end for mplayer.
    Copyright (C) 2006-2012 Ricardo Villalba <rvm@users.sourceforge.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "mplayerversion.h"
#include "global.h"
#include "preferences.h"

#include <QRegExp>

using namespace Global;

QString MplayerVersion::mplayer2_version;
bool MplayerVersion::is_mplayer2 = false;

int MplayerVersion::mplayerVersion(QString string) {
	//static QRegExp rx_mplayer_revision("^MPlayer (\\S+)-SVN-r(\\d+)-(.*)");
	static QRegExp rx_mplayer_revision("^MPlayer (.*)[-\\.]r(\\d+)(.*)");
	static QRegExp rx_mplayer_version("^MPlayer ([a-z,0-9,.]+)-(.*)");
	static QRegExp rx_mplayer_git("^MPlayer GIT(.*)", Qt::CaseInsensitive);
	static QRegExp rx_mplayer_version_final("1.0rc([0-9])");
	static QRegExp rx_mplayer2_version("^MPlayer2 (.*) \\(C\\).*");
#ifndef Q_OS_WIN
	static QRegExp rx_mplayer_version_ubuntu("^MPlayer (\\d):(\\d)\\.(\\d)~(.*)");
	static QRegExp rx_mplayer_version_mandriva("^MPlayer ([a-z0-9\\.]+)-\\d+\\.([a-z0-9]+)\\.[\\d\\.]+[a-z]+[\\d\\.]+-(.*)");
#endif

	int mplayer_svn = 0;
	mplayer2_version = QString::null;
	is_mplayer2 = false;

#ifdef Q_OS_WIN
	// Hack to recognize mplayer 1.0rc2 from CCCP:
	if (string.startsWith("MPlayer CCCP ")) { 
		string.remove("CCCP "); 
		qDebug("MplayerVersion::mplayerVersion: removing CCCP: '%s'", string.toUtf8().data()); 
	}
#else
	// Hack to recognize mplayer 1.0rc1 from Ubuntu:
	if (rx_mplayer_version_ubuntu.indexIn(string) > -1) {
		int v1 = rx_mplayer_version_ubuntu.cap(2).toInt();
		int v2 = rx_mplayer_version_ubuntu.cap(3).toInt();
		QString rest = rx_mplayer_version_ubuntu.cap(4);
		//qDebug("%d - %d - %d", rx_mplayer_version_ubuntu.cap(1).toInt(), v1 , v2);
		string = QString("MPlayer %1.%2%3").arg(v1).arg(v2).arg(rest);
		qDebug("MplayerVersion::mplayerVersion: line converted to '%s'", string.toUtf8().data());
	}
	// Hack to recognize mplayer version from Mandriva:
	if (rx_mplayer_version_mandriva.indexIn(string) > -1) {
		QString v1 = rx_mplayer_version_mandriva.cap(1);
		QString v2 = rx_mplayer_version_mandriva.cap(2);
		QString rest = rx_mplayer_version_mandriva.cap(3);
		string = QString("MPlayer %1%2-%3").arg(v1).arg(v2).arg(rest);
		qDebug("MplayerVersion::mplayerVersion: line converted to '%s'", string.toUtf8().data());
	}
#endif

	if (rx_mplayer_git.indexIn(string) > -1) {
		qDebug("MplayerVersion::mplayerVersion: MPlayer from git. Assuming >= 1.0rc3");
		mplayer_svn = MPLAYER_1_0_RC3_SVN;
	}
	else
	if (rx_mplayer_revision.indexIn(string) > -1) {
		mplayer_svn = rx_mplayer_revision.cap(2).toInt();
		qDebug("MplayerVersion::mplayerVersion: MPlayer SVN revision found: %d", mplayer_svn);
	} 
	else
	if (rx_mplayer_version.indexIn(string) > -1) {
		QString version = rx_mplayer_version.cap(1);
		qDebug("MplayerVersion::mplayerVersion: MPlayer version found: %s", version.toUtf8().data());
		mplayer_svn = 0;

		if (version == "1.0rc4") mplayer_svn = MPLAYER_1_0_RC4_SVN;
		else
		if (version == "1.0rc3") mplayer_svn = MPLAYER_1_0_RC3_SVN;
		else
		if (version == "1.0rc2") mplayer_svn = MPLAYER_1_0_RC2_SVN;
		else
		if (version == "1.0rc1") mplayer_svn = MPLAYER_1_0_RC1_SVN;
		else
		if(rx_mplayer_version_final.indexIn(version) > -1 && rx_mplayer_version_final.cap(1).toInt() > 3)
			mplayer_svn = MPLAYER_1_0_RC3_SVN; //version is > 1.0rc3, so treat as 1.0rc3 since support for later versions is not yet implemented

		else
			qWarning("MplayerVersion::mplayerVersion: unknown MPlayer version");

	}
	else
	if (rx_mplayer2_version.indexIn(string) > -1) {
		mplayer2_version = rx_mplayer2_version.cap(1);
		qDebug("MplayerVersion::mplayerVersion: MPlayer2 version found: %s", mplayer2_version.toUtf8().data());
		is_mplayer2 = true;
		mplayer_svn = MPLAYER_1_0_RC4_SVN; // simulates mplayer 1.0rc4
	}

	if (pref) {
		pref->mplayer_detected_version = mplayer_svn;
		pref->mplayer_is_mplayer2 = is_mplayer2;
		pref->mplayer2_detected_version = mplayer2_version;
	}

	return mplayer_svn;
}

bool MplayerVersion::isMplayerAtLeast(int mplayer_svn, int svn_revision) {
	qDebug("MplayerVersion::isMplayerAtLeast: comparing %d with %d", svn_revision, mplayer_svn);

	if (mplayer_svn == -1) {
		qWarning("MplayerVersion::isMplayerAtLeast: no version found!");
	}
	else
	if (mplayer_svn == 0) {
		qWarning("MplayerVersion::isMplayerAtLeast: version couldn't be parsed!");
	}

	if (mplayer_svn <= 0) {
		qWarning("MplayerVersion::isMplayerAtLeast: assuming that the mplayer version is less than %d", svn_revision);
		return false;
	}

	return (mplayer_svn >= svn_revision);
}

bool MplayerVersion::isMplayerAtLeast(int svn_revision) {
	if (pref->mplayer_detected_version >= MPLAYER_1_0_RC1_SVN) {
		// SVN version seems valid
		if (pref->mplayer_user_supplied_version != -1) {
			qDebug("MplayerVersion::isMplayerAtLeast: using the parsed svn version from mplayer output");
			qDebug("MplayerVersion::isMplayerAtLeast: and clearing the previously user supplied version");
			pref->mplayer_user_supplied_version = -1;
		}
		return isMplayerAtLeast(pref->mplayer_detected_version, svn_revision);
	} 
	else 
	if (pref->mplayer_user_supplied_version != -1) {
		qDebug("MplayerVersion::isMplayerAtLeast: no parsed version, using user supplied version");
		return isMplayerAtLeast(pref->mplayer_user_supplied_version, svn_revision);
	}
	else {
		qWarning("MplayerVersion::isMplayerAtLeast: there's no parsed version nor user supplied version!");
		return isMplayerAtLeast(pref->mplayer_detected_version, svn_revision);
	}
}

QString MplayerVersion::toString(int svn_revision) {
	QString version;

	switch (svn_revision) {
		case MPLAYER_1_0_RC1_SVN: version = QString("1.0rc1"); break;
		case MPLAYER_1_0_RC2_SVN: version = QString("1.0rc2"); break;
		case MPLAYER_1_0_RC3_SVN: version = QString("1.0rc3"); break;
		case MPLAYER_1_0_RC4_SVN: version = QString("1.0rc4"); break;
		default : version =  QString("SVN r%1").arg(svn_revision);
	}

	return version;
}