move Settings and About to Window Menu
[toast/confclerk.git] / src / gui / mainwindow.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 "mainwindow.h"
20
21 #include <QTreeView>
22 #include <QFile>
23 #include <QNetworkProxy>
24
25 #include <sqlengine.h>
26
27 #include <track.h>
28 #include <eventmodel.h>
29 #include <delegate.h>
30
31 #include <conference.h>
32
33 #include <QDialog>
34 #include <QMessageBox>
35 #include "ui_about.h"
36 #include <eventdialog.h>
37 #include "daynavigatorwidget.h"
38 #include "importschedulewidget.h"
39 #include "mapwindow.h"
40 #include "settingsdialog.h"
41
42 #include <tabcontainer.h>
43 #include <appsettings.h>
44
45 const QString PROXY_USERNAME;
46 const QString PROXY_PASSWD;
47
48 MainWindow::MainWindow(int aEventId, QWidget *aParent)
49     : QMainWindow(aParent)
50 {
51     setupUi(this);
52
53 #ifdef N810
54     tabWidget->setTabText(1,"Favs");
55     //tabWidget->setTabText(2,"Day");
56 #endif
57
58     // first time run aplication: -> let's have it direct connection in this case
59     if(!AppSettings::contains("proxyIsDirectConnection"))
60         AppSettings::setDirectConnection(true);
61
62     if(AppSettings::isDirectConnection())
63     {
64         qDebug() << "Setting-up proxy: " << AppSettings::proxyAddress() << ":" << AppSettings::proxyPort();
65     }
66     QNetworkProxy proxy(
67             AppSettings::isDirectConnection() ? QNetworkProxy::NoProxy : QNetworkProxy::HttpProxy,
68             AppSettings::proxyAddress(),
69             AppSettings::proxyPort(),
70             PROXY_USERNAME,
71             PROXY_PASSWD);
72     QNetworkProxy::setApplicationProxy(proxy);
73
74     int confId = Conference::activeConference();
75
76     connect(importScheduleWidget, SIGNAL(scheduleImported(int)), SLOT(scheduleImported(int)));
77
78     // event details have changed
79     connect(dayTabContainer, SIGNAL(eventHasChanged(int,bool)), SLOT(eventHasChanged(int,bool)));
80     connect(favsTabContainer, SIGNAL(eventHasChanged(int,bool)), SLOT(eventHasChanged(int,bool)));
81     connect(tracksTabContainer, SIGNAL(eventHasChanged(int,bool)), SLOT(eventHasChanged(int,bool)));
82     connect(roomsTabContainer, SIGNAL(eventHasChanged(int,bool)), SLOT(eventHasChanged(int,bool)));
83     connect(nowTabContainer, SIGNAL(eventHasChanged(int,bool)), SLOT(eventHasChanged(int,bool)));
84     connect(searchTabContainer, SIGNAL(eventHasChanged(int,bool)), SLOT(eventHasChanged(int,bool)));
85
86     // event conference map button clicked
87     connect(showMapButton, SIGNAL(clicked()), SLOT(conferenceMapClicked()));
88
89     connect(aboutAction, SIGNAL(triggered()), SLOT(aboutApp()));
90     connect(settingsAction, SIGNAL(triggered()), SLOT(setup()));
91
92     selectConference->setDuplicatesEnabled(false);
93     int confCount = Conference::getAll().count();
94     if(confCount)
95     {
96         initTabs();
97         fillAndShowConferenceHeader();
98         setWindowTitle(Conference::getById(confId).title());
99
100         if(confCount==1) // don't have to show 'selectConference' widget, if there is only one conference in the DB
101             selectConferenceWidget->hide();
102         else
103         {
104             // have to fill comboBox with available conferences
105             QList<Conference> confs = Conference::getAll();
106             QListIterator<Conference> i(confs);
107             while(i.hasNext())
108             {
109                 Conference conf = i.next();
110                 selectConference->addItem(conf.title(),conf.id());
111             }
112             int idx = selectConference->findText(Conference::getById(Conference::activeConference()).title());
113             selectConference->setCurrentIndex(idx);
114         }
115         connect(selectConference, SIGNAL(currentIndexChanged(int)), SLOT(conferenceChanged(int)));
116     }
117     else
118     {
119         conferenceHeader->hide();
120         selectConferenceWidget->hide();
121         // go to the 'conferenceTab', so the user can import the schedule
122         tabWidget->setCurrentIndex(6); // 6 - conference tab
123     }
124
125     // open dialog for given Event ID
126     // this is used in case Alarm Dialog request application to start
127     if(aEventId)
128     {
129         try
130         {
131             EventDialog dialog(aEventId,this);
132             dialog.exec();
133         }
134         catch(OrmNoObjectException&) {} // just start application
135         catch(...) {} // just start application
136     }
137 }
138
139 void MainWindow::scheduleImported(int aConfId)
140 {
141     Q_UNUSED(aConfId);
142
143     Conference conf = Conference::getById(aConfId);
144     if( selectConference->findText(conf.title()) < 0 ) // item doesn't exist
145     {
146         disconnect(selectConference, SIGNAL(currentIndexChanged(int)), this, SLOT(conferenceChanged(int)));
147         selectConference->addItem(conf.title(),conf.id());
148         connect(selectConference, SIGNAL(currentIndexChanged(int)), SLOT(conferenceChanged(int)));
149     }
150     int confCount = Conference::getAll().count();
151     if(confCount)
152     {
153         int idx = selectConference->findText(conf.title());
154         selectConference->setCurrentIndex(idx);
155
156         if(confCount>1)
157             selectConferenceWidget->show();
158
159         conferenceChanged(idx);
160     }
161 }
162
163 void MainWindow::aboutApp()
164 {
165     QDialog dialog(this);
166     Ui::AboutDialog ui;
167     ui.setupUi(&dialog);
168 #ifdef N810
169     dialog.setFixedWidth(width());
170 #endif
171     dialog.exec();
172 }
173
174 void MainWindow::conferenceMapClicked()
175 {
176     QString mapPath = QString(":/maps/campus.png");
177     if(!QFile::exists(mapPath))
178         mapPath = QString(":/maps/rooms/not-available.png");
179
180     QString roomName;
181
182     QPixmap map(mapPath);
183     MapWindow window(map,roomName,this);
184     window.exec();
185 }
186
187 void MainWindow::eventHasChanged(int aEventId, bool aReloadModel)
188 {
189     dayTabContainer->updateTreeViewModel(aEventId);
190     favsTabContainer->updateTreeViewModel(aEventId,aReloadModel);
191     tracksTabContainer->updateTreeViewModel(aEventId);
192     nowTabContainer->updateTreeViewModel(aEventId);
193     roomsTabContainer->updateTreeViewModel(aEventId);
194     searchTabContainer->updateTreeViewModel(aEventId);
195 }
196
197 void MainWindow::fillAndShowConferenceHeader()
198 {
199     int confId = Conference::activeConference();
200     conferenceTitle->setText(Conference::getById(confId).title());
201     conferenceSubtitle->setText(Conference::getById(confId).subtitle());
202     conferenceWhere->setText(Conference::getById(confId).city() + ", " + Conference::getById(confId).venue());
203     conferenceWhen->setText(
204             Conference::getById(confId).start().toString("dd-MM-yyyy")
205             + ", " +
206             Conference::getById(confId).end().toString("dd-MM-yyyy"));
207     conferenceHeader->show();
208 }
209
210 void MainWindow::initTabs()
211 {
212     int confId = Conference::activeConference();
213     QDate startDate = Conference::getById(confId).start();
214     QDate endDate = Conference::getById(confId).end();
215
216     // 'dayNavigator' emits signal 'dateChanged' after setting valid START:END dates
217     dayTabContainer->setDates(startDate, endDate);
218     tracksTabContainer->setDates(startDate, endDate);
219     roomsTabContainer->setDates(startDate, endDate);
220     favsTabContainer->setDates(startDate, endDate);
221     searchTabContainer->setDates(startDate, endDate);
222     searchTabContainer->searchAgainClicked();
223     nowTabContainer->updateTreeView(QDate::currentDate());
224 }
225
226 void MainWindow::conferenceChanged(int aIndex)
227 {
228     Conference::getById(Conference::activeConference()).update("active",0);
229     Conference::getById(selectConference->itemData(aIndex).toInt()).update("active",1);
230
231     initTabs();
232     fillAndShowConferenceHeader();
233     setWindowTitle(Conference::getById(Conference::activeConference()).title());
234 }
235
236 void MainWindow::setup()
237 {
238     SettingsDialog dialog;
239     dialog.exec();
240
241     qDebug() << "Setting-up proxy: " << AppSettings::proxyAddress() << ":" << AppSettings::proxyPort();
242     QNetworkProxy proxy(
243             AppSettings::isDirectConnection() ? QNetworkProxy::NoProxy : QNetworkProxy::HttpProxy,
244             AppSettings::proxyAddress(),
245             AppSettings::proxyPort(),
246             PROXY_USERNAME,
247             PROXY_PASSWD);
248     QNetworkProxy::setApplicationProxy(proxy);
249 }