-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
calendar_sfos_wrapper.cpp
146 lines (125 loc) · 5.65 KB
/
calendar_sfos_wrapper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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);
}
}
}