Merge branch 'filters/zionworx'
[kworship.git] / kworship / filters / zionworx / KwZwStringList.cpp
blob971fab73f8c6c1b3d0fec555aaeebd137b994822
1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
4 * *
5 * KWorship is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * KWorship is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with KWorship. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
20 /**
21 * @file KwZwStringList.cpp
22 * @brief String list encoding widget.
23 * @author James Hogan <james@albanarts.com>
26 #include "KwZwStringList.h"
28 #include <KLocale>
30 #include <QDataStream>
31 #include <QLineEdit>
32 #include <QTextEdit>
33 #include <QVBoxLayout>
34 #include <QPushButton>
35 #include <QFile>
37 KwZwStringList::KwZwStringList()
38 : m_list()
39 , m_edit(new QLineEdit(this))
40 , m_output(new QTextEdit(this))
42 QVBoxLayout* layout = new QVBoxLayout(this);
43 layout->addWidget(m_edit);
45 QPushButton* btnAdd = new QPushButton(i18n("Add string"), this);
46 layout->addWidget(btnAdd);
47 connect(btnAdd, SIGNAL(clicked(bool)), this, SLOT(addItem()));
49 layout->addWidget(m_output);
50 m_output->setReadOnly(true);
53 QByteArray KwZwStringList::encodeList(const QStringList& stringlist)
55 QByteArray result;
57 // Type of object
58 QByteArray type = "Strings";
59 result.append((char)type.size());
60 result.append(type);
62 // Not sure what this byte is for, perhaps the number of bytes to represent something
63 char sep = '\x06';
64 result.append((char)0x01);
66 // Each item
67 foreach (QString item, stringlist)
69 /// @todo Check the assumption that lines are encoded in utf-8
70 QByteArray encoded = item.toUtf8();
71 /// @todo Find what to do if length >= 255
72 Q_ASSERT(encoded.size() < 255);
73 // Length followed by string encoding
74 result.append(sep);
75 char size = encoded.size();
76 result.append(QByteArray::fromRawData(&size, 1));
77 result.append(encoded);
79 result.append('\0');
80 return result;
83 void KwZwStringList::addItem()
85 m_list += m_edit->text();
86 m_edit->setText("");
87 m_output->setText(m_list.join("\n"));
89 QByteArray result = encodeList(m_list);
91 QFile op("dump");
92 op.open(QIODevice::WriteOnly);
93 op.write(result);
96 QFile op("dump.base64");
97 op.open(QIODevice::WriteOnly);
98 op.write(result.toBase64());