1 /* 2 * Copyright (C) 2007 Apple Inc. All rights reserved. 3 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies) 4 * Copyright (C) 2008 Holger Hans Peter Freyther 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 16 * its contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include "config.h" 32 #include "InspectorClientQt.h" 33 34 #include "qwebinspector.h" 35 #include "qwebinspector_p.h" 36 #include "qwebpage.h" 37 #include "qwebpage_p.h" 38 #include "qwebview.h" 39 40 #include <QtCore/QSettings> 41 #include <QtCore/QCoreApplication> 42 43 #include "InspectorController.h" 44 #include "NotImplemented.h" 45 #include "Page.h" 46 #include "PlatformString.h" 47 48 namespace WebCore { 49 50 static const QLatin1String settingStoragePrefix("Qt/QtWebKit/QWebInspector/"); 51 static const QLatin1String settingStorageTypeSuffix(".type"); 52 53 static String variantToSetting(const QVariant& qvariant); 54 static QVariant settingToVariant(const String& value); 55 56 class InspectorClientWebPage : public QWebPage { 57 Q_OBJECT 58 friend class InspectorClientQt; 59 public: 60 InspectorClientWebPage(QObject* parent = 0) 61 : QWebPage(parent) 62 { 63 } 64 65 QWebPage* createWindow(QWebPage::WebWindowType) 66 { 67 QWebView* view = new QWebView; 68 QWebPage* page = new QWebPage; 69 view->setPage(page); 70 view->setAttribute(Qt::WA_DeleteOnClose); 71 return page; 72 } 73 }; 74 75 InspectorClientQt::InspectorClientQt(QWebPage* page) 76 : m_inspectedWebPage(page) 77 {} 78 79 void InspectorClientQt::inspectorDestroyed() 80 { 81 delete this; 82 } 83 84 Page* InspectorClientQt::createPage() 85 { 86 QWebView* inspectorView = new QWebView; 87 InspectorClientWebPage* inspectorPage = new InspectorClientWebPage(inspectorView); 88 inspectorView->setPage(inspectorPage); 89 m_inspectorView.set(inspectorView); 90 91 inspectorPage->mainFrame()->load(QString::fromLatin1("qrc:/webkit/inspector/inspector.html")); 92 m_inspectedWebPage->d->inspectorFrontend = inspectorView; 93 m_inspectedWebPage->d->getOrCreateInspector()->d->setFrontend(inspectorView); 94 95 return m_inspectorView->page()->d->page; 96 } 97 98 String InspectorClientQt::localizedStringsURL() 99 { 100 notImplemented(); 101 return String(); 102 } 103 104 String InspectorClientQt::hiddenPanels() 105 { 106 notImplemented(); 107 return String(); 108 } 109 110 void InspectorClientQt::showWindow() 111 { 112 updateWindowTitle(); 113 114 #if ENABLE(INSPECTOR) 115 m_inspectedWebPage->d->inspectorController()->setWindowVisible(true, true); 116 #endif 117 } 118 119 void InspectorClientQt::closeWindow() 120 { 121 #if ENABLE(INSPECTOR) 122 m_inspectedWebPage->d->inspectorController()->setWindowVisible(false); 123 #endif 124 } 125 126 void InspectorClientQt::attachWindow() 127 { 128 notImplemented(); 129 } 130 131 void InspectorClientQt::detachWindow() 132 { 133 notImplemented(); 134 } 135 136 void InspectorClientQt::setAttachedWindowHeight(unsigned) 137 { 138 notImplemented(); 139 } 140 141 void InspectorClientQt::highlight(Node*) 142 { 143 notImplemented(); 144 } 145 146 void InspectorClientQt::hideHighlight() 147 { 148 notImplemented(); 149 } 150 151 void InspectorClientQt::inspectedURLChanged(const String& newURL) 152 { 153 m_inspectedURL = newURL; 154 updateWindowTitle(); 155 } 156 157 void InspectorClientQt::inspectorWindowObjectCleared() 158 { 159 notImplemented(); 160 } 161 162 void InspectorClientQt::updateWindowTitle() 163 { 164 if (m_inspectedWebPage->d->inspector) { 165 QString caption = QCoreApplication::translate("QWebPage", "Web Inspector - %2").arg(m_inspectedURL); 166 m_inspectedWebPage->d->inspector->setWindowTitle(caption); 167 } 168 } 169 170 void InspectorClientQt::populateSetting(const String& key, String* setting) 171 { 172 QSettings qsettings; 173 if (qsettings.status() == QSettings::AccessError) { 174 // QCoreApplication::setOrganizationName and QCoreApplication::setApplicationName haven't been called 175 qWarning("QWebInspector: QSettings couldn't read configuration setting [%s].", 176 qPrintable(static_cast<QString>(key))); 177 return; 178 } 179 180 QString settingKey(settingStoragePrefix + key); 181 QString storedValueType = qsettings.value(settingKey + settingStorageTypeSuffix).toString(); 182 QVariant storedValue = qsettings.value(settingKey); 183 storedValue.convert(QVariant::nameToType(storedValueType.toAscii().data())); 184 *setting = variantToSetting(storedValue); 185 } 186 187 void InspectorClientQt::storeSetting(const String& key, const String& setting) 188 { 189 QSettings qsettings; 190 if (qsettings.status() == QSettings::AccessError) { 191 qWarning("QWebInspector: QSettings couldn't persist configuration setting [%s].", 192 qPrintable(static_cast<QString>(key))); 193 return; 194 } 195 196 QVariant valueToStore = settingToVariant(setting); 197 QString settingKey(settingStoragePrefix + key); 198 qsettings.setValue(settingKey, valueToStore); 199 qsettings.setValue(settingKey + settingStorageTypeSuffix, QVariant::typeToName(valueToStore.type())); 200 } 201 202 static String variantToSetting(const QVariant& qvariant) 203 { 204 String retVal; 205 206 switch (qvariant.type()) { 207 case QVariant::Bool: 208 retVal = qvariant.toBool() ? "true" : "false"; 209 case QVariant::String: 210 retVal = qvariant.toString(); 211 } 212 213 return retVal; 214 } 215 216 static QVariant settingToVariant(const String& setting) 217 { 218 QVariant retVal; 219 retVal.setValue(static_cast<QString>(setting)); 220 return retVal; 221 } 222 223 } 224 225 #include "InspectorClientQt.moc" 226