2022-09-07 19:14:32 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2022 Paul Davis <paul@linuxaudiosystems.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.
|
|
|
|
*/
|
|
|
|
|
2022-09-08 20:19:13 -04:00
|
|
|
#include <cmath>
|
2022-09-08 09:46:56 -04:00
|
|
|
#include <iostream>
|
|
|
|
|
2022-09-10 17:19:16 -04:00
|
|
|
#include <glib/gstdio.h>
|
|
|
|
|
2022-09-13 15:53:39 -04:00
|
|
|
#include "pbd/downloader.h"
|
|
|
|
#include "pbd/inflater.h"
|
2022-09-07 19:02:50 -04:00
|
|
|
#include "pbd/i18n.h"
|
|
|
|
|
|
|
|
#include <glibmm/markup.h>
|
|
|
|
|
2022-09-08 09:46:56 -04:00
|
|
|
#include "ardour/rc_configuration.h"
|
2022-09-07 19:02:50 -04:00
|
|
|
#include "ardour/library.h"
|
|
|
|
|
2022-09-13 15:53:39 -04:00
|
|
|
#include "gui_thread.h"
|
2022-09-07 19:02:50 -04:00
|
|
|
#include "library_download_dialog.h"
|
2022-09-13 23:22:39 -04:00
|
|
|
#include "ui_config.h"
|
2022-09-07 19:02:50 -04:00
|
|
|
|
2022-09-13 15:53:39 -04:00
|
|
|
using namespace PBD;
|
2022-09-08 09:46:56 -04:00
|
|
|
using namespace ARDOUR;
|
|
|
|
using std::string;
|
|
|
|
|
2022-09-07 19:02:50 -04:00
|
|
|
LibraryDownloadDialog::LibraryDownloadDialog ()
|
2022-09-26 03:09:09 -04:00
|
|
|
: ArdourDialog (_("Loop Download Manager"), true) /* modal */
|
2022-09-13 15:53:39 -04:00
|
|
|
, inflater(0)
|
2022-09-07 19:02:50 -04:00
|
|
|
{
|
2022-10-05 12:46:50 -04:00
|
|
|
/* pick some vaguely reasonable window size so that we do not just
|
|
|
|
* grow to show all available rows in the listview
|
|
|
|
*/
|
|
|
|
set_size_request (-1, 600);
|
|
|
|
|
2022-09-07 19:02:50 -04:00
|
|
|
_model = Gtk::ListStore::create (_columns);
|
|
|
|
_display.set_model (_model);
|
|
|
|
|
|
|
|
_display.append_column (_("Name"), _columns.name);
|
|
|
|
_display.append_column (_("Author"), _columns.author);
|
|
|
|
_display.append_column (_("License"), _columns.license);
|
|
|
|
_display.append_column (_("Size"), _columns.size);
|
2022-09-13 15:53:39 -04:00
|
|
|
|
|
|
|
append_install_column ();
|
2022-09-08 20:19:13 -04:00
|
|
|
append_progress_column ();
|
2022-09-07 19:02:50 -04:00
|
|
|
|
|
|
|
_display.set_headers_visible (true);
|
2022-09-13 23:22:39 -04:00
|
|
|
_display.set_hover_selection (true);
|
2022-09-07 19:02:50 -04:00
|
|
|
|
2022-09-08 20:19:13 -04:00
|
|
|
_display.signal_button_press_event().connect (sigc::mem_fun (*this, &LibraryDownloadDialog::display_button_press), false);
|
|
|
|
|
2022-10-05 12:46:50 -04:00
|
|
|
scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
|
|
|
|
scroller.add (_display);
|
|
|
|
|
2022-09-07 19:02:50 -04:00
|
|
|
Gtk::HBox* h = new Gtk::HBox;
|
|
|
|
h->set_spacing (8);
|
|
|
|
h->set_border_width (8);
|
2022-10-05 12:46:50 -04:00
|
|
|
h->pack_start (scroller);
|
2022-09-07 19:02:50 -04:00
|
|
|
|
|
|
|
get_vbox()->set_spacing (8);
|
2022-09-13 23:22:39 -04:00
|
|
|
get_vbox()->set_border_width (12);
|
2022-09-07 19:02:50 -04:00
|
|
|
get_vbox()->pack_start (*Gtk::manage (h));
|
|
|
|
|
2022-09-13 23:22:39 -04:00
|
|
|
_display.get_selection()->set_mode (Gtk::SELECTION_SINGLE);
|
|
|
|
_display.get_selection()->signal_changed().connect (sigc::mem_fun (*this, &LibraryDownloadDialog::library_selected));
|
|
|
|
|
|
|
|
description_view.set_size_request (-1, 80);
|
|
|
|
description_view.set_wrap_mode (Gtk::WRAP_WORD);
|
2022-10-05 12:46:50 -04:00
|
|
|
get_vbox()->pack_start (description_view, Gtk::PACK_SHRINK);
|
2022-09-07 19:02:50 -04:00
|
|
|
|
2022-10-04 13:18:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
LibraryDownloadDialog::~LibraryDownloadDialog ()
|
|
|
|
{
|
|
|
|
delete inflater;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LibraryDownloadDialog::refill ()
|
|
|
|
{
|
2022-09-07 19:02:50 -04:00
|
|
|
ARDOUR::LibraryFetcher lf;
|
|
|
|
lf.get_descriptions ();
|
2022-10-04 13:18:55 -04:00
|
|
|
|
|
|
|
if (lf.n_descriptions()) {
|
|
|
|
_model->clear ();
|
|
|
|
}
|
|
|
|
|
2022-09-07 19:02:50 -04:00
|
|
|
lf.foreach_description (boost::bind (&LibraryDownloadDialog::add_library, this, _1));
|
|
|
|
}
|
|
|
|
|
2022-10-04 13:18:55 -04:00
|
|
|
void
|
|
|
|
LibraryDownloadDialog::on_show ()
|
2022-09-13 15:53:39 -04:00
|
|
|
{
|
2022-10-04 13:18:55 -04:00
|
|
|
ArdourDialog::on_show ();
|
|
|
|
refill ();
|
2022-09-13 15:53:39 -04:00
|
|
|
}
|
|
|
|
|
2022-09-13 23:22:39 -04:00
|
|
|
void
|
|
|
|
LibraryDownloadDialog::library_selected ()
|
|
|
|
{
|
|
|
|
if (_display.get_selection()->count_selected_rows() == 0) {
|
|
|
|
description_view.get_buffer()->set_text (string());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Gtk::TreeModel::iterator row = _display.get_selection()->get_selected();
|
|
|
|
string txt = (*row)[_columns.description];
|
|
|
|
description_view.get_buffer()->set_text (txt);
|
|
|
|
}
|
|
|
|
|
2022-09-08 20:19:13 -04:00
|
|
|
void
|
|
|
|
LibraryDownloadDialog::append_progress_column ()
|
|
|
|
{
|
|
|
|
progress_renderer = new Gtk::CellRendererProgress();
|
|
|
|
progress_renderer->property_width() = 100;
|
|
|
|
Gtk::TreeViewColumn* tvc = manage (new Gtk::TreeViewColumn ("", *progress_renderer));
|
|
|
|
tvc->add_attribute (*progress_renderer, "value", _columns.progress);
|
|
|
|
_display.append_column (*tvc);
|
|
|
|
}
|
|
|
|
|
2022-09-13 15:53:39 -04:00
|
|
|
void
|
|
|
|
LibraryDownloadDialog::append_install_column ()
|
|
|
|
{
|
|
|
|
install_renderer = new Gtk::CellRendererText();
|
|
|
|
Gtk::TreeViewColumn* tvc = manage (new Gtk::TreeViewColumn ("", *install_renderer));
|
|
|
|
tvc->set_data (X_("index"), (void*) (intptr_t (_columns.install.index())));
|
|
|
|
tvc->add_attribute (*install_renderer, "text", _columns.install);
|
|
|
|
_display.append_column (*tvc);
|
|
|
|
}
|
|
|
|
|
2022-09-08 20:19:13 -04:00
|
|
|
|
2022-09-07 19:02:50 -04:00
|
|
|
void
|
|
|
|
LibraryDownloadDialog::add_library (ARDOUR::LibraryDescription const & ld)
|
|
|
|
{
|
|
|
|
|
|
|
|
Gtk::TreeModel::iterator i = _model->append();
|
|
|
|
(*i)[_columns.name] = ld.name();
|
|
|
|
(*i)[_columns.author] = ld.author();
|
|
|
|
(*i)[_columns.license] = ld.license();
|
|
|
|
(*i)[_columns.size] = ld.size();
|
|
|
|
(*i)[_columns.installed] = ld.installed();
|
2022-09-08 09:46:56 -04:00
|
|
|
(*i)[_columns.url] = ld.url();
|
2022-09-13 15:53:39 -04:00
|
|
|
(*i)[_columns.toplevel] = ld.toplevel_dir();
|
2022-09-07 19:02:50 -04:00
|
|
|
|
2022-09-08 20:19:13 -04:00
|
|
|
if (ld.installed()) {
|
2022-09-13 16:20:11 -04:00
|
|
|
(*i)[_columns.install] = string (_("Installed"));
|
2022-09-08 20:19:13 -04:00
|
|
|
} else {
|
|
|
|
(*i)[_columns.install] = string (_("Install"));
|
|
|
|
}
|
|
|
|
|
2022-09-13 15:53:39 -04:00
|
|
|
/* tooltip must be escape for pango markup
|
2022-09-07 19:02:50 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
(*i)[_columns.description] = Glib::Markup::escape_text (ld.description());
|
2022-09-08 20:19:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LibraryDownloadDialog::install (std::string const & path, Gtk::TreePath const & treepath)
|
2022-09-13 15:53:39 -04:00
|
|
|
{
|
|
|
|
std::string destdir = Glib::path_get_dirname (path);
|
|
|
|
|
|
|
|
inflater = new Inflater (path, destdir);
|
|
|
|
inflater->progress.connect (install_connection, invalidator(*this), boost::bind (&LibraryDownloadDialog::install_progress, this, _1, _2, path, treepath), gui_context());
|
|
|
|
inflater->start (); /* starts unpacking in a thread */
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LibraryDownloadDialog::install_progress (size_t nread, size_t total, std::string path, Gtk::TreePath treepath)
|
2022-09-08 20:19:13 -04:00
|
|
|
{
|
|
|
|
Gtk::TreeModel::iterator row = _model->get_iter (treepath);
|
|
|
|
|
2022-09-13 15:53:39 -04:00
|
|
|
if (!inflater) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inflater->status() >= 0) {
|
|
|
|
LibraryDownloadDialog::install_finished (row, path, inflater->status());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*row)[_columns.progress] = (int) round ((double) nread / total);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LibraryDownloadDialog::install_finished (Gtk::TreeModel::iterator row, std::string path, int status)
|
|
|
|
{
|
|
|
|
if (status == 0) {
|
|
|
|
|
|
|
|
std::string toplevel = (*row)[_columns.toplevel];
|
|
|
|
toplevel = Glib::build_filename (Glib::path_get_dirname (path), toplevel);
|
|
|
|
|
|
|
|
LibraryFetcher lf;
|
|
|
|
|
|
|
|
lf.add (toplevel);
|
|
|
|
|
2022-09-08 20:19:13 -04:00
|
|
|
(*row)[_columns.installed] = true;
|
2022-09-13 16:20:11 -04:00
|
|
|
(*row)[_columns.install] = string (_("Installed"));
|
2022-09-13 15:53:39 -04:00
|
|
|
(*row)[_columns.progress] = 100;
|
2022-09-08 20:19:13 -04:00
|
|
|
} else {
|
|
|
|
(*row)[_columns.installed] = false;
|
|
|
|
(*row)[_columns.install] = _("Install");
|
2022-09-13 15:53:39 -04:00
|
|
|
(*row)[_columns.progress] = 0;
|
2022-09-08 20:19:13 -04:00
|
|
|
}
|
2022-09-10 17:19:16 -04:00
|
|
|
|
|
|
|
/* Always unlink (remove) the downloaded archive */
|
|
|
|
|
2022-09-13 15:53:39 -04:00
|
|
|
::g_unlink (path.c_str());
|
|
|
|
|
|
|
|
/* reap thread */
|
|
|
|
|
|
|
|
install_connection.disconnect ();
|
|
|
|
delete inflater;
|
|
|
|
inflater = 0;
|
2022-09-07 19:02:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2022-09-08 20:19:13 -04:00
|
|
|
LibraryDownloadDialog::download (Gtk::TreePath const & path)
|
2022-09-07 19:02:50 -04:00
|
|
|
{
|
2022-09-08 20:19:13 -04:00
|
|
|
Gtk::TreeModel::iterator row = _model->get_iter (path);
|
2022-09-08 09:46:56 -04:00
|
|
|
std::string url = (*row)[_columns.url];
|
2022-09-26 03:10:14 -04:00
|
|
|
PBD::Downloader* downloader;
|
2022-09-08 09:46:56 -04:00
|
|
|
|
2022-09-26 03:10:14 -04:00
|
|
|
try {
|
|
|
|
downloader = new PBD::Downloader (url, ARDOUR::Config->get_clip_library_dir());
|
|
|
|
} catch (...) {
|
|
|
|
(*row)[_columns.install] = _("Error");
|
|
|
|
return;
|
|
|
|
}
|
2022-09-08 09:46:56 -04:00
|
|
|
|
|
|
|
/* setup timer callback to update progressbar */
|
|
|
|
|
2022-09-08 20:19:13 -04:00
|
|
|
Glib::signal_timeout().connect (sigc::bind (sigc::mem_fun (*this, &LibraryDownloadDialog::dl_timer_callback), downloader, path), 40);
|
2022-09-08 09:46:56 -04:00
|
|
|
|
2022-09-10 17:19:16 -04:00
|
|
|
(*row)[_columns.downloader] = downloader;
|
|
|
|
|
2022-09-08 09:46:56 -04:00
|
|
|
/* and go ... */
|
|
|
|
|
|
|
|
downloader->start ();
|
|
|
|
|
2022-09-08 20:19:13 -04:00
|
|
|
/* and back to the GUI event loop, though we're modal so not much is possible */
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
LibraryDownloadDialog::dl_timer_callback (Downloader* dl, Gtk::TreePath treepath)
|
|
|
|
{
|
|
|
|
Gtk::TreeModel::iterator row = _model->get_iter (treepath);
|
|
|
|
|
|
|
|
/* zero status indicates still running; positive status indicates
|
|
|
|
* success; negative value indicates failure
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (dl->status() == 0) {
|
2022-09-10 17:19:16 -04:00
|
|
|
(*row)[_columns.progress] = (int) round (dl->progress() * 100.0);
|
2022-09-08 20:19:13 -04:00
|
|
|
return true; /* call again */
|
|
|
|
}
|
|
|
|
|
|
|
|
(*row)[_columns.progress] = 0.;
|
|
|
|
(*row)[_columns.downloader] = 0;
|
|
|
|
|
|
|
|
if (dl->status() < 0) {
|
|
|
|
(*row)[_columns.install] = _("Install");;
|
|
|
|
} else {
|
|
|
|
(*row)[_columns.install] = _("Installing");
|
|
|
|
install (dl->download_path(), treepath);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete dl;
|
|
|
|
|
|
|
|
return false; /* no more calls, done or cancelled */
|
2022-09-08 09:46:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2022-09-08 20:19:13 -04:00
|
|
|
LibraryDownloadDialog::display_button_press (GdkEventButton* ev)
|
2022-09-08 09:46:56 -04:00
|
|
|
{
|
2022-09-08 20:19:13 -04:00
|
|
|
if ((ev->type == GDK_BUTTON_PRESS) && (ev->button == 1)) {
|
|
|
|
|
|
|
|
Gtk::TreeModel::Path path;
|
|
|
|
Gtk::TreeViewColumn* column;
|
|
|
|
int cellx, celly;
|
|
|
|
|
|
|
|
if (!_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-13 15:53:39 -04:00
|
|
|
int col = (intptr_t) column->get_data (X_("index"));
|
|
|
|
|
|
|
|
if (col != _columns.install.index()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-08 20:19:13 -04:00
|
|
|
Gtk::TreeIter iter = _model->get_iter (path);
|
2022-09-08 09:46:56 -04:00
|
|
|
|
2022-09-08 20:19:13 -04:00
|
|
|
string cur = (*iter)[_columns.install];
|
|
|
|
if (cur == _("Install")) {
|
|
|
|
if (!(*iter)[_columns.installed]) {
|
|
|
|
(*iter)[_columns.install] = _("Cancel");
|
|
|
|
download (path);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Downloader* dl = (*iter)[_columns.downloader];
|
|
|
|
|
|
|
|
if (dl) {
|
|
|
|
dl->cancel ();
|
|
|
|
}
|
|
|
|
|
|
|
|
(*iter)[_columns.install] = _("Install");
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2022-09-08 09:46:56 -04:00
|
|
|
}
|
2022-09-08 20:19:13 -04:00
|
|
|
|
|
|
|
return false;
|
2022-09-07 19:02:50 -04:00
|
|
|
}
|