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