From: Przemyslaw Ciezkowski Date: Tue, 16 Dec 2014 15:23:44 +0000 (+0100) Subject: [TVChannel] GetCurrentProgram body X-Git-Tag: submit/tizen_tv/20150603.064601~1^2~515 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8fdc08f381da36739fd27d96f49fd03220e169dd;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [TVChannel] GetCurrentProgram body Change-Id: Ieab662a16b5ddcfd6e3750a48673dd6be00a7417 Signed-off-by: Przemyslaw Ciezkowski --- diff --git a/src/tvchannel/program_info.cc b/src/tvchannel/program_info.cc new file mode 100644 index 00000000..21b2f76a --- /dev/null +++ b/src/tvchannel/program_info.cc @@ -0,0 +1,163 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include "tvchannel/program_info.h" +#include "tvchannel/tvchannel_manager.h" +#include "common/logger.h" + +namespace extension { +namespace tvchannel { + +ProgramInfo::ProgramInfo() : +m_title(""), +m_startTime(0), +m_duration(0), +m_detailedDescription(""), +m_language(""), +m_rating("") { +} + +ProgramInfo::~ProgramInfo() { +} + +void ProgramInfo::fromApiData(const TCProgramData &data) { + LOGD("Enter"); + setTitle(data); + m_startTime = data.StartTime(); + m_duration = data.EndTime() - m_startTime; + setDetailedDescription(data); + setLanguage(data); +} + +std::string ProgramInfo::getTitle() const { + return m_title; +} + +void ProgramInfo::setTitle(const TCProgramData &data) { + LOGD("Enter"); + try { + unsigned int len; + size_t c_len; + t_wchar_t *title; + + len = data.TitleLength(); + if (len) { + LOGD("Title length %d", len); + title = new t_wchar_t[len + 1]; + TCProgramData copy = data; + copy.Title(title, &len); + c_len = len * sizeof (t_wchar_t) / sizeof (char); + char name[128]; + TVChannelManager::ucs2utf8(name, (size_t)sizeof (name), + reinterpret_cast(title), c_len); + delete [] title; + m_title = name; + } else { + m_title = ""; + } + } catch (...) { + LOGD("There is some problem on text encoding."); + m_title = ""; + } +} + +void ProgramInfo::setTitle(std::string title) { + m_title = title; +} + +int64_t ProgramInfo::getStartTime() const { + return m_startTime; +} + +double ProgramInfo::getStartTimeMs() const { + return static_cast (m_startTime * 1000.0); +} + +void ProgramInfo::setStartTime(int64_t startTime) { + m_startTime = startTime; +} + +int64_t ProgramInfo::getDuration() const { + return m_duration; +} + +void ProgramInfo::setDuration(int64_t duration) { + m_duration = duration; +} + +std::string ProgramInfo::getDetailedDescription() const { + return m_detailedDescription; +} + +void ProgramInfo::setDetailedDescription(std::string detailedDescription) { + m_detailedDescription = detailedDescription; +} + +void ProgramInfo::setDetailedDescription(const TCProgramData &data) { + LOGD("Enter"); + try { + unsigned int len; + size_t c_len; + t_wchar_t *description; + + len = data.ExtendedTextLength(); + LOGD("Description length %d", len); + if (len) { + description = new t_wchar_t[len + 1]; + TCProgramData copy = data; + copy.ExtendedText(description, &len); + + c_len = len * sizeof (t_wchar_t) / sizeof (char); + char name[TCProgramData::EExtendChannelTextLength + ::EXTEND_CHANNEL_TEXT_LENGTH]; + TVChannelManager::ucs2utf8(name, (size_t)sizeof (name), + reinterpret_cast(description), c_len); + delete [] description; + m_detailedDescription = name; + } else { + m_detailedDescription = ""; + } + } catch (...) { + LOGD("There is some problem on text encoding."); + m_detailedDescription = ""; + } +} + +std::string ProgramInfo::getLanguage() const { + return m_language; +} + +void ProgramInfo::setLanguage(std::string language) { + m_language = language; +} + +void ProgramInfo::setLanguage(const TCProgramData &data) { + // todo: convert language number to code? + m_language = std::to_string(data.Language()); +} + +std::string ProgramInfo::getRating() const { + return m_rating; +} + +void ProgramInfo::setRating(std::string rating) { + m_rating = rating; +} + + +} // namespace tvchannel +} // namespace extension diff --git a/src/tvchannel/program_info.h b/src/tvchannel/program_info.h new file mode 100644 index 00000000..372221a3 --- /dev/null +++ b/src/tvchannel/program_info.h @@ -0,0 +1,69 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef SRC_TVCHANNEL_PROGRAM_INFO_H_ +#define SRC_TVCHANNEL_PROGRAM_INFO_H_ + +#include +#include + +#include "tvchannel/types.h" + +namespace extension { +namespace tvchannel { + +class ProgramInfo { + public: + ProgramInfo(); + virtual ~ProgramInfo(); + void fromApiData(const TCProgramData &data); + + std::string getTitle() const; + void setTitle(std::string title); + void setTitle(const TCProgramData &data); + + int64_t getStartTime() const; + double getStartTimeMs() const; + void setStartTime(int64_t startTime); + + int64_t getDuration() const; + void setDuration(int64_t duration); + + std::string getDetailedDescription() const; + void setDetailedDescription(std::string detailedDescription); + void setDetailedDescription(const TCProgramData &data); + + std::string getLanguage() const; + void setLanguage(std::string language); + void setLanguage(const TCProgramData &data); + + std::string getRating() const; + void setRating(std::string rating); + + private: + std::string m_title; + int64_t m_startTime; + int64_t m_duration; + std::string m_detailedDescription; + std::string m_language; + std::string m_rating; +}; + +} // namespace tvchannel +} // namespace extension + +#endif // SRC_TVCHANNEL_PROGRAM_INFO_H_ diff --git a/src/tvchannel/tvchannel.gyp b/src/tvchannel/tvchannel.gyp index 1c72f1da..f0c2f758 100644 --- a/src/tvchannel/tvchannel.gyp +++ b/src/tvchannel/tvchannel.gyp @@ -36,6 +36,8 @@ 'tvchannel_instance.h', 'channel_info.cc', 'channel_info.h', + 'program_info.cc', + 'program_info.h', 'tvchannel_manager.h', 'tvchannel_manager.cc', 'types.h', diff --git a/src/tvchannel/tvchannel_instance.cc b/src/tvchannel/tvchannel_instance.cc index 9ceb589a..e916eaab 100644 --- a/src/tvchannel/tvchannel_instance.cc +++ b/src/tvchannel/tvchannel_instance.cc @@ -10,6 +10,7 @@ #include "common/picojson.h" #include "tvchannel/tvchannel_manager.h" #include "tvchannel/channel_info.h" +#include "tvchannel/program_info.h" namespace extension { namespace tvchannel { @@ -20,6 +21,10 @@ TVChannelInstance::TVChannelInstance() { std::bind(&TVChannelInstance::getCurrentChannel, this, std::placeholders::_1, std::placeholders::_2)); + RegisterSyncHandler("TVChannelManager_getCurrentProgram", + std::bind(&TVChannelInstance::getCurrentProgram, this, + std::placeholders::_1, + std::placeholders::_2)); } TVChannelInstance::~TVChannelInstance() { @@ -71,5 +76,34 @@ void TVChannelInstance::getCurrentChannel(picojson::value const& args, ReportSuccess(v, out); } +void TVChannelInstance::getCurrentProgram(const picojson::value& args, + picojson::object& out) { + std::unique_ptr pInfo(TVChannelManager::getInstance() + ->getCurrentProgram(args.get("windowType").get())); + picojson::value::object program; + program.insert( + std::make_pair("title", + picojson::value(pInfo->getTitle()))); + program.insert( + std::make_pair("startTime", + picojson::value(static_cast(pInfo->getStartTimeMs())))); + program.insert( + std::make_pair("duration", + picojson::value(static_cast(pInfo->getDuration())))); + program.insert( + std::make_pair("detailedDescription", + picojson::value(pInfo->getDetailedDescription()))); + program.insert( + std::make_pair("language", + picojson::value(pInfo->getLanguage()))); + program.insert( + std::make_pair("rating", + picojson::value(pInfo->getRating()))); + + picojson::value result(program); + ReportSuccess(result, out); +} + + } // namespace tvchannel } // namespace extension diff --git a/src/tvchannel/tvchannel_instance.h b/src/tvchannel/tvchannel_instance.h index 5509c069..8dcd8e38 100644 --- a/src/tvchannel/tvchannel_instance.h +++ b/src/tvchannel/tvchannel_instance.h @@ -22,6 +22,7 @@ class TVChannelInstance: public common::ParsedInstance { private: void getCurrentChannel(const picojson::value& args, picojson::object& out); + void getCurrentProgram(const picojson::value& args, picojson::object& out); }; } // namespace tvchannel diff --git a/src/tvchannel/tvchannel_manager.cc b/src/tvchannel/tvchannel_manager.cc index 274c0c31..a1322d97 100644 --- a/src/tvchannel/tvchannel_manager.cc +++ b/src/tvchannel/tvchannel_manager.cc @@ -5,12 +5,15 @@ #include "tvchannel/tvchannel_manager.h" #include #include "tvchannel/channel_info.h" +#include "tvchannel/program_info.h" #include "common/logger.h" #include "common/platform_exception.h" namespace extension { namespace tvchannel { +using common::UnknownException; + TVChannelManager* TVChannelManager::getInstance() { static TVChannelManager manager; return &manager; @@ -77,6 +80,57 @@ EProfile TVChannelManager::getProfile(WindowType windowType) { } } +TCServiceId TVChannelManager::getCurrentChannelId( + const std::string& _windowType) { + TCServiceData serviceData; + TCCriteriaHelper criteria; + criteria.Fetch(SERVICE_ID); + // Navigation + IServiceNavigation* navigation; + int ret = TVServiceAPI::CreateServiceNavigation( + getProfile(stringToWindowType(_windowType)), 0, &navigation); + if (TV_SERVICE_API_SUCCESS != ret) { + LOGE("Failed to create service navigation: %d", ret); + throw UnknownException("Failed to create service navigation"); + } + + struct TSTvMode tvMode; + ret = navigation->GetTvMode(tvMode); + if (TV_SERVICE_API_METHOD_SUCCESS != ret) { + LOGE("Failed to get current tv mode: %d", ret); + throw UnknownException("Failed to get current tv mode"); + } + + ret = navigation->GetCurrentServiceInfo(tvMode, criteria, serviceData); + if (TV_SERVICE_API_METHOD_SUCCESS != ret) { + LOGE("Failed to get current service info: %d", ret); + throw UnknownException("Failed to get current service info"); + } + return serviceData.Get(SERVICE_ID); +} + +ProgramInfo* TVChannelManager::getCurrentProgram( + const std::string& _windowType) { + + IServiceGuide* guide; + int ret = TVServiceAPI::CreateServiceGuide(&guide); + if (TV_SERVICE_API_SUCCESS != ret) { + LOGE("Failed to create service guide: %d", ret); + throw UnknownException("Failed to create service guide"); + } + + TCProgramData programData; + ret = guide->GetPresentProgram(getCurrentChannelId(_windowType), + programData); + if (TV_SERVICE_API_METHOD_SUCCESS != ret) { + LOGE("Failed to get current program: %d", ret); + throw UnknownException("Failed to get current program"); + } + ProgramInfo* program = new ProgramInfo(); + program->fromApiData(programData); + return program; +} + void TVChannelManager::ucs2utf8(char *out, size_t out_len, char *in, size_t in_len) { iconv_t cd; diff --git a/src/tvchannel/tvchannel_manager.h b/src/tvchannel/tvchannel_manager.h index bc8bf3e6..a35eb4d6 100644 --- a/src/tvchannel/tvchannel_manager.h +++ b/src/tvchannel/tvchannel_manager.h @@ -14,6 +14,7 @@ namespace extension { namespace tvchannel { class ChannelInfo; +class ProgramInfo; // TVServiceAPI static methods returns 0 on success static const int TV_SERVICE_API_SUCCESS = 0; // TVServiceAPI object methods return 1 on success @@ -27,6 +28,7 @@ class TVChannelManager { public: static TVChannelManager* getInstance(); std::unique_ptr getCurrentChannel(std::string const& _windowType); + ProgramInfo* getCurrentProgram(std::string const& _windowType); static void ucs2utf8(char *out, size_t out_len, char *in, size_t in_len); private: // Not copyable, assignable, movable @@ -37,6 +39,7 @@ class TVChannelManager { TVChannelManager(TVChannelManager &&) = delete; EProfile getProfile(WindowType windowType); + TCServiceId getCurrentChannelId(std::string const& _windowType); }; } // namespace tvchannel