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