1 /*
2     SPDX-FileCopyrightText: 1998-2008 Sebastian Trueg <trueg@k3b.org>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #ifndef K3B_ISO_IMAGER_H
8 #define K3B_ISO_IMAGER_H
9 
10 #include "k3bjob.h"
11 #include "k3bmkisofshandler.h"
12 #include "k3bprocess.h"
13 
14 #include <QStringList>
15 
16 class QTextStream;
17 class QTemporaryFile;
18 
19 namespace K3b {
20     class DataDoc;
21     class DirItem;
22     class FileItem;
23 
24     class IsoImager : public Job, public MkisofsHandler
25     {
26         Q_OBJECT
27 
28     public:
29         IsoImager( DataDoc*, JobHandler*, QObject* parent = 0 );
30         ~IsoImager() override;
31 
32         bool active() const override;
33 
size()34         int size() const { return m_mkisofsPrintSizeResult; }
35 
36         bool hasBeenCanceled() const override;
37 
38         QIODevice* ioDevice() const;
39 
40     public Q_SLOTS:
41         /**
42          * Starts the actual image creation. Always run init()
43          * before starting the image creation
44          */
45         void start() override;
46         void cancel() override;
47 
48         /**
49          * Initialize the image creator. This calculates the image size and performs
50          * some checks on the project.
51          *
52          * The initialization process also finishes with the finished() signal just
53          * like a normal job operation. Get the calculated image size via size()
54          */
55         virtual void init();
56 
57         /**
58          * Only calculates the size of the image without the additional checks in
59          * init()
60          *
61          * Use this if you need to recalculate the image size for example if the
62          * multisession info changed.
63          */
64         virtual void calculateSize();
65 
66         /**
67          * If dev == 0 IsoImager will ignore the data in the previous session.
68          * This is usable for CD-Extra.
69          */
70         void setMultiSessionInfo( const QString&, Device::Device* dev = 0 );
71 
72         QString multiSessionInfo() const;
73         Device::Device* multiSessionImportDevice() const;
74 
device()75         Device::Device* device() const { return m_device; }
doc()76         DataDoc* doc() const { return m_doc; }
77 
78     protected:
79         void handleMkisofsProgress( int ) override;
80         void handleMkisofsInfoMessage( const QString&, int ) override;
81 
82         virtual bool addMkisofsParameters( bool printSize = false );
83 
84         /**
85          * calls writePathSpec, writeRRHideFile, and writeJolietHideFile
86          */
87         bool prepareMkisofsFiles();
88 
89         /**
90          * The dummy dir is used to create dirs on the iso-filesystem.
91          *
92          * @return an empty dummy dir for use with DirItems.
93          */
94         QString dummyDir( DirItem* );
95 
96         void outputData();
97         void initVariables();
98         virtual void cleanup();
99         void clearDummyDirs();
100 
101         /**
102          * @returns The number of entries written or -1 on error
103          */
104         virtual int writePathSpec();
105         bool writeRRHideFile();
106         bool writeJolietHideFile();
107         bool writeSortWeightFile();
108 
109         // used by writePathSpec
110         virtual int writePathSpecForDir( DirItem* dirItem, QTextStream& stream );
111         virtual void writePathSpecForFile( FileItem*, QTextStream& stream );
112         QString escapeGraftPoint( const QString& str );
113 
114         QTemporaryFile* m_pathSpecFile;
115         QTemporaryFile* m_rrHideFile;
116         QTemporaryFile* m_jolietHideFile;
117         QTemporaryFile* m_sortWeightFile;
118 
119         Process* m_process;
120 
121         bool m_canceled;
122 
123     protected Q_SLOTS:
124         virtual void slotReceivedStderr( const QString& );
125         virtual void slotProcessExited( int, QProcess::ExitStatus );
126 
127     private Q_SLOTS:
128         void slotCollectMkisofsPrintSizeStderr( const QString& );
129         void slotCollectMkisofsPrintSizeStdout( const QString& );
130         void slotMkisofsPrintSizeFinished();
131         void slotDataPreparationDone( bool success );
132 
133     private:
134         void startSizeCalculation();
135 
136         class Private;
137         Private* d;
138 
139         DataDoc* m_doc;
140 
141         bool m_noDeepDirectoryRelocation;
142 
143         bool m_importSession;
144         QString m_multiSessionInfo;
145         Device::Device* m_device;
146 
147         // used for mkisofs -print-size parsing
148         QString m_collectedMkisofsPrintSizeStdout;
149         QString m_collectedMkisofsPrintSizeStderr;
150         int m_mkisofsPrintSizeResult;
151 
152         QStringList m_tempFiles;
153 
154         bool m_containsFilesWithMultibleBackslashes;
155 
156         // used to create a unique session id
157         static int s_imagerSessionCounter;
158 
159         int m_sessionNumber;
160     };
161 }
162 
163 #endif
164