1/*
2 * Controller.cpp - Media Player for the Haiku Operating System
3 *
4 * Copyright (C) 2006 Marcus Overhagen <marcus@overhagen.de>
5 * Copyright (C) 2007 Stephan A��mus <superstippi@gmx.de>
6 * Copyright (C) 2008-2009 Fredrik Mod��en 	<[FirstName]@[LastName].se> (MIT ok)
7 *
8 * Released under the terms of the MIT license.
9 */
10
11
12#include "ControllerView.h"
13
14#include <Autolock.h>
15#include <Message.h>
16#include <stdio.h>
17#include <string.h>
18
19#include "Controller.h"
20#include "Playlist.h"
21#include "PlaylistObserver.h"
22
23
24ControllerView::ControllerView(BRect frame, Controller* controller,
25		Playlist* playlist)
26	:
27	TransportControlGroup(frame, true, true, false),
28	fController(controller),
29	fPlaylist(playlist),
30	fPlaylistObserver(new PlaylistObserver(this))
31{
32	fPlaylist->AddListener(fPlaylistObserver);
33}
34
35
36ControllerView::~ControllerView()
37{
38	fPlaylist->RemoveListener(fPlaylistObserver);
39	delete fPlaylistObserver;
40}
41
42
43void
44ControllerView::AttachedToWindow()
45{
46	TransportControlGroup::AttachedToWindow();
47}
48
49
50void
51ControllerView::Draw(BRect updateRect)
52{
53	TransportControlGroup::Draw(updateRect);
54}
55
56
57void
58ControllerView::MessageReceived(BMessage* message)
59{
60	switch (message->what) {
61		case MSG_PLAYLIST_ITEM_ADDED:
62		case MSG_PLAYLIST_ITEM_REMOVED:
63		case MSG_PLAYLIST_ITEMS_SORTED:
64		case MSG_PLAYLIST_CURRENT_ITEM_CHANGED:
65			_CheckSkippable();
66			break;
67
68		case MSG_PLAYLIST_IMPORT_FAILED:
69			break;
70
71		default:
72			TransportControlGroup::MessageReceived(message);
73			break;
74	}
75}
76
77
78// #pragma mark -
79
80
81void
82ControllerView::TogglePlaying()
83{
84	BAutolock _(fPlaylist);
85	if (fPlaylist->CurrentItemIndex() == fPlaylist->CountItems() - 1
86		&& Position() == 1.0) {
87		// Reached end of playlist and end of last item
88		// -> start again from the first item.
89		fPlaylist->SetCurrentItemIndex(0, true);
90	} else
91		fController->TogglePlaying();
92}
93
94
95void
96ControllerView::Stop()
97{
98	fController->Stop();
99}
100
101
102void
103ControllerView::Rewind()
104{
105	// TODO: make it so this function is called repeatedly
106	//printf("ControllerView::Rewind()\n");
107}
108
109
110void
111ControllerView::Forward()
112{
113	// TODO: make it so this function is called repeatedly
114	//printf("ControllerView::Forward()\n");
115}
116
117
118void
119ControllerView::SkipBackward()
120{
121	BAutolock _(fPlaylist);
122	int32 index = fPlaylist->CurrentItemIndex() - 1;
123	if (index < 0)
124		index = 0;
125	fPlaylist->SetCurrentItemIndex(index, true);
126}
127
128
129void
130ControllerView::SkipForward()
131{
132	BAutolock _(fPlaylist);
133	int32 index = fPlaylist->CurrentItemIndex() + 1;
134	if (index >= fPlaylist->CountItems())
135		index = fPlaylist->CountItems() - 1;
136	fPlaylist->SetCurrentItemIndex(index, true);
137}
138
139
140void
141ControllerView::VolumeChanged(float value)
142{
143	fController->SetVolume(value);
144}
145
146
147void
148ControllerView::ToggleMute()
149{
150	fController->ToggleMute();
151}
152
153
154void
155ControllerView::PositionChanged(float value)
156{
157	// 0.0 ... 1.0
158	fController->SetPosition(value);
159}
160
161
162// #pragma mark -
163
164
165void
166ControllerView::_CheckSkippable()
167{
168	BAutolock _(fPlaylist);
169
170	bool canSkipNext, canSkipPrevious;
171	fPlaylist->GetSkipInfo(&canSkipPrevious, &canSkipNext);
172	SetSkippable(canSkipPrevious, canSkipNext);
173}
174