From 8e4f348b8d7fe329e3165a537279e406533228a1 Mon Sep 17 00:00:00 2001 From: zvon Date: Wed, 12 Feb 2020 11:48:03 +0100 Subject: [PATCH] Move some shared functions into gtkfunctions.cpp --- gtkfunctions.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ gtkfunctions.hpp | 14 +++++++++++ 2 files changed, 79 insertions(+) create mode 100644 gtkfunctions.cpp create mode 100644 gtkfunctions.hpp diff --git a/gtkfunctions.cpp b/gtkfunctions.cpp new file mode 100644 index 0000000..43672a4 --- /dev/null +++ b/gtkfunctions.cpp @@ -0,0 +1,65 @@ +#include "gtkfunctions.hpp" + +#include +#include + +#include "tv_rename.hpp" + +void searchShow( Gtk::Entry *entry_show, Gtk::ComboBox *combo_possible, + const Gtk::TreeModelColumn< std::string > &col_show, + const Gtk::TreeModelColumn< std::string > &col_id, + const std::string &language_code, Gtk::Window *parent ) { + // check required field is filled out + if ( entry_show->get_text().empty() ) { + Gtk::MessageDialog dialog( *parent, "Show field is empty" ); + dialog.run(); + return; + } + + // fill up m_combo_possible with possible tv shows + auto possible_shows = + searchShow( std::string( entry_show->get_text() ), language_code ); + + // if no possible shows were found, tell the user + if ( possible_shows.size() == 0 ) { + Gtk::MessageDialog dialog( *parent, + "No results found for given show name" ); + dialog.run(); + return; + } + + // show widgets + combo_possible->show(); + + // fill up combo box with results from thetvdb + auto model = + static_cast< Gtk::ListStore * >( combo_possible->get_model().get() ); + + auto row = *( model->append() ); + + row[col_show] = possible_shows[0].first; + row[col_id] = possible_shows[0].second; + combo_possible->set_active( row ); + + for ( size_t i = 1; i < possible_shows.size(); i++ ) { + auto row = *( model->append() ); + row[col_show] = possible_shows[i].first; + row[col_id] = possible_shows[i].second; + } +} + +void freeChildren( std::vector< Gtk::Widget * > &children ) { + size_t max = children.size(); + size_t index{}; + while ( index < max ) { + if ( auto *p = dynamic_cast< Gtk::Container * >( children[index] ) ) { + auto temp = p->get_children(); + children.insert( children.end(), temp.begin(), temp.end() ); + max = children.size(); + } + index++; + } + for ( int i = max - 1; i >= 0; i-- ) { + delete children[i]; + } +} diff --git a/gtkfunctions.hpp b/gtkfunctions.hpp new file mode 100644 index 0000000..27bcb04 --- /dev/null +++ b/gtkfunctions.hpp @@ -0,0 +1,14 @@ +#ifndef GTK_FUNCTIONS_HPP +#define GTK_FUNCTIONS_HPP + +#include +#include + +void searchShow( Gtk::Entry *entry_show, Gtk::ComboBox *combo_possible, + const Gtk::TreeModelColumn< std::string > &col_show, + const Gtk::TreeModelColumn< std::string > &col_id, + const std::string &language_code, Gtk::Window *parent ); + +void freeChildren( std::vector< Gtk::Widget * > &children ); + +#endif