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