summaryrefslogtreecommitdiffstats
path: root/noatun-plugins/wavecapture/wavecapture.cpp
blob: 2fe6f08314268e046e56ce43f3f37d608231758d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
	Copyright (C) 2001 Matthias Kretz <kretz@kde.org>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
/* $Id$ */
#include "wavecapture.h"

#include <noatun/player.h>
#include <noatun/app.h>
#include <noatun/engine.h>
#include <noatun/noatunarts.h>

#include <arts/soundserver.h>
#include <arts/mcoputils.h>

#include <tdelocale.h>
#include <tdepopupmenu.h>
#include <tdefiledialog.h>
#include <kurl.h>
#include <tderecentdocument.h>
#include <tdeio/job.h>
#include <tdeio/jobclasses.h>

#include <tqfile.h>
#include <tqstring.h>
#include <tqtimer.h>

#include <string>

extern "C"
{
	Plugin *create_plugin()
	{
		TDEGlobal::locale()->insertCatalogue("wavecapture");
		return new WaveCapture();
	}
}

WaveCapture::WaveCapture() : TQObject(0,0), Plugin(),
	_enabled( false ),
	_status( STOPPED ),
	_capture( Arts::DynamicCast( napp->player()->engine()->server()->createObject( "Arts::Effect_WAVECAPTURE" ) ) ),
	_count( 0 ),
	_id( 0 ),
	_filename( "" ),
	_timer( new TQTimer( this ) ),
	m_job( 0 )
{
	NOATUNPLUGINC(WaveCapture);

	if( napp->player()->isPlaying() )
		_status = PLAYING;
	else if( napp->player()->isPaused() )
		_status = PAUSED;

	newSong();

	connect( _timer, TQT_SIGNAL( timeout() ), TQT_SLOT( saveAs() ) );

	connect(napp->player(), TQT_SIGNAL(changed()), TQT_SLOT(newSong()));
	connect(napp->player(), TQT_SIGNAL(stopped()), TQT_SLOT(stopped()));
	connect(napp->player(), TQT_SIGNAL(playing()), TQT_SLOT(playing()));
	connect(napp->player(), TQT_SIGNAL( paused()), TQT_SLOT( paused()));
}

WaveCapture::~WaveCapture()
{
	napp->pluginMenuRemove(pluginMenuItem);
	if( _enabled )
	{
		if( PLAYING == _status )
			stop();
		TQString filename = TQFile::decodeName( (Arts::MCOPUtils::createFilePath( _filename ) + ".wav").c_str() );
		TQFile::remove( filename );
	}
	delete m_job;
}

void WaveCapture::init()
{
	pluginMenuItem = napp->pluginMenuAdd( i18n( "Wave Capture" ), this, TQT_SLOT( toggle() ) );
	napp->pluginMenu()->setCheckable( true );
}

void WaveCapture::toggle()
{
	_enabled = ! _enabled;
	if( PLAYING == _status )
	{
		if( _enabled )
			start();
		else
		{
			stop();
			TQString filename = TQFile::decodeName( (Arts::MCOPUtils::createFilePath( _filename ) + ".wav").c_str() );
			TQFile::remove( filename );
		}
	}
	napp->pluginMenu()->setItemChecked( pluginMenuItem, _enabled );
}

void WaveCapture::newSong()
{
	if( napp->player()->current() )
	{
		TQString title = napp->player()->current().title();
		_capture.filename( std::string( TQFile::encodeName( title ) ) );
	}
	_timer->start( 0, true );
}

void WaveCapture::saveAs()
{
	// this could be a candidate for a race condition - but unlikely and harmless
	std::string filename = _filename;
	_filename = _capture.filename().c_str();
	if( _enabled && ( !filename.empty() ) )
	{
		KFileDialog dlg( ":savedir", "*.wav", 0, "filedialog", true );
		dlg.setCaption( i18n( "Save Last Wave File As" ) );
		dlg.setOperationMode( KFileDialog::Saving );
		dlg.setSelection( TQFile::decodeName( filename.c_str() ) + ".wav" );
			 
		TQString filename2 = TQFile::decodeName( (Arts::MCOPUtils::createFilePath( filename ) + ".wav").c_str() );
		if( dlg.exec() )
		{
			KURL url = dlg.selectedURL();
			if (url.isValid())
				TDERecentDocument::add( url );

			m_job = TDEIO::file_move( KURL( filename2 ), url, -1, true );
			connect( m_job, TQT_SIGNAL( result( TDEIO::Job* ) ), TQT_SLOT( copyFinished( TDEIO::Job* ) ) );
		}
		else
		{
			TQFile::remove( filename2 );
		}
	}
}

void WaveCapture::stopped()
{
	if( _enabled && PLAYING == _status )
		stop();
	_status = STOPPED;
	_capture.filename( std::string( "" ) );
}

void WaveCapture::playing()
{
	if( _enabled && PLAYING != _status )
		start();
	_status = PLAYING;
}

void WaveCapture::paused()
{
	if( _enabled && PLAYING == _status )
		stop();
	_status = PAUSED;
}

void WaveCapture::start()
{
	_capture.start();
	_id = napp->player()->engine()->globalEffectStack()->insertBottom( _capture, "capture too wave" );
}

void WaveCapture::stop()
{
	napp->player()->engine()->globalEffectStack()->remove( _id );
	_capture.stop();
	_timer->start( 0, true );
}

void WaveCapture::copyFinished( TDEIO::Job* job )
{
	if( job == m_job )
		m_job = 0;
}

#include "wavecapture.moc"

// vim:ts=4:sw=4