Skip to content

Commit

Permalink
Added a parallel calendar class for sfos only which does not run in a
Browse files Browse the repository at this point in the history
thread. This permits us to just use the the DesktopService.
  • Loading branch information
poetaster committed Apr 12, 2022
1 parent a62027b commit d8d8471
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 4 deletions.
2 changes: 1 addition & 1 deletion data/sailfishos/harbour-fahrplan2.desktop
Expand Up @@ -8,5 +8,5 @@ Exec=harbour-fahrplan2
[X-Sailjail]
OrganizationName=de.smurfy
ApplicationName=harbour-fahrplan2
Permissions=Internet;Location;Calendar;Synchronization;UserDirs
Permissions=Internet;Location;Calendar;Synchronization;UserDirs;Accounts;Email;Privileged

2 changes: 1 addition & 1 deletion data/sailfishos/openrepos/harbour-fahrplan2.desktop
Expand Up @@ -8,5 +8,5 @@ Exec=harbour-fahrplan2
[X-Sailjail]
OrganizationName=de.smurfy
ApplicationName=harbour-fahrplan2
Permissions=Internet;Location;Calendar;Synchronization;UserDirs
Permissions=Internet;Location;Calendar;Synchronization;UserDirs;Accounts;Email;Privileged

2 changes: 2 additions & 0 deletions fahrplan2.pro
Expand Up @@ -101,6 +101,7 @@ INCLUDEPATH += src
unix:!symbian: LIBS += -lz

HEADERS += \
src/calendar_sfos_wrapper.h \
src/parser/parser_hafasxml.h \
src/parser/parser_abstract.h \
src/parser/parser_definitions.h \
Expand Down Expand Up @@ -135,6 +136,7 @@ HEADERS += \
src/models/backends.h

