2 * Copyright (C) 2010 Ixonos Plc.
4 * This file is part of fosdem-schedule.
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)
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
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/>.
19 #include "mainwindow.h"
23 #include <QNetworkProxy>
24 #include <QNetworkAccessManager>
25 #include <QNetworkReply>
27 #include <sqlengine.h>
30 #include <eventmodel.h>
33 #include <conference.h>
36 #include <QMessageBox>
39 #include <eventdialog.h>
40 #include "daynavigatorwidget.h"
41 #include "settingsdialog.h"
42 #include "conferenceeditor.h"
43 #include "schedulexmlparser.h"
44 #include "errormessage.h"
46 #include <tabcontainer.h>
47 #include <appsettings.h>
49 const QString PROXY_USERNAME;
50 const QString PROXY_PASSWD;
52 MainWindow::MainWindow(int aEventId, QWidget *aParent)
53 : QMainWindow(aParent)
54 , conferenceModel(new ConferenceModel(this))
55 , mXmlParser(new ScheduleXmlParser(this))
56 , mNetworkAccessManager(new QNetworkAccessManager(this))
60 saved_title = windowTitle();
63 tabWidget->setTabText(1,"Favs");
64 //tabWidget->setTabText(2,"Day");
67 // first time run aplication: -> let's have it direct connection in this case
68 if(!AppSettings::contains("proxyIsDirectConnection"))
69 AppSettings::setDirectConnection(true);
71 if(AppSettings::isDirectConnection())
73 qDebug() << "Setting-up proxy: " << AppSettings::proxyAddress() << ":" << AppSettings::proxyPort();
76 AppSettings::isDirectConnection() ? QNetworkProxy::NoProxy : QNetworkProxy::HttpProxy,
77 AppSettings::proxyAddress(),
78 AppSettings::proxyPort(),
81 QNetworkProxy::setApplicationProxy(proxy);
83 // event details have changed
84 connect(dayTabContainer, SIGNAL(eventHasChanged(int,bool)), SLOT(eventHasChanged(int,bool)));
85 connect(favsTabContainer, SIGNAL(eventHasChanged(int,bool)), SLOT(eventHasChanged(int,bool)));
86 connect(tracksTabContainer, SIGNAL(eventHasChanged(int,bool)), SLOT(eventHasChanged(int,bool)));
87 connect(roomsTabContainer, SIGNAL(eventHasChanged(int,bool)), SLOT(eventHasChanged(int,bool)));
88 connect(nowTabContainer, SIGNAL(eventHasChanged(int,bool)), SLOT(eventHasChanged(int,bool)));
89 connect(searchTabContainer, SIGNAL(eventHasChanged(int,bool)), SLOT(eventHasChanged(int,bool)));
91 connect(aboutAction, SIGNAL(triggered()), SLOT(aboutApp()));
92 connect(settingsAction, SIGNAL(triggered()), SLOT(setup()));
93 connect(conferencesAction, SIGNAL(triggered()), SLOT(showConferences()));
95 useConference(Conference::activeConference());
96 // optimization, see useConference() code
99 } catch (OrmException) {
103 // TODO: open conferences at startup?
106 tabWidget->setCurrentIndex(6); // 6 - conference tab
110 // open dialog for given Event ID
111 // this is used in case Alarm Dialog request application to start
116 EventDialog dialog(aEventId,this);
119 catch(OrmNoObjectException&) {} // just start application
120 catch(...) {} // just start application
123 connect(mNetworkAccessManager, SIGNAL(finished(QNetworkReply*)), SLOT(networkQueryFinished(QNetworkReply*)));
125 connect(mXmlParser, SIGNAL(parsingScheduleBegin()), conferenceModel, SLOT(newConferenceBegin()));
126 connect(mXmlParser, SIGNAL(parsingScheduleEnd(const QString&)), conferenceModel, SLOT(newConferenceEnd(const QString&)));
129 void MainWindow::aboutApp()
131 QDialog dialog(this);
135 dialog.setFixedWidth(width());
140 void MainWindow::eventHasChanged(int aEventId, bool aReloadModel)
142 dayTabContainer->updateTreeViewModel(aEventId);
143 favsTabContainer->updateTreeViewModel(aEventId,aReloadModel);
144 tracksTabContainer->updateTreeViewModel(aEventId);
145 nowTabContainer->updateTreeViewModel(aEventId);
146 roomsTabContainer->updateTreeViewModel(aEventId);
147 searchTabContainer->updateTreeViewModel(aEventId);
150 void MainWindow::useConference(int id)
153 Conference::getById(Conference::activeConference()).update("active",0);
154 Conference new_active = Conference::getById(id);
155 new_active.update("active",1);
157 // looks like it does not work at n900
158 setWindowTitle(new_active.title());
161 // dont run initTabs() here
162 // it takes much CPU, making travelling between conferences in ConferenceEditor longer
163 // and is not seen in maemo WM anyway
164 // instead run it explicitly
166 // 2. when ConferenceEditor finished
167 // dont forget to protect the calls by try-catch!
169 // just in case, clear conference selection instead
172 // end of optimization
174 } catch (OrmException& e) {
175 // cannon set an active conference
182 void MainWindow::initTabs()
184 int confId = Conference::activeConference();
185 Conference active = Conference::getById(confId);
186 QDate startDate = active.start();
187 QDate endDate = active.end();
189 // 'dayNavigator' emits signal 'dateChanged' after setting valid START:END dates
190 dayTabContainer->setDates(startDate, endDate);
191 tracksTabContainer->setDates(startDate, endDate);
192 roomsTabContainer->setDates(startDate, endDate);
193 favsTabContainer->setDates(startDate, endDate);
194 searchTabContainer->setDates(startDate, endDate);
195 searchTabContainer->searchAgainClicked();
196 nowTabContainer->updateTreeView(QDate::currentDate());
199 void MainWindow::clearTabs()
201 dayTabContainer->clearModel();
202 tracksTabContainer->clearModel();
203 roomsTabContainer->clearModel();
204 favsTabContainer->clearModel();
205 searchTabContainer->clearModel();
206 searchTabContainer->searchAgainClicked();
207 nowTabContainer->clearModel();
210 void MainWindow::unsetConference()
213 setWindowTitle(saved_title);
216 void MainWindow::setup()
218 SettingsDialog dialog;
221 qDebug() << "Setting-up proxy: " << AppSettings::proxyAddress() << ":" << AppSettings::proxyPort();
223 AppSettings::isDirectConnection() ? QNetworkProxy::NoProxy : QNetworkProxy::HttpProxy,
224 AppSettings::proxyAddress(),
225 AppSettings::proxyPort(),
228 QNetworkProxy::setApplicationProxy(proxy);
231 /** Create and run ConferenceEditor dialog, making required connections for it.
233 This method manages, which classes actually perform changes in conference list.
235 There are several classes that modify the conferences:
237 deletion and URL update.
238 this, mXmlParser and mNetworkAccessManager:
239 addition and refresh.
241 void MainWindow::showConferences()
243 ConferenceEditor dialog(conferenceModel, this);
245 connect(&dialog, SIGNAL(haveConferenceUrl(const QString&)), SLOT(importFromNetwork(const QString&)));
246 connect(&dialog, SIGNAL(haveConferenceFile(const QString&)), SLOT(importFromFile(const QString&)));
247 connect(&dialog, SIGNAL(removeConferenceRequested(int)), SLOT(removeConference(int)));
248 connect(&dialog, SIGNAL(changeUrlRequested(int, const QString&)),
249 SLOT(changeConferenceUrl(int, const QString&)));
251 connect(&dialog, SIGNAL(haveConferenceSelected(int)), SLOT(useConference(int)));
252 connect(&dialog, SIGNAL(noneConferenceSelected()), SLOT(unsetConference()));
254 connect(mXmlParser, SIGNAL(parsingScheduleBegin()), &dialog, SLOT(importStarted()));
255 connect(mXmlParser, SIGNAL(progressStatus(int)), &dialog, SLOT(showParsingProgress(int)));
256 connect(mXmlParser, SIGNAL(parsingScheduleEnd(const QString&)), &dialog, SLOT(importFinished(const QString&)));
258 connect(this, SIGNAL(conferenceRemoved()), &dialog, SLOT(conferenceRemoved()));
262 // optimization, see useConference() code
265 } catch (OrmException) {
270 void MainWindow::networkQueryFinished(QNetworkReply *aReply)
272 if ( aReply->error() != QNetworkReply::NoError )
274 error_message(QString("Error occured during download: ") + aReply->errorString());
278 qDebug() << __PRETTY_FUNCTION__ << ": have data";
279 importData(aReply->readAll(), aReply->url().toEncoded());
283 void MainWindow::importData(const QByteArray &aData, const QString& url)
285 mXmlParser->parseData(aData, url);
288 void MainWindow::importFromNetwork(const QString& url)
290 qDebug() << __PRETTY_FUNCTION__;
291 QNetworkRequest request;
292 request.setUrl(QUrl(url));
294 mNetworkAccessManager->setProxy(QNetworkProxy::applicationProxy());
295 mNetworkAccessManager->get(request);
298 void MainWindow::importFromFile(const QString& filename)
300 qDebug() << __PRETTY_FUNCTION__;
301 QFile file(filename);
302 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
303 static const QString format("Cannot read \"%1\": error %2");
304 error_message(format.arg(filename, QString::number(file.error())));
307 importData(file.readAll(), "");
310 void MainWindow::removeConference(int id)
312 Conference::deleteConference(id);
313 conferenceModel->conferenceRemoved();
315 emit conferenceRemoved();
318 void MainWindow::changeConferenceUrl(int id, const QString& url)
320 Conference::getById(id).setUrl(url);