PackageManager: Options.xml
[reactos.git] / rosapps / packmgr / lib / main.cpp
1 ////////////////////////////////////////////////////////
2 //
3 // main.cpp
4 //
5 // Doit stuff and
6 // everything that fits nowhere else.
7 //
8 //
9 // Maarten Bosma, 09.01.2004
10 // maarten.paul@bosma.de
11 //
12 ////////////////////////////////////////////////////////////////////
13
14 #include <windows.h>
15 #include "package.hpp"
16 #include "log.h"
17 #include "script.h"
18
19 HANDLE hThread = NULL;
20 BOOL thread_abort = FALSE;
21
22 char* PML_Download (pTree, const char* url, const char* server = "tree", const char* filename = NULL);
23
24
25 // Abort other thread
26 extern "C" void PML_Abort (void)
27 {
28 thread_abort = TRUE;
29
30 if(hThread)
31 WaitForSingleObject(hThread, INFINITE);
32 }
33
34 // Callback function of the "doit"-thread
35 DWORD WINAPI DoitThread (void* lpParam)
36 {
37 UINT i;
38 int ret = ERR_OK;
39 TREE* tree = (TREE*)lpParam;
40 vector<SCRIPT*> scripts;
41
42 /* Load the scripts */
43
44 tree->setStatus(0, 0, L"Downloading Install instructions ...");
45
46 for(i=0; i<tree->todo.size(); i++)
47 {
48 SCRIPT* script;
49
50 char* path = PML_Download(tree, tree->todo[i]);
51
52 if(RPS_Load(&script, path) == ERR_OK)
53 scripts.push_back(script);
54 else
55 ret = ERR_PACK;
56 }
57
58 /* Preinstall */
59
60 Log("* enter preinstall");
61
62 tree->setStatus(250, 0, L"Preinstall");
63
64 for(i=0; i<scripts.size(); i++)
65 {
66 if(RPS_Execute(scripts[i], "preinstall") != ERR_OK)
67 ret = ERR_PACK;
68 }
69
70 /* Install */
71
72 Log("* enter install");
73
74 tree->setStatus(500, 0, L"Install");
75
76 for(i=0; i<scripts.size(); i++)
77 {
78 if(RPS_Execute(scripts[i], "main") != ERR_OK)
79 ret = ERR_PACK;
80 }
81
82 /* Postinstall */
83
84 Log("* enter postinstall");
85
86 tree->setStatus(750, 0, L"Postinstall");
87
88 for(i=0; i<scripts.size(); i++)
89 {
90 if(RPS_Execute(scripts[i], "after") != ERR_OK)
91 ret = ERR_PACK;
92 }
93
94 /* Finish */
95
96 for(i=0; i<tree->todo.size(); i++)
97 RPS_Clear(scripts[i]);
98
99 // clear the todo list
100 tree->todo.clear();
101
102 // set all actions to none
103 for(i=0; i<tree->packages.size(); i++)
104 PML_SetAction (tree, i, 0, tree->setIcon, NULL);
105
106 tree->setStatus(1000, ret, NULL);
107
108 return 1;
109 }
110
111 // Do the actions the user wants us to do
112 extern "C" int PML_DoIt (TREE* tree, PML_SetStatus SetStatus, PML_Ask Ask)
113 {
114 DWORD dummy;
115 tree->setStatus = SetStatus;
116
117 if(!tree->todo.size())
118 return ERR_NOTODO;
119
120 //ask
121 WCHAR buffer [2000];
122 wsprintf(buffer, PML_TransError(ERR_READY), tree->todo.size());
123
124 if(!Ask(buffer))
125 return ERR_GENERIC;
126
127
128 hThread = CreateThread(NULL, 0, DoitThread, tree, 0, &dummy);
129
130 if(!hThread)
131 return ERR_GENERIC;
132
133 LogAdd("\n");
134
135 return ERR_OK;
136 }
137
138 // Translates Errorcode into human language
139 extern "C" WCHAR* PML_TransError (int code)
140 {
141 // I know we, got a memory leak here
142 static WCHAR string [256];
143
144 if(!LoadString(GetModuleHandle(L"package"), code, string, 256))
145 return PML_TransError(ERR_GENERIC);
146
147 return string;
148 }
149
150 // Free alloced memory
151 extern "C" void PML_CloseTree (TREE* tree)
152 {
153 UINT i;
154
155 LogAdd ("\n");
156 Log("* free alloced memory");
157 Log("* package manager will exit now. Bye!");
158
159 for(i=0; i<tree->packages.size(); i++)
160 {
161 if(tree->packages[i].path)
162 delete tree->packages[i].path;
163
164 if(tree->packages[i].name)
165 delete tree->packages[i].name;
166
167 if(tree->packages[i].description)
168 delete tree->packages[i].description;
169
170 tree->packages.clear();
171
172 if(tree->packages[i].files[0])
173 delete tree->packages[i].files[0];
174
175 if(tree->packages[i].files[1])
176 delete tree->packages[i].files[1];
177
178 if(tree->packages[i].files[2])
179 delete tree->packages[i].files[2];
180
181 if(tree->packages[i].files[3])
182 delete tree->packages[i].files[3];
183 }
184
185 tree->descriptionPath.clear();
186 tree->todo.clear();
187 tree->packages.clear();
188
189 if(tree)
190 delete tree;
191 }
192