]> git.vomp.tv Git - vompserver.git/blob - media.c
Media Player From Andreas Vogel
[vompserver.git] / media.c
1 /*
2     Copyright 2004-2005 Chris Tallon, Andreas Vogel
3
4     This file is part of VOMP.
5
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.
10
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.
15
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
19 */
20
21 #include "media.h"
22 //#include "tools.h"
23 #include <sys/stat.h>
24 #include <sys/types.h>
25
26
27
28 Media::Media(int type, const char * filename, int time){
29   _filename=NULL;
30   if (filename) {
31     int len=strlen(filename)+1;
32     _filename=new char[len];
33     strcpy(_filename,filename);
34     }
35   _type=type;
36   _time=time;
37   };
38
39 Media::~Media(){
40   delete _filename;
41   _filename=NULL;
42   };
43
44 const char * Media::getFilename() {
45   return _filename;
46   }
47
48 int Media::getType() {
49   return _type;
50   }
51 int Media::getTime() {
52   return _time;
53   }
54
55
56 static struct mtype{
57    const char* extension;
58    int type;
59    } mediatypes[]= {
60      {".mp3",MEDIA_TYPE_AUDIO},
61      {".MP3",MEDIA_TYPE_AUDIO},
62      {".jpg",MEDIA_TYPE_PICTURE},
63      {".JPG",MEDIA_TYPE_PICTURE},
64      {".jpeg",MEDIA_TYPE_PICTURE},
65      {".JPEG",MEDIA_TYPE_PICTURE}
66      };
67 //#define NUMTYPES (sizeof(mediatypes)/sizeof(mtype))
68 #define NUMTYPES 6
69
70 MediaList * MediaList::readList(Config * cfg,const char * dirname, int type) {
71   MediaList *rt=NULL;
72   if (dirname == NULL) {
73     //the configured Media List
74     //for the moment up to 10 entries [Media] Dir.1 ...Dir.10
75     for (int nr=1;nr<=10;nr++){
76       char buffer[20];
77       sprintf(buffer,"Dir.%d",nr);
78       const char * dn=cfg->getValueString("Media",buffer);
79       if (dn != NULL) {
80         if (rt == NULL) rt=new MediaList();
81         Media *m=new Media(MEDIA_TYPE_DIR,dn,0);
82         rt->Add(m);
83         Log::getInstance()->log("Media",Log::DEBUG,"added base dir %s",dn);
84        }
85      }
86    }
87   if (rt != NULL) return rt;
88   //if nothing is configured, we start at /
89   if (dirname == NULL) dirname="/";
90   rt=new MediaList();
91   //open the directory and read out the entries
92   cReadDir d(dirname);
93   struct dirent *e;
94   while ((e=d.Next()) != NULL) {
95     {
96     const char * fname=e->d_name;
97     if ( fname == NULL) continue;
98     if (strcmp(fname,".") == 0) continue;
99     char *buffer;
100     asprintf(&buffer, "%s/%s", dirname, e->d_name);
101     struct stat st;
102     int mtype=MEDIA_TYPE_UNKNOWN;
103     if (stat(buffer, &st) == 0) {
104       if (S_ISDIR(st.st_mode)) {
105          mtype=MEDIA_TYPE_DIR;
106    }
107       else {
108          for (int i=0;i<NUMTYPES;i++) {
109             if (endswith(fname,mediatypes[i].extension)) {
110               mtype=mediatypes[i].type;
111               break;
112               }
113            }
114          }
115       }
116     free(buffer);
117     //only consider entries we accept by type here
118     if (mtype & type) {
119      Media * m =new Media(mtype,fname,(int)(st.st_mtime));
120      rt->Add(m);
121      Log::getInstance()->log("Media",Log::DEBUG,"added entry %s, type=%d",fname,mtype);
122      }
123     }
124   }
125   //test
126   //Media *m=new Media(MEDIA_TYPE_DIR,"testMedia1",0);
127   //rt->Add(m);
128   return rt;
129   }
130
131
132