summaryrefslogtreecommitdiffstats
path: root/kaudiocreator/kaudiocreator.cpp
blob: c5db63373f488c87ca86a2e98af4398f885d1618 (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., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "kaudiocreator.h"

#include <tqvbox.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( TQWidget* parent, const char* name) :
       KMainWindow(parent, name)
{
    janusWidget = new KJanusWidget(this, name, KJanusWidget::Tabbed);
    setCentralWidget(janusWidget);

    TQVBox *frame = janusWidget->addVBoxPage(i18n("&CD Tracks"), TQString(), SmallIcon("cdaudio_unmount", 32));
    tracks = new TracksImp(frame, "Tracks");

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

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

    resize(500, 440);

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

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

    KAction *selectAll   = new KAction( i18n( "Select &All Tracks"), 0, TQT_TQOBJECT(tracks),
                                        TQT_SLOT( selectAllTracks()   ), actionCollection(), "select_all" ) ;
    KAction *deselectAll = new KAction( i18n( "Deselect &All Tracks"), 0, TQT_TQOBJECT(tracks),
                                        TQT_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, TQT_SIGNAL( activated() ), TQT_TQOBJECT(tracks), TQT_SLOT( startSession() ) );

    ripMenu = actActionMenu->popupMenu();
    connect( ripMenu, TQT_SIGNAL( activated(int) ), TQT_TQOBJECT(this), TQT_SLOT( slotRipSelection(int)) );
    connect( ripMenu, TQT_SIGNAL( aboutToShow() ), TQT_TQOBJECT(this), TQT_SLOT( getRipMenu()) );

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

    connect( jobQue, TQT_SIGNAL( removeJob(int) ), TQT_TQOBJECT(this),    TQT_SLOT( updateStatus() ) );
    connect( jobQue, TQT_SIGNAL( removeJob(int) ), TQT_TQOBJECT(ripper), TQT_SLOT( removeJob(int) ) );
    connect( jobQue, TQT_SIGNAL( removeJob(int) ), TQT_TQOBJECT(encoder), TQT_SLOT( removeJob(int)) );

    connect( ripper, TQT_SIGNAL( updateProgress(int, int) ), TQT_TQOBJECT(jobQue), TQT_SLOT( updateProgress(int,int) ) );
    connect( ripper, TQT_SIGNAL( addJob(Job*, const TQString &) ), TQT_TQOBJECT(jobQue),  TQT_SLOT( addJob(Job*, const TQString &)) );
    connect( ripper, TQT_SIGNAL( eject(const TQString &) )       , TQT_TQOBJECT(tracks),  TQT_SLOT( ejectDevice(const TQString &)) );
    connect( ripper, TQT_SIGNAL( encodeWav(Job *) )             , TQT_TQOBJECT(encoder), TQT_SLOT( encodeWav(Job *)) );
    connect( ripper, TQT_SIGNAL( jobsChanged() )                , TQT_TQOBJECT(this),    TQT_SLOT( updateStatus() ) );   

    connect( encoder, TQT_SIGNAL( updateProgress(int, int) )   , TQT_TQOBJECT(jobQue),  TQT_SLOT( updateProgress(int,int)) );
    connect( encoder, TQT_SIGNAL( addJob(Job*, const TQString&)), TQT_TQOBJECT(jobQue),  TQT_SLOT( addJob(Job*, const TQString &)) );
    connect( encoder, TQT_SIGNAL( jobsChanged() )              , TQT_TQOBJECT(this),    TQT_SLOT( updateStatus() ) );

    connect( tracks, TQT_SIGNAL( hasCD(bool) )    , TQT_TQOBJECT(this),          TQT_SLOT( hasCD(bool) ) );
    connect( tracks, TQT_SIGNAL( ripTrack(Job *) ), TQT_TQOBJECT(ripper),       TQT_SLOT( ripTrack(Job *)) );
    connect( tracks, TQT_SIGNAL( hasTracks(bool) ), rip,           TQT_SLOT( setEnabled(bool)) );
    connect( tracks, TQT_SIGNAL( hasTracks(bool) ), actActionMenu, TQT_SLOT( setEnabled(bool)) );
    connect( tracks, TQT_SIGNAL( hasTracks(bool) ), deselectAll,   TQT_SLOT( setEnabled(bool)) );
    connect( tracks, TQT_SIGNAL( hasTracks(bool) ), selectAll,     TQT_SLOT( setEnabled(bool)) );

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

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

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

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

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

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

    setupGUI();
}

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

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

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

    int i=0;
    TQString currentGroup = TQString("Encoder_%1").arg(i);
    while(EncoderPrefs::hasPrefs(currentGroup)){
        ripMenu->insertItem(EncoderPrefs::prefs(currentGroup)->encoderName(), i);
        currentGroup = TQString("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() {
    TQString status = i18n("Idle.");
    TQString rippingStatus;
    TQString 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, TQT_SIGNAL(startJob(Job*)),encoder, TQT_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, TQT_SIGNAL(settingsChanged()), ripper, TQT_SLOT(loadSettings()));
    connect(dialog, TQT_SIGNAL(settingsChanged()), TQT_TQOBJECT(encoder), TQT_SLOT(loadSettings()));
    connect(dialog, TQT_SIGNAL(settingsChanged()), TQT_TQOBJECT(tracks), TQT_SLOT(loadSettings()));
    connect(dialog->encoderConfigImp, TQT_SIGNAL(encoderUpdated()), TQT_TQOBJECT(encoder), TQT_SLOT(loadSettings()));
    dialog->show();
}

SettingsDialog::SettingsDialog(TQWidget *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(TQString("libkcddb"), KCModuleLoader::Inline);
        if (cddb)
        {
            cddb->load();
            addPage(cddb, i18n("CDDB"), "cdaudio_mount", i18n("CDDB Configuration"), false);
            connect(cddb, TQT_SIGNAL(changed(bool)), TQT_TQOBJECT(this), TQT_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"