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"
38 // const QString SCHEDULE_URL = "http://fosdem.org/2010/schedule/xml";
40 const QString PROXY_USERNAME;
41 const QString PROXY_PASSWD;
43 ImportScheduleWidget::ImportScheduleWidget(QWidget *aParent)
48 mXmlParser = new ScheduleXmlParser(this);
49 connect(mXmlParser, SIGNAL(progressStatus(int)), SLOT(showParsingProgress(int)));
50 connect(mXmlParser, SIGNAL(parsingSchedule(const QString &)), SLOT(parsingSchedule(const QString &)));
52 connect(browse, SIGNAL(clicked()), SLOT(browseSchedule()));
56 connect(online, SIGNAL(clicked()), SLOT(downloadSchedule()));
58 connect(changeUrl, SIGNAL(clicked()), SLOT(on_changeUrl()));
59 connect(newConfFromUrl, SIGNAL(clicked()), SLOT(on_newFromUrl()));
60 connect(delConf, SIGNAL(clicked()), SLOT(on_delete()));
62 mNetworkAccessManager = new QNetworkAccessManager(this);
63 connect(mNetworkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkQueryFinished(QNetworkReply*)));
64 mNetworkAccessManager->setProxy(QNetworkProxy::applicationProxy());
67 ImportScheduleWidget::~ImportScheduleWidget()
74 if(mNetworkAccessManager)
76 delete mNetworkAccessManager;
77 mNetworkAccessManager = NULL;
81 void ImportScheduleWidget::parsingSchedule(const QString &aTitle)
83 importScheduleLabel->setText("Importing: " + aTitle);
86 void ImportScheduleWidget::showParsingProgress(int progress)
88 progressBar->setValue(progress);
91 void ImportScheduleWidget::browseSchedule()
93 QString scheduleFileName = QFileDialog::getOpenFileName(this, tr("Select Conference Schedule"), QDir::homePath(), tr("Schedule Files (*.xml)"));
94 if(QFile::exists(scheduleFileName))
96 QFile file(scheduleFileName);
97 if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
99 qDebug() << "can't open " << file.fileName();
103 importData(file.readAll(), QString());
112 void ImportScheduleWidget::networkQueryFinished(QNetworkReply *aReply)
114 if ( aReply->error() != QNetworkReply::NoError )
116 qDebug() << "Error occured during download: " << aReply->errorString();
120 importData(aReply->readAll(), aReply->url().toEncoded());
124 void ImportScheduleWidget::downloadSchedule()
127 // TODO: make a nicer GUI
128 // basically, you have to do the following things:
129 // 1. store schedule URL for each conteferce
130 // 2. allow refreshing of the current conference schedule with "1 button click"
131 // 3. allow changing of the URL for a conference;
132 // run refresh together with it is ok and even justified by usability,
133 // but it must not loose this change if refresh not available.
134 // So it cannot be done as "do like #4 and rely on REPLACE".
135 // 4. allow getting the new conference by URL
137 // FIXME: it will throw
138 // GUI should not show this button if there is no active conf
139 importFromNetwork(Conference::getById(Conference::activeConference()).getUrl());
142 void ImportScheduleWidget::on_changeUrl()
144 // FIXME: it will throw
145 // GUI should not show this button if there is no active conf
146 Conference active_conference = Conference::getById(Conference::activeConference());
149 QInputDialog::getText(this, "URL request", "Enter the new URL for conference schedule"
151 , active_conference.getUrl()
154 active_conference.setUrl(new_url);
158 void ImportScheduleWidget::on_newFromUrl()
161 QString url = QInputDialog::getText(this, "URL request", "Put the schedule URL", QLineEdit::Normal, "", &ok);
163 importFromNetwork(url);
168 void ImportScheduleWidget::on_delete()
170 int active_id = Conference::activeConference();
171 Conference active_conference = Conference::getById(active_id);
173 QMessageBox::StandardButton answer =
174 QMessageBox::question(0
175 , "Deletion confirmation"
176 , QString("Really delete the %1 conference").arg(active_conference.title())
177 , QMessageBox::Yes | QMessageBox::No
180 if (answer == QMessageBox::Yes) {
181 QString title = active_conference.title();
182 Conference::deleteConference(active_id);
183 emit(scheduleDeleted(title));
187 void ImportScheduleWidget::importFromNetwork(const QString& url)
189 QNetworkRequest request;
190 request.setUrl(QUrl(url));
192 mNetworkAccessManager->setProxy(QNetworkProxy::applicationProxy());
193 mNetworkAccessManager->get(request);
196 void ImportScheduleWidget::importData(const QByteArray &aData, const QString& url)
201 // proxySettings->hide();
203 int confId = mXmlParser->parseData(aData, url);
208 // proxySettings->show();
209 importScheduleLabel->setText("Schedule:");
211 emit(scheduleImported(confId));