1 /***************************************************************************
2 Class of utility functions.
4 Copyright : (C) 2004 Paul Giannaros <ceruleanblaze@gmail.com>
6 Original author: Paul Giannaros <ceruleanblaze@gmail.com>
7 Current Maintainer: Paul Giannaros <ceruleanblaze@gmail.com>
8 ****************************************************************************/
10 /***************************************************************************
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; version 2 of the License. *
16 ***************************************************************************/
22 #include <QtGui/QComboBox>
24 KttsUtils::KttsUtils() {
28 KttsUtils::~KttsUtils() {
32 * Check if an XML document has a certain root element.
33 * @param xmldoc The document to check for the element.
34 * @param elementName The element to check for in the document.
35 * @returns True if the root element exists in the document, false otherwise.
37 bool KttsUtils::hasRootElement(const QString
&xmldoc
, const QString
&elementName
) {
38 // Strip all whitespace and go from there.
39 QString doc
= xmldoc
.simplified();
40 // Take off the <?xml...?> if it exists
41 if(doc
.startsWith("<?xml")) {
42 // Look for ?> and strip everything off from there to the start - effectively removing
44 int xmlStatementEnd
= doc
.indexOf("?>");
45 if(xmlStatementEnd
== -1) {
46 kDebug() << "KttsUtils::hasRootElement: Bad XML file syntax\n";
49 xmlStatementEnd
+= 2; // len '?>' == 2
50 doc
= doc
.right(doc
.length() - xmlStatementEnd
);
52 // Take off leading comments, if they exist.
53 while(doc
.startsWith("<!--") || doc
.startsWith(" <!--")) {
54 int commentStatementEnd
= doc
.indexOf("-->");
55 if(commentStatementEnd
== -1) {
56 kDebug() << "KttsUtils::hasRootElement: Bad XML file syntax\n";
59 commentStatementEnd
+= 3; // len '>' == 2
60 doc
= doc
.right(doc
.length() - commentStatementEnd
);
62 // Take off the doctype statement if it exists.
63 while(doc
.startsWith("<!DOCTYPE") || doc
.startsWith(" <!DOCTYPE")) {
64 int doctypeStatementEnd
= doc
.indexOf(">");
65 if(doctypeStatementEnd
== -1) {
66 kDebug() << "KttsUtils::hasRootElement: Bad XML file syntax\n";
69 doctypeStatementEnd
+= 1; // len '>' == 2
70 doc
= doc
.right(doc
.length() - doctypeStatementEnd
);
72 // We should (hopefully) be left with the root element.
73 return (doc
.startsWith('<' + elementName
) || doc
.startsWith(" <" + elementName
));
77 * Check if an XML document has a certain DOCTYPE.
78 * @param xmldoc The document to check for the doctype.
79 * @param name The doctype name to check for. Pass QString() to not check the name.
80 * @param publicId The public ID to check for. Pass QString() to not check the ID.
81 * @param systemId The system ID to check for. Pass QString() to not check the ID.
82 * @returns True if the parameters match the doctype, false otherwise.
84 bool KttsUtils::hasDoctype(const QString
&xmldoc
, const QString
&name
/*, const QString &publicId, const QString &systemId*/) {
85 // Strip all whitespace and go from there.
86 QString doc
= xmldoc
.trimmed();
87 // Take off the <?xml...?> if it exists
88 if(doc
.startsWith("<?xml")) {
89 // Look for ?> and strip everything off from there to the start - effectively removing
91 int xmlStatementEnd
= doc
.indexOf("?>");
92 if(xmlStatementEnd
== -1) {
93 kDebug() << "KttsUtils::hasDoctype: Bad XML file syntax\n";
96 xmlStatementEnd
+= 2; // len '?>' == 2
97 doc
= doc
.right(doc
.length() - xmlStatementEnd
);
100 // Take off leading comments, if they exist.
101 while(doc
.startsWith("<!--")) {
102 int commentStatementEnd
= doc
.indexOf("-->");
103 if(commentStatementEnd
== -1) {
104 kDebug() << "KttsUtils::hasDoctype: Bad XML file syntax\n";
107 commentStatementEnd
+= 3; // len '>' == 2
108 doc
= doc
.right(doc
.length() - commentStatementEnd
);
111 // Match the doctype statement if it exists.
112 // kDebug() << "KttsUtils::hasDoctype: searching " << doc.left(20) << "... for " << "<!DOCTYPE " << name;
113 return (doc
.startsWith("<!DOCTYPE " + name
));
117 * Sets the current item in the given combobox to the item with the given text.
118 * If item with the text not found, does nothing.
120 /*static*/ void KttsUtils::setCbItemFromText(QComboBox
* cb
, const QString
& text
)
122 const int itemCount
= cb
->count();
123 for (int ndx
= 0; ndx
< itemCount
; ++ndx
)
125 if (cb
->itemText(ndx
) == text
)
127 cb
->setCurrentIndex(ndx
);
133 #include "player.moc"