xref: /haiku/src/apps/processcontroller/PCWorld.cpp (revision a6e73cb9e8addfe832c064bfcb68067f1c2fa3eb)
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 <FindDirectory.h>
34 #include <Path.h>
35 #include <Roster.h>
36 
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 
41 
42 #undef B_TRANSLATE_CONTEXT
43 #define B_TRANSLATE_CONTEXT "ProcessController"
44 
45 
46 class PCApplication : public BApplication {
47 public:
48 								PCApplication();
49 	virtual						~PCApplication();
50 
51 	virtual	void				ReadyToRun();
52 	virtual	void				ArgvReceived(int32 argc, char** argv);
53 };
54 
55 
56 const char* kSignature = "application/x-vnd.Haiku-ProcessController";
57 const char* kTrackerSig = "application/x-vnd.Be-TRAK";
58 const char* kDeskbarSig = "application/x-vnd.Be-TSKB";
59 const char* kTerminalSig = "application/x-vnd.Haiku-Terminal";
60 const char* kPreferencesFileName = "ProcessController Prefs";
61 
62 const char*	kPosPrefName = "Position";
63 const char*	kVersionName = "Version";
64 const int kCurrentVersion = 311;
65 
66 thread_id id = 0;
67 
68 
69 PCApplication::PCApplication()
70 	:
71 	BApplication(kSignature)
72 {
73 }
74 
75 
76 PCApplication::~PCApplication()
77 {
78 	if (id) {
79 		status_t returnValue;
80 		wait_for_thread(id, &returnValue);
81 	}
82 }
83 
84 
85 void
86 PCApplication::ReadyToRun()
87 {
88 	BDeskbar deskbar;
89 	if (!deskbar.HasItem(kDeskbarItemName)) {
90 		// We're not yet installed in the Deskbar, ask if we should
91 		BAlert* alert = new BAlert(B_TRANSLATE("Info"),
92 			B_TRANSLATE("You can run ProcessController in a window"
93 			" or install it in the Deskbar."), B_TRANSLATE("Run in window"),
94 			B_TRANSLATE("Install in Deskbar"),
95 			NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT);
96 		alert->SetShortcut(0, B_ESCAPE);
97 
98 		if (alert->Go() != 0) {
99 			BDeskbar deskbar;
100 			if (!deskbar.HasItem(kDeskbarItemName))
101 				move_to_deskbar(deskbar);
102 			Quit();
103 			return;
104 		}
105 	} else {
106 		BAlert* alert = new BAlert(B_TRANSLATE("Info"),
107 			B_TRANSLATE("ProcessController is already installed in Deskbar."),
108 			B_TRANSLATE("OK"), NULL,
109 			NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT);
110 		alert->SetShortcut(0, B_ESCAPE);
111 		alert->Go();
112 	}
113 
114 	new PCWindow();
115 
116 	// quit other eventually running instances
117 	BList list;
118 	be_roster->GetAppList(kSignature, &list);
119 	long count = list.CountItems();
120 	if (count > 1) {
121 		for (long i = 0; i < count - 1; i++) {
122 			BMessenger* otherme = new BMessenger(NULL, (team_id)list.ItemAt(i));
123 			BMessage* message = new BMessage(B_QUIT_REQUESTED);
124 			otherme->SendMessage(message);
125 			delete otherme;
126 		}
127 	}
128 }
129 
130 
131 void
132 PCApplication::ArgvReceived(int32 argc, char **argv)
133 {
134 	if (argc == 2 && strcmp(argv[1], "-desktop-reset") == 0) {
135 		team_id tracker = be_roster->TeamFor(kTrackerSig);
136 		if (tracker >= 0) {
137 			BMessenger messenger(NULL, tracker);
138 			messenger.SendMessage(B_QUIT_REQUESTED);
139 			int	k = 500;
140 			do {
141 				snooze(10000);
142 			} while (be_roster->IsRunning(kTrackerSig) && k-- > 0);
143 		}
144 		BPath shelfPath;
145 		if (find_directory(B_USER_SETTINGS_DIRECTORY, &shelfPath) == B_OK
146 			&& shelfPath.Append("Tracker/tracker_shelf") == B_OK) {
147 			remove(shelfPath.Path());
148 		}
149 		BPath trackerPath;
150 		if (find_directory(B_SYSTEM_DIRECTORY, &trackerPath) == B_OK
151 			&& trackerPath.Append("Tracker") == B_OK) {
152 			launch(kTrackerSig, trackerPath.Path());
153 		}
154 	} else if (argc == 2 && strcmp(argv[1], "-deskbar") == 0) {
155 		BDeskbar deskbar;
156 		if (!gInDeskbar && !deskbar.HasItem(kDeskbarItemName))
157 			move_to_deskbar(deskbar);
158 	} else if (argc > 1) {
159 		// print a simple usage string
160 		printf(B_TRANSLATE("Usage: %s [-deskbar]\n"), argv[0]);
161 		printf(B_TRANSLATE("(c) 1996-2001 Georges-Edouard Berenger, "
162 		"berenger@francenet.fr\n"));
163 	}
164 
165 	Quit();
166 }
167 
168 
169 //	#pragma mark -
170 
171 
172 int
173 main()
174 {
175 	PCApplication application;
176 	application.Run();
177 
178 	return B_OK;
179 }
180 
181