Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
igrivko committed Apr 12, 2012
0 parents commit a7752e5
Show file tree
Hide file tree
Showing 21 changed files with 2,057 additions and 0 deletions.
302 changes: 302 additions & 0 deletions calendar_data_manager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,302 @@
#include "calendar_data_manager.h"
#include <QJson/Parser>
#include <QJson/Serializer>

#include <QDebug>
#include <QDateTime>
#include <QApplication>

CalendarDataManager::CalendarDataManager(QObject* parent) : QObject(parent)
{
m_pNetworkAccessManager = new QNetworkAccessManager(this);
connect(m_pNetworkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));

}

void CalendarDataManager::replyFinished(QNetworkReply * reply)
{
QApplication::restoreOverrideCursor();
qDebug() << __FUNCTION__;
QString json = reply->readAll();
qDebug() << "Reply = " << json;
qDebug() << "URL = " << reply->url();
QString strUrl = reply->url().toString();

if(json.isEmpty())
{
//Empty reply.
qDebug() << "Empty answer";
//Deletion of calendars or events
QRegExp reg("calendars/*/events", Qt::CaseSensitive, QRegExp::Wildcard);
if(strUrl.indexOf(reg) != -1)
{
emit eventChanged("");
}
else
{
emit calendarListChanged();
}
return;

}

QJson::Parser parser;
bool ok;
// json is a QString containing the data to convert
QVariant result = parser.parse (json.toLatin1(), &ok);

if(!ok)
{
emit errorOccured(QString("Cannot convert to QJson object: %1").arg(json));
return;
}

if(result.toMap().contains("error"))
{
qDebug() << "ERROR occured:\n" << strUrl;
emit errorOccured(result.toMap()["error"].toMap()["message"].toString());
return;
}
if(result.toMap()["kind"].toString() == "calendar#calendarList")
{
m_calendars = result.toMap()["items"].toList();
emit calendarListReady();
}
else if(result.toMap()["kind"].toString() == "calendar#calendar")
{
emit calendarListChanged();
}
else if(result.toMap()["kind"].toString() == "calendar#events")
{
m_events = result.toMap()["items"].toList();
emit eventsReady();
}
else if(result.toMap()["kind"].toString() == "calendar#event")
{
emit eventChanged(result.toMap()["id"].toString());
}

}

void CalendarDataManager::getCalendars(const QString& access_token)
{
qDebug() << Q_FUNC_INFO;
QString s = QString("https://www.googleapis.com/calendar/v3/users/me/calendarList?access_token=%1").arg(access_token);
QApplication::setOverrideCursor(Qt::WaitCursor);
m_pNetworkAccessManager->get(QNetworkRequest(QUrl(s)));
}

void CalendarDataManager::getEventsForCalendar(const QString& access_token, const QString & calID)
{
qDebug() << Q_FUNC_INFO;
QString s = QString("https://www.googleapis.com/calendar/v3/calendars/%1/events").arg(calID); //?access_token=%2").arg(calID).arg(access_token);

QUrl url;
url.setEncodedUrl(QUrl::toPercentEncoding(s, "/:"));

QNetworkRequest request;
request.setUrl(url);
request.setRawHeader("Authorization", QString("OAuth %1").arg(access_token).toLatin1());
request.setRawHeader("X-JavaScript-User-Agent", "Google APIs Explorer");

QApplication::setOverrideCursor(Qt::WaitCursor);
m_pNetworkAccessManager->get(request);
}

QVariantList CalendarDataManager::getCalendars()
{
return m_calendars;
}

QVariantList CalendarDataManager::getEvents()
{
return m_events;
}

void CalendarDataManager::newCalendar(const QString& access_token, const QString & name)
{
QString s = QString("https://www.googleapis.com/calendar/v3/calendars?access_token=%1").arg(access_token);


QByteArray params;
params.append(QString("{ \"summary\": \"%1\" }").arg(name).toUtf8());
QNetworkRequest request;
request.setUrl(QUrl(s));
request.setRawHeader("Content-Type", "application/json");

QApplication::setOverrideCursor(Qt::WaitCursor);
m_pNetworkAccessManager->post(request, params);
}

void CalendarDataManager::deleteCalendar(const QString& access_token, const QString & calID)
{
QString s = QString("https://www.googleapis.com/calendar/v3/calendars/%1").arg(calID);

QUrl url;
url.setEncodedUrl(QUrl::toPercentEncoding(s, "/:"));

QNetworkRequest request;
request.setUrl(url);
request.setRawHeader("Authorization", QString("OAuth %1").arg(access_token).toLatin1());

QApplication::setOverrideCursor(Qt::WaitCursor);
m_pNetworkAccessManager->deleteResource(request);
}

