Cosmetic tidy on DiaryWindow, tidy CalDAV code.

This commit is contained in:
Mark Liversedge
2011-01-16 20:09:29 +00:00
parent 696a9273dd
commit c7b1c77fb7
6 changed files with 109 additions and 48 deletions

View File

@@ -21,7 +21,7 @@
CalDAV::CalDAV(MainWindow *main) : main(main), mode(None)
{
nam = new QNetworkAccessManager(this);
connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(authenticateResponse(QNetworkReply*)));
connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestReply(QNetworkReply*)));
connect(nam, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), this,
SLOT(userpass(QNetworkReply*,QAuthenticator*)));
@@ -29,8 +29,11 @@ CalDAV::CalDAV(MainWindow *main) : main(main), mode(None)
// SLOT(sslErrors(QNetworkReply*,QList<QSslError>)));
}
//
// GET event directory listing
//
bool
CalDAV::authenticate()
CalDAV::download()
{
QString url = appsettings->cvalue(main->cyclist, GC_DVURL, "").toString();
if (url == "") return false; // not configured
@@ -46,35 +49,44 @@ CalDAV::authenticate()
return true;
}
void
CalDAV::authenticateResponse(QNetworkReply *reply)
//
// REPORT of "all" VEVENTS
//
bool
CalDAV::report()
{
switch (mode) {
case Events:
main->rideCalendar->refreshRemote(reply->readAll());
break;
default:
case Put:
break;
QString url = appsettings->cvalue(main->cyclist, GC_DVURL, "").toString();
if (url == "") return false; // not configured
QNetworkRequest request = QNetworkRequest(QUrl(url));
QByteArray *queryText = new QByteArray("<x1:calendar-query xmlns:x1=\"urn:ietf:params:xml:ns:caldav\">"
"<x0:prop xmlns:x0=\"DAV:\">"
"<x0:getetag/>"
"<x1:calendar-data/>"
"</x0:prop>"
"<x1:filter>"
"<x1:comp-filter name=\"VCALENDAR\">"
"<x1:comp-filter name=\"VEVENT\">"
"<x1:time-range end=\"21001231\" start=\"20000101T000000Z\"/>"
"</x1:comp-filter>"
"</x1:comp-filter>"
"</x1:filter>"
"</x1:calendar-query>");
QBuffer *query = new QBuffer(queryText);
mode = Report;
QNetworkReply *reply = nam->sendCustomRequest(request, "REPORT", query);
if (reply->error() != QNetworkReply::NoError) {
QMessageBox::warning(main, tr("CalDAV REPORT url error"), reply->errorString());
mode = None;
return false;
}
mode = None;
return true;
}
void
CalDAV::userpass(QNetworkReply*,QAuthenticator*a)
{
QString user = appsettings->cvalue(main->cyclist, GC_DVUSER, "").toString();
QString pass = appsettings->cvalue(main->cyclist, GC_DVPASS, "").toString();
a->setUser(user);
a->setPassword(pass);
}
void
CalDAV::sslErrors(QNetworkReply*,QList<QSslError>&)
{
}
// convert a rideItem into a vcard ICal Event
// utility function to create a VCALENDAR from a single RideItem
static
icalcomponent *createEvent(RideItem *rideItem)
{
@@ -141,6 +153,9 @@ icalcomponent *createEvent(RideItem *rideItem)
return root;
}
//
// PUT a ride item
//
bool
CalDAV::upload(RideItem *rideItem)
{
@@ -174,3 +189,41 @@ CalDAV::upload(RideItem *rideItem)
return true;
}
//
// All queries/commands respond here
//
void
CalDAV::requestReply(QNetworkReply *reply)
{
QString response = reply->readAll();
switch (mode) {
case Report:
case Events:
main->rideCalendar->refreshRemote(response);
break;
default:
case Put:
break;
}
mode = None;
}
//
// Provide user credentials, called when receive a 401
//
void
CalDAV::userpass(QNetworkReply*,QAuthenticator*a)
{
QString user = appsettings->cvalue(main->cyclist, GC_DVUSER, "").toString();
QString pass = appsettings->cvalue(main->cyclist, GC_DVPASS, "").toString();
a->setUser(user);
a->setPassword(pass);
}
//
// Trap SSL errors, does nothing ... for now
//
void
CalDAV::sslErrors(QNetworkReply*,QList<QSslError>&)
{
}

View File

@@ -51,7 +51,7 @@ class CalDAV : public QObject
Q_OBJECT
G_OBJECT
enum action { Put, Get, Events, None };
enum action { Put, Get, Events, Report, None };
typedef enum action ActionType;
public:
@@ -61,20 +61,22 @@ public:
public slots:
// authentication (and refresh all events)
bool authenticate();
bool download();
// upload ride and data (as attachment) to calendar
// Query CalDAV server for events ...
bool report();
// Upload ride as a VEVENT
bool upload(RideItem *rideItem);
// Catch NAM signals ...
void requestReply(QNetworkReply *reply);
void userpass(QNetworkReply*r,QAuthenticator*a);
void sslErrors(QNetworkReply*,QList<QSslError>&);
void authenticateResponse(QNetworkReply *reply);
private:
MainWindow *main;
QNetworkAccessManager *nam;
ActionType mode;
/* CalendarParser *parser; */
};
#endif

