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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <arpa/inet.h>
25 #define SNPRINTF snprintf
29 MediaURI::MediaURI(const MediaURI *cp) {
34 _allowedTypes=MEDIA_TYPE_ALL;
37 _providerId=cp->_providerId;
38 _allowedTypes=cp->_allowedTypes;
41 _name=new char[strlen(cp->_name)+1];
42 strcpy(_name,cp->_name);
46 _display=new char[strlen(cp->_display)+1];
47 strcpy(_display,cp->_display);
51 MediaURI::MediaURI(ULONG p, const char * n, const char * dp) {
52 _allowedTypes=MEDIA_TYPE_ALL;
55 _name=new char[strlen(n)+1];
62 _display=new char[strlen(dp)+1];
68 int MediaURI::getSerializedLenImpl() {
69 int rt=4+4; //provider+allowedType
70 rt+=getSerializedStringLen(_name);
71 rt+=getSerializedStringLen(_display);
79 * 4 displaylen (incl. 0)
82 int MediaURI::serializeImpl(SerializeBuffer *b) {
84 if (b->encodeLong(_providerId) != 0) return -1;
85 if (b->encodeLong(_allowedTypes) != 0) return -1;
86 if (b->encodeString(_name) != 0) return -1;
87 if (b->encodeString(_display) != 0) return -1;
91 int MediaURI::deserializeImpl(SerializeBuffer *b) {
92 if (_name) delete _name;
94 if (_display) delete _display;
96 if (b->decodeLong(_providerId) != 0) return -1;
97 if (b->decodeLong(_allowedTypes) != 0) return -1;
99 if (b->decodeString(nlen,_name) != 0) return -1;
100 if (b->decodeString(nlen,_display) != 0) return -1;
101 //if (version > 1) ...
108 int MediaInfo::getSerializedLenImpl() {
109 int rt=8+1+4+4; //8len+1canPos+4type+4subtype
115 * serialize to buffer
121 int MediaInfo::serializeImpl(SerializeBuffer *b) {
122 if (b->encodeLongLong(size) != 0) return -1;
123 if (b->encodeByte(canPosition?1:0) != 0) return -1;
124 if (b->encodeLong(type) != 0) return -1;
125 if (b->encodeLong(subtype) != 0) return -1;
130 * should be compatible to older serialize functions
132 int MediaInfo::deserializeImpl(SerializeBuffer *b) {
133 if (b->decodeLongLong(size) != 0) return -1;
135 if (b->decodeByte(cp) != 0) return -1;
137 if (b->decodeLong(type) != 0) return -1;
138 if (b->decodeLong(subtype) != 0) return -1;
149 mediaType=MEDIA_TYPE_UNKNOWN;
154 Media::Media(const Media *m) {
158 mediaType=m->mediaType;
162 if (m->uri) uri=new MediaURI(m->uri);
163 setFileName(m->fileName);
164 setDisplayName(m->displayName);
170 if (displayName) { delete[] displayName; displayName = NULL; }
171 if (fileName) { delete[] fileName; fileName = NULL; }
173 index = -1; // just in case
176 ULONG Media::getTime() const
181 const char* Media::getDisplayName() const
183 if (displayName) return displayName;
187 const char* Media::getFileName() const
192 void Media::setTime(ULONG tstartTime)
197 void Media::setMediaType(ULONG mtype)
202 ULONG Media::getMediaType() const
207 void Media::setDisplayName(const char* tDisplayName)
209 if (displayName) delete[] displayName;
211 if (! tDisplayName) return;
212 displayName = new char[strlen(tDisplayName) + 1];
213 if (displayName) strcpy(displayName, tDisplayName);
216 void Media::setFileName(const char* tFileName)
218 if (fileName) delete[] fileName;
220 if (! tFileName) return;
221 fileName = new char[strlen(tFileName) + 1];
222 if (fileName) strcpy(fileName, tFileName);
225 bool Media::hasDisplayName() const {
226 return (displayName != NULL);
229 char * Media::getTimeString(char * buffer) const {
230 if (! buffer) buffer=new char[TIMEBUFLEN];
232 time_t tTime = (time_t)getTime();
233 struct tm *btime = localtime(&tTime);
234 memcpy(<ime,btime, sizeof(struct tm));
236 if (btime && tTime != 0) {
238 strftime(buffer,TIMEBUFLEN, "%0g/%0m/%0d %0H:%0M ", btime);
240 strftime(buffer, TIMEBUFLEN, "%g/%m/%d %H:%M ", btime);
244 SNPRINTF(buffer,TIMEBUFLEN,"00/00/00 00:00 ");
249 const MediaURI * Media::getURI() const {
253 void Media::setURI(const MediaURI *u) {
258 int Media::getSerializedLenImpl() {
259 int rt=4+4+1; //type,time,hasURI
260 rt+=getSerializedStringLen(fileName);
261 rt+=getSerializedStringLen(displayName);
262 if (uri) rt+=uri->getSerializedLen();
268 * 4 namelen (incl. 0)
270 * 4 displaylen (incl. 0)
275 int Media::serializeImpl(SerializeBuffer *b) {
276 if (b->encodeLong(mediaType) != 0) return -1;
277 if (b->encodeLong(mtime) != 0) return -1;
278 if (b->encodeString(fileName) != 0) return -1;
279 if (b->encodeString(displayName) != 0) return -1;
281 if (b->encodeByte(uri?1:0) != 0) return -1;
283 if (uri->serialize(b) != 0) return -1;
288 int Media::deserializeImpl(SerializeBuffer *b) {
289 if (fileName) delete fileName;
291 if (displayName) delete displayName;
295 if (b->decodeLong(mediaType) != 0) return -1;
296 if (b->decodeLong(mtime) != 0) return -1;
298 if (b->decodeString(nlen,fileName) != 0) return -1;
299 if (b->decodeString(nlen,displayName) != 0) return -1;
301 if (b->decodeByte(hasURI) != 0) return -1;
304 if (uri->deserialize(b) != 0) return -1;
311 MediaURI * MediaList::getURI(Media * m) {
312 if (! m) return NULL;
313 const MediaURI *rtc=m->getURI();
314 if (rtc) return new MediaURI(rtc);
315 if (!_root) return NULL;
316 if (! m->getFileName()) return NULL;
317 int len=strlen(m->getFileName());
318 if (_root->getName()) len+=strlen(_root->getName())+1;
319 MediaURI *rt=new MediaURI();
320 rt->_name=new char[len+1];
321 const char *fn=m->getFileName();
322 if (_root->getName()) {
323 while (*fn=='/') fn++;
324 sprintf(rt->_name,"%s/%s",_root->getName(),fn);
327 sprintf(rt->_name,"%s",fn);
329 if (m->hasDisplayName() || _root->hasDisplayName()) {
330 len=strlen(m->getDisplayName())+1;
331 if (_root->hasDisplayName()) {
332 len+=strlen(_root->getDisplayName())+2;
334 rt->_display=new char[len];
335 if (_root->hasDisplayName()) {
336 const char *sp=m->getDisplayName();
338 sprintf(rt->_display,"%s/%s",_root->getDisplayName(),sp);
341 sprintf(rt->_display,"%s",m->getDisplayName());
344 rt->_providerId=_root->_providerId;
345 rt->_allowedTypes=_root->_allowedTypes;
349 MediaURI * MediaList::getParent(MediaURI *c) {
350 MediaURI * rt=new MediaURI();
351 rt->_providerId=c->_providerId;
352 rt->_allowedTypes=c->_allowedTypes;
355 char * ls=strrchr(c->_name,'/');
358 rt->_name=new char[nlen+1];
359 strncpy(rt->_name,c->_name,nlen);
364 char * ls=strrchr(c->_display,'/');
367 rt->_display=new char[nlen+1];
368 strncpy(rt->_display,c->_display,nlen);
369 rt->_display[nlen]=0;
376 MediaList::MediaList(const MediaURI *root) {
377 _root=new MediaURI(root);
382 MediaList::~MediaList() {
385 void MediaList::emptyList(){
387 for (UINT i = 0; i < size(); i++)
393 if (_root) delete _root;
399 MediaURI * MediaList::getRootURI() {
400 if ( ! _root) return NULL;
401 return new MediaURI(_root);
404 ULONG MediaList::getProvider() {
405 if (! _root) return 0;
406 return _root->getProvider();
409 void MediaList::setOwning(bool owning) {
415 int MediaList::getSerializedLenImpl() {
416 int rt=4+1; //numelem+hasRoot
417 if (_root) rt+=_root->getSerializedLen();
418 for (MediaList::iterator it=begin();it<end();it++) {
419 rt+=(*it)->getSerializedLen();
431 int MediaList::serializeImpl(SerializeBuffer *b) {
432 if (b->encodeLong(size()) != 0) return -1;
433 if (b->encodeByte(_root?1:0) != 0) return -1;
435 if (_root->serialize(b) != 0) return -1;
437 for (MediaList::iterator it=begin();it<end();it++) {
438 if ((*it)->serialize(b) !=0) return -1;
443 int MediaList::deserializeImpl(SerializeBuffer *b) {
446 if (b->decodeLong(numelem) != 0) return -1;
448 if (b->decodeByte(hasRoot) != 0) return -1;
450 _root=new MediaURI();
451 if (_root->deserialize(b) != 0) return -1;
453 for (ULONG i=0;i<numelem;i++) {
454 Media *m=new Media();
455 if (m->deserialize(b) != 0) {