Subversion Repositories Scribus

Rev

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

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