View File

@@ -36,9 +36,8 @@ DiaryWindow::DiaryWindow(MainWindow *mainWindow) :
bold.setPointSize(14);
bold.setWeight(QFont::Bold);
title = new QLabel("", this);
title->setAlignment(Qt::AlignCenter | Qt::AlignVCenter);
title->setFont(bold);
title->setFixedWidth(250);
title->setAlignment(Qt::AlignCenter);
QIcon prevIcon(":images/toolbar/back_alt.png");
QIcon nextIcon(":images/toolbar/forward_alt.png");
@@ -59,15 +58,11 @@ DiaryWindow::DiaryWindow(MainWindow *mainWindow) :
viewMode->setCurrentIndex(appsettings->cvalue(mainWindow->cyclist, GC_DIARY_VIEW, "1").toInt());
QLabel *spacer = new QLabel(this);
spacer->setFixedWidth(120); // same as viewMode, centering controls...
controls->addWidget(spacer);
controls->addStretch();
controls->addWidget(prev);
controls->addWidget(title);
controls->addWidget(next);
controls->addStretch();
controls->addWidget(title, Qt::AlignCenter | Qt::AlignVCenter);
controls->addStretch();
controls->addWidget(viewMode);
vlayout->addLayout(controls);
@@ -85,6 +80,7 @@ DiaryWindow::DiaryWindow(MainWindow *mainWindow) :
monthlyView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
monthlyView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
monthlyView->viewport()->installEventFilter(this);
monthlyView->setGridStyle(Qt::DotLine);
// weekly view via QxtScheduleView
weeklyView = new QxtScheduleView;

View File

@@ -417,13 +417,13 @@ class GcCalendarDelegate : public QItemDelegate
QPen pen;
pen.setColor(Qt::black);
pen.setWidth(2);
painter->setPen(Qt::NoPen);
QColor fillColor = colors[i++];
fillColor.setAlpha(250);
fillColor.setAlpha(200);
painter->setBrush(fillColor);
painter->setPen(pen);
painter->setRenderHint(QPainter::Antialiasing);
painter->drawPath(cachePath);
painter->setPen(pen);
painter->drawText(bd, text);
}
}

View File

@@ -255,16 +255,26 @@ QVariant ICalendar::data(QDate date, int role)
QList<icalcomponent*>*p = localCalendar.value(date, NULL);
if (p) {
foreach(icalcomponent*event, *p) {
QString desc;
icalproperty *summary = icalcomponent_get_first_property(event, ICAL_SUMMARY_PROPERTY);
desc = propertyToString(summary);
icalproperty *property = icalcomponent_get_first_property(event, ICAL_DESCRIPTION_PROPERTY);
strings << propertyToString(property);
desc += " ";
desc += propertyToString(property);
strings << desc;
}
}
// remote
p = remoteCalendar.value(date, NULL);
if (p) {
foreach(icalcomponent*event, *p) {
QString desc;
icalproperty *summary = icalcomponent_get_first_property(event, ICAL_SUMMARY_PROPERTY);
desc = propertyToString(summary);
icalproperty *property = icalcomponent_get_first_property(event, ICAL_DESCRIPTION_PROPERTY);
strings << propertyToString(property);
desc += " ";
desc += propertyToString(property);
strings << desc;
}
}
return strings;

View File

@@ -320,7 +320,7 @@ MainWindow::MainWindow(const QDir &home) :
#ifdef GC_HAVE_ICAL
rideCalendar = new ICalendar(this); // my local/remote calendar entries
davCalendar = new CalDAV(this); // remote caldav
davCalendar->authenticate(); // login
davCalendar->download(); // login
#endif
QTreeWidgetItem *last = NULL;
@@ -1805,7 +1805,7 @@ void
MainWindow::refreshCalendar()
{
#ifdef GC_HAVE_ICAL
davCalendar->authenticate();
davCalendar->download();
calendarDownload->download();
#endif
}