File indexing completed on 2024-07-21 06:28:18

0001 /*
0002     SPDX-FileCopyrightText: 2018 Jasem Mutlaq <mutlaqja@ikarustech.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "customdrivers.h"
0008 
0009 #include "Options.h"
0010 
0011 #include "kstars.h"
0012 #include "driverinfo.h"
0013 #include "kspaths.h"
0014 #include "kstarsdata.h"
0015 #include "ksnotification.h"
0016 
0017 
0018 CustomDrivers::CustomDrivers(QWidget *parent, const QList<QSharedPointer<DriverInfo>> &driversList) : QDialog(parent),
0019     m_DriversList(driversList)
0020 {
0021     setupUi(this);
0022 
0023     auto userdb = QSqlDatabase::database(KStarsData::Instance()->userdb()->connectionName());
0024     model = new QSqlTableModel(this, userdb);
0025     model->setTable("customdrivers");
0026 
0027     driversView->setModel(model);
0028     driversView->hideColumn(0);
0029 
0030     refreshFromDB();
0031 
0032     tipLabel->setPixmap((QIcon::fromTheme("help-hint").pixmap(32, 32)));
0033     cautionLabel->setPixmap((QIcon::fromTheme("emblem-warning").pixmap(32, 32)));
0034 
0035     familyCombo->addItems(DeviceFamilyLabels.values());
0036 
0037     for (const auto &oneDriver : driversList)
0038     {
0039         // MDPD drivers CANNOT have aliases.
0040         // JM 2021.10.31 While usually MDPD drivers cannot have aliases
0041         // We should add the ability to do so in case only a single device is connected.
0042         //        if (oneDriver->getAuxInfo().value("mdpd", false).toBool() == true)
0043         //            continue;
0044 
0045         QString label = oneDriver->getLabel();
0046         if (label.isEmpty())
0047             continue;
0048 
0049         driverCombo->addItem(label);
0050     }
0051 
0052     syncDriver();
0053 
0054     connect(driverCombo, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated),
0055             this, &CustomDrivers::syncDriver);
0056 
0057     connect(labelIN, &QLineEdit::textChanged, this, [&]()
0058     {
0059         addDriverB->setEnabled(labelIN->text().isEmpty() == false);
0060     });
0061 
0062     connect(driversView, &QTableView::pressed, this, [&]()
0063     {
0064         removeDriverB->setEnabled(true);
0065     });
0066 
0067     connect(addDriverB, &QPushButton::clicked, this, &CustomDrivers::addDriver);
0068     connect(removeDriverB, &QPushButton::clicked, this, &CustomDrivers::removeDriver);
0069 }
0070 
0071 CustomDrivers::~CustomDrivers()
0072 {
0073     userdb.close();
0074 }
0075 
0076 void CustomDrivers::refreshFromDB()
0077 {
0078     KStarsData::Instance()->userdb()->GetAllCustomDrivers(m_CustomDrivers);
0079     model->select();
0080 }
0081 
0082 void CustomDrivers::syncDriver()
0083 {
0084     const QString currentDriverLabel = driverCombo->currentText();
0085 
0086     for (const QSharedPointer<DriverInfo>oneDriver : m_DriversList)
0087     {
0088         if (currentDriverLabel == oneDriver->getLabel())
0089         {
0090             familyCombo->setCurrentIndex(oneDriver->getType());
0091             execIN->setText(oneDriver->getExecutable());
0092             nameIN->setText(oneDriver->getName());
0093             manufacturerIN->setText(oneDriver->manufacturer());
0094             break;
0095         }
0096     }
0097 }
0098 
0099 void CustomDrivers::addDriver()
0100 {
0101     // Make sure label is unique in canonical drivers
0102     for (auto &oneDriver : m_DriversList)
0103     {
0104         if (labelIN->text() == oneDriver->getLabel())
0105         {
0106             KSNotification::error(i18n("Label already exists. Label must be unique."));
0107             return;
0108         }
0109     }
0110 
0111     QVariantMap newDriver;
0112 
0113     newDriver["Label"] = labelIN->text();
0114     newDriver["Name"] = nameIN->text();
0115     newDriver["Family"] = familyCombo->currentText();
0116     newDriver["Manufacturer"] = manufacturerIN->text();
0117     newDriver["Exec"] = execIN->text();
0118     // TODO Try to make this editable as well
0119     newDriver["Version"] = "1.0";
0120 
0121     if (KStarsData::Instance()->userdb()->AddCustomDriver(newDriver) == false)
0122         KSNotification::error(i18n("Failed to add new driver. Is the label unique?"));
0123 
0124     refreshFromDB();
0125 
0126 }
0127 
0128 void CustomDrivers::removeDriver()
0129 {
0130     int row = driversView->currentIndex().row();
0131 
0132     if (row < 0)
0133         return;
0134 
0135     QVariantMap oneDriver = m_CustomDrivers[row];
0136 
0137     KStarsData::Instance()->userdb()->DeleteCustomDriver(oneDriver["id"].toString());
0138 
0139     refreshFromDB();
0140 }