From 11b4101c589e943b9ecaaf54d8fb414bfde3b747 Mon Sep 17 00:00:00 2001 From: Sagi Dayan Date: Sun, 7 Sep 2014 22:57:01 +0300 Subject: [PATCH] Added Featcher: Export calendar to CSV format for GoogleCalendar/iCal/Outlook at the gui - calendar tab - there is now an export to csv button. clicking it will prompt thr user to save the file on his system hard drive. the app will add the .csv extention. the csv file, will export only one week, starting from the begining of the first semester of 2015 (the one that is coming). in the future i will add a Held doc for exporting and importimg to the users calendar. cheers! --- jceGrade.pro | 6 +- main/CalendarTab/CalendarManager.cpp | 5 + main/CalendarTab/CalendarManager.h | 1 + main/mainscreen.cpp | 5 + main/mainscreen.h | 3 + main/mainscreen.ui | 9 +- src/jceData/CSV/csv_exporter.cpp | 145 ++++++++++++++++++++++++ src/jceData/CSV/csv_exporter.h | 26 +++++ src/jceData/Calendar/calendarSchedule.h | 2 +- 9 files changed, 198 insertions(+), 4 deletions(-) create mode 100644 src/jceData/CSV/csv_exporter.cpp create mode 100644 src/jceData/CSV/csv_exporter.h diff --git a/jceGrade.pro b/jceGrade.pro index a2c9e16..139040f 100644 --- a/jceGrade.pro +++ b/jceGrade.pro @@ -36,7 +36,8 @@ HEADERS += \ src/jceSettings/jceLoginHtmlScripts.h \ src/jceSettings/user.h \ src/jceData/Calendar/calendarCourse.h \ - src/jceData/Calendar/calendarSchedule.h + src/jceData/Calendar/calendarSchedule.h \ + src/jceData/CSV/csv_exporter.h SOURCES += \ main/CalendarTab/CalendarManager.cpp \ @@ -53,5 +54,6 @@ SOURCES += \ src/jceSettings/jcelogin.cpp \ src/jceSettings/user.cpp \ src/jceData/Calendar/calendarCourse.cpp \ - src/jceData/Calendar/calendarSchedule.cpp + src/jceData/Calendar/calendarSchedule.cpp \ + src/jceData/CSV/csv_exporter.cpp diff --git a/main/CalendarTab/CalendarManager.cpp b/main/CalendarTab/CalendarManager.cpp index ebb9899..addeaac 100644 --- a/main/CalendarTab/CalendarManager.cpp +++ b/main/CalendarTab/CalendarManager.cpp @@ -9,3 +9,8 @@ void CalendarManager::setCalendar(std::string html) { caliSchedPtr->setPage(html); } + +calendarSchedule *CalendarManager::getSch() +{ + return this->caliSchedPtr; +} diff --git a/main/CalendarTab/CalendarManager.h b/main/CalendarTab/CalendarManager.h index 042e831..e7a9737 100644 --- a/main/CalendarTab/CalendarManager.h +++ b/main/CalendarTab/CalendarManager.h @@ -16,6 +16,7 @@ public: void setCalendar(std::string html); void resetTable() { if (caliSchedPtr != NULL) caliSchedPtr->clearTableItems(); } + calendarSchedule* getSch(); private: calendarSchedule * caliSchedPtr; }; diff --git a/main/mainscreen.cpp b/main/mainscreen.cpp index bb26fc3..8e4bca3 100644 --- a/main/mainscreen.cpp +++ b/main/mainscreen.cpp @@ -291,3 +291,8 @@ void MainScreen::on_actionHow_To_triggered() ""); } + +void MainScreen::on_pushButton_2_clicked() +{ + CSV_Exporter::exportCalendar(this->calendar->getSch()); +} diff --git a/main/mainscreen.h b/main/mainscreen.h index 2ea31d2..0c68e4b 100644 --- a/main/mainscreen.h +++ b/main/mainscreen.h @@ -13,6 +13,7 @@ #include "./CourseTab/coursestablemanager.h" #include "./LoginTab/loginhandler.h" #include "./CalendarTab/CalendarManager.h" +#include "../src/jceData/CSV/csv_exporter.h" #define StatusIconHeight 35 namespace Ui { @@ -59,6 +60,8 @@ private slots: void on_checkBoxCoursesInfluence_toggled(bool checked); + void on_pushButton_2_clicked(); + private: void uiSetDisconnectMode(); diff --git a/main/mainscreen.ui b/main/mainscreen.ui index df3cece..a9c42a5 100644 --- a/main/mainscreen.ui +++ b/main/mainscreen.ui @@ -588,6 +588,13 @@ font-size: 15px; + + + + Export to CSV + + + @@ -619,7 +626,7 @@ font-size: 15px; 0 0 855 - 21 + 29 diff --git a/src/jceData/CSV/csv_exporter.cpp b/src/jceData/CSV/csv_exporter.cpp new file mode 100644 index 0000000..dc2159e --- /dev/null +++ b/src/jceData/CSV/csv_exporter.cpp @@ -0,0 +1,145 @@ +#include "csv_exporter.h" + +CSV_Exporter::CSV_Exporter() +{ + +} + +bool CSV_Exporter::exportCalendar(calendarSchedule *calSched) +{ + 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); + if(!file.open(QIODevice::ReadWrite | QIODevice::Text)) + { + qDebug() << "unable to open/create the file... maybe permissions error."; + return false; + }//else + + 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()); + + QString line = makeLine(name, day, startH, startM, endH, endM, lecturer, room, type); + if(line != NULL) + out << line << char(0x0A); + } + + + out.flush(); + + + file.close(); + qDebug() << "Saved Successfuly! - HazZaA!"; + return true; + +} + +QString CSV_Exporter::getFileFath() +{ + QString fileName = QFileDialog::getSaveFileName(); + if(fileName == "") + return NULL; + fileName.append(".csv"); + return fileName; +} + +QString CSV_Exporter::makeLine(QString name, int day, int startH, int startM, int endH, int endM, QString lecturer, QString room, QString type) +{ + //Creating a CSV text line for Google Calendar/iCal/Outlook + // First day for semester 10/26/2014 + + QString CSV_line = ""; + QString subject = "\""; + subject.append(name); + subject.append(" - "); + subject.append(type); + subject.append("\""); + + QString date; + switch (day) { + case 1: + date = "10/26/2014"; + break; + case 2: + date = "10/27/2014"; + break; + case 3: + date = "10/28/2014"; + break; + case 4: + date = "10/29/2014"; + break; + case 5: + date = "10/30/2014"; + break; + case 6: + date = "10/31/2014"; + break; + default: + return NULL; + break; + } + + 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(","); + + CSV_line.append(date); + CSV_line.append(","); + + CSV_line.append(start); + CSV_line.append(","); + + CSV_line.append(date); + 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; +} diff --git a/src/jceData/CSV/csv_exporter.h b/src/jceData/CSV/csv_exporter.h new file mode 100644 index 0000000..dec0783 --- /dev/null +++ b/src/jceData/CSV/csv_exporter.h @@ -0,0 +1,26 @@ +#ifndef CSV_EXPORTER_H +#define CSV_EXPORTER_H + +#include +#include +#include +#include +#include + +#include "../Calendar/calendarSchedule.h" + +#define CSV_CALENDAR_HEADER "Subject,Start Date,Start Time,End Date,End Time,Description,Location" + +class CSV_Exporter +{ +public: + CSV_Exporter(); + static bool exportCalendar(calendarSchedule* calSched); + + +private: + static QString getFileFath(); + static QString makeLine(QString name,int day,int startH,int startM,int endH,int endM,QString lecturer,QString room,QString type); +}; + +#endif // CSV_EXPORTER_H diff --git a/src/jceData/Calendar/calendarSchedule.h b/src/jceData/Calendar/calendarSchedule.h index df27daf..028afa1 100644 --- a/src/jceData/Calendar/calendarSchedule.h +++ b/src/jceData/Calendar/calendarSchedule.h @@ -20,7 +20,7 @@ public: calendarSchedule(); ~calendarSchedule() { clearTableItems(); } void setPage(std::string html); - void clearTableItems(); + void clearTableItems(); signals: