xref: /haiku/src/apps/packageinstaller/main.cpp (revision be8fa2fb3088ab1921612b60f352dd9334b40b3d)
144f919daSRyan Leavengood /*
27974d3dcSStephan 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 
12091440e0SStephan Aßmus #include <Alert.h>
1344f919daSRyan Leavengood #include <Application.h>
14091440e0SStephan Aßmus #include <Autolock.h>
15091440e0SStephan Aßmus #include <Catalog.h>
16091440e0SStephan Aßmus #include <Entry.h>
1744f919daSRyan Leavengood #include <FilePanel.h>
1844f919daSRyan Leavengood #include <List.h>
19091440e0SStephan Aßmus #include <Locale.h>
2044f919daSRyan Leavengood #include <TextView.h>
21091440e0SStephan Aßmus 
2244f919daSRyan Leavengood #include <stdio.h>
2344f919daSRyan Leavengood 
244eb0cbb0SMatt Madia #undef B_TRANSLATE_CONTEXT
254eb0cbb0SMatt Madia #define B_TRANSLATE_CONTEXT "Packageinstaller main"
2644f919daSRyan Leavengood 
2744f919daSRyan Leavengood class PackageInstaller : public BApplication {
2844f919daSRyan Leavengood 	public:
2944f919daSRyan Leavengood 		PackageInstaller();
3044f919daSRyan Leavengood 		~PackageInstaller();
3144f919daSRyan Leavengood 
3244f919daSRyan Leavengood 		void RefsReceived(BMessage *msg);
3344f919daSRyan Leavengood 		void ArgvReceived(int32 argc, char **argv);
3444f919daSRyan Leavengood 		void ReadyToRun();
3544f919daSRyan Leavengood 
3644f919daSRyan Leavengood 		void MessageReceived(BMessage *msg);
3744f919daSRyan Leavengood 
3844f919daSRyan Leavengood 		void AboutRequested();
3944f919daSRyan Leavengood 
4044f919daSRyan Leavengood 	private:
4144f919daSRyan Leavengood 		BFilePanel	*fOpen;
4244f919daSRyan Leavengood 		uint32		fWindowCount;
4344f919daSRyan Leavengood };
4444f919daSRyan Leavengood 
4544f919daSRyan Leavengood 
4644f919daSRyan Leavengood PackageInstaller::PackageInstaller()
4744f919daSRyan Leavengood 	:	BApplication("application/x-vnd.Haiku-PackageInstaller"),
48091440e0SStephan Aßmus 	fOpen(NULL),
49*be8fa2fbSAdrien Destugues 	fWindowCount(0)
5044f919daSRyan Leavengood {
5144f919daSRyan Leavengood 	fOpen = new BFilePanel(B_OPEN_PANEL);
5244f919daSRyan Leavengood }
5344f919daSRyan Leavengood 
5444f919daSRyan Leavengood 
5544f919daSRyan Leavengood PackageInstaller::~PackageInstaller()
5644f919daSRyan Leavengood {
5744f919daSRyan Leavengood }
5844f919daSRyan Leavengood 
5944f919daSRyan Leavengood 
6044f919daSRyan Leavengood void
6144f919daSRyan Leavengood PackageInstaller::ReadyToRun()
6244f919daSRyan Leavengood {
6344f919daSRyan Leavengood 	// We're ready to run - if no windows are yet visible, this means that
6444f919daSRyan Leavengood 	// we should show a open panel
6544f919daSRyan Leavengood 	if (fWindowCount == 0) {
6644f919daSRyan Leavengood 		fOpen->Show();
6744f919daSRyan Leavengood 	}
6844f919daSRyan Leavengood }
6944f919daSRyan Leavengood 
7044f919daSRyan Leavengood 
7144f919daSRyan Leavengood void
7244f919daSRyan Leavengood PackageInstaller::RefsReceived(BMessage *msg)
7344f919daSRyan Leavengood {
7444f919daSRyan Leavengood 	uint32 type;
7544f919daSRyan Leavengood 	int32 i, count;
7644f919daSRyan Leavengood 	status_t ret = msg->GetInfo("refs", &type, &count);
7744f919daSRyan Leavengood 	if (ret != B_OK || type != B_REF_TYPE)
7844f919daSRyan Leavengood 		return;
7944f919daSRyan Leavengood 
8044f919daSRyan Leavengood 	entry_ref ref;
8144f919daSRyan Leavengood 	PackageWindow *iter;
8244f919daSRyan Leavengood 	for (i = 0; i < count; i++) {
8344f919daSRyan Leavengood 		if (msg->FindRef("refs", i, &ref) == B_OK) {
8444f919daSRyan Leavengood 			iter = new PackageWindow(&ref);
8544f919daSRyan Leavengood 			fWindowCount++;
8644f919daSRyan Leavengood 			iter->Show();
8744f919daSRyan Leavengood 		}
8844f919daSRyan Leavengood 	}
8944f919daSRyan Leavengood }
9044f919daSRyan Leavengood 
9144f919daSRyan Leavengood 
9244f919daSRyan Leavengood void
9344f919daSRyan Leavengood PackageInstaller::ArgvReceived(int32 argc, char **argv)
9444f919daSRyan Leavengood {
9544f919daSRyan Leavengood 	int i;
9644f919daSRyan Leavengood 	BPath path;
9744f919daSRyan Leavengood 	entry_ref ref;
9844f919daSRyan Leavengood 	status_t ret = B_OK;
9944f919daSRyan Leavengood 	PackageWindow *iter = 0;
10044f919daSRyan Leavengood 
10144f919daSRyan Leavengood 	for (i = 1; i < argc; i++) {
10244f919daSRyan Leavengood 		if (path.SetTo(argv[i]) != B_OK) {
103eef95624SMatt Madia 			fprintf(stderr,
104eef95624SMatt Madia 					B_TRANSLATE("Error! \"%s\" is not a valid path.\n"),
105eef95624SMatt Madia 					argv[i]);
10644f919daSRyan Leavengood 			continue;
10744f919daSRyan Leavengood 		}
10844f919daSRyan Leavengood 
10944f919daSRyan Leavengood 		ret = get_ref_for_path(path.Path(), &ref);
11044f919daSRyan Leavengood 		if (ret != B_OK) {
111eef95624SMatt Madia 			fprintf(stderr,
112eef95624SMatt Madia 					B_TRANSLATE("Error (%s)! Could not open \"%s\".\n"),
113eef95624SMatt Madia 					strerror(ret), argv[i]);
11444f919daSRyan Leavengood 			continue;
11544f919daSRyan Leavengood 		}
11644f919daSRyan Leavengood 
11744f919daSRyan Leavengood 		iter = new PackageWindow(&ref);
11844f919daSRyan Leavengood 		fWindowCount++;
11944f919daSRyan Leavengood 		iter->Show();
12044f919daSRyan Leavengood 	}
12144f919daSRyan Leavengood }
12244f919daSRyan Leavengood 
12344f919daSRyan Leavengood 
12444f919daSRyan Leavengood void
12544f919daSRyan Leavengood PackageInstaller::MessageReceived(BMessage *msg)
12644f919daSRyan Leavengood {
12744f919daSRyan Leavengood 	switch (msg->what) {
12844f919daSRyan Leavengood 		case P_WINDOW_QUIT:
12944f919daSRyan Leavengood 			fWindowCount--;
130b052daffSRyan Leavengood 		case B_CANCEL:
13144f919daSRyan Leavengood 			if (fWindowCount == 0) {
13244f919daSRyan Leavengood 				BAutolock lock(this);
13344f919daSRyan Leavengood 				if (lock.IsLocked())
13444f919daSRyan Leavengood 					Quit();
13544f919daSRyan Leavengood 			}
13644f919daSRyan Leavengood 			break;
13744f919daSRyan Leavengood 		default:
13844f919daSRyan Leavengood 			BApplication::MessageReceived(msg);
13944f919daSRyan Leavengood 	}
14044f919daSRyan Leavengood }
14144f919daSRyan Leavengood 
14244f919daSRyan Leavengood 
14344f919daSRyan Leavengood void
14444f919daSRyan Leavengood PackageInstaller::AboutRequested()
14544f919daSRyan Leavengood {
14644f919daSRyan Leavengood 	BAlert *about = new BAlert("about",
147eef95624SMatt Madia 		B_TRANSLATE("PackageInstaller\n"
14844f919daSRyan Leavengood 		"BeOS legacy .pkg file installer for Haiku.\n\n"
14944f919daSRyan Leavengood 		"Copyright 2007,\nŁukasz 'Sil2100' Zemczak\n\n"
150091440e0SStephan Aßmus 		"Copyright (c) 2007 Haiku, Inc. \n"),
151eef95624SMatt Madia 		B_TRANSLATE("OK"));
15244f919daSRyan Leavengood 
15344f919daSRyan Leavengood 	BTextView *view = about->TextView();
15444f919daSRyan Leavengood 	BFont font;
15544f919daSRyan Leavengood 	view->SetStylable(true);
15644f919daSRyan Leavengood 	view->GetFont(&font);
15744f919daSRyan Leavengood 	font.SetFace(B_BOLD_FACE);
15844f919daSRyan Leavengood 	font.SetSize(font.Size() * 1.5);
15944f919daSRyan Leavengood 	view->SetFontAndColor(0, 17, &font);
16044f919daSRyan Leavengood 
16144f919daSRyan Leavengood 	about->Go();
16244f919daSRyan Leavengood }
16344f919daSRyan Leavengood 
16444f919daSRyan Leavengood 
16544f919daSRyan Leavengood int
16644f919daSRyan Leavengood main(void)
16744f919daSRyan Leavengood {
16844f919daSRyan Leavengood 	PackageInstaller app;
16944f919daSRyan Leavengood 	app.Run();
17044f919daSRyan Leavengood 
17144f919daSRyan Leavengood 	return 0;
17244f919daSRyan Leavengood }
17344f919daSRyan Leavengood 
174