summaryrefslogtreecommitdiffstats
path: root/lib/kofficecore/KoDocumentChild.cpp
blob: 0767ab0498e9f58163d45346f4cb3982de8a85a7 (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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/* This file is part of the KDE project
   Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
   Copyright (C) 2004-2006 David Faure <faure@kde.org>

   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 "KoDocumentChild.h"
#include "KoOasisStore.h"
#include <KoDocument.h>
#include <KoQueryTrader.h>
#include <KoXmlWriter.h>
#include <KoXmlNS.h>
#include <KoUnit.h>
#include <KoStore.h>
#include <KoStoreDevice.h>

#include <kparts/partmanager.h>

#include <kmimetype.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kdebug.h>

#include <tqapplication.h>

#include <assert.h>

// Define the protocol used here for embedded documents' URL
// This used to "store" but KURL didn't like it,
// so let's simply make it "tar" !
#define STORE_PROTOCOL "tar"
#define INTERNAL_PROTOCOL "intern"
// Warning, keep it sync in koStore.cc and koDocument.cc

/**********************************************************
 *
 * KoDocumentChild
 *
 **********************************************************/

class KoDocumentChildPrivate
{
public:
  KoDocumentChildPrivate()
  {
  }
  ~KoDocumentChildPrivate()
  {
  }

  KoDocument *m_parent;
  KoDocument *m_doc;
  bool m_deleted;
};

KoDocumentChild::KoDocumentChild( KoDocument* parent, KoDocument* doc, const TQRect& tqgeometry )
    : KoChild( parent )
{
  d = new KoDocumentChildPrivate;
  d->m_parent = parent;
  d->m_doc = doc;
  setGeometry( tqgeometry );
  d->m_deleted = false;
  if ( doc )
    doc->setStoreInternal( !doc->hasExternURL() );
}

KoDocumentChild::KoDocumentChild( KoDocument* parent )
    : KoChild( parent )
{
  d = new KoDocumentChildPrivate;
  d->m_parent = parent;
  d->m_doc = 0L;
  d->m_deleted = false;
}

void KoDocumentChild::setDocument( KoDocument *doc, const TQRect &tqgeometry )
{
  kdDebug()<<k_funcinfo<<"doc: "<<doc->url().url()<<endl;
  d->m_doc = doc;
  setGeometry( tqgeometry );

  updateMatrix();
}

KoDocument *KoDocumentChild::document() const
{
  return d->m_doc;
}

KoDocument* KoDocumentChild::parentDocument() const
{
  return d->m_parent;
}

KoDocument* KoDocumentChild::hitTest( const TQPoint &p, const TQWMatrix &_matrix )
{
  if ( !region( _matrix ).contains( p ) || !d->m_doc )
    return 0L;

  TQWMatrix m( _matrix );
  m = matrix() * m;
  m.scale( xScaling(), yScaling() );

  return d->m_doc->hitTest( p, m );
}

void KoDocumentChild::loadOasis( const TQDomElement &frameElement, const TQDomElement& objectElement )
{
    double x, y, w, h;
    x = KoUnit::parseValue( frameElement.attributeNS( KoXmlNS::svg, "x", TQString() ) );
    y = KoUnit::parseValue( frameElement.attributeNS( KoXmlNS::svg, "y", TQString() ) );
    w = KoUnit::parseValue( frameElement.attributeNS( KoXmlNS::svg, "width", TQString() ) );
    h = KoUnit::parseValue( frameElement.attributeNS( KoXmlNS::svg, "height", TQString() ) );
    m_tmpGeometry = TQRect((int)x, (int)y, (int)w, (int)h); // #### double->int conversion
    setGeometry(m_tmpGeometry);

    TQString url = objectElement.attributeNS( KoXmlNS::xlink, "href", TQString() );
    if ( url[0] == '#' )
        url = url.mid( 1 );
    if ( url.startsWith( "./" ) )
        m_tmpURL = TQString( INTERNAL_PROTOCOL ) + ":/" + url.mid( 2 );
    else
        m_tmpURL = url;
    kdDebug() << k_funcinfo << m_tmpURL << endl;
}


bool KoDocumentChild::load( const TQDomElement& element, bool uppercase )
{
    if ( element.hasAttribute( "url" ) )
        m_tmpURL = element.attribute("url");
    if ( element.hasAttribute("mime") )
        m_tmpMimeType = element.attribute("mime");

    if ( m_tmpURL.isEmpty() )
    {
        kdDebug(30003) << "Empty 'url' attribute in OBJECT" << endl;
        return false;
    }
    if ( m_tmpMimeType.isEmpty() )
    {
        kdDebug(30003) << "Empty 'mime' attribute in OBJECT" << endl;
        return false;
    }

    bool brect = FALSE;
    TQDomNode n = element.firstChild();
    for( ; !n.isNull(); n = n.nextSibling() )
    {
        TQDomElement e = n.toElement();
        if ( e.isNull() ) continue;
        if ( e.tagName() == "rect" || ( uppercase && e.tagName() == "RECT" ) )
        {
            brect = true;
            int x, y, w, h;
            x=y=w=h=0;
            if ( e.hasAttribute( "x" ) )
                x = e.attribute( "x" ).toInt(&brect);
            if ( e.hasAttribute( "y" ) )
                y = e.attribute( "y" ).toInt(&brect);
            if ( e.hasAttribute( "w" ) )
                w = e.attribute( "w" ).toInt(&brect);
            if ( e.hasAttribute( "h" ) )
                h = e.attribute( "h" ).toInt(&brect);
            m_tmpGeometry = TQRect(x, y, w, h);
            setGeometry(m_tmpGeometry);
        }
    }

    if ( !brect )
    {
        kdDebug(30003) << "Missing RECT in OBJECT" << endl;
        return false;
    }

    return true;
}

bool KoDocumentChild::loadDocument( KoStore* store )
{
    assert( !m_tmpURL.isEmpty() );

    kdDebug(30003) << "KoDocumentChild::loadDocument: trying to load " << m_tmpURL << endl;

    // Backwards compatibility
    if ( m_tmpMimeType == "application/x-killustrator" )
        m_tmpMimeType = "application/x-kontour";

    KoDocumentEntry e = KoDocumentEntry::queryByMimeType( m_tmpMimeType );
    if ( e.isEmpty() )
    {
        kdWarning(30003) << "Could not create child document with type " << m_tmpMimeType << endl;
        bool res = createUnavailDocument( store, true, m_tmpMimeType );
        if ( res )
        {
            // Try to turn the mimetype name into its comment
            TQString mimeName = m_tmpMimeType;
            KMimeType::Ptr mime = KMimeType::mimeType( m_tmpMimeType );
            if ( mime->name() != KMimeType::defaultMimeType() )
                mimeName = mime->comment();
            d->m_doc->setProperty( "unavailReason", i18n( "No handler found for %1" ).tqarg( mimeName ) );
        }
        return res;
    }

    return loadDocumentInternal( store, e, true /*open url*/, false /*not oasis*/ );
}

bool KoDocumentChild::loadOasisDocument( KoStore* store, const TQDomDocument& manifestDoc )
{
    TQString path = m_tmpURL;
    if ( m_tmpURL.startsWith( INTERNAL_PROTOCOL ) ) {
        path = store->currentDirectory();
        if ( !path.isEmpty() )
            path += '/';
        TQString relPath = KURL( m_tmpURL ).path();
        path += relPath.mid( 1 ); // remove leading '/'
    }
    if ( !path.endsWith( "/" ) )
        path += '/';
    const TQString mimeType = KoOasisStore::mimeForPath( manifestDoc, path );
    kdDebug() << k_funcinfo << "path for manifest file=" << path << " mimeType=" << mimeType << endl;
    if ( mimeType.isEmpty() ) {
        kdError(30003) << "Manifest doesn't have media-type for " << path << endl;
        return false;
    }

    KoDocumentEntry e = KoDocumentEntry::queryByMimeType( mimeType );
    if ( e.isEmpty() )
    {
        kdWarning(30003) << "Could not create child document with type " << mimeType << endl;
        bool res = createUnavailDocument( store, true, mimeType );
        if ( res )
        {
            // Try to turn the mimetype name into its comment
            TQString mimeName = mimeType;
            KMimeType::Ptr mime = KMimeType::mimeType( mimeType );
            if ( mime->name() != KMimeType::defaultMimeType() )
                mimeName = mime->comment();
            d->m_doc->setProperty( "unavailReason", i18n( "No handler found for %1" ).tqarg( mimeName ) );
        }
        return res;
    }

    const bool oasis = mimeType.startsWith( "application/vnd.oasis.opendocument" );
    if ( !oasis ) {
        m_tmpURL += "/maindoc.xml";
        kdDebug() << " m_tmpURL adjusted to " << m_tmpURL << endl;
    }
    return loadDocumentInternal( store, e, true /*open url*/, oasis );
}

bool KoDocumentChild::loadDocumentInternal( KoStore* store, const KoDocumentEntry& e, bool doOpenURL, bool oasis )
{
    kdDebug(30003) << "KoDocumentChild::loadDocumentInternal doOpenURL=" << doOpenURL << " m_tmpURL=" << m_tmpURL << endl;
    KoDocument * doc = e.createDoc( d->m_parent );
    if (!doc) {
        kdWarning(30003) << "createDoc failed" << endl;
        return false;
    }
    setDocument( doc, m_tmpGeometry );

    bool res = true;
    if ( doOpenURL )
    {
        bool internalURL = false;
        if ( m_tmpURL.startsWith( STORE_PROTOCOL ) || m_tmpURL.startsWith( INTERNAL_PROTOCOL ) || KURL::isRelativeURL( m_tmpURL ) )
        {
            if ( oasis ) {
                store->pushDirectory();
                assert( m_tmpURL.startsWith( INTERNAL_PROTOCOL ) );
                TQString relPath = KURL( m_tmpURL ).path().mid( 1 );
                store->enterDirectory( relPath );
                res = d->m_doc->loadOasisFromStore( store );
                store->popDirectory();
            } else {
                if ( m_tmpURL.startsWith( INTERNAL_PROTOCOL ) )
                    m_tmpURL = KURL( m_tmpURL ).path().mid( 1 );
                res = d->m_doc->loadFromStore( store, m_tmpURL );
            }
            internalURL = true;
            d->m_doc->setStoreInternal( true );
        }
        else
        {
            // Reference to an external document. Hmmm...
            d->m_doc->setStoreInternal( false );
            KURL url( m_tmpURL );
            if ( !url.isLocalFile() )
            {
                TQApplication::restoreOverrideCursor();
                // For security reasons we need to ask confirmation if the url is remote
                int result = KMessageBox::warningYesNoCancel(
                    0, i18n( "This document contains an external link to a remote document\n%1").tqarg(m_tmpURL),
                    i18n( "Confirmation Required" ), i18n( "Download" ), i18n( "Skip" ) );

                if ( result == KMessageBox::Cancel )
                {
                    d->m_parent->setErrorMessage("USER_CANCELED");
                    return false;
                }
                if ( result == KMessageBox::Yes )
                    res = d->m_doc->openURL( url );
                // and if == No, res will still be false so we'll use a kounavail below
            }
            else
                res = d->m_doc->openURL( url );
        }
        if ( !res )
        {
            // Keep the error message from the attempted loading
            TQString errorMessage = d->m_doc->errorMessage();
            delete d->m_doc;
            d->m_doc = 0;
            TQString tmpURL = m_tmpURL; // keep a copy, createUnavailDocument will erase it
            // Not found -> use a kounavail instead
            res = createUnavailDocument( store, false /* the URL doesn't exist, don't try to open it */, m_tmpMimeType );
            if ( res )
            {
                d->m_doc->setProperty( "realURL", tmpURL ); // so that it gets saved correctly
                d->m_doc->setStoreInternal( true );
                if ( internalURL )
                    d->m_doc->setProperty( "unavailReason", i18n( "Could not load embedded object:\n%1" ).tqarg( errorMessage ) );
                else
                    d->m_doc->setProperty( "unavailReason", i18n( "Could not load external document %1:\n%2" ).tqarg( tmpURL, errorMessage ) );
            }
            return res;
        }
        // Still waiting...
        TQApplication::setOverrideCursor( waitCursor );
    }

    m_tmpURL = TQString();

    // see KoDocument::insertChild for an explanation what's going on
    // now :-)
    if ( parentDocument() )
    {
        KoDocument *parent = parentDocument();

        if ( parent->manager() && parent->manager()->parts() )
        {
            KParts::PartManager *manager = parent->manager();

            if ( !manager->parts()->containsRef( d->m_doc ) &&
                 !parent->isSingleViewMode() )
                manager->addPart( d->m_doc, false );
        }
    }

    TQApplication::restoreOverrideCursor();

    return res;
}

bool KoDocumentChild::createUnavailDocument( KoStore* store, bool doOpenURL, const TQString& mimeType )
{
    // We don't need a trader query here. We're looking for a very specific component.
    KService::Ptr serv = KService::serviceByDesktopName( "kounavail" );
    if ( serv == 0L )
    {
        kdWarning(30003) << "ERROR: service kounavail not found " << endl;
        return false;
    }
    KoDocumentEntry e( serv );
    if ( !loadDocumentInternal( store, e, doOpenURL, false ) )
        return false;
    d->m_doc->setProperty( "mimetype", mimeType );
    return true;
}

bool KoDocumentChild::saveOasis( KoStore* store, KoXmlWriter* manifestWriter )
{
    TQString path;
    if ( d->m_doc->isStoredExtern() )
    {
        kdDebug(30003)<<k_funcinfo<<" external (don't save) url:" << d->m_doc->url().url()<<endl;
        path = d->m_doc->url().url();
    }
    else
    {
        // The name comes from KoDocumentChild (which was set while saving the
        // parent document).
        assert( d->m_doc->url().protocol() == INTERNAL_PROTOCOL );
        const TQString name = d->m_doc->url().path();
        kdDebug() << k_funcinfo << "saving " << name << endl;

        if ( d->m_doc->nativeOasisMimeType().isEmpty() ) {
            // Embedded object doesn't support OASIS OpenDocument, save in the old format.
            kdDebug() << k_funcinfo << "Embedded object doesn't support OASIS OpenDocument, save in the old format." << endl;

            if ( !d->m_doc->saveToStore( store, name ) )
                return false;
        }
        else
        {
            // To make the tqchildren happy cd to the correct directory
            store->pushDirectory();
            store->enterDirectory( name );

            if ( !d->m_doc->saveOasis( store, manifestWriter ) ) {
                kdWarning(30003) << "KoDocumentChild::saveOasis failed" << endl;
                return false;
            }
            // Now that we're done leave the directory again
            store->popDirectory();
        }

        assert( d->m_doc->url().protocol() == INTERNAL_PROTOCOL );
        path = store->currentDirectory();
        if ( !path.isEmpty() )
            path += '/';
        path += d->m_doc->url().path();
        if ( path.startsWith( "/" ) )
            path = path.mid( 1 ); // remove leading '/', no wanted in manifest
    }

    // OOo uses a trailing slash for the path to embedded objects (== directories)
    if ( !path.endsWith( "/" ) )
        path += '/';
    TQCString mimetype = d->m_doc->nativeOasisMimeType();
    if ( mimetype.isEmpty() )
        mimetype = d->m_doc->nativeFormatMimeType();
    manifestWriter->addManifestEntry( path, mimetype );

    return true;
}

void KoDocumentChild::saveOasisAttributes( KoXmlWriter &xmlWriter, const TQString& name )
{
    if ( !d->m_doc->isStoredExtern() )
    {
        // set URL in document so that KoDocument::saveChildrenOasis will save
        // the actual embedded object with the right name in the store.
        KURL u;
        u.setProtocol( INTERNAL_PROTOCOL );
        u.setPath( name );
        kdDebug() << k_funcinfo << u << endl;
        d->m_doc->setURL( u );
    }

    //<draw:object draw:style-name="standard" draw:id="1" draw:layer="tqlayout" svg:width="14.973cm" svg:height="4.478cm" svg:x="11.641cm" svg:y="14.613cm" xlink:href="#./Object 1" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad"/>
    xmlWriter.addAttribute( "xlink:type", "simple" );
    xmlWriter.addAttribute( "xlink:show", "embed" );
    xmlWriter.addAttribute( "xlink:actuate", "onLoad" );

    const TQString ref = d->m_doc->isStoredExtern() ? d->m_doc->url().url() : "./"+ name;
    kdDebug(30003) << "KoDocumentChild::saveOasis saving reference to embedded document as " << ref << endl;
    xmlWriter.addAttribute( "xlink:href", /*"#" + */ref );
}

TQDomElement KoDocumentChild::save( TQDomDocument& doc, bool uppercase )
{
    if( d->m_doc )
    {
        TQDomElement e = doc.createElement( ( uppercase ? "OBJECT" : "object" ) );
        if ( d->m_doc->url().protocol() != INTERNAL_PROTOCOL ) {
            e.setAttribute( "url", d->m_doc->url().url() );
            kdDebug() << "KoDocumentChild::save url=" << d->m_doc->url().url() << endl;
        }
        else {
            e.setAttribute( "url", d->m_doc->url().path().mid( 1 ) );
            kdDebug() << "KoDocumentChild::save url=" << d->m_doc->url().path().mid( 1 ) << endl;
        }
        e.setAttribute( "mime", d->m_doc->nativeFormatMimeType().data() );
        kdDebug() << "KoDocumentChild::save mime=" << d->m_doc->nativeFormatMimeType() << endl;
        TQDomElement rect = doc.createElement( ( uppercase ? "RECT" : "rect" ) );
        rect.setAttribute( "x", tqgeometry().left() );
        rect.setAttribute( "y", tqgeometry().top() );
        rect.setAttribute( "w", tqgeometry().width() );
        rect.setAttribute( "h", tqgeometry().height() );
        e.appendChild(rect);
        return e;
    }
    return TQDomElement();
}

bool KoDocumentChild::isStoredExtern() const
{
    assert( d->m_doc );
    return d->m_doc->isStoredExtern();
}

KURL KoDocumentChild::url() const
{
    return ( d->m_doc ? d->m_doc->url() : KURL() );
}

KoDocumentChild::~KoDocumentChild()
{
  if ( d->m_doc ) {
    delete d->m_doc;
    d->m_doc=0L;
  }
  delete d;
}

bool KoDocumentChild::isDeleted() const
{
    return d->m_deleted;
}

void KoDocumentChild::setDeleted( bool on )
{
    d->m_deleted = on;
}

#include "KoDocumentChild.moc"