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];
234 time_t tTime = (time_t)getTime();
235 struct tm *btime = localtime(&tTime);
236 memcpy(<ime,btime, sizeof(struct tm));
238 if (btime && tTime != 0) {
240 strftime(buffer,TIMEBUFLEN, "%0g/%0m/%0d %0H:%0M ", btime);
242 strftime(buffer,TIMEBUFLEN, "%Y/%m/%d %H:%M ", btime);
247 SNPRINTF(buffer,TIMEBUFLEN,"00/00/00 00:00 ");
252 const MediaURI * Media::getURI() const {
256 void Media::setURI(const MediaURI *u) {
261 int Media::getSerializedLenImpl() {
262 int rt=4+4+1; //type,time,hasURI
263 rt+=getSerializedStringLen(fileName);
264 rt+=getSerializedStringLen(displayName);
265 if (uri) rt+=uri->getSerializedLen();
271 * 4 namelen (incl. 0)
273 * 4 displaylen (incl. 0)
278 int Media::serializeImpl(SerializeBuffer *b) {
279 if (b->encodeLong(mediaType) != 0) return -1;
280 if (b->encodeLong(mtime) != 0) return -1;
281 if (b->encodeString(fileName) != 0) return -1;
282 if (b->encodeString(displayName) != 0) return -1;
284 if (b->encodeByte(uri?1:0) != 0) return -1;
286 if (uri->serialize(b) != 0) return -1;
291 int Media::deserializeImpl(SerializeBuffer *b) {
292 if (fileName) delete fileName;
294 if (displayName) delete displayName;
298 if (b->decodeLong(mediaType) != 0) return -1;
299 if (b->decodeLong(mtime) != 0) return -1;
301 if (b->decodeString(nlen,fileName) != 0) return -1;
302 if (b->decodeString(nlen,displayName) != 0) return -1;
304 if (b->decodeByte(hasURI) != 0) return -1;
307 if (uri->deserialize(b) != 0) return -1;
314 MediaURI * MediaList::getURI(Media * m) {
315 if (! m) return NULL;
316 const MediaURI *rtc=m->getURI();
317 if (rtc) return new MediaURI(rtc);
318 if (!_root) return NULL;
319 if (! m->getFileName()) return NULL;
320 int len=strlen(m->getFileName());
321 if (_root->getName()) len+=strlen(_root->getName())+1;
322 MediaURI *rt=new MediaURI();
323 rt->_name=new char[len+1];
324 const char *fn=m->getFileName();
325 if (_root->getName()) {
326 while (*fn=='/') fn++;
327 sprintf(rt->_name,"%s/%s",_root->getName(),fn);
330 sprintf(rt->_name,"%s",fn);
332 if (m->hasDisplayName() || _root->hasDisplayName()) {
333 len=strlen(m->getDisplayName())+1;
334 if (_root->hasDisplayName()) {
335 len+=strlen(_root->getDisplayName())+2;
337 rt->_display=new char[len];
338 if (_root->hasDisplayName()) {
339 const char *sp=m->getDisplayName();
341 sprintf(rt->_display,"%s/%s",_root->getDisplayName(),sp);
344 sprintf(rt->_display,"%s",m->getDisplayName());
347 rt->_providerId=_root->_providerId;
348 rt->_allowedTypes=_root->_allowedTypes;
352 MediaURI * MediaList::getParent(MediaURI *c) {
353 MediaURI * rt=new MediaURI();
354 rt->_providerId=c->_providerId;
355 rt->_allowedTypes=c->_allowedTypes;
358 char * ls=strrchr(c->_name,'/');
361 rt->_name=new char[nlen+1];
362 strncpy(rt->_name,c->_name,nlen);
367 char * ls=strrchr(c->_display,'/');
370 rt->_display=new char[nlen+1];
371 strncpy(rt->_display,c->_display,nlen);
372 rt->_display[nlen]=0;
379 MediaList::MediaList(const MediaURI *root) {
380 _root=new MediaURI(root);
385 MediaList::~MediaList() {
388 void MediaList::emptyList(){
390 for (UINT i = 0; i < size(); i++)
396 if (_root) delete _root;
402 MediaURI * MediaList::getRootURI() {
403 if ( ! _root) return NULL;
404 return new MediaURI(_root);
407 ULONG MediaList::getProvider() {
408 if (! _root) return 0;
409 return _root->getProvider();
412 void MediaList::setOwning(bool owning) {
418 int MediaList::getSerializedLenImpl() {
419 int rt=4+1; //numelem+hasRoot
420 if (_root) rt+=_root->getSerializedLen();
421 for (MediaList::iterator it=begin();it<end();it++) {
422 rt+=(*it)->getSerializedLen();
434 int MediaList::serializeImpl(SerializeBuffer *b) {
435 if (b->encodeLong(size()) != 0) return -1;
436 if (b->encodeByte(_root?1:0) != 0) return -1;
438 if (_root->serialize(b) != 0) return -1;
440 for (MediaList::iterator it=begin();it<end();it++) {
441 if ((*it)->serialize(b) !=0) return -1;
446 int MediaList::deserializeImpl(SerializeBuffer *b) {
449 if (b->decodeLong(numelem) != 0) return -1;
451 if (b->decodeByte(hasRoot) != 0) return -1;
453 _root=new MediaURI();
454 if (_root->deserialize(b) != 0) return -1;
456 for (ULONG i=0;i<numelem;i++) {
457 Media *m=new Media();
458 if (m->deserialize(b) != 0) {