2 * resourceremote.cpp - KAlarm remote alarm calendar resource
4 * Copyright © 2006-2011 by David Jarvie <djarvie@kde.org>
5 * Based on resourceremote.cpp in kresources (updated to rev 721447),
6 * Copyright (c) 2003,2004 Cornelius Schumacher <schumacher@kde.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include "resourceremote.moc"
27 //#include <libkdepim/progressmanager.h>
28 #include <kcal/calendarlocal.h>
31 #include <kio/jobuidelegate.h>
32 #include <kio/jobclasses.h>
34 #include <kstandarddirs.h>
39 KAResourceRemote::KAResourceRemote()
45 mRemoteReadOnly(false)
50 KAResourceRemote::KAResourceRemote(const KConfigGroup
& group
)
51 : AlarmResource(group
),
56 mRemoteReadOnly(false)
58 mDownloadUrl
= KUrl(group
.readEntry("DownloadUrl"));
59 mUploadUrl
= KUrl(group
.readEntry("UploadUrl"));
60 ResourceCached::readConfig(group
);
64 KAResourceRemote::KAResourceRemote(CalEvent::Type type
, const KUrl
& downloadUrl
, const KUrl
& uploadUrl
)
65 : AlarmResource(type
),
66 mDownloadUrl(downloadUrl
),
67 mUploadUrl(uploadUrl
.isEmpty() ? mDownloadUrl
: uploadUrl
),
72 mRemoteReadOnly(false)
77 void KAResourceRemote::init()
79 setType("remote"); // set resource type
83 KAResourceRemote::~KAResourceRemote()
89 void KAResourceRemote::doClose()
97 AlarmResource::doClose();
100 void KAResourceRemote::writeConfig(KConfigGroup
& group
)
102 group
.writeEntry("DownloadUrl", mDownloadUrl
.url());
103 group
.writeEntry("UploadUrl", mUploadUrl
.url());
104 AlarmResource::writeConfig(group
);
107 void KAResourceRemote::startReconfig()
109 mNewDownloadUrl
= mDownloadUrl
;
110 mNewUploadUrl
= mUploadUrl
;
111 AlarmResource::startReconfig();
114 void KAResourceRemote::applyReconfig()
118 AlarmResource::applyReconfig();
119 if (setUrls(mNewDownloadUrl
, mNewUploadUrl
))
120 mReconfiguring
= 3; // indicate that location has changed
121 AlarmResource::applyReconfig();
125 bool KAResourceRemote::readOnly() const
127 return mRemoteReadOnly
|| AlarmResource::readOnly();
130 void KAResourceRemote::enableResource(bool enable
)
133 cancelDownload(false);
136 bool KAResourceRemote::doLoad(bool syncCache
)
139 syncCache
= false; // still uploading, so the cache is up-to-date
142 kWarning(KARES_DEBUG
) << "Download still in progress";
146 emit
invalidate(this);
148 setWrongAlarmType(false, false);
152 updateCustomEvents(false); // calendar is now empty
157 if (mUseCacheFile
|| !syncCache
)
159 disableChangeNotification();
160 syncCache
= !loadFromCache(); // if cache file doesn't exist yet, we need to download
161 mUseCacheFile
= false;
162 enableChangeNotification();
164 emit
resourceChanged(this);
168 kDebug(KARES_DEBUG
) << mDownloadUrl
.prettyUrl() << ": from cache";
169 slotLoadJobResult(0);
171 else if (!lock()->lock())
173 kDebug(KARES_DEBUG
) << mDownloadUrl
.prettyUrl() << ": cache file is locked - something else must be loading the file";
174 updateCustomEvents();
178 kDebug(KARES_DEBUG
) << mDownloadUrl
.prettyUrl() << ": downloading...";
179 mDownloadJob
= KIO::file_copy(mDownloadUrl
, KUrl(cacheFile()), -1, KIO::Overwrite
|
180 ((mShowProgress
&& hasGui()) ? KIO::DefaultFlags
: KIO::HideProgressInfo
));
181 connect(mDownloadJob
, SIGNAL(result(KJob
*)), SLOT(slotLoadJobResult(KJob
*)));
183 if (mShowProgress
&& hasGui())
185 connect(mDownloadJob
, SIGNAL(percent(KJob
*,ulong
)),
186 SLOT(slotPercent(KJob
*,ulong
)));
187 emit
downloading(this, 0);
194 void KAResourceRemote::slotPercent(KJob
*, unsigned long percent
)
198 emit
downloading(this, percent
);
202 void KAResourceRemote::slotLoadJobResult(KJob
* job
)
207 emit
invalidate(this);
209 setWrongAlarmType(false, false);
215 KIO::FileCopyJob
* j
= qobject_cast
<KIO::FileCopyJob
*>(job
);
217 j
->ui()->showErrorMessage();
219 kError(KARES_DEBUG
) << "Resource" << identifier() << " download error:" << job
->errorString();
225 kDebug(KARES_DEBUG
) << mDownloadUrl
.prettyUrl() << ": success";
226 setReloaded(true); // the resource has now been downloaded at least once
227 emit
cacheDownloaded(this);
228 disableChangeNotification();
230 enableChangeNotification();
234 emit
downloading(this, (unsigned long)-1);
241 checkCompatibility(cacheFile());
246 updateCustomEvents();
249 emit
resourceChanged(this);
252 void KAResourceRemote::cancelDownload(bool disable
)
256 mDownloadJob
->kill();
261 emit
downloading(this, (unsigned long)-1);
265 updateCustomEvents();
270 bool KAResourceRemote::doSave(bool syncCache
)
272 kDebug(KARES_DEBUG
) << mUploadUrl
.prettyUrl();
273 if (readOnly() || !hasChanges())
277 kWarning(KARES_DEBUG
) << "Download still in progress";
282 kWarning(KARES_DEBUG
) << "Upload still in progress";
286 mChangedIncidences
= allChanges();
288 (*mCalIDFunction
)(*calendar()); // write the application ID into the calendar
292 mUploadJob
= KIO::file_copy(KUrl(cacheFile()), mUploadUrl
, -1, KIO::Overwrite
| (hasGui()?KIO::DefaultFlags
:KIO::HideProgressInfo
));
293 connect(mUploadJob
, SIGNAL(result(KJob
*)), SLOT(slotSaveJobResult(KJob
*)));
298 void KAResourceRemote::slotSaveJobResult(KJob
* job
)
304 KIO::FileCopyJob
* j
= qobject_cast
<KIO::FileCopyJob
*>(job
);
306 j
->ui()->showErrorMessage();
308 kError(KARES_DEBUG
) << "Resource" << identifier() << " upload error:" << job
->errorString();
312 kDebug(KARES_DEBUG
) << mUploadUrl
.prettyUrl() << ": success";
317 emit
resourceSaved(this);
318 if (closeAfterSave())
322 bool KAResourceRemote::setUrls(const KUrl
& downloadUrl
, const KUrl
& uploadUrl
)
324 if (mReconfiguring
== 1)
326 mNewDownloadUrl
= downloadUrl
;
327 mNewUploadUrl
= uploadUrl
;
330 if (downloadUrl
.equals(mDownloadUrl
)
331 && uploadUrl
.equals(mUploadUrl
))
333 kDebug(KARES_DEBUG
) << downloadUrl
.prettyUrl() << "," << uploadUrl
.prettyUrl();
336 bool active
= isActive();
338 enableResource(false);
339 mDownloadUrl
= downloadUrl
;
340 mUploadUrl
= uploadUrl
;
342 enableResource(true);
343 // Trigger loading the new resource, and ensure that the new configuration is saved
344 emit
locationChanged(this);
348 bool KAResourceRemote::setLocation(const QString
& downloadUrl
, const QString
& uploadUrl
)
350 return setUrls(KUrl(downloadUrl
), KUrl(uploadUrl
));
353 QStringList
KAResourceRemote::location() const
355 return QStringList(downloadUrl().url()) << uploadUrl().url();
358 QString
KAResourceRemote::displayLocation() const
360 return mDownloadUrl
.prettyUrl();
363 QString
KAResourceRemote::displayType() const
365 return i18nc("@info/plain", "URL");