source: src/gui/mainwindow.cpp @ 1ca13ef

qt5
Last change on this file since 1ca13ef was 1ca13ef, checked in by Philipp Spitzer <philipp@…>, 11 years ago

Implemented the reload button functionality. Closes: #34

  • Property mode set to 100644
File size: 10.7 KB
Line 
1/*
2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011 Philipp Spitzer, gregor herrmann
4 *
5 * This file is part of ConfClerk.
6 *
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)
10 * any later version.
11 *
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
15 * more details.
16 *
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/>.
19 */
20#include "mainwindow.h"
21
22#include <QTreeView>
23#include <QFile>
24#include <QNetworkProxy>
25#include <QNetworkAccessManager>
26#include <QNetworkReply>
27
28#include <sqlengine.h>
29
30#include <track.h>
31#include <eventmodel.h>
32#include <delegate.h>
33
34#include <conference.h>
35
36#include <QDialog>
37#include <QMessageBox>
38
39#include "ui_about.h"
40#include <eventdialog.h>
41#include "daynavigatorwidget.h"
42#include "settingsdialog.h"
43#include "conferenceeditor.h"
44#include "schedulexmlparser.h"
45#include "errormessage.h"
46
47#include <tabcontainer.h>
48#include <appsettings.h>
49
50const QString PROXY_USERNAME;
51const QString PROXY_PASSWD;
52
53MainWindow::MainWindow(int aEventId, QWidget *aParent)
54    : QMainWindow(aParent)
55    , conferenceModel(new ConferenceModel(this))
56    , mXmlParser(new ScheduleXmlParser(this))
57    , mNetworkAccessManager(new QNetworkAccessManager(this))
58{
59    setupUi(this);
60
61    saved_title = windowTitle();
62
63#ifdef N810
64    tabWidget->setTabText(1,"Favs");
65    //tabWidget->setTabText(2,"Day");
66#endif
67
68    // first time run aplication: -> let's have it direct connection in this case
69    if(!AppSettings::contains("proxyIsDirectConnection"))
70        AppSettings::setDirectConnection(true);
71
72    /*
73    if(AppSettings::isDirectConnection())
74    {
75        qDebug() << "Setting-up proxy: " << AppSettings::proxyAddress() << ":" << AppSettings::proxyPort();
76    }
77    */
78    QNetworkProxy proxy(
79            AppSettings::isDirectConnection() ? QNetworkProxy::NoProxy : QNetworkProxy::HttpProxy,
80            AppSettings::proxyAddress(),
81            AppSettings::proxyPort(),
82            PROXY_USERNAME,
83            PROXY_PASSWD);
84    QNetworkProxy::setApplicationProxy(proxy);
85
86    // event details have changed
87    connect(dayTabContainer, SIGNAL(eventChanged(int,bool)), SLOT(onEventChanged(int,bool)));
88    connect(favsTabContainer, SIGNAL(eventChanged(int,bool)), SLOT(onEventChanged(int,bool)));
89    connect(tracksTabContainer, SIGNAL(eventChanged(int,bool)), SLOT(onEventChanged(int,bool)));
90    connect(roomsTabContainer, SIGNAL(eventChanged(int,bool)), SLOT(onEventChanged(int,bool)));
91    connect(searchTabContainer, SIGNAL(eventChanged(int,bool)), SLOT(onEventChanged(int,bool)));
92
93    // date has changed
94    connect(dayNavigator, SIGNAL(dateChanged(QDate)), dayTabContainer, SLOT(redisplayDate(QDate)));
95    connect(dayNavigator, SIGNAL(dateChanged(QDate)), favsTabContainer, SLOT(redisplayDate(QDate)));
96    connect(dayNavigator, SIGNAL(dateChanged(QDate)), tracksTabContainer, SLOT(redisplayDate(QDate)));
97    connect(dayNavigator, SIGNAL(dateChanged(QDate)), roomsTabContainer, SLOT(redisplayDate(QDate)));
98    connect(dayNavigator, SIGNAL(dateChanged(QDate)), searchTabContainer, SLOT(redisplayDate(QDate)));
99
100    useConference(Conference::activeConference());
101    // optimization, see useConference() code
102    try {
103        initTabs();
104    } catch (const OrmException& e) {
105        qDebug() << "OrmException:" << e.text();
106        clearTabs();
107    }
108
109    // open dialog for given Event ID
110    // this is used in case Alarm Dialog request application to start
111    if(aEventId)
112    {
113        try
114        {
115            EventDialog dialog(aEventId,this);
116            dialog.exec();
117        }
118        catch(OrmNoObjectException&) {} // just start application
119        catch(...) {} // just start application
120    }
121
122    connect(mNetworkAccessManager, SIGNAL(finished(QNetworkReply*)), SLOT(networkQueryFinished(QNetworkReply*)));
123
124    connect(mXmlParser, SIGNAL(parsingScheduleBegin()), conferenceModel, SLOT(newConferenceBegin()));
125    connect(mXmlParser, SIGNAL(parsingScheduleEnd(const QString&)), conferenceModel, SLOT(newConferenceEnd(const QString&)));
126}
127
128void MainWindow::on_aboutAction_triggered()
129{
130    QDialog dialog(this);
131    Ui::AboutDialog ui;
132    ui.setupUi(&dialog);
133    ui.labDescription->setText(ui.labDescription->text().arg(qApp->applicationVersion()));
134#ifdef N810
135    dialog.setFixedWidth(width());
136#endif
137    dialog.exec();
138}
139
140
141void MainWindow::on_reloadAction_triggered() {
142    int confId = Conference::activeConference();
143    if (confId== -1) return;
144    Conference active = Conference::getById(confId);
145    if (active.url().isEmpty()) return;
146    importFromNetwork(active.url());
147    setEnabled(false);
148}
149
150
151void MainWindow::on_nowAction_triggered() {
152
153}
154
155
156void MainWindow::on_searchAction_triggered() {
157    searchTabContainer->showSearchDialog();
158    tabWidget->setCurrentWidget(searchTab);
159}
160
161
162void MainWindow::onEventChanged(int aEventId, bool favouriteChanged) {
163    dayTabContainer->redisplayEvent(aEventId);
164    if (favouriteChanged) favsTabContainer->redisplayDate(dayNavigator->curDate());
165    else favsTabContainer->redisplayEvent(aEventId);
166    tracksTabContainer->redisplayEvent(aEventId);
167    roomsTabContainer->redisplayEvent(aEventId);
168    searchTabContainer->redisplayEvent(aEventId);
169}
170
171
172void MainWindow::useConference(int id)
173{
174    if (id == -1)  // in case no conference is active
175    {
176        unsetConference();
177        return;
178    }
179    try {
180        Conference::getById(Conference::activeConference()).update("active",0);
181        Conference new_active = Conference::getById(id);
182        new_active.update("active",1);
183
184        // looks like it does not work at n900
185        setWindowTitle(new_active.title());
186
187        // optimization.
188        // dont run initTabs() here
189        // it takes much CPU, making travelling between conferences in ConferenceEditor longer
190        // and is not seen in maemo WM anyway
191        // instead run it explicitly
192        // 1. at startup
193        // 2. when ConferenceEditor finished
194        // dont forget to protect the calls by try-catch!
195
196        // just in case, clear conference selection instead
197        clearTabs();
198
199        // end of optimization
200        // initTabs();
201    } catch (OrmException& e) {
202        // cannon set an active conference
203        unsetConference();   // TODO: as no active conference is now correctly managed this should be handled as a fatal error
204        return;
205    }
206
207}
208
209void MainWindow::initTabs()
210{
211    int confId = Conference::activeConference();
212    if (confId != -1)   // only init tabs if a conference is active
213    {
214        Conference active = Conference::getById(confId);
215        QDate startDate = active.start();
216        QDate endDate = active.end();
217
218        // 'dayNavigator' emits signal 'dateChanged' after setting valid START:END dates
219        dayNavigator->setDates(startDate, endDate);
220    }
221}
222
223void MainWindow::clearTabs()
224{
225    dayTabContainer->clearModel();
226    tracksTabContainer->clearModel();
227    roomsTabContainer->clearModel();
228    favsTabContainer->clearModel();
229    searchTabContainer->clearModel();
230}
231
232void MainWindow::unsetConference()
233{
234    clearTabs();
235    dayNavigator->unsetDates();
236    setWindowTitle(saved_title);
237}
238
239void MainWindow::on_settingsAction_triggered()
240{
241    SettingsDialog dialog;
242    dialog.loadDialogData();
243    if (dialog.exec() == QDialog::Accepted) {
244        dialog.saveDialogData();
245        QNetworkProxy proxy(
246                AppSettings::isDirectConnection() ? QNetworkProxy::NoProxy : QNetworkProxy::HttpProxy,
247                AppSettings::proxyAddress(),
248                AppSettings::proxyPort(),
249                PROXY_USERNAME,
250                PROXY_PASSWD);
251        QNetworkProxy::setApplicationProxy(proxy);
252    }
253}
254
255/** Create and run ConferenceEditor dialog, making required connections for it.
256
257This method manages, which classes actually perform changes in conference list.
258
259There are several classes that modify the conferences:
260this:
261 deletion and URL update.
262this, mXmlParser and mNetworkAccessManager:
263 addition and refresh.
264*/
265void MainWindow::on_conferencesAction_triggered()
266{
267    ConferenceEditor dialog(conferenceModel, this);
268
269    connect(&dialog, SIGNAL(haveConferenceUrl(const QString&)), SLOT(importFromNetwork(const QString&)));
270    connect(&dialog, SIGNAL(haveConferenceFile(const QString&)), SLOT(importFromFile(const QString&)));
271    connect(&dialog, SIGNAL(removeConferenceRequested(int)), SLOT(removeConference(int)));
272    connect(&dialog, SIGNAL(changeUrlRequested(int, const QString&)),
273                    SLOT(changeConferenceUrl(int, const QString&)));
274
275    connect(&dialog, SIGNAL(haveConferenceSelected(int)), SLOT(useConference(int)));
276    connect(&dialog, SIGNAL(noneConferenceSelected()), SLOT(unsetConference()));
277
278    connect(mXmlParser, SIGNAL(parsingScheduleBegin()), &dialog, SLOT(importStarted()));
279    connect(mXmlParser, SIGNAL(progressStatus(int)), &dialog, SLOT(showParsingProgress(int)));
280    connect(mXmlParser, SIGNAL(parsingScheduleEnd(const QString&)), &dialog, SLOT(importFinished(const QString&)));
281
282    connect(this, SIGNAL(conferenceRemoved()), &dialog, SLOT(conferenceRemoved()));
283
284    dialog.exec();
285
286    // optimization, see useConference() code
287    try {
288        initTabs();
289    } catch (OrmException) {
290        clearTabs();
291    }
292}
293
294void MainWindow::networkQueryFinished(QNetworkReply *aReply)
295{
296    if ( aReply->error() != QNetworkReply::NoError )
297    {
298        error_message(QString("Error occured during download: ") + aReply->errorString());
299    }
300    else
301    {
302        importData(aReply->readAll(), aReply->url().toEncoded());
303    }
304    setEnabled(true);
305}
306
307void MainWindow::importData(const QByteArray &aData, const QString& url)
308{
309    mXmlParser->parseData(aData, url);
310}
311
312void MainWindow::importFromNetwork(const QString& url)
313{
314    QNetworkRequest request;
315    request.setUrl(QUrl(url));
316
317    mNetworkAccessManager->setProxy(QNetworkProxy::applicationProxy());
318    mNetworkAccessManager->get(request);
319}
320
321void MainWindow::importFromFile(const QString& filename)
322{
323    QFile file(filename);
324    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {   
325        static const QString format("Cannot read \"%1\": error %2");
326        error_message(format.arg(filename, QString::number(file.error())));
327    }
328
329    importData(file.readAll(), "");
330}
331
332void MainWindow::removeConference(int id)
333{
334    Conference::deleteConference(id);
335    conferenceModel->conferenceRemoved();
336
337    emit conferenceRemoved();
338}
339
340void MainWindow::changeConferenceUrl(int id, const QString& url)
341{
342    Conference::getById(id).setUrl(url);
343}
344
Note: See TracBrowser for help on using the repository browser.