xref: /haiku/src/apps/processcontroller/PCWorld.cpp (revision 58481f0f6ef1a61ba07283f012cafbc2ed874ead)
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 <Deskbar.h>
32 #include <Roster.h>
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 
38 class PCApplication : public BApplication {
39 	public:
40 						PCApplication();
41 		virtual			~PCApplication();
42 
43 		virtual	void	ReadyToRun();
44 		virtual	void	ArgvReceived(int32 argc, char **argv);
45 };
46 
47 
48 const char* kSignature = "application/x-vnd.Geb-ProcessController";
49 const char* kTrackerSig = "application/x-vnd.Be-TRAK";
50 const char* kDeskbarSig = "application/x-vnd.Be-TSKB";
51 const char* kTerminalSig = "application/x-vnd.Haiku-Terminal";
52 const char* kPreferencesFileName = "ProcessController Prefs";
53 
54 const char*	kPosPrefName = "Position";
55 const char*	kVersionName = "Version";
56 const int kCurrentVersion = 311;
57 
58 thread_id id = 0;
59 
60 
61 PCApplication::PCApplication()
62 	: BApplication(kSignature)
63 {
64 }
65 
66 
67 PCApplication::~PCApplication()
68 {
69 	if (id) {
70 		status_t returnValue;
71 		wait_for_thread(id, &returnValue);
72 	}
73 }
74 
75 
76 void
77 PCApplication::ReadyToRun()
78 {
79 	Preferences preferences(kPreferencesFileName);
80 	int32 version = 0;
81 	preferences.ReadInt32(version, kVersionName);
82 	if (version != kCurrentVersion) {
83 		BAlert* alert = new BAlert("", "Do you want ProcessController to live in the Deskbar?",
84 			"Don't", "Install", NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT);
85 		alert->SetShortcut(0, B_ESCAPE);
86 
87 		if (alert->Go()) {
88 			bool hasOne = false;
89 			{
90 				BDeskbar deskbar;
91 				if (deskbar.HasItem(kDeskbarItemName))
92 					hasOne = true;
93 			}
94 
95 			if (hasOne) {
96 				// Restart deskbar to make sure the new version is picked up
97 				team_id deskbarTeam = be_roster->TeamFor(kDeskbarSig);
98 				if (deskbarTeam >= 0) {
99 					BMessenger messenger(NULL, deskbarTeam);
100 					messenger.SendMessage(B_QUIT_REQUESTED);
101 					int	k = 500;
102 					do {
103 						snooze (10000);
104 					} while (be_roster->IsRunning(kDeskbarSig) && k-- > 0);
105 				}
106 				be_roster->Launch(kDeskbarSig);
107 				int	k = 500;
108 				do {
109 					snooze (10000);
110 				} while (!be_roster->IsRunning(kDeskbarSig) && k-- > 0);
111 			}
112 
113 			BDeskbar deskbar;
114 			if (!deskbar.HasItem(kDeskbarItemName))
115 				move_to_deskbar(deskbar);
116 
117 			Quit();
118 			return;
119 		}
120 	}
121 
122 	new PCWindow();
123 
124 	// quit other eventually running instances
125 	BList list;
126 	be_roster->GetAppList(kSignature, &list);
127 	long count = list.CountItems();
128 	if (count > 1) {
129 		for (long i = 0; i < count - 1; i++) {
130 			BMessenger* otherme = new BMessenger(NULL, (team_id)list.ItemAt(i));
131 			BMessage* message = new BMessage(B_QUIT_REQUESTED);
132 			otherme->SendMessage(message);
133 			delete otherme;
134 		}
135 	}
136 }
137 
138 
139 void
140 PCApplication::ArgvReceived(int32 argc, char **argv)
141 {
142 	if (argc == 2 && strcmp(argv[1], "-desktop-reset") == 0) {
143 		team_id tracker = be_roster->TeamFor(kTrackerSig);
144 		if (tracker >= 0) {
145 			BMessenger messenger(NULL, tracker);
146 			messenger.SendMessage(B_QUIT_REQUESTED);
147 			int	k = 500;
148 			do {
149 				snooze(10000);
150 			} while (be_roster->IsRunning(kTrackerSig) && k-- > 0);
151 		}
152 		remove("/boot/home/config/settings/Tracker/tracker_shelf");
153 		launch(kTrackerSig, "/boot/system/Tracker");
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("Usage: %s [-deskbar]\n", argv[0]);
161 		printf("(c) 1996-2001 Georges-Edouard Berenger, berenger@francenet.fr\n");
162 	}
163 
164 	Quit();
165 }
166 
167 
168 //	#pragma mark -
169 
170 
171 int
172 main()
173 {
174 	PCApplication application;
175 	application.Run();
176 
177 	return B_OK;
178 }
179 
180