From d8d84714ff6cc7984703506ed94e6f8e90bcf89f Mon Sep 17 00:00:00 2001 From: Mark Washeim Date: Tue, 12 Apr 2022 14:47:16 +0200 Subject: [PATCH] Added a parallel calendar class for sfos only which does not run in a thread. This permits us to just use the the DesktopService. --- data/sailfishos/harbour-fahrplan2.desktop | 2 +- .../openrepos/harbour-fahrplan2.desktop | 2 +- fahrplan2.pro | 2 + src/calendar_sfos_wrapper.cpp | 146 ++++++++++++++++++ src/calendar_sfos_wrapper.h | 40 +++++ src/calendarthreadwrapper.cpp | 2 +- src/fahrplan.cpp | 12 +- 7 files changed, 202 insertions(+), 4 deletions(-) create mode 100644 src/calendar_sfos_wrapper.cpp create mode 100644 src/calendar_sfos_wrapper.h diff --git a/data/sailfishos/harbour-fahrplan2.desktop b/data/sailfishos/harbour-fahrplan2.desktop index 04affe3..ebb2c88 100644 --- a/data/sailfishos/harbour-fahrplan2.desktop +++ b/data/sailfishos/harbour-fahrplan2.desktop @@ -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 diff --git a/data/sailfishos/openrepos/harbour-fahrplan2.desktop b/data/sailfishos/openrepos/harbour-fahrplan2.desktop index 04affe3..ebb2c88 100644 --- a/data/sailfishos/openrepos/harbour-fahrplan2.desktop +++ b/data/sailfishos/openrepos/harbour-fahrplan2.desktop @@ -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 diff --git a/fahrplan2.pro b/fahrplan2.pro index bd3c08f..39491ba 100644 --- a/fahrplan2.pro +++ b/fahrplan2.pro @@ -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 \ @@ -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 \ diff --git a/src/calendar_sfos_wrapper.cpp b/src/calendar_sfos_wrapper.cpp new file mode 100644 index 0000000..24d422f --- /dev/null +++ b/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 . +** +****************************************************************************/ + +#include "calendar_sfos_wrapper.h" + +#include +#include +#include +# include +# include +# include +# include +# include +# include +# include +# include +# include + +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); + } + } +} diff --git a/src/calendar_sfos_wrapper.h b/src/calendar_sfos_wrapper.h new file mode 100644 index 0000000..027fcd9 --- /dev/null +++ b/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 . +** +****************************************************************************/ + +#ifndef CALENDARSFOSWRAPPER_H +#define CALENDARSFOSWRAPPER_H + +#include +#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 diff --git a/src/calendarthreadwrapper.cpp b/src/calendarthreadwrapper.cpp index 34ca41b..0576e6f 100644 --- a/src/calendarthreadwrapper.cpp +++ b/src/calendarthreadwrapper.cpp @@ -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 { diff --git a/src/fahrplan.cpp b/src/fahrplan.cpp index d8d854e..cd53c4a 100644 --- a/src/fahrplan.cpp +++ b/src/fahrplan.cpp @@ -26,9 +26,12 @@ #include "models/timetable.h" #include "models/trainrestrictions.h" #include "models/backends.h" - #include +#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; @@ -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); @@ -398,6 +407,7 @@ void Fahrplan::addJourneyDetailResultToCalendar(JourneyDetailResultList *result) wrapper->moveToThread(workerThread); workerThread->start(); +#endif } Station Fahrplan::getStation(StationType type) const