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