File indexing completed on 2024-05-12 04:51:03

0001 /*
0002     SPDX-FileCopyrightText: 1998-2008 Sebastian Trueg <trueg@k3b.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 
0008 #include "k3bdataitem.h"
0009 #include "k3bdiritem.h"
0010 #include "k3bdatadoc.h"
0011 #include "k3bisooptions.h"
0012 #include <QDebug>
0013 
0014 #include <math.h>
0015 
0016 
0017 class K3b::DataItem::Private
0018 {
0019 public:
0020     K3b::DataItem::ItemFlags flags;
0021 };
0022 
0023 
0024 K3b::DataItem::DataItem( const ItemFlags& flags )
0025     : m_parentDir(0),
0026       m_sortWeight(0),
0027       m_bHideOnRockRidge(false),
0028       m_bHideOnJoliet(false),
0029       m_bRemoveable(true),
0030       m_bRenameable(true),
0031       m_bMovable(true),
0032       m_bHideable(true),
0033       m_bWriteToCd(true)
0034 {
0035     d = new Private;
0036     d->flags = flags;
0037 }
0038 
0039 
0040 K3b::DataItem::DataItem( const K3b::DataItem& item )
0041     : m_k3bName( item.m_k3bName ),
0042       m_extraInfo( item.m_extraInfo ),
0043       m_parentDir( 0 ),
0044       m_sortWeight( item.m_sortWeight ),
0045       m_bHideOnRockRidge( item.m_bHideOnRockRidge ),
0046       m_bHideOnJoliet( item.m_bHideOnJoliet ),
0047       m_bRemoveable( item.m_bRemoveable ),
0048       m_bRenameable( item.m_bRenameable ),
0049       m_bMovable( item.m_bMovable ),
0050       m_bHideable( item.m_bHideable ),
0051       m_bWriteToCd( item.m_bWriteToCd )
0052 {
0053     d = new Private;
0054     d->flags = item.d->flags;
0055 }
0056 
0057 
0058 K3b::DataItem::~DataItem()
0059 {
0060     delete d;
0061 }
0062 
0063 
0064 const K3b::DataItem::ItemFlags& K3b::DataItem::flags() const
0065 {
0066    return d->flags;
0067 }
0068 
0069 
0070 void K3b::DataItem::setFlags( const ItemFlags& flags )
0071 {
0072     d->flags = flags;
0073 }
0074 
0075 
0076 bool K3b::DataItem::isDir() const
0077 {
0078    return d->flags & DIR;
0079 }
0080 
0081 
0082 bool K3b::DataItem::isFile() const
0083 {
0084    return d->flags & FILE;
0085 }
0086 
0087 
0088 bool K3b::DataItem::isSpecialFile() const
0089 {
0090    return d->flags & SPECIALFILE;
0091 }
0092 
0093 
0094 bool K3b::DataItem::isSymLink() const
0095 {
0096    return d->flags & SYMLINK;
0097 }
0098 
0099 
0100 bool K3b::DataItem::isFromOldSession() const
0101 {
0102    return d->flags & OLD_SESSION;
0103 }
0104 
0105 
0106 bool K3b::DataItem::isBootItem() const
0107 {
0108    return d->flags & BOOT_IMAGE;
0109 }
0110 
0111 
0112 KIO::filesize_t K3b::DataItem::size() const
0113 {
0114     if( DataDoc* doc = getDoc() ) {
0115         return itemSize( doc->isoOptions().followSymbolicLinks() ||
0116                          !doc->isoOptions().createRockRidge() );
0117     } else {
0118         return itemSize( false );
0119     }
0120 }
0121 
0122 
0123 K3b::Msf K3b::DataItem::blocks() const
0124 {
0125     if( DataDoc* doc = getDoc() ) {
0126         return itemBlocks( doc->isoOptions().followSymbolicLinks() ||
0127                            !doc->isoOptions().createRockRidge() );
0128     } else {
0129         return itemBlocks( false );
0130     }
0131 }
0132 
0133 
0134 K3b::Msf K3b::DataItem::itemBlocks( bool followSymbolicLinks ) const
0135 {
0136     return (long)::ceil( (double)itemSize( followSymbolicLinks ) / 2048.0 );
0137 }
0138 
0139 
0140 void K3b::DataItem::setK3bName( const QString& name ) {
0141     if ( name != m_k3bName ) {
0142         // test for not-allowed characters
0143         if( name.contains('/') ) {
0144             qDebug() << "(K3b::DataItem) name contained invalid characters!";
0145             return;
0146         }
0147 
0148         if( parent() ) {
0149             K3b::DataItem* item = parent()->find( name );
0150             if( item && item != this ) {
0151                 qDebug() << "(K3b::DataItem) item with that name already exists.";
0152                 return;
0153             }
0154         }
0155 
0156         m_k3bName = name;
0157 
0158         if( DataDoc* doc = getDoc() ) {
0159             doc->setModified();
0160         }
0161     }
0162 }
0163 
0164 
0165 K3b::DataDoc* K3b::DataItem::getDoc() const
0166 {
0167     return m_parentDir ? m_parentDir->getDoc() : 0;
0168 }
0169 
0170 
0171 QString K3b::DataItem::k3bName() const
0172 {
0173     return m_k3bName;
0174 }
0175 
0176 
0177 K3b::DataItem* K3b::DataItem::take()
0178 {
0179     if( parent() )
0180         parent()->takeDataItem( this );
0181 
0182     return this;
0183 }
0184 
0185 
0186 QString K3b::DataItem::k3bPath() const
0187 {
0188     if( !parent() )
0189         return QString();  // the root item is the only one not having a parent
0190     else if( isDir() )
0191         return parent()->k3bPath() + k3bName() + '/';
0192     else
0193         return parent()->k3bPath() + k3bName();
0194 }
0195 
0196 
0197 QString K3b::DataItem::writtenPath() const
0198 {
0199     if( !parent() )
0200         return QString();  // the root item is the only one not having a parent
0201     else if( isDir() )
0202         return parent()->writtenPath() + writtenName() + '/';
0203     else
0204         return parent()->writtenPath() + writtenName();
0205 }
0206 
0207 
0208 QString K3b::DataItem::iso9660Path() const
0209 {
0210     if( !parent() )
0211         return QString();  // the root item is the only one not having a parent
0212     else if( isDir() )
0213         return parent()->iso9660Path() + iso9660Name() + '/';
0214     else
0215         return parent()->iso9660Path() + iso9660Name();
0216 }
0217 
0218 
0219 K3b::DataItem* K3b::DataItem::nextSibling() const
0220 {
0221     K3b::DataItem* item = const_cast<K3b::DataItem*>(this); // urg, but we know that we don't mess with it, so...
0222     K3b::DirItem* parentItem = parent();
0223 
0224     while( parentItem ) {
0225         if( K3b::DataItem* i = parentItem->nextChild( item ) )
0226             return i;
0227 
0228         item = parentItem;
0229         parentItem = item->parent();
0230     }
0231 
0232     return 0;
0233 }
0234 
0235 
0236 void K3b::DataItem::reparent( K3b::DirItem* newParent )
0237 {
0238     // addDataItem will do all the stuff including taking this
0239     newParent->addDataItem( this );
0240 }
0241 
0242 
0243 bool K3b::DataItem::hideOnRockRidge() const
0244 {
0245     if( !isHideable() )
0246         return false;
0247     if( parent() )
0248         return m_bHideOnRockRidge || parent()->hideOnRockRidge();
0249     else
0250         return m_bHideOnRockRidge;
0251 }
0252 
0253 
0254 bool K3b::DataItem::hideOnJoliet() const
0255 {
0256     if( !isHideable() )
0257         return false;
0258     if( parent() )
0259         return m_bHideOnJoliet || parent()->hideOnJoliet();
0260     else
0261         return m_bHideOnJoliet;
0262 }
0263 
0264 
0265 void K3b::DataItem::setHideOnRockRidge( bool b )
0266 {
0267     // there is no use in changing the value if
0268     // it is already set by the parent
0269     if( ( !parent() || !parent()->hideOnRockRidge() ) &&
0270         b != m_bHideOnRockRidge ) {
0271         m_bHideOnRockRidge = b;
0272         if( DataDoc* doc = getDoc() ) {
0273             doc->setModified();
0274         }
0275     }
0276 }
0277 
0278 
0279 void K3b::DataItem::setHideOnJoliet( bool b )
0280 {
0281     // there is no use in changing the value if
0282     // it is already set by the parent
0283     if( ( !parent() || !parent()->hideOnJoliet() ) &&
0284         b != m_bHideOnJoliet ) {
0285         m_bHideOnJoliet = b;
0286         if( DataDoc* doc = getDoc() ) {
0287             doc->setModified();
0288         }
0289     }
0290 }
0291 
0292 
0293 int K3b::DataItem::depth() const
0294 {
0295     if( parent() )
0296         return parent()->depth() + 1;
0297     else
0298         return 0;
0299 }
0300 
0301 
0302 QMimeType K3b::DataItem::mimeType() const
0303 {
0304     return QMimeType();
0305 }