Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
PIM
Itinerary
Commits
94842886
Commit
94842886
authored
Dec 25, 2018
by
Volker Krause
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Persist live data information
parent
514138ac
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
0 deletions
+54
-0
src/app/livedatamanager.cpp
src/app/livedatamanager.cpp
+50
-0
src/app/livedatamanager.h
src/app/livedatamanager.h
+4
-0
No files found.
src/app/livedatamanager.cpp
View file @
94842886
...
...
@@ -33,6 +33,12 @@
#include <KPublicTransport/Manager>
#include <QDateTime>
#include <QDir>
#include <QDirIterator>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QStandardPaths>
#include <QVector>
using
namespace
KItinerary
;
...
...
@@ -65,6 +71,8 @@ void LiveDataManager::setReservationManager(ReservationManager *resMgr)
std
::
sort
(
m_reservations
.
begin
(),
m_reservations
.
end
(),
[
this
](
const
auto
&
lhs
,
const
auto
&
rhs
)
{
return
SortUtil
::
isBefore
(
m_resMgr
->
reservation
(
lhs
),
m_resMgr
->
reservation
(
rhs
));
});
loadPublicTransportData
();
}
void
LiveDataManager
::
setPkPassManager
(
PkPassManager
*
pkPassMgr
)
...
...
@@ -164,6 +172,7 @@ void LiveDataManager::checkTrainTrip(const TrainTrip& trip, const QString& resId
}
qCDebug
(
Log
)
<<
"Found departure information:"
<<
dep
.
route
().
line
().
name
()
<<
dep
.
expectedPlatform
()
<<
dep
.
expectedDepartureTime
();
m_departures
.
insert
(
resId
,
dep
);
storePublicTransportData
(
resId
,
dep
,
QStringLiteral
(
"departure"
));
emit
departureUpdated
(
resId
);
break
;
}
...
...
@@ -187,6 +196,7 @@ void LiveDataManager::checkTrainTrip(const TrainTrip& trip, const QString& resId
}
qCDebug
(
Log
)
<<
"Found arrival information:"
<<
arr
.
route
().
line
().
name
()
<<
arr
.
expectedPlatform
()
<<
arr
.
expectedDepartureTime
();
m_arrivals
.
insert
(
resId
,
arr
);
storePublicTransportData
(
resId
,
arr
,
QStringLiteral
(
"arrival"
));
emit
arrivalUpdated
(
resId
);
break
;
}
...
...
@@ -194,4 +204,44 @@ void LiveDataManager::checkTrainTrip(const TrainTrip& trip, const QString& resId
}
void
LiveDataManager
::
loadPublicTransportData
(
const
QString
&
prefix
,
QHash
<
QString
,
KPublicTransport
::
Departure
>
&
data
)
const
{
const
auto
basePath
=
QString
(
QStandardPaths
::
writableLocation
(
QStandardPaths
::
CacheLocation
)
+
QLatin1String
(
"/publictransport/"
));
QDirIterator
it
(
basePath
+
prefix
,
QDir
::
Files
|
QDir
::
NoSymLinks
);
while
(
it
.
hasNext
())
{
it
.
next
();
const
auto
resId
=
it
.
fileInfo
().
baseName
();
if
(
std
::
find
(
m_reservations
.
begin
(),
m_reservations
.
end
(),
resId
)
==
m_reservations
.
end
())
{
QDir
(
it
.
path
()).
remove
(
it
.
fileName
());
}
else
{
QFile
f
(
it
.
filePath
());
if
(
!
f
.
open
(
QFile
::
ReadOnly
))
{
qCWarning
(
Log
)
<<
"Failed to load public transport file"
<<
f
.
fileName
()
<<
f
.
errorString
();
continue
;
}
data
.
insert
(
resId
,
KPublicTransport
::
Departure
::
fromJson
(
QJsonDocument
::
fromJson
(
f
.
readAll
()).
object
()));
}
}
}
void
LiveDataManager
::
loadPublicTransportData
()
{
loadPublicTransportData
(
QStringLiteral
(
"arrival"
),
m_arrivals
);
loadPublicTransportData
(
QStringLiteral
(
"departure"
),
m_departures
);
}
void
LiveDataManager
::
storePublicTransportData
(
const
QString
&
resId
,
const
KPublicTransport
::
Departure
&
dep
,
const
QString
&
type
)
const
{
const
auto
basePath
=
QString
(
QStandardPaths
::
writableLocation
(
QStandardPaths
::
CacheLocation
)
+
QLatin1String
(
"/publictransport/"
)
+
type
+
QLatin1Char
(
'/'
));
QDir
().
mkpath
(
basePath
);
QFile
file
(
basePath
+
resId
+
QLatin1String
(
".json"
));
if
(
!
file
.
open
(
QFile
::
WriteOnly
|
QFile
::
Truncate
))
{
qCWarning
(
Log
)
<<
"Failed to open public transport cache file:"
<<
file
.
fileName
()
<<
file
.
errorString
();
return
;
}
file
.
write
(
QJsonDocument
(
KPublicTransport
::
Departure
::
toJson
(
dep
)).
toJson
());
}
#include "moc_livedatamanager.cpp"
src/app/livedatamanager.h
View file @
94842886
...
...
@@ -61,6 +61,10 @@ signals:
private:
void
checkTrainTrip
(
const
KItinerary
::
TrainTrip
&
trip
,
const
QString
&
resId
);
void
storePublicTransportData
(
const
QString
&
resId
,
const
KPublicTransport
::
Departure
&
dep
,
const
QString
&
type
)
const
;
void
loadPublicTransportData
();
void
loadPublicTransportData
(
const
QString
&
prefix
,
QHash
<
QString
,
KPublicTransport
::
Departure
>&
data
)
const
;
ReservationManager
*
m_resMgr
;
PkPassManager
*
m_pkPassMgr
;
std
::
unique_ptr
<
KPublicTransport
::
Manager
>
m_ptMgr
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment