add buttons for refreshm new url and delete and partly implement corresponding actions
[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 // TODO: this is temporary
33 #include <QInputDialog>
34
35 #include "conference.h"
36
37 // const QString SCHEDULE_URL = "http://fosdem.org/2010/schedule/xml";
38
39 const QString PROXY_USERNAME;
40 const QString PROXY_PASSWD;
41
42 ImportScheduleWidget::ImportScheduleWidget(QWidget *aParent)
43     : QWidget(aParent)
44 {
45     setupUi(this);
46
47     mXmlParser = new ScheduleXmlParser(this);
48     connect(mXmlParser, SIGNAL(progressStatus(int)), SLOT(showParsingProgress(int)));
49     connect(mXmlParser, SIGNAL(parsingSchedule(const QString &)), SLOT(parsingSchedule(const QString &)));
50
51     connect(browse, SIGNAL(clicked()), SLOT(browseSchedule()));
52     progressBar->hide();
53
54     cancel->hide();
55     connect(online, SIGNAL(clicked()), SLOT(downloadSchedule()));
56
57     connect(changeUrl, SIGNAL(clicked()), SLOT(on_changeUrl()));
58     connect(newConfFromUrl, SIGNAL(clicked()), SLOT(on_newFromUrl()));
59     connect(delConf, SIGNAL(clicked()), SLOT(on_delete()));
60
61     mNetworkAccessManager = new QNetworkAccessManager(this);
62     connect(mNetworkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkQueryFinished(QNetworkReply*)));
63     mNetworkAccessManager->setProxy(QNetworkProxy::applicationProxy());
64 }
65
66 ImportScheduleWidget::~ImportScheduleWidget()
67 {
68     if(mXmlParser)
69     {
70         delete mXmlParser;
71         mXmlParser = NULL;
72     }
73     if(mNetworkAccessManager)
74     {
75         delete mNetworkAccessManager;
76         mNetworkAccessManager = NULL;
77     }
78 }
79
80 void ImportScheduleWidget::parsingSchedule(const QString &aTitle)
81 {
82     importScheduleLabel->setText("Importing: " + aTitle);
83 }
84
85 void ImportScheduleWidget::showParsingProgress(int progress)
86 {
87     progressBar->setValue(progress);
88 }
89
90 void ImportScheduleWidget::browseSchedule()
91 {
92     QString scheduleFileName = QFileDialog::getOpenFileName(this, tr("Select Conference Schedule"), QDir::homePath(), tr("Schedule Files (*.xml)"));
93     if(QFile::exists(scheduleFileName))
94     {
95         QFile file(scheduleFileName);
96         if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
97         {
98             qDebug() << "can't open " << file.fileName();
99             return;
100         }
101
102         importData(file.readAll(), QString());
103
104     }
105     else
106     {
107         progressBar->hide();
108     }
109 }
110
111 void ImportScheduleWidget::networkQueryFinished(QNetworkReply *aReply)
112 {
113     if ( aReply->error() != QNetworkReply::NoError )
114     {
115         qDebug() << "Error occured during download: " << aReply->errorString();
116     }
117     else
118     {
119         importData(aReply->readAll(), aReply->url().toEncoded());
120     }
121 }
122
123 void ImportScheduleWidget::downloadSchedule()
124 {
125
126     // TODO: make a nicer GUI
127     // basically, you have to do the following things:
128     // 1. store schedule URL for each conteferce
129     // 2. allow refreshing of the current conference schedule with "1 button click"
130     // 3. allow changing of the URL for a conference;
131     //     run refresh together with it is ok and even justified by usability,
132     //     but it must not loose this change if refresh not available.
133     //     So it cannot be done as "do like #4 and rely on REPLACE".
134     // 4. allow getting the new conference by URL
135
136     // FIXME: it will throw
137     // GUI should not show this button if there is no active conf
138     importFromNetwork(Conference::getById(Conference::activeConference()).getUrl());
139 }
140
141 void ImportScheduleWidget::on_changeUrl()
142 {
143     // FIXME: it will throw
144     // GUI should not show this button if there is no active conf
145     Conference active_conference = Conference::getById(Conference::activeConference());
146     bool ok = false;
147     QString new_url =
148         QInputDialog::getText(this, "URL request", "Enter the new URL for conference schedule"
149                                 , QLineEdit::Normal
150                                 , active_conference.getUrl()
151                                 , &ok);
152     if (ok) {
153         active_conference.setUrl(new_url);
154     }
155 }
156
157 void ImportScheduleWidget::on_newFromUrl()
158 {
159     bool ok = false;
160     QString url = QInputDialog::getText(this, "URL request", "Put the schedule URL", QLineEdit::Normal, "", &ok);
161     if (ok) {
162         importFromNetwork(url);
163     }
164
165 }
166
167 void ImportScheduleWidget::on_delete()
168 {
169     // TODO: implement
170 }
171
172 void ImportScheduleWidget::importFromNetwork(const QString& url)
173 {
174     QNetworkRequest request;
175     request.setUrl(QUrl(url));
176
177     mNetworkAccessManager->setProxy(QNetworkProxy::applicationProxy());
178     mNetworkAccessManager->get(request);
179 }
180
181 void ImportScheduleWidget::importData(const QByteArray &aData, const QString& url)
182 {
183     browse->hide();
184     online->hide();
185     progressBar->show();
186     // proxySettings->hide();
187
188     int confId = mXmlParser->parseData(aData, url);
189
190     progressBar->hide();
191     browse->show();
192     online->show();
193     // proxySettings->show();
194     importScheduleLabel->setText("Import schedule: ");
195
196     emit(scheduleImported(confId));
197 }
198