radio_plus_plus/main.cpp

220 lines
5.4 KiB
C++
Raw Normal View History

2022-11-24 00:56:35 +01:00
#include "main.h"
#include "search.h"
// Binding events to functions
wxBEGIN_EVENT_TABLE(cFrame, wxFrame)
EVT_BUTTON(1001, cFrame::OnPP)
EVT_SLIDER(1002, cFrame::OnScroll)
EVT_BUTTON(1003, cFrame::OnAdd)
EVT_BUTTON(1004, cFrame::OnRemove)
EVT_BUTTON(1005, cFrame::OnSearch)
EVT_BUTTON(1008, cFrame::refreshEvent)
wxEND_EVENT_TABLE()
// Window content
cFrame::cFrame() : wxFrame(nullptr, wxID_ANY, "Radio++", wxPoint(30, 30), wxSize(450, 600)) {
wxFrame::SetMinSize(wxSize(450, 600));
wxFrame::SetMaxSize(wxSize(450, 600));
menuBar->Append(menuHelp, "&Help");
menuHelp->Append(wxID_ABOUT);
menuHelp->AppendSeparator();
menuHelp->Append(wxID_EXIT);
SetMenuBar(menuBar);
Bind(wxEVT_MENU, &cFrame::OnAbout, this, wxID_ABOUT);
Bind(wxEVT_MENU, &cFrame::OnExit, this, wxID_EXIT);
b_pp = new wxButton(this, 1001, "Play", wxPoint(10, 10), wxSize(100,50));
t_box = new wxTextCtrl(this, wxID_ANY, "", wxPoint(10, 190), wxSize(250, 30));
t_show = new wxStaticText(this, wxID_ANY, "None", wxPoint(10, 80), wxSize(50, 100));
s_val = new wxStaticText(this, wxID_ANY, "70", wxPoint(150, 100), wxSize(50, 100));
s_volume = new wxSlider(this, 1002, 70, 0, 100, wxPoint(50, 100), wxSize(220, 80));
b_add = new wxButton(this, 1003, "Add", wxPoint(280, 230), wxSize(80,30));
b_add = new wxButton(this, 1004, "Remove", wxPoint(280, 270), wxSize(80,30));
s_sender = new wxButton(this, 1005, "Search", wxPoint(280, 310), wxSize(80,30));
b_refresh = new wxButton(this, 1008, "Refresh", wxPoint(280, 350), wxSize(80,30));
l_sender = new wxListBox(this, wxID_ANY, wxPoint(10, 230), wxSize(250, 300));
// Text for the Statusbar
CreateStatusBar();
SetStatusText("Radio++");
cFrame::refreshList();
}
cFrame::~cFrame() {
}
void cFrame::refreshList() {
l_sender->Clear();
readfile.open(homedir+"/.config/radio++/sender");
if (readfile.is_open())
{
while ( getline (readfile,line) )
{
wxString wx_line(line);
l_sender->Append(wx_line);
}
readfile.close();
}
}
void cFrame::refreshEvent(wxCommandEvent &evt) {
l_sender->Clear();
readfile.open(homedir+"/.config/radio++/sender");
if (readfile.is_open())
{
while ( getline (readfile,line) )
{
wxString wx_line(line);
l_sender->Append(wx_line);
}
readfile.close();
}
evt.Skip();
}
// Get Slider Value and sets Volume
void cFrame::OnScroll(wxCommandEvent &evt) {
wxString value = wxString::Format(wxT("%d"), (int)s_volume->GetValue());
s_val->SetLabel(value);
int vol = wxAtoi(value);
if (b_pp->GetLabel() == "Pause") {
libvlc_audio_set_volume(mp, vol);
}
evt.Skip();
}
// Plays Radio sound yee
void cFrame::playRadio(const char* url) {
sender = libvlc_media_new_location(inst, url);
mp = libvlc_media_player_new_from_media(sender);
libvlc_media_player_play(mp);
}
// Play/Pause Button
void cFrame::OnPP(wxCommandEvent &evt) {
if(b_pp->GetLabel() == "Play") {
t_show->SetLabel("Playing...");
b_pp->SetLabel("Pause");
std::string url = std::string(l_sender->GetStringSelection().mb_str());
const char * c_url = url.c_str();
cFrame::playRadio(c_url);
wxString value = s_val->GetLabel();
int vol = wxAtoi(value);
libvlc_audio_set_volume(mp, vol);
} else if (b_pp->GetLabel() == "Pause") {
t_show->SetLabel("Paused");
b_pp->SetLabel("Play");
libvlc_media_player_stop(mp);
}
evt.Skip();
}
// Add Button
void cFrame::OnAdd(wxCommandEvent &evt) {
l_sender->Append(t_box->GetValue());
std::string item = std::string(t_box->GetValue().mb_str());
writefile.open(homedir+"/.config/radio++/sender", std::ios::app);
writefile << item << "\n";
writefile.close();
t_box->Clear();
}
// Remove Button
void cFrame::OnRemove(wxCommandEvent &evt) {
if (l_sender->GetSelection() != wxNOT_FOUND) {
std::string deleteline = std::string(l_sender->GetStringSelection().mb_str());
l_sender->Delete(l_sender->GetSelection());
// https://stackoverflow.com/questions/26576714/deleting-specific-line-from-file
std::string path = homedir+"/.config/radio++/sender";
std::string tempfile = homedir+"/.config/radio++/temp.txt";
std::string line;
std::ifstream fin;
fin.open(path);
// contents of path must be copied to a temp file then
// renamed back to the path file
std::ofstream temp;
temp.open(homedir+"/.config/radio++/temp.txt");
while (getline(fin, line)) {
// write all lines to temp other than the line marked for erasing
if (line != deleteline)
temp << line << std::endl;
}
temp.close();
fin.close();
// required conversion for remove and rename functions
const char * t = tempfile.c_str();
const char * p = path.c_str();
remove(p);
rename(t, p);
}
evt.Skip();
}
// Search Button
void cFrame::OnSearch(wxCommandEvent &evt)
{
cSearch* m_frame2 = new cSearch();
m_frame2->Show(true);
evt.Skip();
}
// OnAbout
void cFrame::OnAbout(wxCommandEvent &evt)
{
wxMessageBox("By MIRO#5825",
"About", wxOK | wxICON_INFORMATION);
}
// OnExit
void cFrame::OnExit(wxCommandEvent &evt) {
libvlc_release (inst);
Close(true);
}