xref: /haiku/src/apps/mediaplayer/ControllerView.cpp (revision 83b1a68c52ba3e0e8796282759f694b7fdddf06d)
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  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program 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
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20  *
21  */
22 
23 
24 #include "ControllerView.h"
25 
26 #include <Autolock.h>
27 #include <Message.h>
28 #include <stdio.h>
29 #include <string.h>
30 
31 #include "Controller.h"
32 #include "Playlist.h"
33 #include "PlaylistObserver.h"
34 
35 
36 ControllerView::ControllerView(BRect frame, Controller* controller,
37 		Playlist* playlist)
38 	:
39 	TransportControlGroup(frame, true, true, false),
40 	fController(controller),
41 	fPlaylist(playlist),
42 	fPlaylistObserver(new PlaylistObserver(this))
43 {
44 	fPlaylist->AddListener(fPlaylistObserver);
45 }
46 
47 
48 ControllerView::~ControllerView()
49 {
50 	fPlaylist->RemoveListener(fPlaylistObserver);
51 	delete fPlaylistObserver;
52 }
53 
54 
55 void
56 ControllerView::AttachedToWindow()
57 {
58 	TransportControlGroup::AttachedToWindow();
59 }
60 
61 
62 void
63 ControllerView::Draw(BRect updateRect)
64 {
65 	TransportControlGroup::Draw(updateRect);
66 }
67 
68 
69 void
70 ControllerView::MessageReceived(BMessage* message)
71 {
72 	switch (message->what) {
73 		case MSG_PLAYLIST_ITEM_ADDED:
74 		case MSG_PLAYLIST_ITEM_REMOVED:
75 		case MSG_PLAYLIST_ITEMS_SORTED:
76 		case MSG_PLAYLIST_CURRENT_ITEM_CHANGED:
77 			_CheckSkippable();
78 			break;
79 
80 		case MSG_PLAYLIST_IMPORT_FAILED:
81 			break;
82 
83 		default:
84 			TransportControlGroup::MessageReceived(message);
85 	}
86 }
87 
88 
89 // #pragma mark -
90 
91 
92 void
93 ControllerView::TogglePlaying()
94 {
95 	BAutolock _(fPlaylist);
96 	if (fPlaylist->CurrentItemIndex() == fPlaylist->CountItems() - 1
97 		&& Position() == 1.0) {
98 		// Reached end of playlist and end of last item
99 		// -> start again from the first item.
100 		fPlaylist->SetCurrentItemIndex(0, true);
101 	} else
102 		fController->TogglePlaying();
103 }
104 
105 
106 void
107 ControllerView::Stop()
108 {
109 	fController->Stop();
110 }
111 
112 
113 void
114 ControllerView::Rewind()
115 {
116 	// TODO: make it so this function is called repeatedly
117 	//printf("ControllerView::Rewind()\n");
118 }
119 
120 
121 void
122 ControllerView::Forward()
123 {
124 	// TODO: make it so this function is called repeatedly
125 	//printf("ControllerView::Forward()\n");
126 }
127 
128 
129 void
130 ControllerView::SkipBackward()
131 {
132 	BAutolock _(fPlaylist);
133 	int32 index = fPlaylist->CurrentItemIndex() - 1;
134 	if (index < 0)
135 		index = 0;
136 	fPlaylist->SetCurrentItemIndex(index, true);
137 }
138 
139 
140 void
141 ControllerView::SkipForward()
142 {
143 	BAutolock _(fPlaylist);
144 	int32 index = fPlaylist->CurrentItemIndex() + 1;
145 	if (index >= fPlaylist->CountItems())
146 		index = fPlaylist->CountItems() - 1;
147 	fPlaylist->SetCurrentItemIndex(index, true);
148 }
149 
150 
151 void
152 ControllerView::VolumeChanged(float value)
153 {
154 	fController->SetVolume(value);
155 }
156 
157 
158 void
159 ControllerView::ToggleMute()
160 {
161 	fController->ToggleMute();
162 }
163 
164 
165 void
166 ControllerView::PositionChanged(float value)
167 {
168 	// 0.0 ... 1.0
169 	fController->SetPosition(value);
170 }
171 
172 
173 // #pragma mark -
174 
175 
176 void
177 ControllerView::_CheckSkippable()
178 {
179 	BAutolock _(fPlaylist);
180 
181 	bool canSkipNext, canSkipPrevious;
182 	fPlaylist->GetSkipInfo(&canSkipPrevious, &canSkipNext);
183 	SetSkippable(canSkipPrevious, canSkipNext);
184 }
185