File indexing completed on 2024-04-21 14:46:45

0001 /*
0002     SPDX-FileCopyrightText: 2011 Jerome SONRIER <jsid@emor3j.fr.eu.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "satellitegroup.h"
0008 
0009 #include "ksutils.h"
0010 #include "kspaths.h"
0011 #include "skyobjects/satellite.h"
0012 
0013 #include <QTextStream>
0014 
0015 SatelliteGroup::SatelliteGroup(const QString& name, const QString& tle_filename, const QUrl& update_url)
0016 {
0017     m_name     = name;
0018     m_tle_file = tle_filename;
0019     m_tle_url  = update_url;
0020 
0021     // Read TLE file and create satellites
0022     readTLE();
0023 }
0024 
0025 void SatelliteGroup::readTLE()
0026 {
0027     QFile file;
0028     QString line1, line2;
0029 
0030     // Delete all satellites
0031     qDeleteAll(*this);
0032     clear();
0033 
0034     // Read TLE file
0035     if (KSUtils::openDataFile(file, m_tle_file))
0036     {
0037         QTextStream stream(&file);
0038         while (!stream.atEnd())
0039         {
0040             // Read satellite name
0041             QString sat_name = stream.readLine().trimmed();
0042             line1            = stream.readLine();
0043             line2            = stream.readLine();
0044             // Create a new satellite and add it to the list of satellites
0045             if (line1.startsWith('1') && line2.startsWith('2'))
0046                 append(new Satellite(sat_name, line1, line2));
0047         }
0048         file.close();
0049     }
0050 }
0051 
0052 void SatelliteGroup::updateSatellitesPos()
0053 {
0054     QMutableListIterator<Satellite *> sats(*this);
0055 
0056     while (sats.hasNext())
0057     {
0058         Satellite *sat = sats.next();
0059 
0060         if (sat->selected())
0061         {
0062             int rc = sat->updatePos();
0063             // If position cannot be calculated, remove it from list
0064             if (rc != 0)
0065                 sats.remove();
0066         }
0067     }
0068 }
0069 
0070 QUrl SatelliteGroup::tleFilename()
0071 {
0072     // Return absolute path with "file:" before the path
0073     //return QUrl( "file:" + (QDir(KSPaths::writableLocation(QStandardPaths::AppLocalDataLocation)).filePath(m_tle_file));
0074     return QUrl::fromLocalFile(QDir(KSPaths::writableLocation(QStandardPaths::AppLocalDataLocation)).filePath(m_tle_file));
0075 }
0076 
0077 QUrl SatelliteGroup::tleUrl()
0078 {
0079     return m_tle_url;
0080 }
0081 
0082 QString SatelliteGroup::name()
0083 {
0084     return m_name;
0085 }