xref: /haiku/src/apps/powerstatus/PowerStatus.cpp (revision 3af8011358bd4c624a0979336d48dabb466171ed)
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	ReadyToRun();
37 		virtual void	AboutRequested();
38 };
39 
40 
41 const char* kSignature = "application/x-vnd.Haiku-PowerStatus";
42 const char* kDeskbarSignature = "application/x-vnd.Be-TSKB";
43 const char* kDeskbarItemName = "PowerStatus";
44 
45 
46 status_t
47 our_image(image_info& image)
48 {
49 	int32 cookie = 0;
50 	while (get_next_image_info(B_CURRENT_TEAM, &cookie, &image) == B_OK) {
51 		if ((char *)our_image >= (char *)image.text
52 			&& (char *)our_image <= (char *)image.text + image.text_size)
53 			return B_OK;
54 	}
55 
56 	return B_ERROR;
57 }
58 
59 
60 //	#pragma mark -
61 
62 
63 PowerStatus::PowerStatus()
64 	: BApplication(kSignature)
65 {
66 }
67 
68 
69 PowerStatus::~PowerStatus()
70 {
71 }
72 
73 
74 void
75 PowerStatus::ReadyToRun()
76 {
77 	bool isInstalled = false;
78 	bool isDeskbarRunning = true;
79 
80 	PowerStatusDriverInterface* interface;
81 	interface = new ACPIDriverInterface;
82 	if (interface->Connect() != B_OK) {
83 		delete interface;
84 		interface = new APMDriverInterface;
85 		if (interface->Connect() != B_OK) {
86 			BAlert* alert = new BAlert("",
87 				B_TRANSLATE("No supported battery detected. PowerStatus "
88 				"cannot be used on your system."), B_TRANSLATE("Too bad!"),
89 				NULL, NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT);
90 			alert->Go();
91 			Quit();
92 		}
93 	}
94 	delete interface;
95 
96 	{
97 		// if the Deskbar is not alive at this point, it might be after having
98 		// acknowledged the requester below
99 		BDeskbar deskbar;
100 #ifdef HAIKU_TARGET_PLATFORM_HAIKU
101 		isDeskbarRunning = deskbar.IsRunning();
102 #endif
103 		isInstalled = deskbar.HasItem(kDeskbarItemName);
104 	}
105 
106 	if (isDeskbarRunning && !isInstalled) {
107 		BAlert* alert = new BAlert("",
108 			B_TRANSLATE("You can run PowerStatus in a window "
109 			"or install it in the Deskbar."), B_TRANSLATE("Run in window"),
110 			B_TRANSLATE("Install in Deskbar"), NULL, B_WIDTH_AS_USUAL,
111 			B_WARNING_ALERT);
112 
113 		if (alert->Go()) {
114 			image_info info;
115 			entry_ref ref;
116 
117 			if (our_image(info) == B_OK
118 				&& get_ref_for_path(info.name, &ref) == B_OK) {
119 				BDeskbar deskbar;
120 				deskbar.AddItem(&ref);
121 			}
122 
123 			Quit();
124 			return;
125 		}
126 	}
127 
128 	BWindow* window = new PowerStatusWindow();
129 	window->Show();
130 }
131 
132 
133 void
134 PowerStatus::AboutRequested()
135 {
136 	BWindow* window = WindowAt(0);
137 	if (window == NULL)
138 		return;
139 
140 	BView* view = window->FindView(kDeskbarItemName);
141 	if (view == NULL)
142 		return;
143 
144 	BMessenger target((BHandler*)view);
145 	BMessage about(B_ABOUT_REQUESTED);
146 	target.SendMessage(&about);
147 }
148 
149 
150 //	#pragma mark -
151 
152 
153 int
154 main(int, char**)
155 {
156 	PowerStatus app;
157 	app.Run();
158 
159 	return 0;
160 }
161 
162