summaryrefslogtreecommitdiff
path: root/app/bin/manifest.c
blob: 5fd9b5adfea3af299b44bf342aa7a2a7784e2e08 (plain)
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
175
176
/** \file manifest.c
 * JSON routines
 */
 /*  XTrkCad - Model Railroad CAD
  *  Copyright (C) 2018 Adam Richards and Martin Fischer
  *
  *  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; if not, write to the Free Software
  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
  
#include <string.h>

#include "cJSON.h"
#include "fileio.h"
#include "layout.h"
#include "misc2.h"
#include "paths.h"
#include "include/utf8convert.h"

extern int log_zip;

 /**********************************************************
  * Build JSON Manifest -  manifest.json
  * There are only two objects in the root -
  * - The layout object defines the correct filename for the layout
  * - The dependencies object is an arraylist of included elements
  *
  * Each element has a name, a filename and an arch-path (where in the archive it is located)
  * It may have other values - a common one the copy-path is where it was copied from the originators machine (info only)
  *
  * There is one reserved name - "background" which is for the image file that is used as a layout background
  *
  *\param IN nameOfLayout - the layout this is a manifest for
  *\param IN background - the full filepath to the background image (or NULL) -> TODO this will become an array with a count
  *\param IN DependencyDir - the relative path in the archive to the directory in which the included object(s) will be stored
  *
  *\returns a String containing the JSON object
  */

char* CreateManifest(char* nameOfLayout, char* background,
	char* dependencyDir)
{
	cJSON* manifest = cJSON_CreateObject();
	if (manifest != NULL) {
		char *copyOfFileName = MyStrdup(nameOfLayout);
		cJSON* a_object = cJSON_CreateObject();
		cJSON_AddItemToObject(manifest, "layout", a_object);
#ifdef UTFCONVERT
		copyOfFileName = Convert2UTF8(copyOfFileName);
#endif // UTFCONVERT
		cJSON_AddStringToObject(a_object, "name", copyOfFileName);
		MyFree(copyOfFileName);

		cJSON* dependencies = cJSON_AddArrayToObject(manifest, "dependencies");
		cJSON* b_object = cJSON_CreateObject();
		if (background && background[0]) {
			char *backg;
			cJSON_AddStringToObject(b_object, "name", "background");

			backg = MyStrdup(FindFilename(background));
#ifdef UTFCONVERT
			backg = Convert2UTF8(backg);
#endif
			cJSON_AddStringToObject(b_object, "filename", backg);
			MyFree(backg);
			backg = MyStrdup(background);
#ifdef UTFCONVERT
			backg = Convert2UTF8(backg);
			ConvertPathForward(backg);
#endif // UTFCONVERT			
			cJSON_AddStringToObject(b_object, "copy-path", backg);
			cJSON_AddStringToObject(b_object, "arch-path", dependencyDir);
			MyFree(backg);
			cJSON_AddNumberToObject(b_object, "size", GetLayoutBackGroundSize());
			cJSON_AddNumberToObject(b_object, "pos-x", GetLayoutBackGroundPos().x);
			cJSON_AddNumberToObject(b_object, "pos-y", GetLayoutBackGroundPos().y);
			cJSON_AddNumberToObject(b_object, "screen", GetLayoutBackGroundScreen());
			cJSON_AddNumberToObject(b_object, "angle", GetLayoutBackGroundAngle());
			cJSON_AddItemToArray(dependencies, b_object);
		}
	}
	char* json_Manifest = cJSON_Print(manifest);
	cJSON_Delete(manifest);
	return json_Manifest;
}

/**************************************************************************
 * Pull in a Manifest File and extract values from it
 * \param IN manifest - the full path to the mainifest.json file
 * \param IN zip_directory - the path to the directory for extracted objects
 *
 * \returns - the layout filename
 */

char* ParseManifest(char* manifest, char* zip_directory)
{
	char* background_file[1] = { NULL };
	char* layoutname;

	SetCLocale();
	cJSON* json_manifest = cJSON_Parse(manifest);
	SetUserLocale();

	cJSON* layout = cJSON_GetObjectItemCaseSensitive(json_manifest, "layout");
	cJSON* name = cJSON_GetObjectItemCaseSensitive(layout, "name");
	layoutname = cJSON_GetStringValue(name);
#ifdef UTFCONVERT
	ConvertUTF8ToSystem(layoutname);
#endif // UTFCONVERT

	LOG(log_zip, 1, ("Zip-Manifest %s \n", layoutname))
#if DEBUG
		fprintf(stderr, "Layout name %s \n", layoutname);
#endif

	cJSON* dependencies = cJSON_GetObjectItemCaseSensitive(json_manifest,
		"dependencies");
	cJSON* dependency;
	cJSON_ArrayForEach(dependency, dependencies) {
		cJSON* name = cJSON_GetObjectItemCaseSensitive(dependency, "name");
		if (strcmp(cJSON_GetStringValue(name), "background") == 0) {
			char *file;
			char *path;
			cJSON* filename = cJSON_GetObjectItemCaseSensitive(dependency, "filename");
			cJSON* archpath = cJSON_GetObjectItemCaseSensitive(dependency, "arch-path");
			file = MyStrdup(cJSON_GetStringValue(filename));
			path = MyStrdup(cJSON_GetStringValue(archpath));
#ifdef UTFCONVERT
			ConvertUTF8ToSystem(file);
			ConvertUTF8ToSystem(path);
#endif
			MakeFullpath(&background_file[0], zip_directory, path,
				file, NULL);
			MyFree(file);
			MyFree(path);
#if DEBUG
			printf("Link to background image %s \n", background_file[0]);
#endif
			LoadImageFile(1, &background_file[0], NULL);
			cJSON* size = cJSON_GetObjectItemCaseSensitive(dependency, "size");
			SetLayoutBackGroundSize(size->valuedouble);
			cJSON* posx = cJSON_GetObjectItemCaseSensitive(dependency, "pos-x");
			cJSON* posy = cJSON_GetObjectItemCaseSensitive(dependency, "pos-y");
			coOrd pos;
			pos.x = posx->valuedouble;
			pos.y = posy->valuedouble;
			SetLayoutBackGroundPos(pos);
			cJSON* screen = cJSON_GetObjectItemCaseSensitive(dependency, "screen");
			SetLayoutBackGroundScreen((int)screen->valuedouble);
			cJSON* angle = cJSON_GetObjectItemCaseSensitive(dependency, "angle");
			SetLayoutBackGroundAngle(angle->valuedouble);
			LayoutBackGroundSave();      //Force out Values	to override saved
		}
	}
	char *str = NULL;
	if (background_file[0]) {
		free(background_file[0]);
	}
	if (layoutname) {
		str = strdup(layoutname);
	}
	cJSON_Delete(json_manifest);
	return str;
}