|
@@ -1,4 +1,7 @@
|
|
|
#include "MainWindow.hpp"
|
|
|
+#include "core.hpp"
|
|
|
+#include <YoutubeApi/youtubeapi.hpp>
|
|
|
+#include <unistd.h>
|
|
|
|
|
|
namespace components {
|
|
|
|
|
@@ -17,9 +20,10 @@ namespace components {
|
|
|
m_search_btn.set_child(*(this->getButtonContents("Поиск", "find")));
|
|
|
m_search_btn.signal_clicked().connect(sigc::mem_fun(
|
|
|
*this,
|
|
|
- &MainWindow::on_btn_clicked
|
|
|
+ &MainWindow::onSearchButtonClicked
|
|
|
));
|
|
|
m_search_field.set_hexpand(true);
|
|
|
+ m_search_field.set_placeholder_text("Поиск YouTube");
|
|
|
m_search_box.append(m_search_field);
|
|
|
m_search_box.append(m_search_btn);
|
|
|
m_search_box.set_margin(8);
|
|
@@ -27,12 +31,13 @@ namespace components {
|
|
|
// --Body-
|
|
|
// Модель видео
|
|
|
m_video_storage = Gio::ListStore<VideoModel>::create();
|
|
|
- m_video_storage->append(VideoModel::create(1, "7yZvzo4_s6Q", "Hermitcraft 10 - Ep 12: Who Has The Best Storage System???"));
|
|
|
|
|
|
// Модель выбора
|
|
|
- auto selection_model = Gtk::SingleSelection::create(m_video_storage);
|
|
|
+ selection_model = Gtk::SingleSelection::create(m_video_storage);
|
|
|
selection_model->set_autoselect(false);
|
|
|
selection_model->set_can_unselect(true);
|
|
|
+ selection_model->property_selected().signal_changed().connect(
|
|
|
+ sigc::mem_fun(*this, &MainWindow::onVideoChanged));
|
|
|
m_video_list.set_model(selection_model);
|
|
|
|
|
|
// Создание фабрики
|
|
@@ -42,6 +47,8 @@ namespace components {
|
|
|
factory->signal_bind().connect(
|
|
|
sigc::mem_fun(*this, &MainWindow::onVideoBind));
|
|
|
m_video_list.set_factory(factory);
|
|
|
+ m_video_list.signal_activate().connect(
|
|
|
+ sigc::mem_fun(*this, &MainWindow::onVideoSelected));
|
|
|
|
|
|
// Прокручиваемое окно
|
|
|
m_video_scroll.set_policy(Gtk::PolicyType::AUTOMATIC, Gtk::PolicyType::AUTOMATIC);
|
|
@@ -62,9 +69,21 @@ namespace components {
|
|
|
{
|
|
|
}
|
|
|
|
|
|
- void MainWindow::on_btn_clicked()
|
|
|
+ void MainWindow::onSearchButtonClicked()
|
|
|
{
|
|
|
- //m_video_storage->append(VideoModel::create(2, "test."));
|
|
|
+ // Очистить предыдущие результаты
|
|
|
+ m_video_storage->remove_all();
|
|
|
+
|
|
|
+ // Получить результаты поиска
|
|
|
+ Glib::ustring query = m_search_field.get_buffer()->get_text();
|
|
|
+ db::video found;
|
|
|
+ std::vector<std::string> video_ids = ytapi::getVideoIDsByQuery(query.release());
|
|
|
+
|
|
|
+ // Добавить в список видео
|
|
|
+ for (auto id : video_ids) {
|
|
|
+ found = core::getVideoByYTID(id);
|
|
|
+ m_video_storage->append(VideoModel::create(found.id, found.yt_id, found.title));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
void MainWindow::onVideoSetup(const Glib::RefPtr<Gtk::ListItem>& list_item)
|
|
@@ -77,6 +96,26 @@ namespace components {
|
|
|
list_item->set_child(*box_main);
|
|
|
}
|
|
|
|
|
|
+ void MainWindow::onVideoChanged()
|
|
|
+ {
|
|
|
+ auto position = selection_model->get_selected();
|
|
|
+ auto item = m_video_storage->get_item(position);
|
|
|
+ if (!item) return;
|
|
|
+
|
|
|
+ // Запуск mpv в заднем фоне
|
|
|
+ int pid = fork();
|
|
|
+ if (pid == 0) {
|
|
|
+ std::string yt_url = "https://youtu.be/"+item->m_yt_id;
|
|
|
+ execl(
|
|
|
+ "/usr/bin/mpv", // Путь к mpv
|
|
|
+ "mpv", // Название файла
|
|
|
+ "--no-terminal", // Терминал не нужен
|
|
|
+ yt_url.c_str(), // URL видео
|
|
|
+ (char*)0 // Терминатор
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
void MainWindow::onVideoBind(const Glib::RefPtr<Gtk::ListItem>& list_item)
|
|
|
{
|
|
|
auto pos = list_item->get_position();
|
|
@@ -119,6 +158,19 @@ namespace components {
|
|
|
lbl_title->set_text(item->m_title);
|
|
|
}
|
|
|
|
|
|
+ // DO NOT USE!!
|
|
|
+ void MainWindow::onVideoSelected(unsigned int position)
|
|
|
+ {
|
|
|
+ std::cout << position;
|
|
|
+ auto item = m_video_storage->get_item(position);
|
|
|
+ if (!item) return;
|
|
|
+
|
|
|
+ std::string command = "mpv https://youtu.be/"+item->m_yt_id;
|
|
|
+
|
|
|
+ std::cout << command;
|
|
|
+ system(command.c_str());
|
|
|
+ }
|
|
|
+
|
|
|
Gtk::Box* MainWindow::getButtonContents(Glib::ustring text, std::string icon_name)
|
|
|
{
|
|
|
// Создание иконки и текста
|