summaryrefslogtreecommitdiffstats
path: root/kaudiocreator/kaudiocreator.cpp
blob: 12267ed020af43b03b6075d8ebcab2292a9bfc8f (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
/**
 * This file is part of the KAudioCreator package
 * Copyright (C) 2003 Benjamin C Meyer (ben+kaudiocreator at meyerhome dot net)
 *
 * 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 library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "kaudiocreator.h"

#include <qvbox.h>
#include <kiconloader.h>

#include <kmessagebox.h>
#include <kpopupmenu.h>
#include <kaction.h>
#include <kstatusbar.h>
#include <knotifydialog.h>
#include <kcombobox.h>

#include "tracksimp.h"
#include "jobqueimp.h"
#include "ripper.h"
#include "encoder.h"
#include "prefs.h"
#include "encodefileimp.h"
#include "job.h"

// Settings
#include "ripconfig.h"
#include "cdconfig.h"
#include "encoderconfigimp.h"
#include "general.h"
#include <kcmoduleloader.h>
#include <kurlrequester.h>

/**
 * Constructor. Connect all of the object and the job control.
 */
KAudioCreator::KAudioCreator( QWidget* parent, const char* name) :
       KMainWindow(parent, name)
{
    janusWidget = new KJanusWidget(this, name, KJanusWidget::Tabbed);
    setCentralWidget(janusWidget);

    QVBox *frame = janusWidget->addVBoxPage(i18n("&CD Tracks"), QString::null, SmallIcon("cdaudio_unmount", 32));
    tracks = new TracksImp(frame, "Tracks");

    ripper  = new Ripper ( frame, "Rip" );
    encoder = new Encoder( frame, "Encoder" );

    frame = janusWidget->addVBoxPage( i18n("&Jobs"), QString::null, SmallIcon( "run", 32 ) );
    jobQue = new JobQueImp( frame, "Que" );

    resize(500, 440);

    /*KAction *eject = */new KAction( i18n("&Eject CD"), 0, tracks,
                                  SLOT( eject() ), actionCollection(), "eject" );

    (void)new KAction( i18n("&Configure KAudioCreator..."), 0, this,
                       SLOT( showSettings() ), actionCollection(), "configure_kaudiocreator" );

    KAction *selectAll   = new KAction( i18n( "Select &All Tracks"), 0, tracks,
                                        SLOT( selectAllTracks()   ), actionCollection(), "select_all" ) ;
    KAction *deselectAll = new KAction( i18n( "Deselect &All Tracks"), 0, tracks,
                                        SLOT( deselectAllTracks() ), actionCollection(), "deselect_all" );
    selectAll->setEnabled( false );
    deselectAll->setEnabled( false );

    KActionMenu *actActionMenu = new KActionMenu( i18n("Rip &Selection"), "rip", actionCollection(), "rip" );
    actActionMenu->setDelayed(true); //needed for checking "all accounts"
    actActionMenu->setEnabled( false );   
    connect( actActionMenu, SIGNAL( activated() ), tracks, SLOT( startSession() ) );

    ripMenu = actActionMenu->popupMenu();
    connect( ripMenu, SIGNAL( activated(int) ), this, SLOT( slotRipSelection(int)) );
    connect( ripMenu, SIGNAL( aboutToShow() ),  this, SLOT( getRipMenu()) );

    KAction *rip = new KAction( i18n( "Rip &Selection" ), 0, tracks,
                                SLOT( startSession() ), actionCollection(), "rip_selected" );
    rip->setEnabled( false );

    connect( jobQue, SIGNAL( removeJob(int) ), this,    SLOT( updateStatus() ) );
    connect( jobQue, SIGNAL( removeJob(int) ), ripper,  SLOT( removeJob(int) ) );
    connect( jobQue, SIGNAL( removeJob(int) ), encoder, SLOT( removeJob(int)) );

    connect( ripper, SIGNAL( updateProgress(int, int) )     , jobQue,  SLOT( updateProgress(int,int) ) );
    connect( ripper, SIGNAL( addJob(Job*, const QString &) ), jobQue,  SLOT( addJob(Job*, const QString &)) );
    connect( ripper, SIGNAL( eject(const QString &) )       , tracks,  SLOT( ejectDevice(const QString &)) );
    connect( ripper, SIGNAL( encodeWav(Job *) )             , encoder, SLOT( encodeWav(Job *)) );
    connect( ripper, SIGNAL( jobsChanged() )                , this,    SLOT( updateStatus() ) );   

    connect( encoder, SIGNAL( updateProgress(int, int) )   , jobQue,  SLOT( updateProgress(int,int)) );
    connect( encoder, SIGNAL( addJob(Job*, const QString&)), jobQue,  SLOT( addJob(Job*, const QString &)) );
    connect( encoder, SIGNAL( jobsChanged() )              , this,    SLOT( updateStatus() ) );

    connect( tracks, SIGNAL( hasCD(bool) )    , this,          SLOT( hasCD(bool) ) );
    connect( tracks, SIGNAL( ripTrack(Job *) ), ripper,        SLOT( ripTrack(Job *)) );
    connect( tracks, SIGNAL( hasTracks(bool) ), rip,           SLOT( setEnabled(bool)) );
    connect( tracks, SIGNAL( hasTracks(bool) ), actActionMenu, SLOT( setEnabled(bool)) );
    connect( tracks, SIGNAL( hasTracks(bool) ), deselectAll,   SLOT( setEnabled(bool)) );
    connect( tracks, SIGNAL( hasTracks(bool) ), selectAll,     SLOT( setEnabled(bool)) );

    (void)new KAction(i18n("Remove &Completed Jobs"), 0, jobQue,
          SLOT(clearDoneJobs()), actionCollection(), "clear_done_jobs" );

    KAction *edit = new KAction(i18n("&Edit Album..."), 0, tracks,
          SLOT(editInformation()), actionCollection(), "edit_cd");
    connect(tracks, SIGNAL(hasCD(bool)), edit, SLOT(setEnabled(bool)));
    edit->setEnabled( false );

    (void)new KAction(i18n("Encode &File..."), 0, this,
          SLOT(encodeFile()), actionCollection(), "encode_file");

    KAction *cddb = new KAction(i18n("&CDDB Lookup"), 0, tracks,
          SLOT(performCDDB()), actionCollection(), "cddb_now");
    connect(tracks, SIGNAL(hasCD(bool)), cddb, SLOT(setEnabled(bool)));
    cddb->setEnabled( false );

    KStdAction::configureNotifications(this, SLOT(configureNotifications()),
          actionCollection());
    KStdAction::quit( this, SLOT(close()), actionCollection(), "quit" );

    // Init statusbar
    statusBar()->insertItem(i18n("No Audio CD detected"), 0 );
    hasCD(tracks->hasCD());

    setupGUI();
}

void KAudioCreator::setDevice( const QString &device )
{
    tracks->deviceCombo->setCurrentText( device );
}

void KAudioCreator::slotRipSelection(int selection){
    tracks->startSession( selection );
}

void KAudioCreator::getRipMenu(){
    ripMenu->clear();

    int i=0;
    QString currentGroup = QString("Encoder_%1").arg(i);
    while(EncoderPrefs::hasPrefs(currentGroup)){
        ripMenu->insertItem(EncoderPrefs::prefs(currentGroup)->encoderName(), i);
        currentGroup = QString("Encoder_%1").arg(++i);
    }
}

/**
 * Changes the status bar to let the user no if a cd was not detected or inserted.
 */
void KAudioCreator::hasCD(bool cd){
    if(cd)
        statusBar()->changeItem(i18n("CD Inserted"), 0 );
    else
        statusBar()->changeItem(i18n("No Audio CD detected"), 0 );
}

void KAudioCreator::updateStatus() {
    QString status = i18n("Idle.");
    QString rippingStatus;
    QString encodingStatus;
    int activeRippingJobs = ripper->activeJobCount();
    int pendingRippingJobs = ripper->pendingJobCount();
    int activeEncodingJobs = encoder->activeJobCount();
    int pendingEncodingJobs = encoder->pendingJobCount();

    if ( activeRippingJobs ) {
        rippingStatus = i18n("Ripping (%1 active, %2 queued)").arg( activeRippingJobs ).arg( pendingRippingJobs );
        status = rippingStatus;
    }
    if ( activeEncodingJobs ) {
        encodingStatus = i18n("Encoding (%1 active, %2 queued)").arg( activeEncodingJobs ).arg( pendingEncodingJobs );

        if ( activeRippingJobs ) {
            status.append(" : ");
            status.append( encodingStatus );
        }
        else {
            status = encodingStatus;
        }
    }

    statusBar()->changeItem( status, 0 );
}

/**
 * Ask the user if they really want to quit if there are open jobs.
 */
bool KAudioCreator::queryClose() {
    if(jobQue->numberOfJobsNotFinished() > 0 &&
        (KMessageBox::warningContinueCancel(this, i18n("There are unfinished jobs in the queue. Would you like to quit anyway?"), i18n("Unfinished Jobs in Queue"),KStdGuiItem::quit())
              == KMessageBox::Cancel ))
        return false;
    return true;
}

void KAudioCreator::configureNotifications() {
    KNotifyDialog *dialog = new KNotifyDialog(this, "KNotifyDialog", false);
    dialog->show();
}

void KAudioCreator::encodeFile(){
    EncodeFileImp *file = new EncodeFileImp(this, "EncodeFile");
    connect(file, SIGNAL(startJob(Job*)),encoder, SLOT(encodeWav(Job*)));
    file->show();
}

/**
 * Show Settings dialog.
 */
void KAudioCreator::showSettings(){
    if(KConfigDialog::showDialog("settings"))
        return;

    SettingsDialog *dialog = new SettingsDialog(this, "settings", Prefs::self());
    connect(dialog, SIGNAL(settingsChanged()), ripper, SLOT(loadSettings()));
    connect(dialog, SIGNAL(settingsChanged()), encoder, SLOT(loadSettings()));
    connect(dialog, SIGNAL(settingsChanged()), tracks, SLOT(loadSettings()));
    connect(dialog->encoderConfigImp, SIGNAL(encoderUpdated()), encoder, SLOT(loadSettings()));
    dialog->show();
}

SettingsDialog::SettingsDialog(QWidget *parent, const char *name,KConfigSkeleton *config)
 : KConfigDialog(parent, name, config),
 cddb(0), cddbChanged(false)
{
    addPage(new General(0, "General"), i18n("General"), "package_settings",
          i18n("General Configuration"));
    addPage(new CdConfig(0, "CD"), i18n("CD"), "package_system",
          i18n("CD Configuration"));

    // Because WE don't segfault on our users...
    KService::Ptr libkcddb = KService::serviceByDesktopName("libkcddb");
    if (libkcddb && libkcddb->isValid())
    {
        cddb = KCModuleLoader::loadModule(QString("libkcddb"), KCModuleLoader::Inline);
        if (cddb)
        {
            cddb->load();
            addPage(cddb, i18n("CDDB"), "cdaudio_mount", i18n("CDDB Configuration"), false);
            connect(cddb, SIGNAL(changed(bool)), this, SLOT(slotCddbChanged(bool)));
        }
    }
    RipConfig *rip = new RipConfig(0, "Ripper");
    rip->kcfg_tempDir->setMode(KFile::Directory);
    addPage(rip, i18n("Ripper"), "gear", i18n("Ripper Configuration") );

    encoderConfigImp = new EncoderConfigImp(0, "Encoder");
    addPage(encoderConfigImp, i18n("Encoder"), "filter", i18n("Encoder Configuration") );
}

void SettingsDialog::updateSettings()
{
    if (cddb)
        cddb->save();
}

void SettingsDialog::updateWidgets()
{
    if (cddb)
        cddb->load();
    cddbChanged = false;
}

void SettingsDialog::updateWidgetsDefault()
{
    if (cddb)
        cddb->defaults();
}

bool SettingsDialog::hasChanged()
{
    return cddbChanged;
}

bool SettingsDialog::isDefault()
{
    if (cddb)
        return false;
    return true;
}

void SettingsDialog::slotCddbChanged(bool changed)
{
    cddbChanged = changed;
    updateButtons();
}

#include "kaudiocreator.moc"