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