summaryrefslogtreecommitdiffstats
path: root/libk3b/projects/mixedcd/k3bmixeddoc.cpp
blob: b5d4451419b63853de2e5052ddc864e6f5f61ac9 (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
/* 
 *
 * $Id: k3bmixeddoc.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 "k3bmixeddoc.h"
#include "k3bmixedjob.h"

#include <k3bdatadoc.h>
#include <k3baudiodoc.h>
#include <k3bglobals.h>
#include <k3bmsf.h>

#include <tqfileinfo.h>
#include <tqdom.h>

#include <klocale.h>
#include <tdeconfig.h>
#include <tdeapplication.h>
#include <kmessagebox.h>



K3bMixedDoc::K3bMixedDoc( TQObject* parent )
  : K3bDoc( parent )
{
  m_dataDoc = new K3bDataDoc( this );
  m_audioDoc = new K3bAudioDoc( this );

  connect( m_dataDoc, TQT_SIGNAL(changed()),
	   this, TQT_SIGNAL(changed()) );
  connect( m_audioDoc, TQT_SIGNAL(changed()),
	   this, TQT_SIGNAL(changed()) );
}


K3bMixedDoc::~K3bMixedDoc()
{
}


bool K3bMixedDoc::newDocument()
{
  m_dataDoc->newDocument();
  m_audioDoc->newDocument();

  return K3bDoc::newDocument();
}


TQString K3bMixedDoc::name() const
{
  return m_dataDoc->name();
}


void K3bMixedDoc::setURL( const KURL& url )
{
  K3bDoc::setURL( url );
  m_audioDoc->setURL( url );
  m_dataDoc->setURL( url );
}


void K3bMixedDoc::setModified( bool m )
{
  m_audioDoc->setModified( m );
  m_dataDoc->setModified( m );
}


bool K3bMixedDoc::isModified() const
{
  return ( m_audioDoc->isModified() || m_dataDoc->isModified() );
}


TDEIO::filesize_t K3bMixedDoc::size() const
{
  return m_dataDoc->size() + m_audioDoc->size();
}

K3b::Msf K3bMixedDoc::length() const
{
  return m_dataDoc->length() + m_audioDoc->length();
}


int K3bMixedDoc::numOfTracks() const
{
  return m_audioDoc->numOfTracks() + 1;
}


K3bBurnJob* K3bMixedDoc::newBurnJob( K3bJobHandler* hdl, TQObject* parent )
{
  return new K3bMixedJob( this, hdl, parent  );
}


void K3bMixedDoc::addUrls( const KURL::List& urls )
{
  dataDoc()->addUrls( urls );
}


bool K3bMixedDoc::loadDocumentData( TQDomElement* rootElem )
{
  TQDomNodeList nodes = rootElem->childNodes();

  if( nodes.length() < 4 )
    return false;

  if( nodes.item(0).nodeName() != "general" )
    return false;
  if( !readGeneralDocumentData( nodes.item(0).toElement() ) )
    return false;

  if( nodes.item(1).nodeName() != "audio" )
    return false;
  TQDomElement audioElem = nodes.item(1).toElement();
  if( !m_audioDoc->loadDocumentData( &audioElem ) )
    return false;

  if( nodes.item(2).nodeName() != "data" )
    return false;
  TQDomElement dataElem = nodes.item(2).toElement();
  if( !m_dataDoc->loadDocumentData( &dataElem ) )
    return false;

  if( nodes.item(3).nodeName() != "mixed" )
    return false;

  TQDomNodeList optionList = nodes.item(3).childNodes();
  for( uint i = 0; i < optionList.count(); i++ ) {

    TQDomElement e = optionList.item(i).toElement();
    if( e.isNull() )
      return false;

    if( e.nodeName() == "remove_buffer_files" )
      setRemoveImages( e.toElement().text() == "yes" );
    else if( e.nodeName() == "image_path" )
      setTempDir( e.toElement().text() );
    else if( e.nodeName() == "mixed_type" ) {
      TQString mt = e.toElement().text();
      if( mt == "last_track" )
	setMixedType( DATA_LAST_TRACK );
      else if( mt == "second_session" )
	setMixedType( DATA_SECOND_SESSION );
      else
	setMixedType( DATA_FIRST_TRACK );
    }
  }

  return true;
}


bool K3bMixedDoc::saveDocumentData( TQDomElement* docElem )
{
  TQDomDocument doc = docElem->ownerDocument();
  saveGeneralDocumentData( docElem );

  TQDomElement audioElem = doc.createElement( "audio" );
  m_audioDoc->saveDocumentData( &audioElem );
  docElem->appendChild( audioElem );

  TQDomElement dataElem = doc.createElement( "data" );
  m_dataDoc->saveDocumentData( &dataElem );
  docElem->appendChild( dataElem );

  TQDomElement mixedElem = doc.createElement( "mixed" );
  docElem->appendChild( mixedElem );

  TQDomElement bufferFilesElem = doc.createElement( "remove_buffer_files" );
  bufferFilesElem.appendChild( doc.createTextNode( removeImages() ? "yes" : "no" ) );
  mixedElem.appendChild( bufferFilesElem );

  TQDomElement imagePathElem = doc.createElement( "image_path" );
  imagePathElem.appendChild( doc.createTextNode( tempDir() ) );
  mixedElem.appendChild( imagePathElem );

  TQDomElement mixedTypeElem = doc.createElement( "mixed_type" );
  switch( mixedType() ) {
  case DATA_FIRST_TRACK:
    mixedTypeElem.appendChild( doc.createTextNode( "first_track" ) );
    break;
  case DATA_LAST_TRACK:
    mixedTypeElem.appendChild( doc.createTextNode( "last_track" ) );
    break;
  case DATA_SECOND_SESSION:
    mixedTypeElem.appendChild( doc.createTextNode( "second_session" ) );
    break;
  }
  mixedElem.appendChild( mixedTypeElem );

  setModified( false );

  return true;
}

  
K3bDevice::Toc K3bMixedDoc::toToc( int dataMode, const K3b::Msf& dataTrackLength ) const
{
  // !inaccurate datatrack size!
  K3bDevice::Track dataTrack( 0, dataTrackLength > 0 ? dataTrackLength-1 : m_dataDoc->length()-1, 
				K3bDevice::Track::DATA, dataMode );
  K3bDevice::Toc toc = audioDoc()->toToc();
  if( mixedType() == DATA_FIRST_TRACK ) {
    // fix the audio tracks' sectors
    for( K3bDevice::Toc::iterator it = toc.begin(); it != toc.end(); ++it ) {
      (*it).setLastSector( (*it).lastSector() + dataTrack.length() );
      (*it).setFirstSector( (*it).firstSector() + dataTrack.length() );
    }
    toc.insert( toc.begin(), dataTrack );
  }
  else {
    // fix the datatrack's sectors
    dataTrack.setLastSector( dataTrack.lastSector() + toc.back().lastSector()+1 );
    dataTrack.setFirstSector( toc.back().lastSector()+1 );
    toc.append( dataTrack );
    
    if( mixedType() == DATA_SECOND_SESSION ) {
      // fix the session numbers
      for( K3bDevice::Toc::iterator it = toc.begin(); it != toc.end(); ++it ) {
	if( (*it).type() == K3bDevice::Track::DATA )
	  (*it).setSession( 2 );
	else
	  (*it).setSession( 1 );
      }
    }
  }
  
  return toc;
}

#include "k3bmixeddoc.moc"