A header with the proper copyright/lincence statement was added into each source...
[toast/confclerk.git] / src / gui / importschedulewidget.cpp
1 /*
2  * Copyright (C) 2010 Ixonos Plc.
3  *
4  * This file is part of fosdem-schedule.
5  *
6  * fosdem-schedule is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation, either version 2 of the License, or (at your option)
9  * any later version.
10  *
11  * fosdem-schedule is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * fosdem-schedule.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "importschedulewidget.h"
20
21 #include <schedulexmlparser.h>
22 #include "proxysettingsdialog.h"
23
24 #include <QDir>
25 #include <QFile>
26 #include <QFileDialog>
27 #include <QNetworkProxy>
28 #include <QNetworkAccessManager>
29 #include <QNetworkReply>
30 #include <QDebug>
31 #include <appsettings.h>
32
33 const QString SCHEDULE_URL = "http://fosdem.org/2010/schedule/xml";
34
35 const QString PROXY_USERNAME;
36 const QString PROXY_PASSWD;
37
38 ImportScheduleWidget::ImportScheduleWidget(QWidget *aParent)
39     : QWidget(aParent)
40 {
41     setupUi(this);
42
43     mXmlParser = new ScheduleXmlParser(this);
44     connect(mXmlParser, SIGNAL(progressStatus(int)), SLOT(showParsingProgress(int)));
45     connect(mXmlParser, SIGNAL(parsingSchedule(const QString &)), SLOT(parsingSchedule(const QString &)));
46
47     connect(browse, SIGNAL(clicked()), SLOT(browseSchedule()));
48     progressBar->hide();
49
50     cancel->hide();
51     importAction->hide();
52     connect(online, SIGNAL(clicked()), SLOT(downloadSchedule()));
53
54     connect(proxySettings, SIGNAL(clicked()), SLOT(setupProxy()));
55     mNetworkAccessManager = new QNetworkAccessManager(this);
56     connect(mNetworkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkQueryFinished(QNetworkReply*)));
57     mNetworkAccessManager->setProxy(QNetworkProxy::applicationProxy());
58 }
59
60 ImportScheduleWidget::~ImportScheduleWidget()
61 {
62     if(mXmlParser)
63     {
64         delete mXmlParser;
65         mXmlParser = NULL;
66     }
67     if(mNetworkAccessManager)
68     {
69         delete mNetworkAccessManager;
70         mNetworkAccessManager = NULL;
71     }
72 }
73
74 void ImportScheduleWidget::parsingSchedule(const QString &aTitle)
75 {
76     importScheduleLabel->setText("Importing: " + aTitle);
77 }
78
79 void ImportScheduleWidget::showParsingProgress(int progress)
80 {
81     progressBar->setValue(progress);
82 }
83
84 void ImportScheduleWidget::browseSchedule()
85 {
86     QString scheduleFileName = QFileDialog::getOpenFileName(this, tr("Select Conference Schedule"), QDir::homePath(), tr("Schedule Files (*.xml)"));
87     if(QFile::exists(scheduleFileName))
88     {
89         QFile file(scheduleFileName);
90         if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
91         {
92             qDebug() << "can't open " << file.fileName();
93             return;
94         }
95
96         importData(file.readAll());
97
98     }
99     else
100     {
101         progressBar->hide();
102     }
103 }
104
105 void ImportScheduleWidget::networkQueryFinished(QNetworkReply *aReply)
106 {
107     if ( aReply->error() != QNetworkReply::NoError )
108     {
109         qDebug() << "Error occured during download: " << aReply->errorString();
110     }
111     else
112     {
113         importData(aReply->readAll());
114     }
115 }
116
117 void ImportScheduleWidget::downloadSchedule()
118 {
119     QNetworkRequest request;
120     request.setUrl(QUrl(SCHEDULE_URL));
121     mNetworkAccessManager->get(request);
122 }
123
124 void ImportScheduleWidget::importData(const QByteArray &aData)
125 {
126     browse->hide();
127     online->hide();
128     progressBar->show();
129     proxySettings->hide();
130
131     int confId = mXmlParser->parseData(aData);
132
133     progressBar->hide();
134     browse->show();
135     online->show();
136     proxySettings->show();
137     importScheduleLabel->setText("Import schedule: ");
138
139     emit(scheduleImported(confId));
140 }
141
142 void ImportScheduleWidget::setupProxy()
143 {
144     ProxySettingsDialog dialog;
145     dialog.exec();
146
147     qDebug() << "Setting-up proxy: " << AppSettings::proxyAddress() << ":" << AppSettings::proxyPort();
148     QNetworkProxy proxy(
149             AppSettings::isDirectConnection() ? QNetworkProxy::NoProxy : QNetworkProxy::HttpProxy,
150             AppSettings::proxyAddress(),
151             AppSettings::proxyPort(),
152             PROXY_USERNAME,
153             PROXY_PASSWD);
154     QNetworkProxy::setApplicationProxy(proxy);
155
156     mNetworkAccessManager->setProxy(QNetworkProxy::applicationProxy());
157 }
158