Subversion Repositories Scribus

Rev

Rev 512 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
506 fschmid 1
#include "fileloader.h"
2
#include <qfile.h>
3
#include <qfileinfo.h>
4
#include "scribus.h"
5
#include "scribusXml.h"
6
#if (_MSC_VER >= 1200)
7
        #include "win-config.h"
8
#else
9
        #include "config.h"
10
#endif
11
#ifdef HAVE_LIBZ
12
        #include <zlib.h>
13
#endif
14
extern bool loadText(QString nam, QString *Buffer);
15
 
16
/*!
17
 \fn FileLoader::FileLoader(QString fileName)
18
 \author Franz Schmid
19
 \date
20
 \brief Constructor, sets the variable "FileName" to the input parameter fileName
21
 \param QString fileName
22
 \retval None
23
 */
24
FileLoader::FileLoader(QString fileName)
25
{
26
        FileName = fileName;
27
        FileType = -1;
28
}
29
 
30
/*!
31
 \fn int FileLoader::TestFile()
32
 \author Franz Schmid
33
 \date
34
 \brief Tests if the file "FileName" exists and determines the type of the file.
35
 \param None
36
 \retval int -1 if the file doesn't exist or any other error has occured, 0 for the old Format, 1 for the new Format, 2 for EPS and PS files, 3 for SVG files and 4 for PDF files
37
 */
38
int FileLoader::TestFile()
39
{
40
        QFileInfo fi = QFileInfo(FileName);
41
        int ret = -1;
42
        if (!fi.exists())
43
                ret = -1;
44
        QString ext = fi.extension(true).lower();
45
        if ((ext == "sla.gz") || (ext == "sla") || (ext == "scd.gz") || (ext == "scd"))
46
                ret = CheckScribus();
47
        if ((ext == "ps") || (ext == "eps"))
48
                ret = 2;
49
        if ((ext == "svg") || (ext == "svgz"))
50
                ret = 3;
51
/*      if (ext == "pdf")
52
                ret = 4; */
53
        FileType = ret;
54
        return ret;
55
}
56
 
57
/*!
58
 \fn int FileLoader::CheckScribus()
59
 \author Franz Schmid
60
 \date
61
 \brief Checks which Scribus fileformat the File in FileName has.
62
 \param None
63
 \retval 0 for old Scribus files, 1 for Scribus files created by Scribus 1.3 and above
64
 */
65
int FileLoader::CheckScribus()
66
{
67
        int ret = -1;
68
        QString fText = ReadDatei(FileName);
69
        if (fText == "")
70
                return ret;
71
        if ((fText.startsWith("<SCRIBUS")) && (fText.contains("<PAGE ", TRUE) != 0))
72
                return 0;
73
        if ((fText.startsWith("<SCRIBUSUTF8NEW")) && (fText.contains("<PAGE ", TRUE) != 0))
74
                return 1;
75
        return ret;
76
}
77
 
78
/*!
79
 \fn QString FileLoader::ReadDatei(QString fileName)
80
 \author Franz Schmid
81
 \date
82
 \brief Auxiliary function loads the file "fileName" into a QString and does conversion from UTF8 if required
83
 \param QString fileName
84
 \retval QString Contents of the file, converted from UTF8
85
 */
86
QString FileLoader::ReadDatei(QString fileName)
87
{
88
/**
89
  * added to support gz docs
90
  * 2.7.2002 C.Toepp
91
  * <c.toepp@gmx.de>
92
  */
93
        QString f = "";
94
#ifdef HAVE_LIBZ
95
        if(fileName.right(2) == "gz")
96
        {
97
                gzFile gzDoc;
98
                char buff[4097];
99
                int i;
100
                gzDoc = gzopen(fileName.latin1(),"rb");
101
                if(gzDoc == NULL)
102
                        return "";
103
                while((i = gzread(gzDoc,&buff,4096)) > 0)
104
                {
105
                        buff[i] = '\0';
106
                        f.append(buff);
107
                }
108
                gzclose(gzDoc);
109
        }
110
        else
111
                loadText(fileName, &f);
112
#else
113
        loadText(fileName, &f);
114
#endif
115
        QString ff = "";
116
        if (f.startsWith("<SCRIBUSUTF8"))
117
                ff = QString::fromUtf8(f);
118
        else if (f.startsWith("<SCRIBUS"))
119
                ff = f;
120
        return ff;
121
/** end changes */
122
}
123
 
124
/*!
125
 \fn bool FileLoader::LoadFile(ScribusApp* app)
126
 \author Franz Schmid
127
 \date
128
 \brief Loads the file "FileName" as a Scribus document
129
 \param ScribusApp* app, pointer to the main application class
130
 \retval bool true when loading is succsessful, false otherwise
131
 */
132
bool FileLoader::LoadFile(ScribusApp* app)
133
{
134
        bool ret = false;
135
        switch (FileType)
136
        {
137
                case 0:
138
                        {
139
                                ScriXmlDoc *ss = new ScriXmlDoc();
140
                                QObject::connect(ss, SIGNAL(NewPage(int)), app, SLOT(slotNewPage(int)));
141
                                ret = ss->ReadDoc(FileName, app->Prefs.AvailFonts, app->doc, app->view, app->FProg);
142
                                QObject::disconnect(ss, SIGNAL(NewPage(int)), app, SLOT(slotNewPage(int)));
143
                                delete ss;
144
                        }
145
                        break;
146
                case 2:
147
                        app->DLLinput = FileName;
148
                        app->CallDLL( QObject::tr("Import &EPS/PS...") );
149
                        ret = true;
150
                        break;
151
                case 3:
152
                        app->DLLinput = FileName;
153
                        app->CallDLL( QObject::tr("Import &SVG...") );
154
                        ret = true;
155
                        break;
156
                default:
157
                        ret = false;
158
                        break;
159
        }
160
        return ret;
161
}