2 * Copyright (C) 2005-2013 Team Kodi
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)
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"
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 },
37 m_api_nomore
{ false },
38 m_initialized
{ false }
41 m_codechars
= "abcdefghijklmnopqrstuvwxyz";
45 void CInputCodingTableBaiduPY::Process()
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
);
57 auto work
= m_work
.front();
62 XFILE::CCurlFile http
;
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
77 std::vector
<std::wstring
> words
;
79 reg
.RegComp("\\[\"(.+?)\",[^\\]]+\\]");
82 while ((pos
= reg
.RegFind(response
.c_str(), pos
)) >= 0)
85 std::string full
= reg
.GetMatch(0);
86 std::string word
= reg
.GetMatch(1);
88 words
.push_back(UnicodeToWString(word
));
90 if (words
.size() < 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
);
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)
111 sscanf(unicode
.c_str() + i
, "\\u%x", &c
);
112 result
+= (wchar_t)c
;
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
);
125 void CInputCodingTableBaiduPY::Initialize()
127 CSingleLock
lock(m_CS
);
132 void CInputCodingTableBaiduPY::Deinitialize()
136 m_initialized
= false;
139 bool CInputCodingTableBaiduPY::IsInitialized() const
141 return m_initialized
;
144 bool CInputCodingTableBaiduPY::GetWordListPage(const std::string
& strCode
, bool isFirstPage
)
148 if (isFirstPage
|| m_code
!= strCode
)
153 m_api_nomore
= false;
161 CSingleLock
lock(m_CS
);
162 m_work
.push_back(strCode
);