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