VTB: release CVBuffer after it actually has been rendered
[xbmc.git] / xbmc / input / InputCodingTableBaiduPY.cpp
blobc79ebe23d6384ff65d1845428c8a397757a14812
1 /*
2 * Copyright (C) 2005-2013 Team Kodi
3 * http://kodi.tv
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #include "InputCodingTableBaiduPY.h"
23 #include <stdlib.h>
24 #include <utility>
26 #include "filesystem/CurlFile.h"
27 #include "utils/StringUtils.h"
28 #include "utils/RegExp.h"
29 #include "guilib/GUIMessage.h"
30 #include "guilib/GUIWindowManager.h"
32 CInputCodingTableBaiduPY::CInputCodingTableBaiduPY(const std::string& strUrl) :
33 CThread("BaiduPYApi"),
34 m_messageCounter{ 0 },
35 m_api_begin{ 0 },
36 m_api_end{ 20 },
37 m_api_nomore{ false },
38 m_initialized{ false }
40 m_url = strUrl;
41 m_codechars = "abcdefghijklmnopqrstuvwxyz";
42 m_code = "";
45 void CInputCodingTableBaiduPY::Process()
47 m_initialized = true;
48 while (!m_bStop) //Make sure we don't exit the thread
50 AbortableWait(m_Event, -1); //Wait for work to appear
51 while (!m_bStop) //Process all queued work before going back to wait on the event
53 CSingleLock lock(m_CS);
54 if (m_work.empty())
55 break;
57 auto work = m_work.front();
58 m_work.pop_front();
59 lock.Leave();
61 std::string data;
62 XFILE::CCurlFile http;
63 std::string strUrl;
64 strUrl = StringUtils::Format(m_url.c_str(), work.c_str(), m_api_begin, m_api_end);
66 if (http.Get(strUrl, data))
67 HandleResponse(work, data);
72 void CInputCodingTableBaiduPY::HandleResponse(const std::string& strCode, const std::string& response)
74 if (strCode != m_code) // don't handle obsolete response
75 return;
77 std::vector<std::wstring> words;
78 CRegExp reg;
79 reg.RegComp("\\[\"(.+?)\",[^\\]]+\\]");
80 int pos = 0;
81 int num = 0;
82 while ((pos = reg.RegFind(response.c_str(), pos)) >= 0)
84 num++;
85 std::string full = reg.GetMatch(0);
86 std::string word = reg.GetMatch(1);
87 pos += full.length();
88 words.push_back(UnicodeToWString(word));
90 if (words.size() < 20)
91 m_api_nomore = true;
92 else
94 m_api_begin += 20;
95 m_api_end += 20;
97 CSingleLock lock(m_CS);
98 m_responses.insert(std::make_pair(++m_messageCounter, words));
99 CGUIMessage msg(GUI_MSG_CODINGTABLE_LOOKUP_COMPLETED, 0, 0, m_messageCounter);
100 msg.SetStringParam(strCode);
101 lock.Leave();
102 g_windowManager.SendThreadMessage(msg, g_windowManager.GetActiveWindowID());
105 std::wstring CInputCodingTableBaiduPY::UnicodeToWString(const std::string& unicode)
107 std::wstring result = L"";
108 for (unsigned int i = 0; i < unicode.length(); i += 6)
110 int c;
111 sscanf(unicode.c_str() + i, "\\u%x", &c);
112 result += (wchar_t)c;
114 return result;
117 std::vector<std::wstring> CInputCodingTableBaiduPY::GetResponse(int response)
119 CSingleLock lock(m_CS);
120 auto words = m_responses.at(response);
121 m_responses.erase(response);
122 return words;
125 void CInputCodingTableBaiduPY::Initialize()
127 CSingleLock lock(m_CS);
128 if (!IsRunning())
129 Create();
132 void CInputCodingTableBaiduPY::Deinitialize()
134 m_Event.Set();
135 StopThread(true);
136 m_initialized = false;
139 bool CInputCodingTableBaiduPY::IsInitialized() const
141 return m_initialized;
144 bool CInputCodingTableBaiduPY::GetWordListPage(const std::string& strCode, bool isFirstPage)
146 if (strCode.empty())
147 return false;
148 if (isFirstPage || m_code != strCode)
150 m_api_begin = 0;
151 m_api_end = 20;
152 m_code = strCode;
153 m_api_nomore = false;
155 else
157 if (m_api_nomore)
158 return false;
161 CSingleLock lock(m_CS);
162 m_work.push_back(strCode);
163 m_Event.Set();
164 return true;