Git commit 00870364fcd560c50d4d47dfab27665063658028 by Giorgos Tsiapaliwkas. Committed on 16/07/2012 at 17:21. Pushed by tsiapaliwkas into branch 'terietor/kconfigxt'. Initial commit for kconfigxtwriter. Also s/kconfigxtparser/kconfigxtreader M +4 -2 CMakeLists.txt M +55 -16 editors/kconfigxt/kconfigxteditor.cpp M +19 -10 editors/kconfigxt/kconfigxteditor.h R +21 -18 editors/kconfigxt/kconfigxtreader.cpp [from: editors/kconfig= xt/kconfigxtparser.cpp - 083% similarity] R +11 -10 editors/kconfigxt/kconfigxtreader.h [from: editors/kconfigxt= /kconfigxtparser.h - 080% similarity] A +171 -0 editors/kconfigxt/kconfigxtwriter.cpp [License: GPL (v2+= )] A +86 -0 editors/kconfigxt/kconfigxtwriter.h [License: GPL (v2+)] http://commits.kde.org/plasmate/00870364fcd560c50d4d47dfab27665063658028 diff --git a/CMakeLists.txt b/CMakeLists.txt index ed0b7f7..09696e1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,7 +32,8 @@ set (PlasMate_SRC editors/imageviewer/imageviewer.cpp editors/text/texteditor.cpp editors/kconfigxt/kconfigxteditor.cpp - editors/kconfigxt/kconfigxtparser.cpp + editors/kconfigxt/kconfigxtreader.cpp + editors/kconfigxt/kconfigxtwriter.cpp main.cpp sidebaritem.cpp sidebardelegate.cpp @@ -145,7 +146,8 @@ install( FILES previewer/windowswitcher/tabboxdelegate.= qml DESTINATION ${DATA_IN #plasmakconfigxteditor set(plasmakconfigxt_SRCS editors/kconfigxt/kconfigxteditor.cpp - editors/kconfigxt/kconfigxtparser.cpp + editors/kconfigxt/kconfigxtreader.cpp + editors/kconfigxt/kconfigxtwriter.cpp editors/kconfigxt/standalone/plasmakconfigxteditor.cpp editors/kconfigxt/standalone/main.cpp ) diff --git a/editors/kconfigxt/kconfigxteditor.cpp b/editors/kconfigxt/kcon= figxteditor.cpp index 019d7d3..20c6376 100644 --- a/editors/kconfigxt/kconfigxteditor.cpp +++ b/editors/kconfigxt/kconfigxteditor.cpp @@ -1,6 +1,7 @@ /* This file is part of the KDE project Copyright 2009 by Dmitry Suzdalev + Copyright 2012 by Giorgos Tsiapaliwkas = This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public @@ -19,6 +20,7 @@ */ = #include "kconfigxteditor.h" +#include "kconfigxtwriter.h" = #include #include @@ -28,6 +30,7 @@ #include #include #include +#include = KConfigXtEditor::KConfigXtEditor(QWidget *parent) : QWidget(parent) @@ -43,6 +46,7 @@ KConfigXtEditor::KConfigXtEditor(QWidget *parent) m_ui.lblHintIcon->setPixmap(KIcon("dialog-information").pixmap(16, 16)= ); = connect(m_ui.pbAddGroup, SIGNAL(clicked()), this, SLOT(createNewGroup(= ))); + connect(m_ui.pbAddEntry, SIGNAL(clicked()), this, SLOT(createNewEntry(= ))); = connect(m_ui.twGroups, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(setupWidgetsForEntries(QTreeWidgetItem*))); @@ -68,6 +72,11 @@ void KConfigXtEditor::setFilename(const KUrl& filename) m_filename =3D filename; } = +KUrl KConfigXtEditor::filename() +{ + return m_filename; +} + void KConfigXtEditor::readFile() { if (m_filename.isEmpty()) { @@ -86,11 +95,6 @@ void KConfigXtEditor::readFile() } } = -void KConfigXtEditor::writeFile() -{ - // TODO: writing goes here -} - void KConfigXtEditor::setupWidgetsForNewFile() { // Add default group @@ -120,8 +124,48 @@ void KConfigXtEditor::createNewGroup() m_groups.append(newGroupName); = addGroupToUi(newGroupName); + + KConfigXtReaderItem newGroupItem; + newGroupItem.setGroupName(newGroupName); + giveElementsToWriter(newGroupItem); } = +void KConfigXtEditor::createNewEntry() +{ + QTreeWidgetItem *currentGroupItem =3D m_ui.twGroups->currentItem(); + + //check if the ptr is evil!! + if (!currentGroupItem) { + return; + } + + KConfigXtReaderItem newEntryItem; + newEntryItem.setGroupName(currentGroupItem->text(0)); + newEntryItem.setEntryName("TODO"); + //remember we need a valid type due to the + //internal check + newEntryItem.setEntryType("String"); + newEntryItem.setEntryValue("TODO"); + + //create the entries for the existing data + setupWidgetsForEntries(currentGroupItem); + + //add our new entry into the tree + addEntryToUi(newEntryItem.entryName(), + newEntryItem.entryType(), newEntryItem.entryValue()); + + giveElementsToWriter(newEntryItem); +} + +void KConfigXtEditor::giveElementsToWriter(KConfigXtReaderItem& newElement) +{ + takeDataFromParser(); + m_keysValuesTypes.append(newElement); + KConfigXtWriter writer(filename().pathOrUrl()); + QList list =3D writer.readerItemsToWriterIems(m_g= roups, m_keysValuesTypes); + writer.setData(list); + writer.writeXML(); +} = void KConfigXtEditor::setupWidgetsForGroups() { @@ -155,10 +199,9 @@ void KConfigXtEditor::setupWidgetsForEntries(QTreeWidg= etItem *item) //take keys,values and types for the specified group takeDataFromParser(item->text(0)); = - foreach(const KConfigXtParserItem& item, m_keysValuesTypes) { + foreach(const KConfigXtReaderItem& item, m_keysValuesTypes) { addEntryToUi(item.entryName(), item.entryType(), item.entryValue()); } - } = void KConfigXtEditor::addEntryToUi(const QString& key, const QString& type= , const QString& value) @@ -170,26 +213,22 @@ void KConfigXtEditor::addEntryToUi(const QString& key= , const QString& type, cons item->setFlags(item->flags() | Qt::ItemIsEditable); } = -void KConfigXtEditor::takeDataFromParser(const QString& group) +void KConfigXtEditor::takeDataFromParser() { - //we need to update the data of our parser first m_parser.parse(); = - foreach(const KConfigXtParserItem& item, m_parser.dataList()) { + foreach(const KConfigXtReaderItem& item, m_parser.dataList()) { = - //take the name of the groups also due to the fact + //take the name of the groups, also due to the fact //that we take data from a parser we have to be careful so //check if the group exists in the list if (!item.groupName().isEmpty() && !m_groups.contains(item.groupNa= me())) { m_groups << item.groupName(); } = - if (!group.isEmpty() && item.groupName() =3D=3D group - && !m_keysValuesTypes.contains(item)) { - //we have specified a group - //so probably we want to populate the - //m_ui.twEntries. + if (!m_keysValuesTypes.contains(item)) { + //take the entrie m_keysValuesTypes.append(item); } } diff --git a/editors/kconfigxt/kconfigxteditor.h b/editors/kconfigxt/kconfi= gxteditor.h index 046bd92..a7d3225 100644 --- a/editors/kconfigxt/kconfigxteditor.h +++ b/editors/kconfigxt/kconfigxteditor.h @@ -1,6 +1,7 @@ /* This file is part of the KDE project Copyright 2009 by Dmitry Suzdalev + Copyright 2012 by Giorgos Tsiapaliwkas = This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public @@ -22,7 +23,7 @@ = #include "ui_kconfigxteditor.h" = -#include "kconfigxtparser.h" +#include "kconfigxtreader.h" = #include #include @@ -45,6 +46,7 @@ public: * Sets filename to edit */ void setFilename(const KUrl& filename); + KUrl filename(); = public slots: /** @@ -53,12 +55,6 @@ public slots: */ void readFile(); = - /** - * Writes config values to config file specified with setFilename acco= rding - * to current editor widgets state - */ - void writeFile(); - private slots: /** * Creates new kconfig group @@ -66,6 +62,19 @@ private slots: void createNewGroup(); = /** + * Creates new entry + */ + void createNewEntry(); + + /** + * Write elements in the xml + * When a new group or new entry will be + * created, this method will write them + * into the xml + **/ + void giveElementsToWriter(KConfigXtReaderItem& newElement); + + /** * Sets up editor widgets for * the groups. This method should be called every time that the * a group is modified/deleted/etc. @@ -134,7 +143,7 @@ private: * and m_keysValuesTypes */ = - void takeDataFromParser(const QString& group =3D QString()); + void takeDataFromParser(); = //with this method we avoid duplication void addGroupToUi(const QString& group); @@ -153,9 +162,9 @@ private: = KUrl m_filename; QStringList m_groups; - QList m_keysValuesTypes; + QList m_keysValuesTypes; = - KConfigXtParser m_parser; + KConfigXtReader m_parser; = QString m_lastGroupItem; //avoid duplication diff --git a/editors/kconfigxt/kconfigxtparser.cpp b/editors/kconfigxt/kcon= figxtreader.cpp similarity index 83% rename from editors/kconfigxt/kconfigxtparser.cpp rename to editors/kconfigxt/kconfigxtreader.cpp index 994596c..bde4e4e 100644 --- a/editors/kconfigxt/kconfigxtparser.cpp +++ b/editors/kconfigxt/kconfigxtreader.cpp @@ -1,6 +1,7 @@ /* This file is part of the KDE project Copyright 2009 by Dmitry Suzdalev + Copyright 2012 by Giorgos Tsiapaliwkas = This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public @@ -18,7 +19,7 @@ Boston, MA 02110-1301, USA. */ = -#include "kconfigxtparser.h" +#include "kconfigxtreader.h" = #include #include @@ -26,42 +27,44 @@ #include #include = -KConfigXtParserItem::KConfigXtParserItem(QObject* parent) +#include + +KConfigXtReaderItem::KConfigXtReaderItem(QObject* parent) { Q_UNUSED(parent) } = -QString KConfigXtParserItem::groupName() const +QString KConfigXtReaderItem::groupName() const { return m_groupName; } = -QString KConfigXtParserItem::entryName() const +QString KConfigXtReaderItem::entryName() const { return m_entryName; } = -QString KConfigXtParserItem::entryType() const +QString KConfigXtReaderItem::entryType() const { return m_entryType; } = -QString KConfigXtParserItem::entryValue() const +QString KConfigXtReaderItem::entryValue() const { return m_entryValue; } = -void KConfigXtParserItem::setGroupName(const QString& groupName) +void KConfigXtReaderItem::setGroupName(const QString& groupName) { m_groupName =3D groupName; } = -void KConfigXtParserItem::setEntryName(const QString& entryName) +void KConfigXtReaderItem::setEntryName(const QString& entryName) { m_entryName =3D entryName; } = -bool KConfigXtParserItem::operator=3D=3D(const KConfigXtParserItem& item) +bool KConfigXtReaderItem::operator=3D=3D(const KConfigXtReaderItem& item) { if (this->entryName() =3D=3D item.entryName()) { return true; @@ -70,7 +73,7 @@ bool KConfigXtParserItem::operator=3D=3D(const KConfigXtP= arserItem& item) } } = -void KConfigXtParserItem::setEntryType(const QString& entryType) +void KConfigXtReaderItem::setEntryType(const QString& entryType) { //those are the possible types QStringList types; @@ -106,24 +109,24 @@ void KConfigXtParserItem::setEntryType(const QString&= entryType) } } = -void KConfigXtParserItem::setEntryValue(const QString& entryValue) +void KConfigXtReaderItem::setEntryValue(const QString& entryValue) { m_entryValue =3D entryValue; } = -KConfigXtParser::KConfigXtParser(QObject *parent) +KConfigXtReader::KConfigXtReader(QObject *parent) : QObject(parent), m_parseResult(false) { = } = -void KConfigXtParser::setConfigXmlFile(const QString& filename) +void KConfigXtReader::setConfigXmlFile(const QString& filename) { m_filename =3D filename; } = -bool KConfigXtParser::parse() +bool KConfigXtReader::parse() { //we will parse the xml again so clear the list if (!m_dataList.isEmpty()) { @@ -158,7 +161,7 @@ bool KConfigXtParser::parse() if(reader.hasError()) { //an error has occured KMessageBox::error(0, i18n("The xml parsing has failed")); - + kDebug() << "parsing error!!!!" << reader.errorString(); //clear the reader reader.clear(); //the parse has failed @@ -171,7 +174,7 @@ bool KConfigXtParser::parse() return m_parseResult; } = -void KConfigXtParser::parseGroup(QXmlStreamReader& reader) +void KConfigXtReader::parseGroup(QXmlStreamReader& reader) { //verify if we really has a group if(reader.tokenType() !=3D QXmlStreamReader::StartElement && @@ -202,7 +205,7 @@ void KConfigXtParser::parseGroup(QXmlStreamReader& read= er) } } = -void KConfigXtParser::parseEntry(QXmlStreamReader& reader) +void KConfigXtReader::parseEntry(QXmlStreamReader& reader) { // Check if we are inside an element like if(reader.tokenType() !=3D QXmlStreamReader::StartElement) { @@ -254,7 +257,7 @@ void KConfigXtParser::parseEntry(QXmlStreamReader& read= er) m_dataList.append(m_data); } = -QList KConfigXtParser::dataList() const +QList KConfigXtReader::dataList() const { return m_dataList; } diff --git a/editors/kconfigxt/kconfigxtparser.h b/editors/kconfigxt/kconfi= gxtreader.h similarity index 80% rename from editors/kconfigxt/kconfigxtparser.h rename to editors/kconfigxt/kconfigxtreader.h index cb964bd..ff49ccc 100644 --- a/editors/kconfigxt/kconfigxtparser.h +++ b/editors/kconfigxt/kconfigxtreader.h @@ -1,6 +1,7 @@ /* This file is part of the KDE project Copyright 2009 by Dmitry Suzdalev + Copyright 2012 by Giorgos Tsiapaliwkas = This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public @@ -18,19 +19,19 @@ Boston, MA 02110-1301, USA. */ = -#ifndef KCONFIGXTPARSER_H -#define KCONFIGXTPARSER_H +#ifndef KCONFIGXTREADER_H +#define KCONFIGXTREADER_H = #include #include #include #include = -class KConfigXtParserItem +class KConfigXtReaderItem { = public: - KConfigXtParserItem(QObject* parent =3D 0); + KConfigXtReaderItem(QObject* parent =3D 0); = QString groupName() const; void setGroupName(const QString& groupName); @@ -44,7 +45,7 @@ public: QString entryValue() const; void setEntryValue(const QString& entryValue); = - bool operator=3D=3D(const KConfigXtParserItem& item); + bool operator=3D=3D(const KConfigXtReaderItem& item); = private: QString m_groupName; @@ -54,12 +55,12 @@ private: }; = = -class KConfigXtParser : public QObject +class KConfigXtReader : public QObject { = Q_OBJECT public: - KConfigXtParser(QObject *parent =3D 0); + KConfigXtReader(QObject *parent =3D 0); = void setConfigXmlFile(const QString& filename); = @@ -73,7 +74,7 @@ public: * Returns the data from the xml file. * Valid only after a successful call to parse() **/ - QList dataList() const; + QList dataList() const; = private: void parseGroup(QXmlStreamReader& reader); @@ -81,8 +82,8 @@ private: bool m_parseResult; = QString m_filename; - QList m_dataList; - KConfigXtParserItem m_data; + QList m_dataList; + KConfigXtReaderItem m_data; }; = #endif diff --git a/editors/kconfigxt/kconfigxtwriter.cpp b/editors/kconfigxt/kcon= figxtwriter.cpp new file mode 100644 index 0000000..eca35dd --- /dev/null +++ b/editors/kconfigxt/kconfigxtwriter.cpp @@ -0,0 +1,171 @@ +/* + This file is part of the KDE project + Copyright 2012 by Giorgos Tsiapaliwkas + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; see the file COPYING. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "kconfigxtwriter.h" + +#include +#include + +#include +#include + +#include + +KConfigXtWriterItem::KConfigXtWriterItem(QObject* parent) +{ + Q_UNUSED(parent) +} + +QString KConfigXtWriterItem::group() const +{ + return m_group; +} + + +void KConfigXtWriterItem::setGroup(const QString& group) +{ + m_group =3D group; +} + +QList< KConfigXtReaderItem > KConfigXtWriterItem::entries() const +{ + return m_entries; +} + +void KConfigXtWriterItem::setEntries(QList entries) +{ + m_entries =3D entries; +} + +KConfigXtWriter::KConfigXtWriter(QObject *parent) + : QObject(parent) +{ +} + +KConfigXtWriter::KConfigXtWriter(const QString& xmlFilePath, QObject *pare= nt) + : QObject(parent) +{ + setConfigXmlFile(xmlFilePath); +} + +void KConfigXtWriter::setConfigXmlFile(const QString& filename) +{ + QFile xmlFile(filename); + + //clear the file + xmlFile.resize(0); + + if (!xmlFile.open(QIODevice::ReadWrite)) { + return; + } + + m_writer.setDevice(&xmlFile); + +} + +QList< KConfigXtWriterItem > KConfigXtWriter::data() +{ + return m_dataList; +} + +void KConfigXtWriter::setData(QList< KConfigXtWriterItem > dataList) +{ + m_dataList =3D dataList; +} + +void KConfigXtWriter::writeXML() +{ + if (m_dataList.isEmpty()) { + return; + } + + m_writer.setAutoFormatting(true); + m_writer.writeStartDocument(); + + foreach(const KConfigXtWriterItem& writerItem, m_dataList) { + //start group + m_writer.writeStartElement("group"); + //start kcfg element + m_writer.writeStartElement("kcfg"); + + //TODO add the above into the xml + /*every kconfigxt xml file contains the below data + * = + * + * + * + * + * + */ + + m_writer.writeAttribute("name", writerItem.group()); + + foreach(const KConfigXtReaderItem& readerItem, writerItem.entries(= )) { + //start entry + m_writer.writeStartElement("entry"); + m_writer.writeAttribute("name", readerItem.entryName()); + m_writer.writeAttribute("type", readerItem.entryType()); + //start default + m_writer.writeStartElement("default"); + m_writer.writeCharacters(readerItem.entryValue()); + //end default + m_writer.writeEndElement(); + //end entry + m_writer.writeEndElement(); + } + //end group + m_writer.writeEndElement(); + } + //end kcfg + m_writer.writeEndElement(); + //end the document + m_writer.writeEndDocument(); +} + +QList KConfigXtWriter::readerItemsToWriterIems(QStrin= gList& groupList, + QList ent= riesList) const +{ + + QList list; + + foreach(const QString& group, groupList) { + KConfigXtWriterItem tmpItem; + tmpItem.setGroup(group); + QList tmpEntryList; + foreach (const KConfigXtReaderItem& entry, entriesList) { + if (group =3D=3D entry.groupName()) { + KConfigXtReaderItem tmpEntry; + tmpEntry.setEntryName(entry.entryName()); + tmpEntry.setEntryType(entry.entryType()); + tmpEntry.setEntryValue(entry.entryValue()); + + tmpEntryList.append(tmpEntry); + } + } + tmpItem.setEntries(tmpEntryList); + list.append(tmpItem); + } + + return list; +} + diff --git a/editors/kconfigxt/kconfigxtwriter.h b/editors/kconfigxt/kconfi= gxtwriter.h new file mode 100644 index 0000000..ea3607d --- /dev/null +++ b/editors/kconfigxt/kconfigxtwriter.h @@ -0,0 +1,86 @@ +/* + This file is part of the KDE project + Copyright 2012 by Giorgos Tsiapaliwkas + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; see the file COPYING. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef KCONFIGXTWRITER_H +#define KCONFIGXTWRITER_H + +#include +#include +#include +#include + +#include "kconfigxtreader.h" + +class KConfigXtWriterItem +{ + +public: + KConfigXtWriterItem(QObject* parent =3D 0); + + QString group() const; + void setGroup(const QString& groupName); + + QList entries() const; + void setEntries(QList entries); + +private: + QString m_group; + QList m_entries; +}; + + +class KConfigXtWriter : public QObject +{ + + Q_OBJECT +public: + KConfigXtWriter(QObject *parent =3D 0); + KConfigXtWriter( const QString& xmlFilePath, QObject *parent =3D 0); + + /** + * This is the xml in which the data will be + * writen + **/ + void setConfigXmlFile(const QString& filename); + + /** + * Returns the data that will be writen in the xml + **/ + QList data(); + + /** + * Sets the data that will be writen in the xml + **/ + void setData(QList dataList); + + /** + * writes the data in the xml + **/ + void writeXML(); + + QList readerItemsToWriterIems(QStringList& groupL= ist, + QList entriesList) const; + +private: + QList m_dataList; + QXmlStreamWriter m_writer; +}; + +#endif