1 | /* smplayer, GUI front-end for mplayer.
|
---|
2 | Copyright (C) 2006-2011 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 "discname.h"
|
---|
20 | #include <QRegExp>
|
---|
21 |
|
---|
22 | QString DiscName::joinDVD(int title, const QString & device, bool use_dvdnav) {
|
---|
23 | return join(use_dvdnav ? DVDNAV : DVD, title, device);
|
---|
24 | }
|
---|
25 |
|
---|
26 | QString DiscName::join(const DiscData & d) {
|
---|
27 | QString s = d.protocol + "://";
|
---|
28 | if (d.title > 0) s += QString::number(d.title);
|
---|
29 | if (!d.device.isEmpty()) s+= "/" + removeTrailingSlash(d.device);
|
---|
30 |
|
---|
31 | qDebug("DiscName::join: result: '%s'", s.toUtf8().constData());
|
---|
32 | return s;
|
---|
33 | }
|
---|
34 |
|
---|
35 | QString DiscName::join(Disc type, int title, const QString & device) {
|
---|
36 | QString protocol;
|
---|
37 | switch (type) {
|
---|
38 | case DVD: protocol = "dvd"; break;
|
---|
39 | case DVDNAV: protocol = "dvdnav"; break;
|
---|
40 | case VCD: protocol = "vcd"; break;
|
---|
41 | case CDDA: protocol = "cdda"; break;
|
---|
42 | }
|
---|
43 | return join( DiscData(protocol, title, device) );
|
---|
44 | }
|
---|
45 |
|
---|
46 | DiscData DiscName::split(const QString & disc_url, bool * ok) {
|
---|
47 | qDebug("DiscName::split: disc_url: '%s'", disc_url.toUtf8().constData());
|
---|
48 |
|
---|
49 | QRegExp rx1("^(dvd|dvdnav|vcd|cdda)://(\\d+)/(.*)");
|
---|
50 | QRegExp rx2("^(dvd|dvdnav|vcd|cdda)://(\\d+)");
|
---|
51 | QRegExp rx3("^(dvd|dvdnav|vcd|cdda):///(.*)");
|
---|
52 | QRegExp rx4("^(dvd|dvdnav|vcd|cdda):(.*)");
|
---|
53 |
|
---|
54 | DiscData d;
|
---|
55 |
|
---|
56 | bool success = false;
|
---|
57 |
|
---|
58 | if (rx1.indexIn(disc_url) != -1) {
|
---|
59 | d.protocol = rx1.cap(1);
|
---|
60 | d.title = rx1.cap(2).toInt();
|
---|
61 | d.device = rx1.cap(3);
|
---|
62 | success = true;
|
---|
63 | }
|
---|
64 | else
|
---|
65 | if (rx2.indexIn(disc_url) != -1) {
|
---|
66 | d.protocol = rx2.cap(1);
|
---|
67 | d.title = rx2.cap(2).toInt();
|
---|
68 | d.device = "";
|
---|
69 | success = true;
|
---|
70 | }
|
---|
71 | else
|
---|
72 | if (rx3.indexIn(disc_url) != -1) {
|
---|
73 | d.protocol = rx3.cap(1);
|
---|
74 | d.title = 0;
|
---|
75 | d.device = rx3.cap(2);
|
---|
76 | success = true;
|
---|
77 | }
|
---|
78 | else
|
---|
79 | if (rx4.indexIn(disc_url) != -1) {
|
---|
80 | d.protocol = rx4.cap(1);
|
---|
81 | d.title = 0;
|
---|
82 | d.device ="";
|
---|
83 | success = true;
|
---|
84 | }
|
---|
85 |
|
---|
86 | if (!d.device.isEmpty()) d.device = removeTrailingSlash(d.device);
|
---|
87 |
|
---|
88 | if (success) {
|
---|
89 | qDebug("DiscName::split: protocol: '%s'", d.protocol.toUtf8().constData());
|
---|
90 | qDebug("DiscName::split: title: '%d'", d.title);
|
---|
91 | qDebug("DiscName::split: device: '%s'", d.device.toUtf8().constData());
|
---|
92 | } else {
|
---|
93 | qWarning("DiscName::split: no match in regular expression");
|
---|
94 | }
|
---|
95 |
|
---|
96 | if (ok != 0) (*ok) = success;
|
---|
97 |
|
---|
98 | return d;
|
---|
99 | }
|
---|
100 |
|
---|
101 | // This functions remove the trailing "/" from the device
|
---|
102 | // with one exception: from Windows drives letters (D:/ E:/...)
|
---|
103 | QString DiscName::removeTrailingSlash(const QString & device) {
|
---|
104 | QString dev = device;
|
---|
105 |
|
---|
106 | if (dev.endsWith("/")) {
|
---|
107 | #ifdef Q_OS_WIN
|
---|
108 | QRegExp r("^[A-Z]:/$");
|
---|
109 | int pos = r.indexIn(dev);
|
---|
110 | //qDebug("DiscName::removeTrailingSlash: drive check: '%s': regexp: %d", dev.toUtf8().data(), pos);
|
---|
111 | if (pos == -1)
|
---|
112 | #endif
|
---|
113 | dev = dev.remove( dev.length()-1, 1);
|
---|
114 | }
|
---|
115 |
|
---|
116 | return dev;
|
---|
117 | }
|
---|
118 |
|
---|
119 | #if DISCNAME_TEST
|
---|
120 | void DiscName::test() {
|
---|
121 | DiscData d;
|
---|
122 | d = split( "vcd://1//dev/dvd/" );
|
---|
123 | d = split( "vcd://1/E:/" );
|
---|
124 | d = split( "vcd://5" );
|
---|
125 | d = split( "vcd://" );
|
---|
126 | d = split( "vcd:////dev/dvdrecorder" );
|
---|
127 |
|
---|
128 | d = split( "dvd://1//dev/dvd" );
|
---|
129 | d = split( "dvd://1/E:" );
|
---|
130 | d = split( "dvd://5" );
|
---|
131 | d = split( "dvd://" );
|
---|
132 | d = split( "dvd:" );
|
---|
133 | d = split( "dvd:////dev/dvdrecorder" );
|
---|
134 |
|
---|
135 | d = split( "cdda://1//dev/dvd" );
|
---|
136 | d = split( "cdda://1/E:" );
|
---|
137 | d = split( "cdda://5" );
|
---|
138 | d = split( "cdda://" );
|
---|
139 | d = split( "cdda:////dev/dvdrecorder" );
|
---|
140 |
|
---|
141 | d = split( "dvdnav://1//dev/dvd" );
|
---|
142 | d = split( "dvdnav://1/D:/" );
|
---|
143 | d = split( "dvdnav://5" );
|
---|
144 | d = split( "dvdnav://" );
|
---|
145 | d = split( "dvdnav:////dev/dvdrecorder/" );
|
---|
146 |
|
---|
147 | QString s;
|
---|
148 | s = join( DVD, 4, "/dev/dvd/" );
|
---|
149 | s = join( DVD, 2, "E:" );
|
---|
150 | s = join( DVD, 3, "E:/" );
|
---|
151 | s = join( DVDNAV, 5, "/dev/dvdrecorder" );
|
---|
152 | s = join( VCD, 1, "/dev/cdrom" );
|
---|
153 | s = join( CDDA, 10, "/dev/cdrom" );
|
---|
154 | s = joinDVD( 1, "/dev/cdrom", false );
|
---|
155 | s = joinDVD( 2, "/dev/cdrom/", true );
|
---|
156 | s = joinDVD( 3, "", true );
|
---|
157 | s = join( VCD, 3, "" );
|
---|
158 | }
|
---|
159 | #endif
|
---|