summaryrefslogtreecommitdiffstats
path: root/lib/kofficecore/KoPictureCollection.cpp
blob: 3d680bf731709bc1ce8c908e8cb75894b4deaa37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* This file is part of the KDE project
   Copyright (C) 1998, 1999 Reginald Stadlbauer <[email protected]>
   Copyright (c) 2001, 2004 David Faure <[email protected]>
   Copyright (C) 2002, 2004 Nicolas GOUTTE <[email protected]>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include <tqdom.h>

#include <kdebug.h>
#include <kurl.h>

#include <KoStoreDevice.h>
#include <KoXmlWriter.h>

#include "KoPicture.h"
#include "KoPictureCollection.h"

//#define DEBUG_PICTURES

KoPicture KoPictureCollection::findPicture(const KoPictureKey& key) const
{
#ifdef DEBUG_PICTURES
    kdDebug(30003) << "KoPictureCollection::findPicture " << key.toString() << endl;
#endif
    ConstIterator it = tqfind( key );
    if ( it == end() )
    {
        KoPicture picture;
        picture.setKey(key);
        return picture;
    }

    return *it;
}


KoPicture KoPictureCollection::insertPicture(const KoPictureKey& key, const KoPicture& picture)
{
#ifdef DEBUG_PICTURES
    kdDebug(30003) << "KoPictureCollection::insertPicture " << key.toString() << endl;
#endif
    KoPicture c = findPicture(key);
    if (c.isNull())
    {
#ifdef DEBUG_PICTURES
        kdDebug(30003) << "KoPictureCollection::insertPicture not found -> inserting" << endl;
#endif
        c=picture;
        c.setKey(key); // Be sure that the key is correctly set in the KoPicture!
        insert(key, c);
    }
    return c;
}

KoPicture KoPictureCollection::insertPicture(const KoPicture& picture)
{
    return insertPicture(picture.getKey(), picture);
}

KoPicture KoPictureCollection::downloadPicture(const KURL& url, TQWidget *window)
{
#ifdef DEBUG_PICTURES
    kdDebug(30003) << "KoPictureCollection::downloadPicture " << url.prettyURL() << endl;
#endif

    // If it is a local file, we can check the last modification date, so we should better use loadPicture
    if (url.isLocalFile())
        return loadPicture(url.path());


    // We have really a remote file, so we cannot check the last modification date
    // Therefore we have to always download the file

    KoPicture pic;
#ifdef DEBUG_PICTURES
    kdDebug(30003) << "Trying to download picture from file " << url.prettyURL() << endl;
#endif

    if (pic.setKeyAndDownloadPicture(url, window))
        insertPicture(pic.getKey(), pic);
    else
        kdWarning(30003) << "Could not download KoPicture from " << url.prettyURL() << endl;

    return pic;
}

KoPicture KoPictureCollection::loadPicture(const TQString& fileName)
{
#ifdef DEBUG_PICTURES
    kdDebug(30003) << "KoPictureCollection::loadPicture " << fileName << endl;
#endif
    // Check the last modified date of the file, as it is now.
    KoPictureKey key;
    key.setKeyFromFile(fileName);

    KoPicture c = findPicture(key);
    if (c.isNull())
    {
#ifdef DEBUG_PICTURES
        kdDebug(30003) << "Trying to load picture from file " << fileName << endl;
#endif
        if (c.loadFromFile(fileName))
            insertPicture(key, c);
        else
            kdWarning(30003) << "Could not load KoPicture from " << fileName << endl;
    }
    return c;
}

TQString KoPictureCollection::getFileName(const Type pictureType, KoPicture& picture, int& counter)
{
    TQString storeURL;
    // ### TODO: remove "cliparts" when KPresenter is ready for it
    if (pictureType==CollectionClipart)
        storeURL="cliparts/clipart";
    else
        storeURL="pictures/picture";
    storeURL+=TQString::number(++counter);
    storeURL+='.';
    storeURL+=picture.getExtension();
    return storeURL;
}

TQString KoPictureCollection::getOasisFileName(const KoPicture& picture) const
{
    TQString storeURL( "Pictures/");
    if ( !picture.uniquePictureId().isEmpty() )
        storeURL+=picture.uniquePictureId();
    else
        storeURL+=picture.getKey().toString();
    storeURL+='.';
    storeURL+=picture.getExtension();
    return storeURL;
}

bool KoPictureCollection::saveToStore(const Type pictureType, KoStore *store, const TQValueList<KoPictureKey>& keys)
{
    int counter=0;
    TQValueList<KoPictureKey>::const_iterator it = keys.begin();
    for ( ; it != keys.end(); ++it )
    {
        KoPicture c = findPicture( *it );
        if (c.isNull())
            kdWarning(30003) << "Picture " << (*it).toString() << " not found in collection !" << endl;
        else
        {
            TQString storeURL=getFileName(pictureType, c, counter);

            if (store->open(storeURL))
            {
                KoStoreDevice dev(store);
                if ( ! c.save(&dev) )
                    return false; // e.g. bad image?
                if ( !store->close() )
                    return false; // e.g. disk full
            }
        }
    }
    return true;
}

