Subversion Repositories Scribus

Rev

Rev 5076 | Rev 5642 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4430 cbradney 1
/*
2
For general Scribus (>=1.3.2) copyright and licensing information please refer
3
to the COPYING file provided with the program. Following this notice may exist
4
a copyright and/or license notice that predates the release of Scribus 1.3.2
5
for which a new license (GPL+exception) is in place.
6
*/
3487 craig 7
#include "loadsaveplugin.h"
5076 cbradney 8
#include "loadsaveplugin.moc"
3487 craig 9
 
4024 craig 10
QValueList<FileFormat> LoadSavePlugin::formats;
3686 craig 11
 
3487 craig 12
LoadSavePlugin::LoadSavePlugin()
3696 craig 13
        : ScPlugin()
3487 craig 14
{
15
}
16
 
17
LoadSavePlugin::~LoadSavePlugin()
18
{
19
}
3686 craig 20
 
3693 craig 21
// STATIC method - return a list of all existing formats
4024 craig 22
const QValueList<FileFormat> & LoadSavePlugin::supportedFormats()
3693 craig 23
{
24
        return formats;
25
}
26
 
4024 craig 27
const FileFormat * LoadSavePlugin::getFormatById(const int id)
28
{
29
        QValueList<FileFormat>::iterator it(findFormat(id));
30
        if (it == formats.end())
31
                return 0;
32
        else
33
                return &(*it);
34
}
35
 
3696 craig 36
const QStringList LoadSavePlugin::fileDialogLoadFilter()
37
{
38
        return getDialogFilter(true);
39
}
40
 
41
const QStringList LoadSavePlugin::fileDialogSaveFilter()
42
{
43
        return getDialogFilter(false);
44
}
45
 
46
const QStringList LoadSavePlugin::getDialogFilter(bool forLoad)
47
{
4024 craig 48
        QValueList<FileFormat>::const_iterator it(formats.constBegin());
49
        QValueList<FileFormat>::const_iterator itEnd(formats.constEnd());
3696 craig 50
        QStringList filterList;
51
        // We know the list is sorted by id, then priority, so we can just take the
52
        // highest priority entry for each ID, and we can start with the first entry
53
        // in the list.
54
        filterList.append((*it).filter);
55
        unsigned int lastID = (*it).formatId;
56
        ++it;
57
        for ( ; it != itEnd ; ++it )
58
                // Find the next load/save (as appropriate) plugin for the next format type
59
                if ( (forLoad ? (*it).load : (*it).save) && ((*it).formatId > lastID) )
60
                {
61
                        // And add it to the filter list, since we know it's 
62
                        // the highest priority because of the sort order.
63
                        filterList.append((*it).filter);
64
                        lastID = (*it).formatId;
65
                }
66
        filterList.append(tr("All Files (*)"));
67
        return filterList;
68
}
69
 
3686 craig 70
bool LoadSavePlugin::saveFile(const QString & /* fileName */,
4024 craig 71
                                                          const FileFormat & /* fmt */)
3686 craig 72
{
73
        return false;
74
}
75
 
76
bool LoadSavePlugin::loadFile(const QString & /* fileName */,
5540 mrdocs 77
                                                          const FileFormat & /* fmt */,
78
                                                          int /* flags */,
79
                                                          int /* index */)
3686 craig 80
{
81
        return false;
82
}
83
 
5540 mrdocs 84
bool LoadSavePlugin::checkFlags(int flags)
85
{
86
        int numFlags = 0;
87
        // Only one of the following flags must be set:
88
        // lfCreateDoc, lfUseCurrentPage, lfInsertPage
89
        if( flags & lfCreateDoc )
90
                numFlags++;
91
        if( flags & lfUseCurrentPage )
92
                numFlags++;
93
        if( flags & lfInsertPage )
94
                numFlags++;
95
        if( numFlags > 1 )
96
                return false;
97
        return true;
98
}
99
 
4024 craig 100
void LoadSavePlugin::registerFormat(const FileFormat & fmt)
3686 craig 101
{
3696 craig 102
        // We insert the format in a very specific location so that the formats
103
        // list is sorted by ascending id, then descending priority.
104
        // We first look for entries with equal or greater ID, then equal or
105
        // lesser priority, and insert before the first item that is of either:
106
        //     - Equal ID and lesser or equal priority; or
107
        //     - Greater ID
108
        // If we don't find one, we insert before the end iterator, ie append.
4024 craig 109
        QValueList<FileFormat>::iterator it(formats.begin());
110
        QValueList<FileFormat>::iterator itEnd(formats.end());
3696 craig 111
        while (it != itEnd)
112
        {
113
                if ( ( ((*it).formatId == fmt.formatId) && ((*it).priority <= fmt.priority) ) ||
114
                         ((*it).formatId > fmt.formatId))
115
                                break;
116
                ++it;
117
        }
118
        formats.insert(it, fmt);
119
        //printFormatList(); // DEBUG
3686 craig 120
}
121
 
3696 craig 122
// static debugging function - prints the human readable format list
123
void LoadSavePlugin::printFormatList()
3686 craig 124
{
3696 craig 125
        qDebug("Current format list:");
4024 craig 126
        QValueList<FileFormat>::const_iterator it(formats.constBegin());
127
        QValueList<FileFormat>::const_iterator itEnd(formats.constEnd());
3696 craig 128
        for ( ; it != itEnd ; ++it )
129
        {
130
                qDebug("    Format: Id: %3u, Prio: %3hu, Name: %s",
131
                                (*it).formatId, (*it).priority, (*it).trName.local8Bit().data() );
132
        }
133
        qDebug("Done");
134
}
135
 
136
void LoadSavePlugin::unregisterFormat(unsigned int id)
137
{
4024 craig 138
        QValueList<FileFormat>::iterator it(findFormat(id, this));
3686 craig 139
        Q_ASSERT(it != formats.end());
140
        formats.remove(it);
141
}
142
 
143
void LoadSavePlugin::unregisterAll()
144
{
4024 craig 145
        QValueList<FileFormat>::iterator it(formats.begin());
146
        QValueList<FileFormat>::iterator itEnd(formats.end());
3693 craig 147
        while (it != itEnd)
3686 craig 148
        {
149
                if ((*it).plug == this)
3693 craig 150
                        it = formats.remove(it);
151
                else
152
                        ++it;
3686 craig 153
        }
154
}
155
 
4024 craig 156
QValueList<FileFormat>::iterator
3696 craig 157
LoadSavePlugin::findFormat(unsigned int id,
3686 craig 158
                                                   LoadSavePlugin* plug,
4024 craig 159
                                                   QValueList<FileFormat>::iterator it)
3686 craig 160
{
4024 craig 161
        QValueList<FileFormat>::iterator itEnd(formats.end());
3686 craig 162
        for ( ; it != itEnd ; ++it )
163
        {
3696 craig 164
                if (
165
                                ((*it).formatId == id) &&
166
                                ((plug == 0) || (plug == (*it).plug))
167
                        )
3686 craig 168
                        return it;
169
        }
170
        return itEnd;
171
}
4024 craig 172
 
173
 
174
 
5540 mrdocs 175
bool FileFormat::loadFile(const QString & fileName, int flags, int index) const
4024 craig 176
{
5540 mrdocs 177
        return (plug && load) ? plug->loadFile(fileName, *this, flags, index) : false;
4024 craig 178
}
179
 
180
bool FileFormat::saveFile(const QString & fileName) const
181
{
182
        return (plug && save) ? plug->saveFile(fileName, *this) : false;
183
}