summaryrefslogtreecommitdiffstats
path: root/src/kstreamripper.cpp
blob: 0c124b3acc18e4e8335ceb313772c78ece1fa9a7 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/***************************************************************************
 *   Copyright (C) 2003-2005 by Michael Goettsche                          *
 *   mail@tuxipuxi.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.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#include <tdeconfig.h>
#include <kglobal.h>
#include <kled.h>
#include <tdefiledialog.h>
#include <kaboutapplication.h>

#include <tqpushbutton.h>
#include <tqlistview.h>
#include <tqlineedit.h>
#include <tqdir.h>
#include <tqstring.h>
#include <tqstringlist.h>
#include <tqbuttongroup.h>
#include <tqcheckbox.h>
#include <tqprocess.h>
#include <tqtextedit.h>
#include <tqframe.h>
#include <tqtimer.h>
#include <kmessagebox.h>
#include <kstandarddirs.h>
#include <tdeversion.h>

#include "kstreamripper.h"
#include "addnewstreamimpl.h"
#include "processlistviewitem.h"
#include "processcontroller.h"



KStreamRipper::KStreamRipper( TQWidget* parent, const char* name )
    : KStreamRipperBase( parent,name )
#if KDE_IS_VERSION(3,3,90)
	, m_browser("_shoutcast._tcp",0,true)
#endif
{
    m_destEdit->setText( TQDir::homeDirPath() );
	
    m_streamsListView->addColumn( "Name" );
    m_streamsListView->addColumn( "Status" );
    m_streamsListView->addColumn( "Size" );
	
    //app config
    TDEConfig *appConfig = TDEGlobal::config();
	
    m_destEdit->setText( appConfig->readEntry( "Destination" , "" ) );
    m_timeEdit->setText( appConfig->readEntry( "Riptime", "0" ));
    m_tuneInEdit->setText( appConfig->readEntry( "Command", "xmms <url>" ));
    m_id3Checkbox->setChecked( appConfig->readBoolEntry( "Id3Tag", 1 ));
	
    //listview entrys
    TQStringList nameList,urlList,descList;
	
    nameList = appConfig->readListEntry( "names" );
    urlList = appConfig->readListEntry( "urls"  );
    descList = appConfig->readListEntry( "descs" );
    TQStringList::iterator iter1, iter2, iter3;
    iter1 = nameList.begin();
    iter2 = urlList.begin();
    iter3 = descList.begin();
	
    for( ; iter1 != nameList.end() && iter2 != urlList.end() && iter3 != descList.end(); ++iter1, ++iter2, ++iter3 )
	{
            ProcessListViewItem * proc = new ProcessListViewItem( m_streamsListView, *iter1, "", "");
            proc->setRenameEnabled(0, true);
            proc->getProcessController()->setUrl( *iter2 );
            proc->getProcessController()->setDescription( *iter3 );
	}
	
    //CONNECTS
	
    //clicks
    connect(m_addStreamButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(addStreamButtonClicked()));
    connect(m_deleteStreamButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(deleteStreamButtonClicked()));
    connect(m_tuneInButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(tuneInButtonClicked()));
    connect(m_ripButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(ripButtonClicked()));
    connect(m_stopRipButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(stopRipButtonClicked()));
    connect(m_browseButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(browseButtonClicked()));
    connect(m_aboutButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(aboutButtonClicked()));
    connect(m_quitButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(quitButtonClicked()));
	
    //other
    connect( m_streamsListView, TQT_SIGNAL(selectionChanged()), this, TQT_SLOT( selectedNewListItem()) );
    connect( m_DescriptionEdit, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT( descriptionChanged()) );
    connect( m_UrlEdit, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT( urlChanged()) );

    // zeroconf
#if KDE_IS_VERSION(3,3,90)
    connect(&m_browser, TQT_SIGNAL(serviceAdded(DNSSD::RemoteService::Ptr)), this,  
            TQT_SLOT(serviceAdded(DNSSD::RemoteService::Ptr)));
    connect(&m_browser, TQT_SIGNAL(serviceRemoved(DNSSD::RemoteService::Ptr)), this,  
            TQT_SLOT(serviceRemoved(DNSSD::RemoteService::Ptr)));
    m_browser.startBrowse();
#endif
}


KStreamRipper::~KStreamRipper()
{
}


void KStreamRipper::closeEvent( TQCloseEvent *e )
{
    TDEConfig *appConfig = TDEGlobal::config();
	
    appConfig->writeEntry( "Destination", m_destEdit->text());
    appConfig->writeEntry( "Riptime", m_timeEdit->text());
    appConfig->writeEntry( "Command", m_tuneInEdit->text());
    appConfig->writeEntry( "Id3Tag", m_id3Checkbox->isChecked());
	
    //save the listview entrys
	
    TQStringList nameList,urlList,descList;
    TQListViewItemIterator iter( m_streamsListView );
	
    while( iter.current() )
	{
            ProcessListViewItem * ProcItem = (ProcessListViewItem*)iter.current();
            if(ProcItem->getProcessController()->getAutomatic()) continue; 
		
            nameList.append( iter.current()->text( 0 ));
            urlList.append( ProcItem->getProcessController()->getUrl() );
            descList.append( ProcItem->getProcessController()->getDescription() );
            ++iter;
	}
	
    appConfig->writeEntry( "names", nameList );
    appConfig->writeEntry( "urls" , urlList  );
    appConfig->writeEntry( "descs", descList );
	
    appConfig->sync();
	
    e->accept();
}


//SLOTS
void KStreamRipper::addStreamButtonClicked()
{
    AddNewStreamImpl *test = new AddNewStreamImpl( this );
    connect( test, TQT_SIGNAL( finished( AddNewStreamImpl* )), this, TQT_SLOT( addStreamFinished( AddNewStreamImpl* )) );
	
    test->show();
}

void KStreamRipper::deleteStreamButtonClicked()
{
    ProcessListViewItem * ProcItem = (ProcessListViewItem*)m_streamsListView->currentItem();
    stopRipButtonClicked();
    delete ProcItem;
}

void KStreamRipper::tuneInButtonClicked()
{
    if( m_streamsListView->currentItem() )
	{
            TQString command = m_tuneInEdit->text().replace( "<url>",  m_UrlEdit->text() );
		
            TQStringList commands = TQStringList::split( " ", command );
		
            TQProcess *process = new TQProcess( TQT_TQOBJECT(this) );
            process->setArguments( commands );
		
            process->start();
	}
    else ;
}

#if KDE_IS_VERSION(3,3,90)
void KStreamRipper::serviceAdded(DNSSD::RemoteService::Ptr srv)
{
    ProcessListViewItem * proc = new ProcessListViewItem( m_streamsListView, srv->serviceName(), "", "");
    proc->getProcessController()->setUrl( TQString("http://%1:%2%3").arg(srv->hostName()).arg(srv->port()).arg(srv->textData()["path"]) );
    proc->getProcessController()->setDescription( i18n("found by Zeroconf") );
    proc->getProcessController()->setAutomatic(true);
    proc->getProcessController()->setService(srv);

}
void KStreamRipper::serviceRemoved(DNSSD::RemoteService::Ptr srv)
{
    TQListViewItemIterator iter( m_streamsListView );
	
    while( iter.current() ) {
            ProcessListViewItem * ProcItem = (ProcessListViewItem*)iter.current();
	    if (ProcItem->getProcessController()->getAutomatic() && 
                srv==ProcItem->getProcessController()->getService()) { 
				delete ProcItem; 
				return; 
				}
	    ++iter;
    }

}
#endif

void KStreamRipper::ripButtonClicked()
{
    if (KStandardDirs::findExe("streamripper")) {
        ProcessListViewItem * ProcItem = (ProcessListViewItem*)m_streamsListView->currentItem();
        ProcItem->getProcessController()->startRip(m_destEdit->text(), m_timeEdit->text());
        m_ripButton->setEnabled( false );
        m_stopRipButton->setEnabled( true );
    } else {
        KMessageBox::error(this, "The streamripper executable wasn't found. Make sure "
                           "it's in your path.", "streamripper not found");
    }
}

void KStreamRipper::stopRipButtonClicked()
{
    ProcessListViewItem * ProcItem = (ProcessListViewItem*)m_streamsListView->currentItem();
    ProcItem->getProcessController()->stopRip();
    m_ripButton->setEnabled( true );
    m_stopRipButton->setEnabled( false );
}

void KStreamRipper::browseButtonClicked()
{
    TQString openDest = KFileDialog::getExistingDirectory( TQDir::homeDirPath(),
                                                          this,
                                                          "Select Destination..." );
    m_destEdit->setText( openDest );
}

void KStreamRipper::aboutButtonClicked()
{
    KAboutApplication *aboutApp = new KAboutApplication();
    aboutApp->show();
}

void KStreamRipper::quitButtonClicked()
{
    this->close();
}

void KStreamRipper::selectedNewListItem()
{
    if ( !m_streamsListView->selectedItem() ) {
        m_deleteStreamButton->setEnabled( false );
        m_tuneInButton->setEnabled( false );
        m_ripButton->setEnabled( false );
        m_DescriptionEdit->setText( TQString() );
	m_DescriptionEdit->setEnabled(false);
	m_UrlEdit->setEnabled(false);
        m_UrlEdit->setText( TQString() );
	return;
    }
    ProcessController * ProcCtl = ((ProcessListViewItem*)m_streamsListView->currentItem())->getProcessController();

    // reconfigure what the user is allowed to do based on if this process is ripping	
    m_ripButton->setEnabled( !ProcCtl->getStatus() );
    m_stopRipButton->setEnabled( ProcCtl->getStatus() );
    m_tuneInButton->setEnabled( true );
    m_deleteStreamButton->setEnabled( !ProcCtl->getAutomatic() );
	
    m_DescriptionEdit->setText(ProcCtl->getDescription());
    m_DescriptionEdit->setEnabled(true);
    m_UrlEdit->setText(ProcCtl->getUrl());
    m_UrlEdit->setEnabled( !ProcCtl->getAutomatic() );

	
    // maybe these are more elegant than the next two functions, assuming the slots are implemented in ProcessController
    //connect(m_DescriptionEdit, TQT_SIGNAL(textChanged(const TQString&)), (ProcessListViewItem*)m_streamsListView->currentItem()->getProcessController(), TQT_SIGNAL(descriptionChanged(const TQString&))
    //connect(m_UrlEdit, TQT_SIGNAL(textChanged(const TQString&)), (ProcessListViewItem*)m_streamsListView->currentItem()->getProcessController(), TQT_SIGNAL(urlChanged(const TQString&))
}

void KStreamRipper::descriptionChanged()
{
    // maybe this should be deleted and the communication would be through a slot
    ProcessListViewItem * ProcItem = (ProcessListViewItem*)m_streamsListView->currentItem();
    ProcItem->getProcessController()->setDescription(m_DescriptionEdit->text());
}

void KStreamRipper::urlChanged()
{
    // maybe this should be deleted and the communication would be through a slot
    ProcessListViewItem * ProcItem = (ProcessListViewItem*)m_streamsListView->currentItem();
    ProcItem->getProcessController()->setUrl(m_UrlEdit->text());
}


void KStreamRipper::addStreamFinished( AddNewStreamImpl *e )
{
    ProcessListViewItem * proc = new ProcessListViewItem( m_streamsListView, e->d_nameEdit->text(), "", "");
    proc->getProcessController()->setUrl( e->d_urlEdit->text() );
    proc->getProcessController()->setDescription( e->d_descEdit->text() );
}