Added localization
[quandoparte] / application / stationview.cpp
1 /*
2
3 Copyright (C) 2011 Luciano Montanaro <mikelima@cirulla.net>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING.  If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19
20 */
21
22 #include "stationview.h"
23
24 #include <QAction>
25 #include <QActionGroup>
26 #include <QDebug>
27 #include <QMenu>
28 #include <QMenuBar>
29 #include <QWebElement>
30 #include <QWebFrame>
31 #include <QWebView>
32
33 StationView::StationView(QWidget *parent) :
34     QMainWindow(parent),
35     theStation(""),
36     showArrivalsAction(new QAction(tr("Arrivals"), this)),
37     showDeparturesAction(new QAction(tr("Departures"), this)),
38     showSettingsAction(new QAction(tr("Settings"), this)),
39     showStationListSelectAction(new QAction(tr("Change Station"), this)),
40     showAboutAction(new QAction(tr("About"), this)),
41     viewSelectionGroup(new QActionGroup(this)),
42     menuBar(new QMenuBar(this)),
43     menu(new QMenu(menuBar)),
44     view(new QWebView(this))
45 {
46     showArrivalsAction->setCheckable(true);
47     showDeparturesAction->setCheckable(true);
48     showDeparturesAction->setChecked(true);
49     viewSelectionGroup->addAction(showArrivalsAction);
50     viewSelectionGroup->addAction(showDeparturesAction);
51     menu->addAction(showDeparturesAction);
52     menu->addAction(showArrivalsAction);
53     menu->addAction(showStationListSelectAction);
54     menu->addAction(showSettingsAction);
55     menu->addAction(showAboutAction);
56     menuBar->addAction(menu->menuAction());
57     setMenuBar(menuBar);
58     view->setTextSizeMultiplier(2.0);
59     view->setBackgroundRole(QPalette::Window);
60     connect(showAboutAction, SIGNAL(triggered()), this, SIGNAL(aboutTriggered()));
61     connect(showSettingsAction, SIGNAL(triggered()), this, SIGNAL(settingsTriggered()));
62     connect(showStationListSelectAction, SIGNAL(triggered()), this, SIGNAL(stationListSelectTriggered()));
63     connect(viewSelectionGroup, SIGNAL(triggered(QAction *)), this, SLOT(viewSelectionGroupTriggered(QAction *)));
64     setCentralWidget(view);
65 #ifdef Q_WS_MAEMO_5
66     setAttribute(Qt::WA_Maemo5StackedWindow);
67     setAttribute(Qt::WA_Maemo5AutoOrientation);
68 #endif
69 }
70
71 void StationView::setStation(const QString &station)
72 {
73     setWindowTitle(station);
74     theStation = station;
75 }
76
77 void StationView::changeView(void)
78 {
79     qDebug() << "View Changed";
80     if (showArrivalsAction->isChecked()) {
81         qDebug() << "Showing Arrivals";
82     } else {
83         qDebug() << "Showing Departures";
84     }
85 }
86
87 void StationView::setBaseUrl(const QUrl &baseUrl)
88 {
89     theBaseUrl = baseUrl;
90 }
91
92 void StationView::updateView(const QByteArray &page)
93 {
94     qDebug() << page;
95     updateCss();
96     view->setContent(page, "text/html", theBaseUrl);
97     QWebElement doc = view->page()->mainFrame()->documentElement();
98
99     // Find the first div
100     QWebElement current = doc.findFirst("div");
101
102     qDebug() << "skipping to the departures";
103     // Skip to the first div of class corpocentrale, which contains the first
104     // departure-related contents
105     while (!current.classes().contains("corpocentrale")) {
106         current = current.nextSibling();
107         qDebug() << "skipping to the next element";
108         if (current.isNull())
109             break;
110     }
111     // Mark every div as a departure class element; the next corpocentrale
112     // marks the start of the arrivals section
113     qDebug() << "marking departures";
114     do {
115         current.addClass("departures");
116         current = current.nextSibling();
117         qDebug() << "marking as departures";
118         if (current.isNull())
119             break;
120     } while (!current.classes().contains("corpocentrale"));
121     // Mark everything as an arrival, until reaching the footer
122     while (!current.classes().contains("footer")) {
123         current.addClass("arrivals");
124         current = current.nextSibling();
125         qDebug() << "marking as arrival";
126         if (current.isNull())
127             break;
128     }
129 }
130
131 void StationView::viewSelectionGroupTriggered(QAction *action)
132 {
133     if (action == showArrivalsAction) {
134         emit showingArrivalsChanged(true);
135     } else {
136         emit showingArrivalsChanged(false);
137     }
138     updateCss();
139 }
140
141 void StationView::updateCss(void)
142 {
143     QByteArray styleSheet("data:text/css;charset=utf-8,base64,");
144     QByteArray styleSheetText = QByteArray(
145                 "body {\n"
146                 "-webkit-user-select: none ! important;\n"
147                 "background-color: black ! important;\n"
148                 "color: white ! important;}\n"
149                 ".testata_red {visibility: hidden ! important;}\n"
150                 "#footer {\n"
151                 "visibility: hidden ! important;}\n");
152     styleSheet += styleSheetText.toBase64();
153     qDebug() << styleSheet;
154     //QUrl cssUrl = QUrl::fromEncoded(styleSheet);
155     QUrl cssUrl;
156
157     // XXX Maemo5 specific
158     if (showArrivalsAction->isChecked()) {
159         cssUrl.setEncodedUrl("file:///opt/usr/share/apps/quandoparte/css/arrivals.css");
160     } else {
161         cssUrl.setEncodedUrl("file:///opt/usr/share/apps/quandoparte/css/departures.css");
162     }
163     QWebSettings::globalSettings()->setUserStyleSheetUrl(cssUrl);
164 }