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
57
58
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
if(getRequestCountForUser(app->currentUser) < 3)
{
GroovePlayerMgr::getInstance()->requestQueue.push_back(std::pair<AudioTrack,User>(track,app->currentUser));
//Now check if this is this user's last request and disable buttons.
updateRequestButtonState();
}
}
void RequestInterface::updateRequestButtonState()
{
if(getRequestCountForUser(app->currentUser) < 3)
{
//enable buttons
for(RequestItem* item : foundItems)
{
if(item != nullptr) item->voteBtn->setEnabled(true);
}
}
else
{
//disable buttons
for(RequestItem* item : foundItems)
{
if(item != nullptr) item->voteBtn->setEnabled(false);
}
}
}
int RequestInterface::getRequestCountForUser(User user)
{
int count = 0;
for(std::pair<AudioTrack,User> request : GroovePlayerMgr::getInstance()->requestQueue)
{
if(request.second.username == user.username)
{
count += 1;
}
}
return count;
Kevin Whitaker
committed
}
Kevin Whitaker
committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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;
}
Kevin Whitaker
committed
updateRequestButtonState();
Kevin Whitaker
committed
}
else
{
searchBox->setText("Not enough/too many results");
}
searchTransaction.commit();
}
}