xref: /haiku/src/apps/processcontroller/PCWorld.cpp (revision 7f83301d7caa07b77d530d3ee35d5d77b712df95)
1ed7a3501SAxel Dörfler /*
2f6581478SAugustin Cavalier  * Copyright 2000, Georges-Edouard Berenger. All rights reserved.
3f6581478SAugustin Cavalier  * Distributed under the terms of the MIT License.
4ed7a3501SAxel Dörfler  */
5ed7a3501SAxel Dörfler 
6e2940ab6SAxel Dörfler 
7ed7a3501SAxel Dörfler #include "PCWorld.h"
8ed7a3501SAxel Dörfler #include "PCWindow.h"
9a12827f2SAxel Dörfler #include "Preferences.h"
10a12827f2SAxel Dörfler #include "ProcessController.h"
11a12827f2SAxel Dörfler #include "Utilities.h"
12e2940ab6SAxel Dörfler 
13a12827f2SAxel Dörfler #include <Alert.h>
14a12827f2SAxel Dörfler #include <Application.h>
1595fcf739SAdrien Destugues #include <Catalog.h>
16ed7a3501SAxel Dörfler #include <Deskbar.h>
17323b6546SOliver Tappe #include <FindDirectory.h>
18323b6546SOliver Tappe #include <Path.h>
19a12827f2SAxel Dörfler #include <Roster.h>
20ed7a3501SAxel Dörfler 
21e2940ab6SAxel Dörfler #include <stdio.h>
22e2940ab6SAxel Dörfler #include <stdlib.h>
2389f088e0SIthamar R. Adema #include <string.h>
24a12827f2SAxel Dörfler 
25dd9740daSAxel Dörfler 
26546208a5SOliver Tappe #undef B_TRANSLATION_CONTEXT
27546208a5SOliver Tappe #define B_TRANSLATION_CONTEXT "ProcessController"
2895fcf739SAdrien Destugues 
2995fcf739SAdrien Destugues 
30a12827f2SAxel Dörfler class PCApplication : public BApplication {
31a12827f2SAxel Dörfler public:
32a12827f2SAxel Dörfler 								PCApplication();
33a12827f2SAxel Dörfler 	virtual						~PCApplication();
34a12827f2SAxel Dörfler 
35a12827f2SAxel Dörfler 	virtual	void				ReadyToRun();
36a12827f2SAxel Dörfler 	virtual	void				ArgvReceived(int32 argc, char** argv);
37a12827f2SAxel Dörfler };
38a12827f2SAxel Dörfler 
39a12827f2SAxel Dörfler 
40f3f9e93aSRyan Leavengood const char* kSignature = "application/x-vnd.Haiku-ProcessController";
41ed7a3501SAxel Dörfler const char* kTrackerSig = "application/x-vnd.Be-TRAK";
42ed7a3501SAxel Dörfler const char* kDeskbarSig = "application/x-vnd.Be-TSKB";
43e2940ab6SAxel Dörfler const char* kTerminalSig = "application/x-vnd.Haiku-Terminal";
442dfee1f7SSiarzhuk Zharski const char* kPreferencesFileName = "ProcessController Prefs";
45ed7a3501SAxel Dörfler 
462dfee1f7SSiarzhuk Zharski const char*	kPosPrefName = "Position";
472dfee1f7SSiarzhuk Zharski const char*	kVersionName = "Version";
48a12827f2SAxel Dörfler const int kCurrentVersion = 311;
49e2940ab6SAxel Dörfler 
50ed7a3501SAxel Dörfler thread_id id = 0;
51ed7a3501SAxel Dörfler 
52ed7a3501SAxel Dörfler 
PCApplication()53e2940ab6SAxel Dörfler PCApplication::PCApplication()
54dd9740daSAxel Dörfler 	:
55dd9740daSAxel Dörfler 	BApplication(kSignature)
56ed7a3501SAxel Dörfler {
57ed7a3501SAxel Dörfler }
58ed7a3501SAxel Dörfler 
59e2940ab6SAxel Dörfler 
~PCApplication()60ed7a3501SAxel Dörfler PCApplication::~PCApplication()
61ed7a3501SAxel Dörfler {
62ed7a3501SAxel Dörfler 	if (id) {
63e2940ab6SAxel Dörfler 		status_t returnValue;
64e2940ab6SAxel Dörfler 		wait_for_thread(id, &returnValue);
65ed7a3501SAxel Dörfler 	}
66ed7a3501SAxel Dörfler }
67ed7a3501SAxel Dörfler 
68e2940ab6SAxel Dörfler 
69e2940ab6SAxel Dörfler void
ReadyToRun()70e2940ab6SAxel Dörfler PCApplication::ReadyToRun()
71ed7a3501SAxel Dörfler {
72a12827f2SAxel Dörfler 	BDeskbar deskbar;
73dd9740daSAxel Dörfler 	if (!deskbar.HasItem(kDeskbarItemName)) {
74dd9740daSAxel Dörfler 		// We're not yet installed in the Deskbar, ask if we should
7580cdaa81SSiarzhuk Zharski 		BAlert* alert = new BAlert(B_TRANSLATE("Info"),
7682116426SJoachim Seemer 			B_TRANSLATE("You can run ProcessController in a window"
7795fcf739SAdrien Destugues 			" or install it in the Deskbar."), B_TRANSLATE("Run in window"),
7895fcf739SAdrien Destugues 			B_TRANSLATE("Install in Deskbar"),
7995fcf739SAdrien Destugues 			NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT);
80dd9740daSAxel Dörfler 
81dd9740daSAxel Dörfler 		if (alert->Go() != 0) {
82dd9740daSAxel Dörfler 			BDeskbar deskbar;
83dd9740daSAxel Dörfler 			if (!deskbar.HasItem(kDeskbarItemName))
84dd9740daSAxel Dörfler 				move_to_deskbar(deskbar);
85dd9740daSAxel Dörfler 			Quit();
86dd9740daSAxel Dörfler 			return;
87dd9740daSAxel Dörfler 		}
88fcab391bSFredrik Modeen 	} else {
8980cdaa81SSiarzhuk Zharski 		BAlert* alert = new BAlert(B_TRANSLATE("Info"),
9095fcf739SAdrien Destugues 			B_TRANSLATE("ProcessController is already installed in Deskbar."),
9195fcf739SAdrien Destugues 			B_TRANSLATE("OK"), NULL,
92fcab391bSFredrik Modeen 			NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT);
93aed35104SHumdinger 		alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
9405afd333SFredrik Modeen 		alert->Go();
95a12827f2SAxel Dörfler 	}
96a12827f2SAxel Dörfler 
97ed7a3501SAxel Dörfler 	new PCWindow();
98e2940ab6SAxel Dörfler 
99e2940ab6SAxel Dörfler 	// quit other eventually running instances
100ed7a3501SAxel Dörfler 	BList list;
101ed7a3501SAxel Dörfler 	be_roster->GetAppList(kSignature, &list);
102c3fa08a6SRene Gollent 	int32 count = list.CountItems();
103e2940ab6SAxel Dörfler 	if (count > 1) {
104c3fa08a6SRene Gollent 		for (int32 i = 0; i < count - 1; i++) {
105b7fd72cdSAugustin Cavalier 			BMessenger otherme(NULL, (addr_t)list.ItemAt(i));
106b7fd72cdSAugustin Cavalier 			otherme.SendMessage(B_QUIT_REQUESTED);
107ed7a3501SAxel Dörfler 		}
108ed7a3501SAxel Dörfler 	}
109ed7a3501SAxel Dörfler }
110ed7a3501SAxel Dörfler 
111e2940ab6SAxel Dörfler 
112e2940ab6SAxel Dörfler void
ArgvReceived(int32 argc,char ** argv)113e2940ab6SAxel Dörfler PCApplication::ArgvReceived(int32 argc, char **argv)
114ed7a3501SAxel Dörfler {
115e2940ab6SAxel Dörfler 	if (argc == 2 && strcmp(argv[1], "-desktop-reset") == 0) {
116ed7a3501SAxel Dörfler 		team_id tracker = be_roster->TeamFor(kTrackerSig);
117e2940ab6SAxel Dörfler 		if (tracker >= 0) {
118ed7a3501SAxel Dörfler 			BMessenger messenger(NULL, tracker);
119ed7a3501SAxel Dörfler 			messenger.SendMessage(B_QUIT_REQUESTED);
120ed7a3501SAxel Dörfler 			int	k = 500;
121ed7a3501SAxel Dörfler 			do {
122ed7a3501SAxel Dörfler 				snooze(10000);
123ed7a3501SAxel Dörfler 			} while (be_roster->IsRunning(kTrackerSig) && k-- > 0);
124ed7a3501SAxel Dörfler 		}
125323b6546SOliver Tappe 		BPath shelfPath;
126323b6546SOliver Tappe 		if (find_directory(B_USER_SETTINGS_DIRECTORY, &shelfPath) == B_OK
127323b6546SOliver Tappe 			&& shelfPath.Append("Tracker/tracker_shelf") == B_OK) {
128323b6546SOliver Tappe 			remove(shelfPath.Path());
129323b6546SOliver Tappe 		}
130323b6546SOliver Tappe 		BPath trackerPath;
131323b6546SOliver Tappe 		if (find_directory(B_SYSTEM_DIRECTORY, &trackerPath) == B_OK
132323b6546SOliver Tappe 			&& trackerPath.Append("Tracker") == B_OK) {
133323b6546SOliver Tappe 			launch(kTrackerSig, trackerPath.Path());
134323b6546SOliver Tappe 		}
135e2940ab6SAxel Dörfler 	} else if (argc == 2 && strcmp(argv[1], "-deskbar") == 0) {
136e2940ab6SAxel Dörfler 		BDeskbar deskbar;
137e2940ab6SAxel Dörfler 		if (!gInDeskbar && !deskbar.HasItem(kDeskbarItemName))
138e2940ab6SAxel Dörfler 			move_to_deskbar(deskbar);
139e2940ab6SAxel Dörfler 	} else if (argc > 1) {
140ed7a3501SAxel Dörfler 		// print a simple usage string
14195fcf739SAdrien Destugues 		printf(B_TRANSLATE("Usage: %s [-deskbar]\n"), argv[0]);
142*7f83301dSMurai Takashi 		puts(B_TRANSLATE("(c) 1996-2001 Georges-Edouard Berenger, "
143*7f83301dSMurai Takashi 		"berenger@francenet.fr"));
144ed7a3501SAxel Dörfler 	}
145e2940ab6SAxel Dörfler 
146ed7a3501SAxel Dörfler 	Quit();
147ed7a3501SAxel Dörfler }
148e2940ab6SAxel Dörfler 
149e2940ab6SAxel Dörfler 
150e2940ab6SAxel Dörfler //	#pragma mark -
151e2940ab6SAxel Dörfler 
152e2940ab6SAxel Dörfler 
153e2940ab6SAxel Dörfler int
main()154e2940ab6SAxel Dörfler main()
155e2940ab6SAxel Dörfler {
156e2940ab6SAxel Dörfler 	PCApplication application;
157e2940ab6SAxel Dörfler 	application.Run();
158e2940ab6SAxel Dörfler 
159e2940ab6SAxel Dörfler 	return B_OK;
160e2940ab6SAxel Dörfler }
161e2940ab6SAxel Dörfler 
162