void CalendarDataManager::createEvent(const QString& access_token, const QString & calID, const QString & summary,
const QDate& startDate, const QDate& endDate,
const QTime& startTime, const QTime& endTime)
{
QString s = QString("https://www.googleapis.com/calendar/v3/calendars/%1/events").arg(calID);
QUrl url;
url.setEncodedUrl(QUrl::toPercentEncoding(s, "/:"));

QNetworkRequest request;
request.setUrl(url);
request.setRawHeader("Authorization", QString("OAuth %1").arg(access_token).toLatin1());
request.setRawHeader("Content-Type", "application/json");


QString str;
if(startTime.isNull())
{
str = QString("{\n") +
QString("\"summary\": \"%1\",\n").arg(summary) +
QString("\"start\": { \"date\": \"%1\" },\n").arg(startDate.toString()) +
QString("\"end\": { \"date\": \"%1\" }\n").arg(endDate.toString()) +
QString("}");
}
else
{
QDateTime startDT(startDate, startTime), endDT(endDate, endTime);
QString strStart = startDT.toUTC().toString(Qt::ISODate);
QString strEnd = endDT.toUTC().toString(Qt::ISODate);
if(!strStart.endsWith("Z"))
strStart += "Z";
if(!strEnd.endsWith("Z"))
strEnd += "Z";
str = QString("{\n") +
QString("\"summary\": \"%1\",\n").arg(summary) +
QString("\"start\": { \"dateTime\": \"%1\" },\n").arg(strStart) +
QString("\"end\": { \"dateTime\": \"%1\" }\n").arg(strEnd) +
QString("}");
}

QByteArray params = str.toUtf8();
qDebug() << "Params to send" << params;


QApplication::setOverrideCursor(Qt::WaitCursor);
m_pNetworkAccessManager->post(request, params);

}

void CalendarDataManager::moveEvent(const QString& access_token, const QString & calID,
const QString & eventID, const QString & destinationCalID)
{
QString s = QString("https://www.googleapis.com/calendar/v3/calendars/%1/events/%2/move?destination=%3").arg(calID).arg(eventID).arg(destinationCalID);
QUrl url;
url.setEncodedUrl(QUrl::toPercentEncoding(s, "/:?="));

QNetworkRequest request;
request.setUrl(url);
request.setRawHeader("Authorization", QString("OAuth %1").arg(access_token).toLatin1());
request.setRawHeader("Content-Type", "application/json");

QByteArray params;

QApplication::setOverrideCursor(Qt::WaitCursor);
m_pNetworkAccessManager->post(request, params);

}


void CalendarDataManager::deleteEvent(const QString& access_token, const QString & calID, const QString & eventID)
{
QString s = QString("https://www.googleapis.com/calendar/v3/calendars/%1/events/%2").arg(calID).arg(eventID);

QUrl url;
url.setEncodedUrl(QUrl::toPercentEncoding(s, "/:"));

QNetworkRequest request;
request.setUrl(url);
request.setRawHeader("Authorization", QString("OAuth %1").arg(access_token).toLatin1());
request.setRawHeader("X-JavaScript-User-Agent", "Google APIs Explorer");

QApplication::setOverrideCursor(Qt::WaitCursor);
m_pNetworkAccessManager->deleteResource(request);
}

void CalendarDataManager::getTimeShift(int& hours_shift, int& minutes_shift)
{
//Adding UTC time shift!!!
QDateTime curr = QDateTime::currentDateTime();
QDateTime utc = curr.toUTC();
QDateTime local = curr.toLocalTime();

QDateTime u1(utc.date(), utc.time(), Qt::LocalTime);
QDateTime l1(local.date(), local.time(), Qt::LocalTime);

//int fromLocalToUtc = l1.secsTo(u1);
int fromUtcToLocal = u1.secsTo(l1);

//qDebug() << "u1" << u1;
//qDebug() << "l1" << l1;

//qDebug() << "fromLocalToUtc" << fromLocalToUtc;
//qDebug() << "fromUtcToLocal" << fromUtcToLocal;

hours_shift = fromUtcToLocal/3600;
minutes_shift = (fromUtcToLocal%3600)/60;
}

