summaryrefslogtreecommitdiffstats
path: root/kghostview/kgvdocument.h
blob: 960843f81de36c109fe98f19b21f1f35505a029e (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
/** 
 * Copyright (C) 1997-2003 the KGhostView authors. See file AUTHORS.
 * 	
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#ifndef __KGV_DOCUMENT_H__
#define __KGV_DOCUMENT_H__

#include <qprinter.h>
#include <qsize.h>
#include <qstring.h>

#include "kgv.h"
#include "dscparse_adapter.h"
#include "kgv_view.h"

class KPrinter;
class KTempFile;
class Pdf2dsc;

class KGVDocument : public QObject
{
    Q_OBJECT

public:
    enum Format { PS, PDF };

    KGVDocument( KGVPart* part, const char* name = 0 );
    ~KGVDocument();

    void readSettings();

    /**
     * Is a file currently open?
     */
    bool isOpen() const;

    /**
     * Open the @em local file @p filename asynchronously.
     */
    void openFile( const QString& filename, const QString& mimetype );

    /**
     * Close the document.
     */
    void close();

    const QString& fileName() const { return _fileName; }
    FILE* psFile() { return _psFile; }

    Format format() const { return _format; }

    /**
     * @return the document structure for the current document, or 0 if no
     * file is loaded.
     */ 
    KDSC* const dsc() const;

    /**
     * A list of page media (sizes).
     */
    QStringList mediaNames() const;

    const CDSCMEDIA* findMediaByName( const QString& mediaName ) const;

    QSize computePageSize( const QString& pageMedia ) const;

    static QString pageSizeToString( QPrinter::PageSize );

    /**
     * Returns a QString which contains a range representation of @p pageList.
     * Examples: [1,3]       -> "1,3"
     *           [1,2,3]     -> "1-3"
     *           [1,3,4,5,8] -> "1,3-5,8"
     */
    static QString pageListToRange( const KGV::PageList& );

public slots:
    void fileChanged( const QString& );

    void saveAs();
    void print();

signals:
    /**
     * Emitted if a fileChanged() call fails.
     */
    void fileChangeFailed();

    void completed();
    void canceled( const QString& );
    
protected:
    void scanDSC();
    void clearTemporaryFiles();
    
    /** 
     * Tries to uncompress the file. Returns true if it uncompressed the file
     * ( it changes _fileName to point to the uncompressed version of the file ).
     * Else, it does nothing and returns false.
     *
     * What type of files this is able to uncompress will depend on the
     * kdelibs installed. Generally it will work for .gz and .bz2
     */
    bool uncompressFile();
    void openPSFile(const QString &file=QString::null);

protected:
    bool savePages( const QString& saveFileName,
                    const KGV::PageList& pageList );
    
    bool psCopyDoc( const QString& inputFile, const QString& outputFile,
                    const KGV::PageList& pageList );

    bool convertFromPDF( const QString& saveFileName, 
                         unsigned int firstPage, unsigned int lastPage );

protected slots:
    void doOpenFile();
    void openPDFFileContinue( bool pdf2dscResult );

private:
    FILE* _psFile;

    QString _fileName;
    QString _mimetype;

    KGVPart*   _part;

    Format _format;

    KTempFile* _tmpUnzipped;
    KTempFile* _tmpFromPDF;
    KTempFile* _tmpDSC;

    Pdf2dsc* _pdf2dsc;

    QString _interpreterPath;

    bool _isFileOpen;

    KDSC* _dsc;
};


class Pdf2dsc : public QObject
{
    Q_OBJECT
	
public:
    Pdf2dsc( const QString& ghostscriptPath, QObject* parent = 0, const char* name = 0 );
    ~Pdf2dsc();
    
    void run( const QString& pdfName, const QString& dscName );
    void kill();
    
signals:
    void finished( bool result );

protected slots:
    void processExited();

private:
    KProcess* _process;
    QString   _ghostscriptPath;
};


inline KDSC* const KGVDocument::dsc() const
{
    return _dsc;
}

inline bool KGVDocument::isOpen() const
{
    return _isFileOpen;
}


#endif

// vim:sw=4:sts=4:ts=8:sta:tw=78:noet