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