2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011 Philipp Spitzer, gregor herrmann
5 * This file is part of ConfClerk.
7 * ConfClerk is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 2 of the License, or (at your option)
12 * ConfClerk is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * ConfClerk. If not, see <http://www.gnu.org/licenses/>.
20 #include "mainwindow.h"
24 #include <QNetworkProxy>
25 #include <QNetworkAccessManager>
26 #include <QNetworkReply>
28 #include <sqlengine.h>
31 #include <eventmodel.h>
34 #include <conference.h>
37 #include <QMessageBox>
40 #include <eventdialog.h>
41 #include "daynavigatorwidget.h"
42 #include "settingsdialog.h"
43 #include "conferenceeditor.h"
44 #include "schedulexmlparser.h"
45 #include "errormessage.h"
47 #include <tabcontainer.h>
48 #include <appsettings.h>
50 const QString PROXY_USERNAME;
51 const QString PROXY_PASSWD;
53 MainWindow::MainWindow(int aEventId, QWidget *aParent)
54 : QMainWindow(aParent)
55 , conferenceModel(new ConferenceModel(this))
56 , mXmlParser(new ScheduleXmlParser(this))
57 , mNetworkAccessManager(new QNetworkAccessManager(this))
61 saved_title = windowTitle();
64 tabWidget->setTabText(1,"Favs");
65 //tabWidget->setTabText(2,"Day");
68 // first time run aplication: -> let's have it direct connection in this case
69 if(!AppSettings::contains("proxyIsDirectConnection"))
70 AppSettings::setDirectConnection(true);
73 if(AppSettings::isDirectConnection())
75 qDebug() << "Setting-up proxy: " << AppSettings::proxyAddress() << ":" << AppSettings::proxyPort();
79 AppSettings::isDirectConnection() ? QNetworkProxy::NoProxy : QNetworkProxy::HttpProxy,
80 AppSettings::proxyAddress(),
81 AppSettings::proxyPort(),
84 QNetworkProxy::setApplicationProxy(proxy);
86 // event details have changed
87 connect(dayTabContainer, SIGNAL(eventHasChanged(int,bool)), SLOT(eventHasChanged(int,bool)));
88 connect(favsTabContainer, SIGNAL(eventHasChanged(int,bool)), SLOT(eventHasChanged(int,bool)));
89 connect(tracksTabContainer, SIGNAL(eventHasChanged(int,bool)), SLOT(eventHasChanged(int,bool)));
90 connect(roomsTabContainer, SIGNAL(eventHasChanged(int,bool)), SLOT(eventHasChanged(int,bool)));
91 connect(nowTabContainer, SIGNAL(eventHasChanged(int,bool)), SLOT(eventHasChanged(int,bool)));
92 connect(searchTabContainer, SIGNAL(eventHasChanged(int,bool)), SLOT(eventHasChanged(int,bool)));
94 connect(aboutAction, SIGNAL(triggered()), SLOT(aboutApp()));
95 connect(settingsAction, SIGNAL(triggered()), SLOT(setup()));
96 connect(conferencesAction, SIGNAL(triggered()), SLOT(showConferences()));
98 useConference(Conference::activeConference());
99 // optimization, see useConference() code
102 } catch (const OrmException& e) {
103 qDebug() << "OrmException:" << e.text();
107 // TODO: open conferences at startup?
110 tabWidget->setCurrentIndex(6); // 6 - conference tab
114 // open dialog for given Event ID
115 // this is used in case Alarm Dialog request application to start
120 EventDialog dialog(aEventId,this);
123 catch(OrmNoObjectException&) {} // just start application
124 catch(...) {} // just start application
127 connect(mNetworkAccessManager, SIGNAL(finished(QNetworkReply*)), SLOT(networkQueryFinished(QNetworkReply*)));
129 connect(mXmlParser, SIGNAL(parsingScheduleBegin()), conferenceModel, SLOT(newConferenceBegin()));
130 connect(mXmlParser, SIGNAL(parsingScheduleEnd(const QString&)), conferenceModel, SLOT(newConferenceEnd(const QString&)));
133 void MainWindow::aboutApp()
135 QDialog dialog(this);
138 ui.labDescription->setText(ui.labDescription->text().arg(qApp->applicationVersion()));
140 dialog.setFixedWidth(width());
145 void MainWindow::eventHasChanged(int aEventId, bool aReloadModel)
147 dayTabContainer->updateTreeViewModel(aEventId);
148 favsTabContainer->updateTreeViewModel(aEventId,aReloadModel);
149 tracksTabContainer->updateTreeViewModel(aEventId);
150 nowTabContainer->updateTreeViewModel(aEventId);
151 roomsTabContainer->updateTreeViewModel(aEventId);
152 searchTabContainer->updateTreeViewModel(aEventId);
155 void MainWindow::useConference(int id)
158 Conference::getById(Conference::activeConference()).update("active",0);
159 Conference new_active = Conference::getById(id);
160 new_active.update("active",1);
162 // looks like it does not work at n900
163 setWindowTitle(new_active.title());
166 // dont run initTabs() here
167 // it takes much CPU, making travelling between conferences in ConferenceEditor longer
168 // and is not seen in maemo WM anyway
169 // instead run it explicitly
171 // 2. when ConferenceEditor finished
172 // dont forget to protect the calls by try-catch!
174 // just in case, clear conference selection instead
177 // end of optimization
179 } catch (OrmException& e) {
180 // cannon set an active conference
187 void MainWindow::initTabs()
189 int confId = Conference::activeConference();
190 Conference active = Conference::getById(confId);
191 QDate startDate = active.start();
192 QDate endDate = active.end();
194 // 'dayNavigator' emits signal 'dateChanged' after setting valid START:END dates
195 dayTabContainer->setDates(startDate, endDate);
196 tracksTabContainer->setDates(startDate, endDate);
197 roomsTabContainer->setDates(startDate, endDate);
198 favsTabContainer->setDates(startDate, endDate);
199 searchTabContainer->setDates(startDate, endDate);
200 searchTabContainer->searchAgainClicked();
201 nowTabContainer->updateTreeView(QDate::currentDate());
204 void MainWindow::clearTabs()
206 dayTabContainer->clearModel();
207 tracksTabContainer->clearModel();
208 roomsTabContainer->clearModel();
209 favsTabContainer->clearModel();
210 searchTabContainer->clearModel();
211 searchTabContainer->searchAgainClicked();
212 nowTabContainer->clearModel();
215 void MainWindow::unsetConference()
218 setWindowTitle(saved_title);
221 void MainWindow::setup()
223 SettingsDialog dialog;
224 dialog.loadDialogData();
225 if (dialog.exec() == QDialog::Accepted) {
226 dialog.saveDialogData();
228 AppSettings::isDirectConnection() ? QNetworkProxy::NoProxy : QNetworkProxy::HttpProxy,
229 AppSettings::proxyAddress(),
230 AppSettings::proxyPort(),
233 QNetworkProxy::setApplicationProxy(proxy);
237 /** Create and run ConferenceEditor dialog, making required connections for it.
239 This method manages, which classes actually perform changes in conference list.
241 There are several classes that modify the conferences:
243 deletion and URL update.
244 this, mXmlParser and mNetworkAccessManager:
245 addition and refresh.
247 void MainWindow::showConferences()
249 ConferenceEditor dialog(conferenceModel, this);
251 connect(&dialog, SIGNAL(haveConferenceUrl(const QString&)), SLOT(importFromNetwork(const QString&)));
252 connect(&dialog, SIGNAL(haveConferenceFile(const QString&)), SLOT(importFromFile(const QString&)));
253 connect(&dialog, SIGNAL(removeConferenceRequested(int)), SLOT(removeConference(int)));
254 connect(&dialog, SIGNAL(changeUrlRequested(int, const QString&)),
255 SLOT(changeConferenceUrl(int, const QString&)));
257 connect(&dialog, SIGNAL(haveConferenceSelected(int)), SLOT(useConference(int)));
258 connect(&dialog, SIGNAL(noneConferenceSelected()), SLOT(unsetConference()));
260 connect(mXmlParser, SIGNAL(parsingScheduleBegin()), &dialog, SLOT(importStarted()));
261 connect(mXmlParser, SIGNAL(progressStatus(int)), &dialog, SLOT(showParsingProgress(int)));
262 connect(mXmlParser, SIGNAL(parsingScheduleEnd(const QString&)), &dialog, SLOT(importFinished(const QString&)));
264 connect(this, SIGNAL(conferenceRemoved()), &dialog, SLOT(conferenceRemoved()));
268 // optimization, see useConference() code
271 } catch (OrmException) {
276 void MainWindow::networkQueryFinished(QNetworkReply *aReply)
278 if ( aReply->error() != QNetworkReply::NoError )
280 error_message(QString("Error occured during download: ") + aReply->errorString());
284 importData(aReply->readAll(), aReply->url().toEncoded());
288 void MainWindow::importData(const QByteArray &aData, const QString& url)
290 mXmlParser->parseData(aData, url);
293 void MainWindow::importFromNetwork(const QString& url)
295 QNetworkRequest request;
296 request.setUrl(QUrl(url));
298 mNetworkAccessManager->setProxy(QNetworkProxy::applicationProxy());
299 mNetworkAccessManager->get(request);
302 void MainWindow::importFromFile(const QString& filename)
304 QFile file(filename);
305 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
306 static const QString format("Cannot read \"%1\": error %2");
307 error_message(format.arg(filename, QString::number(file.error())));
310 importData(file.readAll(), "");
313 void MainWindow::removeConference(int id)
315 Conference::deleteConference(id);
316 conferenceModel->conferenceRemoved();
318 emit conferenceRemoved();
321 void MainWindow::changeConferenceUrl(int id, const QString& url)
323 Conference::getById(id).setUrl(url);