• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
3  * Copyright (C) 2009 Girish Ramakrishnan <girish@forwardbias.in>
4  * Copyright (C) 2006 George Staikos <staikos@kde.org>
5  * Copyright (C) 2006 Dirk Mueller <mueller@kde.org>
6  * Copyright (C) 2006 Zack Rusin <zack@kde.org>
7  * Copyright (C) 2006 Simon Hausmann <hausmann@kde.org>
8  *
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
21  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
28  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "mainwindow.h"
34 
35 #include "locationedit.h"
36 #include "utils.h"
37 
MainWindow()38 MainWindow::MainWindow()
39     : m_page(new WebPage(this))
40     , m_toolBar(0)
41     , urlEdit(0)
42 {
43     setAttribute(Qt::WA_DeleteOnClose);
44     if (qgetenv("QTTESTBROWSER_USE_ARGB_VISUALS").toInt() == 1)
45         setAttribute(Qt::WA_TranslucentBackground);
46 
47     buildUI();
48 }
49 
buildUI()50 void MainWindow::buildUI()
51 {
52 #if defined(Q_OS_SYMBIAN)
53     delete urlEdit;
54 #endif
55     delete m_toolBar;
56 
57     m_toolBar = addToolBar("Navigation");
58 #if defined(Q_OS_SYMBIAN)
59     m_toolBar->setIconSize(QSize(16, 16));
60 #endif
61     QAction* reloadAction = page()->action(QWebPage::Reload);
62     connect(reloadAction, SIGNAL(triggered()), this, SLOT(changeLocation()));
63 
64     m_toolBar->addAction(page()->action(QWebPage::Back));
65     m_toolBar->addAction(page()->action(QWebPage::Forward));
66     m_toolBar->addAction(reloadAction);
67     m_toolBar->addAction(page()->action(QWebPage::Stop));
68 
69 #ifndef QT_NO_INPUTDIALOG
70     urlEdit = new LocationEdit(m_toolBar);
71     urlEdit->setSizePolicy(QSizePolicy::Expanding, urlEdit->sizePolicy().verticalPolicy());
72     connect(urlEdit, SIGNAL(returnPressed()), SLOT(changeLocation()));
73     QCompleter* completer = new QCompleter(m_toolBar);
74     urlEdit->setCompleter(completer);
75     completer->setModel(&urlModel);
76 #if defined(Q_OS_SYMBIAN)
77     addToolBarBreak();
78     addToolBar("Location")->addWidget(urlEdit);
79 #else
80     m_toolBar->addWidget(urlEdit);
81 #endif
82 
83     connect(page()->mainFrame(), SIGNAL(urlChanged(QUrl)), this, SLOT(setAddressUrl(QUrl)));
84     connect(page(), SIGNAL(loadProgress(int)), urlEdit, SLOT(setProgress(int)));
85 #endif
86 
87     connect(page()->mainFrame(), SIGNAL(loadStarted()), this, SLOT(onLoadStarted()));
88     connect(page()->mainFrame(), SIGNAL(iconChanged()), this, SLOT(onIconChanged()));
89     connect(page()->mainFrame(), SIGNAL(titleChanged(QString)), this, SLOT(onTitleChanged(QString)));
90     connect(page(), SIGNAL(windowCloseRequested()), this, SLOT(close()));
91 
92 #ifndef QT_NO_SHORTCUT
93     // short-cuts
94     page()->action(QWebPage::Back)->setShortcut(QKeySequence::Back);
95     page()->action(QWebPage::Stop)->setShortcut(Qt::Key_Escape);
96     page()->action(QWebPage::Forward)->setShortcut(QKeySequence::Forward);
97     page()->action(QWebPage::Reload)->setShortcut(QKeySequence::Refresh);
98 #ifndef QT_NO_UNDOSTACK
99     page()->action(QWebPage::Undo)->setShortcut(QKeySequence::Undo);
100     page()->action(QWebPage::Redo)->setShortcut(QKeySequence::Redo);
101 #endif
102     page()->action(QWebPage::Cut)->setShortcut(QKeySequence::Cut);
103     page()->action(QWebPage::Copy)->setShortcut(QKeySequence::Copy);
104     page()->action(QWebPage::Paste)->setShortcut(QKeySequence::Paste);
105     page()->action(QWebPage::SelectAll)->setShortcut(QKeySequence::SelectAll);
106 
107     page()->action(QWebPage::ToggleBold)->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_B));
108     page()->action(QWebPage::ToggleItalic)->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_I));
109     page()->action(QWebPage::ToggleUnderline)->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_U));
110 #endif
111 }
112 
setPage(WebPage * page)113 void MainWindow::setPage(WebPage* page)
114 {
115     if (page && m_page)
116         page->setUserAgent(m_page->userAgentForUrl(QUrl()));
117 
118     delete m_page;
119     m_page = page;
120 
121     buildUI();
122 }
123 
page() const124 WebPage* MainWindow::page() const
125 {
126     return m_page;
127 }
128 
setAddressUrl(const QUrl & url)129 void MainWindow::setAddressUrl(const QUrl& url)
130 {
131     setAddressUrl(url.toString(QUrl::RemoveUserInfo));
132 }
133 
setAddressUrl(const QString & url)134 void MainWindow::setAddressUrl(const QString& url)
135 {
136 #ifndef QT_NO_INPUTDIALOG
137     if (!url.contains("about:"))
138         urlEdit->setText(url);
139 #endif
140 }
141 
addCompleterEntry(const QUrl & url)142 void MainWindow::addCompleterEntry(const QUrl& url)
143 {
144     QUrl::FormattingOptions opts;
145     opts |= QUrl::RemoveScheme;
146     opts |= QUrl::RemoveUserInfo;
147     opts |= QUrl::StripTrailingSlash;
148     QString s = url.toString(opts);
149     s = s.mid(2);
150     if (s.isEmpty())
151         return;
152 
153     if (!urlList.contains(s))
154         urlList += s;
155     urlModel.setStringList(urlList);
156 }
157 
load(const QString & url)158 void MainWindow::load(const QString& url)
159 {
160     QUrl qurl = urlFromUserInput(url);
161     if (qurl.scheme().isEmpty())
162         qurl = QUrl("http://" + url + "/");
163     load(qurl);
164 }
165 
load(const QUrl & url)166 void MainWindow::load(const QUrl& url)
167 {
168     if (!url.isValid())
169         return;
170 
171     setAddressUrl(url.toString());
172     page()->mainFrame()->load(url);
173 }
174 
addressUrl() const175 QString MainWindow::addressUrl() const
176 {
177 #ifndef QT_NO_INPUTDIALOG
178     return urlEdit->text();
179 #endif
180     return QString();
181 }
182 
changeLocation()183 void MainWindow::changeLocation()
184 {
185 #ifndef QT_NO_INPUTDIALOG
186     QString string = urlEdit->text();
187     QUrl mainFrameURL = page()->mainFrame()->url();
188 
189     if (mainFrameURL.isValid() && string == mainFrameURL.toString()) {
190         page()->triggerAction(QWebPage::Reload);
191         return;
192     }
193 
194     load(string);
195 #endif
196 }
197 
openFile()198 void MainWindow::openFile()
199 {
200 #ifndef QT_NO_FILEDIALOG
201     static const QString filter("HTML Files (*.htm *.html);;Text Files (*.txt);;Image Files (*.gif *.jpg *.png);;All Files (*)");
202 
203     QFileDialog fileDialog(this, tr("Open"), QString(), filter);
204     fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
205     fileDialog.setFileMode(QFileDialog::ExistingFile);
206     fileDialog.setOptions(QFileDialog::ReadOnly);
207 
208     if (fileDialog.exec()) {
209         QString selectedFile = fileDialog.selectedFiles()[0];
210         if (!selectedFile.isEmpty())
211             load(QUrl::fromLocalFile(selectedFile));
212     }
213 #endif
214 }
215 
openLocation()216 void MainWindow::openLocation()
217 {
218 #ifndef QT_NO_INPUTDIALOG
219     urlEdit->selectAll();
220     urlEdit->setFocus();
221 #endif
222 }
223 
onIconChanged()224 void MainWindow::onIconChanged()
225 {
226 #ifndef QT_NO_INPUTDIALOG
227     urlEdit->setPageIcon(page()->mainFrame()->icon());
228 #endif
229 }
230 
onLoadStarted()231 void MainWindow::onLoadStarted()
232 {
233 #ifndef QT_NO_INPUTDIALOG
234     urlEdit->setPageIcon(QIcon());
235 #endif
236 }
237 
onTitleChanged(const QString & title)238 void MainWindow::onTitleChanged(const QString& title)
239 {
240     if (title.isEmpty())
241         setWindowTitle(QCoreApplication::applicationName());
242     else
243         setWindowTitle(QString::fromLatin1("%1 - %2").arg(title).arg(QCoreApplication::applicationName()));
244 }
245