xref: /haiku/src/apps/packageinstaller/main.cpp (revision 7974d3dcf39ce78e5885b481eee12e561ad6e096)
144f919daSRyan Leavengood /*
2*7974d3dcSStephan Aßmus  * Copyright (c) 2007-2009, Haiku, Inc. All rights reserved.
344f919daSRyan Leavengood  * Distributed under the terms of the MIT license.
444f919daSRyan Leavengood  *
544f919daSRyan Leavengood  * Author:
644f919daSRyan Leavengood  *		Łukasz 'Sil2100' Zemczak <sil2100@vexillium.org>
744f919daSRyan Leavengood  */
844f919daSRyan Leavengood 
944f919daSRyan Leavengood 
1044f919daSRyan Leavengood #include "PackageWindow.h"
1144f919daSRyan Leavengood 
1244f919daSRyan Leavengood #include <Application.h>
1344f919daSRyan Leavengood #include <FilePanel.h>
1444f919daSRyan Leavengood #include <List.h>
1544f919daSRyan Leavengood #include <Alert.h>
1644f919daSRyan Leavengood #include <TextView.h>
1744f919daSRyan Leavengood #include <Entry.h>
1844f919daSRyan Leavengood #include <Autolock.h>
1944f919daSRyan Leavengood #include <stdio.h>
2044f919daSRyan Leavengood 
2144f919daSRyan Leavengood 
2244f919daSRyan Leavengood class PackageInstaller : public BApplication {
2344f919daSRyan Leavengood 	public:
2444f919daSRyan Leavengood 		PackageInstaller();
2544f919daSRyan Leavengood 		~PackageInstaller();
2644f919daSRyan Leavengood 
2744f919daSRyan Leavengood 		void RefsReceived(BMessage *msg);
2844f919daSRyan Leavengood 		void ArgvReceived(int32 argc, char **argv);
2944f919daSRyan Leavengood 		void ReadyToRun();
3044f919daSRyan Leavengood 
3144f919daSRyan Leavengood 		void MessageReceived(BMessage *msg);
3244f919daSRyan Leavengood 
3344f919daSRyan Leavengood 		void AboutRequested();
3444f919daSRyan Leavengood 
3544f919daSRyan Leavengood 	private:
3644f919daSRyan Leavengood 		BFilePanel *fOpen;
3744f919daSRyan Leavengood 		uint32 fWindowCount;
3844f919daSRyan Leavengood };
3944f919daSRyan Leavengood 
4044f919daSRyan Leavengood 
4144f919daSRyan Leavengood PackageInstaller::PackageInstaller()
4244f919daSRyan Leavengood 	:	BApplication("application/x-vnd.Haiku-PackageInstaller"),
4344f919daSRyan Leavengood 	fWindowCount(0)
4444f919daSRyan Leavengood {
4544f919daSRyan Leavengood 	fOpen = new BFilePanel(B_OPEN_PANEL);
4644f919daSRyan Leavengood }
4744f919daSRyan Leavengood 
4844f919daSRyan Leavengood 
4944f919daSRyan Leavengood PackageInstaller::~PackageInstaller()
5044f919daSRyan Leavengood {
5144f919daSRyan Leavengood }
5244f919daSRyan Leavengood 
5344f919daSRyan Leavengood 
5444f919daSRyan Leavengood void
5544f919daSRyan Leavengood PackageInstaller::ReadyToRun()
5644f919daSRyan Leavengood {
5744f919daSRyan Leavengood 	// We're ready to run - if no windows are yet visible, this means that
5844f919daSRyan Leavengood 	// we should show a open panel
5944f919daSRyan Leavengood 	if (fWindowCount == 0) {
6044f919daSRyan Leavengood 		fOpen->Show();
6144f919daSRyan Leavengood 	}
6244f919daSRyan Leavengood }
6344f919daSRyan Leavengood 
6444f919daSRyan Leavengood 
6544f919daSRyan Leavengood void
6644f919daSRyan Leavengood PackageInstaller::RefsReceived(BMessage *msg)
6744f919daSRyan Leavengood {
6844f919daSRyan Leavengood 	uint32 type;
6944f919daSRyan Leavengood 	int32 i, count;
7044f919daSRyan Leavengood 	status_t ret = msg->GetInfo("refs", &type, &count);
7144f919daSRyan Leavengood 	if (ret != B_OK || type != B_REF_TYPE)
7244f919daSRyan Leavengood 		return;
7344f919daSRyan Leavengood 
7444f919daSRyan Leavengood 	entry_ref ref;
7544f919daSRyan Leavengood 	PackageWindow *iter;
7644f919daSRyan Leavengood 	for (i = 0; i < count; i++) {
7744f919daSRyan Leavengood 		if (msg->FindRef("refs", i, &ref) == B_OK) {
7844f919daSRyan Leavengood 			iter = new PackageWindow(&ref);
7944f919daSRyan Leavengood 			fWindowCount++;
8044f919daSRyan Leavengood 			iter->Show();
8144f919daSRyan Leavengood 		}
8244f919daSRyan Leavengood 	}
8344f919daSRyan Leavengood }
8444f919daSRyan Leavengood 
8544f919daSRyan Leavengood 
8644f919daSRyan Leavengood void
8744f919daSRyan Leavengood PackageInstaller::ArgvReceived(int32 argc, char **argv)
8844f919daSRyan Leavengood {
8944f919daSRyan Leavengood 	int i;
9044f919daSRyan Leavengood 	BPath path;
9144f919daSRyan Leavengood 	entry_ref ref;
9244f919daSRyan Leavengood 	status_t ret = B_OK;
9344f919daSRyan Leavengood 	PackageWindow *iter = 0;
9444f919daSRyan Leavengood 
9544f919daSRyan Leavengood 	for (i = 1; i < argc; i++) {
9644f919daSRyan Leavengood 		if (path.SetTo(argv[i]) != B_OK) {
9744f919daSRyan Leavengood 			fprintf(stderr, "Error! \"%s\" is not a valid path.\n", argv[i]);
9844f919daSRyan Leavengood 			continue;
9944f919daSRyan Leavengood 		}
10044f919daSRyan Leavengood 
10144f919daSRyan Leavengood 		ret = get_ref_for_path(path.Path(), &ref);
10244f919daSRyan Leavengood 		if (ret != B_OK) {
10344f919daSRyan Leavengood 			fprintf(stderr, "Error (%s)! Could not open \"%s\".\n", strerror(ret),
10444f919daSRyan Leavengood 					argv[i]);
10544f919daSRyan Leavengood 			continue;
10644f919daSRyan Leavengood 		}
10744f919daSRyan Leavengood 
10844f919daSRyan Leavengood 		iter = new PackageWindow(&ref);
10944f919daSRyan Leavengood 		fWindowCount++;
11044f919daSRyan Leavengood 		iter->Show();
11144f919daSRyan Leavengood 	}
11244f919daSRyan Leavengood }
11344f919daSRyan Leavengood 
11444f919daSRyan Leavengood 
11544f919daSRyan Leavengood void
11644f919daSRyan Leavengood PackageInstaller::MessageReceived(BMessage *msg)
11744f919daSRyan Leavengood {
11844f919daSRyan Leavengood 	switch (msg->what) {
11944f919daSRyan Leavengood 		case P_WINDOW_QUIT:
12044f919daSRyan Leavengood 			fWindowCount--;
121b052daffSRyan Leavengood 		case B_CANCEL:
12244f919daSRyan Leavengood 			if (fWindowCount == 0) {
12344f919daSRyan Leavengood 				BAutolock lock(this);
12444f919daSRyan Leavengood 				if (lock.IsLocked())
12544f919daSRyan Leavengood 					Quit();
12644f919daSRyan Leavengood 			}
12744f919daSRyan Leavengood 			break;
12844f919daSRyan Leavengood 		default:
12944f919daSRyan Leavengood 			BApplication::MessageReceived(msg);
13044f919daSRyan Leavengood 	}
13144f919daSRyan Leavengood }
13244f919daSRyan Leavengood 
13344f919daSRyan Leavengood 
13444f919daSRyan Leavengood void
13544f919daSRyan Leavengood PackageInstaller::AboutRequested()
13644f919daSRyan Leavengood {
13744f919daSRyan Leavengood 	BAlert *about = new BAlert("about",
13844f919daSRyan Leavengood 		"PackageInstaller\n"
13944f919daSRyan Leavengood 		"BeOS legacy .pkg file installer for Haiku.\n\n"
14044f919daSRyan Leavengood 		"Copyright 2007,\nŁukasz 'Sil2100' Zemczak\n\n"
14144f919daSRyan Leavengood 		"Copyright (c) 2007 Haiku, Inc. \n",
142*7974d3dcSStephan Aßmus 		"OK");
14344f919daSRyan Leavengood 
14444f919daSRyan Leavengood 	BTextView *view = about->TextView();
14544f919daSRyan Leavengood 	BFont font;
14644f919daSRyan Leavengood 	view->SetStylable(true);
14744f919daSRyan Leavengood 	view->GetFont(&font);
14844f919daSRyan Leavengood 	font.SetFace(B_BOLD_FACE);
14944f919daSRyan Leavengood 	font.SetSize(font.Size() * 1.5);
15044f919daSRyan Leavengood 	view->SetFontAndColor(0, 17, &font);
15144f919daSRyan Leavengood 
15244f919daSRyan Leavengood 	about->Go();
15344f919daSRyan Leavengood }
15444f919daSRyan Leavengood 
15544f919daSRyan Leavengood 
15644f919daSRyan Leavengood int
15744f919daSRyan Leavengood main(void)
15844f919daSRyan Leavengood {
15944f919daSRyan Leavengood 	PackageInstaller app;
16044f919daSRyan Leavengood 	app.Run();
16144f919daSRyan Leavengood 
16244f919daSRyan Leavengood 	return 0;
16344f919daSRyan Leavengood }
16444f919daSRyan Leavengood 
165