2 Copyright 2004-2005 Chris Tallon, Andreas Vogel
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include <arpa/inet.h>
27 #define SNPRINTF snprintf
31 MediaURI::MediaURI(const MediaURI *cp) {
36 _allowedTypes=MEDIA_TYPE_ALL;
39 _providerId=cp->_providerId;
40 _allowedTypes=cp->_allowedTypes;
43 _name=new char[strlen(cp->_name)+1];
44 strcpy(_name,cp->_name);
48 _display=new char[strlen(cp->_display)+1];
49 strcpy(_display,cp->_display);
53 MediaURI::MediaURI(ULONG p, const char * n, const char * dp) {
54 _allowedTypes=MEDIA_TYPE_ALL;
57 _name=new char[strlen(n)+1];
64 _display=new char[strlen(dp)+1];
70 int MediaURI::getSerializedLenImpl() {
71 int rt=4+4; //provider+allowedType
72 rt+=getSerializedStringLen(_name);
73 rt+=getSerializedStringLen(_display);
81 * 4 displaylen (incl. 0)
84 int MediaURI::serializeImpl(SerializeBuffer *b) {
86 if (b->encodeLong(_providerId) != 0) return -1;
87 if (b->encodeLong(_allowedTypes) != 0) return -1;
88 if (b->encodeString(_name) != 0) return -1;
89 if (b->encodeString(_display) != 0) return -1;
93 int MediaURI::deserializeImpl(SerializeBuffer *b) {
94 if (_name) delete _name;
96 if (_display) delete _display;
98 if (b->decodeLong(_providerId) != 0) return -1;
99 if (b->decodeLong(_allowedTypes) != 0) return -1;
101 if (b->decodeString(nlen,_name) != 0) return -1;
102 if (b->decodeString(nlen,_display) != 0) return -1;
103 //if (version > 1) ...
110 int MediaInfo::getSerializedLenImpl() {
111 int rt=8+1+4+4; //8len+1canPos+4type+4subtype
117 * serialize to buffer
123 int MediaInfo::serializeImpl(SerializeBuffer *b) {
124 if (b->encodeLongLong(size) != 0) return -1;
125 if (b->encodeByte(canPosition?1:0) != 0) return -1;
126 if (b->encodeLong(type) != 0) return -1;
127 if (b->encodeLong(subtype) != 0) return -1;
132 * should be compatible to older serialize functions
134 int MediaInfo::deserializeImpl(SerializeBuffer *b) {
135 if (b->decodeLongLong(size) != 0) return -1;
137 if (b->decodeByte(cp) != 0) return -1;
139 if (b->decodeLong(type) != 0) return -1;
140 if (b->decodeLong(subtype) != 0) return -1;
151 mediaType=MEDIA_TYPE_UNKNOWN;
156 Media::Media(const Media *m) {
160 mediaType=m->mediaType;
164 if (m->uri) uri=new MediaURI(m->uri);
165 setFileName(m->fileName);
166 setDisplayName(m->displayName);
172 if (displayName) { delete[] displayName; displayName = NULL; }
173 if (fileName) { delete[] fileName; fileName = NULL; }
175 index = -1; // just in case
178 ULONG Media::getTime() const
183 const char* Media::getDisplayName() const
185 if (displayName) return displayName;
189 const char* Media::getFileName() const
194 void Media::setTime(ULONG tstartTime)
199 void Media::setMediaType(ULONG mtype)
204 ULONG Media::getMediaType() const
209 void Media::setDisplayName(const char* tDisplayName)
211 if (displayName) delete[] displayName;
213 if (! tDisplayName) return;
214 displayName = new char[strlen(tDisplayName) + 1];
215 if (displayName) strcpy(displayName, tDisplayName);
218 void Media::setFileName(const char* tFileName)
220 if (fileName) delete[] fileName;
222 if (! tFileName) return;
223 fileName = new char[strlen(tFileName) + 1];
224 if (fileName) strcpy(fileName, tFileName);
227 bool Media::hasDisplayName() const {
228 return (displayName != NULL);
231 char * Media::getTimeString(char * buffer) const {
232 if (! buffer) buffer=new char[TIMEBUFLEN];
233 time_t tTime = (time_t)getTime();
235 LOCALTIME_R(&tTime, &btime);
239 strftime(buffer,TIMEBUFLEN, "%0g/%0m/%0d %0H:%0M ", &btime);
241 strftime(buffer,TIMEBUFLEN, "%Y/%m/%d %H:%M ", &btime);
246 SNPRINTF(buffer,TIMEBUFLEN,"00/00/00 00:00 ");
251 const MediaURI * Media::getURI() const {
255 void Media::setURI(const MediaURI *u) {
260 int Media::getSerializedLenImpl() {
261 int rt=4+4+1; //type,time,hasURI
262 rt+=getSerializedStringLen(fileName);
263 rt+=getSerializedStringLen(displayName);
264 if (uri) rt+=uri->getSerializedLen();
270 * 4 namelen (incl. 0)
272 * 4 displaylen (incl. 0)
277 int Media::serializeImpl(SerializeBuffer *b) {
278 if (b->encodeLong(mediaType) != 0) return -1;
279 if (b->encodeLong(mtime) != 0) return -1;
280 if (b->encodeString(fileName) != 0) return -1;
281 if (b->encodeString(displayName) != 0) return -1;
283 if (b->encodeByte(uri?1:0) != 0) return -1;
285 if (uri->serialize(b) != 0) return -1;
290 int Media::deserializeImpl(SerializeBuffer *b) {
291 if (fileName) delete fileName;
293 if (displayName) delete displayName;
297 if (b->decodeLong(mediaType) != 0) return -1;
298 if (b->decodeLong(mtime) != 0) return -1;
300 if (b->decodeString(nlen,fileName) != 0) return -1;
301 if (b->decodeString(nlen,displayName) != 0) return -1;
303 if (b->decodeByte(hasURI) != 0) return -1;
306 if (uri->deserialize(b) != 0) return -1;
313 MediaURI * MediaList::getURI(Media * m) {
314 if (! m) return NULL;
315 const MediaURI *rtc=m->getURI();
316 if (rtc) return new MediaURI(rtc);
317 if (!_root) return NULL;
318 if (! m->getFileName()) return NULL;
319 int len=strlen(m->getFileName());
320 if (_root->getName()) len+=strlen(_root->getName())+1;
321 MediaURI *rt=new MediaURI();
322 rt->_name=new char[len+1];
323 const char *fn=m->getFileName();
324 if (_root->getName()) {
325 while (*fn=='/') fn++;
326 sprintf(rt->_name,"%s/%s",_root->getName(),fn);
329 sprintf(rt->_name,"%s",fn);
331 if (m->hasDisplayName() || _root->hasDisplayName()) {
332 len=strlen(m->getDisplayName())+1;
333 if (_root->hasDisplayName()) {
334 len+=strlen(_root->getDisplayName())+2;
336 rt->_display=new char[len];
337 if (_root->hasDisplayName()) {
338 const char *sp=m->getDisplayName();
340 sprintf(rt->_display,"%s/%s",_root->getDisplayName(),sp);
343 sprintf(rt->_display,"%s",m->getDisplayName());
346 rt->_providerId=_root->_providerId;
347 rt->_allowedTypes=_root->_allowedTypes;
351 MediaURI * MediaList::getParent(MediaURI *c) {
352 MediaURI * rt=new MediaURI();
353 rt->_providerId=c->_providerId;
354 rt->_allowedTypes=c->_allowedTypes;
357 char * ls=strrchr(c->_name,'/');
360 rt->_name=new char[nlen+1];
361 strncpy(rt->_name,c->_name,nlen);
366 char * ls=strrchr(c->_display,'/');
369 rt->_display=new char[nlen+1];
370 strncpy(rt->_display,c->_display,nlen);
371 rt->_display[nlen]=0;
378 MediaList::MediaList(const MediaURI *root) {
379 _root=new MediaURI(root);
384 MediaList::~MediaList() {
387 void MediaList::emptyList(){
389 for (UINT i = 0; i < size(); i++)
395 if (_root) delete _root;
401 MediaURI * MediaList::getRootURI() {
402 if ( ! _root) return NULL;
403 return new MediaURI(_root);
406 ULONG MediaList::getProvider() {
407 if (! _root) return 0;
408 return _root->getProvider();
411 void MediaList::setOwning(bool owning) {
417 int MediaList::getSerializedLenImpl() {
418 int rt=4+1; //numelem+hasRoot
419 if (_root) rt+=_root->getSerializedLen();
420 for (MediaList::iterator it=begin();it<end();it++) {
421 rt+=(*it)->getSerializedLen();
433 int MediaList::serializeImpl(SerializeBuffer *b) {
434 if (b->encodeLong(size()) != 0) return -1;
435 if (b->encodeByte(_root?1:0) != 0) return -1;
437 if (_root->serialize(b) != 0) return -1;
439 for (MediaList::iterator it=begin();it<end();it++) {
440 if ((*it)->serialize(b) !=0) return -1;
445 int MediaList::deserializeImpl(SerializeBuffer *b) {
448 if (b->decodeLong(numelem) != 0) return -1;
450 if (b->decodeByte(hasRoot) != 0) return -1;
452 _root=new MediaURI();
453 if (_root->deserialize(b) != 0) return -1;
455 for (ULONG i=0;i<numelem;i++) {
456 Media *m=new Media();
457 if (m->deserialize(b) != 0) {