~ubuntu-branches/ubuntu/saucy/goldencheetah/saucy

« back to all changes in this revision

Viewing changes to src/CalendarDownload.cpp

  • Committer: Package Import Robot
  • Author(s): KURASHIKI Satoru
  • Date: 2013-08-18 07:02:45 UTC
  • mfrom: (4.1.8 sid)
  • Revision ID: package-import@ubuntu.com-20130818070245-zgdvb47e1k3mtgil
Tags: 3.0-3
debian/control: remove needless dependency. (Closes: #719571)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2010 Mark Liversedge (liversedge@gmail.com)
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License as published by the Free
 
6
 * Software Foundation; either version 2 of the License, or (at your option)
 
7
 * any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful, but WITHOUT
 
10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 
12
 * more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License along
 
15
 * with this program; if not, write to the Free Software Foundation, Inc., 51
 
16
 * Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
17
 */
 
18
 
 
19
#include "CalendarDownload.h"
 
20
#ifdef GC_HAVE_ICAL
 
21
#include "ICalendar.h"
 
22
#include <libical/ical.h>
 
23
#endif
 
24
 
 
25
CalendarDownload::CalendarDownload(MainWindow *main) : main(main)
 
26
{
 
27
    nam = new QNetworkAccessManager(this);
 
28
    connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadFinished(QNetworkReply*)));
 
29
}
 
30
 
 
31
bool
 
32
CalendarDownload::download()
 
33
{
 
34
    QString request = appsettings->cvalue(main->cyclist, GC_WEBCAL_URL, "").toString();
 
35
    if (request == "") return false;
 
36
    else {
 
37
        // change webcal to http, since it is basically the same port
 
38
        QRegExp webcal("^webcal:");
 
39
        request.replace(webcal, QString("http:"));
 
40
    }
 
41
 
 
42
    QNetworkReply *reply = nam->get(QNetworkRequest(QUrl(request)));
 
43
 
 
44
    if (reply->error() != QNetworkReply::NoError) {
 
45
        QMessageBox::warning(main, tr("Calendar Data Download"), reply->errorString());
 
46
        return false;
 
47
    }
 
48
    return true;
 
49
}
 
50
 
 
51
void
 
52
CalendarDownload::downloadFinished(QNetworkReply *reply)
 
53
{
 
54
    QString fulltext = reply->readAll();
 
55
    QStringList errors;
 
56
 
 
57
#ifdef GC_HAVE_ICAL
 
58
    QString remoteCache = main->home.absolutePath()+"/remote.ics";
 
59
    QFile remoteCacheFile(remoteCache);
 
60
 
 
61
    if (fulltext != "") {
 
62
 
 
63
        // update remote cache - write to it!
 
64
        remoteCacheFile.open(QFile::ReadWrite | QFile::Text);
 
65
        QTextStream out(&remoteCacheFile);
 
66
        out << fulltext;
 
67
        remoteCacheFile.close();
 
68
 
 
69
    } else {
 
70
 
 
71
        if (remoteCacheFile.exists()) {
 
72
            QMessageBox msgBox;
 
73
            msgBox.setText(tr("Remote Calendar not available, reverting to cached workouts."));
 
74
            msgBox.setIcon(QMessageBox::Information);
 
75
            msgBox.exec();
 
76
 
 
77
            // read cache
 
78
            // read in the whole thing
 
79
            remoteCacheFile.open(QFile::ReadOnly | QFile::Text);
 
80
            QTextStream in(&remoteCacheFile);
 
81
            fulltext = in.readAll();
 
82
            remoteCacheFile.close();
 
83
        }
 
84
    }
 
85
 
 
86
    if (fulltext != "") main->rideCalendar->refreshRemote(fulltext);
 
87
#endif
 
88
}