File indexing completed on 2025-02-02 04:11:30

0001 /*
0002  * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #include "plugin_settings_widget.hpp"
0008 #include "ui_plugin_settings_widget.h"
0009 
0010 #include "plugin/plugin.hpp"
0011 
0012 #include <QEvent>
0013 
0014 using namespace glaxnimate::gui;
0015 using namespace glaxnimate;
0016 
0017 PluginSettingsWidget::PluginSettingsWidget(QWidget* parent)
0018     : QWidget(parent), d ( std::make_unique<Ui::PluginSettingsWidget>() )
0019 {
0020     d->setupUi ( this );
0021     d->list_services->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
0022     update_entries();
0023 }
0024 
0025 PluginSettingsWidget::~PluginSettingsWidget() = default;
0026 
0027 void PluginSettingsWidget::changeEvent ( QEvent* e )
0028 {
0029     QWidget::changeEvent(e);
0030 
0031     if ( e->type() == QEvent::LanguageChange)
0032     {
0033         d->retranslateUi(this);
0034         update_entries();
0035     }
0036 }
0037 
0038 void PluginSettingsWidget::current_changed ( QListWidgetItem* item )
0039 {
0040     d->stacked_widget->setCurrentWidget(d->page_noplugin);
0041 
0042     if ( !item )
0043         return;
0044 
0045     current = plugin::PluginRegistry::instance().plugin(item->data(Qt::UserRole).toString());
0046     if ( !current )
0047         return;
0048 
0049     bool checked = item->checkState() == Qt::Checked;
0050     if ( checked != current->enabled() )
0051     {
0052         if ( checked )
0053             current->enable();
0054         else
0055             current->disable();
0056     }
0057 
0058     d->widget_plugin->setTitle(current->data().name);
0059     d->line_plugin_path->setText(current->data().dir.absolutePath());
0060     d->line_version->setText(QString::number(current->data().version));
0061     d->line_author->setText(current->data().author);
0062     d->btn_disable->setEnabled(current->can_disable());
0063     d->btn_enable->setEnabled(current->can_enable());
0064     d->btn_uninstall->setEnabled(current->user_installed());
0065     d->description->setPlainText(current->data().description);
0066 
0067     d->list_services->clearContents();
0068     d->list_services->setRowCount(current->data().services.size());
0069 
0070     int row = 0;
0071     for ( const auto& svc : current->data().services )
0072     {
0073         QString type = i18n("Unknown");
0074         if ( svc->type() == plugin::ServiceType::Action )
0075             type = i18n("Menu Action");
0076         else if ( svc->type() == plugin::ServiceType::IoFormat )
0077             type = i18n("File Format");
0078 
0079         QTableWidgetItem* it;
0080 
0081         it = new QTableWidgetItem(svc->service_icon(), type);
0082         it->setFlags(Qt::ItemIsEnabled|Qt::ItemIsSelectable);
0083         d->list_services->setItem(row, 0, it);
0084         it = new QTableWidgetItem(svc->name());
0085         it->setFlags(Qt::ItemIsEnabled|Qt::ItemIsSelectable);
0086         d->list_services->setItem(row, 1, it);
0087         row++;
0088     }
0089 
0090 
0091     d->stacked_widget->setCurrentWidget(d->page_plugin);
0092 
0093 }
0094 
0095 void PluginSettingsWidget::disable_current()
0096 {
0097     if ( current )
0098     {
0099         current->disable();
0100         update_entries();
0101     }
0102 }
0103 
0104 void PluginSettingsWidget::enable_current()
0105 {
0106     if ( current )
0107     {
0108         current->enable();
0109         update_entries();
0110     }
0111 }
0112 
0113 void PluginSettingsWidget::install_dialog()
0114 {
0115     /// @todo
0116 }
0117 
0118 void PluginSettingsWidget::refresh_plugins()
0119 {
0120     clear_selection();
0121     plugin::PluginRegistry::instance().load();
0122     update_entries();
0123 }
0124 
0125 void PluginSettingsWidget::uninstall_current()
0126 {
0127     /// @todo
0128 }
0129 
0130 void PluginSettingsWidget::update_entries()
0131 {
0132     clear_selection();
0133 
0134     QString current_id;
0135     if ( auto item = d->list_plugins->currentItem() )
0136     {
0137         current_id = item->data(Qt::UserRole).toString();
0138     }
0139 
0140     QListWidgetItem* current_item = nullptr;
0141 
0142     d->list_plugins->blockSignals(true);
0143     d->list_plugins->clear();
0144 
0145     for ( const auto& plugin : plugin::PluginRegistry::instance().plugins() )
0146     {
0147         QListWidgetItem* item = new QListWidgetItem();
0148         item->setIcon(plugin->icon());
0149         item->setText(plugin->data().name);
0150         item->setCheckState(plugin->enabled() ? Qt::Checked : Qt::Unchecked );
0151         item->setData(Qt::UserRole, plugin->data().id);
0152         Qt::ItemFlags flags = Qt::ItemIsSelectable;
0153         if ( plugin->available() )
0154             flags |= Qt::ItemIsEnabled|Qt::ItemIsUserCheckable;
0155         item->setFlags(flags);
0156         item->setData(Qt::ToolTipRole, plugin->data().description);
0157 
0158         d->list_plugins->addItem(item);
0159 
0160         if ( plugin->data().id == current_id )
0161         {
0162             d->list_plugins->setCurrentItem(item);
0163             current_item = item;
0164         }
0165 
0166     }
0167 
0168     if ( !current_item )
0169         current_item = d->list_plugins->currentItem();
0170 
0171     d->list_plugins->blockSignals(false);
0172     current_changed(current_item);
0173 
0174 }
0175 
0176 void PluginSettingsWidget::clear_selection()
0177 {
0178     current = nullptr;
0179     d->stacked_widget->setCurrentWidget(d->page_noplugin);
0180 }
0181