1
by Jo Shields
Import upstream version 1.0 |
1 |
/*
|
2 |
* xaml.cpp: xaml parser
|
|
3 |
*
|
|
4 |
* Contact:
|
|
5 |
* Moonlight List (moonlight-list@lists.ximian.com)
|
|
6 |
*
|
|
7 |
* Copyright 2008 Novell, Inc. (http://www.novell.com)
|
|
8 |
*
|
|
9 |
* See the LICENSE file included with the distribution for details.
|
|
10 |
*
|
|
11 |
*/
|
|
12 |
#include <config.h> |
|
13 |
#include <string.h> |
|
14 |
#include <malloc.h> |
|
15 |
#include <glib.h> |
|
16 |
#include <stdlib.h> |
|
17 |
#include <unistd.h> |
|
18 |
#include <fcntl.h> |
|
19 |
||
20 |
#include "xaml.h" |
|
21 |
#include "error.h" |
|
22 |
#include "utils.h" |
|
23 |
#include "type.h" |
|
24 |
#include "zip/unzip.h" |
|
25 |
#include "xap.h" |
|
26 |
||
27 |
char * |
|
28 |
Xap::Unpack (const char *fname) |
|
29 |
{
|
|
30 |
char *xap_dir; |
|
31 |
||
32 |
xap_dir = CreateTempDir (fname); |
|
33 |
if (xap_dir == NULL) |
|
34 |
return NULL; |
|
35 |
||
36 |
unzFile zipfile = unzOpen (fname); |
|
37 |
if (zipfile == NULL) |
|
38 |
goto exception0; |
|
39 |
||
40 |
if (unzGoToFirstFile (zipfile) != UNZ_OK) |
|
41 |
goto exception1; |
|
42 |
||
43 |
if (unzOpenCurrentFile (zipfile) != UNZ_OK) |
|
44 |
goto exception1; |
|
45 |
||
46 |
do { |
|
47 |
char *fname, *output, *dirname; |
|
48 |
unz_file_info finfo; |
|
49 |
size_t len, i; |
|
50 |
int fd; |
|
51 |
||
52 |
unzGetCurrentFileInfo (zipfile, &finfo, NULL, 0, NULL, 0, NULL, 0); |
|
53 |
fname = (char *) malloc (finfo.size_filename + 2); |
|
54 |
if (fname == 0) |
|
55 |
goto exception1; |
|
56 |
||
57 |
unzGetCurrentFileInfo (zipfile, NULL, fname, finfo.size_filename+1, NULL, 0, NULL, 0); |
|
58 |
||
59 |
output = g_build_filename (xap_dir, fname, NULL); |
|
60 |
len = strlen (output); |
|
61 |
||
62 |
for (i = 0; i < len; i++) { |
|
63 |
if (output[i] == '\\') |
|
64 |
output[i] = '/'; |
|
65 |
}
|
|
66 |
||
67 |
dirname = g_path_get_dirname (output); |
|
68 |
g_mkdir_with_parents (dirname, 0700); |
|
69 |
g_free (dirname); |
|
70 |
||
71 |
fd = open (output, O_CREAT | O_WRONLY, 0644); |
|
72 |
g_free (output); |
|
73 |
g_free (fname); |
|
74 |
||
75 |
if (fd == -1) |
|
76 |
goto exception1; |
|
77 |
||
78 |
if (unzOpenCurrentFile (zipfile) != UNZ_OK) |
|
79 |
goto exception1; |
|
80 |
||
81 |
bool exc = ExtractFile (zipfile, fd); |
|
82 |
unzCloseCurrentFile (zipfile); |
|
83 |
if (exc == false) |
|
84 |
goto exception1; |
|
85 |
} while (unzGoToNextFile (zipfile) == UNZ_OK); |
|
86 |
unzClose (zipfile); |
|
87 |
||
88 |
return xap_dir; |
|
89 |
||
90 |
exception1: |
|
91 |
unzClose (zipfile); |
|
92 |
||
93 |
exception0: |
|
94 |
RemoveDir (xap_dir); |
|
95 |
g_free (xap_dir); |
|
96 |
||
97 |
return NULL; |
|
98 |
}
|
|
99 |
||
100 |
Xap::Xap (XamlLoader *loader, char *xap_dir, DependencyObject *root) |
|
101 |
{
|
|
102 |
this->loader = loader; |
|
103 |
this->xap_dir = xap_dir; |
|
104 |
this->root = root; |
|
105 |
}
|
|
106 |
||
107 |
Xap::~Xap () |
|
108 |
{
|
|
109 |
g_free (xap_dir); |
|
110 |
xap_dir = NULL; |
|
111 |
}
|
|
112 |
||
113 |
Xap * |
|
114 |
xap_create_from_file (XamlLoader *loader, const char *filename) |
|
115 |
{
|
|
116 |
char *xap_dir = Xap::Unpack (filename); |
|
117 |
Type::Kind element_type; |
|
118 |
DependencyObject *element; |
|
119 |
||
120 |
if (xap_dir == NULL) |
|
121 |
return NULL; |
|
122 |
||
123 |
// Load the AppManifest file
|
|
124 |
char *manifest = g_build_filename (xap_dir, "AppManifest.xaml", NULL); |
|
125 |
element = loader->CreateFromFile (manifest, false, &element_type); |
|
126 |
g_free (manifest); |
|
127 |
||
128 |
if (element_type != Type::DEPLOYMENT) |
|
129 |
return NULL; |
|
130 |
||
131 |
// TODO: Create a DependencyObject from the root node.
|
|
132 |
||
133 |
Xap *xap = new Xap (loader, xap_dir, element); |
|
134 |
return xap; |
|
135 |
}
|