1
/*  This file is part of the KDE project
2
    Copyright (C) 2007 Matthias Kretz <kretz@kde.org>
3
4
    This library is free software; you can redistribute it and/or
5
    modify it under the terms of the GNU Lesser General Public
6
    License as published by the Free Software Foundation; either
7
    version 2.1 of the License, or (at your option) version 3, or any
8
    later version accepted by the membership of KDE e.V. (or its
9
    successor approved by the membership of KDE e.V.), Nokia Corporation 
10
    (or its successors, if any) and the KDE Free Qt Foundation, which shall
11
    act as a proxy defined in Section 6 of version 3 of the license.
12
13
    This library is distributed in the hope that it will be useful,
14
    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
    Lesser General Public License for more details.
17
18
    You should have received a copy of the GNU Lesser General Public 
19
    License along with this library.  If not, see <http://www.gnu.org/licenses/>.
20
21
*/
22
23
#include "mediasource.h"
24
#include "mediasource_p.h"
25
#include "iodevicestream_p.h"
26
#include "abstractmediastream_p.h"
27
28
#include <QtCore/QFileInfo>
29
#include <QtCore/QFile>
30
#include <QtCore/QFSFileEngine>
31
32
QT_BEGIN_NAMESPACE
33
34
namespace Phonon
35
{
36
37
MediaSource::MediaSource(MediaSourcePrivate &dd)
38
    : d(&dd)
39
{
40
}
41
42
MediaSource::MediaSource()
43
    : d(new MediaSourcePrivate(Empty))
44
{
45
}
46
47
MediaSource::MediaSource(const QString &filename)
48
    : d(new MediaSourcePrivate(LocalFile))
49
{
50
    const QFileInfo fileInfo(filename);
51
    if (fileInfo.exists()) {
52
        bool localFs = QAbstractFileEngine::LocalDiskFlag & QFSFileEngine(filename).fileFlags(QAbstractFileEngine::LocalDiskFlag);
53
        if (localFs && !filename.startsWith(QLatin1String(":/")) && !filename.startsWith(QLatin1String("qrc://"))) {
54
            d->url = QUrl::fromLocalFile(fileInfo.absoluteFilePath());
55
        } else {
56
#ifndef QT_NO_PHONON_ABSTRACTMEDIASTREAM
57
            // it's a Qt resource -> use QFile
58
            d->type = Stream;
59
            d->ioDevice = new QFile(filename);
60
            d->setStream(new IODeviceStream(d->ioDevice, d->ioDevice));
61
            d->url =  QUrl::fromLocalFile(fileInfo.absoluteFilePath());
62
#else
63
            d->type = Invalid;
64
#endif //QT_NO_PHONON_ABSTRACTMEDIASTREAM
65
        }
66
    } else {
67
        d->url = filename;
68
        if (d->url.isValid()) {
69
            d->type = Url;
70
        } else {
71
            d->type = Invalid;
72
        }
73
    }
74
}
75
76
MediaSource::MediaSource(const QUrl &url)
77
    : d(new MediaSourcePrivate(Url))
78
{
79
    if (url.isValid()) {
80
        d->url = url;
81
    } else {
82
        d->type = Invalid;
83
    }
84
}
85
86
MediaSource::MediaSource(Phonon::DiscType dt, const QString &deviceName)
87
    : d(new MediaSourcePrivate(Disc))
88
{
89
    if (dt == NoDisc) {
90
        d->type = Invalid;
91
        return;
92
    }
93
    d->discType = dt;
94
    d->deviceName = deviceName;
95
}
96
97
#ifndef QT_NO_PHONON_ABSTRACTMEDIASTREAM
98
MediaSource::MediaSource(AbstractMediaStream *stream)
99
    : d(new MediaSourcePrivate(Stream))
100
{
101
    if (stream) {
102
        d->setStream(stream);
103
    } else {
104
        d->type = Invalid;
105
    }
106
}
107
108
MediaSource::MediaSource(QIODevice *ioDevice)
109
    : d(new MediaSourcePrivate(Stream))
110
{
111
    if (ioDevice) {
112
        d->setStream(new IODeviceStream(ioDevice, ioDevice));
113
        d->ioDevice = ioDevice;
114
    } else {
115
        d->type = Invalid;
116
    }
117
}
118
#endif //QT_NO_PHONON_ABSTRACTMEDIASTREAM
119
120
/* post 4.0
121
MediaSource::MediaSource(const QList<MediaSource> &mediaList)
122
    : d(new MediaSourcePrivate(Link))
123
{
124
    d->linkedSources = mediaList;
125
    foreach (MediaSource ms, mediaList) {
126
        Q_ASSERT(ms.type() != Link);
127
    }
128
}
129
130
QList<MediaSource> MediaSource::substreams() const
131
{
132
    return d->linkedSources;
133
}
134
*/
135
136
MediaSource::~MediaSource()
137
{
138
}
139
140
MediaSourcePrivate::~MediaSourcePrivate()
141
{
142
#ifndef QT_NO_PHONON_ABSTRACTMEDIASTREAM
143
    if (autoDelete) {
144
        //here we use deleteLater because this object
145
        //might be destroyed from another thread
146
        if (stream)
147
            stream->deleteLater();
148
        if (ioDevice)
149
            ioDevice->deleteLater();
150
    }
151
#endif //QT_NO_PHONON_ABSTRACTMEDIASTREAM
152
}
153
154
MediaSource::MediaSource(const MediaSource &rhs)
155
    : d(rhs.d)
156
{
157
}
158
159
MediaSource &MediaSource::operator=(const MediaSource &rhs)
160
{
161
    d = rhs.d;
162
    return *this;
163
}
164
165
bool MediaSource::operator==(const MediaSource &rhs) const
166
{
167
    return d == rhs.d;
168
}
169
170
void MediaSource::setAutoDelete(bool autoDelete)
171
{
172
    d->autoDelete = autoDelete;
173
}
174
175
bool MediaSource::autoDelete() const
176
{
177
    return d->autoDelete;
178
}
179
180
MediaSource::Type MediaSource::type() const
181
{
182
#ifndef QT_NO_PHONON_ABSTRACTMEDIASTREAM
183
    if (d->type == Stream && d->stream == 0) {
184
        return Invalid;
185
    }
186
#endif //QT_NO_PHONON_ABSTRACTMEDIASTREAM
187
    return d->type;
188
}
189
190
QString MediaSource::fileName() const
191
{
192
    return d->url.toLocalFile();
193
}
194
195
QUrl MediaSource::url() const
196
{
197
    return d->url;
198
}
199
200
Phonon::DiscType MediaSource::discType() const
201
{
202
    return d->discType;
203
}
204
205
QString MediaSource::deviceName() const
206
{
207
    return d->deviceName;
208
}
209
210
#ifndef QT_NO_PHONON_ABSTRACTMEDIASTREAM
211
AbstractMediaStream *MediaSource::stream() const
212
{
213
    return d->stream;
214
}
215
216
void MediaSourcePrivate::setStream(AbstractMediaStream *s)
217
{
218
    stream = s;
219
}
220
#endif //QT_NO_PHONON_ABSTRACTMEDIASTREAM
221
222
223
//X AudioCaptureDevice MediaSource::audioCaptureDevice() const
224
//X {
225
//X     return d->audioCaptureDevice;
226
//X }
227
//X 
228
//X VideoCaptureDevice MediaSource::videoCaptureDevice() const
229
//X {
230
//X     return d->videoCaptureDevice;
231
//X }
232
233
} // namespace Phonon
234
235
QT_END_NAMESPACE
236
237
// vim: sw=4 sts=4 et tw=100