[TVChannel] GetCurrentProgram body
authorPrzemyslaw Ciezkowski <p.ciezkowski@samsung.com>
Tue, 16 Dec 2014 15:23:44 +0000 (16:23 +0100)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Mon, 2 Feb 2015 11:07:30 +0000 (20:07 +0900)
Change-Id: Ieab662a16b5ddcfd6e3750a48673dd6be00a7417
Signed-off-by: Przemyslaw Ciezkowski <p.ciezkowski@samsung.com>
src/tvchannel/program_info.cc [new file with mode: 0644]
src/tvchannel/program_info.h [new file with mode: 0644]
src/tvchannel/tvchannel.gyp
src/tvchannel/tvchannel_instance.cc
src/tvchannel/tvchannel_instance.h
src/tvchannel/tvchannel_manager.cc
src/tvchannel/tvchannel_manager.h

diff --git a/src/tvchannel/program_info.cc b/src/tvchannel/program_info.cc
new file mode 100644 (file)
index 0000000..21b2f76
--- /dev/null
@@ -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<char*>(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<double> (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<char*>(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 (file)
index 0000000..372221a
--- /dev/null
@@ -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 <ProgramData.h>
+#include <string>
+
+#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_
index 1c72f1da0b99f4a16e665e9d8b123f01ec1f13cc..f0c2f7589a53c6ab8e8c27b91160079801cddfbd 100644 (file)
@@ -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',
index 9ceb589ad5093e18f9c850e53077b6c104de3656..e916eaab37e24c6c6d7e00cb1cc3ea8ac41c5b31 100644 (file)
@@ -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<ProgramInfo> pInfo(TVChannelManager::getInstance()
+        ->getCurrentProgram(args.get("windowType").get<std::string>()));
+    picojson::value::object program;
+    program.insert(
+        std::make_pair("title",
+            picojson::value(pInfo->getTitle())));
+    program.insert(
+        std::make_pair("startTime",
+            picojson::value(static_cast<double>(pInfo->getStartTimeMs()))));
+    program.insert(
+        std::make_pair("duration",
+            picojson::value(static_cast<double>(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
index 5509c069afd9d26237549531fd0e057dd4567cf1..8dcd8e38842b2cd546d44ce96b4c0419462ad400 100644 (file)
@@ -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
index 274c0c31a3127e9b52e171df7c6c9ddb8e96fad9..a1322d97979c84d2ce9954c07f477180182e519e 100644 (file)
@@ -5,12 +5,15 @@
 #include "tvchannel/tvchannel_manager.h"
 #include <iconv.h>
 #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<TCServiceId>(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;
index bc8bf3e6c55cc5b7e0923716e794f1252d0fad88..a35eb4d6522bd535a93047050b10db53a7394c21 100644 (file)
@@ -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<ChannelInfo> 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