2014-09-08 15:54:52 +00:00
|
|
|
#include "csv_exporter.h"
|
|
|
|
|
|
|
|
CSV_Exporter::CSV_Exporter()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-09-09 20:31:48 +00:00
|
|
|
bool CSV_Exporter::exportCalendar(calendarSchedule *calSched, CalendarDialog *cal)
|
2014-09-08 15:54:52 +00:00
|
|
|
{
|
2014-09-11 20:08:37 +00:00
|
|
|
if ((cal == NULL) || (calSched == NULL) || (calSched->getCourses() == NULL)) //pointers checking!
|
|
|
|
return false;
|
2014-09-08 15:54:52 +00:00
|
|
|
qDebug() << "Getting path for csv file from user...";
|
|
|
|
QString filePath = getFileFath();
|
|
|
|
if(filePath == NULL) //User canceled
|
|
|
|
{
|
|
|
|
qDebug() << "User pressed Cancel... returning false";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
qDebug() << "User Chose: " << filePath;
|
|
|
|
qDebug() << "Atempting to export the Schedule...";
|
|
|
|
|
|
|
|
QFile file(filePath);
|
2014-09-08 17:59:02 +00:00
|
|
|
if(!file.open(QIODevice::ReadWrite | QIODevice::Text |QIODevice::Truncate))
|
2014-09-08 15:54:52 +00:00
|
|
|
{
|
|
|
|
qDebug() << "unable to open/create the file... maybe permissions error.";
|
|
|
|
return false;
|
|
|
|
}//else
|
2014-09-08 17:59:02 +00:00
|
|
|
//Delete the file
|
2014-09-08 15:54:52 +00:00
|
|
|
QTextStream out(&file);
|
|
|
|
out << CSV_CALENDAR_HEADER << "\n";
|
|
|
|
for (calendarCourse *coursePtr: *(calSched->getCourses()))
|
|
|
|
{
|
|
|
|
// Subject,Start Date,Start Time,End Date,End Time,Description,Location
|
|
|
|
int day = coursePtr->getDay();
|
|
|
|
int startH = coursePtr->getHourBegin();
|
|
|
|
int startM = coursePtr->getMinutesBegin();
|
|
|
|
int endH = coursePtr->getHourEnd();
|
|
|
|
int endM = coursePtr->getMinutesEnd();
|
2014-09-17 01:08:38 +00:00
|
|
|
QString lecturer = coursePtr->getLecturer();
|
|
|
|
QString type = coursePtr->getType();
|
|
|
|
QString name = coursePtr->getName();
|
|
|
|
QString room = coursePtr->getRoom();
|
2014-09-08 15:54:52 +00:00
|
|
|
|
2014-09-09 20:31:48 +00:00
|
|
|
QDate currentDate = cal->getStartDate();
|
|
|
|
|
|
|
|
currentDate = currentDate.addDays(day-1);
|
|
|
|
|
|
|
|
for(;currentDate <= cal->getEndDate(); currentDate = currentDate.addDays(7))
|
|
|
|
{
|
|
|
|
QString line = makeLine(name, ¤tDate, startH, startM, endH, endM, lecturer, room, type);
|
2014-09-08 17:52:38 +00:00
|
|
|
#ifdef Q_OS_LINUX || Q_OS_UNIX
|
2014-09-09 20:31:48 +00:00
|
|
|
if(line != NULL)
|
|
|
|
out << line << char(0x0A);
|
2014-09-08 17:52:38 +00:00
|
|
|
#endif
|
2014-09-11 19:58:08 +00:00
|
|
|
#ifdef Q_OS_OSX
|
2014-09-09 20:31:48 +00:00
|
|
|
if(line != NULL)
|
|
|
|
out << line << char(0x0A);
|
2014-09-08 17:52:38 +00:00
|
|
|
#endif
|
|
|
|
#ifdef Q_OS_WIN
|
2014-09-09 20:31:48 +00:00
|
|
|
if(line != NULL)
|
|
|
|
out << line << char(0x0D) << char(0x0A);
|
2014-09-08 17:52:38 +00:00
|
|
|
#endif
|
2014-09-09 20:31:48 +00:00
|
|
|
}
|
2014-09-11 19:58:08 +00:00
|
|
|
out.flush();
|
2014-09-08 15:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
qDebug() << "Saved Successfuly! - HazZaA!";
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QString CSV_Exporter::getFileFath()
|
|
|
|
{
|
|
|
|
QString fileName = QFileDialog::getSaveFileName();
|
|
|
|
if(fileName == "")
|
|
|
|
return NULL;
|
2014-09-08 17:07:42 +00:00
|
|
|
if(!fileName.contains(".csv", Qt::CaseInsensitive))
|
|
|
|
fileName.append(".csv");
|
2014-09-08 15:54:52 +00:00
|
|
|
return fileName;
|
|
|
|
}
|
|
|
|
|
2014-09-09 20:31:48 +00:00
|
|
|
QString CSV_Exporter::makeLine(QString name, QDate *date, int startH, int startM, int endH, int endM, QString lecturer, QString room, QString type)
|
2014-09-08 15:54:52 +00:00
|
|
|
{
|
|
|
|
//Creating a CSV text line for Google Calendar/iCal/Outlook
|
|
|
|
|
|
|
|
QString CSV_line = "";
|
|
|
|
QString subject = "\"";
|
|
|
|
subject.append(name);
|
|
|
|
subject.append(" - ");
|
|
|
|
subject.append(type);
|
|
|
|
subject.append("\"");
|
|
|
|
|
2014-09-09 20:31:48 +00:00
|
|
|
|
|
|
|
QString dateStr = date->toString("MM/dd/yyyy");
|
|
|
|
|
2014-09-08 15:54:52 +00:00
|
|
|
|
|
|
|
QString start;
|
|
|
|
start.append(QString::number(startH));
|
|
|
|
start.append(":00");
|
|
|
|
//start.append(QString::number(startM));
|
|
|
|
start.append(":00");
|
|
|
|
|
|
|
|
QString end;
|
|
|
|
end.append(QString::number(endH));
|
|
|
|
end.append(":");
|
|
|
|
end.append(QString::number(endM));
|
|
|
|
end.append(":00");
|
|
|
|
|
|
|
|
QString description = "\"מרצה ";
|
|
|
|
description.append(lecturer);
|
|
|
|
description.append("\n");
|
|
|
|
description.append(" ב");
|
|
|
|
description.append(room);
|
|
|
|
description.append("\"");
|
|
|
|
|
|
|
|
//Create the Fucking Line
|
|
|
|
//Header: Subject,Start Date,Start Time,End Date,End Time,Description,Location
|
|
|
|
CSV_line.append(subject);
|
|
|
|
CSV_line.append(",");
|
|
|
|
|
2014-09-09 20:31:48 +00:00
|
|
|
CSV_line.append(dateStr);
|
2014-09-08 15:54:52 +00:00
|
|
|
CSV_line.append(",");
|
|
|
|
|
|
|
|
CSV_line.append(start);
|
|
|
|
CSV_line.append(",");
|
|
|
|
|
2014-09-09 20:31:48 +00:00
|
|
|
CSV_line.append(dateStr);
|
2014-09-08 15:54:52 +00:00
|
|
|
CSV_line.append(",");
|
|
|
|
|
|
|
|
CSV_line.append(end);
|
|
|
|
CSV_line.append(",");
|
|
|
|
|
|
|
|
CSV_line.append(description);
|
|
|
|
CSV_line.append(",");
|
|
|
|
|
|
|
|
CSV_line.append("\"JCE Jerusalem\"");
|
|
|
|
|
|
|
|
return CSV_line;
|
|
|
|
}
|