xref: /haiku/src/apps/powerstatus/PowerStatus.cpp (revision c42868a015daa160e093679b2637b1cf9f0b26ba)
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_TRANSLATION_CONTEXT
25 #define B_TRANSLATION_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 
94 		if (alert->Go()) {
95 			image_info info;
96 			entry_ref ref;
97 
98 			if (our_image(info) == B_OK
99 				&& get_ref_for_path(info.name, &ref) == B_OK) {
100 				BDeskbar deskbar;
101 				deskbar.AddItem(&ref);
102 			}
103 
104 			Quit();
105 			return;
106 		}
107 	}
108 
109 	BWindow* window = new PowerStatusWindow();
110 	window->Show();
111 }
112 
113 
114 void
115 PowerStatus::AboutRequested()
116 {
117 	BWindow* window = WindowAt(0);
118 	if (window == NULL)
119 		return;
120 
121 	BView* view = window->FindView(kDeskbarItemName);
122 	if (view == NULL)
123 		return;
124 
125 	BMessenger target((BHandler*)view);
126 	BMessage about(B_ABOUT_REQUESTED);
127 	target.SendMessage(&about);
128 }
129 
130 
131 //	#pragma mark -
132 
133 
134 int
135 main(int, char**)
136 {
137 	PowerStatus app;
138 	app.Run();
139 
140 	return 0;
141 }
142 
143