SOURCES += src/main.cpp \
src/calendar_sfos_wrapper.cpp \
src/parser/parser_hafasxml.cpp \
src/parser/parser_abstract.cpp \
src/parser/parser_definitions.cpp \
Expand Down
146 changes: 146 additions & 0 deletions src/calendar_sfos_wrapper.cpp
@@ -0,0 +1,146 @@
/****************************************************************************
**
** This file is a part of Fahrplan.
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License along
** with this program. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/

#include "calendar_sfos_wrapper.h"

#include <QCoreApplication>
#include <QThread>
#include <QSettings>
# include <extendedcalendar.h>
# include <extendedstorage.h>
# include <KCalendarCore/Event>
# include <KCalendarCore/ICalFormat>
# include <QDesktopServices>
# include <QTemporaryFile>
# include <QTextStream>
# include <QDir>
# include <QUrl>

QString formatStations(const QDateTime dateTime, const QString &stationName, const QString &info = QString())
{
QString station;
if (info.isEmpty()) {
station = stationName;
} else {
//: STATION / PLATFORM
station = CalendarSfosWrapper::tr("%1 / %2", "STATION / PLATFORM").arg(stationName, info);
}

//: DATE TIME STATION
return CalendarSfosWrapper::tr("%1 %2 %3", "DATE TIME STATION").arg(
// TODO: Don't force QLocale::ShortFormat for date, but make it configurable.
dateTime.toString(QLocale().dateFormat(QLocale::ShortFormat)),
// Always use short format for time, else you get something like "12:35:00 t".
dateTime.toString(QLocale().timeFormat(QLocale::ShortFormat)),
station);
}

CalendarSfosWrapper::CalendarSfosWrapper(JourneyDetailResultList *result, QObject *parent) :
QObject(parent), m_result(result)
{
}

CalendarSfosWrapper::~CalendarSfosWrapper()
{

}

void CalendarSfosWrapper::addToCalendar()
{

const QString viaStation = m_result->viaStation();
QSettings settings(FAHRPLAN_SETTINGS_NAMESPACE, "fahrplan2");
QString calendarEntryTitle;
QString calendarEntryDesc;

if (viaStation.isEmpty())
calendarEntryTitle = tr("%1 to %2").arg(m_result->departureStation(),
m_result->arrivalStation());
else
calendarEntryTitle = tr("%1 via %3 to %2").arg(m_result->departureStation(),
m_result->arrivalStation(),
viaStation);

if (!m_result->info().isEmpty())
calendarEntryDesc.append(m_result->info()).append("\n");

const bool compactFormat = settings.value("compactCalendarEntries", false).toBool();
for (int i=0; i < m_result->itemcount(); i++) {
JourneyDetailResultItem *item = m_result->getItem(i);

const QString train = item->direction().isEmpty()
? item->train()
: tr("%1 to %2").arg(item->train(), item->direction());

if (!compactFormat && !train.isEmpty())
calendarEntryDesc.append("--- ").append(train).append(" ---\n");

calendarEntryDesc.append(formatStations(item->departureDateTime(),
item->departureStation(),
item->departureInfo()));
calendarEntryDesc.append("\n");

if (compactFormat && !train.isEmpty())
calendarEntryDesc.append("--- ").append(train).append(" ---\n");

calendarEntryDesc.append(formatStations(item->arrivalDateTime(),
item->arrivalStation(),
item->arrivalInfo()));
calendarEntryDesc.append("\n");

if (!compactFormat) {
if (!item->info().isEmpty())
calendarEntryDesc.append(item->info()).append("\n");
calendarEntryDesc.append("\n");
}
}

if (!compactFormat)
calendarEntryDesc.append(
tr("-- \nAdded by Fahrplan. Please, re-check the information before your journey."));

KCalendarCore::Event::Ptr event( new KCalendarCore::Event() );
event->setSummary(calendarEntryTitle);
event->setDescription(calendarEntryDesc);
event->setDtStart( m_result->departureDateTime() );
event->setDtEnd( m_result->arrivalDateTime() );
KCalendarCore::ICalFormat format;
QString icsData = format.toICalString(event);
QTemporaryFile *tmpFile = new QTemporaryFile(
QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) +
QDir::separator() + "event-XXXXXX.ics",
this);
// destructed and file deleted with this object

//qDebug() << "IcalData: " << icsData;
if (tmpFile->open())
{
QTextStream stream( tmpFile );
stream << icsData;
tmpFile->close();
qDebug() << "Opening" << tmpFile->fileName();
if ( !QDesktopServices::openUrl(QUrl::fromLocalFile(tmpFile->fileName())) )
{
qWarning() << "QDesktopServices::openUrl fails!";
emit addCalendarEntryComplete(false);
} else {
emit addCalendarEntryComplete(true);
}
}
}
40 changes: 40 additions & 0 deletions src/calendar_sfos_wrapper.h
@@ -0,0 +1,40 @@
/****************************************************************************
**
** This file is a part of Fahrplan.
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License along
** with this program. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/

#ifndef CALENDARSFOSWRAPPER_H
#define CALENDARSFOSWRAPPER_H

#include <QObject>
#include "parser/parser_definitions.h"

class CalendarSfosWrapper : public QObject
{
Q_OBJECT
public:
explicit CalendarSfosWrapper(JourneyDetailResultList *result, QObject *parent = 0);
virtual ~CalendarSfosWrapper();
public slots:
void addToCalendar();
signals:
void addCalendarEntryComplete(bool success);
private:
JourneyDetailResultList * const m_result;
};

#endif // CALENDARSFOSWRAPPER_H
2 changes: 1 addition & 1 deletion src/calendarthreadwrapper.cpp
Expand Up @@ -193,7 +193,7 @@ void CalendarThreadWrapper::addToCalendar()
tmpFile->close();
qDebug() << "Opening" << tmpFile->fileName();
if ( !QDesktopServices::openUrl(QUrl::fromLocalFile(tmpFile->fileName())) )
{
{
qWarning() << "QDesktopServices::openUrl fails!";
emit addCalendarEntryComplete(false);
} else {
Expand Down
12 changes: 11 additions & 1 deletion src/fahrplan.cpp
Expand Up @@ -26,9 +26,12 @@
#include "models/timetable.h"
#include "models/trainrestrictions.h"
#include "models/backends.h"

#include <QThread>

#ifdef defined(BUILD_FOR_SAILFISHOS) && defined(BUILD_FOR_OPENREPOS)
#include "calendar_sfos_wrapper.h"
#endif

FahrplanBackendManager *Fahrplan::m_parser_manager = NULL;
StationSearchResults *Fahrplan::m_stationSearchResults= NULL;
Favorites *Fahrplan::m_favorites = NULL;
Expand Down Expand Up @@ -387,6 +390,12 @@ void Fahrplan::setParser(int index)

void Fahrplan::addJourneyDetailResultToCalendar(JourneyDetailResultList *result)
{
#ifdef defined(BUILD_FOR_SAILFISHOS) && defined(BUILD_FOR_OPENREPOS)

CalendarSfosWrapper *wrapper = new CalendarSFosWrapper(result);
connect(wrapper, SLOT( addToCalendar() ) );
connect(wrapper,SIGNAL(addCalendarEntryComplete(bool)), SIGNAL(addCalendarEntryComplete(bool)));
#else
QThread *workerThread = new QThread(this);
CalendarThreadWrapper *wrapper = new CalendarThreadWrapper(result);

Expand All @@ -398,6 +407,7 @@ void Fahrplan::addJourneyDetailResultToCalendar(JourneyDetailResultList *result)

wrapper->moveToThread(workerThread);
workerThread->start();
#endif
}

Station Fahrplan::getStation(StationType type) const
Expand Down

0 comments on commit d8d8471

Please sign in to comment.