[ca90cb1] | 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 | */ |
---|
[c15be10] | 19 | #include "importschedulewidget.h" |
---|
[a957cfa] | 20 | |
---|
| 21 | #include <schedulexmlparser.h> |
---|
[a023fd2] | 22 | #include "proxysettingsdialog.h" |
---|
[a957cfa] | 23 | |
---|
| 24 | #include <QDir> |
---|
| 25 | #include <QFile> |
---|
| 26 | #include <QFileDialog> |
---|
[5007fde] | 27 | #include <QNetworkProxy> |
---|
| 28 | #include <QNetworkAccessManager> |
---|
| 29 | #include <QNetworkReply> |
---|
[a957cfa] | 30 | #include <QDebug> |
---|
[a023fd2] | 31 | #include <appsettings.h> |
---|
[a957cfa] | 32 | |
---|
[5007fde] | 33 | const QString SCHEDULE_URL = "http://fosdem.org/2010/schedule/xml"; |
---|
| 34 | |
---|
[a023fd2] | 35 | const QString PROXY_USERNAME; |
---|
| 36 | const QString PROXY_PASSWD; |
---|
| 37 | |
---|
[c15be10] | 38 | ImportScheduleWidget::ImportScheduleWidget(QWidget *aParent) |
---|
| 39 | : QWidget(aParent) |
---|
[a957cfa] | 40 | { |
---|
| 41 | setupUi(this); |
---|
| 42 | |
---|
| 43 | mXmlParser = new ScheduleXmlParser(this); |
---|
[d336730] | 44 | connect(mXmlParser, SIGNAL(progressStatus(int)), SLOT(showParsingProgress(int))); |
---|
[b86d4aa] | 45 | connect(mXmlParser, SIGNAL(parsingSchedule(const QString &)), SLOT(parsingSchedule(const QString &))); |
---|
[d336730] | 46 | |
---|
[b86d4aa] | 47 | connect(browse, SIGNAL(clicked()), SLOT(browseSchedule())); |
---|
[a957cfa] | 48 | progressBar->hide(); |
---|
[8b0bf22] | 49 | |
---|
| 50 | cancel->hide(); |
---|
| 51 | importAction->hide(); |
---|
[5007fde] | 52 | connect(online, SIGNAL(clicked()), SLOT(downloadSchedule())); |
---|
| 53 | |
---|
[a023fd2] | 54 | connect(proxySettings, SIGNAL(clicked()), SLOT(setupProxy())); |
---|
[5007fde] | 55 | mNetworkAccessManager = new QNetworkAccessManager(this); |
---|
| 56 | connect(mNetworkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkQueryFinished(QNetworkReply*))); |
---|
| 57 | mNetworkAccessManager->setProxy(QNetworkProxy::applicationProxy()); |
---|
[a957cfa] | 58 | } |
---|
| 59 | |
---|
[c15be10] | 60 | ImportScheduleWidget::~ImportScheduleWidget() |
---|
[a957cfa] | 61 | { |
---|
| 62 | if(mXmlParser) |
---|
| 63 | { |
---|
| 64 | delete mXmlParser; |
---|
| 65 | mXmlParser = NULL; |
---|
| 66 | } |
---|
[5007fde] | 67 | if(mNetworkAccessManager) |
---|
| 68 | { |
---|
| 69 | delete mNetworkAccessManager; |
---|
| 70 | mNetworkAccessManager = NULL; |
---|
| 71 | } |
---|
[a957cfa] | 72 | } |
---|
| 73 | |
---|
[b86d4aa] | 74 | void ImportScheduleWidget::parsingSchedule(const QString &aTitle) |
---|
| 75 | { |
---|
| 76 | importScheduleLabel->setText("Importing: " + aTitle); |
---|
| 77 | } |
---|
| 78 | |
---|
[c15be10] | 79 | void ImportScheduleWidget::showParsingProgress(int progress) |
---|
[a957cfa] | 80 | { |
---|
| 81 | progressBar->setValue(progress); |
---|
| 82 | } |
---|
| 83 | |
---|
[b86d4aa] | 84 | void ImportScheduleWidget::browseSchedule() |
---|
[a957cfa] | 85 | { |
---|
[b86d4aa] | 86 | QString scheduleFileName = QFileDialog::getOpenFileName(this, tr("Select Conference Schedule"), QDir::homePath(), tr("Schedule Files (*.xml)")); |
---|
| 87 | if(QFile::exists(scheduleFileName)) |
---|
[a957cfa] | 88 | { |
---|
[b86d4aa] | 89 | QFile file(scheduleFileName); |
---|
| 90 | if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) |
---|
| 91 | { |
---|
| 92 | qDebug() << "can't open " << file.fileName(); |
---|
| 93 | return; |
---|
| 94 | } |
---|
| 95 | |
---|
[5007fde] | 96 | importData(file.readAll()); |
---|
[b86d4aa] | 97 | |
---|
[5007fde] | 98 | } |
---|
| 99 | else |
---|
| 100 | { |
---|
[a957cfa] | 101 | progressBar->hide(); |
---|
[5007fde] | 102 | } |
---|
| 103 | } |
---|
[a957cfa] | 104 | |
---|
[5007fde] | 105 | void ImportScheduleWidget::networkQueryFinished(QNetworkReply *aReply) |
---|
| 106 | { |
---|
| 107 | if ( aReply->error() != QNetworkReply::NoError ) |
---|
| 108 | { |
---|
| 109 | qDebug() << "Error occured during download: " << aReply->errorString(); |
---|
[c15be10] | 110 | } |
---|
[b86d4aa] | 111 | else |
---|
[a957cfa] | 112 | { |
---|
[5007fde] | 113 | importData(aReply->readAll()); |
---|
[a957cfa] | 114 | } |
---|
| 115 | } |
---|
| 116 | |
---|
[5007fde] | 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(); |
---|
[a023fd2] | 129 | proxySettings->hide(); |
---|
[5007fde] | 130 | |
---|
| 131 | int confId = mXmlParser->parseData(aData); |
---|
| 132 | |
---|
| 133 | progressBar->hide(); |
---|
| 134 | browse->show(); |
---|
| 135 | online->show(); |
---|
[a023fd2] | 136 | proxySettings->show(); |
---|
[5007fde] | 137 | importScheduleLabel->setText("Import schedule: "); |
---|
| 138 | |
---|
| 139 | emit(scheduleImported(confId)); |
---|
| 140 | } |
---|
| 141 | |
---|
[a023fd2] | 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 | |
---|