1/*
2 * Copyright 2006-2008, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Axel D��rfler, axeld@pinc-software.de
7 *		Stephan A��mus <superstippi@gmx.de>
8 */
9
10
11#include "NetworkStatus.h"
12#include "NetworkStatusWindow.h"
13
14#include <Alert.h>
15#include <Application.h>
16#include <Catalog.h>
17#include <Deskbar.h>
18#include <Entry.h>
19#include <Locale.h>
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
25
26#undef B_TRANSLATION_CONTEXT
27#define B_TRANSLATION_CONTEXT "NetworkStatus"
28
29
30class NetworkStatus : public BApplication {
31	public:
32		NetworkStatus();
33		virtual	~NetworkStatus();
34
35		virtual	void	ArgvReceived(int32 argc, char** argv);
36		virtual	void	ReadyToRun();
37		virtual void	AboutRequested();
38	private:
39				void	_InstallReplicantInDeskbar();
40
41				bool	fAutoInstallInDeskbar;
42				bool	fQuitImmediately;
43};
44
45
46const char* kSignature = "application/x-vnd.Haiku-NetworkStatus";
47const char* kDeskbarSignature = "application/x-vnd.Be-TSKB";
48const char* kDeskbarItemName = "NetworkStatus";
49
50
51status_t
52our_image(image_info& image)
53{
54	int32 cookie = 0;
55	while (get_next_image_info(B_CURRENT_TEAM, &cookie, &image) == B_OK) {
56		if ((char *)our_image >= (char *)image.text
57			&& (char *)our_image <= (char *)image.text + image.text_size)
58			return B_OK;
59	}
60
61	return B_ERROR;
62}
63
64
65//	#pragma mark -
66
67
68NetworkStatus::NetworkStatus()
69	:
70	BApplication(kSignature),
71	fAutoInstallInDeskbar(false),
72	fQuitImmediately(false)
73{
74}
75
76
77NetworkStatus::~NetworkStatus()
78{
79}
80
81
82void
83NetworkStatus::ArgvReceived(int32 argc, char** argv)
84{
85	if (argc <= 1)
86		return;
87
88	if (strcmp(argv[1], "--help") == 0
89		|| strcmp(argv[1], "-h") == 0) {
90		const char* str = B_TRANSLATE("NetworkStatus options:\n"
91			"\t--deskbar\tautomatically add replicant to Deskbar\n"
92			"\t--help\t\tprint this info and exit");
93		puts(str);
94		fQuitImmediately = true;
95		return;
96	}
97
98	if (strcmp(argv[1], "--deskbar") == 0)
99		fAutoInstallInDeskbar = true;
100}
101
102
103void
104NetworkStatus::ReadyToRun()
105{
106	if (fQuitImmediately) {
107		// we printed the help message into the Terminal and
108		// should just quit
109		Quit();
110		return;
111	}
112
113	bool isDeskbarRunning = true;
114	bool isInstalled = false;
115
116	{
117		// if the Deskbar is not alive at this point, it might be after having
118		// acknowledged the requester below
119		BDeskbar deskbar;
120#ifdef HAIKU_TARGET_PLATFORM_HAIKU
121		isDeskbarRunning = deskbar.IsRunning();
122#endif
123		isInstalled = deskbar.HasItem(kDeskbarItemName);
124	}
125
126	if (fAutoInstallInDeskbar) {
127		if (isInstalled) {
128			Quit();
129			return;
130		}
131#ifdef HAIKU_TARGET_PLATFORM_HAIKU
132		// wait up to 10 seconds for Deskbar to become available
133		// in case it is not running (yet?)
134		int32 tries = 10;
135		while (!isDeskbarRunning && --tries) {
136			BDeskbar deskbar;
137			if (deskbar.IsRunning()) {
138				isDeskbarRunning = true;
139				break;
140			}
141			snooze(1000000);
142		}
143#endif
144		if (!isDeskbarRunning) {
145			const char* str = B_TRANSLATE("Deskbar is not running, giving up.");
146			puts(str);
147			Quit();
148			return;
149		}
150
151		_InstallReplicantInDeskbar();
152		return;
153	}
154
155	if (isDeskbarRunning && !isInstalled) {
156		BAlert* alert = new BAlert("", B_TRANSLATE("You can run NetworkStatus "
157			"in a window or install it in the Deskbar."),
158			B_TRANSLATE("Run in window"), B_TRANSLATE("Install in Deskbar"),
159			NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT);
160
161		if (alert->Go() == 1) {
162			_InstallReplicantInDeskbar();
163			return;
164		}
165	}
166
167	BWindow* window = new NetworkStatusWindow();
168	window->Show();
169}
170
171
172void
173NetworkStatus::AboutRequested()
174{
175	BWindow* window = WindowAt(0);
176	if (window == NULL)
177		return;
178
179	BView* view = window->FindView(kDeskbarItemName);
180	if (view == NULL)
181		return;
182
183	BMessenger target((BHandler*)view);
184	BMessage about(B_ABOUT_REQUESTED);
185	target.SendMessage(&about);
186}
187
188
189void
190NetworkStatus::_InstallReplicantInDeskbar()
191{
192	image_info info;
193	entry_ref ref;
194
195	if (our_image(info) == B_OK
196		&& get_ref_for_path(info.name, &ref) == B_OK) {
197		BDeskbar deskbar;
198		deskbar.AddItem(&ref);
199	}
200
201	Quit();
202}
203
204
205//	#pragma mark -
206
207
208int
209main(int, char**)
210{
211	NetworkStatus app;
212	app.Run();
213
214	return 0;
215}
216
217