Files
GoldenCheetah/src/CalendarDownload.cpp
Mark Liversedge a48f7c00d1 Clean compile time nits
Lots of nitty fixups, largely for uninitialised temporary
variables.

I have left the use of boost::function and boost::bind in the
DownloadRideDialog alone, so it will vomit when compiled
with boost 1.46 and gcc 4.5 or higher. Will look into this
more carefully at a later stage.

I am working up to resolving issues identified from -pedantic next.
2011-05-12 22:12:36 +01:00

116 lines
3.3 KiB
C++

/*
* Copyright (c) 2010 Mark Liversedge (liversedge@gmail.com)
*
* 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, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "CalendarDownload.h"
#ifdef GC_HAVE_ICAL
#include "ICalendar.h"
#include <libical/ical.h>
#endif
CalendarDownload::CalendarDownload(MainWindow *main) : main(main)
{
nam = new QNetworkAccessManager(this);
connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadFinished(QNetworkReply*)));
}
bool
CalendarDownload::download()
{
QString request = appsettings->cvalue(main->cyclist, GC_WEBCAL_URL, "").toString();
if (request == "") return false;
else {
// change webcal to http, since it is basically the same port
QRegExp webcal("^webcal:");
request.replace(webcal, QString("http:"));
}
QNetworkReply *reply = nam->get(QNetworkRequest(QUrl(request)));
if (reply->error() != QNetworkReply::NoError) {
QMessageBox::warning(main, tr("Calendar Data Download"), reply->errorString());
return false;
}
return true;
}
#if 0
static QString propertyToString(icalproperty *p)
{
if (p) {
icalvalue *v = icalproperty_get_value(p);
QString converted(icalvalue_as_ical_string(v));
// some special characters are escaped in the text
converted.replace("\\n", "\n");
converted.replace("\\;", ";");
return converted;
} else {
return QString("");
}
}
static QDate propertyToDate(icalproperty *p)
{
if (p) {
icalvalue *v = icalproperty_get_value(p);
struct icaltimetype date = icalvalue_get_datetime(v);
QDate when(date.year, date.month, date.day);
return when;
} else {
return QDate();
}
}
#endif
void
CalendarDownload::downloadFinished(QNetworkReply *reply)
{
QString fulltext = reply->readAll();
QStringList errors;
#ifdef GC_HAVE_ICAL
QString remoteCache = main->home.absolutePath()+"/remote.ics";
QFile remoteCacheFile(remoteCache);
if (fulltext != "") {
// update remote cache - write to it!
remoteCacheFile.open(QFile::ReadWrite | QFile::Text);
QTextStream out(&remoteCacheFile);
out << fulltext;
remoteCacheFile.close();
} else {
QMessageBox msgBox;
msgBox.setText("Remote Calendar not available, reverting to cached workouts.");
msgBox.setIcon(QMessageBox::Information);
msgBox.exec();
// read cache
// read in the whole thing
remoteCacheFile.open(QFile::ReadOnly | QFile::Text);
QTextStream in(&remoteCacheFile);
fulltext = in.readAll();
remoteCacheFile.close();
}
main->rideCalendar->refreshRemote(fulltext);
#endif
}