File indexing completed on 2024-04-28 15:09:10

0001 /*
0002     SPDX-FileCopyrightText: 2017 Jasem Mutlaq <mutlaqja@ikarustech.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "opslogs.h"
0008 
0009 #include "kstars.h"
0010 #include "Options.h"
0011 #include "auxiliary/kspaths.h"
0012 #include "indi/indilistener.h"
0013 
0014 #include <KConfigDialog>
0015 #include <KFormat>
0016 #include <KMessageBox>
0017 
0018 #include <QFrame>
0019 #include <QUrl>
0020 #include <QDesktopServices>
0021 
0022 #include <basedevice.h>
0023 
0024 namespace Ekos
0025 {
0026 OpsLogs::OpsLogs() : QFrame(KStars::Instance())
0027 {
0028     setupUi(this);
0029 
0030     refreshInterface();
0031 
0032     //Get a pointer to the KConfigDialog
0033     KConfigDialog *m_ConfigDialog = KConfigDialog::exists("logssettings");
0034     connect(m_ConfigDialog->button(QDialogButtonBox::Apply), SIGNAL(clicked()), SLOT(refreshInterface()));
0035     connect(m_ConfigDialog->button(QDialogButtonBox::Ok), SIGNAL(clicked()), SLOT(refreshInterface()));
0036 
0037     connect(clearLogsB, SIGNAL(clicked()), this, SLOT(slotClearLogs()));
0038     connect(kcfg_VerboseLogging, SIGNAL(toggled(bool)), this, SLOT(slotToggleVerbosityOptions()));
0039 
0040     connect(kcfg_LogToFile, SIGNAL(toggled(bool)), this, SLOT(slotToggleOutputOptions()));
0041 
0042     connect(showLogsB, &QPushButton::clicked, []()
0043     {
0044         QDesktopServices::openUrl(QUrl::fromLocalFile(QDir(KSPaths::writableLocation(
0045                                       QStandardPaths::AppLocalDataLocation)).filePath("logs")));
0046     });
0047 
0048     for (auto &b : modulesGroup->buttons())
0049         b->setEnabled(kcfg_VerboseLogging->isChecked());
0050     for (auto &b : driversGroup->buttons())
0051         b->setEnabled(kcfg_VerboseLogging->isChecked());
0052 
0053     qint64 totalSize = getDirSize(QDir(KSPaths::writableLocation(QStandardPaths::AppLocalDataLocation)).filePath("logs"));
0054     totalSize += getDirSize(QDir(KSPaths::writableLocation(QStandardPaths::AppLocalDataLocation)).filePath("autofocus"));
0055 
0056     clearLogsB->setToolTip(i18n("Clear all logs (%1)", KFormat().formatByteSize(totalSize)));
0057 
0058 }
0059 
0060 void OpsLogs::slotToggleVerbosityOptions()
0061 {
0062     if (kcfg_DisableLogging->isChecked())
0063         KSUtils::Logging::Disable();
0064 
0065     foreach (QAbstractButton *b, modulesGroup->buttons())
0066     {
0067         b->setEnabled(kcfg_VerboseLogging->isChecked());
0068         // If verbose is not checked, CLEAR all selections
0069         b->setChecked(kcfg_VerboseLogging->isChecked() ? b->isChecked() : false);
0070     }
0071 
0072     foreach (QAbstractButton *b, driversGroup->buttons())
0073     {
0074         b->setEnabled(kcfg_VerboseLogging->isChecked());
0075         // If verbose is not checked, CLEAR all selections
0076         b->setChecked(kcfg_VerboseLogging->isChecked() ? b->isChecked() : false);
0077     }
0078 }
0079 
0080 void OpsLogs::slotToggleOutputOptions()
0081 {
0082     if (kcfg_LogToDefault->isChecked())
0083     {
0084         if (kcfg_DisableLogging->isChecked() == false)
0085             KSUtils::Logging::UseDefault();
0086     }
0087     else
0088         KSUtils::Logging::UseFile();
0089 }
0090 
0091 void OpsLogs::refreshInterface()
0092 {
0093     uint16_t previousInterface = m_INDIDebugInterface;
0094 
0095     m_INDIDebugInterface = 0;
0096 
0097     if (Options::iNDIMountLogging())
0098         m_INDIDebugInterface |= INDI::BaseDevice::TELESCOPE_INTERFACE;
0099     if (Options::iNDICCDLogging())
0100         m_INDIDebugInterface |= INDI::BaseDevice::CCD_INTERFACE;
0101     if (Options::iNDIFocuserLogging())
0102         m_INDIDebugInterface |= INDI::BaseDevice::FOCUSER_INTERFACE;
0103     if (Options::iNDIFilterWheelLogging())
0104         m_INDIDebugInterface |= INDI::BaseDevice::FILTER_INTERFACE;
0105     if (Options::iNDIDomeLogging())
0106         m_INDIDebugInterface |= INDI::BaseDevice::DOME_INTERFACE;
0107     if (Options::iNDIWeatherLogging())
0108         m_INDIDebugInterface |= INDI::BaseDevice::WEATHER_INTERFACE;
0109     if (Options::iNDIDetectorLogging())
0110         m_INDIDebugInterface |= INDI::BaseDevice::DETECTOR_INTERFACE;
0111     if (Options::iNDIRotatorLogging())
0112         m_INDIDebugInterface |= INDI::BaseDevice::ROTATOR_INTERFACE;
0113     if (Options::iNDIGPSLogging())
0114         m_INDIDebugInterface |= INDI::BaseDevice::GPS_INTERFACE;
0115     if (Options::iNDIAOLogging())
0116         m_INDIDebugInterface |= INDI::BaseDevice::AO_INTERFACE;
0117     if (Options::iNDIAuxiliaryLogging())
0118         m_INDIDebugInterface |= INDI::BaseDevice::AUX_INTERFACE;
0119 
0120     Options::setINDILogging((m_INDIDebugInterface > 0));
0121 
0122     m_SettingsChanged = (previousInterface != m_INDIDebugInterface);
0123 }
0124 
0125 // Following 2 functions from Stackoverflow #47854288
0126 qint64 OpsLogs::getDirSize(const QString &dirPath)
0127 {
0128     qint64 size = 0;
0129     QDir dir(dirPath);
0130 
0131     QDir::Filters fileFilters = QDir::Files | QDir::System | QDir::Hidden;
0132 
0133     for (QString &filePath : dir.entryList(fileFilters))
0134     {
0135         QFileInfo fi(dir, filePath);
0136 
0137         size += fi.size();
0138     }
0139 
0140     QDir::Filters dirFilters = QDir::Dirs | QDir::NoDotAndDotDot | QDir::System | QDir::Hidden;
0141 
0142     for (QString &childDirPath : dir.entryList(dirFilters))
0143         size += getDirSize(dirPath + QDir::separator() + childDirPath);
0144 
0145     return size;
0146 }
0147 
0148 void OpsLogs::slotClearLogs()
0149 {
0150     if (KMessageBox::questionYesNo(nullptr, i18n("Are you sure you want to delete all logs?")) == KMessageBox::Yes)
0151     {
0152         QDir logDir(QDir(KSPaths::writableLocation(QStandardPaths::AppLocalDataLocation)).filePath("logs"));
0153         logDir.removeRecursively();
0154         logDir.mkpath(".");
0155 
0156         QDir autoFocusDir(QDir(KSPaths::writableLocation(QStandardPaths::AppLocalDataLocation)).filePath("autofocus"));
0157         autoFocusDir.removeRecursively();
0158         autoFocusDir.mkpath(".");
0159 
0160         clearLogsB->setToolTip(i18n("Clear all logs"));
0161     }
0162 }
0163 
0164 }