1 /*
2  *  data_streamer.cpp -- Metadata handler thread
3  *
4  *  Copyright (C) 2004 Tony Sin(x) '76 <administrator@tortugalabs.it>
5  *  All rights reserved.
6  *
7  */
8 
9 /*
10  *			 GNU GENERAL PUBLIC LICENSE
11  *			    Version 2, June 1991
12  *
13  *  Copyright (C) 1989, 1991 Free Software Foundation, Inc.
14  *                           675 Mass Ave, Cambridge, MA 02139, USA
15  *  Everyone is permitted to copy and distribute verbatim copies
16  *  of this license document, but changing it is not allowed.
17  *
18  *  This program is free software; you can redistribute it and/or modify
19  *  it under the terms of the GNU General Public License as published by
20  *  the Free Software Foundation; either version 2 of the License, or
21  *  (at your option) any later version.
22 
23  *  This program is distributed in the hope that it will be useful,
24  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
25  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  *  GNU General Public License for more details.
27 
28  *  You should have received a copy of the GNU General Public License
29  *  along with this program; if not, write to the Free Software
30  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
31  *
32  */
33 
34 #ifdef HAVE_CONFIG_H
35   #ifndef __main_config_h__
36     #define __main_config_h__
37     #include "../config.h"
38   #endif
39 
40   #ifdef HAVE_STRING_H
41     #include <string.h>
42   #endif
43 
44   #ifdef HAVE_UNISTD_H
45     #include <unistd.h>
46   #endif
47 #endif
48 
49 #include "globals.h"
50 #include "data_streamer.h"
51 #include "meta_parser.h"
52 
data_streamer(void * arg)53 void *data_streamer(void *arg)
54 {
55   sig_obj->Block();
56 
57   if ((conf_obj->GetValue(_metaupdate) == NULL) || (atoi(conf_obj->GetValue(_metaupdate)) > 0))
58   {
59     cMetaDataList *global = NULL, *directory = NULL, *file = NULL, *p = NULL;
60     bool loc_changed = false;
61     #ifdef HAVE_ID3
62       struct ID3TableType *loc_ID3t;
63     #endif
64     int i;
65     char *curr_path = NULL, *new_path = NULL, *temp_buf = NULL, *file_buf = NULL;
66 
67     curr_path = new char[INTERNAL_BUF_SIZE];
68     new_path = new char[INTERNAL_BUF_SIZE];
69     temp_buf = new char[INTERNAL_BUF_SIZE];
70     file_buf = new char[INTERNAL_BUF_SIZE];
71 
72     bzero(temp_buf,INTERNAL_BUF_SIZE);
73     bzero(curr_path,INTERNAL_BUF_SIZE);
74     bzero(new_path,INTERNAL_BUF_SIZE);
75     bzero(file_buf,INTERNAL_BUF_SIZE);
76 
77     #ifdef HAVE_ID3
78       loc_ID3t = new ID3TableType[MAX_ID3_TAGS];
79       for (i = 0; i < MAX_ID3_TAGS; i++)
80       {
81         loc_ID3t[i].ID3Type = ID3Table[i].ID3Type;
82         loc_ID3t[i].str = new char[INTERNAL_BUF_SIZE];
83         bzero(loc_ID3t[i].str,INTERNAL_BUF_SIZE);
84       }
85     #endif
86 
87     if (conf_obj->GetValue(_mdfpath) != NULL)
88     {
89       global = new cMetaDataList;
90       global->LoadMeta(conf_obj->GetValue(_mdfpath));
91       global->StartMeta();
92     }
93 
94     directory = new cMetaDataList;
95     file = new cMetaDataList;
96 
97     sem_obj->Wait(start_meta);
98 
99     while (!ok_to_end)
100     {
101       sem_obj->Wait(data_mutex);
102 
103       if (loc_changed = changed)
104       {
105         ready_obj->GetPath(new_path);
106         strcpy(temp_buf,ready_obj->GetData());
107         ready_obj->GetFile(file_buf);
108         changed = false;
109 
110         #ifdef HAVE_ID3
111           ready_obj->GetID3Table(loc_ID3t);
112         #endif
113       }
114       sem_obj->Signal(data_mutex);
115 
116       if (loc_changed)
117       {
118         int len;
119 
120         file->PurgeMeta();
121         file_buf[strlen(file_buf)-4] = '\0';
122 
123         len = strlen(temp_buf);
124         temp_buf[len-1] = 'f';
125         temp_buf[len-2] = 'd';
126         temp_buf[len-3] = 'm';
127         file->LoadMeta(temp_buf);
128         file->StartMeta();
129 
130         if (file->IsEmpty() && strcmp(curr_path,new_path))
131         {
132           directory->PurgeMeta();
133           strcat(new_path,"/default.mdf");
134           directory->LoadMeta(new_path);
135           directory->StartMeta();
136         }
137         strcpy(curr_path,new_path);
138 
139         if (!file->IsEmpty())
140           p = file;
141         else if (!directory->IsEmpty())
142           p = directory;
143         else if ((global != NULL) && !global->IsEmpty())
144           p = global;
145         else
146           p = NULL;
147       }
148 
149       if (p != NULL)
150         #ifdef HAVE_ID3
151           p->GetNextMeta(temp_buf,loc_ID3t);
152         #else
153           p->GetNextMeta(temp_buf);
154         #endif
155       else
156         strcpy(temp_buf,file_buf);
157 
158       if (strlen(temp_buf) > 0)
159         stream_obj->UpdateMetaData(temp_buf);
160       sleep(((conf_obj->GetValue(_metaupdate) != NULL)? atoi(conf_obj->GetValue(_metaupdate)) : 5));
161     }
162 
163 
164     if (file != NULL)
165       delete file;
166     if (directory != NULL)
167       delete directory;
168     if (global != NULL)
169       delete global;
170 
171     #ifdef HAVE_ID3
172       for (i = 0; i < MAX_ID3_TAGS; i++)
173         delete [] loc_ID3t[i].str;
174 
175       delete [] loc_ID3t;
176     #endif
177 
178     delete [] curr_path;
179     delete [] new_path;
180     delete [] temp_buf;
181     delete [] file_buf;
182   }
183 
184   return NULL;
185 }
186