xref: /haiku/src/apps/processcontroller/PCWorld.cpp (revision 3216a856947f9746d8c4c1e720ccf3dc5c0ac786)
1 /*
2  * Copyright 2000, Georges-Edouard Berenger. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "PCWorld.h"
8 #include "PCWindow.h"
9 #include "Preferences.h"
10 #include "ProcessController.h"
11 #include "Utilities.h"
12 
13 #include <Alert.h>
14 #include <Application.h>
15 #include <Catalog.h>
16 #include <Deskbar.h>
17 #include <FindDirectory.h>
18 #include <Path.h>
19 #include <Roster.h>
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 
26 #undef B_TRANSLATION_CONTEXT
27 #define B_TRANSLATION_CONTEXT "ProcessController"
28 
29 
30 class PCApplication : public BApplication {
31 public:
32 								PCApplication();
33 	virtual						~PCApplication();
34 
35 	virtual	void				ReadyToRun();
36 	virtual	void				ArgvReceived(int32 argc, char** argv);
37 };
38 
39 
40 const char* kSignature = "application/x-vnd.Haiku-ProcessController";
41 const char* kTrackerSig = "application/x-vnd.Be-TRAK";
42 const char* kDeskbarSig = "application/x-vnd.Be-TSKB";
43 const char* kTerminalSig = "application/x-vnd.Haiku-Terminal";
44 const char* kPreferencesFileName = "ProcessController Prefs";
45 
46 const char*	kPosPrefName = "Position";
47 const char*	kVersionName = "Version";
48 const int kCurrentVersion = 311;
49 
50 thread_id id = 0;
51 
52 
53 PCApplication::PCApplication()
54 	:
55 	BApplication(kSignature)
56 {
57 }
58 
59 
60 PCApplication::~PCApplication()
61 {
62 	if (id) {
63 		status_t returnValue;
64 		wait_for_thread(id, &returnValue);
65 	}
66 }
67 
68 
69 void
70 PCApplication::ReadyToRun()
71 {
72 	BDeskbar deskbar;
73 	if (!deskbar.HasItem(kDeskbarItemName)) {
74 		// We're not yet installed in the Deskbar, ask if we should
75 		BAlert* alert = new BAlert(B_TRANSLATE("Info"),
76 			B_TRANSLATE("You can run ProcessController in a window"
77 			" or install it in the Deskbar."), B_TRANSLATE("Run in window"),
78 			B_TRANSLATE("Install in Deskbar"),
79 			NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT);
80 
81 		if (alert->Go() != 0) {
82 			BDeskbar deskbar;
83 			if (!deskbar.HasItem(kDeskbarItemName))
84 				move_to_deskbar(deskbar);
85 			Quit();
86 			return;
87 		}
88 	} else {
89 		BAlert* alert = new BAlert(B_TRANSLATE("Info"),
90 			B_TRANSLATE("ProcessController is already installed in Deskbar."),
91 			B_TRANSLATE("OK"), NULL,
92 			NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT);
93 		alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
94 		alert->Go();
95 	}
96 
97 	new PCWindow();
98 
99 	// quit other eventually running instances
100 	BList list;
101 	be_roster->GetAppList(kSignature, &list);
102 	int32 count = list.CountItems();
103 	if (count > 1) {
104 		for (int32 i = 0; i < count - 1; i++) {
105 			BMessenger otherme(NULL, (addr_t)list.ItemAt(i));
106 			otherme.SendMessage(B_QUIT_REQUESTED);
107 		}
108 	}
109 }
110 
111 
112 void
113 PCApplication::ArgvReceived(int32 argc, char **argv)
114 {
115 	if (argc == 2 && strcmp(argv[1], "-desktop-reset") == 0) {
116 		team_id tracker = be_roster->TeamFor(kTrackerSig);
117 		if (tracker >= 0) {
118 			BMessenger messenger(NULL, tracker);
119 			messenger.SendMessage(B_QUIT_REQUESTED);
120 			int	k = 500;
121 			do {
122 				snooze(10000);
123 			} while (be_roster->IsRunning(kTrackerSig) && k-- > 0);
124 		}
125 		BPath shelfPath;
126 		if (find_directory(B_USER_SETTINGS_DIRECTORY, &shelfPath) == B_OK
127 			&& shelfPath.Append("Tracker/tracker_shelf") == B_OK) {
128 			remove(shelfPath.Path());
129 		}
130 		BPath trackerPath;
131 		if (find_directory(B_SYSTEM_DIRECTORY, &trackerPath) == B_OK
132 			&& trackerPath.Append("Tracker") == B_OK) {
133 			launch(kTrackerSig, trackerPath.Path());
134 		}
135 	} else if (argc == 2 && strcmp(argv[1], "-deskbar") == 0) {
136 		BDeskbar deskbar;
137 		if (!gInDeskbar && !deskbar.HasItem(kDeskbarItemName))
138 			move_to_deskbar(deskbar);
139 	} else if (argc > 1) {
140 		// print a simple usage string
141 		printf(B_TRANSLATE("Usage: %s [-deskbar]\n"), argv[0]);
142 		puts(B_TRANSLATE("(c) 1996-2001 Georges-Edouard Berenger, "
143 		"berenger@francenet.fr"));
144 	}
145 
146 	Quit();
147 }
148 
149 
150 //	#pragma mark -
151 
152 
153 int
154 main()
155 {
156 	PCApplication application;
157 	application.Run();
158 
159 	return B_OK;
160 }
161 
162