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!
|
2014-09-16 21:08:29 +00:00
|
|
|
{
|
|
|
|
qWarning() << "CSV : User trying to export to csv but no calendar was loaded. aborting.";
|
2014-09-11 20:08:37 +00:00
|
|
|
return false;
|
2014-09-16 21:08:29 +00:00
|
|
|
}
|
|
|
|
qDebug() << "CSV : Getting path for csv file from user...";
|
2014-09-08 15:54:52 +00:00
|
|
|
QString filePath = getFileFath();
|
|
|
|
if(filePath == NULL) //User canceled
|
|
|
|
{
|
2014-09-16 21:08:29 +00:00
|
|
|
qDebug() << "CSV : User pressed Cancel... returning false";
|
2014-09-08 15:54:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-09-16 21:08:29 +00:00
|
|
|
qDebug() << "CSV : User Chose: " << filePath;
|
|
|
|
qDebug() << "CSV : Atempting to export the Schedule...";
|
2014-09-08 15:54:52 +00:00
|
|
|
|
|
|
|
QFile file(filePath);
|
2014-09-16 21:08:29 +00:00
|
|
|
if(!file.open(QIODevice::ReadWrite | QIODevice::Truncate))
|
2014-09-08 15:54:52 +00:00
|
|
|
{
|
2014-09-16 21:08:29 +00:00
|
|
|
qCritical() << "CSV : unable to open/create the file... maybe permissions error.";
|
2014-09-08 15:54:52 +00:00
|
|
|
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();
|
|
|
|
QString lecturer = QString(coursePtr->getLecturer().c_str()); //WHY YOU USED STD STRING?!
|
|
|
|
QString type = QString(coursePtr->getType().c_str());
|
|
|
|
QString name = QString(coursePtr->getName().c_str());
|
|
|
|
QString room = QString(coursePtr->getRoom().c_str());
|
|
|
|
|
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);
|
|
|
|
if(line != NULL)
|
|
|
|
out << line << char(0x0A);
|
2014-09-16 21:08:29 +00:00
|
|
|
else
|
|
|
|
qWarning() << "CSV : Got A NULL in Line! in function: " << Q_FUNC_INFO;
|
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();
|
2014-09-16 21:08:29 +00:00
|
|
|
qDebug() << "CSV : Exported Successfully";
|
2014-09-08 15:54:52 +00:00
|
|
|
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;
|
|
|
|
}
|