~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to libs/odf/KoOdfLoadingContext.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2010-10-27 17:52:57 UTC
  • mfrom: (0.12.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20101027175257-s04zqqk5bs8ckm9o
Tags: 1:2.2.83-0ubuntu1
* Merge with Debian git remaining changes:
 - Add build-deps on librcps-dev, opengtl-dev, libqtgtl-dev, freetds-dev,
   create-resources, libspnav-dev
 - Remove needless build-dep on libwv2-dev
 - koffice-libs recommends create-resources
 - krita recommends pstoedit
 - Keep our patches
* New upstream release 2.3 beta 3
  - Remove debian/patches fixed by upstream
  - Update install files

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
 
30
30
class KoOdfLoadingContext::Private
31
31
{
 
32
public:
 
33
    Private(KoOdfStylesReader &sr, KoStore *s)
 
34
        : store(s),
 
35
        stylesReader(sr),
 
36
        generatorType(KoOdfLoadingContext::Unknown),
 
37
        metaXmlParsed(false),
 
38
        useStylesAutoStyles(false)
 
39
    {
 
40
    }
 
41
 
 
42
    KoStore *store;
 
43
    KoOdfStylesReader &stylesReader;
 
44
    KoStyleStack styleStack;
 
45
 
 
46
    mutable QString generator;
 
47
    GeneratorType generatorType;
 
48
    mutable bool metaXmlParsed;
 
49
    bool useStylesAutoStyles;
 
50
 
 
51
    KoXmlDocument manifestDoc;
 
52
 
 
53
    KoOdfStylesReader defaultStylesReader;
 
54
    KoXmlDocument doc; // the doc needs to be kept around so it is possible to access the styles
32
55
};
33
56
 
34
 
KoOdfLoadingContext::KoOdfLoadingContext(KoOdfStylesReader& stylesReader, KoStore* store, const KComponentData & componentData)
35
 
        : m_store(store)
36
 
        , m_stylesReader(stylesReader)
37
 
        , m_metaXmlParsed(false)
38
 
        , m_useStylesAutoStyles(false)
39
 
        , d(0)
 
57
KoOdfLoadingContext::KoOdfLoadingContext(KoOdfStylesReader &stylesReader, KoStore* store, const KComponentData &componentData)
 
58
        : d(new Private(stylesReader, store))
40
59
{
41
60
    // Ideally this should be done by KoDocument and passed as argument here...
42
61
    KoOdfReadStore oasisStore(store);
43
62
    QString dummy;
44
 
    (void)oasisStore.loadAndParse("tar:/META-INF/manifest.xml", m_manifestDoc, dummy);
 
63
    (void)oasisStore.loadAndParse("tar:/META-INF/manifest.xml", d->manifestDoc, dummy);
45
64
 
46
65
    if (componentData.isValid()) {
47
66
        QString fileName( KStandardDirs::locate( "styles", "defaultstyles.xml", componentData ) );
48
67
        if ( ! fileName.isEmpty() ) {
49
68
            QFile file( fileName );
50
69
            QString errorMessage;
51
 
            if ( KoOdfReadStore::loadAndParse( &file, m_doc, errorMessage, fileName ) ) {
52
 
                m_defaultStylesReader.createStyleMap( m_doc, true );
 
70
            if ( KoOdfReadStore::loadAndParse( &file, d->doc, errorMessage, fileName ) ) {
 
71
                d->defaultStylesReader.createStyleMap( d->doc, true );
53
72
            }
54
73
            else {
55
74
                kWarning(30010) << "reading of defaultstyles.xml failed:" << errorMessage;
67
86
}
68
87
 
69
88
void KoOdfLoadingContext::setManifestFile(const QString& fileName) {
70
 
    KoOdfReadStore oasisStore(m_store);
 
89
    KoOdfReadStore oasisStore(d->store);
71
90
    QString dummy;
72
 
    (void)oasisStore.loadAndParse(fileName, m_manifestDoc, dummy);
 
91
    (void)oasisStore.loadAndParse(fileName, d->manifestDoc, dummy);
73
92
}
74
93
 
75
94
void KoOdfLoadingContext::fillStyleStack(const KoXmlElement& object, const char* nsURI, const char* attrName, const char* family)
77
96
    // find all styles associated with an object and push them on the stack
78
97
    if (object.hasAttributeNS(nsURI, attrName)) {
79
98
        const QString styleName = object.attributeNS(nsURI, attrName, QString());
80
 
        const KoXmlElement * style = m_stylesReader.findStyle(styleName, family, m_useStylesAutoStyles);
 
99
        const KoXmlElement * style = d->stylesReader.findStyle(styleName, family, d->useStylesAutoStyles);
81
100
 
82
101
        if (style)
83
 
            addStyles(style, family, m_useStylesAutoStyles);
 
102
            addStyles(style, family, d->useStylesAutoStyles);
84
103
        else
85
 
            kWarning(32500) << "style" << styleName << "not found in" << (m_useStylesAutoStyles ? "styles.xml" : "content.xml");
 
104
            kWarning(32500) << "style" << styleName << "not found in" << (d->useStylesAutoStyles ? "styles.xml" : "content.xml");
86
105
    }
87
106
}
88
107
 
94
113
    // this recursive function is necessary as parent styles can have parents themselves
95
114
    if (style->hasAttributeNS(KoXmlNS::style, "parent-style-name")) {
96
115
        const QString parentStyleName = style->attributeNS(KoXmlNS::style, "parent-style-name", QString());
97
 
        const KoXmlElement* parentStyle = m_stylesReader.findStyle(parentStyleName, family, usingStylesAutoStyles);
 
116
        const KoXmlElement* parentStyle = d->stylesReader.findStyle(parentStyleName, family, usingStylesAutoStyles);
98
117
 
99
118
        if (parentStyle)
100
119
            addStyles(parentStyle, family, usingStylesAutoStyles);
102
121
            kWarning(32500) << "Parent style not found: " << family << parentStyleName << usingStylesAutoStyles;
103
122
            //we are handling a non compliant odf file. let's at the very least load the application default, and the eventual odf default
104
123
            if (family) {
105
 
                const KoXmlElement* def = m_stylesReader.defaultStyle(family);
 
124
                const KoXmlElement* def = d->stylesReader.defaultStyle(family);
106
125
                if (def) {   // then, the default style for this family
107
 
                    m_styleStack.push(*def);
 
126
                    d->styleStack.push(*def);
108
127
                }
109
128
            }
110
129
        }
111
130
    } else if (family) {
112
 
        const KoXmlElement* def = m_stylesReader.defaultStyle(family);
 
131
        const KoXmlElement* def = d->stylesReader.defaultStyle(family);
113
132
        if (def) {   // then, the default style for this family
114
 
            m_styleStack.push(*def);
 
133
            d->styleStack.push(*def);
115
134
        }
116
135
    }
117
136
 
118
137
    //kDebug(32500) <<"pushing style" << style->attributeNS( KoXmlNS::style,"name", QString() );
119
 
    m_styleStack.push(*style);
 
138
    d->styleStack.push(*style);
 
139
}
 
140
 
 
141
void KoOdfLoadingContext::parseGenerator() const
 
142
{
 
143
    // Regardless of whether we cd into the parent directory
 
144
    // or not to find a meta.xml, restore the directory that
 
145
    // we were in afterwards.
 
146
    d->store->pushDirectory();
 
147
 
 
148
    // Some embedded documents to not contain their own meta.xml
 
149
    // Use the parent directory's instead.
 
150
    if (!d->store->hasFile("meta.xml"))
 
151
        // Only has an effect if there is a parent directory
 
152
        d->store->leaveDirectory();
 
153
 
 
154
    if (d->store->hasFile("meta.xml")) {
 
155
        KoXmlDocument metaDoc;
 
156
        KoOdfReadStore oasisStore(d->store);
 
157
        QString errorMsg;
 
158
        if (oasisStore.loadAndParse("meta.xml", metaDoc, errorMsg)) {
 
159
            KoXmlNode meta   = KoXml::namedItemNS(metaDoc, KoXmlNS::office, "document-meta");
 
160
            KoXmlNode office = KoXml::namedItemNS(meta, KoXmlNS::office, "meta");
 
161
            KoXmlElement generator = KoXml::namedItemNS(office, KoXmlNS::meta, "generator");
 
162
            if (!generator.isNull()) {
 
163
                d->generator = generator.text();
 
164
                if (d->generator.startsWith("KOffice")) {
 
165
                    d->generatorType = KOffice;
 
166
                } else if (d->generator.startsWith("OpenOffice.org")) {
 
167
                    d->generatorType = OpenOffice;
 
168
                } else if (d->generator.startsWith("MicrosoftOffice")) {
 
169
                    d->generatorType = MicrosoftOffice;
 
170
                }
 
171
            }
 
172
        }
 
173
    }
 
174
    d->metaXmlParsed = true;
 
175
 
 
176
    d->store->popDirectory();
120
177
}
121
178
 
122
179
QString KoOdfLoadingContext::generator() const
123
180
{
124
 
    parseMeta();
125
 
    return m_generator;
126
 
}
127
 
 
128
 
void KoOdfLoadingContext::parseMeta() const
129
 
{
130
 
    if (!m_metaXmlParsed && m_store) {
131
 
        // Regardless of whether we cd into the parent directory
132
 
        // or not to find a meta.xml, restore the directory that
133
 
        // we were in afterwards.
134
 
        m_store->pushDirectory();
135
 
 
136
 
        // Some embedded documents to not contain their own meta.xml
137
 
        // Use the parent directory's instead.
138
 
        if (!m_store->hasFile("meta.xml"))
139
 
            // Only has an effect if there is a parent directory
140
 
            m_store->leaveDirectory();
141
 
 
142
 
        if (m_store->hasFile("meta.xml")) {
143
 
            KoXmlDocument metaDoc;
144
 
            KoOdfReadStore oasisStore(m_store);
145
 
            QString errorMsg;
146
 
            if (oasisStore.loadAndParse("meta.xml", metaDoc, errorMsg)) {
147
 
                KoXmlNode meta   = KoXml::namedItemNS(metaDoc, KoXmlNS::office, "document-meta");
148
 
                KoXmlNode office = KoXml::namedItemNS(meta, KoXmlNS::office, "meta");
149
 
                KoXmlElement generator = KoXml::namedItemNS(office, KoXmlNS::meta, "generator");
150
 
                if (!generator.isNull())
151
 
                    m_generator = generator.text();
152
 
            }
153
 
        }
154
 
        m_metaXmlParsed = true;
155
 
 
156
 
        m_store->popDirectory();
157
 
    }
158
 
}
 
181
    if (!d->metaXmlParsed && d->store) {
 
182
        parseGenerator();
 
183
    }
 
184
    return d->generator;
 
185
}
 
186
 
 
187
KoOdfLoadingContext::GeneratorType KoOdfLoadingContext::generatorType() const
 
188
{
 
189
    if (!d->metaXmlParsed && d->store) {
 
190
        parseGenerator();
 
191
    }
 
192
    return d->generatorType;
 
193
}
 
194
 
 
195
KoStore *KoOdfLoadingContext::store() const
 
196
{
 
197
    return d->store;
 
198
}
 
199
 
 
200
KoOdfStylesReader &KoOdfLoadingContext::stylesReader()
 
201
{
 
202
    return d->stylesReader;
 
203
}
 
204
 
 
205
/**
 
206
* Get the application default styles styleReader
 
207
*/
 
208
KoOdfStylesReader &KoOdfLoadingContext::defaultStylesReader()
 
209
{
 
210
    return d->defaultStylesReader;
 
211
}
 
212
 
 
213
KoStyleStack &KoOdfLoadingContext::styleStack() const
 
214
{
 
215
    return d->styleStack;
 
216
}
 
217
 
 
218
const KoXmlDocument &KoOdfLoadingContext::manifestDocument() const
 
219
{
 
220
    return d->manifestDoc;
 
221
}
 
222
 
 
223
void KoOdfLoadingContext::setUseStylesAutoStyles(bool useStylesAutoStyles)
 
224
{
 
225
    d->useStylesAutoStyles = useStylesAutoStyles;
 
226
}
 
227
 
 
228
bool KoOdfLoadingContext::useStylesAutoStyles() const
 
229
{
 
230
    return d->useStylesAutoStyles;
 
231
}
 
232