source: src/gui/importschedulewidget.cpp @ 5007fde

qt5
Last change on this file since 5007fde was 5007fde, checked in by pavelpa <pavelpa@…>, 12 years ago

implemented importing the schedule from the Internet

  • Property mode set to 100644
File size: 2.9 KB
Line 
1#include "importschedulewidget.h"
2
3#include <schedulexmlparser.h>
4
5#include <QDir>
6#include <QFile>
7#include <QFileDialog>
8#include <QNetworkProxy>
9#include <QNetworkAccessManager>
10#include <QNetworkReply>
11#include <QDebug>
12
13const QString SCHEDULE_URL = "http://fosdem.org/2010/schedule/xml";
14
15ImportScheduleWidget::ImportScheduleWidget(QWidget *aParent)
16    : QWidget(aParent)
17{
18    setupUi(this);
19
20    mXmlParser = new ScheduleXmlParser(this);
21    connect(mXmlParser, SIGNAL(progressStatus(int)), SLOT(showParsingProgress(int)));
22    connect(mXmlParser, SIGNAL(parsingSchedule(const QString &)), SLOT(parsingSchedule(const QString &)));
23
24    connect(browse, SIGNAL(clicked()), SLOT(browseSchedule()));
25    progressBar->hide();
26
27    cancel->hide();
28    importAction->hide();
29    connect(online, SIGNAL(clicked()), SLOT(downloadSchedule()));
30
31    mNetworkAccessManager = new QNetworkAccessManager(this);
32    connect(mNetworkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkQueryFinished(QNetworkReply*)));
33    mNetworkAccessManager->setProxy(QNetworkProxy::applicationProxy());
34}
35
36ImportScheduleWidget::~ImportScheduleWidget()
37{
38    if(mXmlParser)
39    {
40        delete mXmlParser;
41        mXmlParser = NULL;
42    }
43    if(mNetworkAccessManager)
44    {
45        delete mNetworkAccessManager;
46        mNetworkAccessManager = NULL;
47    }
48}
49
50void ImportScheduleWidget::parsingSchedule(const QString &aTitle)
51{
52    importScheduleLabel->setText("Importing: " + aTitle);
53}
54
55void ImportScheduleWidget::showParsingProgress(int progress)
56{
57    progressBar->setValue(progress);
58}
59
60void ImportScheduleWidget::browseSchedule()
61{
62    QString scheduleFileName = QFileDialog::getOpenFileName(this, tr("Select Conference Schedule"), QDir::homePath(), tr("Schedule Files (*.xml)"));
63    if(QFile::exists(scheduleFileName))
64    {
65        QFile file(scheduleFileName);
66        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
67        {
68            qDebug() << "can't open " << file.fileName();
69            return;
70        }
71
72        importData(file.readAll());
73
74    }
75    else
76    {
77        progressBar->hide();
78    }
79}
80
81void ImportScheduleWidget::networkQueryFinished(QNetworkReply *aReply)
82{
83    qDebug() << "ImportScheduleWidget::networkQueryFinished()";
84
85    if ( aReply->error() != QNetworkReply::NoError )
86    {
87        qDebug() << "Error occured during download: " << aReply->errorString();
88    }
89    else
90    {
91        importData(aReply->readAll());
92    }
93}
94
95void ImportScheduleWidget::downloadSchedule()
96{
97    qDebug() << "downloading schedule";
98
99    QNetworkRequest request;
100    request.setUrl(QUrl(SCHEDULE_URL));
101    mNetworkAccessManager->get(request);
102}
103
104void ImportScheduleWidget::importData(const QByteArray &aData)
105{
106    browse->hide();
107    online->hide();
108    progressBar->show();
109
110    int confId = mXmlParser->parseData(aData);
111
112    progressBar->hide();
113    browse->show();
114    online->show();
115    importScheduleLabel->setText("Import schedule: ");
116
117    emit(scheduleImported(confId));
118}
119
Note: See TracBrowser for help on using the repository browser.