Newer
Older
Kevin Whitaker
committed
/*
* Copyright (C) 2017 Kevin Whitaker <eyecreate@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "RequestInterface.h"
Kevin Whitaker
committed
#include <Wt/Dbo/Session>
#include <Wt/Dbo/Transaction>
#include "../db/UserAction.h"
#include <Wt/Dbo/ptr>
#include <Wt/Dbo/collection>
Kevin Whitaker
committed
RequestInterface::RequestInterface(WebInterface* app)
{
this->app = app;
mainLayout = new Wt::WVBoxLayout();
searchHeader = new Wt::WContainerWidget();
searchLayout = new Wt::WHBoxLayout();
searchBox = new Wt::WLineEdit();
searchBtn = new Wt::WPushButton();
resultBody = new Wt::WContainerWidget();
resultLayout = new Wt::WGridLayout();
this->setLayout(mainLayout);
mainLayout->addWidget(searchHeader,0,Wt::AlignmentFlag::AlignCenter);
mainLayout->addWidget(resultBody);
searchHeader->setLayout(searchLayout);
searchBtn->setTextFormat(Wt::XHTMLText);
searchBtn->decorationStyle().font().setFamily(Wt::WFont::Default,"FontAwesome");
searchBtn->setText("");
searchLayout->addWidget(searchBox);
searchLayout->addWidget(searchBtn);
Kevin Whitaker
committed
searchBtn->clicked().connect(this, &RequestInterface::searchClicked);
resultBody->setLayout(resultLayout);
Kevin Whitaker
committed
resultBody->setOverflow(Wt::WContainerWidget::OverflowAuto);
}
void RequestInterface::trackRequestClicked(AudioTrack track)
{
Kevin Whitaker
committed
//TODO
}
Kevin Whitaker
committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
void RequestInterface::searchClicked()
{
if(!searchBox->text().empty())
{
Wt::Dbo::Session sqlSession;
sqlSession.setConnectionPool(*GroovePlayerMgr::getInstance()->connectionPool);
sqlSession.mapClass<User>("user");
sqlSession.mapClass<AudioTrack>("tracks");
sqlSession.mapClass<UserAction>("actions");
Wt::Dbo::Transaction searchTransaction(sqlSession);
//Do quick check for invalid seaches
if(searchBox->text().toUTF8().find("\"") != std::string::npos)
{
//Bad character, reject search.
searchBox->setText("");
searchTransaction.commit();
return;
}
//Do a like search on title, artist, and album on DB
Wt::Dbo::collection<Wt::Dbo::ptr<AudioTrack>> trackOptions = sqlSession.find<AudioTrack>(std::string("where tracks.name like \"%")+searchBox->text().toUTF8()+std::string("%\" OR tracks.artist like \"%")+searchBox->text().toUTF8()+std::string("%\" OR tracks.album like \"%")+searchBox->text().toUTF8()+std::string("%\""));
//Make sure all items are gone
for(RequestItem* item: foundItems)
{
delete item;
}
foundItems.clear();
resultLayout->clear();
//Put results into grid
if(trackOptions.size() > 0 && trackOptions.size() < 50) {
bool left = true;
int rowCount = 0;
for(Wt::Dbo::collection<Wt::Dbo::ptr<AudioTrack>>::const_iterator dbTrack = trackOptions.begin(); dbTrack != trackOptions.end(); ++dbTrack)
{
AudioTrack trackItem = (*(*dbTrack));
GroovePlayerMgr::getInstance()->getPictureFromTrack(&trackItem);
RequestItem* item = new RequestItem(this,trackItem);
foundItems.push_back(item);
resultLayout->addWidget(item,rowCount,left?0:1);
if(!left) rowCount += 1;
left = !left;
}
}
else
{
searchBox->setText("Not enough/too many results");
}
searchTransaction.commit();
}
}