xref: /haiku/src/apps/packageinstaller/main.cpp (revision 2a7d988dc529d2276d5572f5822cee3e907518d2)
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>
20*2a7d988dSStephan Aßmus #include <Path.h>
2144f919daSRyan Leavengood #include <TextView.h>
22091440e0SStephan Aßmus 
2344f919daSRyan Leavengood #include <stdio.h>
2444f919daSRyan Leavengood 
25bdcd6afcSStephan Aßmus 
26546208a5SOliver Tappe #undef B_TRANSLATION_CONTEXT
27546208a5SOliver Tappe #define B_TRANSLATION_CONTEXT "Packageinstaller main"
2844f919daSRyan Leavengood 
29bdcd6afcSStephan Aßmus 
3044f919daSRyan Leavengood class PackageInstaller : public BApplication {
3144f919daSRyan Leavengood public:
3244f919daSRyan Leavengood 								PackageInstaller();
33bdcd6afcSStephan Aßmus 	virtual						~PackageInstaller();
3444f919daSRyan Leavengood 
35bdcd6afcSStephan Aßmus 	virtual void				RefsReceived(BMessage* message);
36bdcd6afcSStephan Aßmus 	virtual void				ArgvReceived(int32 argc, char** argv);
37bdcd6afcSStephan Aßmus 	virtual void				ReadyToRun();
3844f919daSRyan Leavengood 
39bdcd6afcSStephan Aßmus 	virtual void				MessageReceived(BMessage* message);
4044f919daSRyan Leavengood 
4144f919daSRyan Leavengood private:
42bdcd6afcSStephan Aßmus 			void				_NewWindow(const entry_ref* ref);
43bdcd6afcSStephan Aßmus 
44bdcd6afcSStephan Aßmus private:
45bdcd6afcSStephan Aßmus 			BFilePanel*			fOpenPanel;
4644f919daSRyan Leavengood 			uint32				fWindowCount;
4744f919daSRyan Leavengood };
4844f919daSRyan Leavengood 
4944f919daSRyan Leavengood 
5044f919daSRyan Leavengood PackageInstaller::PackageInstaller()
51bdcd6afcSStephan Aßmus 	:
52bdcd6afcSStephan Aßmus 	BApplication("application/x-vnd.Haiku-PackageInstaller"),
53bdcd6afcSStephan Aßmus 	fOpenPanel(new BFilePanel(B_OPEN_PANEL)),
54be8fa2fbSAdrien Destugues 	fWindowCount(0)
5544f919daSRyan Leavengood {
5644f919daSRyan Leavengood }
5744f919daSRyan Leavengood 
5844f919daSRyan Leavengood 
5944f919daSRyan Leavengood PackageInstaller::~PackageInstaller()
6044f919daSRyan Leavengood {
6144f919daSRyan Leavengood }
6244f919daSRyan Leavengood 
6344f919daSRyan Leavengood 
6444f919daSRyan Leavengood void
6544f919daSRyan Leavengood PackageInstaller::ReadyToRun()
6644f919daSRyan Leavengood {
6744f919daSRyan Leavengood 	// We're ready to run - if no windows are yet visible, this means that
6844f919daSRyan Leavengood 	// we should show a open panel
69bdcd6afcSStephan Aßmus 	if (fWindowCount == 0)
70bdcd6afcSStephan Aßmus 		fOpenPanel->Show();
7144f919daSRyan Leavengood }
7244f919daSRyan Leavengood 
7344f919daSRyan Leavengood 
7444f919daSRyan Leavengood void
75bdcd6afcSStephan Aßmus PackageInstaller::RefsReceived(BMessage* message)
7644f919daSRyan Leavengood {
7744f919daSRyan Leavengood 	entry_ref ref;
78bdcd6afcSStephan Aßmus 	for (int32 i = 0; message->FindRef("refs", i, &ref) == B_OK; i++)
79bdcd6afcSStephan Aßmus 		_NewWindow(&ref);
8044f919daSRyan Leavengood }
8144f919daSRyan Leavengood 
8244f919daSRyan Leavengood 
8344f919daSRyan Leavengood void
8444f919daSRyan Leavengood PackageInstaller::ArgvReceived(int32 argc, char** argv)
8544f919daSRyan Leavengood {
86bdcd6afcSStephan Aßmus 	for (int i = 1; i < argc; i++) {
8744f919daSRyan Leavengood 		BPath path;
8844f919daSRyan Leavengood 		if (path.SetTo(argv[i]) != B_OK) {
89bdcd6afcSStephan Aßmus 			fprintf(stderr, B_TRANSLATE("Error! \"%s\" is not a valid path.\n"),
90eef95624SMatt Madia 				argv[i]);
9144f919daSRyan Leavengood 			continue;
9244f919daSRyan Leavengood 		}
9344f919daSRyan Leavengood 
94bdcd6afcSStephan Aßmus 		entry_ref ref;
95bdcd6afcSStephan Aßmus 		status_t ret = get_ref_for_path(path.Path(), &ref);
9644f919daSRyan Leavengood 		if (ret != B_OK) {
97bdcd6afcSStephan Aßmus 			fprintf(stderr, B_TRANSLATE("Error (%s)! Could not open \"%s\".\n"),
98eef95624SMatt Madia 				strerror(ret), argv[i]);
9944f919daSRyan Leavengood 			continue;
10044f919daSRyan Leavengood 		}
10144f919daSRyan Leavengood 
102bdcd6afcSStephan Aßmus 		_NewWindow(&ref);
10344f919daSRyan Leavengood 	}
10444f919daSRyan Leavengood }
10544f919daSRyan Leavengood 
10644f919daSRyan Leavengood 
10744f919daSRyan Leavengood void
108bdcd6afcSStephan Aßmus PackageInstaller::MessageReceived(BMessage* message)
10944f919daSRyan Leavengood {
110bdcd6afcSStephan Aßmus 	switch (message->what) {
11144f919daSRyan Leavengood 		case P_WINDOW_QUIT:
11244f919daSRyan Leavengood 			fWindowCount--;
113bdcd6afcSStephan Aßmus 			// fall through
114b052daffSRyan Leavengood 		case B_CANCEL:
115bdcd6afcSStephan Aßmus 			if (fWindowCount == 0)
116bdcd6afcSStephan Aßmus 				PostMessage(B_QUIT_REQUESTED);
11744f919daSRyan Leavengood 			break;
118bdcd6afcSStephan Aßmus 
11944f919daSRyan Leavengood 		default:
120bdcd6afcSStephan Aßmus 			BApplication::MessageReceived(message);
12144f919daSRyan Leavengood 	}
12244f919daSRyan Leavengood }
12344f919daSRyan Leavengood 
12444f919daSRyan Leavengood 
125bdcd6afcSStephan Aßmus void
126bdcd6afcSStephan Aßmus PackageInstaller::_NewWindow(const entry_ref* ref)
127bdcd6afcSStephan Aßmus {
128bdcd6afcSStephan Aßmus 	PackageWindow* window = new PackageWindow(ref);
129bdcd6afcSStephan Aßmus 	window->Show();
130bdcd6afcSStephan Aßmus 
131bdcd6afcSStephan Aßmus 	fWindowCount++;
132bdcd6afcSStephan Aßmus }
133bdcd6afcSStephan Aßmus 
134bdcd6afcSStephan Aßmus 
13544f919daSRyan Leavengood int
13644f919daSRyan Leavengood main(void)
13744f919daSRyan Leavengood {
13844f919daSRyan Leavengood 	PackageInstaller app;
13944f919daSRyan Leavengood 	app.Run();
14044f919daSRyan Leavengood 
14144f919daSRyan Leavengood 	return 0;
14244f919daSRyan Leavengood }
14344f919daSRyan Leavengood 
144