1/* This file is part of the KDE project
2 Copyright (C) 2010 David Faure <faure@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18 */
19
20#include "htmlextension.h"
21
22#include "part.h"
23
24#include <kglobal.h>
25#include <klocalizedstring.h>
26
27
28using namespace KParts;
29
30KParts::HtmlExtension::HtmlExtension(KParts::ReadOnlyPart* parent)
31 : QObject(parent), d(0)
32{
33}
34
35KParts::HtmlExtension::~HtmlExtension()
36{
37}
38
39bool HtmlExtension::hasSelection() const
40{
41 return false;
42}
43
44HtmlExtension * KParts::HtmlExtension::childObject( QObject *obj )
45{
46 return KGlobal::findDirectChild<KParts::HtmlExtension *>(obj);
47}
48
49SelectorInterface::QueryMethods SelectorInterface::supportedQueryMethods() const
50{
51 return KParts::SelectorInterface::None;
52}
53
54class SelectorInterface::ElementPrivate : public QSharedData
55{
56public:
57 QString tag;
58 QHash<QString, QString> attributes;
59};
60
61SelectorInterface::Element::Element()
62 : d(new ElementPrivate)
63{
64}
65
66SelectorInterface::Element::Element(const SelectorInterface::Element& other)
67 : d(other.d)
68{
69}
70
71SelectorInterface::Element::~Element()
72{
73}
74
75bool SelectorInterface::Element::isNull() const
76{
77 return d->tag.isNull();
78}
79
80void SelectorInterface::Element::setTagName(const QString& tag)
81{
82 d->tag = tag;
83}
84
85QString SelectorInterface::Element::tagName() const
86{
87 return d->tag;
88}
89
90void SelectorInterface::Element::setAttribute(const QString& name, const QString& value)
91{
92 d->attributes[name] = value; // insert or replace
93}
94
95QStringList SelectorInterface::Element::attributeNames() const
96{
97 return d->attributes.keys();
98}
99
100QString SelectorInterface::Element::attribute(const QString& name, const QString& defaultValue) const
101{
102 return d->attributes.value(name, defaultValue);
103}
104
105bool SelectorInterface::Element::hasAttribute(const QString& name) const
106{
107 return d->attributes.contains(name);
108}
109
110const char* HtmlSettingsInterface::javascriptAdviceToText(HtmlSettingsInterface::JavaScriptAdvice advice)
111{
112 // NOTE: The use of I18N_NOOP below is intended to allow GUI code to call
113 // i18n on the returned text without affecting use of untranslated text in
114 // config files.
115 switch (advice) {
116 case JavaScriptAccept:
117 return I18N_NOOP("Accept");
118 case JavaScriptReject:
119 return I18N_NOOP("Reject");
120 default:
121 break;
122 }
123
124 return 0;
125}
126
127HtmlSettingsInterface::JavaScriptAdvice HtmlSettingsInterface::textToJavascriptAdvice(const QString& text)
128{
129 JavaScriptAdvice ret = JavaScriptDunno;
130
131 if (!text.isEmpty()) {
132 if (text.compare(QLatin1String("accept"), Qt::CaseInsensitive) == 0) {
133 ret = JavaScriptAccept;
134 } else if (text.compare(QLatin1String("reject"), Qt::CaseInsensitive) == 0) {
135 ret = JavaScriptReject;
136 }
137 }
138
139 return ret;
140}
141
142void HtmlSettingsInterface::splitDomainAdvice(const QString& adviceStr, QString& domain, HtmlSettingsInterface::JavaScriptAdvice& javaAdvice, HtmlSettingsInterface::JavaScriptAdvice& javaScriptAdvice)
143{
144 const QString tmp(adviceStr);
145 const int splitIndex = tmp.indexOf(':');
146
147 if (splitIndex == -1) {
148 domain = adviceStr.toLower();
149 javaAdvice = JavaScriptDunno;
150 javaScriptAdvice = JavaScriptDunno;
151 } else {
152 domain = tmp.left(splitIndex).toLower();
153 const QString adviceString = tmp.mid(splitIndex+1, tmp.length());
154 const int splitIndex2 = adviceString.indexOf(QLatin1Char(':'));
155 if (splitIndex2 == -1) {
156 // Java advice only
157 javaAdvice = textToJavascriptAdvice(adviceString);
158 javaScriptAdvice = JavaScriptDunno;
159 } else {
160 // Java and JavaScript advice
161 javaAdvice = textToJavascriptAdvice(adviceString.left(splitIndex2));
162 javaScriptAdvice = textToJavascriptAdvice(adviceString.mid(splitIndex2+1,
163 adviceString.length()));
164 }
165 }
166}
167