Subversion Repositories Scribus

Rev

Rev 4430 | Rev 5540 | 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 */,
4024 craig 77
                                                          const FileFormat & /* fmt */)
3686 craig 78
{
79
        return false;
80
}
81
 
4024 craig 82
void LoadSavePlugin::registerFormat(const FileFormat & fmt)
3686 craig 83
{
3696 craig 84
        // We insert the format in a very specific location so that the formats
85
        // list is sorted by ascending id, then descending priority.
86
        // We first look for entries with equal or greater ID, then equal or
87
        // lesser priority, and insert before the first item that is of either:
88
        //     - Equal ID and lesser or equal priority; or
89
        //     - Greater ID
90
        // If we don't find one, we insert before the end iterator, ie append.
4024 craig 91
        QValueList<FileFormat>::iterator it(formats.begin());
92
        QValueList<FileFormat>::iterator itEnd(formats.end());
3696 craig 93
        while (it != itEnd)
94
        {
95
                if ( ( ((*it).formatId == fmt.formatId) && ((*it).priority <= fmt.priority) ) ||
96
                         ((*it).formatId > fmt.formatId))
97
                                break;
98
                ++it;
99
        }
100
        formats.insert(it, fmt);
101
        //printFormatList(); // DEBUG
3686 craig 102
}
103
 
3696 craig 104
// static debugging function - prints the human readable format list
105
void LoadSavePlugin::printFormatList()
3686 craig 106
{
3696 craig 107
        qDebug("Current format list:");
4024 craig 108
        QValueList<FileFormat>::const_iterator it(formats.constBegin());
109
        QValueList<FileFormat>::const_iterator itEnd(formats.constEnd());
3696 craig 110
        for ( ; it != itEnd ; ++it )
111
        {
112
                qDebug("    Format: Id: %3u, Prio: %3hu, Name: %s",
113
                                (*it).formatId, (*it).priority, (*it).trName.local8Bit().data() );
114
        }
115
        qDebug("Done");
116
}
117
 
118
void LoadSavePlugin::unregisterFormat(unsigned int id)
119
{
4024 craig 120
        QValueList<FileFormat>::iterator it(findFormat(id, this));
3686 craig 121
        Q_ASSERT(it != formats.end());
122
        formats.remove(it);
123
}
124
 
125
void LoadSavePlugin::unregisterAll()
126
{
4024 craig 127
        QValueList<FileFormat>::iterator it(formats.begin());
128
        QValueList<FileFormat>::iterator itEnd(formats.end());
3693 craig 129
        while (it != itEnd)
3686 craig 130
        {
131
                if ((*it).plug == this)
3693 craig 132
                        it = formats.remove(it);
133
                else
134
                        ++it;
3686 craig 135
        }
136
}
137
 
4024 craig 138
QValueList<FileFormat>::iterator
3696 craig 139
LoadSavePlugin::findFormat(unsigned int id,
3686 craig 140
                                                   LoadSavePlugin* plug,
4024 craig 141
                                                   QValueList<FileFormat>::iterator it)
3686 craig 142
{
4024 craig 143
        QValueList<FileFormat>::iterator itEnd(formats.end());
3686 craig 144
        for ( ; it != itEnd ; ++it )
145
        {
3696 craig 146
                if (
147
                                ((*it).formatId == id) &&
148
                                ((plug == 0) || (plug == (*it).plug))
149
                        )
3686 craig 150
                        return it;
151
        }
152
        return itEnd;
153
}
4024 craig 154
 
155
 
156
 
157
bool FileFormat::loadFile(const QString & fileName) const
158
{
159
        return (plug && load) ? plug->loadFile(fileName, *this) : false;
160
}
161
 
162
bool FileFormat::saveFile(const QString & fileName) const
163
{
164
        return (plug && save) ? plug->saveFile(fileName, *this) : false;
165
}