2 * Copyright (C) 2010 Ixonos Plc.
4 * This file is part of fosdem-schedule.
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)
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
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/>.
19 #include "importschedulewidget.h"
21 #include <schedulexmlparser.h>
25 #include <QFileDialog>
26 #include <QNetworkProxy>
27 #include <QNetworkAccessManager>
28 #include <QNetworkReply>
29 #include <QMessageBox>
31 #include <appsettings.h>
33 // TODO: this is temporary
34 #include <QInputDialog>
36 #include "conference.h"
37 #include "errormessage.h"
39 // const QString SCHEDULE_URL = "http://fosdem.org/2010/schedule/xml";
41 const QString PROXY_USERNAME;
42 const QString PROXY_PASSWD;
44 ImportScheduleWidget::ImportScheduleWidget(QWidget *aParent)
49 mXmlParser = new ScheduleXmlParser(this);
50 connect(mXmlParser, SIGNAL(progressStatus(int)), SLOT(showParsingProgress(int)));
51 connect(mXmlParser, SIGNAL(parsingSchedule(const QString &)), SLOT(parsingSchedule(const QString &)));
53 connect(browse, SIGNAL(clicked()), SLOT(browseSchedule()));
57 connect(online, SIGNAL(clicked()), SLOT(downloadSchedule()));
59 connect(changeUrl, SIGNAL(clicked()), SLOT(on_changeUrl()));
60 connect(newConfFromUrl, SIGNAL(clicked()), SLOT(on_newFromUrl()));
61 connect(delConf, SIGNAL(clicked()), SLOT(on_delete()));
63 mNetworkAccessManager = new QNetworkAccessManager(this);
64 connect(mNetworkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkQueryFinished(QNetworkReply*)));
65 mNetworkAccessManager->setProxy(QNetworkProxy::applicationProxy());
68 ImportScheduleWidget::~ImportScheduleWidget()
75 if(mNetworkAccessManager)
77 delete mNetworkAccessManager;
78 mNetworkAccessManager = NULL;
82 void ImportScheduleWidget::parsingSchedule(const QString &aTitle)
84 importScheduleLabel->setText("Importing: " + aTitle);
87 void ImportScheduleWidget::showParsingProgress(int progress)
89 progressBar->setValue(progress);
92 void ImportScheduleWidget::browseSchedule()
94 QString scheduleFileName = QFileDialog::getOpenFileName(this, tr("Select Conference Schedule"), QDir::homePath(), tr("Schedule Files (*.xml)"));
95 if(QFile::exists(scheduleFileName))
97 QFile file(scheduleFileName);
98 if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
100 qDebug() << "can't open " << file.fileName();
104 importData(file.readAll(), QString());
113 void ImportScheduleWidget::networkQueryFinished(QNetworkReply *aReply)
115 if ( aReply->error() != QNetworkReply::NoError )
117 error_message(QString("Error occured during download: ") + aReply->errorString());
121 importData(aReply->readAll(), aReply->url().toEncoded());
125 void ImportScheduleWidget::downloadSchedule()
128 // TODO: make a nicer GUI
129 // basically, you have to do the following things:
130 // 1. store schedule URL for each conteferce
131 // 2. allow refreshing of the current conference schedule with "1 button click"
132 // 3. allow changing of the URL for a conference;
133 // run refresh together with it is ok and even justified by usability,
134 // but it must not loose this change if refresh not available.
135 // So it cannot be done as "do like #4 and rely on REPLACE".
136 // 4. allow getting the new conference by URL
138 // FIXME: it will throw
139 // GUI should not show this button if there is no active conf
140 importFromNetwork(Conference::getById(Conference::activeConference()).getUrl());
143 void ImportScheduleWidget::on_changeUrl()
145 // FIXME: it will throw
146 // GUI should not show this button if there is no active conf
147 Conference active_conference = Conference::getById(Conference::activeConference());
150 QInputDialog::getText(this, "URL request", "Enter the new URL for conference schedule"
152 , active_conference.getUrl()
155 active_conference.setUrl(new_url);
159 void ImportScheduleWidget::on_newFromUrl()
162 QString url = QInputDialog::getText(this, "URL request", "Put the schedule URL", QLineEdit::Normal, "", &ok);
164 importFromNetwork(url);
169 void ImportScheduleWidget::on_delete()
171 int active_id = Conference::activeConference();
172 Conference active_conference = Conference::getById(active_id);
174 QMessageBox::StandardButton answer =
175 QMessageBox::question(0
176 , "Deletion confirmation"
177 , QString("Really delete the %1 conference").arg(active_conference.title())
178 , QMessageBox::Yes | QMessageBox::No
181 if (answer == QMessageBox::Yes) {
182 QString title = active_conference.title();
183 Conference::deleteConference(active_id);
184 emit(scheduleDeleted(title));
188 void ImportScheduleWidget::importFromNetwork(const QString& url)
190 QNetworkRequest request;
191 request.setUrl(QUrl(url));
193 mNetworkAccessManager->setProxy(QNetworkProxy::applicationProxy());
194 mNetworkAccessManager->get(request);
197 void ImportScheduleWidget::importData(const QByteArray &aData, const QString& url)
202 // proxySettings->hide();
204 int confId = mXmlParser->parseData(aData, url);
209 // proxySettings->show();
210 importScheduleLabel->setText("Schedule:");
213 emit(scheduleImported(confId));