00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00031
00032 #ifdef HAVE_CONFIG_H
00033 #include "autoconfig.h"
00034 #endif
00035
00036 #ifdef ATRAILERS
00037
00038 #include "zmm/zmm.h"
00039 #include "atrailers_service.h"
00040 #include "atrailers_content_handler.h"
00041 #include "content_manager.h"
00042 #include "string_converter.h"
00043 #include "config_manager.h"
00044 #include "config_options.h"
00045 #include "server.h"
00046
00047 using namespace zmm;
00048 using namespace mxml;
00049
00050 #define ATRAILERS_SERVICE_URL_640 "http://www.apple.com/trailers/home/xml/current.xml"
00051 #define ATRAILERS_SERVICE_URL_720P "http://www.apple.com/trailers/home/xml/current_720p.xml"
00052
00053 ATrailersService::ATrailersService()
00054 {
00055 url = Ref<URL>(new URL());
00056 pid = 0;
00057 curl_handle = curl_easy_init();
00058 if (!curl_handle)
00059 throw _Exception(_("failed to initialize curl!\n"));
00060
00061 if (ConfigManager::getInstance()->getOption(CFG_ONLINE_CONTENT_ATRAILERS_RESOLUTION) == "640")
00062 service_url = _(ATRAILERS_SERVICE_URL_640);
00063 else
00064 service_url = _(ATRAILERS_SERVICE_URL_720P);
00065 }
00066
00067 ATrailersService::~ATrailersService()
00068 {
00069 if (curl_handle)
00070 curl_easy_cleanup(curl_handle);
00071 }
00072
00073 service_type_t ATrailersService::getServiceType()
00074 {
00075 return OS_ATrailers;
00076 }
00077
00078 String ATrailersService::getServiceName()
00079 {
00080 return _("Apple Trailers");
00081 }
00082
00083
00084 Ref<Object> ATrailersService::defineServiceTask(Ref<Element> xmlopt, Ref<Object> params)
00085 {
00086
00087 return nil;
00088 }
00089
00090 Ref<Element> ATrailersService::getData()
00091 {
00092 long retcode;
00093 Ref<StringConverter> sc = StringConverter::i2i();
00094
00095 Ref<StringBuffer> buffer;
00096
00097 try
00098 {
00099 log_debug("DOWNLOADING URL: %s\n", service_url.c_str());
00100 buffer = url->download(service_url, &retcode,
00101 curl_handle, false, true, true);
00102 }
00103 catch (Exception ex)
00104 {
00105 log_error("Failed to download Apple Trailers XML data: %s\n",
00106 ex.getMessage().c_str());
00107 return nil;
00108 }
00109
00110 if (buffer == nil)
00111 return nil;
00112
00113 if (retcode != 200)
00114 return nil;
00115
00116 log_debug("GOT BUFFER\n%s\n", buffer->toString().c_str());
00117 Ref<Parser> parser(new Parser());
00118 try
00119 {
00120 return parser->parseString(sc->convert(buffer->toString()))->getRoot();
00121 }
00122 catch (ParseException pe)
00123 {
00124 log_error("Error parsing Apple Trailers XML %s line %d:\n%s\n",
00125 pe.context->location.c_str(),
00126 pe.context->line,
00127 pe.getMessage().c_str());
00128 return nil;
00129 }
00130 catch (Exception ex)
00131 {
00132 log_error("Error parsing Apple Trailers XML %s\n",
00133 ex.getMessage().c_str());
00134 return nil;
00135 }
00136
00137 return nil;
00138 }
00139
00140 bool ATrailersService::refreshServiceData(Ref<Layout> layout)
00141 {
00142 log_debug("Refreshing Apple Trailers\n");
00143
00144
00145
00146
00147
00148
00149
00150 if (pid == 0)
00151 pid = pthread_self();
00152
00153 if (pid != pthread_self())
00154 throw _Exception(_("Not allowed to call refreshServiceData from different threads!"));
00155
00156 Ref<Element> reply = getData();
00157
00158 Ref<ATrailersContentHandler> sc(new ATrailersContentHandler());
00159 if (reply != nil)
00160 sc->setServiceContent(reply);
00161 else
00162 {
00163 log_debug("Failed to get XML content from Trailers service\n");
00164 throw _Exception(_("Failed to get XML content from Trailers service"));
00165 }
00166
00167 Ref<CdsObject> obj;
00168 do
00169 {
00170 obj = sc->getNextObject();
00171 if (obj == nil)
00172 break;
00173
00174 obj->setVirtual(true);
00175
00176 Ref<CdsObject> old = Storage::getInstance()->loadObjectByServiceID(RefCast(obj, CdsItem)->getServiceID());
00177 if (old == nil)
00178 {
00179 log_debug("Adding new Trailers object\n");
00180
00181 if (layout != nil)
00182 layout->processCdsObject(obj, nil);
00183 }
00184 else
00185 {
00186 log_debug("Updating existing Trailers object\n");
00187 obj->setID(old->getID());
00188 obj->setParentID(old->getParentID());
00189 struct timespec oldt, newt;
00190 oldt.tv_nsec = 0;
00191 oldt.tv_sec = old->getAuxData(_(ONLINE_SERVICE_LAST_UPDATE)).toLong();
00192 newt.tv_nsec = 0;
00193 newt.tv_sec = obj->getAuxData(_(ONLINE_SERVICE_LAST_UPDATE)).toLong();
00194 ContentManager::getInstance()->updateObject(obj);
00195 }
00196
00197 if (Server::getInstance()->getShutdownStatus())
00198 return false;
00199
00200 }
00201 while (obj != nil);
00202
00203 return false;
00204 }
00205
00206 #endif//ATRAILERS