1 /*
2     SPDX-FileCopyrightText: 1998-2009 Sebastian Trueg <trueg@k3b.org>
3     SPDX-License-Identifier: GPL-2.0-or-later
4 */
5 
6 #ifndef _K3B_GLOBALS_H_
7 #define _K3B_GLOBALS_H_
8 
9 #include <config-k3b.h>
10 
11 #include "k3bdevicetypes.h"
12 #include "k3b_export.h"
13 
14 #include <KIO/Global>
15 
16 #include <QFile>
17 #include <QString>
18 #include <QUrl>
19 
20 #include <sys/stat.h>
21 
22 #ifdef HAVE_STAT64
23 #define k3b_struct_stat struct stat64
24 #define k3b_stat        ::stat64
25 #define k3b_lstat       ::lstat64
26 #else
27 #define k3b_struct_stat struct stat
28 #define k3b_stat        ::stat
29 #define k3b_lstat       ::lstat
30 #endif
31 
32 
33 namespace K3b {
34     class ExternalBin;
35     class Msf;
36     class Version;
37     namespace Device {
38         class Device;
39     }
40 
41     enum MediaSize {
42         MediaSizeCd74Min = 74*60*75,
43         MediaSizeCd80Min = 80*60*75,
44         MediaSizeCd100Min = 100*60*75,
45 
46         MediaSizeDvd4Gb = 2295104,
47         MediaSizeDvd8Gb = 4173824,
48 
49         MediaSizeBluRay25Gb = 12219392,
50         MediaSizeBluRay50Gb = 24438784
51     };
52 
53     enum WritingApp {
54         WritingAppAuto = 0,
55         WritingAppCdrecord = 1,
56         WritingAppCdrdao = 2,
57         WritingAppGrowisofs = 4,
58         WritingAppDvdRwFormat = 8,
59         WritingAppCdrskin = 9
60     };
61     Q_DECLARE_FLAGS( WritingApps, WritingApp )
62 
63     LIBK3B_EXPORT WritingApp writingAppFromString( const QString& );
64     LIBK3B_EXPORT QString writingAppToString( WritingApp );
65 
66     /**
67      * The data mode which determines the size of the user data in data
68      * CD sectors.
69      */
70     enum DataMode {
71         DataModeAuto, /**< let %K3b determine the best mode */
72         DataMode1,    /**< refers to the default Yellow book mode1 */
73         DataMode2     /**< refers to CDROM XA mode2 form1 */
74     };
75 
76     /**
77      * The sector size denotes the number of bytes K3b provides per sector.
78      * This is based on the sizes cdrecord's -data, -xa, and -xamix parameters
79      * demand.
80      */
81     enum SectorSize {
82         SectorSizeAuto = 0,
83         SectorSizeAudio = 2352,
84         SectorSizeData2048 = 2048,
85         SectorSizeData2048Subheader = 2056,
86         SectorSizeData2324 = 2324,
87         SectorSizeData2324Subheader = 2332,
88         SectorSizeRaw = 2448
89     };
90 
91     /**
92      * WritingModeAuto  - let K3b determine the best mode
93      * WritingModeTao   - Track at once
94      * WritingModeSao   - Disk at once (or session at once)
95      * WritingModeRaw   - Raw mode
96      *
97      * may be or'ed together (except for WritingModeAuto of course)
98      */
99     enum WritingMode {
100         WritingModeAuto = 0,
101         WritingModeTao = Device::WRITINGMODE_TAO,
102         WritingModeSao = Device::WRITINGMODE_SAO,
103         WritingModeRaw = Device::WRITINGMODE_RAW,
104         WritingModeIncrementalSequential = Device::WRITINGMODE_INCR_SEQ,  // Incremental Sequential
105         WritingModeRestrictedOverwrite = Device::WRITINGMODE_RES_OVWR // Restricted Overwrite
106     };
107     Q_DECLARE_FLAGS( WritingModes, WritingMode )
108 
109     /**
110      * Unified mode for erasing/formatting of CD-RW/DVD-RW/BD-RW
111      */
112     enum FormattingMode {
113         FormattingComplete = 0,
114         FormattingQuick = 1
115     };
116 
117     LIBK3B_EXPORT QString writingModeString( WritingModes );
118 
119     LIBK3B_EXPORT qint16 swapByteOrder( const qint16& i );
120     LIBK3B_EXPORT qint32 swapByteOrder( const qint32& i );
121     LIBK3B_EXPORT qint64 swapByteOrder( const qint64& i );
122 
123     /**
124      * This checks the free space on the filesystem path is in.
125      * We use this since we encountered problems with the KDE version.
126      * @returns true on success.
127      *
128      * \deprecated Use KDiskFreeSpaceInfo
129      */
130     LIBK3B_EXPORT bool kbFreeOnFs( const QString& path, unsigned long& size, unsigned long& avail );
131 
132     /**
133      * Cut a filename preserving the extension
134      */
135     LIBK3B_EXPORT QString cutFilename( const QString& name, int len );
136 
137     LIBK3B_EXPORT QString removeFilenameExtension( const QString& name );
138 
139     /**
140      * Append a number to a filename preserving the extension.
141      * The resulting name's length will not exceed @p maxlen
142      */
143     LIBK3B_EXPORT QString appendNumberToFilename( const QString& name, int num, unsigned int maxlen );
144 
145     LIBK3B_EXPORT QString findUniqueFilePrefix( const QString& _prefix = QString(), const QString& path = QString() );
146 
147     /**
148      * Find a unique filename in directory d (if d is empty the method uses the defaultTempPath)
149      */
150     LIBK3B_EXPORT QString findTempFile( const QString& ending = QString(), const QString& d = QString() );
151 
152     /**
153      * Wrapper around QStandardPaths::findExecutable which searches the PATH and some additional
154      * directories to find system tools which are normally only in root's PATH.
155      */
156     LIBK3B_EXPORT QString findExe( const QString& name );
157 
158     /**
159      * get the default K3b temp path to store image files
160      *
161      * \sa GlobalSettings::defaultTempPath
162      */
163     LIBK3B_EXPORT QString defaultTempPath();
164 
165     /**
166      * makes sure a path ends with a "/"
167      */
168     LIBK3B_EXPORT QString prepareDir( const QString& dir );
169 
170     /**
171      * returns the parent dir of a path.
172      * CAUTION: this does only work well with absolute paths.
173      *
174      * Example: /usr/share/doc -> /usr/share/
175      */
176     LIBK3B_EXPORT QString parentDir( const QString& path );
177 
178     /**
179      * For now this just replaces multiple occurrences of / with a single /
180      */
181     LIBK3B_EXPORT QString fixupPath( const QString& );
182 
183     /**
184      * resolves a symlinks completely. Meaning it also handles links to links to links...
185      */
186     LIBK3B_EXPORT QString resolveLink( const QString& );
187 
188     LIBK3B_EXPORT Version kernelVersion();
189 
190     /**
191      * Kernel version stripped of all suffixes
192      */
193     LIBK3B_EXPORT Version simpleKernelVersion();
194 
195     LIBK3B_EXPORT QString systemName();
196 
197     LIBK3B_EXPORT KIO::filesize_t filesize( const QUrl& );
198 
199     /**
200      * Calculate the total size of an image file. This also includes
201      * images split by a FileSplitter.
202      *
203      * \returns the total size of the image file at url
204      */
205     LIBK3B_EXPORT KIO::filesize_t imageFilesize( const QUrl& url );
206 
207     /**
208      * true if the kernel supports ATAPI devices without SCSI emulation.
209      * use in combination with the ExternalProgram feature "plain-atapi"
210      */
211     LIBK3B_EXPORT bool plainAtapiSupport();
212 
213     /**
214      * true if the kernel supports ATAPI devices without SCSI emulation
215      * via the ATAPI: pseudo stuff
216      * use in combination with the ExternalProgram feature "hacked-atapi"
217      */
218     LIBK3B_EXPORT bool hackedAtapiSupport();
219 
220     /**
221      * Used to create a parameter for cdrecord, cdrdao or readcd.
222      * Takes care of SCSI and ATAPI.
223      */
224     LIBK3B_EXPORT QString externalBinDeviceParameter( Device::Device* dev, const ExternalBin* );
225 
226     /**
227      * Tries to convert urls from local protocols != "file" to file (for now supports media:/)
228      */
229     LIBK3B_EXPORT QUrl convertToLocalUrl( const QUrl& url );
230     LIBK3B_EXPORT QList<QUrl> convertToLocalUrls( const QList<QUrl>& l );
231 
232     LIBK3B_EXPORT qint16 fromLe16( char* );
233     LIBK3B_EXPORT qint32 fromLe32( char* );
234     LIBK3B_EXPORT qint64 fromLe64( char* );
235 
236     LIBK3B_EXPORT bool isMounted( Device::Device* );
237 
238     /**
239      * Tries to unmount the device ignoring its actual mounting state.
240      * This method uses both KIO::unmount and pumount if available.
241      */
242     LIBK3B_EXPORT bool unmount( Device::Device* );
243 
244     /**
245      * Tries to mount the medium. Since K3b does not gather any information
246      * about mount points the only methods used are pmount and HAL::mount
247      */
248     LIBK3B_EXPORT bool mount( Device::Device* );
249 
250     /**
251      * Ejects the medium in the device or simply opens the tray.
252      * This method improves over Device::Device::eject in that it
253      * unmounts before ejecting and introduces HAL support.
254      *
255      * It also makes sure the MediaCache is up to date. This is very
256      * important in case one uses the EmptyDiscWaiter directly after
257      * ejecting. If the MediaCache would not be updated, it might still
258      * contain the old media information.
259      *
260      * \sa MediaCache::reset
261      */
262     LIBK3B_EXPORT bool eject( Device::Device* );
263 
264     /**
265      * Get the speed multiplicator for a media type.
266      * \sa K3b::Device::SpeedMultiplicator
267      */
268     LIBK3B_EXPORT K3b::Device::SpeedMultiplicator speedMultiplicatorForMediaType( K3b::Device::MediaType mediaType );
269 
270     /**
271      * Describes format of writing speed produced by formatWritingSpeedFactor function.
272      * \sa K3b::formatWritingSpeedFactor
273      */
274     enum SpeedFormat {
275         SpeedFormatInteger, /**< Format as integer number */
276         SpeedFormatReal     /**< Format as real number (integer or fraction) */
277     };
278 
279     /**
280      * Create a string representation of the speed factor to be used in command line
281      * commands like cdrecord and growisofs.
282      *
283      * \param speed The speed in KB/s
284      * \param mediaType The media type that is going to be written. This is used to
285      * determine the multiplicator factor.
286      * \param speedFormat specifies format of speed value. E.g. cdrecord only accepts
287      * integral speed values, in that case SpeedFormatInteger can be used.
288      *
289      * This method takes small variances into account and rounds them properly. Also
290      * the "weird" burn speeds like 2.4 are handled.
291      */
292     LIBK3B_EXPORT QString
293     formatWritingSpeedFactor( int speed, K3b::Device::MediaType mediaType, SpeedFormat speedFormat = SpeedFormatReal );
294 
295     /**
296      * Checks if overburn can be performed taking into consideration
297      * project size and 'overburn' setting in GlobalSettings.
298      * \param projectSize Size of project to be written
299      * \param capacity Declared capacity of a medium
300      */
301     LIBK3B_EXPORT bool IsOverburnAllowed( const Msf& projectSize, const Msf& capacity );
302 
303     /**
304      * Checks if overburn can be performed taking into consideration
305      * project size, size of data already written to disk and 'overburn' setting in GlobalSettings.
306      * \param projectSize Size of project to be written
307      * \param capacity Declared capacity of a medium
308      * \param usedCapacity Size of the used part of a medium
309      */
310     LIBK3B_EXPORT bool IsOverburnAllowed( const Msf& projectSize, const Msf& capacity, const Msf& usedCapacity );
311 
312     QDebug& operator<<( QDebug& dbg, WritingMode );
313     QDebug& operator<<( QDebug& dbg, WritingModes );
314 }
315 
316 Q_DECLARE_OPERATORS_FOR_FLAGS(K3b::WritingApps)
317 Q_DECLARE_OPERATORS_FOR_FLAGS(K3b::WritingModes)
318 
319 #endif
320