File indexing completed on 2024-06-16 04:33:37

0001 /*
0002     SPDX-FileCopyrightText: 1998-2009 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #ifndef _K3B_GLOBALS_H_
0007 #define _K3B_GLOBALS_H_
0008 
0009 #include <config-k3b.h>
0010 
0011 #include "k3bdevicetypes.h"
0012 #include "k3b_export.h"
0013 
0014 #include <KIO/Global>
0015 
0016 #include <QFile>
0017 #include <QString>
0018 #include <QUrl>
0019 
0020 #include <sys/stat.h>
0021 
0022 #ifdef HAVE_STAT64
0023 #define k3b_struct_stat struct stat64
0024 #define k3b_stat        ::stat64
0025 #define k3b_lstat       ::lstat64
0026 #else
0027 #define k3b_struct_stat struct stat
0028 #define k3b_stat        ::stat
0029 #define k3b_lstat       ::lstat
0030 #endif
0031 
0032 
0033 namespace K3b {
0034     class ExternalBin;
0035     class Msf;
0036     class Version;
0037     namespace Device {
0038         class Device;
0039     }
0040 
0041     enum MediaSize {
0042         MediaSizeCd74Min = 74*60*75,
0043         MediaSizeCd80Min = 80*60*75,
0044         MediaSizeCd100Min = 100*60*75,
0045 
0046         MediaSizeDvd4Gb = 2295104,
0047         MediaSizeDvd8Gb = 4173824,
0048 
0049         MediaSizeBluRay25Gb = 12219392,
0050         MediaSizeBluRay50Gb = 24438784
0051     };
0052 
0053     enum WritingApp {
0054         WritingAppAuto = 0,
0055         WritingAppCdrecord = 1,
0056         WritingAppCdrdao = 2,
0057         WritingAppGrowisofs = 4,
0058         WritingAppDvdRwFormat = 8,
0059         WritingAppCdrskin = 9
0060     };
0061     Q_DECLARE_FLAGS( WritingApps, WritingApp )
0062 
0063     LIBK3B_EXPORT WritingApp writingAppFromString( const QString& );
0064     LIBK3B_EXPORT QString writingAppToString( WritingApp );
0065 
0066     /**
0067      * The data mode which determines the size of the user data in data
0068      * CD sectors.
0069      */
0070     enum DataMode {
0071         DataModeAuto, /**< let %K3b determine the best mode */
0072         DataMode1,    /**< refers to the default Yellow book mode1 */
0073         DataMode2     /**< refers to CDROM XA mode2 form1 */
0074     };
0075 
0076     /**
0077      * The sector size denotes the number of bytes K3b provides per sector.
0078      * This is based on the sizes cdrecord's -data, -xa, and -xamix parameters
0079      * demand.
0080      */
0081     enum SectorSize {
0082         SectorSizeAuto = 0,
0083         SectorSizeAudio = 2352,
0084         SectorSizeData2048 = 2048,
0085         SectorSizeData2048Subheader = 2056,
0086         SectorSizeData2324 = 2324,
0087         SectorSizeData2324Subheader = 2332,
0088         SectorSizeRaw = 2448
0089     };
0090 
0091     /**
0092      * WritingModeAuto  - let K3b determine the best mode
0093      * WritingModeTao   - Track at once
0094      * WritingModeSao   - Disk at once (or session at once)
0095      * WritingModeRaw   - Raw mode
0096      *
0097      * may be or'ed together (except for WritingModeAuto of course)
0098      */
0099     enum WritingMode {
0100         WritingModeAuto = 0,
0101         WritingModeTao = Device::WRITINGMODE_TAO,
0102         WritingModeSao = Device::WRITINGMODE_SAO,
0103         WritingModeRaw = Device::WRITINGMODE_RAW,
0104         WritingModeIncrementalSequential = Device::WRITINGMODE_INCR_SEQ,  // Incremental Sequential
0105         WritingModeRestrictedOverwrite = Device::WRITINGMODE_RES_OVWR // Restricted Overwrite
0106     };
0107     Q_DECLARE_FLAGS( WritingModes, WritingMode )
0108 
0109     /**
0110      * Unified mode for erasing/formatting of CD-RW/DVD-RW/BD-RW
0111      */
0112     enum FormattingMode {
0113         FormattingComplete = 0,
0114         FormattingQuick = 1
0115     };
0116 
0117     LIBK3B_EXPORT QString writingModeString( WritingModes );
0118 
0119     LIBK3B_EXPORT qint16 swapByteOrder( const qint16& i );
0120     LIBK3B_EXPORT qint32 swapByteOrder( const qint32& i );
0121     LIBK3B_EXPORT qint64 swapByteOrder( const qint64& i );
0122 
0123     /**
0124      * This checks the free space on the filesystem path is in.
0125      * We use this since we encountered problems with the KDE version.
0126      * @returns true on success.
0127      *
0128      * \deprecated Use KDiskFreeSpaceInfo
0129      */
0130     LIBK3B_EXPORT bool kbFreeOnFs( const QString& path, unsigned long& size, unsigned long& avail );
0131 
0132     /**
0133      * Cut a filename preserving the extension
0134      */
0135     LIBK3B_EXPORT QString cutFilename( const QString& name, int len );
0136 
0137     LIBK3B_EXPORT QString removeFilenameExtension( const QString& name );
0138 
0139     /**
0140      * Append a number to a filename preserving the extension.
0141      * The resulting name's length will not exceed @p maxlen
0142      */
0143     LIBK3B_EXPORT QString appendNumberToFilename( const QString& name, int num, unsigned int maxlen );
0144 
0145     LIBK3B_EXPORT QString findUniqueFilePrefix( const QString& _prefix = QString(), const QString& path = QString() );
0146 
0147     /**
0148      * Find a unique filename in directory d (if d is empty the method uses the defaultTempPath)
0149      */
0150     LIBK3B_EXPORT QString findTempFile( const QString& ending = QString(), const QString& d = QString() );
0151 
0152     /**
0153      * Wrapper around QStandardPaths::findExecutable which searches the PATH and some additional
0154      * directories to find system tools which are normally only in root's PATH.
0155      */
0156     LIBK3B_EXPORT QString findExe( const QString& name );
0157 
0158     /**
0159      * get the default K3b temp path to store image files
0160      *
0161      * \sa GlobalSettings::defaultTempPath
0162      */
0163     LIBK3B_EXPORT QString defaultTempPath();
0164 
0165     /**
0166      * makes sure a path ends with a "/"
0167      */
0168     LIBK3B_EXPORT QString prepareDir( const QString& dir );
0169 
0170     /**
0171      * returns the parent dir of a path.
0172      * CAUTION: this does only work well with absolute paths.
0173      *
0174      * Example: /usr/share/doc -> /usr/share/
0175      */
0176     LIBK3B_EXPORT QString parentDir( const QString& path );
0177 
0178     /**
0179      * For now this just replaces multiple occurrences of / with a single /
0180      */
0181     LIBK3B_EXPORT QString fixupPath( const QString& );
0182 
0183     /**
0184      * resolves a symlinks completely. Meaning it also handles links to links to links...
0185      */
0186     LIBK3B_EXPORT QString resolveLink( const QString& );
0187 
0188     LIBK3B_EXPORT Version kernelVersion();
0189 
0190     /**
0191      * Kernel version stripped of all suffixes
0192      */
0193     LIBK3B_EXPORT Version simpleKernelVersion();
0194 
0195     LIBK3B_EXPORT QString systemName();
0196 
0197     LIBK3B_EXPORT KIO::filesize_t filesize( const QUrl& );
0198 
0199     /**
0200      * Calculate the total size of an image file. This also includes
0201      * images split by a FileSplitter.
0202      *
0203      * \returns the total size of the image file at url
0204      */
0205     LIBK3B_EXPORT KIO::filesize_t imageFilesize( const QUrl& url );
0206 
0207     /**
0208      * true if the kernel supports ATAPI devices without SCSI emulation.
0209      * use in combination with the ExternalProgram feature "plain-atapi"
0210      */
0211     LIBK3B_EXPORT bool plainAtapiSupport();
0212 
0213     /**
0214      * true if the kernel supports ATAPI devices without SCSI emulation
0215      * via the ATAPI: pseudo stuff
0216      * use in combination with the ExternalProgram feature "hacked-atapi"
0217      */
0218     LIBK3B_EXPORT bool hackedAtapiSupport();
0219 
0220     /**
0221      * Used to create a parameter for cdrecord, cdrdao or readcd.
0222      * Takes care of SCSI and ATAPI.
0223      */
0224     LIBK3B_EXPORT QString externalBinDeviceParameter( Device::Device* dev, const ExternalBin* );
0225 
0226     /**
0227      * Tries to convert urls from local protocols != "file" to file (for now supports media:/)
0228      */
0229     LIBK3B_EXPORT QUrl convertToLocalUrl( const QUrl& url );
0230     LIBK3B_EXPORT QList<QUrl> convertToLocalUrls( const QList<QUrl>& l );
0231 
0232     LIBK3B_EXPORT qint16 fromLe16( char* );
0233     LIBK3B_EXPORT qint32 fromLe32( char* );
0234     LIBK3B_EXPORT qint64 fromLe64( char* );
0235 
0236     LIBK3B_EXPORT bool isMounted( Device::Device* );
0237 
0238     /**
0239      * Tries to unmount the device ignoring its actual mounting state.
0240      * This method uses both KIO::unmount and pumount if available.
0241      */
0242     LIBK3B_EXPORT bool unmount( Device::Device* );
0243 
0244     /**
0245      * Tries to mount the medium. Since K3b does not gather any information
0246      * about mount points the only methods used are pmount and HAL::mount
0247      */
0248     LIBK3B_EXPORT bool mount( Device::Device* );
0249 
0250     /**
0251      * Ejects the medium in the device or simply opens the tray.
0252      * This method improves over Device::Device::eject in that it
0253      * unmounts before ejecting and introduces HAL support.
0254      *
0255      * It also makes sure the MediaCache is up to date. This is very
0256      * important in case one uses the EmptyDiscWaiter directly after
0257      * ejecting. If the MediaCache would not be updated, it might still
0258      * contain the old media information.
0259      *
0260      * \sa MediaCache::reset
0261      */
0262     LIBK3B_EXPORT bool eject( Device::Device* );
0263 
0264     /**
0265      * Get the speed multiplicator for a media type.
0266      * \sa K3b::Device::SpeedMultiplicator
0267      */
0268     LIBK3B_EXPORT K3b::Device::SpeedMultiplicator speedMultiplicatorForMediaType( K3b::Device::MediaType mediaType );
0269 
0270     /**
0271      * Describes format of writing speed produced by formatWritingSpeedFactor function.
0272      * \sa K3b::formatWritingSpeedFactor
0273      */
0274     enum SpeedFormat {
0275         SpeedFormatInteger, /**< Format as integer number */
0276         SpeedFormatReal     /**< Format as real number (integer or fraction) */
0277     };
0278 
0279     /**
0280      * Create a string representation of the speed factor to be used in command line
0281      * commands like cdrecord and growisofs.
0282      *
0283      * \param speed The speed in KB/s
0284      * \param mediaType The media type that is going to be written. This is used to
0285      * determine the multiplicator factor.
0286      * \param speedFormat specifies format of speed value. E.g. cdrecord only accepts
0287      * integral speed values, in that case SpeedFormatInteger can be used.
0288      *
0289      * This method takes small variances into account and rounds them properly. Also
0290      * the "weird" burn speeds like 2.4 are handled.
0291      */
0292     LIBK3B_EXPORT QString
0293     formatWritingSpeedFactor( int speed, K3b::Device::MediaType mediaType, SpeedFormat speedFormat = SpeedFormatReal );
0294 
0295     /**
0296      * Checks if overburn can be performed taking into consideration
0297      * project size and 'overburn' setting in GlobalSettings.
0298      * \param projectSize Size of project to be written
0299      * \param capacity Declared capacity of a medium
0300      */
0301     LIBK3B_EXPORT bool IsOverburnAllowed( const Msf& projectSize, const Msf& capacity );
0302 
0303     /**
0304      * Checks if overburn can be performed taking into consideration
0305      * project size, size of data already written to disk and 'overburn' setting in GlobalSettings.
0306      * \param projectSize Size of project to be written
0307      * \param capacity Declared capacity of a medium
0308      * \param usedCapacity Size of the used part of a medium
0309      */
0310     LIBK3B_EXPORT bool IsOverburnAllowed( const Msf& projectSize, const Msf& capacity, const Msf& usedCapacity );
0311 
0312     LIBK3B_EXPORT QDebug& operator<<( QDebug& dbg, WritingMode );
0313     LIBK3B_EXPORT QDebug& operator<<( QDebug& dbg, WritingModes );
0314 }
0315 
0316 Q_DECLARE_OPERATORS_FOR_FLAGS(K3b::WritingApps)
0317 Q_DECLARE_OPERATORS_FOR_FLAGS(K3b::WritingModes)
0318 
0319 #endif