1 /*
2     SPDX-FileCopyrightText: 2011 Jerome SONRIER <jsid@emor3j.fr.eu.org>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "satellitegroup.h"
8 
9 #include "ksutils.h"
10 #include "kspaths.h"
11 #include "skyobjects/satellite.h"
12 
13 #include <QTextStream>
14 
SatelliteGroup(const QString & name,const QString & tle_filename,const QUrl & update_url)15 SatelliteGroup::SatelliteGroup(const QString& name, const QString& tle_filename, const QUrl& update_url)
16 {
17     m_name     = name;
18     m_tle_file = tle_filename;
19     m_tle_url  = update_url;
20 
21     // Read TLE file and create satellites
22     readTLE();
23 }
24 
readTLE()25 void SatelliteGroup::readTLE()
26 {
27     QFile file;
28     QString line1, line2;
29 
30     // Delete all satellites
31     qDeleteAll(*this);
32     clear();
33 
34     // Read TLE file
35     if (KSUtils::openDataFile(file, m_tle_file))
36     {
37         QTextStream stream(&file);
38         while (!stream.atEnd())
39         {
40             // Read satellite name
41             QString sat_name = stream.readLine().trimmed();
42             line1            = stream.readLine();
43             line2            = stream.readLine();
44             // Create a new satellite and add it to the list of satellites
45             if (line1.startsWith('1') && line2.startsWith('2'))
46                 append(new Satellite(sat_name, line1, line2));
47         }
48         file.close();
49     }
50 }
51 
updateSatellitesPos()52 void SatelliteGroup::updateSatellitesPos()
53 {
54     QMutableListIterator<Satellite *> sats(*this);
55 
56     while (sats.hasNext())
57     {
58         Satellite *sat = sats.next();
59 
60         if (sat->selected())
61         {
62             int rc = sat->updatePos();
63             // If position cannot be calculated, remove it from list
64             if (rc != 0)
65                 sats.remove();
66         }
67     }
68 }
69 
tleFilename()70 QUrl SatelliteGroup::tleFilename()
71 {
72     // Return absolute path with "file:" before the path
73     //return QUrl( "file:" + (QDir(KSPaths::writableLocation(QStandardPaths::AppDataLocation)).filePath(m_tle_file));
74     return QUrl::fromLocalFile(QDir(KSPaths::writableLocation(QStandardPaths::AppDataLocation)).filePath(m_tle_file));
75 }
76 
tleUrl()77 QUrl SatelliteGroup::tleUrl()
78 {
79     return m_tle_url;
80 }
81 
name()82 QString SatelliteGroup::name()
83 {
84     return m_name;
85 }
86