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