[Ktutorial-commits] SF.net SVN: ktutorial:[166] trunk/ktutorial/ktutorial-editor
Status: Alpha
Brought to you by:
danxuliu
|
From: <dan...@us...> - 2010-03-19 18:14:22
|
Revision: 166
http://ktutorial.svn.sourceforge.net/ktutorial/?rev=166&view=rev
Author: danxuliu
Date: 2010-03-19 18:14:11 +0000 (Fri, 19 Mar 2010)
Log Message:
-----------
Add Serialization class to act as a facade for the serialization subsystem. Right now it only abstracts exporting a tutorial to some file, but in the future it will provide a way to load and save tutorials.
Modified Paths:
--------------
trunk/ktutorial/ktutorial-editor/src/serialization/CMakeLists.txt
trunk/ktutorial/ktutorial-editor/tests/unit/serialization/CMakeLists.txt
Added Paths:
-----------
trunk/ktutorial/ktutorial-editor/src/serialization/Serialization.cpp
trunk/ktutorial/ktutorial-editor/src/serialization/Serialization.h
trunk/ktutorial/ktutorial-editor/tests/unit/serialization/SerializationTest.cpp
Modified: trunk/ktutorial/ktutorial-editor/src/serialization/CMakeLists.txt
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/serialization/CMakeLists.txt 2010-03-19 07:12:21 UTC (rev 165)
+++ trunk/ktutorial/ktutorial-editor/src/serialization/CMakeLists.txt 2010-03-19 18:14:11 UTC (rev 166)
@@ -2,6 +2,7 @@
set(ktutorial_editor_serialization_SRCS
JavascriptExporter.cpp
+ Serialization.cpp
)
kde4_add_library(ktutorial_editor_serialization ${ktutorial_editor_serialization_SRCS})
Added: trunk/ktutorial/ktutorial-editor/src/serialization/Serialization.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/serialization/Serialization.cpp (rev 0)
+++ trunk/ktutorial/ktutorial-editor/src/serialization/Serialization.cpp 2010-03-19 18:14:11 UTC (rev 166)
@@ -0,0 +1,71 @@
+/***************************************************************************
+ * Copyright (C) 2010 by Daniel Calviño Sánchez *
+ * dan...@gm... *
+ * *
+ * 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 3 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; If not, see <http://www.gnu.org/licenses/>. *
+ ***************************************************************************/
+
+#include "Serialization.h"
+
+#include <KLocalizedString>
+#include <KTemporaryFile>
+#include <KUrl>
+#include <KIO/NetAccess>
+
+#include "JavascriptExporter.h"
+
+//public:
+
+QString Serialization::availableExporterTypes() {
+ QStringList typeList = availableExporterTypeList();
+
+ QString types;
+ for (int i=0; i<typeList.count() - 1; ++i) {
+ types += typeList[i] + '\n';
+ }
+ types += typeList[typeList.count()-1];
+
+ return types;
+}
+
+bool Serialization::exportTutorial(const Tutorial* tutorial,
+ const QString& type, const KUrl& url) {
+ Q_ASSERT(tutorial);
+ Q_ASSERT(url.isValid());
+
+ QString exportedCode;
+ if (type == "*.js") {
+ exportedCode = JavascriptExporter().exportTutorial(tutorial);
+ } else {
+ Q_ASSERT(false);
+ }
+
+ KTemporaryFile temporaryFile;
+ temporaryFile.open();
+ QTextStream out(&temporaryFile);
+ out << exportedCode;
+ out.flush();
+ temporaryFile.close();
+
+ return KIO::NetAccess::upload(temporaryFile.fileName(), url, 0);
+}
+
+//private:
+
+QStringList Serialization::availableExporterTypeList() {
+ QStringList types;
+ types << i18nc("@item:combobox A KFileDialog filter",
+ "*.js|Javascript file");
+ return types;
+}
Property changes on: trunk/ktutorial/ktutorial-editor/src/serialization/Serialization.cpp
___________________________________________________________________
Added: svn:eol-style
+ native
Added: trunk/ktutorial/ktutorial-editor/src/serialization/Serialization.h
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/serialization/Serialization.h (rev 0)
+++ trunk/ktutorial/ktutorial-editor/src/serialization/Serialization.h 2010-03-19 18:14:11 UTC (rev 166)
@@ -0,0 +1,76 @@
+/***************************************************************************
+ * Copyright (C) 2010 by Daniel Calviño Sánchez *
+ * dan...@gm... *
+ * *
+ * 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 3 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; If not, see <http://www.gnu.org/licenses/>. *
+ ***************************************************************************/
+
+#ifndef SERIALIZATION_H
+#define SERIALIZATION_H
+
+#include <QStringList>
+
+class KUrl;
+class Tutorial;
+
+/**
+ * Facade for serialization system.
+ * It provides the methods needed to know the available exporters and export a
+ * tutorial using one of them.
+ */
+class Serialization {
+public:
+
+ /**
+ * Returns the available exporter types.
+ * The string has the format expected by KFileDialog setFilter method, that
+ * is, "*.fileExtension1|Human readable name1\n*.fileExtension2|Human
+ * readable name2\n...".
+ * The first type in the list is the default type, Javascript.
+ *
+ * @return The available exporter types.
+ */
+ static QString availableExporterTypes();
+
+ /**
+ * Exports the tutorial to the file specified by the url using the given
+ * exporter type.
+ * The type must be the extension fragment of one of the types returned by
+ * availableExporterTypes(). It is the value returned by KDialog
+ * currentFilter() method.
+ *
+ * The url can be local or remote. If the file can't be saved, false is
+ * returned. In that case, KIO::NetAccess can be asked for the error code
+ * and string.
+ *
+ * @param tutorial The tutorial to export.
+ * @param type The type of the exporter to use.
+ * @param url The url to save the script file to.
+ * @return True if the exported tutorial was saved, false otherwise.
+ */
+ static bool exportTutorial(const Tutorial* tutorial, const QString& type,
+ const KUrl& url);
+
+private:
+
+ /**
+ * Returns a list of strings with the available exporter types.
+ *
+ * @return A list with the available exporter types.
+ */
+ static QStringList availableExporterTypeList();
+
+};
+
+#endif
Property changes on: trunk/ktutorial/ktutorial-editor/src/serialization/Serialization.h
___________________________________________________________________
Added: svn:eol-style
+ native
Modified: trunk/ktutorial/ktutorial-editor/tests/unit/serialization/CMakeLists.txt
===================================================================
--- trunk/ktutorial/ktutorial-editor/tests/unit/serialization/CMakeLists.txt 2010-03-19 07:12:21 UTC (rev 165)
+++ trunk/ktutorial/ktutorial-editor/tests/unit/serialization/CMakeLists.txt 2010-03-19 18:14:11 UTC (rev 166)
@@ -13,6 +13,7 @@
unit_tests(
JavascriptExporter
+ Serialization
)
MACRO(MEM_TESTS)
@@ -23,4 +24,5 @@
mem_tests(
JavascriptExporter
+ Serialization
)
Added: trunk/ktutorial/ktutorial-editor/tests/unit/serialization/SerializationTest.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/tests/unit/serialization/SerializationTest.cpp (rev 0)
+++ trunk/ktutorial/ktutorial-editor/tests/unit/serialization/SerializationTest.cpp 2010-03-19 18:14:11 UTC (rev 166)
@@ -0,0 +1,162 @@
+/***************************************************************************
+ * Copyright (C) 2010 by Daniel Calviño Sánchez *
+ * dan...@gm... *
+ * *
+ * 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 3 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; If not, see <http://www.gnu.org/licenses/>. *
+ ***************************************************************************/
+
+#include <QtTest>
+
+#include "Serialization.h"
+
+#include <QFile>
+
+#include <KLocalizedString>
+#include <KStandardDirs>
+#include <KUrl>
+
+#include "../Tutorial.h"
+
+class SerializationTest: public QObject {
+Q_OBJECT
+
+private slots:
+
+ void init();
+ void cleanup();
+
+ void testAvailableExporterTypes();
+
+ void testExportJavascript();
+
+ void testExportToExistingUrl();
+ void testExportToUnwritableUrl();
+
+private:
+
+ QFile* mExportedFile;
+
+ QString readFile(QFile* file);
+ void writeFile(QFile* file, const QString& contents);
+
+};
+
+void SerializationTest::init() {
+ mExportedFile = 0;
+}
+
+void SerializationTest::cleanup() {
+ if (mExportedFile) {
+ mExportedFile->remove();
+ }
+ delete mExportedFile;
+}
+
+void SerializationTest::testAvailableExporterTypes() {
+ QString types = Serialization::availableExporterTypes();
+
+ QCOMPARE(types, i18nc("@item:combobox A KFileDialog filter",
+ "*.js|Javascript file"));
+}
+
+void SerializationTest::testExportJavascript() {
+ Tutorial tutorial;
+ tutorial.setName("The name");
+ tutorial.setDescription("The description");
+
+ KUrl url = KGlobal::dirs()->saveLocation("tmp") + "/SerializationTest.js";
+
+ QVERIFY(Serialization::exportTutorial(&tutorial, "*.js", url));
+
+ mExportedFile = new QFile(url.toLocalFile());
+ QVERIFY(mExportedFile->exists());
+ QVERIFY(mExportedFile->open(QIODevice::ReadOnly | QIODevice::Text));
+
+ QString actualContents = readFile(mExportedFile);
+ QString expectedContents =
+"t = Kross.module(\"kdetranslation\");\n"
+"\n"
+"tutorial.tutorialInformationAsObject().setName(t.i18n(\"The name\"));\n"
+"tutorial.tutorialInformationAsObject().setDescription("
+ "t.i18n(\"The description\"));\n"
+"\n";
+
+ QCOMPARE(actualContents, expectedContents);
+}
+
+void SerializationTest::testExportToExistingUrl() {
+ Tutorial tutorial;
+ tutorial.setName("The name");
+ tutorial.setDescription("The description");
+
+ KUrl url = KGlobal::dirs()->saveLocation("tmp") + "/SerializationTest.js";
+ mExportedFile = new QFile(url.toLocalFile());
+ mExportedFile->open(QIODevice::WriteOnly | QIODevice::Text);
+ writeFile(mExportedFile, "Hello world!");
+
+ QVERIFY(Serialization::exportTutorial(&tutorial, "*.js", url));
+
+ QVERIFY(mExportedFile->exists());
+ QVERIFY(mExportedFile->open(QIODevice::ReadOnly | QIODevice::Text));
+
+ QString actualContents = readFile(mExportedFile);
+ QString expectedContents =
+"t = Kross.module(\"kdetranslation\");\n"
+"\n"
+"tutorial.tutorialInformationAsObject().setName(t.i18n(\"The name\"));\n"
+"tutorial.tutorialInformationAsObject().setDescription("
+ "t.i18n(\"The description\"));\n"
+"\n";
+
+ QCOMPARE(actualContents, expectedContents);
+}
+
+void SerializationTest::testExportToUnwritableUrl() {
+ Tutorial tutorial;
+ tutorial.setName("The name");
+ tutorial.setDescription("The description");
+
+ KUrl url = KGlobal::dirs()->saveLocation("tmp") + "/SerializationTest.js";
+ mExportedFile = new QFile(url.toLocalFile());
+ mExportedFile->open(QIODevice::WriteOnly | QIODevice::Text);
+ writeFile(mExportedFile, "Hello world!");
+ QVERIFY(QFile::setPermissions(mExportedFile->fileName(), 0));
+
+ QVERIFY(!Serialization::exportTutorial(&tutorial, "*.js", url));
+}
+
+/////////////////////////////////// Helpers ////////////////////////////////////
+
+QString SerializationTest::readFile(QFile* file) {
+ QString data;
+
+ QTextStream in(file);
+ while (!in.atEnd()) {
+ data += in.readAll();
+ }
+ file->close();
+
+ return data;
+}
+
+void SerializationTest::writeFile(QFile* file, const QString& contents) {
+ QTextStream out(file);
+ out << contents;
+
+ file->close();
+}
+
+QTEST_MAIN(SerializationTest)
+
+#include "SerializationTest.moc"
Property changes on: trunk/ktutorial/ktutorial-editor/tests/unit/serialization/SerializationTest.cpp
___________________________________________________________________
Added: svn:eol-style
+ native
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|