1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
/*
* 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.
*/
/* (C) Marcin Kwadrans <quarter@users.sourceforge.net> */
#include <cstdio>
#include "include/support.h"
#include "project.h"
#include "environment.h"
/*! \brief Konstruktor
Tworzy projekt
*/
LWProject::LWProject (): file_name (NULL)
{
world = new LWBoard (LW_TYPE_WORLD);
world->setSize (10, 10);
program = new LWBoard (LW_TYPE_PROGRAM);
}
/*! \brief Destruktor
Niszczy zbiór ikon
*/
LWProject::~LWProject ()
{
delete (world);
delete (program);
if (file_name != NULL)
g_free (file_name);
if (this == LWEnvironment::getProject())
LWEnvironment::unsetProject ();
}
/*! \brief Ładowanie pliku projektu
Ładuje projekt z pliku
\param a_file_name Nazwa pliku z projektem
\return TRUE if success, otherwise FALSE.
*/
gboolean LWProject::load (const gchar *a_file_name)
{
/* Parse the file and get the DOM */
xmlDoc *doc = xmlParseFile(a_file_name);
if (doc == NULL) return FALSE;
/* Get root node */
xmlNode *node = xmlDocGetRootElement(doc);
g_return_val_if_fail (!xmlStrcasecmp (node->name, (xmlChar *) "LittleWizardProject"), FALSE);
/* Version check */
xmlChar *version = xmlGetProp (node, (xmlChar *) "version");
if (version != NULL) {
unsigned int promajor=0, prominor=0, envminor=0, envmajor=0;
sscanf ((char *) version, "%u.%u", &promajor, &prominor);
sscanf (VERSION, "%u.%u", &envmajor, &envminor);
xmlFree (version);
g_return_val_if_fail (promajor < envmajor || (promajor == envmajor && prominor <= envminor), FALSE);
}
#if 1
/* Workaround GTK bug */
gtk_widget_hide (world->getWidget());
gtk_widget_hide (program->getWidget());
#endif
world->restoreFromXML (node->children);
program->restoreFromXML (node->children->next);
#if 1
gtk_widget_show (world->getWidget());
gtk_widget_show (program->getWidget());
#endif
xmlFreeDoc (doc);
setFileName (a_file_name);
return TRUE;
}
/*! \brief Zapisywanie projektu do pliku
Zapisuje projekt do pliku
\param a_file_name Nazwa pliku, pod którą ma być zapisany projekt
\return TRUE if success, otherwise FALSE.
*/
gboolean LWProject::save (const gchar *a_file_name)
{
xmlDoc *doc = xmlNewDoc(BAD_CAST "1.0");
xmlNode *node = xmlNewNode(NULL, BAD_CAST "LittleWizardProject");
/* Setting proper version for project */
gchar **versionarr = g_strsplit (VERSION, ".", 3);
gchar *version = g_strjoin (".", versionarr[0], versionarr[1], NULL);
g_strfreev (versionarr);
xmlNewProp (node, (xmlChar *) "version", (xmlChar *) version);
g_free (version);
xmlDocSetRootElement(doc, node);
world->storeToXML (node);
program->storeToXML (node);
int r = xmlSaveFile (a_file_name, doc);
xmlFreeDoc (doc);
setFileName (a_file_name);
return (r > -1) ? TRUE : FALSE;
}
/*! \brief Ustawia nazwę pliku
Ustawia nazwę pliku pod którą został zapisany projekt
\param a_file_name Nazwa pliku, pod którą został zapisany projekt
*/
void LWProject::setFileName (const gchar *a_file_name)
{
g_return_if_fail (a_file_name != NULL);
if (file_name != NULL)
g_free (file_name);
file_name = g_strdup (a_file_name);
}
/*! \brief Pobierz nazwę pliku
Pobiera nazwę pliku pod którą został zapisany projekt
\return Nazwa pliku, pod którą został zapisany projekt.
Należy ją zwolnić samodzielnie
*/
gchar *LWProject::getFileName ()
{
return (file_name != NULL) ? g_strdup (file_name) : NULL;
}
/*! \brief Pobierz świat
Pobiera planszę ze światem wchodzącą w skład projektu
\return Plansza ze światem
*/
LWBoard *LWProject::getWorld ()
{
return world;
}
/*! \brief Pobierz program
Pobiera planszę z programem wchodzącą w skład projektu
\return Plansza z programem
*/
LWBoard *LWProject::getProgram ()
{
return program;
}
|