adapt opensearch reader and writer for xml suggestions
[rekonq:nikhilms-mainline.git] / src / opensearch / opensearchwriter.cpp
1 /* ============================================================
2 *
3 * This file is a part of the rekonq project
4 *
5 * Copyright (C) 2009 by Jakub Wieczorek <faw217@gmail.com>
6 * Copyright (C) 2010 by Lionel Chauvin <megabigbug@yahoo.fr>
7 * Copyright (C) 2010 by Andrea Diamantini <adjam7 at gmail dot com>
8 *
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License or (at your option) version 3 or any later version
14 * accepted by the membership of KDE e.V. (or its successor approved
15 * by the membership of KDE e.V.), which shall act as a proxy
16 * defined in Section 14 of version 3 of the license.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25 *
26 * ============================================================ */
27
28
29 // Self Includes
30 #include "opensearchwriter.h"
31
32 // Local Includes
33 #include "opensearchengine.h"
34
35 // KDE Includes
36 #include <KDebug>
37
38 // Qt Includes
39 #include <QtCore/QIODevice>
40
41
42
43 OpenSearchWriter::OpenSearchWriter()
44     : QXmlStreamWriter()
45 {
46     setAutoFormatting(true);
47 }
48
49
50 bool OpenSearchWriter::write(QIODevice *device, OpenSearchEngine *engine)
51 {
52     if (!engine)
53         return false;
54
55     if (!device->isOpen())
56         device->open(QIODevice::WriteOnly);
57
58     setDevice(device);
59     write(engine);
60     return true;
61 }
62
63
64 void OpenSearchWriter::write(OpenSearchEngine *engine)
65 {
66     writeStartDocument();
67     writeStartElement(QL1S("OpenSearchDescription"));
68     writeDefaultNamespace(QL1S("http://a9.com/-/spec/opensearch/1.1/"));
69
70     if (!engine->name().isEmpty()) 
71     {
72         writeTextElement(QL1S("ShortName"), engine->name());
73     }
74
75     if (!engine->description().isEmpty()) 
76     {
77         writeTextElement(QL1S("Description"), engine->description());
78     }
79
80     if (!engine->searchUrlTemplate().isEmpty()) 
81     {
82         writeStartElement(QL1S("Url"));
83         writeAttribute(QL1S("method"), QL1S("get"));
84         writeAttribute(QL1S("template"), engine->searchUrlTemplate());
85
86         if (!engine->searchParameters().empty()) 
87         {
88             writeNamespace(QL1S("http://a9.com/-/spec/opensearch/extensions/parameters/1.0/"), QL1S("p"));
89
90             QList<OpenSearchEngine::Parameter>::const_iterator end = engine->searchParameters().constEnd();
91             QList<OpenSearchEngine::Parameter>::const_iterator i = engine->searchParameters().constBegin();
92             for (; i != end; ++i) 
93             {
94                 writeStartElement(QL1S("p:Parameter"));
95                 writeAttribute(QL1S("name"), i->first);
96                 writeAttribute(QL1S("value"), i->second);
97                 writeEndElement();
98             }
99         }
100
101         writeEndElement();
102     }
103
104     if (!engine->suggestionsUrlTemplate().isEmpty()) 
105     {
106         writeStartElement(QL1S("Url"));
107         writeAttribute(QL1S("method"), QL1S("get"));
108         writeAttribute(QL1S("type"), engine->type());
109         writeAttribute(QL1S("template"), engine->suggestionsUrlTemplate());
110
111         if (!engine->suggestionsParameters().empty()) 
112         {
113             writeNamespace(QL1S("http://a9.com/-/spec/opensearch/extensions/parameters/1.0/"), QL1S("p"));
114
115             QList<OpenSearchEngine::Parameter>::const_iterator end = engine->suggestionsParameters().constEnd();
116             QList<OpenSearchEngine::Parameter>::const_iterator i = engine->suggestionsParameters().constBegin();
117             for (; i != end; ++i) 
118             {
119                 writeStartElement(QL1S("p:Parameter"));
120                 writeAttribute(QL1S("name"), i->first);
121                 writeAttribute(QL1S("value"), i->second);
122                 writeEndElement();
123             }
124         }
125
126         writeEndElement();
127     }
128
129     if (!engine->imageUrl().isEmpty())
130         writeTextElement(QL1S("Image"), engine->imageUrl());
131
132     writeEndElement();
133     writeEndDocument();
134 }