bool KoPictureCollection::saveOasisToStore( KoStore *store, TQValueList<KoPictureKey> keys, KoXmlWriter* manifestWriter )
{
    TQValueList<KoPictureKey>::Iterator it = keys.begin();
    for ( ; it != keys.end(); ++it )
    {
        KoPicture c = findPicture( *it );
        if (c.isNull())
            kdWarning(30003) << "Picture " << (*it).toString() << " not found in collection !" << endl;
        else
        {
            TQString storeURL( getOasisFileName(c) );
            if (store->open(storeURL))
            {
                KoStoreDevice dev(store);
                if ( ! c.save(&dev) )
                    return false; // e.g. bad image?
                if ( !store->close() )
                    return false; // e.g. disk full
                manifestWriter->addManifestEntry( storeURL, c.getMimeType() );
            }
        }
    }
    return true;
}

TQDomElement KoPictureCollection::saveXML(const Type pictureType, TQDomDocument &doc, TQValueList<KoPictureKey> keys)
{
    TQString strElementName("PICTURES");
    if (pictureType==CollectionImage)
        strElementName="PIXMAPS";
    else if (pictureType==CollectionClipart)
        strElementName="CLIPARTS";
    TQDomElement cliparts = doc.createElement( strElementName );
    int counter=0;
    TQValueList<KoPictureKey>::Iterator it = keys.begin();
    for ( ; it != keys.end(); ++it )
    {
        KoPicture picture = findPicture( *it );
        if ( picture.isNull() )
            kdWarning(30003) << "Picture " << (*it).toString() << " not found in collection !" << endl;
        else
        {
            TQString pictureName=getFileName(pictureType, picture, counter);
            TQDomElement keyElem = doc.createElement( "KEY" );
            cliparts.appendChild(keyElem);
            (*it).saveAttributes(keyElem);
            keyElem.setAttribute("name", pictureName);
        }
    }
    return cliparts;
}

void KoPictureCollection::readXML( TQDomElement& pixmapsElem, TQMap <KoPictureKey, TQString>& map )
{
    for( TQDomNode n = pixmapsElem.firstChild();
         !n.isNull();
         n = n.nextSibling() )
    {
        TQDomElement keyElement = n.toElement();
        if (keyElement.isNull()) continue;
        if (keyElement.tagName()=="KEY")
        {
            KoPictureKey key;
            key.loadAttributes(keyElement);
            map.insert(key, keyElement.attribute("name"));
        }
    }
}


KoPictureCollection::StoreMap KoPictureCollection::readXML( TQDomElement& pixmapsElem )
{
    StoreMap map;
    readXML(pixmapsElem, map);
    return map;
}

void KoPictureCollection::readFromStore( KoStore * store, const StoreMap & storeMap )
{
#ifdef DEBUG_PICTURES
    kdDebug(30003) << "KoPictureCollection::readFromStore " << store << endl;
#endif
    StoreMap::ConstIterator it = storeMap.begin();
    for ( ; it != storeMap.end(); ++it )
    {
        KoPicture c = findPicture(it.key());
        if (!c.isNull())
        {
            // Do not load a file that we already have!
            //kdDebug(30003) << store << endl;
            continue;
        }
        TQString u(it.data());
        if (u.isEmpty())
        {
            // old naming I suppose ?
            u=it.key().toString();
        }

        KoPicture picture;

        if (!store->open( u ))
        {
            u.prepend( "file:" );
            if (!store->open( u ))
            {
                kdWarning(30003) << "Could load neither from store nor from file: " << it.data() << endl;
                return;
            }
        }

        const int pos=u.tqfindRev('.');
        if (pos==-1)
        {
            kdError(30003) << "File with no extension! Not supported!" << endl;
            return;
        }
        const TQString extension(u.mid(pos+1));

        KoStoreDevice dev(store);
        picture.load(&dev, extension);
        store->close();

        if (!picture.isNull())
            insertPicture(it.key(), picture);
    }
}

KoPicture KoPictureCollection::findOrLoad(const TQString& fileName, const TQDateTime& dateTime)
{
    // As now all KoPictureKey objects have a valid TQDateTime, we must do it without a date/time check.
    ConstIterator it = tqfind( KoPictureKey ( fileName, dateTime ) );
    if ( it == end() )
    {
        return loadPicture( fileName );
    }
    return *it;
}

void KoPictureCollection::assignUniqueIds()
{
    uint idx = 0;
    Iterator it;
    for ( it = begin(); it != end(); ++it, ++idx )
    {
        it.data().assignPictureId(idx);
    }
}