summaryrefslogtreecommitdiffstats
path: root/libk3b/core/k3bthreadjob.cpp
blob: a13f10af680936960cb3f5315d234d32badb2c8c (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
/*
 *
 * $Id: k3bthreadjob.cpp 619556 2007-01-03 17:38:12Z trueg $
 * Copyright (C) 2003 Sebastian Trueg <trueg@k3b.org>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.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.
 * See the file "COPYING" for the exact licensing terms.
 */

#include "k3bthreadjob.h"
#include "k3bthread.h"
#include "k3bprogressinfoevent.h"
#include "k3bdataevent.h"

#include <kdebug.h>
#include <kapplication.h>



K3bThreadJob::K3bThreadJob( K3bJobHandler* jh, QObject* parent, const char* name )
  : K3bJob( jh, parent, name ),
    m_running(false)
{
}


K3bThreadJob::K3bThreadJob( K3bThread* thread, K3bJobHandler* jh, QObject* parent, const char* name )
  : K3bJob( jh, parent, name ),
    m_running(false)
{
  setThread(thread);
}


K3bThreadJob::~K3bThreadJob()
{
}


QString K3bThreadJob::jobDescription() const
{
  if( m_thread )
    return m_thread->jobDescription();
  else
    return QString::null;
}


QString K3bThreadJob::jobDetails() const
{
  if( m_thread )
    return m_thread->jobDetails();
  else
    return QString::null;
}


void K3bThreadJob::setThread( K3bThread* t )
{
  m_thread = t;
  m_thread->setProgressInfoEventHandler(this);
}


void K3bThreadJob::start()
{
  if( m_thread ) {
    if( !m_running ) {
      m_thread->setProgressInfoEventHandler(this);
      m_running = true;
      m_thread->init();
      m_thread->start();
    }
    else
      kdDebug() << "(K3bThreadJob) thread not finished yet." << endl;
  }
  else {
    kdError() << "(K3bThreadJob) no job set." << endl;
    jobFinished(false);
  }
}


void K3bThreadJob::cancel()
{
  m_thread->cancel();
  // wait for the thread to finish
  //  m_thread->wait();
}


void K3bThreadJob::cleanupJob( bool success )
{
  Q_UNUSED( success );
}


void K3bThreadJob::customEvent( QCustomEvent* e )
{
  if( K3bDataEvent* de = dynamic_cast<K3bDataEvent*>(e) ) {
    emit data( de->data(), de->length() );
  }
  else {
    K3bProgressInfoEvent* be = static_cast<K3bProgressInfoEvent*>(e);
    switch( be->type() ) {
    case K3bProgressInfoEvent::Progress:
      emit percent( be->firstValue() );
      break;
    case K3bProgressInfoEvent::SubProgress:
      emit subPercent( be->firstValue() );
      break;
    case K3bProgressInfoEvent::ProcessedSize:
      emit processedSize( be->firstValue(), be->secondValue() );
      break;
    case K3bProgressInfoEvent::ProcessedSubSize:
      emit processedSubSize( be->firstValue(), be->secondValue() );
      break;
    case K3bProgressInfoEvent::InfoMessage:
      emit infoMessage( be->firstString(), be->firstValue() ); 
      break;
    case K3bProgressInfoEvent::Started:
      jobStarted();
      break;
    case K3bProgressInfoEvent::Canceled:
      emit canceled();
      break;
    case K3bProgressInfoEvent::Finished:
      // we wait until the thred really finished
      // although this may be dangerous if some thread
      // emits the finished signal although it has not finished yet
      // but makes a lot stuff easier.
      kdDebug() << "(K3bThreadJob) waiting for the thread to finish." << endl;
      m_thread->wait();
      kdDebug() << "(K3bThreadJob) thread finished." << endl;
      cleanupJob( be->firstValue() );
      m_running = false;
      jobFinished( be->firstValue() );
      break;
    case K3bProgressInfoEvent::NewTask:
      emit newTask( be->firstString() );
      break;
    case K3bProgressInfoEvent::NewSubTask:
      emit newSubTask( be->firstString() );
      break;
    case K3bProgressInfoEvent::DebuggingOutput:
      emit debuggingOutput( be->firstString(), be->secondString() );
      break;
    case K3bProgressInfoEvent::NextTrack:
      emit nextTrack( be->firstValue(), be->secondValue() );
      break;
    }
  }
}

#include "k3bthreadjob.moc"