xref: /haiku/src/apps/installer/InstallerWindow.cpp (revision 68ea01249e1e2088933cb12f9c28d4e5c5d1c9ef)
1 /*
2  * Copyright 2009-2010, Stephan Aßmus <superstippi@gmx.de>
3  * Copyright 2005-2008, Jérôme DUVAL
4  * All rights reserved. Distributed under the terms of the MIT License.
5  */
6 
7 
8 #include "InstallerWindow.h"
9 
10 #include <stdio.h>
11 #include <strings.h>
12 
13 #include <Alert.h>
14 #include <Application.h>
15 #include <Autolock.h>
16 #include <Box.h>
17 #include <Button.h>
18 #include <Catalog.h>
19 #include <ControlLook.h>
20 #include <Directory.h>
21 #include <FindDirectory.h>
22 #include <LayoutBuilder.h>
23 #include <LayoutUtils.h>
24 #include <Locale.h>
25 #include <MenuBar.h>
26 #include <MenuField.h>
27 #include <Path.h>
28 #include <PopUpMenu.h>
29 #include <Roster.h>
30 #include <Screen.h>
31 #include <ScrollView.h>
32 #include <SeparatorView.h>
33 #include <SpaceLayoutItem.h>
34 #include <StatusBar.h>
35 #include <String.h>
36 #include <TextView.h>
37 #include <TranslationUtils.h>
38 #include <TranslatorFormats.h>
39 
40 #include "tracker_private.h"
41 
42 #include "DialogPane.h"
43 #include "InstallerDefs.h"
44 #include "PackageViews.h"
45 #include "PartitionMenuItem.h"
46 #include "WorkerThread.h"
47 
48 
49 #undef B_TRANSLATION_CONTEXT
50 #define B_TRANSLATION_CONTEXT "InstallerWindow"
51 
52 
53 static const char* kDriveSetupSignature = "application/x-vnd.Haiku-DriveSetup";
54 static const char* kBootManagerSignature
55 	= "application/x-vnd.Haiku-BootManager";
56 
57 const uint32 BEGIN_MESSAGE = 'iBGN';
58 const uint32 SHOW_BOTTOM_MESSAGE = 'iSBT';
59 const uint32 LAUNCH_DRIVE_SETUP = 'iSEP';
60 const uint32 LAUNCH_BOOTMAN = 'iWBM';
61 const uint32 START_SCAN = 'iSSC';
62 const uint32 PACKAGE_CHECKBOX = 'iPCB';
63 const uint32 ENCOURAGE_DRIVESETUP = 'iENC';
64 
65 
66 class LogoView : public BView {
67 public:
68 								LogoView(const BRect& frame);
69 								LogoView();
70 	virtual						~LogoView();
71 
72 	virtual	void				Draw(BRect update);
73 
74 	virtual	void				GetPreferredSize(float* _width,
75 									float* _height);
76 
77 private:
78 			void				_Init();
79 
80 			BBitmap*			fLogo;
81 };
82 
83 
84 LogoView::LogoView(const BRect& frame)
85 	:
86 	BView(frame, "logoview", B_FOLLOW_LEFT | B_FOLLOW_TOP,
87 		B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE)
88 {
89 	_Init();
90 }
91 
92 
93 LogoView::LogoView()
94 	:
95 	BView("logoview", B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE)
96 {
97 	_Init();
98 }
99 
100 
101 LogoView::~LogoView(void)
102 {
103 	delete fLogo;
104 }
105 
106 
107 void
108 LogoView::Draw(BRect update)
109 {
110 	if (fLogo == NULL)
111 		return;
112 
113 	BRect bounds(Bounds());
114 	BPoint placement;
115 	placement.x = (bounds.left + bounds.right - fLogo->Bounds().Width()) / 2;
116 	placement.y = (bounds.top + bounds.bottom - fLogo->Bounds().Height()) / 2;
117 
118 	DrawBitmap(fLogo, placement);
119 }
120 
121 
122 void
123 LogoView::GetPreferredSize(float* _width, float* _height)
124 {
125 	float width = 0.0;
126 	float height = 0.0;
127 	if (fLogo) {
128 		width = fLogo->Bounds().Width();
129 		height = fLogo->Bounds().Height();
130 	}
131 	if (_width)
132 		*_width = width;
133 	if (_height)
134 		*_height = height;
135 }
136 
137 
138 void
139 LogoView::_Init()
140 {
141 	fLogo = BTranslationUtils::GetBitmap(B_PNG_FORMAT, "logo.png");
142 }
143 
144 
145 // #pragma mark -
146 
147 
148 static BLayoutItem*
149 layout_item_for(BView* view)
150 {
151 	BLayout* layout = view->Parent()->GetLayout();
152 	int32 index = layout->IndexOfView(view);
153 	return layout->ItemAt(index);
154 }
155 
156 
157 InstallerWindow::InstallerWindow()
158 	:
159 	BWindow(BRect(-2000, -2000, -1800, -1800),
160 		B_TRANSLATE_SYSTEM_NAME("Installer"), B_TITLED_WINDOW,
161 		B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS),
162 	fEncouragedToSetupPartitions(false),
163 	fDriveSetupLaunched(false),
164 	fBootManagerLaunched(false),
165 	fInstallStatus(kReadyForInstall),
166 	fWorkerThread(new WorkerThread(this)),
167 	fCopyEngineCancelSemaphore(-1)
168 {
169 	if (!be_roster->IsRunning(kTrackerSignature))
170 		SetWorkspaces(B_ALL_WORKSPACES);
171 
172 	LogoView* logoView = new LogoView();
173 
174 	fStatusView = new BTextView("statusView", be_plain_font, NULL,
175 		B_WILL_DRAW);
176 	fStatusView->SetViewColor(255, 255, 255, 255);
177 	fStatusView->MakeEditable(false);
178 	fStatusView->MakeSelectable(false);
179 
180 	BSize logoSize = logoView->MinSize();
181 	logoView->SetExplicitMaxSize(logoSize);
182 	fStatusView->SetExplicitMinSize(BSize(fStatusView->StringWidth("W") * 22,
183 		logoSize.height));
184 
185 	// Explicitly create group view to set the background white in case
186 	// height resizing is needed for the status view
187 	fLogoGroup = new BGroupView(B_HORIZONTAL, 10);
188 	fLogoGroup->SetViewColor(255, 255, 255);
189 	fLogoGroup->GroupLayout()->SetInsets(0, 0, 10, 0);
190 	fLogoGroup->AddChild(logoView);
191 	fLogoGroup->AddChild(fStatusView);
192 
193 	fDestMenu = new BPopUpMenu(B_TRANSLATE("scanning" B_UTF8_ELLIPSIS),
194 		true, false);
195 	fSrcMenu = new BPopUpMenu(B_TRANSLATE("scanning" B_UTF8_ELLIPSIS),
196 		true, false);
197 
198 	fSrcMenuField = new BMenuField("srcMenuField",
199 		B_TRANSLATE("Install from:"), fSrcMenu);
200 	fSrcMenuField->SetAlignment(B_ALIGN_RIGHT);
201 
202 	fDestMenuField = new BMenuField("destMenuField", B_TRANSLATE("Onto:"),
203 		fDestMenu);
204 	fDestMenuField->SetAlignment(B_ALIGN_RIGHT);
205 
206 	fPackagesSwitch = new PaneSwitch("options_button");
207 	fPackagesSwitch->SetLabels(B_TRANSLATE("Hide optional packages"),
208 		B_TRANSLATE("Show optional packages"));
209 	fPackagesSwitch->SetMessage(new BMessage(SHOW_BOTTOM_MESSAGE));
210 	fPackagesSwitch->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED,
211 		B_SIZE_UNSET));
212 	fPackagesSwitch->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT,
213 		B_ALIGN_TOP));
214 
215 	fPackagesView = new PackagesView("packages_view");
216 	BScrollView* packagesScrollView = new BScrollView("packagesScroll",
217 		fPackagesView, B_WILL_DRAW, false, true);
218 
219 	const char* requiredDiskSpaceString
220 		= B_TRANSLATE("Additional disk space required: 0.0 KiB");
221 	fSizeView = new BStringView("size_view", requiredDiskSpaceString);
222 	fSizeView->SetAlignment(B_ALIGN_RIGHT);
223 	fSizeView->SetExplicitAlignment(
224 		BAlignment(B_ALIGN_RIGHT, B_ALIGN_TOP));
225 
226 	fProgressBar = new BStatusBar("progress",
227 		B_TRANSLATE("Install progress:  "));
228 	fProgressBar->SetMaxValue(100.0);
229 
230 	fBeginButton = new BButton("begin_button", B_TRANSLATE("Begin"),
231 		new BMessage(BEGIN_MESSAGE));
232 	fBeginButton->MakeDefault(true);
233 	fBeginButton->SetEnabled(false);
234 
235 	fLaunchDriveSetupButton = new BButton("setup_button",
236 		B_TRANSLATE("Set up partitions" B_UTF8_ELLIPSIS),
237 		new BMessage(LAUNCH_DRIVE_SETUP));
238 
239 	fLaunchBootManagerItem = new BMenuItem(B_TRANSLATE("Set up boot menu"),
240 		new BMessage(LAUNCH_BOOTMAN));
241 	fLaunchBootManagerItem->SetEnabled(false);
242 
243 	fMakeBootableItem = new BMenuItem(B_TRANSLATE("Write boot sector"),
244 		new BMessage(MSG_WRITE_BOOT_SECTOR));
245 	fMakeBootableItem->SetEnabled(false);
246 	BMenuBar* mainMenu = new BMenuBar("main menu");
247 	BMenu* toolsMenu = new BMenu(B_TRANSLATE("Tools"));
248 	toolsMenu->AddItem(fLaunchBootManagerItem);
249 	toolsMenu->AddItem(fMakeBootableItem);
250 	mainMenu->AddItem(toolsMenu);
251 
252 	BGroupView* packagesGroup = new BGroupView(B_VERTICAL, B_USE_ITEM_SPACING);
253 	packagesGroup->AddChild(fPackagesSwitch);
254 	packagesGroup->AddChild(packagesScrollView);
255 	packagesGroup->AddChild(fProgressBar);
256 	packagesGroup->AddChild(fSizeView);
257 
258 	BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
259 		.Add(mainMenu)
260 		.Add(fLogoGroup)
261 		.Add(new BSeparatorView(B_HORIZONTAL, B_PLAIN_BORDER))
262 		.AddGroup(B_VERTICAL, B_USE_ITEM_SPACING)
263 			.SetInsets(B_USE_WINDOW_SPACING)
264 			.AddGrid(new BGridView(B_USE_ITEM_SPACING, B_USE_ITEM_SPACING))
265 				.AddMenuField(fSrcMenuField, 0, 0)
266 				.AddMenuField(fDestMenuField, 0, 1)
267 				.AddGlue(2, 0, 1, 2)
268 				.Add(BSpaceLayoutItem::CreateVerticalStrut(5), 0, 2, 3)
269 			.End()
270 			.Add(packagesGroup)
271 			.AddGroup(B_HORIZONTAL, B_USE_WINDOW_SPACING)
272 				.Add(fLaunchDriveSetupButton)
273 				.AddGlue()
274 				.Add(fBeginButton)
275 			.End()
276 		.End()
277 	.End();
278 
279 	// Make the optional packages and progress bar invisible on start
280 	fPackagesLayoutItem = layout_item_for(packagesScrollView);
281 	fPkgSwitchLayoutItem = layout_item_for(fPackagesSwitch);
282 	fSizeViewLayoutItem = layout_item_for(fSizeView);
283 	fProgressLayoutItem = layout_item_for(fProgressBar);
284 
285 	fPackagesLayoutItem->SetVisible(false);
286 	fSizeViewLayoutItem->SetVisible(false);
287 	fProgressLayoutItem->SetVisible(false);
288 
289 	// Setup tool tips for the non-obvious features
290 	fLaunchDriveSetupButton->SetToolTip(
291 		B_TRANSLATE("Launch the DriveSetup utility to partition\n"
292 		"available hard drives and other media.\n"
293 		"Partitions can be initialized with the\n"
294 		"Be File System needed for a Haiku boot\n"
295 		"partition."));
296 //	fLaunchBootManagerItem->SetToolTip(
297 //		B_TRANSLATE("Install or uninstall the Haiku boot menu, which allows "
298 //		"to choose an operating system to boot when the computer starts.\n"
299 //		"If this computer already has a boot manager such as GRUB installed, "
300 //		"it is better to add Haiku to that menu than to overwrite it."));
301 //	fMakeBootableItem->SetToolTip(
302 //		B_TRANSLATE("Writes the Haiku boot code to the partition start\n"
303 //		"sector. This step is automatically performed by\n"
304 //		"the installation, but you can manually make a\n"
305 //		"partition bootable in case you do not need to\n"
306 //		"perform an installation."));
307 
308 	// finish creating window
309 	if (!be_roster->IsRunning(kDeskbarSignature))
310 		SetFlags(Flags() | B_NOT_MINIMIZABLE);
311 
312 	CenterOnScreen();
313 	Show();
314 
315 	// Register to receive notifications when apps launch or quit...
316 	be_roster->StartWatching(this);
317 	// ... and check the two we are interested in.
318 	fDriveSetupLaunched = be_roster->IsRunning(kDriveSetupSignature);
319 	fBootManagerLaunched = be_roster->IsRunning(kBootManagerSignature);
320 
321 	if (Lock()) {
322 		fLaunchDriveSetupButton->SetEnabled(!fDriveSetupLaunched);
323 		fLaunchBootManagerItem->SetEnabled(!fBootManagerLaunched);
324 		Unlock();
325 	}
326 
327 	PostMessage(START_SCAN);
328 }
329 
330 
331 InstallerWindow::~InstallerWindow()
332 {
333 	_SetCopyEngineCancelSemaphore(-1);
334 	be_roster->StopWatching(this);
335 }
336 
337 
338 void
339 InstallerWindow::MessageReceived(BMessage *msg)
340 {
341 	switch (msg->what) {
342 		case MSG_RESET:
343 		{
344 			_SetCopyEngineCancelSemaphore(-1);
345 
346 			status_t error;
347 			if (msg->FindInt32("error", &error) == B_OK) {
348 				char errorMessage[2048];
349 				snprintf(errorMessage, sizeof(errorMessage),
350 					B_TRANSLATE("An error was encountered and the "
351 					"installation was not completed:\n\n"
352 					"Error:  %s"), strerror(error));
353 				BAlert* alert = new BAlert("error", errorMessage, B_TRANSLATE("OK"));
354 				alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
355 				alert->Go();
356 			}
357 
358 			_DisableInterface(false);
359 
360 			fProgressLayoutItem->SetVisible(false);
361 			fPkgSwitchLayoutItem->SetVisible(true);
362 			_ShowOptionalPackages();
363 			_UpdateControls();
364 			break;
365 		}
366 		case START_SCAN:
367 			_ScanPartitions();
368 			break;
369 		case BEGIN_MESSAGE:
370 			switch (fInstallStatus) {
371 				case kReadyForInstall:
372 				{
373 					// get source and target
374 					PartitionMenuItem* targetItem
375 						= (PartitionMenuItem*)fDestMenu->FindMarked();
376 					PartitionMenuItem* srcItem
377 						= (PartitionMenuItem*)fSrcMenu->FindMarked();
378 					if (srcItem == NULL || targetItem == NULL)
379 						break;
380 
381 					_SetCopyEngineCancelSemaphore(create_sem(1,
382 						"copy engine cancel"));
383 
384 					BList* list = new BList();
385 					int32 size = 0;
386 					fPackagesView->GetPackagesToInstall(list, &size);
387 					fWorkerThread->SetLock(fCopyEngineCancelSemaphore);
388 					fWorkerThread->SetPackagesList(list);
389 					fWorkerThread->SetSpaceRequired(size);
390 					fInstallStatus = kInstalling;
391 					fWorkerThread->StartInstall(srcItem->ID(),
392 						targetItem->ID());
393 					fBeginButton->SetLabel(B_TRANSLATE("Stop"));
394 					_DisableInterface(true);
395 
396 					fProgressBar->SetTo(0.0, NULL, NULL);
397 
398 					fPkgSwitchLayoutItem->SetVisible(false);
399 					fPackagesLayoutItem->SetVisible(false);
400 					fSizeViewLayoutItem->SetVisible(false);
401 					fProgressLayoutItem->SetVisible(true);
402 					break;
403 				}
404 				case kInstalling:
405 				{
406 					_QuitCopyEngine(true);
407 					break;
408 				}
409 				case kFinished:
410 					PostMessage(B_QUIT_REQUESTED);
411 					break;
412 				case kCancelled:
413 					break;
414 			}
415 			break;
416 		case SHOW_BOTTOM_MESSAGE:
417 			_ShowOptionalPackages();
418 			break;
419 		case SOURCE_PARTITION:
420 			_PublishPackages();
421 			_UpdateControls();
422 			break;
423 		case TARGET_PARTITION:
424 			_UpdateControls();
425 			break;
426 		case LAUNCH_DRIVE_SETUP:
427 			_LaunchDriveSetup();
428 			break;
429 		case LAUNCH_BOOTMAN:
430 			_LaunchBootManager();
431 			break;
432 		case PACKAGE_CHECKBOX:
433 		{
434 			char buffer[15];
435 			fPackagesView->GetTotalSizeAsString(buffer, sizeof(buffer));
436 			char string[256];
437 			snprintf(string, sizeof(string),
438 				B_TRANSLATE("Additional disk space required: %s"), buffer);
439 			fSizeView->SetText(string);
440 			fSizeView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
441 			break;
442 		}
443 		case ENCOURAGE_DRIVESETUP:
444 		{
445 			BAlert* alert = new BAlert("use drive setup", B_TRANSLATE("No partitions have "
446 				"been found that are suitable for installation. Please set "
447 				"up partitions and initialize at least one partition with the "
448 				"Be File System."), B_TRANSLATE("OK"));
449 			alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
450 			alert->Go();
451 			break;
452 		}
453 		case MSG_STATUS_MESSAGE:
454 		{
455 			float progress;
456 			if (msg->FindFloat("progress", &progress) == B_OK) {
457 				const char* currentItem;
458 				if (msg->FindString("item", &currentItem) != B_OK) {
459 					currentItem = B_TRANSLATE_COMMENT("???",
460 						"Unknown currently copied item");
461 				}
462 				BString trailingLabel;
463 				int32 currentCount;
464 				int32 maximumCount;
465 				if (msg->FindInt32("current", &currentCount) == B_OK
466 					&& msg->FindInt32("maximum", &maximumCount) == B_OK) {
467 					char buffer[64];
468 					snprintf(buffer, sizeof(buffer),
469 						B_TRANSLATE_COMMENT("%1ld of %2ld",
470 							"number of files copied"),
471 						currentCount, maximumCount);
472 					trailingLabel << buffer;
473 				} else {
474 					trailingLabel <<
475 						B_TRANSLATE_COMMENT("?? of ??", "Unknown progress");
476 				}
477 				fProgressBar->SetTo(progress, currentItem,
478 					trailingLabel.String());
479 			} else {
480 				const char *status;
481 				if (msg->FindString("status", &status) == B_OK) {
482 					fLastStatus = fStatusView->Text();
483 					_SetStatusMessage(status);
484 				} else
485 					_SetStatusMessage(fLastStatus.String());
486 			}
487 			break;
488 		}
489 		case MSG_INSTALL_FINISHED:
490 		{
491 
492 			_SetCopyEngineCancelSemaphore(-1);
493 
494 			PartitionMenuItem* dstItem
495 				= (PartitionMenuItem*)fDestMenu->FindMarked();
496 
497 			BString status;
498 			if (be_roster->IsRunning(kDeskbarSignature)) {
499 				fBeginButton->SetLabel(B_TRANSLATE("Quit"));
500 				status.SetToFormat(B_TRANSLATE("Installation "
501 					"completed. Boot sector has been written to '%s'. Press "
502 					"Quit to leave the Installer or choose a new target "
503 					"volume to perform another installation."),
504 					dstItem ? dstItem->Name() : B_TRANSLATE_COMMENT("???",
505 						"Unknown partition name"));
506 			} else {
507 				fBeginButton->SetLabel(B_TRANSLATE("Restart"));
508 				status.SetToFormat(B_TRANSLATE("Installation "
509 					"completed. Boot sector has been written to '%s'. Press "
510 					"Restart to restart the computer or choose a new target "
511 					"volume to perform another installation."),
512 					dstItem ? dstItem->Name() : B_TRANSLATE_COMMENT("???",
513 						"Unknown partition name"));
514 			}
515 
516 			_SetStatusMessage(status.String());
517 			fInstallStatus = kFinished;
518 			_DisableInterface(false);
519 			fProgressLayoutItem->SetVisible(false);
520 			fPkgSwitchLayoutItem->SetVisible(true);
521 			_ShowOptionalPackages();
522 			break;
523 		}
524 		case B_SOME_APP_LAUNCHED:
525 		case B_SOME_APP_QUIT:
526 		{
527 			const char *signature;
528 			if (msg->FindString("be:signature", &signature) != B_OK)
529 				break;
530 			bool isDriveSetup = !strcasecmp(signature, kDriveSetupSignature);
531 			bool isBootManager = !strcasecmp(signature, kBootManagerSignature);
532 			if (isDriveSetup || isBootManager) {
533 				bool scanPartitions = false;
534 				if (isDriveSetup) {
535 					bool launched = msg->what == B_SOME_APP_LAUNCHED;
536 					// We need to scan partitions if DriveSetup has quit.
537 					scanPartitions = fDriveSetupLaunched && !launched;
538 					fDriveSetupLaunched = launched;
539 				}
540 				if (isBootManager)
541 					fBootManagerLaunched = msg->what == B_SOME_APP_LAUNCHED;
542 
543 				fBeginButton->SetEnabled(
544 					!fDriveSetupLaunched && !fBootManagerLaunched);
545 				_DisableInterface(fDriveSetupLaunched || fBootManagerLaunched);
546 				if (fDriveSetupLaunched && fBootManagerLaunched) {
547 					_SetStatusMessage(B_TRANSLATE("Running Boot Manager and "
548 						"DriveSetup" B_UTF8_ELLIPSIS
549 						"\n\nClose both applications to continue with the "
550 						"installation."));
551 				} else if (fDriveSetupLaunched) {
552 					_SetStatusMessage(B_TRANSLATE("Running DriveSetup"
553 						B_UTF8_ELLIPSIS
554 						"\n\nClose DriveSetup to continue with the "
555 						"installation."));
556 				} else if (fBootManagerLaunched) {
557 					_SetStatusMessage(B_TRANSLATE("Running Boot Manager"
558 						B_UTF8_ELLIPSIS
559 						"\n\nClose Boot Manager to continue with the "
560 						"installation."));
561 				} else {
562 					// If neither DriveSetup nor Bootman is running, we need
563 					// to scan partitions in case DriveSetup has quit, or
564 					// we need to update the guidance message, unless install
565 					// was already finished.
566 					if (scanPartitions)
567 						_ScanPartitions();
568 					else if (fInstallStatus != kFinished)
569 						_UpdateControls();
570 					else
571 						PostMessage(MSG_INSTALL_FINISHED);
572 				}
573 			}
574 			break;
575 		}
576 		case MSG_WRITE_BOOT_SECTOR:
577 			fWorkerThread->WriteBootSector(fDestMenu);
578 			break;
579 
580 		default:
581 			BWindow::MessageReceived(msg);
582 			break;
583 	}
584 }
585 
586 
587 bool
588 InstallerWindow::QuitRequested()
589 {
590 	if ((Flags() & B_NOT_MINIMIZABLE) != 0) {
591 		// This means Deskbar is not running, i.e. Installer is the only
592 		// thing on the screen and we will reboot the machine once it quits.
593 
594 		if (fDriveSetupLaunched && fBootManagerLaunched) {
595 			BAlert* alert = new BAlert(B_TRANSLATE("Quit Boot Manager and "
596 				"DriveSetup"),	B_TRANSLATE("Please close the Boot Manager "
597 				"and DriveSetup windows before closing the Installer window."),
598 				B_TRANSLATE("OK"));
599 			alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
600 			alert->Go();
601 			return false;
602 		}
603 		if (fDriveSetupLaunched) {
604 			BAlert* alert = new BAlert(B_TRANSLATE("Quit DriveSetup"),
605 				B_TRANSLATE("Please close the DriveSetup window before "
606 				"closing the Installer window."), B_TRANSLATE("OK"));
607 			alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
608 			alert->Go();
609 			return false;
610 		}
611 		if (fBootManagerLaunched) {
612 			BAlert* alert = new BAlert(B_TRANSLATE("Quit Boot Manager"),
613 				B_TRANSLATE("Please close the Boot Manager window before "
614 				"closing the Installer window."), B_TRANSLATE("OK"));
615 			alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
616 			alert->Go();
617 			return false;
618 		}
619 		if (fInstallStatus != kFinished) {
620 			BAlert* alert = new BAlert(B_TRANSLATE_SYSTEM_NAME("Installer"),
621 				B_TRANSLATE("Are you sure you want to abort the "
622 					"installation and restart the system?"),
623 				B_TRANSLATE("Cancel"), B_TRANSLATE("Restart system"), NULL,
624 				B_WIDTH_AS_USUAL, B_STOP_ALERT);
625 			alert->SetShortcut(0, B_ESCAPE);
626 			if (alert->Go() == 0)
627 				return false;
628 		}
629 	} else if (fInstallStatus == kInstalling) {
630 			BAlert* alert = new BAlert(B_TRANSLATE_SYSTEM_NAME("Installer"),
631 				B_TRANSLATE("Are you sure you want to abort the installation?"),
632 				B_TRANSLATE("Cancel"), B_TRANSLATE("Abort"), NULL,
633 				B_WIDTH_AS_USUAL, B_STOP_ALERT);
634 			alert->SetShortcut(0, B_ESCAPE);
635 			if (alert->Go() == 0)
636 				return false;
637 	}
638 
639 	_QuitCopyEngine(false);
640 	fWorkerThread->PostMessage(B_QUIT_REQUESTED);
641 	be_app->PostMessage(B_QUIT_REQUESTED);
642 	return true;
643 }
644 
645 
646 // #pragma mark -
647 
648 
649 void
650 InstallerWindow::_ShowOptionalPackages()
651 {
652 	if (fPackagesLayoutItem && fSizeViewLayoutItem) {
653 		fPackagesLayoutItem->SetVisible(fPackagesSwitch->Value());
654 		fSizeViewLayoutItem->SetVisible(fPackagesSwitch->Value());
655 	}
656 }
657 
658 
659 void
660 InstallerWindow::_LaunchDriveSetup()
661 {
662 	if (be_roster->Launch(kDriveSetupSignature) != B_OK) {
663 		// Try really hard to launch it. It's very likely that this fails,
664 		// when we run from the CD and there is only an incomplete mime
665 		// database for example...
666 		BPath path;
667 		if (find_directory(B_SYSTEM_APPS_DIRECTORY, &path) != B_OK
668 			|| path.Append("DriveSetup") != B_OK) {
669 			path.SetTo("/boot/system/apps/DriveSetup");
670 		}
671 		BEntry entry(path.Path());
672 		entry_ref ref;
673 		if (entry.GetRef(&ref) != B_OK || be_roster->Launch(&ref) != B_OK) {
674 			BAlert* alert = new BAlert("error", B_TRANSLATE("DriveSetup, the "
675 				"application to configure disk partitions, could not be "
676 				"launched."), B_TRANSLATE("OK"));
677 			alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
678 			alert->Go();
679 		}
680 	}
681 }
682 
683 
684 void
685 InstallerWindow::_LaunchBootManager()
686 {
687 	// TODO: Currently BootManager always tries to install to the "first"
688 	// harddisk. If/when it later supports being installed to a certain
689 	// harddisk, we would have to pass it the disk that contains the target
690 	// partition here.
691 	if (be_roster->Launch(kBootManagerSignature) != B_OK) {
692 		// Try really hard to launch it. It's very likely that this fails,
693 		// when we run from the CD and there is only an incomplete mime
694 		// database for example...
695 		BPath path;
696 		if (find_directory(B_SYSTEM_APPS_DIRECTORY, &path) != B_OK
697 			|| path.Append("BootManager") != B_OK) {
698 			path.SetTo("/boot/system/apps/BootManager");
699 		}
700 		BEntry entry(path.Path());
701 		entry_ref ref;
702 		if (entry.GetRef(&ref) != B_OK || be_roster->Launch(&ref) != B_OK) {
703 			BAlert* alert = new BAlert(
704 				B_TRANSLATE("Failed to launch Boot Manager"),
705 				B_TRANSLATE("Boot Manager, the application to configure the "
706 					"Haiku boot menu, could not be launched."),
707 				B_TRANSLATE("OK"), NULL, NULL, B_WIDTH_AS_USUAL, B_STOP_ALERT);
708 			alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
709 			alert->Go();
710 		}
711 	}
712 }
713 
714 
715 void
716 InstallerWindow::_DisableInterface(bool disable)
717 {
718 	fLaunchDriveSetupButton->SetEnabled(!disable);
719 	fLaunchBootManagerItem->SetEnabled(!disable);
720 	fMakeBootableItem->SetEnabled(!disable);
721 	fSrcMenuField->SetEnabled(!disable);
722 	fDestMenuField->SetEnabled(!disable);
723 }
724 
725 
726 void
727 InstallerWindow::_ScanPartitions()
728 {
729 	_SetStatusMessage(B_TRANSLATE("Scanning for disks" B_UTF8_ELLIPSIS));
730 
731 	BMenuItem *item;
732 	while ((item = fSrcMenu->RemoveItem((int32)0)))
733 		delete item;
734 	while ((item = fDestMenu->RemoveItem((int32)0)))
735 		delete item;
736 
737 	fWorkerThread->ScanDisksPartitions(fSrcMenu, fDestMenu);
738 
739 	if (fSrcMenu->ItemAt(0) != NULL)
740 		_PublishPackages();
741 
742 	_UpdateControls();
743 }
744 
745 
746 void
747 InstallerWindow::_UpdateControls()
748 {
749 	PartitionMenuItem* srcItem = (PartitionMenuItem*)fSrcMenu->FindMarked();
750 	BString label;
751 	if (srcItem) {
752 		label = srcItem->MenuLabel();
753 	} else {
754 		if (fSrcMenu->CountItems() == 0)
755 			label = B_TRANSLATE_COMMENT("<none>", "No partition available");
756 		else
757 			label = ((PartitionMenuItem*)fSrcMenu->ItemAt(0))->MenuLabel();
758 	}
759 	fSrcMenuField->MenuItem()->SetLabel(label.String());
760 
761 	// Disable any unsuitable target items, check if at least one partition
762 	// is suitable.
763 	bool foundOneSuitableTarget = false;
764 	for (int32 i = fDestMenu->CountItems() - 1; i >= 0; i--) {
765 		PartitionMenuItem* dstItem
766 			= (PartitionMenuItem*)fDestMenu->ItemAt(i);
767 		if (srcItem != NULL && dstItem->ID() == srcItem->ID()) {
768 			// Prevent the user from having picked the same partition as source
769 			// and destination.
770 			dstItem->SetEnabled(false);
771 			dstItem->SetMarked(false);
772 		} else
773 			dstItem->SetEnabled(dstItem->IsValidTarget());
774 
775 		if (dstItem->IsEnabled())
776 			foundOneSuitableTarget = true;
777 	}
778 
779 	PartitionMenuItem* dstItem = (PartitionMenuItem*)fDestMenu->FindMarked();
780 	if (dstItem) {
781 		label = dstItem->MenuLabel();
782 	} else {
783 		if (fDestMenu->CountItems() == 0)
784 			label = B_TRANSLATE_COMMENT("<none>", "No partition available");
785 		else
786 			label = B_TRANSLATE("Please choose target");
787 	}
788 	fDestMenuField->MenuItem()->SetLabel(label.String());
789 
790 	if (srcItem != NULL && dstItem != NULL) {
791 		BString message;
792 		message.SetToFormat(B_TRANSLATE("Press the Begin button to install "
793 			"from '%1s' onto '%2s'."), srcItem->Name(), dstItem->Name());
794 		_SetStatusMessage(message.String());
795 	} else if (srcItem != NULL) {
796 		_SetStatusMessage(B_TRANSLATE("Choose the disk you want to install "
797 			"onto from the pop-up menu. Then click \"Begin\"."));
798 	} else if (dstItem != NULL) {
799 		_SetStatusMessage(B_TRANSLATE("Choose the source disk from the "
800 			"pop-up menu. Then click \"Begin\"."));
801 	} else {
802 		_SetStatusMessage(B_TRANSLATE("Choose the source and destination disk "
803 			"from the pop-up menus. Then click \"Begin\"."));
804 	}
805 
806 	fInstallStatus = kReadyForInstall;
807 	fBeginButton->SetLabel(B_TRANSLATE("Begin"));
808 	fBeginButton->SetEnabled(srcItem && dstItem);
809 
810 	// adjust "Write Boot Sector" and "Set up boot menu" buttons
811 	if (dstItem != NULL) {
812 		char buffer[256];
813 		snprintf(buffer, sizeof(buffer), B_TRANSLATE("Write boot sector to '%s'"),
814 			dstItem->Name());
815 		label = buffer;
816 	} else
817 		label = B_TRANSLATE("Write boot sector");
818 	fMakeBootableItem->SetEnabled(dstItem != NULL);
819 	fMakeBootableItem->SetLabel(label.String());
820 // TODO: Once bootman support writing to specific disks, enable this, since
821 // we would pass it the disk which contains the target partition.
822 //	fLaunchBootManagerItem->SetEnabled(dstItem != NULL);
823 
824 	if (!fEncouragedToSetupPartitions && !foundOneSuitableTarget) {
825 		// Focus the users attention on the DriveSetup button
826 		fEncouragedToSetupPartitions = true;
827 		PostMessage(ENCOURAGE_DRIVESETUP);
828 	}
829 }
830 
831 
832 void
833 InstallerWindow::_PublishPackages()
834 {
835 	fPackagesView->Clean();
836 	PartitionMenuItem *item = (PartitionMenuItem *)fSrcMenu->FindMarked();
837 	if (item == NULL)
838 		return;
839 
840 	BPath directory;
841 	BDiskDeviceRoster roster;
842 	BDiskDevice device;
843 	BPartition *partition;
844 	if (roster.GetPartitionWithID(item->ID(), &device, &partition) == B_OK) {
845 		if (partition->GetMountPoint(&directory) != B_OK)
846 			return;
847 	} else if (roster.GetDeviceWithID(item->ID(), &device) == B_OK) {
848 		if (device.GetMountPoint(&directory) != B_OK)
849 			return;
850 	} else
851 		return; // shouldn't happen
852 
853 	directory.Append(kPackagesDirectoryPath);
854 	BDirectory dir(directory.Path());
855 	if (dir.InitCheck() != B_OK)
856 		return;
857 
858 	BEntry packageEntry;
859 	BList packages;
860 	while (dir.GetNextEntry(&packageEntry) == B_OK) {
861 		Package* package = Package::PackageFromEntry(packageEntry);
862 		if (package != NULL)
863 			packages.AddItem(package);
864 	}
865 	packages.SortItems(_ComparePackages);
866 
867 	fPackagesView->AddPackages(packages, new BMessage(PACKAGE_CHECKBOX));
868 	PostMessage(PACKAGE_CHECKBOX);
869 }
870 
871 
872 void
873 InstallerWindow::_SetStatusMessage(const char *text)
874 {
875 	fStatusView->SetText(text);
876 	fStatusView->InvalidateLayout();
877 		// In case the status message makes the text view higher than the
878 		// logo, then we need to resize te whole window to fit it.
879 }
880 
881 
882 void
883 InstallerWindow::_SetCopyEngineCancelSemaphore(sem_id id, bool alreadyLocked)
884 {
885 	if (fCopyEngineCancelSemaphore >= 0) {
886 		if (!alreadyLocked)
887 			acquire_sem(fCopyEngineCancelSemaphore);
888 		delete_sem(fCopyEngineCancelSemaphore);
889 	}
890 	fCopyEngineCancelSemaphore = id;
891 }
892 
893 
894 void
895 InstallerWindow::_QuitCopyEngine(bool askUser)
896 {
897 	if (fCopyEngineCancelSemaphore < 0)
898 		return;
899 
900 	// First of all block the copy engine, so that it doesn't continue
901 	// while the alert is showing, which would be irritating.
902 	acquire_sem(fCopyEngineCancelSemaphore);
903 
904 	bool quit = true;
905 	if (askUser) {
906 		BAlert* alert = new BAlert("cancel",
907 			B_TRANSLATE("Are you sure you want to to stop the installation?"),
908 			B_TRANSLATE_COMMENT("Continue", "In alert after pressing Stop"),
909 			B_TRANSLATE_COMMENT("Stop", "In alert after pressing Stop"), 0,
910 			B_WIDTH_AS_USUAL, B_STOP_ALERT);
911 		alert->SetShortcut(1, B_ESCAPE);
912 		quit = alert->Go() != 0;
913 	}
914 
915 	if (quit) {
916 		// Make it quit by having it's lock fail...
917 		_SetCopyEngineCancelSemaphore(-1, true);
918 	} else
919 		release_sem(fCopyEngineCancelSemaphore);
920 }
921 
922 
923 // #pragma mark -
924 
925 
926 int
927 InstallerWindow::_ComparePackages(const void* firstArg, const void* secondArg)
928 {
929 	const Group* group1 = *static_cast<const Group* const *>(firstArg);
930 	const Group* group2 = *static_cast<const Group* const *>(secondArg);
931 	const Package* package1 = dynamic_cast<const Package*>(group1);
932 	const Package* package2 = dynamic_cast<const Package*>(group2);
933 	int sameGroup = strcmp(group1->GroupName(), group2->GroupName());
934 	if (sameGroup != 0)
935 		return sameGroup;
936 	if (package2 == NULL)
937 		return -1;
938 	if (package1 == NULL)
939 		return 1;
940 	return strcmp(package1->Name(), package2->Name());
941 }
942 
943 
944