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 #include "PowerStatusWindow.h" 12 13 #include <Alert.h> 14 #include <Application.h> 15 #include <Deskbar.h> 16 #include <Entry.h> 17 18 #include <stdio.h> 19 #include <stdlib.h> 20 #include <string.h> 21 22 23 class PowerStatus : public BApplication { 24 public: 25 PowerStatus(); 26 virtual ~PowerStatus(); 27 28 virtual void ReadyToRun(); 29 virtual void AboutRequested(); 30 }; 31 32 33 const char* kSignature = "application/x-vnd.Haiku-PowerStatus"; 34 const char* kDeskbarSignature = "application/x-vnd.Be-TSKB"; 35 const char* kDeskbarItemName = "PowerStatus"; 36 37 38 status_t 39 our_image(image_info& image) 40 { 41 int32 cookie = 0; 42 while (get_next_image_info(B_CURRENT_TEAM, &cookie, &image) == B_OK) { 43 if ((char *)our_image >= (char *)image.text 44 && (char *)our_image <= (char *)image.text + image.text_size) 45 return B_OK; 46 } 47 48 return B_ERROR; 49 } 50 51 52 // #pragma mark - 53 54 55 PowerStatus::PowerStatus() 56 : BApplication(kSignature) 57 { 58 } 59 60 61 PowerStatus::~PowerStatus() 62 { 63 } 64 65 66 void 67 PowerStatus::ReadyToRun() 68 { 69 bool isInstalled = false; 70 bool isDeskbarRunning = true; 71 72 { 73 // if the Deskbar is not alive at this point, it might be after having 74 // acknowledged the requester below 75 BDeskbar deskbar; 76 #ifdef HAIKU_TARGET_PLATFORM_HAIKU 77 isDeskbarRunning = deskbar.IsRunning(); 78 #endif 79 isInstalled = deskbar.HasItem(kDeskbarItemName); 80 } 81 82 if (isDeskbarRunning && !isInstalled) { 83 BAlert* alert = new BAlert("", "Do you want PowerStatus to live in the Deskbar?", 84 "Don't", "Install", NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT); 85 alert->SetShortcut(0, B_ESCAPE); 86 87 if (alert->Go()) { 88 image_info info; 89 entry_ref ref; 90 91 if (our_image(info) == B_OK 92 && get_ref_for_path(info.name, &ref) == B_OK) { 93 BDeskbar deskbar; 94 deskbar.AddItem(&ref); 95 } 96 97 Quit(); 98 return; 99 } 100 } 101 102 BWindow* window = new PowerStatusWindow(); 103 window->Show(); 104 } 105 106 107 void 108 PowerStatus::AboutRequested() 109 { 110 BWindow* window = WindowAt(0); 111 if (window == NULL) 112 return; 113 114 BView* view = window->FindView(kDeskbarItemName); 115 if (view == NULL) 116 return; 117 118 BMessenger target((BHandler*)view); 119 BMessage about(B_ABOUT_REQUESTED); 120 target.SendMessage(&about); 121 } 122 123 124 // #pragma mark - 125 126 127 int 128 main(int, char**) 129 { 130 PowerStatus app; 131 app.Run(); 132 133 return 0; 134 } 135 136