summaryrefslogtreecommitdiffstats
path: root/libk3b/projects/datacd/k3bdiritem.cpp
blob: 3ea3236dd6b1eb2ee908f353c8e7ae44932b98e1 (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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
 *
 * $Id: k3bdiritem.cpp 652578 2007-04-11 14:21:04Z 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 "k3bdiritem.h"
#include "k3bdatadoc.h"
#include "k3bsessionimportitem.h"
#include "k3bfileitem.h"

#include <qstring.h>
#include <qptrlist.h>

#include <kdebug.h>


K3bDirItem::K3bDirItem(const QString& name, K3bDataDoc* doc, K3bDirItem* parentDir)
  : K3bDataItem( doc, parentDir ),
    m_size(0),
    m_followSymlinksSize(0),
    m_blocks(0),
    m_followSymlinksBlocks(0),
    m_files(0),
    m_dirs(0)
{
  m_k3bName = name;

  // add automagically like a qlistviewitem
  if( parent() )
    parent()->addDataItem( this );
}


K3bDirItem::K3bDirItem( const K3bDirItem& item )
  : K3bDataItem( item ),
    m_size(0),
    m_followSymlinksSize(0),
    m_blocks(0),
    m_followSymlinksBlocks(0),
    m_files(0),
    m_dirs(0),
    m_localPath( item.m_localPath )
{
  for( QPtrListIterator<K3bDataItem> it( item.children() ); *it; ++it )
    addDataItem( (*it)->copy() );
}

K3bDirItem::~K3bDirItem()
{
  // delete all children
  // doing this by hand is much saver than using the
  // auto-delete feature since some of the items' destructors
  // may change the list
  K3bDataItem* i = m_children.first();
  while( i ) {
    // it is important to use takeDataItem here to be sure
    // the size gets updated properly
    takeDataItem(i);
    delete i;
    i = m_children.first();
  }

  // this has to be done after deleting the children
  // because the directory itself has a size of 0 in K3b
  // and all it's files' sizes have already been substracted
  take();
}


K3bDataItem* K3bDirItem::copy() const
{
  return new K3bDirItem( *this );
}


K3bDirItem* K3bDirItem::getDirItem() const
{
  return const_cast<K3bDirItem*>(this);
}

K3bDirItem* K3bDirItem::addDataItem( K3bDataItem* item )
{
  // check if we are a subdir of item
  if( K3bDirItem* dirItem = dynamic_cast<K3bDirItem*>(item) ) {
    if( dirItem->isSubItem( this ) ) {
      kdDebug() << "(K3bDirItem) trying to move a dir item down in it's own tree." << endl;
      return this;
    }
  }

  if( m_children.findRef( item ) == -1 ) {
    if( item->isFile() ) {
      // do we replace an old item?
      QString name = item->k3bName();
      int cnt = 1;
      while( K3bDataItem* oldItem = find( name ) ) {
	if( !oldItem->isDir() && oldItem->isFromOldSession() ) {
	  // in this case we remove this item from it's parent and save it in the new one
	  // to be able to recover it
	  oldItem->take();
	  static_cast<K3bSessionImportItem*>(oldItem)->setReplaceItem( static_cast<K3bFileItem*>(item) );
	  static_cast<K3bFileItem*>(item)->setReplacedItemFromOldSession( oldItem );
	  break;
	}
	else {
	  //
	  // add a counter to the filename
	  //
	  if( item->k3bName()[item->k3bName().length()-4] == '.' )
	    name = item->k3bName().left( item->k3bName().length()-4 ) + QString::number(cnt++) + item->k3bName().right(4);
	  else
	    name = item->k3bName() + QString::number(cnt++);
	}
      }
      item->setK3bName( name );
    }

    m_children.append( item->take() );
    updateSize( item, false );
    if( item->isDir() )
      updateFiles( ((K3bDirItem*)item)->numFiles(), ((K3bDirItem*)item)->numDirs()+1 );
    else
      updateFiles( 1, 0 );

    item->m_parentDir = this;

    // inform the doc
    if( doc() )
      doc()->itemAddedToDir( this, item );
  }

  return this;
}


K3bDataItem* K3bDirItem::takeDataItem( K3bDataItem* item )
{
  int x = m_children.findRef( item );
  if( x > -1 ) {
    K3bDataItem* item = m_children.take();
    updateSize( item, true );
    if( item->isDir() )
      updateFiles( -1*((K3bDirItem*)item)->numFiles(), -1*((K3bDirItem*)item)->numDirs()-1 );
    else
      updateFiles( -1, 0 );

    item->m_parentDir = 0;

    // inform the doc
    if( doc() )
      doc()->itemRemovedFromDir( this, item );

    if( item->isFile() ) {
      // restore the item imported from an old session
      if( static_cast<K3bFileItem*>(item)->replaceItemFromOldSession() )
	addDataItem( static_cast<K3bFileItem*>(item)->replaceItemFromOldSession() );
    }

    return item;
  }
  else
    return 0;
}


K3bDataItem* K3bDirItem::nextSibling() const
{
  if( !m_children.isEmpty() )
    return m_children.getFirst();
  else
    return K3bDataItem::nextSibling();
}


K3bDataItem* K3bDirItem::nextChild( K3bDataItem* prev ) const
{
  // search for prev in children
  if( m_children.findRef( prev ) < 0 ) {
    return 0;
  }
  else
    return m_children.next();
}


bool K3bDirItem::alreadyInDirectory( const QString& filename ) const
{
  return (find( filename ) != 0);
}


K3bDataItem* K3bDirItem::find( const QString& filename ) const
{
  for( QPtrListIterator<K3bDataItem> it( m_children ); it.current(); ++it ) {
    if( it.current()->k3bName() == filename )
      return it.current();
  }
  return 0;
}


K3bDataItem* K3bDirItem::findByPath( const QString& p )
{
  if( p.isEmpty() || p == "/" )
    return this;

  QString path = p;
  if( path.startsWith("/") )
    path = path.mid(1);
  int pos = path.find( "/" );
  if( pos < 0 )
    return find( path );
  else {
    // do it recursivly
    K3bDataItem* item = find( path.left(pos) );
    if( item && item->isDir() )
      return ((K3bDirItem*)item)->findByPath( path.mid( pos+1 ) );
    else
      return 0;
  }
}


bool K3bDirItem::mkdir( const QString& dirPath )
{
  //
  // An absolut path always starts at the root item
  //
  if( dirPath[0] == '/' ) {
    if( parent() )
      return parent()->mkdir( dirPath );
    else
      return mkdir( dirPath.mid( 1 ) );
  }

  if( findByPath( dirPath ) )
    return false;

  QString restPath;
  QString dirName;
  int pos = dirPath.find( '/' );
  if( pos == -1 ) {
    dirName = dirPath;
  }
  else {
    dirName = dirPath.left( pos );
    restPath = dirPath.mid( pos+1 );
  }

  K3bDataItem* dir = find( dirName );
  if( !dir )
    dir = new K3bDirItem( dirName, doc(), this );
  else if( !dir->isDir() )
    return false;

  if( !restPath.isEmpty() )
    return static_cast<K3bDirItem*>(dir)->mkdir( restPath );

  return true;
}


KIO::filesize_t K3bDirItem::itemSize( bool followsylinks ) const
{
  if( followsylinks )
    return m_followSymlinksSize;
  else
    return m_size;
}


K3b::Msf K3bDirItem::itemBlocks( bool followSymlinks ) const
{
  if( followSymlinks )
    return m_followSymlinksBlocks;
  else
    return m_blocks;
}


bool K3bDirItem::isSubItem( K3bDataItem* item ) const
{
  if( dynamic_cast<K3bDirItem*>(item) == this )
    return true;

  K3bDirItem* d = item->parent();
  while( d ) {
    if( d == this ) {
      return true;
    }
    d = d->parent();
  }

  return false;
}


long K3bDirItem::numFiles() const
{
  return m_files;
}


long K3bDirItem::numDirs() const
{
  return m_dirs;
}


bool K3bDirItem::isRemoveable() const
{
  if( !K3bDataItem::isRemoveable() )
    return false;

  for( QPtrListIterator<K3bDataItem> it( m_children ); it.current(); ++it ) {
    if( !it.current()->isRemoveable() )
      return false;
  }

  return true;
}


void K3bDirItem::updateSize( K3bDataItem* item, bool removed )
{
    if ( !item->isFromOldSession() ) {
        if( removed ) {
            m_followSymlinksSize -= item->itemSize( true );
            m_size -= item->itemSize( false );
            m_followSymlinksBlocks -= item->itemBlocks( true ).lba();
            m_blocks -= item->itemBlocks( false ).lba();
        }
        else {
            m_followSymlinksSize += item->itemSize( true );
            m_size += item->itemSize( false );
            m_followSymlinksBlocks += item->itemBlocks( true ).lba();
            m_blocks += item->itemBlocks( false ).lba();
        }
    }

    if( parent() )
        parent()->updateSize( item, removed );
}

void K3bDirItem::updateFiles( long files, long dirs )
{
  m_files += files;
  m_dirs += dirs;
  if( parent() )
    parent()->updateFiles( files, dirs );
}


bool K3bDirItem::isFromOldSession() const
{
  for( QPtrListIterator<K3bDataItem> it( m_children ); it.current(); ++it ) {
    if( (*it)->isFromOldSession() )
      return true;
  }
  return false;
}


bool K3bDirItem::writeToCd() const
{
  // check if this dir contains items to write
  for( QPtrListIterator<K3bDataItem> it( m_children ); it.current(); ++it ) {
    if( (*it)->writeToCd() )
      return true;
  }
  return K3bDataItem::writeToCd();
}


K3bRootItem::K3bRootItem( K3bDataDoc* doc )
  : K3bDirItem( "root", doc, 0 )
{
}


K3bRootItem::~K3bRootItem()
{
}


const QString& K3bRootItem::k3bName() const
{
  return doc()->isoOptions().volumeID();
}


void K3bRootItem::setK3bName( const QString& text )
{
  doc()->setVolumeID( text );
}