]> ToastFreeware Gitweb - toast/confclerk.git/blob - src/gui/importschedulewidget.cpp
de8efbc92249d6e8099ff5dc43fb93fe1f15da93
[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 <QMessageBox>
30 #include <QDebug>
31 #include <appsettings.h>
32
33 // TODO: this is temporary
34 #include <QInputDialog>
35
36 #include "conference.h"
37 #include "errormessage.h"
38
39 // const QString SCHEDULE_URL = "http://fosdem.org/2010/schedule/xml";
40
41 const QString PROXY_USERNAME;
42 const QString PROXY_PASSWD;
43
44 ImportScheduleWidget::ImportScheduleWidget(QWidget *aParent)
45     : QWidget(aParent)
46 {
47     setupUi(this);
48
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 &)));
52
53     connect(browse, SIGNAL(clicked()), SLOT(browseSchedule()));
54     progressBar->hide();
55
56     cancel->hide();
57     connect(online, SIGNAL(clicked()), SLOT(downloadSchedule()));
58
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
63     mNetworkAccessManager = new QNetworkAccessManager(this);
64     connect(mNetworkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkQueryFinished(QNetworkReply*)));
65     mNetworkAccessManager->setProxy(QNetworkProxy::applicationProxy());
66 }
67
68 ImportScheduleWidget::~ImportScheduleWidget()
69 {
70     if(mXmlParser)
71     {
72         delete mXmlParser;
73         mXmlParser = NULL;
74     }
75     if(mNetworkAccessManager)
76     {
77         delete mNetworkAccessManager;
78         mNetworkAccessManager = NULL;
79     }
80 }
81
82 void ImportScheduleWidget::parsingSchedule(const QString &aTitle)
83 {
84     importScheduleLabel->setText("Importing: " + aTitle);
85 }
86
87 void ImportScheduleWidget::showParsingProgress(int progress)
88 {
89     progressBar->setValue(progress);
90 }
91
92 void ImportScheduleWidget::browseSchedule()
93 {
94     QString scheduleFileName = QFileDialog::getOpenFileName(this, tr("Select Conference Schedule"), QDir::homePath(), tr("Schedule Files (*.xml)"));
95     if(QFile::exists(scheduleFileName))
96     {
97         QFile file(scheduleFileName);
98         if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
99         {
100             qDebug() << "can't open " << file.fileName();
101             return;
102         }
103
104         importData(file.readAll(), QString());
105
106     }
107     else
108     {
109         progressBar->hide();
110     }
111 }
112
113 void ImportScheduleWidget::networkQueryFinished(QNetworkReply *aReply)
114 {
115     if ( aReply->error() != QNetworkReply::NoError )
116     {
117         error_message(QString("Error occured during download: ") + aReply->errorString());
118     }
119     else
120     {
121         importData(aReply->readAll(), aReply->url().toEncoded());
122     }
123 }
124
125 void ImportScheduleWidget::downloadSchedule()
126 {
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
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
143 void 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);
156     }
157 }
158
159 void ImportScheduleWidget::on_newFromUrl()
160 {
161     bool ok = false;
162     QString url = QInputDialog::getText(this, "URL request", "Put the schedule URL", QLineEdit::Normal, "", &ok);
163     if (ok) {
164         importFromNetwork(url);
165     }
166
167 }
168
169 void ImportScheduleWidget::on_delete()
170 {
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     }
186 }
187
188 void ImportScheduleWidget::importFromNetwork(const QString& url)
189 {
190     QNetworkRequest request;
191     request.setUrl(QUrl(url));
192
193     mNetworkAccessManager->setProxy(QNetworkProxy::applicationProxy());
194     mNetworkAccessManager->get(request);
195 }
196
197 void ImportScheduleWidget::importData(const QByteArray &aData, const QString& url)
198 {
199     browse->hide();
200     online->hide();
201     progressBar->show();
202     // proxySettings->hide();
203
204     int confId = mXmlParser->parseData(aData, url);
205
206     progressBar->hide();
207     browse->show();
208     online->show();
209     // proxySettings->show();
210     importScheduleLabel->setText("Schedule:");
211
212     if (confId > 0) {
213         emit(scheduleImported(confId));
214     }
215 }
216