radio_plus_plus/search.cpp

96 lines
2.4 KiB
C++
Raw Permalink Normal View History

2022-11-24 00:56:35 +01:00
#include "search.h"
wxBEGIN_EVENT_TABLE(cSearch, wxFrame)
EVT_BUTTON(1006, cSearch::onSearch)
EVT_BUTTON(1007, cSearch::onAdd)
wxEND_EVENT_TABLE()
cSearch::cSearch() : wxFrame(nullptr, wxID_ANY, "Search", wxPoint(30, 30), wxSize(500, 700))
{
wxFrame::SetMinSize(wxSize(500, 700));
wxFrame::SetMaxSize(wxSize(500, 700));
searchBox = new wxTextCtrl(this, wxID_ANY, "", wxPoint(10, 20), wxSize(300, 40));
searchButton = new wxButton(this, 1006, "Search", wxPoint(365, 20), wxSize(80, 40));
searchAdd = new wxButton(this, 1007, "Add", wxPoint(365, 70), wxSize(80, 40));
searchList = new wxListBox(this, wxID_ANY, wxPoint(10, 130), wxSize(480, 500));
}
cSearch::~cSearch()
{
}
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
void cSearch::onSearch(wxCommandEvent &evt) {
searchList->Clear();
wxString boxValue(searchBox->GetValue());
std::string api = "https://api.laut.fm/search/stations?query=" + std::string(boxValue.mb_str());
CURL *curl;
CURLcode res;
std::string readBuffer;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, api.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
bool j_parse = jread.parse(readBuffer, jval, false);
if (j_parse) {
Json::Value arrays = jval["results"][0]["items"];
int arraynum = arrays.size();
for (int i = 0; i < arraynum; i++)
{
std::string name = fastWriter.write(jval["results"][0]["items"][i]["station"]["stream_url"]);
searchList->Append(name);
}
}
}
evt.Skip();
}
void cSearch::onAdd(wxCommandEvent &evt)
{
if (searchList->GetSelection() != wxNOT_FOUND)
{
std::string item = std::string(searchList->GetStringSelection());
item.erase(0, 1);
int start{item.length()-2};
int end{item.length()-1};
item.erase(start, end);
std::ofstream writefile;
writefile.open(homedir+"/.config/radio++/sender", std::ios::app);
writefile << item << "\n";
writefile.close();
}
evt.Skip();
}