[svn r260] Fixes references over libraries, such as the gmyth-0.1.
4 * @file gmyth/gmyth_epg.c
6 * @brief <p> GMythEPG class provides access to the program and channel data
7 * from the Electronic Program Guide (EPG) of the Mythtv backend.
9 * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
10 * @author Leonardo Sobral Cunha <leonardo.cunha@indt.org.br>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include <mysql/mysql.h>
38 #include "gmyth_epg.h"
39 #include "gmyth_util.h"
40 #include "gmyth_debug.h"
42 static void gmyth_epg_class_init (GMythEPGClass *klass);
43 static void gmyth_epg_init (GMythEPG *object);
45 static void gmyth_epg_dispose (GObject *object);
46 static void gmyth_epg_finalize (GObject *object);
48 G_DEFINE_TYPE(GMythEPG, gmyth_epg, G_TYPE_OBJECT)
51 gmyth_epg_class_init (GMythEPGClass *klass)
53 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
55 gobject_class->dispose = gmyth_epg_dispose;
56 gobject_class->finalize = gmyth_epg_finalize;
60 gmyth_epg_init (GMythEPG *gmyth_epg)
66 gmyth_epg_dispose (GObject *object)
68 //GMythEPG *gmyth_epg = GMYTH_EPG(object);
70 G_OBJECT_CLASS (gmyth_epg_parent_class)->dispose (object);
74 gmyth_epg_finalize (GObject *object)
76 g_signal_handlers_destroy (object);
78 G_OBJECT_CLASS (gmyth_epg_parent_class)->finalize (object);
82 * Creates a new instance of GMythEPG.
84 * @return a new instance of GMythEPG.
89 GMythEPG *epg = GMYTH_EPG (g_object_new(GMYTH_EPG_TYPE, NULL));
94 /** Connects to the Mysql database in the backend. The backend address
95 * is loaded from the GMythSettings instance.
97 * @param gmyth_epg the GMythEPG instance to be connected.
98 * @return true if connection was success, false if failed.
101 gmyth_epg_connect (GMythEPG *gmyth_epg, GMythBackendInfo *backend_info)
105 if (gmyth_epg->sqlquery == NULL) {
106 gmyth_debug ("[%s] Creating gmyth_query", __FUNCTION__);
107 gmyth_epg->sqlquery = gmyth_query_new ( );
110 if (!gmyth_query_connect(gmyth_epg->sqlquery, backend_info)) {
111 g_warning ("[%s] Error while connecting to db", __FUNCTION__);
118 /** Disconnects from the Mysql database in the backend.
120 * @param gmyth_epg the GMythEPG instance to be disconnected
121 * @return true if disconnection was success, false if failed.
124 gmyth_epg_disconnect (GMythEPG *gmyth_epg)
128 if (gmyth_epg->sqlquery != NULL) {
129 g_object_unref (gmyth_epg->sqlquery);
130 gmyth_epg->sqlquery = NULL;
136 /** Retrieves the available list of channels from the backend Mysql database.
138 * @param gmyth_epg the GMythEPG instance.
139 * @param glist_ptr the GList pointer to be filled with the loaded list address.
140 * @return The amount of channels retrieved from database, or -1 if error.
143 gmyth_epg_get_channel_list (GMythEPG *gmyth_epg, GList **glist_ptr)
149 msql_res = gmyth_query_process_statement (gmyth_epg->sqlquery,
150 "SELECT chanid,name FROM channel;");
154 if (msql_res == NULL) {
155 g_warning ("[%s] msql query returned NULL MYSQL_RES", __FUNCTION__);
159 GMythChannelInfo *channel_info;
161 while ((row = mysql_fetch_row (msql_res)) != NULL){
163 channel_info = g_new0(GMythChannelInfo, 1);
164 channel_info->channel_ID = g_ascii_strtoull (row[0], NULL, 10);
165 channel_info->channel_name = g_string_new (row[1]);
168 gmyth_channel_info_print(channel_info);
171 (*glist_ptr) = g_list_append ((*glist_ptr), channel_info);
174 mysql_free_result (msql_res);
175 return (!(*glist_ptr)) ? 0 : g_list_length (*glist_ptr);
179 * Retrieves the available list of channels from the backend Mysql database.
181 * @param gmyth_epg the GMythEPG instance.
182 * @param proglist the GList pointer to be filled with the loaded list.
183 * @param chan_num the channel num on which to search for program.
184 * @param starttime the start time to search for programs.
185 * @param endtime the end time to search for programs.
186 * @return The amount of channels retrieved from database, or -1 if error.
189 gmyth_epg_get_program_list (GMythEPG *gmyth_epg, GList **proglist,
190 const gint chan_num, GTimeVal *starttime, GTimeVal *endtime)
192 gchar *startts = gmyth_util_time_to_string_from_time_val(starttime);
193 gchar *endts = gmyth_util_time_to_string_from_time_val(endtime);
199 querystr = g_string_new(
200 "SELECT DISTINCT program.chanid, program.starttime, program.endtime, "
201 " program.title, program.subtitle, program.description, "
202 " program.category, channel.channum, channel.callsign, "
203 " channel.name, program.previouslyshown, channel.commfree, "
204 " channel.outputfilters, program.seriesid, program.programid, "
205 " program.airdate, program.stars, program.originalairdate, "
206 " program.category_type, oldrecstatus.recordid, "
207 " oldrecstatus.rectype, oldrecstatus.recstatus, "
208 " oldrecstatus.findid "
210 "LEFT JOIN channel ON program.chanid = channel.chanid "
211 "LEFT JOIN oldrecorded AS oldrecstatus ON "
212 " program.title = oldrecstatus.title AND "
213 " channel.callsign = oldrecstatus.station AND "
214 " program.starttime = oldrecstatus.starttime "
217 g_string_append_printf (querystr,
218 "WHERE program.chanid = %d "
219 " AND program.endtime >= '%s' "
220 " AND program.starttime <= '%s' "
221 " AND program.manualid = 0 ",
222 chan_num, startts, endts);
224 if (!g_strrstr(querystr->str, " GROUP BY "))
225 querystr = g_string_append(querystr,
226 " GROUP BY program.starttime, channel.channum, "
227 " channel.callsign, program.title ");
229 if (!g_strrstr(querystr->str, " LIMIT "))
230 querystr = g_string_append(querystr, " LIMIT 1000 ");
233 gmyth_query_process_statement(gmyth_epg->sqlquery, querystr->str);
235 if (res_set == NULL) {
236 g_warning ("[%s] msql query returned NULL MYSQL_RES", __FUNCTION__);
241 while ((row = mysql_fetch_row (res_set)) != NULL) {
243 GMythProgramInfo *p = g_new0 (GMythProgramInfo, 1);
244 p->chanid = g_string_new (row[0]);
246 p->startts = gmyth_util_string_to_time_val (row[1]);
247 p->endts = gmyth_util_string_to_time_val (row[2]);
249 p->recstartts = p->startts;
250 p->recendts = p->endts;
251 p->lastmodified = p->startts;
253 p->title = g_string_new (row[3]);
254 p->subtitle = g_string_new (row[4]);
255 p->description = g_string_new (row[5]);
256 p->category = g_string_new (row[6]);
257 p->chanstr = g_string_new (row[7]);
258 p->chansign = g_string_new (row[8]);
259 p->channame = g_string_new (row[9]);
260 p->repeat = g_ascii_strtoull(row[10], NULL, 10);
261 p->chancommfree = g_ascii_strtoull(row[11], NULL, 10);
262 p->chanOutputFilters = g_string_new (row[12]);
263 p->seriesid = g_string_new (row[13]);
264 p->programid = g_string_new (row[14]);
265 p->year = g_string_new (row[15]);
266 p->stars = g_ascii_strtod(row[16], NULL);
268 if (!row[17] || !strcmp(row[17], "")) {
269 p->originalAirDate = 0;
270 p->hasAirDate = FALSE;
272 p->originalAirDate = gmyth_util_string_to_time_val (row[17]);
273 p->hasAirDate = TRUE;
276 p->catType = g_string_new (row[18]);
278 *proglist = g_list_append((*proglist), p);
281 gmyth_program_info_print(p);
286 mysql_free_result (res_set);
287 g_string_free(querystr, TRUE);