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

0001 /*
0002     SPDX-FileCopyrightText: 2003 Jasem Mutlaq <mutlaqja@ikarustech.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "opsindi.h"
0008 
0009 #include "ksnotification.h"
0010 #include "kstars.h"
0011 #include "Options.h"
0012 
0013 #include <KConfigDialog>
0014 
0015 #include <QCheckBox>
0016 #include <QComboBox>
0017 #include <QDesktopServices>
0018 #include <QFileDialog>
0019 #include <QPushButton>
0020 #include <QStringList>
0021 
0022 OpsINDI::OpsINDI() : QFrame(KStars::Instance())
0023 {
0024     setupUi(this);
0025 
0026     //Get a pointer to the KConfigDialog
0027     m_ConfigDialog = KConfigDialog::exists("settings");
0028 
0029     selectFITSDirB->setIcon(
0030         QIcon::fromTheme("document-open-folder"));
0031     selectFITSDirB->setAttribute(Qt::WA_LayoutUsesWidgetRect);
0032     selectDriversDirB->setIcon(
0033         QIcon::fromTheme("document-open-folder"));
0034     selectDriversDirB->setAttribute(Qt::WA_LayoutUsesWidgetRect);
0035     selectINDIHubB->setIcon(
0036         QIcon::fromTheme("document-open"));
0037     selectINDIHubB->setAttribute(Qt::WA_LayoutUsesWidgetRect);
0038 
0039 #ifdef Q_OS_OSX
0040     connect(kcfg_indiServerIsInternal, SIGNAL(clicked()), this, SLOT(toggleINDIInternal()));
0041     kcfg_indiServerIsInternal->setToolTip(i18n("Internal or external INDI server?"));
0042     connect(kcfg_indiDriversAreInternal, SIGNAL(clicked()), this, SLOT(toggleDriversInternal()));
0043     kcfg_indiDriversAreInternal->setToolTip(i18n("Internal or external INDI drivers?"));
0044 
0045     if (Options::indiServerIsInternal())
0046         kcfg_indiServer->setEnabled(false);
0047     if (Options::indiDriversAreInternal())
0048         kcfg_indiDriversDir->setEnabled(false);
0049 
0050 #else
0051     kcfg_indiServerIsInternal->setVisible(false);
0052     kcfg_indiDriversAreInternal->setVisible(false);
0053 #endif
0054 
0055     connect(selectFITSDirB, SIGNAL(clicked()), this, SLOT(saveFITSDirectory()));
0056     connect(selectDriversDirB, SIGNAL(clicked()), this, SLOT(saveDriversDirectory()));
0057     connect(showLogsB, SIGNAL(clicked()), this, SLOT(slotShowLogFiles()));
0058     connect(kcfg_indiServer, SIGNAL(editingFinished()), this, SLOT(verifyINDIServer()));
0059     connect(selectINDIHubB, &QPushButton::clicked, this, &OpsINDI::saveINDIHubAgent);
0060 
0061 #ifdef Q_OS_WIN
0062     kcfg_indiServer->setEnabled(false);
0063 #endif
0064 }
0065 
0066 void OpsINDI::toggleINDIInternal()
0067 {
0068     kcfg_indiServer->setEnabled(!kcfg_indiServerIsInternal->isChecked());
0069     if (kcfg_indiServerIsInternal->isChecked())
0070         kcfg_indiServer->setText("*Internal INDI Server*");
0071     else
0072         kcfg_indiServer->setText(KSUtils::getDefaultPath("indiServer"));
0073 }
0074 
0075 void OpsINDI::toggleDriversInternal()
0076 {
0077     kcfg_indiDriversDir->setEnabled(!kcfg_indiDriversAreInternal->isChecked());
0078     if (kcfg_indiDriversAreInternal->isChecked())
0079         kcfg_indiDriversDir->setText("*Internal INDI Drivers*");
0080     else
0081         kcfg_indiDriversDir->setText(KSUtils::getDefaultPath("indiDriversDir"));
0082 
0083     KSNotification::info(i18n("You need to restart KStars for this change to take effect."));
0084 }
0085 
0086 void OpsINDI::saveFITSDirectory()
0087 {
0088     QString dir =
0089         QFileDialog::getExistingDirectory(KStars::Instance(), i18nc("@title:window", "FITS Default Directory"), kcfg_fitsDir->text());
0090 
0091     if (!dir.isEmpty())
0092         kcfg_fitsDir->setText(dir);
0093 }
0094 
0095 void OpsINDI::saveDriversDirectory()
0096 {
0097     QString dir = QFileDialog::getExistingDirectory(KStars::Instance(), i18nc("@title:window", "INDI Drivers Directory"),
0098                   kcfg_indiDriversDir->text());
0099 
0100     if (!dir.isEmpty())
0101     {
0102         kcfg_indiDriversDir->setText(dir);
0103         KSNotification::info(i18n("You need to restart KStars for this change to take effect."));
0104     }
0105 }
0106 
0107 void OpsINDI::saveINDIHubAgent()
0108 {
0109     QUrl url = QFileDialog::getOpenFileUrl(this, i18nc("@title:window", "Select INDIHub Agent"), QUrl(),  "indihub-agent");
0110     if (url.isEmpty())
0111         return;
0112 
0113     kcfg_INDIHubAgent->setText(url.toLocalFile());
0114 }
0115 
0116 void OpsINDI::slotShowLogFiles()
0117 {
0118     QUrl path = QUrl::fromLocalFile(QDir::homePath() + "/.indi/logs");
0119 
0120     QDesktopServices::openUrl(path);
0121 }
0122 
0123 void OpsINDI::verifyINDIServer()
0124 {
0125     // Do not verify internal
0126     if (kcfg_indiServerIsInternal->isChecked())
0127         return;
0128 
0129     QFileInfo indiserver(kcfg_indiServer->text());
0130 
0131     if (indiserver.exists() && indiserver.isFile() && indiserver.baseName() == "indiserver")
0132         return;
0133 
0134     KSNotification::error(i18n("%1 is not a valid INDI server binary.", kcfg_indiServer->text()));
0135 }