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