1/*
2 * Copyright (c) 2007-2009, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT license.
4 *
5 * Author:
6 *		Łukasz 'Sil2100' Zemczak <sil2100@vexillium.org>
7 */
8
9
10#include "PackageWindow.h"
11
12#include <Alert.h>
13#include <Application.h>
14#include <Autolock.h>
15#include <Catalog.h>
16#include <Entry.h>
17#include <FilePanel.h>
18#include <List.h>
19#include <Locale.h>
20#include <TextView.h>
21
22#include <stdio.h>
23
24#undef B_TRANSLATION_CONTEXT
25#define B_TRANSLATION_CONTEXT "Packageinstaller main"
26
27class PackageInstaller : public BApplication {
28	public:
29		PackageInstaller();
30		~PackageInstaller();
31
32		void RefsReceived(BMessage *msg);
33		void ArgvReceived(int32 argc, char **argv);
34		void ReadyToRun();
35
36		void MessageReceived(BMessage *msg);
37
38	private:
39		BFilePanel	*fOpen;
40		uint32		fWindowCount;
41};
42
43
44PackageInstaller::PackageInstaller()
45	:	BApplication("application/x-vnd.Haiku-PackageInstaller"),
46	fOpen(NULL),
47	fWindowCount(0)
48{
49	fOpen = new BFilePanel(B_OPEN_PANEL);
50}
51
52
53PackageInstaller::~PackageInstaller()
54{
55}
56
57
58void
59PackageInstaller::ReadyToRun()
60{
61	// We're ready to run - if no windows are yet visible, this means that
62	// we should show a open panel
63	if (fWindowCount == 0) {
64		fOpen->Show();
65	}
66}
67
68
69void
70PackageInstaller::RefsReceived(BMessage *msg)
71{
72	uint32 type;
73	int32 i, count;
74	status_t ret = msg->GetInfo("refs", &type, &count);
75	if (ret != B_OK || type != B_REF_TYPE)
76		return;
77
78	entry_ref ref;
79	PackageWindow *iter;
80	for (i = 0; i < count; i++) {
81		if (msg->FindRef("refs", i, &ref) == B_OK) {
82			iter = new PackageWindow(&ref);
83			fWindowCount++;
84			iter->Show();
85		}
86	}
87}
88
89
90void
91PackageInstaller::ArgvReceived(int32 argc, char **argv)
92{
93	int i;
94	BPath path;
95	entry_ref ref;
96	status_t ret = B_OK;
97	PackageWindow *iter = 0;
98
99	for (i = 1; i < argc; i++) {
100		if (path.SetTo(argv[i]) != B_OK) {
101			fprintf(stderr,
102					B_TRANSLATE("Error! \"%s\" is not a valid path.\n"),
103					argv[i]);
104			continue;
105		}
106
107		ret = get_ref_for_path(path.Path(), &ref);
108		if (ret != B_OK) {
109			fprintf(stderr,
110					B_TRANSLATE("Error (%s)! Could not open \"%s\".\n"),
111					strerror(ret), argv[i]);
112			continue;
113		}
114
115		iter = new PackageWindow(&ref);
116		fWindowCount++;
117		iter->Show();
118	}
119}
120
121
122void
123PackageInstaller::MessageReceived(BMessage *msg)
124{
125	switch (msg->what) {
126		case P_WINDOW_QUIT:
127			fWindowCount--;
128		case B_CANCEL:
129			if (fWindowCount == 0) {
130				BAutolock lock(this);
131				if (lock.IsLocked())
132					Quit();
133			}
134			break;
135		default:
136			BApplication::MessageReceived(msg);
137	}
138}
139
140
141int
142main(void)
143{
144	PackageInstaller app;
145	app.Run();
146
147	return 0;
148}
149
150