summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkarsh Simha <akarsh@kde.org>2016-10-11 09:19:52 (GMT)
committerAkarsh Simha <akarsh@kde.org>2016-10-11 12:19:24 (GMT)
commit30862b948a5afce39a3e3d85bd6964220993444f (patch)
tree7297a210be0c15ba1ffba0baebc511ef85597f39
parent24f6b350d6a6f257718c64c37df590c30994e112 (diff)
Avoid truncating the wishlist if there's a crash while saving it.
There is a bug which induces a crash when writing the observing wish list, and ends up truncating the wish list. This can be very frustrating because of the loss of the wishlist. Although the bug still needs to be found and fixed, this change in code at least prevents the wishlist from being truncated upon crash.
-rw-r--r--kstars/tools/observinglist.cpp21
1 files changed, 13 insertions, 8 deletions
diff --git a/kstars/tools/observinglist.cpp b/kstars/tools/observinglist.cpp
index b9e9613..8001a7e 100644
--- a/kstars/tools/observinglist.cpp
+++ b/kstars/tools/observinglist.cpp
@@ -888,13 +888,10 @@ void ObservingList::slotSaveList() {
QFile f;
// FIXME: Move wishlist into a database.
// TODO: Support multiple wishlists.
- f.setFileName( KSPaths::writableLocation(QStandardPaths::GenericDataLocation) + "wishlist.obslist" ) ;
- if ( ! f.open( QIODevice::WriteOnly ) ) {
- qDebug() << "Cannot write list to file"; // TODO: This should be presented as a message box to the user
- return;
- }
- QTextStream ostream( &f );
- foreach ( SkyObject* o, obsList() ) {
+
+ QString fileContents;
+ QTextStream ostream( &fileContents ); // We first write to a QString to prevent truncating the file in case there is a crash.
+ foreach ( const SkyObject* o, obsList() ) {
if ( !o ) {
qWarning() << "Null entry in observing wishlist! Skipping!";
continue;
@@ -903,7 +900,8 @@ void ObservingList::slotSaveList() {
//ostream << o->name() << " " << o->ra0().Hours() << " " << o->dec0().Degrees() << endl;
ostream << getObjectName(o, false) << endl;
} else if ( o->type() == SkyObject::STAR ) {
- StarObject *s = (StarObject*)o;
+ Q_ASSERT( dynamic_cast<const StarObject *>( o ) );
+ const StarObject *s = static_cast<const StarObject *>( o );
if ( s->name() == s->gname() )
ostream << s->name2() << endl;
else
@@ -912,6 +910,13 @@ void ObservingList::slotSaveList() {
ostream << o->name() << endl;
}
}
+ f.setFileName( KSPaths::writableLocation(QStandardPaths::GenericDataLocation) + "wishlist.obslist" ) ;
+ if ( ! f.open( QIODevice::WriteOnly ) ) {
+ qWarning() << "Cannot save wish list to file!"; // TODO: This should be presented as a message box to the user
+ return;
+ }
+ QTextStream writeemall( &f );
+ writeemall << fileContents;
f.close();
}