1/*
2 * Copyright 2005-2006, Jérôme DUVAL. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5
6#include "CopyEngine.h"
7#include "InstallerWindow.h"
8#include "PartitionMenuItem.h"
9#include <Alert.h>
10#include <FindDirectory.h>
11#include <Path.h>
12#include <String.h>
13#include <VolumeRoster.h>
14
15//#define COPY_TRACE
16#ifdef COPY_TRACE
17#define CALLED() 			printf("CALLED %s\n",__PRETTY_FUNCTION__)
18#else
19#define CALLED()
20#endif
21
22const char BOOT_PATH[] = "/boot";
23
24extern void SizeAsString(off_t size, char *string);
25
26
27CopyEngine::CopyEngine(InstallerWindow *window)
28	: BLooper("copy_engine"),
29	fWindow(window),
30	fPackages(NULL),
31	fSpaceRequired(0)
32{
33	Run();
34}
35
36
37void
38CopyEngine::MessageReceived(BMessage*msg)
39{
40	CALLED();
41	switch (msg->what) {
42		case ENGINE_START:
43			Start(fWindow->GetSourceMenu(), fWindow->GetTargetMenu());
44			break;
45	}
46}
47
48
49void
50CopyEngine::SetStatusMessage(char *status)
51{
52	BMessage msg(STATUS_MESSAGE);
53	msg.AddString("status", status);
54	BMessenger(fWindow).SendMessage(&msg);
55}
56
57
58void
59CopyEngine::LaunchInitScript(BPath &path)
60{
61	BPath bootPath;
62	find_directory(B_BEOS_BOOT_DIRECTORY, &bootPath);
63	BString command("/bin/sh ");
64	command += bootPath.Path();
65	command += "/InstallerInitScript ";
66	command += path.Path();
67	SetStatusMessage("Starting Installation.");
68	system(command.String());
69}
70
71
72void
73CopyEngine::LaunchFinishScript(BPath &path)
74{
75	BPath bootPath;
76	find_directory(B_BEOS_BOOT_DIRECTORY, &bootPath);
77	BString command("/bin/sh ");
78	command += bootPath.Path();
79	command += "/InstallerFinishScript ";
80	command += path.Path();
81	SetStatusMessage("Finishing Installation.");
82	system(command.String());
83}
84
85
86void
87CopyEngine::Start(BMenu *srcMenu, BMenu *targetMenu)
88{
89	CALLED();
90	PartitionMenuItem *targetItem = (PartitionMenuItem *)targetMenu->FindMarked();
91	PartitionMenuItem *srcItem = (PartitionMenuItem *)srcMenu->FindMarked();
92	if (!srcItem || !targetItem) {
93		fprintf(stderr, "bad menu items\n");
94		return;
95	}
96
97	BMessage msg(INSTALL_FINISHED);
98	BMessenger(fWindow).SendMessage(&msg);
99}
100
101
102void
103CopyEngine::ScanDisksPartitions(BMenu *srcMenu, BMenu *targetMenu)
104{
105	PartitionMenuItem *item = new PartitionMenuItem(NULL, "boot", NULL, new BMessage(SRC_PARTITION), 0);
106	srcMenu->AddItem(item);
107
108	PartitionMenuItem *item2 = new PartitionMenuItem(NULL, "target", NULL, new BMessage(TARGET_PARTITION), 0);
109	targetMenu->AddItem(item2);
110}
111
112
113void
114CopyEngine::SetPackagesList(BList *list)
115{
116	if (fPackages)
117		delete fPackages;
118	fPackages = list;
119}
120
121