xref: /haiku/src/apps/powerstatus/PowerStatus.cpp (revision d79d9a8524bfb0778a898b026c7af203df5b8873)
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 	if (ACPIDriverInterface().Connect() != B_OK) {
81 		if (APMDriverInterface().Connect() != B_OK) {
82 			BAlert* alert = new BAlert("",
83 				B_TRANSLATE("No supported battery detected. PowerStatus "
84 				"cannot be used on your system."), B_TRANSLATE("Too bad!"),
85 				NULL, NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT);
86 			alert->Go();
87 			Quit();
88 			return;
89 		}
90 	}
91 
92 	{
93 		// if the Deskbar is not alive at this point, it might be after having
94 		// acknowledged the requester below
95 		BDeskbar deskbar;
96 #ifdef HAIKU_TARGET_PLATFORM_HAIKU
97 		isDeskbarRunning = deskbar.IsRunning();
98 #endif
99 		isInstalled = deskbar.HasItem(kDeskbarItemName);
100 	}
101 
102 	if (isDeskbarRunning && !isInstalled) {
103 		BAlert* alert = new BAlert("",
104 			B_TRANSLATE("You can run PowerStatus in a window "
105 			"or install it in the Deskbar."), B_TRANSLATE("Run in window"),
106 			B_TRANSLATE("Install in Deskbar"), NULL, B_WIDTH_AS_USUAL,
107 			B_WARNING_ALERT);
108 
109 		if (alert->Go()) {
110 			image_info info;
111 			entry_ref ref;
112 
113 			if (our_image(info) == B_OK
114 				&& get_ref_for_path(info.name, &ref) == B_OK) {
115 				BDeskbar deskbar;
116 				deskbar.AddItem(&ref);
117 			}
118 
119 			Quit();
120 			return;
121 		}
122 	}
123 
124 	BWindow* window = new PowerStatusWindow();
125 	window->Show();
126 }
127 
128 
129 void
130 PowerStatus::AboutRequested()
131 {
132 	BWindow* window = WindowAt(0);
133 	if (window == NULL)
134 		return;
135 
136 	BView* view = window->FindView(kDeskbarItemName);
137 	if (view == NULL)
138 		return;
139 
140 	BMessenger target((BHandler*)view);
141 	BMessage about(B_ABOUT_REQUESTED);
142 	target.SendMessage(&about);
143 }
144 
145 
146 //	#pragma mark -
147 
148 
149 int
150 main(int, char**)
151 {
152 	PowerStatus app;
153 	app.Run();
154 
155 	return 0;
156 }
157 
158