source: src/gui/importschedulewidget.cpp @ b431d47

qt5
Last change on this file since b431d47 was 3329d39, checked in by kirilma <kirilma@…>, 12 years ago

use visible notifications of errors

also early detect parsing errors

  • Property mode set to 100644
File size: 6.3 KB
RevLine 
[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>
22
23#include <QDir>
24#include <QFile>
25#include <QFileDialog>
[5007fde]26#include <QNetworkProxy>
27#include <QNetworkAccessManager>
28#include <QNetworkReply>
[d97bcab]29#include <QMessageBox>
[a957cfa]30#include <QDebug>
[a023fd2]31#include <appsettings.h>
[a957cfa]32
[d06ae27]33// TODO: this is temporary
34#include <QInputDialog>
35
36#include "conference.h"
[3329d39]37#include "errormessage.h"
[d06ae27]38
39// const QString SCHEDULE_URL = "http://fosdem.org/2010/schedule/xml";
[5007fde]40
[a023fd2]41const QString PROXY_USERNAME;
42const QString PROXY_PASSWD;
43
[c15be10]44ImportScheduleWidget::ImportScheduleWidget(QWidget *aParent)
45    : QWidget(aParent)
[a957cfa]46{
47    setupUi(this);
48
49    mXmlParser = new ScheduleXmlParser(this);
[d336730]50    connect(mXmlParser, SIGNAL(progressStatus(int)), SLOT(showParsingProgress(int)));
[b86d4aa]51    connect(mXmlParser, SIGNAL(parsingSchedule(const QString &)), SLOT(parsingSchedule(const QString &)));
[d336730]52
[b86d4aa]53    connect(browse, SIGNAL(clicked()), SLOT(browseSchedule()));
[a957cfa]54    progressBar->hide();
[8b0bf22]55
56    cancel->hide();
[5007fde]57    connect(online, SIGNAL(clicked()), SLOT(downloadSchedule()));
58
[f5b68a4]59    connect(changeUrl, SIGNAL(clicked()), SLOT(on_changeUrl()));
60    connect(newConfFromUrl, SIGNAL(clicked()), SLOT(on_newFromUrl()));
61    connect(delConf, SIGNAL(clicked()), SLOT(on_delete()));
62
[5007fde]63    mNetworkAccessManager = new QNetworkAccessManager(this);
64    connect(mNetworkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkQueryFinished(QNetworkReply*)));
65    mNetworkAccessManager->setProxy(QNetworkProxy::applicationProxy());
[a957cfa]66}
67
[c15be10]68ImportScheduleWidget::~ImportScheduleWidget()
[a957cfa]69{
70    if(mXmlParser)
71    {
72        delete mXmlParser;
73        mXmlParser = NULL;
74    }
[5007fde]75    if(mNetworkAccessManager)
76    {
77        delete mNetworkAccessManager;
78        mNetworkAccessManager = NULL;
79    }
[a957cfa]80}
81
[b86d4aa]82void ImportScheduleWidget::parsingSchedule(const QString &aTitle)
83{
84    importScheduleLabel->setText("Importing: " + aTitle);
85}
86
[c15be10]87void ImportScheduleWidget::showParsingProgress(int progress)
[a957cfa]88{
89    progressBar->setValue(progress);
90}
91
[b86d4aa]92void ImportScheduleWidget::browseSchedule()
[a957cfa]93{
[b86d4aa]94    QString scheduleFileName = QFileDialog::getOpenFileName(this, tr("Select Conference Schedule"), QDir::homePath(), tr("Schedule Files (*.xml)"));
95    if(QFile::exists(scheduleFileName))
[a957cfa]96    {
[b86d4aa]97        QFile file(scheduleFileName);
98        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
99        {
100            qDebug() << "can't open " << file.fileName();
101            return;
102        }
103
[d06ae27]104        importData(file.readAll(), QString());
[b86d4aa]105
[5007fde]106    }
107    else
108    {
[a957cfa]109        progressBar->hide();
[5007fde]110    }
111}
[a957cfa]112
[5007fde]113void ImportScheduleWidget::networkQueryFinished(QNetworkReply *aReply)
114{
115    if ( aReply->error() != QNetworkReply::NoError )
116    {
[3329d39]117        error_message(QString("Error occured during download: ") + aReply->errorString());
[c15be10]118    }
[b86d4aa]119    else
[a957cfa]120    {
[d06ae27]121        importData(aReply->readAll(), aReply->url().toEncoded());
[a957cfa]122    }
123}
124
[5007fde]125void ImportScheduleWidget::downloadSchedule()
126{
[d06ae27]127
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
137
[f5b68a4]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());
141}
142
143void ImportScheduleWidget::on_changeUrl()
144{
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());
148    bool ok = false;
149    QString new_url =
150        QInputDialog::getText(this, "URL request", "Enter the new URL for conference schedule"
151                                , QLineEdit::Normal
152                                , active_conference.getUrl()
153                                , &ok);
154    if (ok) {
155        active_conference.setUrl(new_url);
[d06ae27]156    }
[f5b68a4]157}
[d06ae27]158
[f5b68a4]159void ImportScheduleWidget::on_newFromUrl()
160{
[d06ae27]161    bool ok = false;
[f5b68a4]162    QString url = QInputDialog::getText(this, "URL request", "Put the schedule URL", QLineEdit::Normal, "", &ok);
163    if (ok) {
164        importFromNetwork(url);
[d06ae27]165    }
[f5b68a4]166
167}
168
169void ImportScheduleWidget::on_delete()
170{
[d97bcab]171    int active_id = Conference::activeConference();
172    Conference active_conference = Conference::getById(active_id);
173
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
179            , QMessageBox::No);
180
181    if (answer == QMessageBox::Yes) {
182        QString title = active_conference.title();
183        Conference::deleteConference(active_id);
184        emit(scheduleDeleted(title));
185    }
[f5b68a4]186}
187
188void ImportScheduleWidget::importFromNetwork(const QString& url)
189{
190    QNetworkRequest request;
[d06ae27]191    request.setUrl(QUrl(url));
[ad5c62f]192
193    mNetworkAccessManager->setProxy(QNetworkProxy::applicationProxy());
[5007fde]194    mNetworkAccessManager->get(request);
195}
196
[d06ae27]197void ImportScheduleWidget::importData(const QByteArray &aData, const QString& url)
[5007fde]198{
199    browse->hide();
200    online->hide();
201    progressBar->show();
[ad5c62f]202    // proxySettings->hide();
[5007fde]203
[d06ae27]204    int confId = mXmlParser->parseData(aData, url);
[5007fde]205
206    progressBar->hide();
207    browse->show();
208    online->show();
[ad5c62f]209    // proxySettings->show();
[2eda137]210    importScheduleLabel->setText("Schedule:");
[5007fde]211
[3329d39]212    if (confId > 0) {
213        emit(scheduleImported(confId));
214    }
[5007fde]215}
216
Note: See TracBrowser for help on using the repository browser.