-
Notifications
You must be signed in to change notification settings - Fork 32
/
fahrplan_calendar_manager.cpp
285 lines (251 loc) · 9.58 KB
/
fahrplan_calendar_manager.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/****************************************************************************
**
** 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 "fahrplan_calendar_manager.h"
#include <QSettings>
#ifndef QT_NO_CONCURRENT
#if defined(BUILD_FOR_QT5)
# include <QtConcurrent/QtConcurrentRun>
#else
# include <QtConcurrentRun>
#endif
# include <QFutureWatcher>
#endif
#ifdef BUILD_FOR_BLACKBERRY
# include <bb/pim/calendar/CalendarService>
# include <bb/pim/calendar/CalendarFolder>
# include <bb/pim/account/AccountService>
# include <bb/pim/account/Account>
#elif defined(BUILD_FOR_SAILFISHOS) && defined(BUILD_FOR_OPENREPOS)
# include <extendedcalendar.h>
# include <extendedstorage.h>
# include <kdatetime.h>
# include <ksystemtimezone.h>
#elif !defined(BUILD_FOR_DESKTOP) && !defined(BUILD_FOR_UBUNTU) && !defined(BUILD_FOR_SAILFISHOS)
# include <QOrganizerManager>
#endif
FahrplanCalendarManager::FahrplanCalendarManager(QObject *parent)
: QAbstractListModel(parent)
, m_selectedIndex(0)
{
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
setRoleNames(roleNames());
#endif
settings = new QSettings(FAHRPLAN_SETTINGS_NAMESPACE, "fahrplan2", this);
#ifndef QT_NO_CONCURRENT
m_watcher = new QFutureWatcher<void>(this);
connect(m_watcher, SIGNAL(finished()), SLOT(getCalendarsListFinished()));
connect(m_watcher, SIGNAL(started()), SIGNAL(selectedCalendarNameChanged()));
#endif
// Change of slectedIndex always changes selectedCalendarName
connect(this, SIGNAL(selectedIndexChanged()), SIGNAL(selectedCalendarNameChanged()));
}
QHash<int, QByteArray> FahrplanCalendarManager::roleNames() const
{
QHash<int, QByteArray> roles;
roles.insert(Qt::UserRole, "name");
return roles;
}
int FahrplanCalendarManager::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return m_calendars.count();
}
QVariant FahrplanCalendarManager::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || (index.row() >= m_calendars.count()))
return QVariant();
switch (role) {
case Qt::UserRole:
return m_calendars.at(index.row()).name;
break;
}
return QVariant();
}
int FahrplanCalendarManager::count() const
{
return rowCount();
}
int FahrplanCalendarManager::selectedIndex() const
{
return m_selectedIndex;
}
void FahrplanCalendarManager::setSelectedIndex(int index)
{
if ((index == m_selectedIndex) || (index >= m_calendars.count()))
return;
m_selectedIndex = index;
emit selectedIndexChanged();
settings->beginGroup("Calendar");
if (index > 0) {
#ifdef BUILD_FOR_BLACKBERRY
settings->setValue("AccountId", m_calendars.at(index).accountId);
settings->setValue("FolderId", m_calendars.at(index).folderId);
#elif defined(BUILD_FOR_SAILFISHOS) && defined(BUILD_FOR_OPENREPOS)
settings->setValue("notebookUID", m_calendars.at(index).notebookUID);
#else
settings->setValue("CollectionId", m_calendars.at(index).collectionId);
#endif
} else {
#ifdef BUILD_FOR_BLACKBERRY
settings->remove("AccountId");
settings->remove("FolderId");
#elif defined(BUILD_FOR_SAILFISHOS) && defined(BUILD_FOR_OPENREPOS)
settings->remove("notebookUID");
#else
settings->remove("CollectionId");
#endif
}
settings->endGroup();
}
QString FahrplanCalendarManager::selectedCalendarName() const
{
#ifndef QT_NO_CONCURRENT
if (m_watcher->isRunning())
return tr("<loading calendars list...>");
#endif
if ((m_selectedIndex < 0) || (m_selectedIndex >= m_calendars.count()))
return tr("<invalid calendar>");
else if (m_selectedIndex == 0)
return tr("Default Calendar");
return m_calendars.at(m_selectedIndex).name;
}
void FahrplanCalendarManager::reload()
{
#ifndef QT_NO_CONCURRENT
if (m_watcher->isRunning())
m_watcher->waitForFinished();
#endif
beginResetModel();
m_calendars.clear();
m_calendars << CalendarInfo(tr("Default Calendar"));
m_selectedIndex = 0;
#ifndef QT_NO_CONCURRENT
// Run fetch in a separate thread.
QFuture<void> future = QtConcurrent::run(this, &FahrplanCalendarManager::getCalendarsList);
m_watcher->setFuture(future);
#else
getCalendarsList();
getCalendarsListFinished();
#endif
// endResetModel() is called in getCalendarsListFinished() function.
}
void FahrplanCalendarManager::getCalendarsList()
{
settings->beginGroup("Calendar");
#ifdef BUILD_FOR_BLACKBERRY
int accountId = settings->value("AccountId", -1).toInt();
int folderId = settings->value("FolderId", -1).toInt();
bb::pim::calendar::CalendarService service;
bb::pim::account::AccountService accservice;
QList<bb::pim::calendar::CalendarFolder> folders = service.folders();
foreach (const bb::pim::calendar::CalendarFolder &folder, folders) {
if (!folder.isVisible() || folder.isReadOnly())
continue;
QString account;
if (folder.accountId() == 1)
account = tr("Local Calendar");
else
account = accservice.account(folder.accountId()).displayName();
//: Calendar name (Account name)
m_calendars << CalendarInfo(tr("%1 (%2)", "Calendar name (Account name)")
.arg(folder.name()).arg(account)
, folder.accountId(), folder.id());
if ((folder.id() == folderId) && (folder.accountId() == accountId)) {
m_selectedIndex = m_calendars.count() - 1;
emit selectedIndexChanged();
}
}
#elif defined(BUILD_FOR_SAILFISHOS) && defined(BUILD_FOR_OPENREPOS)
QString uid = settings->value("notebookUID").toString();
mKCal::ExtendedCalendar::Ptr calendar = mKCal::ExtendedCalendar::Ptr ( new mKCal::ExtendedCalendar( QLatin1String( "UTC" ) ) );
mKCal::ExtendedStorage::Ptr storage = mKCal::ExtendedCalendar::defaultStorage( calendar );
if (storage->open()) {
mKCal::Notebook::List notebooks = storage->notebooks();
qDebug()<<notebooks.count();
for (int ii = 0; ii < notebooks.count(); ++ii) {
if (!notebooks.at(ii)->isReadOnly()) {
m_calendars << CalendarInfo(normalizeCalendarName(notebooks.at(ii)->name()), notebooks.at(ii)->uid());
if (notebooks.at(ii)->uid() == uid) {
m_selectedIndex = m_calendars.count() - 1;
emit selectedIndexChanged();
}
}
}
}
#elif !defined(BUILD_FOR_DESKTOP) && !defined(BUILD_FOR_UBUNTU) && !defined(BUILD_FOR_SAILFISHOS)
QString id = settings->value("CollectionId").toString();
QOrganizerCollectionId collectionId = QOrganizerCollectionId::fromString(id);
QOrganizerManager manager;
QList<QOrganizerCollection> collections = manager.collections();
foreach (const QOrganizerCollection &collection, collections) {
// TODO: Find out ??? where possible.
// Hidden/disabled calendar:
// - IsVisible on Harmattan;
// - Enabled on Symbian;
// - ??? on Ubuntu Touch.
// Read only calendar:
// - IsReadOnly on Harmattan;
// - ??? on Symbian;
// - ??? on Ubuntu Touch.
// Calendar doesn't store events (stores only notes, todos, etc.):
// - EventsAllowed on Harmattan;
// - ??? on Symbian;
// - ??? on Ubuntu Touch.
// ??? temporary calendar ???:
// - IsRunTimeOnly on Harmattan;
// - ??? on Symbian;
// - ??? on Ubuntu Touch.
if (
#ifdef BUILD_FOR_HARMATTAN
collection.metaData("IsReadOnly").toBool()
|| !collection.metaData("IsVisible").toBool()
|| !collection.metaData("EventsAllowed").toBool()
|| collection.metaData("IsRunTimeOnly").toBool()
#elif defined(BUILD_FOR_SYMBIAN)
!collection.metaData("Enabled").toBool()
#else
// Show all calendars by default on
// unknown/unsupported platforms.
false
#endif
)
continue;
m_calendars << CalendarInfo(normalizeCalendarName(collection.metaData(QOrganizerCollection::KeyName).toString()), collection.id().toString());
if (collection.id() == collectionId) {
m_selectedIndex = m_calendars.count() - 1;
emit selectedIndexChanged();
}
}
#endif
settings->endGroup();
}
QString FahrplanCalendarManager::normalizeCalendarName(QString name)
{
if (name == "qtn_caln_personal_caln") {
return tr("Personal");
}
return name;
}
void FahrplanCalendarManager::getCalendarsListFinished()
{
endResetModel();
emit countChanged();
emit selectedIndexChanged();
}