xref: /haiku/src/apps/processcontroller/PCWorld.cpp (revision 1c09002cbee8e797a0f8bbfc5678dfadd39ee1a7)
1 /*
2 	PCWorld.cpp
3 
4 	ProcessController © 2000, Georges-Edouard Berenger, All Rights Reserved.
5 	Copyright (C) 2004 beunited.org
6 
7 	This library is free software; you can redistribute it and/or
8 	modify it under the terms of the GNU Lesser General Public
9 	License as published by the Free Software Foundation; either
10 	version 2.1 of the License, or (at your option) any later version.
11 
12 	This library is distributed in the hope that it will be useful,
13 	but WITHOUT ANY WARRANTY; without even the implied warranty of
14 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 	Lesser General Public License for more details.
16 
17 	You should have received a copy of the GNU Lesser General Public
18 	License along with this library; if not, write to the Free Software
19 	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 */
21 
22 
23 #include "PCWorld.h"
24 #include "PCWindow.h"
25 #include "Preferences.h"
26 #include "ProcessController.h"
27 #include "Utilities.h"
28 
29 #include <Alert.h>
30 #include <Application.h>
31 #include <Catalog.h>
32 #include <Deskbar.h>
33 #include <Roster.h>
34 
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 
39 
40 #undef B_TRANSLATE_CONTEXT
41 #define B_TRANSLATE_CONTEXT "ProcessController"
42 
43 
44 class PCApplication : public BApplication {
45 public:
46 								PCApplication();
47 	virtual						~PCApplication();
48 
49 	virtual	void				ReadyToRun();
50 	virtual	void				ArgvReceived(int32 argc, char** argv);
51 };
52 
53 
54 const char* kSignature = "application/x-vnd.Haiku-ProcessController";
55 const char* kTrackerSig = "application/x-vnd.Be-TRAK";
56 const char* kDeskbarSig = "application/x-vnd.Be-TSKB";
57 const char* kTerminalSig = "application/x-vnd.Haiku-Terminal";
58 const char* kPreferencesFileName = "ProcessController Prefs";
59 
60 const char*	kPosPrefName = "Position";
61 const char*	kVersionName = "Version";
62 const int kCurrentVersion = 311;
63 
64 thread_id id = 0;
65 
66 
67 PCApplication::PCApplication()
68 	:
69 	BApplication(kSignature)
70 {
71 }
72 
73 
74 PCApplication::~PCApplication()
75 {
76 	if (id) {
77 		status_t returnValue;
78 		wait_for_thread(id, &returnValue);
79 	}
80 }
81 
82 
83 void
84 PCApplication::ReadyToRun()
85 {
86 	BDeskbar deskbar;
87 	if (!deskbar.HasItem(kDeskbarItemName)) {
88 		// We're not yet installed in the Deskbar, ask if we should
89 		BAlert* alert = new BAlert(B_TRANSLATE("Info"),
90 			B_TRANSLATE("You can run ProcessController in a window"
91 			" or install it in the Deskbar."), B_TRANSLATE("Run in window"),
92 			B_TRANSLATE("Install in Deskbar"),
93 			NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT);
94 		alert->SetShortcut(0, B_ESCAPE);
95 
96 		if (alert->Go() != 0) {
97 			BDeskbar deskbar;
98 			if (!deskbar.HasItem(kDeskbarItemName))
99 				move_to_deskbar(deskbar);
100 			Quit();
101 			return;
102 		}
103 	} else {
104 		BAlert* alert = new BAlert(B_TRANSLATE("Info"),
105 			B_TRANSLATE("ProcessController is already installed in Deskbar."),
106 			B_TRANSLATE("OK"), NULL,
107 			NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT);
108 		alert->SetShortcut(0, B_ESCAPE);
109 		alert->Go();
110 	}
111 
112 	new PCWindow();
113 
114 	// quit other eventually running instances
115 	BList list;
116 	be_roster->GetAppList(kSignature, &list);
117 	long count = list.CountItems();
118 	if (count > 1) {
119 		for (long i = 0; i < count - 1; i++) {
120 			BMessenger* otherme = new BMessenger(NULL, (team_id)list.ItemAt(i));
121 			BMessage* message = new BMessage(B_QUIT_REQUESTED);
122 			otherme->SendMessage(message);
123 			delete otherme;
124 		}
125 	}
126 }
127 
128 
129 void
130 PCApplication::ArgvReceived(int32 argc, char **argv)
131 {
132 	if (argc == 2 && strcmp(argv[1], "-desktop-reset") == 0) {
133 		team_id tracker = be_roster->TeamFor(kTrackerSig);
134 		if (tracker >= 0) {
135 			BMessenger messenger(NULL, tracker);
136 			messenger.SendMessage(B_QUIT_REQUESTED);
137 			int	k = 500;
138 			do {
139 				snooze(10000);
140 			} while (be_roster->IsRunning(kTrackerSig) && k-- > 0);
141 		}
142 		remove("/boot/home/config/settings/Tracker/tracker_shelf");
143 		launch(kTrackerSig, "/boot/system/Tracker");
144 	} else if (argc == 2 && strcmp(argv[1], "-deskbar") == 0) {
145 		BDeskbar deskbar;
146 		if (!gInDeskbar && !deskbar.HasItem(kDeskbarItemName))
147 			move_to_deskbar(deskbar);
148 	} else if (argc > 1) {
149 		// print a simple usage string
150 		printf(B_TRANSLATE("Usage: %s [-deskbar]\n"), argv[0]);
151 		printf(B_TRANSLATE("(c) 1996-2001 Georges-Edouard Berenger, "
152 		"berenger@francenet.fr\n"));
153 	}
154 
155 	Quit();
156 }
157 
158 
159 //	#pragma mark -
160 
161 
162 int
163 main()
164 {
165 	PCApplication application;
166 	application.Run();
167 
168 	return B_OK;
169 }
170 
171