2 * Copyright 2006-2008, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Axel Dörfler, axeld@pinc-software.de
7 * Stephan Aßmus <superstippi@gmx.de>
11 #include "NetworkStatus.h"
12 #include "NetworkStatusWindow.h"
15 #include <Application.h>
26 #undef B_TRANSLATION_CONTEXT
27 #define B_TRANSLATION_CONTEXT "NetworkStatus"
30 class NetworkStatus
: public BApplication
{
33 virtual ~NetworkStatus();
35 virtual void ArgvReceived(int32 argc
, char** argv
);
36 virtual void ReadyToRun();
37 virtual void AboutRequested();
39 void _InstallReplicantInDeskbar();
41 bool fAutoInstallInDeskbar
;
42 bool fQuitImmediately
;
46 const char* kSignature
= "application/x-vnd.Haiku-NetworkStatus";
47 const char* kDeskbarSignature
= "application/x-vnd.Be-TSKB";
48 const char* kDeskbarItemName
= "NetworkStatus";
52 our_image(image_info
& image
)
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
)
68 NetworkStatus::NetworkStatus()
70 BApplication(kSignature
),
71 fAutoInstallInDeskbar(false),
72 fQuitImmediately(false)
77 NetworkStatus::~NetworkStatus()
83 NetworkStatus::ArgvReceived(int32 argc
, char** argv
)
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\n");
94 fQuitImmediately
= true;
98 if (strcmp(argv
[1], "--deskbar") == 0)
99 fAutoInstallInDeskbar
= true;
104 NetworkStatus::ReadyToRun()
106 if (fQuitImmediately
) {
107 // we printed the help message into the Terminal and
113 bool isDeskbarRunning
= true;
114 bool isInstalled
= false;
117 // if the Deskbar is not alive at this point, it might be after having
118 // acknowledged the requester below
120 #ifdef HAIKU_TARGET_PLATFORM_HAIKU
121 isDeskbarRunning
= deskbar
.IsRunning();
123 isInstalled
= deskbar
.HasItem(kDeskbarItemName
);
126 if (fAutoInstallInDeskbar
) {
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?)
135 while (!isDeskbarRunning
&& --tries
) {
137 if (deskbar
.IsRunning()) {
138 isDeskbarRunning
= true;
144 if (!isDeskbarRunning
) {
145 printf("Deskbar is not running, giving up.\n");
150 _InstallReplicantInDeskbar();
154 if (isDeskbarRunning
&& !isInstalled
) {
155 BAlert
* alert
= new BAlert("", B_TRANSLATE("You can run NetworkStatus "
156 "in a window or install it in the Deskbar."),
157 B_TRANSLATE("Run in window"), B_TRANSLATE("Install in Deskbar"),
158 NULL
, B_WIDTH_AS_USUAL
, B_WARNING_ALERT
);
160 if (alert
->Go() == 1) {
161 _InstallReplicantInDeskbar();
166 BWindow
* window
= new NetworkStatusWindow();
172 NetworkStatus::AboutRequested()
174 BWindow
* window
= WindowAt(0);
178 BView
* view
= window
->FindView(kDeskbarItemName
);
182 BMessenger
target((BHandler
*)view
);
183 BMessage
about(B_ABOUT_REQUESTED
);
184 target
.SendMessage(&about
);
189 NetworkStatus::_InstallReplicantInDeskbar()
194 if (our_image(info
) == B_OK
195 && get_ref_for_path(info
.name
, &ref
) == B_OK
) {
197 deskbar
.AddItem(&ref
);