xref: /haiku/src/add-ons/tracker/iconvader/IconVader.cpp (revision adb0d19d561947362090081e81d90dde59142026)
1 /*
2  * Copyright 2006, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <Alert.h>
8 #include <Messenger.h>
9 #include <Roster.h>
10 #include <View.h>
11 
12 #include "PoseView.h"
13 
14 static void Error(BView *view, status_t status, bool unlock=false)
15 {
16 	BAlert *alert;
17 	if (view && unlock)
18 		view->UnlockLooper();
19 	BString s(strerror(status));
20 	alert = new BAlert("Error", s.String(), "Ok");
21 	alert->Go();
22 }
23 
24 /*!
25 	\brief Tracker add-on entry
26 */
27 extern "C" void
28 process_refs(entry_ref dir, BMessage* refs, void* /*reserved*/)
29 {
30 	status_t status;
31 	BAlert *alert;
32 	BMessenger msgr;
33 	BPoseView *view = NULL;
34 	BMessage poseViewBackup;
35 	BMessage poseViewColumnBackup;
36 	uint32 poseViewModeBackup;
37 	BString windowTitleBackup;
38 
39 	refs->PrintToStream();
40 
41 	status = refs->FindMessenger("TrackerViewToken", &msgr);
42 	if (status < B_OK) {
43 		Error(view, status);
44 		return;
45 	}
46 
47 	status = B_ERROR;
48 	if (!msgr.LockTarget()) {
49 		Error(view, status);
50 		return;
51 	}
52 
53 	status = B_BAD_HANDLER;
54 	view = dynamic_cast<BPoseView *>(msgr.Target(NULL));
55 	if (!view) {
56 		Error(view, status);
57 		return;
58 	}
59 	if (dynamic_cast<BWindow *>(view->Looper()) == NULL) {
60 		Error(view, status, true);
61 		return;
62 	}
63 
64 	windowTitleBackup = view->Window()->Title();
65 
66 	view->SaveColumnState(poseViewColumnBackup);
67 	view->SaveState(poseViewBackup);
68 	view->SetDragEnabled(false);
69 	view->SetSelectionRectEnabled(false);
70 	view->SetPoseEditing(false);
71 	poseViewModeBackup = view->ViewMode();
72 
73 
74 	view->SetViewMode(kIconMode);
75 
76 	view->ShowBarberPole();
77 
78 
79 	view->UnlockLooper();
80 
81 
82 
83 	alert = new BAlert("Error", "IconVader:\nClick on the icons to get points. Avoid symlinks!", "Ok");
84 	alert->Go();
85 
86 
87 	int32 score = 0;
88 	int32 count = 300;
89 	while (count--) {
90 		status = B_ERROR;
91 		if (!msgr.LockTarget()) {
92 			Error(view, status);
93 			return;
94 		}
95 
96 		BPose *pose;
97 		for (int32 i = 0; (pose = view->PoseAtIndex(i)); i++) {
98 			if (pose->IsSelected()) {
99 				if (pose->TargetModel()->IsFile())
100 					score++;
101 				if (pose->TargetModel()->IsDirectory())
102 					score+=2;
103 				if (pose->TargetModel()->IsSymLink())
104 					score-=10;
105 				pose->Select(false);
106 			}
107 			BPoint location = pose->Location();
108 			location.x += ((rand() % 20) - 10);
109 			location.y += ((rand() % 20) - 10);
110 			pose->SetLocation(location);
111 		}
112 
113 		view->CheckPoseVisibility();
114 
115 		view->Invalidate();
116 
117 		BString str("Score: ");
118 		str << score;
119 		view->Window()->SetTitle(str.String());
120 
121 		view->UnlockLooper();
122 		snooze(100000);
123 	}
124 
125 	BString scoreStr("You scored ");
126 	scoreStr << score << " points!";
127 	alert = new BAlert("Error", scoreStr.String(), "Cool!");
128 	alert->Go();
129 
130 
131 	status = B_ERROR;
132 	if (!msgr.LockTarget()) {
133 		Error(view, status);
134 		return;
135 	}
136 
137 	/*
138 	status = B_BAD_HANDLER;
139 	view = dynamic_cast<BPoseView *>(msgr.Target(NULL));
140 	if (!view)
141 		goto err1;
142 	*/
143 
144 	view->HideBarberPole();
145 	view->SetViewMode(poseViewModeBackup);
146 	view->RestoreState(poseViewBackup);
147 	view->RestoreColumnState(poseViewColumnBackup);
148 
149 	view->Window()->SetTitle(windowTitleBackup.String());
150 
151 /*
152 	BMessage('_RRC') {
153         TrackerViewToken = BMessenger(port=32004, team=591, target=direct:0x131)
154 } */
155 
156 
157 	//be_roster->Launch("application/x-vnd.haiku-filetypes", refs);
158 
159 	view->UnlockLooper();
160 	return;
161 
162 }
163