void CalendarDataManager::updateEvent(const QString& access_token, const QString & calID,
const QString & eventID, const QVariant & eventData)
{
QString s = QString("https://www.googleapis.com/calendar/v3/calendars/%1/events/%2").arg(calID).arg(eventID);
QUrl url;
url.setEncodedUrl(QUrl::toPercentEncoding(s, "/:"));

QNetworkRequest request;
request.setUrl(url);
request.setRawHeader("Authorization", QString("OAuth %1").arg(access_token).toLatin1());
request.setRawHeader("Content-Type", "application/json");


QJson::Serializer serializer;
QByteArray params = serializer.serialize(eventData);
qDebug() << "Params to send" << params;

QApplication::setOverrideCursor(Qt::WaitCursor);
m_pNetworkAccessManager->put(request, params);
}

void CalendarDataManager::updateCalendar(const QString& access_token, const QString & calID,
const QString& summary, const QString& location,
const QString& description)
{
QString s = QString("https://www.googleapis.com/calendar/v3/calendars/%1").arg(calID);
QUrl url;
url.setEncodedUrl(QUrl::toPercentEncoding(s, "/:"));

QNetworkRequest request;
request.setUrl(url);
request.setRawHeader("Authorization", QString("OAuth %1").arg(access_token).toLatin1());
request.setRawHeader("Content-Type", "application/json");

QString str;
str = QString("{\n") +
QString("\"summary\": \"%1\",\n").arg(summary) +
QString("\"location\": \"%1\",\n").arg(location) +
QString("\"description\": \"%1\"\n").arg(description) +
QString("}");

QByteArray params = str.toUtf8();
qDebug() << "Params to send" << params;


QApplication::setOverrideCursor(Qt::WaitCursor);
m_pNetworkAccessManager->put(request, params);

}

63 changes: 63 additions & 0 deletions calendar_data_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#ifndef CALENDAR_DATA_MANAGER_H
#define CALENDAR_DATA_MANAGER_H

#include <QObject>
#include <QVariantList>
#include <QStringList>

#include <QNetworkReply>
#include <QNetworkAccessManager>
#include <QDate>
#include <QTime>

class CalendarDataManager : public QObject
{
Q_OBJECT
public:
explicit CalendarDataManager(QObject *parent = 0);

//Methods to get information from Google Calendar service.
void getCalendars(const QString& access_token);
void getEventsForCalendar(const QString& access_token, const QString & calID);
void newCalendar(const QString& access_token, const QString & name);
void deleteCalendar(const QString& access_token, const QString & calID);
void createEvent(const QString& access_token, const QString & calID, const QString & summary,
const QDate& startDate, const QDate& endDate,
const QTime& startTime, const QTime& endTime);
void deleteEvent(const QString& access_token, const QString & calID, const QString & eventID);
void updateEvent(const QString& access_token, const QString & calID,
const QString & eventID, const QVariant & eventData);

void moveEvent(const QString& access_token, const QString & calID,
const QString & eventID, const QString & destinationCalID);

void updateCalendar(const QString& access_token, const QString & calID,
const QString& summary, const QString& location,
const QString& description);

//Methods to get retrieved Calendars and Events.
QVariantList getCalendars();
QVariantList getEvents();

signals:
void errorOccured(const QString&);
void calendarListReady();
void eventsReady();
void calendarListChanged();
void eventChanged(const QString& id);

private slots:
void replyFinished(QNetworkReply*);

private:
void getTimeShift(int& hours_shift, int& minutes_shift);


private:
QNetworkAccessManager* m_pNetworkAccessManager;

QVariantList m_calendars;
QVariantList m_events;
};

#endif // CALENDAR_DATA_MANAGER_H
29 changes: 29 additions & 0 deletions edit_calendar_dialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "edit_calendar_dialog.h"
#include "ui_edit_calendar_dialog.h"

EditCalendarDialog::EditCalendarDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::EditCalendarDialog)
{
ui->setupUi(this);
}

EditCalendarDialog::~EditCalendarDialog()
{
delete ui;
}

void EditCalendarDialog::setCalendarDetails(const QString& summary, const QString& location,
const QString& description)
{
ui->summaryLineEdit->setText(summary);
ui->locationLineEdit->setText(location);
ui->descriptionTextEdit->setPlainText(description);
}

void EditCalendarDialog::getCalendarDetails(QString& summary, QString& location, QString& description)
{
summary = ui->summaryLineEdit->text();
location = ui->locationLineEdit->text();
description = ui->descriptionTextEdit->toPlainText();
}
Loading

0 comments on commit a7752e5

Please sign in to comment.