21#include "KoDocumentChild.h"
22#include "KoOasisStore.h"
23#include <KoDocument.h>
24#include <KoQueryTrader.h>
25#include <KoXmlWriter.h>
29#include <KoStoreDevice.h>
31#include <tdeparts/partmanager.h>
35#include <tdemessagebox.h>
38#include <tqapplication.h>
45#define STORE_PROTOCOL "tar"
46#define INTERNAL_PROTOCOL "intern"
55class KoDocumentChildPrivate
58 KoDocumentChildPrivate()
61 ~KoDocumentChildPrivate()
73 d =
new KoDocumentChildPrivate;
76 setGeometry( geometry );
85 d =
new KoDocumentChildPrivate;
93 kdDebug()<<k_funcinfo<<
"doc: "<<doc->
url().url()<<endl;
110KoDocument* KoDocumentChild::hitTest(
const TQPoint &p,
const TQWMatrix &_matrix )
112 if ( !
region( _matrix ).contains( p ) || !d->m_doc )
115 TQWMatrix m( _matrix );
119 return d->m_doc->hitTest( p, m );
122void KoDocumentChild::loadOasis(
const TQDomElement &frameElement,
const TQDomElement& objectElement )
127 w =
KoUnit::parseValue( frameElement.attributeNS( KoXmlNS::svg,
"width", TQString() ) );
128 h =
KoUnit::parseValue( frameElement.attributeNS( KoXmlNS::svg,
"height", TQString() ) );
132 TQString
url = objectElement.attributeNS( KoXmlNS::xlink,
"href", TQString() );
135 if (
url.startsWith(
"./" ) )
136 m_tmpURL = TQString( INTERNAL_PROTOCOL ) +
":/" +
url.mid( 2 );
139 kdDebug() << k_funcinfo <<
m_tmpURL << endl;
145 if ( element.hasAttribute(
"url" ) )
146 m_tmpURL = element.attribute(
"url");
147 if ( element.hasAttribute(
"mime") )
152 kdDebug(30003) <<
"Empty 'url' attribute in OBJECT" << endl;
157 kdDebug(30003) <<
"Empty 'mime' attribute in OBJECT" << endl;
162 TQDomNode n = element.firstChild();
163 for( ; !n.isNull(); n = n.nextSibling() )
165 TQDomElement e = n.toElement();
166 if ( e.isNull() )
continue;
167 if ( e.tagName() ==
"rect" || ( uppercase && e.tagName() ==
"RECT" ) )
172 if ( e.hasAttribute(
"x" ) )
173 x = e.attribute(
"x" ).toInt(&brect);
174 if ( e.hasAttribute(
"y" ) )
175 y = e.attribute(
"y" ).toInt(&brect);
176 if ( e.hasAttribute(
"w" ) )
177 w = e.attribute(
"w" ).toInt(&brect);
178 if ( e.hasAttribute(
"h" ) )
179 h = e.attribute(
"h" ).toInt(&brect);
187 kdDebug(30003) <<
"Missing RECT in OBJECT" << endl;
198 kdDebug(30003) <<
"KoDocumentChild::loadDocument: trying to load " <<
m_tmpURL << endl;
207 kdWarning(30003) <<
"Could not create child document with type " <<
m_tmpMimeType << endl;
208 bool res = createUnavailDocument( store,
true,
m_tmpMimeType );
214 if ( mime->name() != KMimeType::defaultMimeType() )
215 mimeName = mime->comment();
216 d->m_doc->setProperty(
"unavailReason", i18n(
"No handler found for %1" ).arg( mimeName ) );
221 return loadDocumentInternal( store, e,
true ,
false );
227 if (
m_tmpURL.startsWith( INTERNAL_PROTOCOL ) ) {
229 if ( !path.isEmpty() )
231 TQString relPath = KURL(
m_tmpURL ).path();
232 path += relPath.mid( 1 );
234 if ( !path.endsWith(
"/" ) )
237 kdDebug() << k_funcinfo <<
"path for manifest file=" << path <<
" mimeType=" << mimeType << endl;
238 if ( mimeType.isEmpty() ) {
239 kdError(30003) <<
"Manifest doesn't have media-type for " << path << endl;
246 kdWarning(30003) <<
"Could not create child document with type " << mimeType << endl;
247 bool res = createUnavailDocument( store,
true, mimeType );
251 TQString mimeName = mimeType;
252 KMimeType::Ptr mime = KMimeType::mimeType( mimeType );
253 if ( mime->name() != KMimeType::defaultMimeType() )
254 mimeName = mime->comment();
255 d->m_doc->setProperty(
"unavailReason", i18n(
"No handler found for %1" ).arg( mimeName ) );
260 const bool oasis = mimeType.startsWith(
"application/vnd.oasis.opendocument" );
263 kdDebug() <<
" m_tmpURL adjusted to " <<
m_tmpURL << endl;
265 return loadDocumentInternal( store, e,
true , oasis );
268bool KoDocumentChild::loadDocumentInternal(
KoStore* store,
const KoDocumentEntry& e,
bool doOpenURL,
bool oasis )
270 kdDebug(30003) <<
"KoDocumentChild::loadDocumentInternal doOpenURL=" << doOpenURL <<
" m_tmpURL=" <<
m_tmpURL << endl;
273 kdWarning(30003) <<
"createDoc failed" << endl;
281 bool internalURL =
false;
282 if (
m_tmpURL.startsWith( STORE_PROTOCOL ) ||
m_tmpURL.startsWith( INTERNAL_PROTOCOL ) || KURL::isRelativeURL(
m_tmpURL ) )
286 assert(
m_tmpURL.startsWith( INTERNAL_PROTOCOL ) );
287 TQString relPath = KURL(
m_tmpURL ).path().mid( 1 );
289 res = d->m_doc->loadOasisFromStore( store );
292 if (
m_tmpURL.startsWith( INTERNAL_PROTOCOL ) )
294 res = d->m_doc->loadFromStore( store,
m_tmpURL );
297 d->m_doc->setStoreInternal(
true );
302 d->m_doc->setStoreInternal(
false );
304 if ( !
url.isLocalFile() )
306 TQApplication::restoreOverrideCursor();
309 0, i18n(
"This document contains an external link to a remote document\n%1").arg(
m_tmpURL),
310 i18n(
"Confirmation Required" ), i18n(
"Download" ), i18n(
"Skip" ) );
312 if ( result == KMessageBox::Cancel )
314 d->m_parent->setErrorMessage(
"USER_CANCELED");
317 if ( result == KMessageBox::Yes )
318 res = d->m_doc->openURL(
url );
322 res = d->m_doc->openURL(
url );
327 TQString errorMessage = d->m_doc->errorMessage();
335 d->m_doc->setProperty(
"realURL", tmpURL );
336 d->m_doc->setStoreInternal(
true );
338 d->m_doc->setProperty(
"unavailReason", i18n(
"Could not load embedded object:\n%1" ).arg( errorMessage ) );
340 d->m_doc->setProperty(
"unavailReason", i18n(
"Could not load external document %1:\n%2" ).arg( tmpURL, errorMessage ) );
345 TQApplication::setOverrideCursor( waitCursor );
360 if ( !manager->
parts()->containsRef( d->m_doc ) &&
362 manager->
addPart( d->m_doc,
false );
366 TQApplication::restoreOverrideCursor();
371bool KoDocumentChild::createUnavailDocument(
KoStore* store,
bool doOpenURL,
const TQString& mimeType )
374 KService::Ptr serv = KService::serviceByDesktopName(
"kounavail" );
377 kdWarning(30003) <<
"ERROR: service kounavail not found " << endl;
381 if ( !loadDocumentInternal( store, e, doOpenURL,
false ) )
383 d->m_doc->setProperty(
"mimetype", mimeType );
390 if ( d->m_doc->isStoredExtern() )
392 kdDebug(30003)<<k_funcinfo<<
" external (don't save) url:" << d->m_doc->url().url()<<endl;
393 path = d->m_doc->url().url();
399 assert( d->m_doc->url().protocol() == INTERNAL_PROTOCOL );
400 const TQString name = d->m_doc->url().path();
401 kdDebug() << k_funcinfo <<
"saving " << name << endl;
403 if ( d->m_doc->nativeOasisMimeType().isEmpty() ) {
405 kdDebug() << k_funcinfo <<
"Embedded object doesn't support OASIS OpenDocument, save in the old format." << endl;
407 if ( !d->m_doc->saveToStore( store, name ) )
416 if ( !d->m_doc->saveOasis( store, manifestWriter ) ) {
417 kdWarning(30003) <<
"KoDocumentChild::saveOasis failed" << endl;
424 assert( d->m_doc->url().protocol() == INTERNAL_PROTOCOL );
426 if ( !path.isEmpty() )
428 path += d->m_doc->url().path();
429 if ( path.startsWith(
"/" ) )
430 path = path.mid( 1 );
434 if ( !path.endsWith(
"/" ) )
436 TQCString mimetype = d->m_doc->nativeOasisMimeType();
437 if ( mimetype.isEmpty() )
438 mimetype = d->m_doc->nativeFormatMimeType();
446 if ( !d->m_doc->isStoredExtern() )
451 u.setProtocol( INTERNAL_PROTOCOL );
453 kdDebug() << k_funcinfo << u << endl;
454 d->m_doc->setURL( u );
462 const TQString ref = d->m_doc->isStoredExtern() ? d->m_doc->url().url() :
"./"+ name;
463 kdDebug(30003) <<
"KoDocumentChild::saveOasis saving reference to embedded document as " << ref << endl;
471 TQDomElement e = doc.createElement( ( uppercase ?
"OBJECT" :
"object" ) );
472 if ( d->m_doc->url().protocol() != INTERNAL_PROTOCOL ) {
473 e.setAttribute(
"url", d->m_doc->url().url() );
474 kdDebug() <<
"KoDocumentChild::save url=" << d->m_doc->url().url() << endl;
477 e.setAttribute(
"url", d->m_doc->url().path().mid( 1 ) );
478 kdDebug() <<
"KoDocumentChild::save url=" << d->m_doc->url().path().mid( 1 ) << endl;
480 e.setAttribute(
"mime", d->m_doc->nativeFormatMimeType().data() );
481 kdDebug() <<
"KoDocumentChild::save mime=" << d->m_doc->nativeFormatMimeType() << endl;
482 TQDomElement rect = doc.createElement( ( uppercase ?
"RECT" :
"rect" ) );
483 rect.setAttribute(
"x",
geometry().left() );
484 rect.setAttribute(
"y",
geometry().top() );
485 rect.setAttribute(
"w",
geometry().width() );
486 rect.setAttribute(
"h",
geometry().height() );
490 return TQDomElement();
493bool KoDocumentChild::isStoredExtern()
const
496 return d->m_doc->isStoredExtern();
501 return ( d->m_doc ? d->m_doc->url() : KURL() );
504KoDocumentChild::~KoDocumentChild()
518void KoDocumentChild::setDeleted(
bool on )
523#include "KoDocumentChild.moc"
static int warningYesNoCancel(TQWidget *parent, const TQString &text, const TQString &caption=TQString::null, const KGuiItem &buttonYes=KStdGuiItem::yes(), const KGuiItem &buttonNo=KStdGuiItem::no(), const TQString &dontAskAgainName=TQString::null, int options=Notify)
const TQPtrList< Part > * parts() const
virtual void addPart(Part *part, bool setActive=true)
PartManager * manager() const
KoChild is an abstract base class that represents the geometry associated with an embedded document.
void setGeometry(const TQRect &rect, bool noEmit=false)
Sets a new geometry for this child document.
virtual TQWMatrix matrix() const
virtual TQRegion region(const TQWMatrix &=TQWMatrix()) const
virtual void updateMatrix()
Stores the current transformation of this child into a matrix.
virtual double xScaling() const
virtual double yScaling() const
TQRect m_tmpGeometry
This variable is set after parsing the OBJECT tag in load and is reset after calling loadDocument.
virtual bool saveOasis(KoStore *store, KoXmlWriter *manifestWriter)
Save an embedded object to an OASIS store.
virtual bool load(const TQDomElement &element, bool uppercase=false)
Parses the "object" tag.
KoDocument * document() const
virtual bool loadDocument(KoStore *)
Actually loads the document from the disk/net or from the store, depending on url.
void saveOasisAttributes(KoXmlWriter &xmlWriter, const TQString &name)
Save an embedded object to OASIS.
bool isDeleted() const
This document (child) is deleted.
virtual TQDomElement save(TQDomDocument &doc, bool uppercase=false)
Writes the "object" tag, but does NOT write the content of the embedded documents.
virtual void setDocument(KoDocument *doc, const TQRect &geometry)
Call this function only directly after calling the constructor that takes only a parent as argument.
TQString m_tmpMimeType
This variable is set after parsing the OBJECT tag in load and is reset after calling loadDocument.
TQString m_tmpURL
Holds the source of this object, for example "file:/home/weis/image.gif" or "tar:/table1/2" if it is ...
virtual KoDocument * parentDocument() const
virtual bool loadOasisDocument(KoStore *store, const TQDomDocument &manifestDoc)
Actually loads the document from the disk/net or from the store depending on the document's url.
Represents an available KOffice component that supports the document interface.
static KoDocumentEntry queryByMimeType(const TQString &mimetype)
This is a convenience function.
KoDocument * createDoc(KoDocument *parent=0, const char *name=0) const
Uses the factory of the component to create a document.
The KOffice document class.
bool isSingleViewMode() const
Tells whether this document is in singleview mode.
void setStoreInternal(bool i)
Set when you want an external embedded document to be stored internally.
static TQString mimeForPath(const TQDomDocument &doc, const TQString &fullPath)
Another method for loading: get mimetype from full path, using the manifest.
Saves and loads KOffice documents using various backends.
void pushDirectory()
Stacks the current directory.
void popDirectory()
Restores the previously pushed directory.
bool enterDirectory(const TQString &directory)
Enters one or multiple directories.
TQString currentDirectory() const
Returns the current directory.
static double parseValue(TQString value, double defaultVal=0.0)
parse common KOffice and OO values, like "10cm", "5mm" to pt
A class for writing out XML (to any TQIODevice), with a special attention on performance.
void addManifestEntry(const TQString &fullPath, const TQString &mediaType)
Special helper for writing "manifest" files This is equivalent to startElement/2*addAttribute/endElem...
void addAttribute(const char *attrName, const TQString &value)
Overloaded version of addAttribute( const char*, const char* ), which is a bit slower because it need...