379aa673e8bb784d2ce2e2da6ffde7d46b7ca12f
[toast/confclerk.git] / src / gui / importschedulewidget.cpp
1 #include "importschedulewidget.h"
2
3 #include <schedulexmlparser.h>
4
5 #include <QDir>
6 #include <QFile>
7 #include <QFileDialog>
8 #include <QNetworkProxy>
9 #include <QNetworkAccessManager>
10 #include <QNetworkReply>
11 #include <QDebug>
12
13 const QString SCHEDULE_URL = "http://fosdem.org/2010/schedule/xml";
14
15 ImportScheduleWidget::ImportScheduleWidget(QWidget *aParent)
16     : QWidget(aParent)
17 {
18     setupUi(this);
19
20     mXmlParser = new ScheduleXmlParser(this);
21     connect(mXmlParser, SIGNAL(progressStatus(int)), SLOT(showParsingProgress(int)));
22     connect(mXmlParser, SIGNAL(parsingSchedule(const QString &)), SLOT(parsingSchedule(const QString &)));
23
24     connect(browse, SIGNAL(clicked()), SLOT(browseSchedule()));
25     progressBar->hide();
26
27     cancel->hide();
28     importAction->hide();
29     connect(online, SIGNAL(clicked()), SLOT(downloadSchedule()));
30
31     mNetworkAccessManager = new QNetworkAccessManager(this);
32     connect(mNetworkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkQueryFinished(QNetworkReply*)));
33     mNetworkAccessManager->setProxy(QNetworkProxy::applicationProxy());
34 }
35
36 ImportScheduleWidget::~ImportScheduleWidget()
37 {
38     if(mXmlParser)
39     {
40         delete mXmlParser;
41         mXmlParser = NULL;
42     }
43     if(mNetworkAccessManager)
44     {
45         delete mNetworkAccessManager;
46         mNetworkAccessManager = NULL;
47     }
48 }
49
50 void ImportScheduleWidget::parsingSchedule(const QString &aTitle)
51 {
52     importScheduleLabel->setText("Importing: " + aTitle);
53 }
54
55 void ImportScheduleWidget::showParsingProgress(int progress)
56 {
57     progressBar->setValue(progress);
58 }
59
60 void ImportScheduleWidget::browseSchedule()
61 {
62     QString scheduleFileName = QFileDialog::getOpenFileName(this, tr("Select Conference Schedule"), QDir::homePath(), tr("Schedule Files (*.xml)"));
63     if(QFile::exists(scheduleFileName))
64     {
65         QFile file(scheduleFileName);
66         if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
67         {
68             qDebug() << "can't open " << file.fileName();
69             return;
70         }
71
72         importData(file.readAll());
73
74     }
75     else
76     {
77         progressBar->hide();
78     }
79 }
80
81 void ImportScheduleWidget::networkQueryFinished(QNetworkReply *aReply)
82 {
83     qDebug() << "ImportScheduleWidget::networkQueryFinished()";
84
85     if ( aReply->error() != QNetworkReply::NoError )
86     {
87         qDebug() << "Error occured during download: " << aReply->errorString();
88     }
89     else
90     {
91         importData(aReply->readAll());
92     }
93 }
94
95 void ImportScheduleWidget::downloadSchedule()
96 {
97     qDebug() << "downloading schedule";
98
99     QNetworkRequest request;
100     request.setUrl(QUrl(SCHEDULE_URL));
101     mNetworkAccessManager->get(request);
102 }
103
104 void ImportScheduleWidget::importData(const QByteArray &aData)
105 {
106     browse->hide();
107     online->hide();
108     progressBar->show();
109
110     int confId = mXmlParser->parseData(aData);
111
112     progressBar->hide();
113     browse->show();
114     online->show();
115     importScheduleLabel->setText("Import schedule: ");
116
117     emit(scheduleImported(confId));
118 }
119