1 /* 2 * Copyright 2006, 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 */ 8 9 10 #include "PowerStatus.h" 11 12 #include <Alert.h> 13 #include <Application.h> 14 #include <Catalog.h> 15 #include <Deskbar.h> 16 #include <Entry.h> 17 18 #include <stdio.h> 19 #include <stdlib.h> 20 #include <string.h> 21 22 #include "ACPIDriverInterface.h" 23 #include "APMDriverInterface.h" 24 #include "PowerStatusWindow.h" 25 26 27 #undef B_TRANSLATION_CONTEXT 28 #define B_TRANSLATION_CONTEXT "PowerStatus" 29 30 31 class PowerStatus : public BApplication { 32 public: 33 PowerStatus(); 34 virtual ~PowerStatus(); 35 36 virtual void ArgvReceived(int32 argc, char** argv); 37 virtual void ReadyToRun(); 38 virtual void AboutRequested(); 39 40 private: 41 bool fAutoInstallInDeskbar; 42 }; 43 44 45 const char* kSignature = "application/x-vnd.Haiku-PowerStatus"; 46 const char* kDeskbarSignature = "application/x-vnd.Be-TSKB"; 47 const char* kDeskbarItemName = "PowerStatus"; 48 49 50 status_t 51 our_image(image_info& image) 52 { 53 int32 cookie = 0; 54 while (get_next_image_info(B_CURRENT_TEAM, &cookie, &image) == B_OK) { 55 if ((char *)our_image >= (char *)image.text 56 && (char *)our_image <= (char *)image.text + image.text_size) 57 return B_OK; 58 } 59 60 return B_ERROR; 61 } 62 63 64 // #pragma mark - 65 66 67 PowerStatus::PowerStatus() 68 : 69 BApplication(kSignature), 70 fAutoInstallInDeskbar(false) 71 { 72 } 73 74 75 PowerStatus::~PowerStatus() 76 { 77 } 78 79 80 void 81 PowerStatus::ArgvReceived(int32 argc, char** argv) 82 { 83 if (argc <= 1) 84 return; 85 86 if (strcmp(argv[1], "--help") == 0 87 || strcmp(argv[1], "-h") == 0) { 88 const char* str = "PowerStatus options:\n" 89 "\t--deskbar\tautomatically add replicant to Deskbar\n" 90 "\t--help\t\tprint this info and exit"; 91 puts(str); 92 Quit(); 93 return; 94 } 95 96 if (strcmp(argv[1], "--deskbar") == 0) 97 fAutoInstallInDeskbar = true; 98 } 99 100 101 void 102 PowerStatus::ReadyToRun() 103 { 104 bool isInstalled = false; 105 bool isDeskbarRunning = true; 106 107 if (ACPIDriverInterface().Connect() != B_OK) { 108 if (APMDriverInterface().Connect() != B_OK) { 109 BAlert* alert = new BAlert("", 110 B_TRANSLATE("No supported battery detected. PowerStatus " 111 "cannot be used on your system."), B_TRANSLATE("Too bad!"), 112 NULL, NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT); 113 if (!fAutoInstallInDeskbar) 114 alert->Go(); 115 Quit(); 116 return; 117 } 118 } 119 120 { 121 // if the Deskbar is not alive at this point, it might be after having 122 // acknowledged the requester below 123 BDeskbar deskbar; 124 isDeskbarRunning = deskbar.IsRunning(); 125 isInstalled = deskbar.HasItem(kDeskbarItemName); 126 } 127 128 if (isDeskbarRunning && !isInstalled) { 129 BAlert* alert = new BAlert("", 130 B_TRANSLATE("You can run PowerStatus in a window " 131 "or install it in the Deskbar."), B_TRANSLATE("Run in window"), 132 B_TRANSLATE("Install in Deskbar"), NULL, B_WIDTH_AS_USUAL, 133 B_WARNING_ALERT); 134 135 if (fAutoInstallInDeskbar || alert->Go()) { 136 image_info info; 137 entry_ref ref; 138 139 if (our_image(info) == B_OK 140 && get_ref_for_path(info.name, &ref) == B_OK) { 141 BDeskbar deskbar; 142 deskbar.AddItem(&ref); 143 } 144 145 Quit(); 146 return; 147 } 148 } 149 150 BWindow* window = new PowerStatusWindow(); 151 window->Show(); 152 } 153 154 155 void 156 PowerStatus::AboutRequested() 157 { 158 BWindow* window = WindowAt(0); 159 if (window == NULL) 160 return; 161 162 BView* view = window->FindView(kDeskbarItemName); 163 if (view == NULL) 164 return; 165 166 BMessenger target((BHandler*)view); 167 BMessage about(B_ABOUT_REQUESTED); 168 target.SendMessage(&about); 169 } 170 171 172 // #pragma mark - 173 174 175 int 176 main(int argc, char* argv[]) 177 { 178 PowerStatus app; 179 app.Run(); 180 181 return 0; 182 } 183