Subversion Repositories Scribus

Rev

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