implement deleting a conference
[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
38 // const QString SCHEDULE_URL = "http://fosdem.org/2010/schedule/xml";
39
40 const QString PROXY_USERNAME;
41 const QString PROXY_PASSWD;
42
43 ImportScheduleWidget::ImportScheduleWidget(QWidget *aParent)
44     : QWidget(aParent)
45 {
46     setupUi(this);
47
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 &)));
51
52     connect(browse, SIGNAL(clicked()), SLOT(browseSchedule()));
53     progressBar->hide();
54
55     cancel->hide();
56     connect(online, SIGNAL(clicked()), SLOT(downloadSchedule()));
57
58     connect(changeUrl, SIGNAL(clicked()), SLOT(on_changeUrl()));
59     connect(newConfFromUrl, SIGNAL(clicked()), SLOT(on_newFromUrl()));
60     connect(delConf, SIGNAL(clicked()), SLOT(on_delete()));
61
62     mNetworkAccessManager = new QNetworkAccessManager(this);
63     connect(mNetworkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkQueryFinished(QNetworkReply*)));
64     mNetworkAccessManager->setProxy(QNetworkProxy::applicationProxy());
65 }
66
67 ImportScheduleWidget::~ImportScheduleWidget()
68 {
69     if(mXmlParser)
70     {
71         delete mXmlParser;
72         mXmlParser = NULL;
73     }
74     if(mNetworkAccessManager)
75     {
76         delete mNetworkAccessManager;
77         mNetworkAccessManager = NULL;
78     }
79 }
80
81 void ImportScheduleWidget::parsingSchedule(const QString &aTitle)
82 {
83     importScheduleLabel->setText("Importing: " + aTitle);
84 }
85
86 void ImportScheduleWidget::showParsingProgress(int progress)
87 {
88     progressBar->setValue(progress);
89 }
90
91 void ImportScheduleWidget::browseSchedule()
92 {
93     QString scheduleFileName = QFileDialog::getOpenFileName(this, tr("Select Conference Schedule"), QDir::homePath(), tr("Schedule Files (*.xml)"));
94     if(QFile::exists(scheduleFileName))
95     {
96         QFile file(scheduleFileName);
97         if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
98         {
99             qDebug() << "can't open " << file.fileName();
100             return;
101         }
102
103         importData(file.readAll(), QString());
104
105     }
106     else
107     {
108         progressBar->hide();
109     }
110 }
111
112 void ImportScheduleWidget::networkQueryFinished(QNetworkReply *aReply)
113 {
114     if ( aReply->error() != QNetworkReply::NoError )
115     {
116         qDebug() << "Error occured during download: " << aReply->errorString();
117     }
118     else
119     {
120         importData(aReply->readAll(), aReply->url().toEncoded());
121     }
122 }
123
124 void ImportScheduleWidget::downloadSchedule()
125 {
126
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
136
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());
140 }
141
142 void ImportScheduleWidget::on_changeUrl()
143 {
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());
147     bool ok = false;
148     QString new_url =
149         QInputDialog::getText(this, "URL request", "Enter the new URL for conference schedule"
150                                 , QLineEdit::Normal
151                                 , active_conference.getUrl()
152                                 , &ok);
153     if (ok) {
154         active_conference.setUrl(new_url);
155     }
156 }
157
158 void ImportScheduleWidget::on_newFromUrl()
159 {
160     bool ok = false;
161     QString url = QInputDialog::getText(this, "URL request", "Put the schedule URL", QLineEdit::Normal, "", &ok);
162     if (ok) {
163         importFromNetwork(url);
164     }
165
166 }
167
168 void ImportScheduleWidget::on_delete()
169 {
170     int active_id = Conference::activeConference();
171     Conference active_conference = Conference::getById(active_id);
172
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
178             , QMessageBox::No);
179
180     if (answer == QMessageBox::Yes) {
181         QString title = active_conference.title();
182         Conference::deleteConference(active_id);
183         emit(scheduleDeleted(title));
184     }
185 }
186
187 void ImportScheduleWidget::importFromNetwork(const QString& url)
188 {
189     QNetworkRequest request;
190     request.setUrl(QUrl(url));
191
192     mNetworkAccessManager->setProxy(QNetworkProxy::applicationProxy());
193     mNetworkAccessManager->get(request);
194 }
195
196 void ImportScheduleWidget::importData(const QByteArray &aData, const QString& url)
197 {
198     browse->hide();
199     online->hide();
200     progressBar->show();
201     // proxySettings->hide();
202
203     int confId = mXmlParser->parseData(aData, url);
204
205     progressBar->hide();
206     browse->show();
207     online->show();
208     // proxySettings->show();
209     importScheduleLabel->setText("Import schedule: ");
210
211     emit(scheduleImported(confId));
212 }
213