00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "KoDocument.h"
00022
00023 #include "KoDocument_p.h"
00024 #include "KoDocumentIface.h"
00025 #include "KoDocumentChild.h"
00026 #include "KoView.h"
00027 #include "KoMainWindow.h"
00028 #include "KoFilterManager.h"
00029 #include "KoDocumentInfo.h"
00030 #include "KoOasisStyles.h"
00031 #include "KoOasisStore.h"
00032 #include "KoXmlNS.h"
00033 #include "KoOpenPane.h"
00034
00035 #include <KoStoreDevice.h>
00036 #include <KoXmlWriter.h>
00037
00038 #include <kapplication.h>
00039 #include <kdebug.h>
00040 #include <kdeversion.h>
00041 #include <kfileitem.h>
00042 #include <kiconloader.h>
00043 #include <kio/job.h>
00044 #include <kio/netaccess.h>
00045 #include <klocale.h>
00046 #include <kmessagebox.h>
00047 #include <kmimetype.h>
00048 #include <kparts/partmanager.h>
00049 #include <kprinter.h>
00050 #include <ksavefile.h>
00051
00052 #include <qbuffer.h>
00053 #include <qcursor.h>
00054 #include <qdir.h>
00055 #include <qfile.h>
00056 #include <qfileinfo.h>
00057 #include <qimage.h>
00058 #include <qmap.h>
00059 #include <qpainter.h>
00060 #include <qtimer.h>
00061 #include <qxml.h>
00062 #include <qlayout.h>
00063
00064 #include <config.h>
00065 #include <assert.h>
00066
00067
00068
00069
00070
00071 #define STORE_PROTOCOL "tar"
00072
00073
00074 #define INTERNAL_PROTOCOL "intern"
00075 #define INTERNAL_PREFIX "intern:/"
00076
00077
00078 QPtrList<KoDocument> *KoDocument::s_documentList=0L;
00079
00080 using namespace std;
00081 class KoViewWrapperWidget;
00082
00083
00084
00085
00086
00087
00088
00089 const int KoDocument::s_defaultAutoSave = 300;
00090
00091 class KoDocument::Private
00092 {
00093 public:
00094 Private() :
00095 m_dcopObject( 0L ),
00096 filterManager( 0L ),
00097 m_specialOutputFlag( 0 ),
00098 m_isImporting( false ), m_isExporting( false ),
00099 m_numOperations( 0 ),
00100 modifiedAfterAutosave( false ),
00101 m_autosaving( false ),
00102 m_shouldCheckAutoSaveFile( true ),
00103 m_autoErrorHandlingEnabled( true ),
00104 m_backupFile( true ),
00105 m_backupPath( QString::null ),
00106 m_doNotSaveExtDoc( false ),
00107 m_current( false ),
00108 m_storeInternal( false ),
00109 m_bLoading( false ),
00110 m_startUpWidget( 0 )
00111 {
00112 m_confirmNonNativeSave[0] = true;
00113 m_confirmNonNativeSave[1] = true;
00114 if ( KGlobal::locale()->measureSystem() == KLocale::Imperial ) {
00115 m_unit = KoUnit::U_INCH;
00116 } else {
00117 m_unit = KoUnit::U_CM;
00118 }
00119 }
00120
00121 QPtrList<KoView> m_views;
00122 QPtrList<KoDocumentChild> m_children;
00123 QPtrList<KoMainWindow> m_shells;
00124 QValueList<QDomDocument> m_viewBuildDocuments;
00125
00126 KoViewWrapperWidget *m_wrapperWidget;
00127 KoDocumentIface * m_dcopObject;
00128 KoDocumentInfo *m_docInfo;
00129 KoView* hitTestView;
00130
00131 KoUnit::Unit m_unit;
00132
00133 KoFilterManager * filterManager;
00134
00135 QCString mimeType;
00136 QCString outputMimeType;
00137 bool m_confirmNonNativeSave [2];
00138
00139
00140 int m_specialOutputFlag;
00141 bool m_isImporting, m_isExporting;
00142
00143 QTimer m_autoSaveTimer;
00144 QString lastErrorMessage;
00145 int m_autoSaveDelay;
00146 int m_numOperations;
00147 bool modifiedAfterAutosave;
00148 bool m_bSingleViewMode;
00149 bool m_autosaving;
00150 bool m_shouldCheckAutoSaveFile;
00151 bool m_autoErrorHandlingEnabled;
00152 bool m_backupFile;
00153 QString m_backupPath;
00154 bool m_doNotSaveExtDoc;
00155 bool m_current;
00156 bool m_storeInternal;
00157 bool m_bLoading;
00158
00159 KoOpenPane* m_startUpWidget;
00160 QString m_templateType;
00161 };
00162
00163
00164 class KoViewWrapperWidget : public QWidget
00165 {
00166 public:
00167 KoViewWrapperWidget( QWidget *parent, const char *name )
00168 : QWidget( parent, name )
00169 {
00170 KGlobal::locale()->insertCatalogue("koffice");
00171
00172 KGlobal::iconLoader()->addAppDir("koffice");
00173 m_view = 0L;
00174
00175 setFocusPolicy( ClickFocus );
00176 }
00177
00178 virtual ~KoViewWrapperWidget() {
00179 setFocusProxy( 0 );
00180 }
00181
00182 virtual void resizeEvent( QResizeEvent * )
00183 {
00184 QObject *wid = child( 0, "QWidget" );
00185 if ( wid )
00186 static_cast<QWidget *>(wid)->setGeometry( 0, 0, width(), height() );
00187 }
00188
00189 virtual void childEvent( QChildEvent *ev )
00190 {
00191 if ( ev->type() == QEvent::ChildInserted )
00192 resizeEvent( 0L );
00193 }
00194
00195
00196 void setKoView( KoView * view ) {
00197 m_view = view;
00198 setFocusProxy( m_view );
00199 }
00200 KoView * koView() const { return m_view; }
00201 private:
00202 KoView* m_view;
00203 };
00204
00205 KoBrowserExtension::KoBrowserExtension( KoDocument * doc, const char * name )
00206 : KParts::BrowserExtension( doc, name )
00207 {
00208 emit enableAction( "print", true );
00209 }
00210
00211 void KoBrowserExtension::print()
00212 {
00213 KoDocument * doc = static_cast<KoDocument *>( parent() );
00214 KoViewWrapperWidget * wrapper = static_cast<KoViewWrapperWidget *>( doc->widget() );
00215 KoView * view = wrapper->koView();
00216
00217 KPrinter printer;
00218
00219 view->setupPrinter( printer );
00220 if ( printer.setup( view ) )
00221 view->print( printer );
00222 }
00223
00224 KoDocument::KoDocument( QWidget * parentWidget, const char *widgetName, QObject* parent, const char* name, bool singleViewMode )
00225 : KParts::ReadWritePart( parent, name )
00226 {
00227 if(s_documentList==0L)
00228 s_documentList=new QPtrList<KoDocument>;
00229 s_documentList->append(this);
00230
00231 d = new Private;
00232 m_bEmpty = TRUE;
00233 connect( &d->m_autoSaveTimer, SIGNAL( timeout() ), this, SLOT( slotAutoSave() ) );
00234 setAutoSave( s_defaultAutoSave );
00235 d->m_bSingleViewMode = singleViewMode;
00236
00237
00238
00239 if ( parent )
00240 {
00241 if ( parent->inherits( "KoDocument" ) )
00242 d->m_bSingleViewMode = ((KoDocument *)parent)->isSingleViewMode();
00243 else if ( parent->inherits( "KParts::Part" ) )
00244 d->m_bSingleViewMode = true;
00245 }
00246
00247 if ( singleViewMode )
00248 {
00249 d->m_wrapperWidget = new KoViewWrapperWidget( parentWidget, widgetName );
00250 setWidget( d->m_wrapperWidget );
00251 kdDebug(30003) << "creating KoBrowserExtension" << endl;
00252 (void) new KoBrowserExtension( this );
00253 }
00254
00255 d->m_docInfo = new KoDocumentInfo( this, "document info" );
00256
00257 m_pageLayout.ptWidth = 0;
00258 m_pageLayout.ptHeight = 0;
00259 m_pageLayout.ptTop = 0;
00260 m_pageLayout.ptBottom = 0;
00261 m_pageLayout.ptLeft = 0;
00262 m_pageLayout.ptRight = 0;
00263
00264
00265 if ( !singleViewMode )
00266 connect( this, SIGNAL( started( KIO::Job* ) ), SLOT( slotStarted( KIO::Job* ) ) );
00267 }
00268
00269 KoDocument::~KoDocument()
00270 {
00271 d->m_autoSaveTimer.stop();
00272
00273 QPtrListIterator<KoDocumentChild> childIt( d->m_children );
00274 for (; childIt.current(); ++childIt )
00275 disconnect( childIt.current(), SIGNAL( destroyed() ),
00276 this, SLOT( slotChildDestroyed() ) );
00277
00278
00279
00280 QPtrListIterator<KoView> vIt( d->m_views );
00281 for (; vIt.current(); ++vIt )
00282 vIt.current()->setDocumentDeleted();
00283
00284 delete d->m_startUpWidget;
00285 d->m_startUpWidget = 0;
00286
00287 d->m_children.setAutoDelete( true );
00288 d->m_children.clear();
00289
00290 d->m_shells.setAutoDelete( true );
00291 d->m_shells.clear();
00292
00293 delete d->m_dcopObject;
00294 delete d->filterManager;
00295 delete d;
00296 s_documentList->removeRef(this);
00297
00298 if(s_documentList->isEmpty()) {
00299 delete s_documentList;
00300 s_documentList=0;
00301 }
00302 }
00303
00304 bool KoDocument::isSingleViewMode() const
00305 {
00306 return d->m_bSingleViewMode;
00307 }
00308
00309 bool KoDocument::isEmbedded() const
00310 {
00311 return dynamic_cast<KoDocument *>( parent() ) != 0;
00312 }
00313
00314 KoView *KoDocument::createView( QWidget *parent, const char *name )
00315 {
00316 KoView *view=createViewInstance(parent, name);
00317 addView(view);
00318 return view;
00319 }
00320
00321 bool KoDocument::exp0rt( const KURL & _url )
00322 {
00323 bool ret;
00324
00325 d->m_isExporting = true;
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335 KURL oldURL = m_url;
00336 QString oldFile = m_file;
00337
00338 bool wasModified = isModified ();
00339 QCString oldMimeType = mimeType ();
00340
00341
00342
00343 ret = saveAs( _url );
00344
00345
00346
00347
00348
00349
00350 kdDebug(30003) << "Restoring KoDocument state to before export" << endl;
00351
00352
00353
00354 m_url = oldURL;
00355 m_file = oldFile;
00356
00357
00358
00359 if (ret)
00360 {
00361 setModified (wasModified);
00362 d->mimeType = oldMimeType;
00363 }
00364
00365
00366 d->m_isExporting = false;
00367
00368 return ret;
00369 }
00370
00371 bool KoDocument::saveFile()
00372 {
00373 kdDebug(30003) << "KoDocument::saveFile() doc='" << url().url() <<"'"<< endl;
00374
00375
00376 const bool wasModified = isModified ();
00377
00378
00379 QCString outputMimeType = d->outputMimeType;
00380
00381 if ( outputMimeType.isEmpty() )
00382 outputMimeType = d->outputMimeType = nativeFormatMimeType();
00383
00384 QApplication::setOverrideCursor( waitCursor );
00385
00386 if ( backupFile() ) {
00387 if ( url().isLocalFile() )
00388 KSaveFile::backupFile( url().path(), d->m_backupPath );
00389 else {
00390 KIO::UDSEntry entry;
00391 if ( KIO::NetAccess::stat( url(), entry, shells().current() ) ) {
00392 emit sigStatusBarMessage( i18n("Making backup...") );
00393 KURL backup;
00394 if ( d->m_backupPath.isEmpty())
00395 backup = url();
00396 else
00397 backup = d->m_backupPath +"/"+url().fileName();
00398 backup.setPath( backup.path() + QString::fromLatin1("~") );
00399 KFileItem item( entry, url() );
00400 Q_ASSERT( item.name() == url().fileName() );
00401 KIO::NetAccess::file_copy( url(), backup, item.permissions(), true , false , shells().current() );
00402 }
00403 }
00404 }
00405
00406 emit sigStatusBarMessage( i18n("Saving...") );
00407 bool ret = false;
00408 bool suppressErrorDialog = false;
00409 if ( !isNativeFormat( outputMimeType ) ) {
00410 kdDebug(30003) << "Saving to format " << outputMimeType << " in " << m_file << endl;
00411
00412 if ( !d->filterManager )
00413 d->filterManager = new KoFilterManager( this );
00414
00415 KoFilter::ConversionStatus status = d->filterManager->exp0rt( m_file, outputMimeType );
00416 ret = status == KoFilter::OK;
00417 suppressErrorDialog = (status == KoFilter::UserCancelled || status == KoFilter::BadConversionGraph );
00418 } else {
00419
00420 Q_ASSERT( !m_file.isEmpty() );
00421 ret = saveNativeFormat( m_file );
00422 }
00423
00424 if ( ret ) {
00425 removeAutoSaveFiles();
00426
00427
00428 setAutoSave( d->m_autoSaveDelay );
00429 }
00430
00431 QApplication::restoreOverrideCursor();
00432 if ( !ret )
00433 {
00434 if ( !suppressErrorDialog )
00435 {
00436 showSavingErrorDialog();
00437 }
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449 resetURL();
00450
00451
00452 setModified( wasModified );
00453 }
00454
00455 if ( ret )
00456 {
00457 d->mimeType = outputMimeType;
00458 setConfirmNonNativeSave ( isExporting (), false );
00459 }
00460 emit sigClearStatusBarMessage();
00461
00462 return ret;
00463 }
00464
00465 QCString KoDocument::mimeType() const
00466 {
00467 return d->mimeType;
00468 }
00469
00470 void KoDocument::setMimeType( const QCString & mimeType )
00471 {
00472 d->mimeType = mimeType;
00473 }
00474
00475 void KoDocument::setOutputMimeType( const QCString & mimeType, int specialOutputFlag )
00476 {
00477 d->outputMimeType = mimeType;
00478 d->m_specialOutputFlag = specialOutputFlag;
00479 }
00480
00481 QCString KoDocument::outputMimeType() const
00482 {
00483 return d->outputMimeType;
00484 }
00485
00486 int KoDocument::specialOutputFlag() const
00487 {
00488 return d->m_specialOutputFlag;
00489 }
00490
00491 bool KoDocument::confirmNonNativeSave( const bool exporting ) const
00492 {
00493
00494
00495 return d->m_confirmNonNativeSave [ exporting ? 1 : 0 ];
00496 }
00497
00498 void KoDocument::setConfirmNonNativeSave( const bool exporting, const bool on )
00499 {
00500 d->m_confirmNonNativeSave [ exporting ? 1 : 0] = on;
00501 }
00502
00503 bool KoDocument::wantExportConfirmation() const
00504 {
00505 return true;
00506 }
00507
00508 bool KoDocument::isImporting() const
00509 {
00510 return d->m_isImporting;
00511 }
00512
00513 bool KoDocument::isExporting() const
00514 {
00515 return d->m_isExporting;
00516 }
00517
00518 void KoDocument::setCheckAutoSaveFile( bool b )
00519 {
00520 d->m_shouldCheckAutoSaveFile = b;
00521 }
00522
00523 void KoDocument::setAutoErrorHandlingEnabled( bool b )
00524 {
00525 d->m_autoErrorHandlingEnabled = b;
00526 }
00527
00528 bool KoDocument::isAutoErrorHandlingEnabled() const
00529 {
00530 return d->m_autoErrorHandlingEnabled;
00531 }
00532
00533 void KoDocument::slotAutoSave()
00534 {
00535 if ( isModified() && d->modifiedAfterAutosave )
00536 {
00537 connect( this, SIGNAL( sigProgress( int ) ), shells().current(), SLOT( slotProgress( int ) ) );
00538 emit sigStatusBarMessage( i18n("Autosaving...") );
00539 d->m_autosaving = true;
00540 bool ret = saveNativeFormat( autoSaveFile( m_file ) );
00541 setModified( true );
00542 if ( ret ) {
00543 d->modifiedAfterAutosave = false;
00544 d->m_autoSaveTimer.stop();
00545 }
00546 d->m_autosaving = false;
00547 emit sigClearStatusBarMessage();
00548 disconnect( this, SIGNAL( sigProgress( int ) ), shells().current(), SLOT( slotProgress( int ) ) );
00549 if ( !ret )
00550 emit sigStatusBarMessage( i18n("Error during autosave! Partition full?") );
00551 }
00552 }
00553
00554 KAction *KoDocument::action( const QDomElement &element ) const
00555 {
00556
00557 KAction* act = KParts::ReadWritePart::action( element );
00558 if ( act )
00559 return act;
00560
00561 Q_ASSERT( d->m_bSingleViewMode );
00562
00563 if ( !d->m_views.isEmpty() )
00564 return d->m_views.getFirst()->action( element );
00565 else
00566 return 0L;
00567 }
00568
00569 QDomDocument KoDocument::domDocument() const
00570 {
00571
00572
00573 Q_ASSERT( d->m_bSingleViewMode );
00574 if ( d->m_views.isEmpty() )
00575 return QDomDocument();
00576 else
00577 return d->m_views.getFirst()->domDocument();
00578 }
00579
00580 void KoDocument::setManager( KParts::PartManager *manager )
00581 {
00582 KParts::ReadWritePart::setManager( manager );
00583 if ( d->m_bSingleViewMode && d->m_views.count() == 1 )
00584 d->m_views.getFirst()->setPartManager( manager );
00585
00586 if ( manager )
00587 {
00588 QPtrListIterator<KoDocumentChild> it( d->m_children );
00589 for (; it.current(); ++it )
00590 if ( it.current()->document() )
00591 manager->addPart( it.current()->document(), false );
00592 }
00593 }
00594
00595 void KoDocument::setReadWrite( bool readwrite )
00596 {
00597 KParts::ReadWritePart::setReadWrite( readwrite );
00598
00599 QPtrListIterator<KoView> vIt( d->m_views );
00600 for (; vIt.current(); ++vIt )
00601 vIt.current()->updateReadWrite( readwrite );
00602
00603 QPtrListIterator<KoDocumentChild> dIt( d->m_children );
00604 for (; dIt.current(); ++dIt )
00605 if ( dIt.current()->document() )
00606 dIt.current()->document()->setReadWrite( readwrite );
00607
00608 setAutoSave( d->m_autoSaveDelay );
00609 }
00610
00611 void KoDocument::setAutoSave( int delay )
00612 {
00613 d->m_autoSaveDelay = delay;
00614 if ( isReadWrite() && !isEmbedded() && d->m_autoSaveDelay > 0 )
00615 d->m_autoSaveTimer.start( d->m_autoSaveDelay * 1000 );
00616 else
00617 d->m_autoSaveTimer.stop();
00618 }
00619
00620 void KoDocument::addView( KoView *view )
00621 {
00622 if ( !view )
00623 return;
00624
00625 d->m_views.append( view );
00626 view->updateReadWrite( isReadWrite() );
00627 }
00628
00629 void KoDocument::removeView( KoView *view )
00630 {
00631 d->m_views.removeRef( view );
00632 }
00633
00634 const QPtrList<KoView>& KoDocument::views() const
00635 {
00636 return d->m_views;
00637 }
00638
00639 int KoDocument::viewCount() const
00640 {
00641 return d->m_views.count();
00642 }
00643
00644 void KoDocument::insertChild( KoDocumentChild *child )
00645 {
00646 setModified( true );
00647
00648 d->m_children.append( child );
00649
00650 connect( child, SIGNAL( changed( KoChild * ) ),
00651 this, SLOT( slotChildChanged( KoChild * ) ) );
00652 connect( child, SIGNAL( destroyed() ),
00653 this, SLOT( slotChildDestroyed() ) );
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663 if ( manager() && !isSingleViewMode() && child->document() )
00664 manager()->addPart( child->document(), false );
00665 }
00666
00667 void KoDocument::slotChildChanged( KoChild *c )
00668 {
00669 assert( c->inherits( "KoDocumentChild" ) );
00670 emit childChanged( static_cast<KoDocumentChild *>( c ) );
00671 }
00672
00673 void KoDocument::slotChildDestroyed()
00674 {
00675 setModified( true );
00676
00677 const KoDocumentChild *child = static_cast<const KoDocumentChild *>( sender() );
00678 d->m_children.removeRef( child );
00679 }
00680
00681 const QPtrList<KoDocumentChild>& KoDocument::children() const
00682 {
00683 return d->m_children;
00684 }
00685
00686 KParts::Part *KoDocument::hitTest( QWidget *widget, const QPoint &globalPos )
00687 {
00688 QPtrListIterator<KoView> it( d->m_views );
00689 for (; it.current(); ++it )
00690 if ( static_cast<QWidget *>(it.current()) == widget )
00691 {
00692 KoView* view = it.current();
00693 d->hitTestView = view;
00694 QPoint canvasPos( view->canvas()->mapFromGlobal( globalPos ) );
00695 canvasPos.rx() += view->canvasXOffset();
00696 canvasPos.ry() += view->canvasYOffset();
00697
00698 KParts::Part *part = view->hitTest( canvasPos );
00699 d->hitTestView = 0;
00700 if ( part )
00701 return part;
00702 }
00703
00704 return 0L;
00705 }
00706
00707 KoView* KoDocument::hitTestView()
00708 {
00709 return d->hitTestView;
00710 }
00711
00712 KoDocument* KoDocument::hitTest( const QPoint &pos, const QWMatrix &matrix )
00713 {
00714
00715 QPtrListIterator<KoDocumentChild> it( d->m_children );
00716 for (; it.current(); ++it )
00717 {
00718 KoDocument *doc = it.current()->hitTest( pos, matrix );
00719 if ( doc )
00720 return doc;
00721 }
00722
00723
00724 return this;
00725 }
00726
00727 KoDocumentChild *KoDocument::child( KoDocument *doc )
00728 {
00729 QPtrListIterator<KoDocumentChild> it( d->m_children );
00730 for (; it.current(); ++it )
00731 if ( it.current()->document() == doc )
00732 return it.current();
00733
00734 return 0L;
00735 }
00736
00737 KoDocumentInfo *KoDocument::documentInfo() const
00738 {
00739 return d->m_docInfo;
00740 }
00741
00742 void KoDocument::setViewBuildDocument( KoView *view, const QDomDocument &doc )
00743 {
00744 if ( d->m_views.find( view ) == -1 )
00745 return;
00746
00747 uint viewIdx = d->m_views.at();
00748
00749 if ( d->m_viewBuildDocuments.count() == viewIdx )
00750 d->m_viewBuildDocuments.append( doc );
00751 else if ( d->m_viewBuildDocuments.count() > viewIdx )
00752 d->m_viewBuildDocuments[ viewIdx ] = doc;
00753 }
00754
00755 QDomDocument KoDocument::viewBuildDocument( KoView *view )
00756 {
00757 QDomDocument res;
00758
00759 if ( d->m_views.find( view ) == -1 )
00760 return res;
00761
00762 uint viewIdx = d->m_views.at();
00763
00764 if ( viewIdx >= d->m_viewBuildDocuments.count() )
00765 return res;
00766
00767 res = d->m_viewBuildDocuments[ viewIdx ];
00768
00769
00770 d->m_viewBuildDocuments[ viewIdx ] = QDomDocument();
00771
00772 return res;
00773 }
00774
00775 void KoDocument::paintEverything( QPainter &painter, const QRect &rect, bool transparent, KoView *view, double zoomX, double zoomY )
00776 {
00777 paintContent( painter, rect, transparent, zoomX, zoomY );
00778 paintChildren( painter, rect, view, zoomX, zoomY );
00779 }
00780
00781 void KoDocument::paintChildren( QPainter &painter, const QRect &, KoView *view, double zoomX, double zoomY )
00782 {
00783 QPtrListIterator<KoDocumentChild> it( d->m_children );
00784 for (; it.current(); ++it )
00785 {
00786
00787 painter.save();
00788 paintChild( it.current(), painter, view, zoomX, zoomY );
00789 painter.restore();
00790 }
00791 }
00792
00793 void KoDocument::paintChild( KoDocumentChild *child, QPainter &painter, KoView *view, double zoomX, double zoomY )
00794 {
00795 if ( child->isDeleted() )
00796 return;
00797
00798
00799
00800 child->transform( painter );
00801 child->document()->paintEverything( painter, child->contentRect(), child->isTransparent(), view, zoomX, zoomY );
00802
00803 if ( view && view->partManager() )
00804 {
00805
00806 KParts::PartManager *manager = view->partManager();
00807
00808 painter.scale( 1.0 / child->xScaling(), 1.0 / child->yScaling() );
00809
00810 int w = int( (double)child->contentRect().width() * child->xScaling() );
00811 int h = int( (double)child->contentRect().height() * child->yScaling() );
00812 if ( ( manager->selectedPart() == (KParts::Part *)child->document() &&
00813 manager->selectedWidget() == (QWidget *)view ) ||
00814 ( manager->activePart() == (KParts::Part *)child->document() &&
00815 manager->activeWidget() == (QWidget *)view ) )
00816 {
00817
00818 painter.setClipping( FALSE );
00819
00820 painter.setPen( black );
00821 painter.fillRect( -5, -5, w + 10, 5, white );
00822 painter.fillRect( -5, h, w + 10, 5, white );
00823 painter.fillRect( -5, -5, 5, h + 10, white );
00824 painter.fillRect( w, -5, 5, h + 10, white );
00825 painter.fillRect( -5, -5, w + 10, 5, BDiagPattern );
00826 painter.fillRect( -5, h, w + 10, 5, BDiagPattern );
00827 painter.fillRect( -5, -5, 5, h + 10, BDiagPattern );
00828 painter.fillRect( w, -5, 5, h + 10, BDiagPattern );
00829
00830 if ( manager->selectedPart() == (KParts::Part *)child->document() &&
00831 manager->selectedWidget() == (QWidget *)view )
00832 {
00833 QColor color;
00834 if ( view->koDocument() == this )
00835 color = black;
00836 else
00837 color = gray;
00838 painter.fillRect( -5, -5, 5, 5, color );
00839 painter.fillRect( -5, h, 5, 5, color );
00840 painter.fillRect( w, h, 5, 5, color );
00841 painter.fillRect( w, -5, 5, 5, color );
00842 painter.fillRect( w / 2 - 3, -5, 5, 5, color );
00843 painter.fillRect( w / 2 - 3, h, 5, 5, color );
00844 painter.fillRect( -5, h / 2 - 3, 5, 5, color );
00845 painter.fillRect( w, h / 2 - 3, 5, 5, color );
00846 }
00847
00848 painter.setClipping( TRUE );
00849 }
00850 }
00851 }
00852
00853 bool KoDocument::isModified() const
00854 {
00855 if ( KParts::ReadWritePart::isModified() )
00856 {
00857
00858 return true;
00859 }
00860
00861 QPtrListIterator<KoDocumentChild> it = children();
00862 for (; it.current(); ++it )
00863 {
00864 KoDocument *doc = it.current()->document();
00865 if ( doc && !it.current()->isStoredExtern() && !it.current()->isDeleted() && doc->isModified() )
00866 return true;
00867 }
00868 return false;
00869 }
00870
00871 bool KoDocument::saveChildren( KoStore* _store )
00872 {
00873
00874 int i = 0;
00875 QPtrListIterator<KoDocumentChild> it( children() );
00876 for( ; it.current(); ++it ) {
00877 KoDocument* childDoc = it.current()->document();
00878 if (childDoc && !it.current()->isDeleted())
00879 {
00880 if ( !childDoc->isStoredExtern() )
00881 {
00882
00883 if ( !childDoc->saveToStore( _store, QString::number( i++ ) ) )
00884 return FALSE;
00885
00886 if (!isExporting ())
00887 childDoc->setModified( false );
00888 }
00889
00890 }
00891 }
00892 return true;
00893 }
00894
00895 bool KoDocument::saveChildrenOasis( KoStore* store, KoXmlWriter* manifestWriter )
00896 {
00897
00898 QPtrListIterator<KoDocumentChild> it( children() );
00899 for( ; it.current(); ++it ) {
00900 KoDocument* childDoc = it.current()->document();
00901 if ( childDoc && !it.current()->isDeleted() )
00902 {
00903 if ( !it.current()->saveOasis( store, manifestWriter ) )
00904 return false;
00905 if ( !childDoc->isStoredExtern() && !isExporting () )
00906 childDoc->setModified( false );
00907 }
00908 }
00909 return true;
00910 }
00911
00912 bool KoDocument::saveExternalChildren()
00913 {
00914 if ( d->m_doNotSaveExtDoc )
00915 {
00916
00917 d->m_doNotSaveExtDoc = false;
00918 return true;
00919 }
00920
00921
00922 KoDocument *doc;
00923 KoDocumentChild *ch;
00924 QPtrListIterator<KoDocumentChild> it = children();
00925 for (; (ch = it.current()); ++it )
00926 {
00927 if ( !ch->isDeleted() )
00928 {
00929 doc = ch->document();
00930 if ( doc && doc->isStoredExtern() && doc->isModified() )
00931 {
00932 kdDebug(30003)<<" save external doc='"<<url().url()<<"'"<<endl;
00933 doc->setDoNotSaveExtDoc();
00934 if ( !doc->save() )
00935 return false;
00936 }
00937
00938
00939 if ( !doc->saveExternalChildren() )
00940 return false;
00941 }
00942 }
00943 return true;
00944 }
00945
00946 bool KoDocument::saveNativeFormat( const QString & file )
00947 {
00948 d->lastErrorMessage = QString::null;
00949
00950
00951 KoStore::Backend backend = KoStore::Auto;
00952 #if 0
00953 if ( d->m_specialOutputFlag == SaveAsKOffice1dot1 )
00954 {
00955 kdDebug(30003) << "Saving as KOffice-1.1 format, using a tar.gz" << endl;
00956 backend = KoStore::Tar;
00958 }
00959 else
00960 #endif
00961 if ( d->m_specialOutputFlag == SaveAsDirectoryStore )
00962 {
00963 backend = KoStore::Directory;
00964 kdDebug(30003) << "Saving as uncompressed XML, using directory store." << endl;
00965 }
00966 else if ( d->m_specialOutputFlag == SaveAsFlatXML )
00967 {
00968 kdDebug(30003) << "Saving as a flat XML file." << endl;
00969 QFile f( file );
00970 if ( f.open( IO_WriteOnly | IO_Translate ) )
00971 {
00972 bool success = saveToStream( &f );
00973 f.close();
00974 return success;
00975 }
00976 else
00977 return false;
00978 }
00979
00980 kdDebug(30003) << "KoDocument::saveNativeFormat nativeFormatMimeType=" << nativeFormatMimeType() << endl;
00981
00982
00983 QCString mimeType = d->outputMimeType;
00984 QCString nativeOasisMime = nativeOasisMimeType();
00985 bool oasis = !mimeType.isEmpty() && ( mimeType == nativeOasisMime || mimeType == nativeOasisMime + "-template" );
00986
00987
00988 KoStore* store = KoStore::createStore( file, KoStore::Write, mimeType, backend );
00989 if ( store->bad() )
00990 {
00991 d->lastErrorMessage = i18n( "Could not create the file for saving" );
00992 delete store;
00993 return false;
00994 }
00995
00996 if ( oasis )
00997 {
00998 kdDebug(30003) << "Saving to OASIS format" << endl;
00999
01000 store->disallowNameExpansion();
01001 KoOasisStore oasisStore( store );
01002 KoXmlWriter* manifestWriter = oasisStore.manifestWriter( mimeType );
01003
01004 if ( !saveOasis( store, manifestWriter ) )
01005 {
01006 kdDebug(30003) << "saveOasis failed" << endl;
01007 delete store;
01008 return false;
01009 }
01010
01011
01012 if ( !saveChildrenOasis( store, manifestWriter ) )
01013 {
01014 kdDebug(30003) << "saveChildrenOasis failed" << endl;
01015 delete store;
01016 return false;
01017 }
01018
01019 if ( store->open( "meta.xml" ) )
01020 {
01021 if ( !d->m_docInfo->saveOasis( store ) || !store->close() ) {
01022 delete store;
01023 return false;
01024 }
01025 manifestWriter->addManifestEntry( "meta.xml", "text/xml" );
01026 }
01027 else
01028 {
01029 d->lastErrorMessage = i18n( "Not able to write '%1'. Partition full?" ).arg( "meta.xml" );
01030 delete store;
01031 return false;
01032 }
01033
01034 if ( store->open( "Thumbnails/thumbnail.png" ) )
01035 {
01036 if ( !saveOasisPreview( store, manifestWriter ) || !store->close() ) {
01037 d->lastErrorMessage = i18n( "Error while trying to write '%1'. Partition full?" ).arg( "Thumbnails/thumbnail.png" );
01038 delete store;
01039 return false;
01040 }
01041
01042 }
01043 else
01044 {
01045 d->lastErrorMessage = i18n( "Not able to write '%1'. Partition full?" ).arg( "Thumbnails/thumbnail.png" );
01046 delete store;
01047 return false;
01048 }
01049
01050
01051 if ( !oasisStore.closeManifestWriter() )
01052 {
01053 d->lastErrorMessage = i18n( "Error while trying to write '%1'. Partition full?" ).arg( "META-INF/manifest.xml" );
01054 delete store;
01055 return false;
01056 }
01057
01058 delete store;
01059 }
01060 else
01061 {
01062
01063 if ( !saveChildren( store ) && !oasis )
01064 {
01065 if ( d->lastErrorMessage.isEmpty() )
01066 d->lastErrorMessage = i18n( "Error while saving embedded documents" );
01067 delete store;
01068 return false;
01069 }
01070
01071 kdDebug(30003) << "Saving root" << endl;
01072 if ( store->open( "root" ) )
01073 {
01074 KoStoreDevice dev( store );
01075 if ( !saveToStream( &dev ) || !store->close() )
01076 {
01077 kdDebug(30003) << "saveToStream failed" << endl;
01078 delete store;
01079 return false;
01080 }
01081 }
01082 else
01083 {
01084 d->lastErrorMessage = i18n( "Not able to write '%1'. Partition full?" ).arg( "maindoc.xml" );
01085 delete store;
01086 return false;
01087 }
01088 if ( store->open( "documentinfo.xml" ) )
01089 {
01090 QDomDocument doc = d->m_docInfo->save();
01091 KoStoreDevice dev( store );
01092
01093 QCString s = doc.toCString();
01094 (void)dev.writeBlock( s.data(), s.size()-1 );
01095 (void)store->close();
01096 }
01097
01098 if ( store->open( "preview.png" ) )
01099 {
01100
01101 savePreview( store );
01102 (void)store->close();
01103 }
01104
01105 if ( !completeSaving( store ) )
01106 {
01107 delete store;
01108 return false;
01109 }
01110 kdDebug(30003) << "Saving done of url: " << url().url() << endl;
01111 delete store;
01112 }
01113 if ( !saveExternalChildren() )
01114 {
01115 return false;
01116 }
01117 return true;
01118 }
01119
01120 bool KoDocument::saveToStream( QIODevice * dev )
01121 {
01122 QDomDocument doc = saveXML();
01123
01124 QCString s = doc.toCString();
01125
01126
01127 int nwritten = dev->writeBlock( s.data(), s.size()-1 );
01128 if ( nwritten != (int)s.size()-1 )
01129 kdWarning(30003) << "KoDocument::saveToStream wrote " << nwritten << " - expected " << s.size()-1 << endl;
01130 return nwritten == (int)s.size()-1;
01131 }
01132
01133
01134 bool KoDocument::saveToStore( KoStore* _store, const QString & _path )
01135 {
01136 kdDebug(30003) << "Saving document to store " << _path << endl;
01137
01138
01139 if ( _path.startsWith( STORE_PROTOCOL ) )
01140 m_url = KURL( _path );
01141 else
01142 m_url = KURL( INTERNAL_PREFIX + _path );
01143
01144
01145 _store->pushDirectory();
01146 _store->enterDirectory( _path );
01147
01148
01149 if ( !saveChildren( _store ) )
01150 return false;
01151
01152
01153 if ( _store->open( "root" ) )
01154 {
01155 KoStoreDevice dev( _store );
01156 if ( !saveToStream( &dev ) )
01157 {
01158 _store->close();
01159 return false;
01160 }
01161 if ( !_store->close() )
01162 return false;
01163 }
01164
01165 if ( !completeSaving( _store ) )
01166 return false;
01167
01168
01169 _store->popDirectory();
01170
01171 kdDebug(30003) << "Saved document to store" << endl;
01172
01173 return true;
01174 }
01175
01176 bool KoDocument::saveOasisPreview( KoStore* store, KoXmlWriter* manifestWriter )
01177 {
01178 const QPixmap pix = generatePreview( QSize( 128, 128 ) );
01179 QImage preview ( pix.convertToImage().convertDepth( 32, Qt::ColorOnly ) );
01180 if ( !preview.hasAlphaBuffer() )
01181 {
01182 preview.setAlphaBuffer( true );
01183 }
01184
01185 KoStoreDevice io ( store );
01186 if ( !io.open( IO_WriteOnly ) )
01187 return false;
01188 if ( ! preview.save( &io, "PNG", 0 ) )
01189 return false;
01190 io.close();
01191 manifestWriter->addManifestEntry( "Thumbnails/", "" );
01192 manifestWriter->addManifestEntry( "Thumbnails/thumbnail.png", "" );
01193 return true;
01194 }
01195
01196 bool KoDocument::savePreview( KoStore* store )
01197 {
01198 QPixmap pix = generatePreview(QSize(256, 256));
01199
01200 const QImage preview ( pix.convertToImage().convertDepth( 8, Qt::AvoidDither | Qt::DiffuseDither) );
01201 KoStoreDevice io ( store );
01202 if ( !io.open( IO_WriteOnly ) )
01203 return false;
01204 if ( ! preview.save( &io, "PNG" ) )
01205 return false;
01206 io.close();
01207 return true;
01208 }
01209
01210 QPixmap KoDocument::generatePreview( const QSize& size )
01211 {
01212 double docWidth, docHeight;
01213 int pixmapSize = QMAX(size.width(), size.height());
01214
01215 if (m_pageLayout.ptWidth > 1.0) {
01216 docWidth = m_pageLayout.ptWidth / 72 * KoGlobal::dpiX();
01217 docHeight = m_pageLayout.ptHeight / 72 * KoGlobal::dpiY();
01218
01219 } else {
01220
01221 docWidth = 500.0;
01222 docHeight = 500.0;
01223 }
01224
01225 double ratio = docWidth / docHeight;
01226
01227 QPixmap pix;
01228 int previewWidth, previewHeight;
01229 if (ratio > 1.0)
01230 {
01231 previewWidth = (int) pixmapSize;
01232 previewHeight = (int) (pixmapSize / ratio);
01233 }
01234 else
01235 {
01236 previewWidth = (int) (pixmapSize * ratio);
01237 previewHeight = (int) pixmapSize;
01238 }
01239
01240 pix.resize((int)docWidth, (int)docHeight);
01241
01242 pix.fill( QColor( 245, 245, 245 ) );
01243
01244 QRect rc(0, 0, pix.width(), pix.height());
01245
01246 QPainter p;
01247 p.begin(&pix);
01248 paintEverything(p, rc, false);
01249 p.end();
01250
01251
01252 pix.convertFromImage(pix.convertToImage().smoothScale(previewWidth, previewHeight));
01253
01254 return pix;
01255 }
01256
01257 QString KoDocument::autoSaveFile( const QString & path ) const
01258 {
01259
01260 KMimeType::Ptr mime = KMimeType::mimeType( nativeFormatMimeType() );
01261 QString extension = mime->property( "X-KDE-NativeExtension" ).toString();
01262 if ( path.isEmpty() )
01263 {
01264
01265
01266
01267 QString ret = QDir::homeDirPath() + "/." + QString::fromLatin1(instance()->instanceName()) + ".autosave" + extension;
01268 return ret;
01269 }
01270 else
01271 {
01272 KURL url( path );
01273 Q_ASSERT( url.isLocalFile() );
01274 QString dir = url.directory(false);
01275 QString filename = url.fileName();
01276 return dir + "." + filename + ".autosave" + extension;
01277 }
01278 }
01279
01280 bool KoDocument::checkAutoSaveFile()
01281 {
01282 QString asf = autoSaveFile( QString::null );
01283
01284 if ( QFile::exists( asf ) )
01285 {
01286 QDateTime date = QFileInfo(asf).lastModified();
01287 QString dateStr = date.toString(Qt::LocalDate);
01288 int res = KMessageBox::warningYesNoCancel(
01289 0, i18n( "An autosaved file for an unnamed document exists in %1.\nThis file is dated %2\nDo you want to open it?" )
01290 .arg(asf, dateStr) );
01291 switch(res) {
01292 case KMessageBox::Yes : {
01293 KURL url;
01294 url.setPath( asf );
01295 bool ret = openURL( url );
01296 if ( ret )
01297 resetURL();
01298 return ret;
01299 }
01300 case KMessageBox::No :
01301 QFile::remove( asf );
01302 return false;
01303 default:
01304 return false;
01305 }
01306 }
01307 return false;
01308 }
01309
01310 bool KoDocument::import( const KURL & _url )
01311 {
01312 bool ret;
01313
01314 kdDebug (30003) << "KoDocument::import url=" << _url.url() << endl;
01315 d->m_isImporting = true;
01316
01317
01318 ret = openURL (_url);
01319
01320
01321
01322 if (ret)
01323 {
01324 kdDebug (30003) << "KoDocument::import success, resetting url" << endl;
01325 resetURL ();
01326 setTitleModified ();
01327 }
01328
01329 d->m_isImporting = false;
01330
01331 return ret;
01332 }
01333
01334 bool KoDocument::openURL( const KURL & _url )
01335 {
01336 kdDebug(30003) << "KoDocument::openURL url=" << _url.url() << endl;
01337 d->lastErrorMessage = QString::null;
01338
01339
01340 if ( !_url.isValid() )
01341 {
01342 d->lastErrorMessage = i18n( "Malformed URL\n%1" ).arg( _url.url() );
01343 return false;
01344 }
01345 if ( !closeURL() )
01346 return false;
01347
01348 KURL url( _url );
01349 bool autosaveOpened = false;
01350 d->m_bLoading = true;
01351 if ( url.isLocalFile() && d->m_shouldCheckAutoSaveFile )
01352 {
01353 QString file = url.path();
01354 QString asf = autoSaveFile( file );
01355 if ( QFile::exists( asf ) )
01356 {
01357
01358
01359 int res = KMessageBox::warningYesNoCancel( 0,
01360 i18n( "An autosaved file exists for this document.\nDo you want to open it instead?" ));
01361 switch(res) {
01362 case KMessageBox::Yes :
01363 url.setPath( asf );
01364 autosaveOpened = true;
01365 break;
01366 case KMessageBox::No :
01367 QFile::remove( asf );
01368 break;
01369 default:
01370 d->m_bLoading = false;
01371 return false;
01372 }
01373 }
01374 }
01375
01376 bool ret = KParts::ReadWritePart::openURL( url );
01377
01378 if ( autosaveOpened )
01379 resetURL();
01380 else
01381 {
01382
01383
01384
01385
01386 QPtrListIterator<KoMainWindow> it( d->m_shells );
01387 for (; it.current(); ++it )
01388 it.current()->addRecentURL( _url );
01389 }
01390 return ret;
01391 }
01392
01393 bool KoDocument::openFile()
01394 {
01395
01396 if ( !QFile::exists(m_file) )
01397 {
01398 QApplication::restoreOverrideCursor();
01399 if ( d->m_autoErrorHandlingEnabled )
01400
01401 KMessageBox::error(0L, i18n("The file %1 does not exist.").arg(m_file) );
01402 d->m_bLoading = false;
01403 return false;
01404 }
01405
01406 QApplication::setOverrideCursor( waitCursor );
01407
01408 d->m_specialOutputFlag = 0;
01409 QCString _native_format = nativeFormatMimeType();
01410
01411 KURL u;
01412 u.setPath( m_file );
01413 QString typeName = KMimeType::findByURL( u, 0, true )->name();
01414
01415
01416 if ( typeName == "application/x-trash" )
01417 {
01418 QString path = u.path();
01419 QStringList patterns = KMimeType::mimeType( typeName )->patterns();
01420
01421 for( QStringList::Iterator it = patterns.begin(); it != patterns.end(); ++it ) {
01422 QString ext = *it;
01423 if ( !ext.isEmpty() && ext[0] == '*' )
01424 {
01425 ext.remove(0, 1);
01426 if ( path.endsWith( ext ) ) {
01427 path.truncate( path.length() - ext.length() );
01428 break;
01429 }
01430 }
01431 }
01432 typeName = KMimeType::findByPath( path, 0, true )->name();
01433 }
01434
01435
01436 if ( u.fileName() == "maindoc.xml" || typeName == "inode/directory" )
01437 {
01438 typeName = _native_format;
01439 d->m_specialOutputFlag = SaveAsDirectoryStore;
01440 kdDebug(30003) << "KoDocument::openFile loading maindoc.xml, using directory store for " << m_file << endl;
01441 }
01442 kdDebug(30003) << "KoDocument::openFile " << m_file << " type:" << typeName << endl;
01443
01444 QString importedFile = m_file;
01445
01446 if ( typeName == KMimeType::defaultMimeType() ) {
01447 kdError(30003) << "No mimetype found for " << m_file << endl;
01448 QApplication::restoreOverrideCursor();
01449 if ( d->m_autoErrorHandlingEnabled )
01450 KMessageBox::error( 0L, i18n( "Could not open\n%1" ).arg( url().prettyURL( 0, KURL::StripFileProtocol ) ) );
01451 d->m_bLoading = false;
01452 return false;
01453 }
01454
01455 if ( !isNativeFormat( typeName.latin1() ) ) {
01456 if ( !d->filterManager )
01457 d->filterManager = new KoFilterManager( this );
01458 KoFilter::ConversionStatus status;
01459 importedFile = d->filterManager->import( m_file, status );
01460 if ( status != KoFilter::OK )
01461 {
01462 QApplication::restoreOverrideCursor();
01463
01464 QString msg;
01465 switch( status )
01466 {
01467 case KoFilter::OK: break;
01468
01469 case KoFilter::CreationError:
01470 msg = i18n( "Creation error" ); break;
01471
01472 case KoFilter::FileNotFound:
01473 msg = i18n( "File not found" ); break;
01474
01475 case KoFilter::StorageCreationError:
01476 msg = i18n( "Cannot create storage" ); break;
01477
01478 case KoFilter::BadMimeType:
01479 msg = i18n( "Bad MIME type" ); break;
01480
01481 case KoFilter::EmbeddedDocError:
01482 msg = i18n( "Error in embedded document" ); break;
01483
01484 case KoFilter::WrongFormat:
01485 msg = i18n( "Format not recognized" ); break;
01486
01487 case KoFilter::NotImplemented:
01488 msg = i18n( "Not implemented" ); break;
01489
01490 case KoFilter::ParsingError:
01491 msg = i18n( "Parsing error" ); break;
01492
01493 case KoFilter::PasswordProtected:
01494 msg = i18n( "Document is password protected" ); break;
01495
01496 case KoFilter::InternalError:
01497 case KoFilter::UnexpectedEOF:
01498 case KoFilter::UnexpectedOpcode:
01499 case KoFilter::StupidError:
01500 case KoFilter::UsageError:
01501 msg = i18n( "Internal error" ); break;
01502
01503 case KoFilter::OutOfMemory:
01504 msg = i18n( "Out of memory" ); break;
01505
01506 case KoFilter::UserCancelled:
01507 case KoFilter::BadConversionGraph:
01508
01509 break;
01510
01511 default: msg = i18n( "Unknown error" ); break;
01512 }
01513
01514 if( d->m_autoErrorHandlingEnabled && !msg.isEmpty())
01515 {
01516 QString errorMsg( i18n( "Could not open\n%2.\nReason: %1" ) );
01517 QString docUrl = url().prettyURL( 0, KURL::StripFileProtocol );
01518 KMessageBox::error( 0L, errorMsg.arg(msg).arg(docUrl) );
01519 }
01520
01521 d->m_bLoading = false;
01522 return false;
01523 }
01524 kdDebug(30003) << "KoDocument::openFile - importedFile '" << importedFile
01525 << "', status: " << static_cast<int>( status ) << endl;
01526 }
01527
01528 QApplication::restoreOverrideCursor();
01529
01530 bool ok = true;
01531
01532 if (!importedFile.isEmpty())
01533 {
01534
01535 if ( !loadNativeFormat( importedFile ) )
01536 {
01537 ok = false;
01538 if ( d->m_autoErrorHandlingEnabled )
01539 {
01540 showLoadingErrorDialog();
01541 }
01542 }
01543 }
01544
01545 if ( importedFile != m_file )
01546 {
01547
01548
01549
01550
01551
01552
01553
01554
01555
01556
01557
01558 #if 0
01559 if ( isReadWrite() )
01560 resetURL();
01561 #endif
01562
01563
01564 if(!importedFile.isEmpty()) {
01565 QFile::remove( importedFile );
01566 }
01567 }
01568
01569 if ( ok && d->m_bSingleViewMode )
01570 {
01571
01572 KXMLGUIFactory* guiFactory = factory();
01573 if( guiFactory )
01574 guiFactory->removeClient( this );
01575
01576 if ( !d->m_views.isEmpty() )
01577 {
01578
01579 KoView* v = d->m_views.first();
01580 if( guiFactory )
01581 guiFactory->removeClient( v );
01582 removeView( v );
01583 delete v;
01584 Q_ASSERT( d->m_views.isEmpty() );
01585 }
01586
01587 KoView *view = createView( d->m_wrapperWidget );
01588 d->m_wrapperWidget->setKoView( view );
01589 view->show();
01590
01591
01592
01593 if ( guiFactory )
01594 guiFactory->addClient( this );
01595 }
01596
01597 if ( ok )
01598 {
01599 setMimeTypeAfterLoading( typeName );
01600 }
01601 d->m_bLoading = false;
01602 return ok;
01603 }
01604
01605
01606 void KoDocument::setMimeTypeAfterLoading( const QString& mimeType )
01607 {
01608 d->mimeType = mimeType.latin1();
01609
01610 d->outputMimeType = d->mimeType;
01611
01612 const bool needConfirm = !isNativeFormat( d->mimeType );
01613 setConfirmNonNativeSave( false, needConfirm );
01614 setConfirmNonNativeSave( true, needConfirm );
01615 }
01616
01617
01618 bool KoDocument::oldLoadAndParse(KoStore* store, const QString& filename, QDomDocument& doc)
01619 {
01620
01621
01622 if (!store->open(filename))
01623 {
01624 kdWarning(30003) << "Entry " << filename << " not found!" << endl;
01625 d->lastErrorMessage = i18n( "Could not find %1" ).arg( filename );
01626 return false;
01627 }
01628
01629 QString errorMsg;
01630 int errorLine, errorColumn;
01631 bool ok = doc.setContent( store->device(), &errorMsg, &errorLine, &errorColumn );
01632 if ( !ok )
01633 {
01634 kdError(30003) << "Parsing error in " << filename << "! Aborting!" << endl
01635 << " In line: " << errorLine << ", column: " << errorColumn << endl
01636 << " Error message: " << errorMsg << endl;
01637 d->lastErrorMessage = i18n( "Parsing error in %1 at line %2, column %3\nError message: %4" )
01638 .arg( filename ).arg( errorLine ).arg( errorColumn )
01639 .arg( i18n ( "QXml", errorMsg.utf8() ) );
01640 store->close();
01641 return false;
01642 }
01643 kdDebug(30003) << "File " << filename << " loaded and parsed" << endl;
01644 return true;
01645 }
01646
01647 bool KoDocument::loadNativeFormat( const QString & file )
01648 {
01649 QFileInfo fileInfo( file );
01650 if ( !fileInfo.exists() )
01651 {
01652 d->lastErrorMessage = i18n("The file %1 does not exist.").arg(file);
01653 return false;
01654 }
01655 if ( !fileInfo.isFile() )
01656 {
01657 d->lastErrorMessage = i18n( "%1 is not a file." ).arg(file);
01658 return false;
01659 }
01660
01661 QApplication::setOverrideCursor( waitCursor );
01662
01663 kdDebug(30003) << "KoDocument::loadNativeFormat( " << file << " )" << endl;
01664
01665 QFile in;
01666 bool isRawXML = false;
01667 if ( d->m_specialOutputFlag != SaveAsDirectoryStore )
01668 {
01669 in.setName(file);
01670 if ( !in.open( IO_ReadOnly ) )
01671 {
01672 QApplication::restoreOverrideCursor();
01673 d->lastErrorMessage = i18n( "Could not open the file for reading (check read permissions)." );
01674 return false;
01675 }
01676
01677
01678 char buf[5];
01679 if ( in.readBlock( buf, 4 ) < 4 )
01680 {
01681 QApplication::restoreOverrideCursor();
01682 in.close();
01683 d->lastErrorMessage = i18n( "Could not read the beginning of the file." );
01684 return false;
01685 }
01686
01687 isRawXML = (strncasecmp( buf, "<?xm", 4 ) == 0);
01688
01689 }
01690
01691 if ( isRawXML )
01692 {
01693 in.at(0);
01694 QString errorMsg;
01695 int errorLine;
01696 int errorColumn;
01697 QDomDocument doc;
01698 bool res;
01699 if ( doc.setContent( &in, &errorMsg, &errorLine, &errorColumn ) )
01700 {
01701 res = loadXML( &in, doc );
01702 if ( res )
01703 res = completeLoading( 0L );
01704 }
01705 else
01706 {
01707 kdError (30003) << "Parsing Error! Aborting! (in KoDocument::loadNativeFormat (QFile))" << endl
01708 << " Line: " << errorLine << " Column: " << errorColumn << endl
01709 << " Message: " << errorMsg << endl;
01710 d->lastErrorMessage = i18n( "parsing error in the main document at line %1, column %2\nError message: %3" )
01711 .arg( errorLine ).arg( errorColumn ).arg( i18n ( errorMsg.utf8() ) );
01712 res=false;
01713 }
01714
01715 QApplication::restoreOverrideCursor();
01716 in.close();
01717 m_bEmpty = false;
01718 return res;
01719 } else
01720 {
01721 in.close();
01722
01723 return loadNativeFormatFromStore( file );
01724 }
01725 }
01726
01727 bool KoDocument::loadNativeFormatFromStore( const QString& file )
01728 {
01729 KoStore::Backend backend = (d->m_specialOutputFlag == SaveAsDirectoryStore) ? KoStore::Directory : KoStore::Auto;
01730 KoStore * store = KoStore::createStore( file, KoStore::Read, "", backend );
01731
01732 if ( store->bad() )
01733 {
01734 d->lastErrorMessage = i18n( "Not a valid KOffice file: %1" ).arg( file );
01735 delete store;
01736 QApplication::restoreOverrideCursor();
01737 return false;
01738 }
01739
01740 bool oasis = true;
01741
01742 if ( store->hasFile( "content.xml" ) )
01743 {
01744 store->disallowNameExpansion();
01745
01746 KoOasisStore oasisStore( store );
01747
01748
01749 if ( !loadOasisFromStore( store ) ) {
01750 delete store;
01751 QApplication::restoreOverrideCursor();
01752 return false;
01753 }
01754
01755 } else if ( store->hasFile( "root" ) )
01756 {
01757 oasis = false;
01758
01759 QDomDocument doc;
01760 bool ok = oldLoadAndParse( store, "root", doc );
01761 if ( ok )
01762 ok = loadXML( store->device(), doc );
01763 if ( !ok )
01764 {
01765 delete store;
01766 QApplication::restoreOverrideCursor();
01767 return false;
01768 }
01769 store->close();
01770
01771 if ( !loadChildren( store ) )
01772 {
01773 kdError(30003) << "ERROR: Could not load children" << endl;
01774
01775 }
01776
01777 } else
01778 {
01779 kdError(30003) << "ERROR: No maindoc.xml" << endl;
01780 d->lastErrorMessage = i18n( "Invalid document: no file 'maindoc.xml'." );
01781 delete store;
01782 QApplication::restoreOverrideCursor();
01783 return false;
01784 }
01785
01786 if ( oasis && store->hasFile( "meta.xml" ) ) {
01787 QDomDocument metaDoc;
01788 KoOasisStore oasisStore( store );
01789 if ( oasisStore.loadAndParse( "meta.xml", metaDoc, d->lastErrorMessage ) ) {
01790 d->m_docInfo->loadOasis( metaDoc );
01791 }
01792 }
01793 else if ( !oasis && store->hasFile( "documentinfo.xml" ) )
01794 {
01795 QDomDocument doc;
01796 if ( oldLoadAndParse( store, "documentinfo.xml", doc ) ) {
01797 store->close();
01798 d->m_docInfo->load( doc );
01799 }
01800 }
01801 else
01802 {
01803
01804 delete d->m_docInfo;
01805 d->m_docInfo = new KoDocumentInfo( this, "document info" );
01806 }
01807
01808 bool res = completeLoading( store );
01809 delete store;
01810 QApplication::restoreOverrideCursor();
01811 m_bEmpty = false;
01812 return res;
01813 }
01814
01815
01816 bool KoDocument::loadFromStore( KoStore* _store, const QString& url )
01817 {
01818 if ( _store->open( url ) )
01819 {
01820 QDomDocument doc;
01821 doc.setContent( _store->device() );
01822 if ( !loadXML( _store->device(), doc ) )
01823 {
01824 _store->close();
01825 return false;
01826 }
01827 _store->close();
01828 } else {
01829 kdWarning() << "couldn't open " << url << endl;
01830 }
01831
01832 _store->pushDirectory();
01833
01834 if ( url.startsWith( STORE_PROTOCOL ) ) {
01835 m_url = KURL( url );
01836 } else {
01837 m_url = KURL( INTERNAL_PREFIX + url );
01838 _store->enterDirectory( url );
01839 }
01840
01841 if ( !loadChildren( _store ) )
01842 {
01843 kdError(30003) << "ERROR: Could not load children" << endl;
01844 #if 0
01845 return false;
01846 #endif
01847 }
01848
01849 bool result = completeLoading( _store );
01850
01851
01852 _store->popDirectory();
01853
01854 return result;
01855 }
01856
01857 bool KoDocument::loadOasisFromStore( KoStore* store )
01858 {
01859 KoOasisStyles oasisStyles;
01860 QDomDocument contentDoc;
01861 QDomDocument settingsDoc;
01862 KoOasisStore oasisStore( store );
01863 bool ok = oasisStore.loadAndParse( "content.xml", contentDoc, d->lastErrorMessage );
01864 if ( !ok )
01865 return false;
01866
01867 QDomDocument stylesDoc;
01868 (void)oasisStore.loadAndParse( "styles.xml", stylesDoc, d->lastErrorMessage );
01869
01870 oasisStyles.createStyleMap( stylesDoc, true );
01871
01872 oasisStyles.createStyleMap( contentDoc, false );
01873
01874
01875
01876
01877
01878
01879
01880
01881 if ( store->hasFile( "settings.xml" ) ) {
01882 (void)oasisStore.loadAndParse( "settings.xml", settingsDoc, d->lastErrorMessage );
01883 }
01884 if ( !loadOasis( contentDoc, oasisStyles, settingsDoc, store ) )
01885 return false;
01886
01887 return true;
01888 }
01889
01890 bool KoDocument::isInOperation() const
01891 {
01892 return d->m_numOperations > 0;
01893 }
01894
01895 void KoDocument::emitBeginOperation()
01896 {
01897
01898
01899 if (!isInOperation())
01900 emit sigBeginOperation();
01901 d->m_numOperations++;
01902 }
01903
01904 void KoDocument::emitEndOperation()
01905 {
01906 d->m_numOperations--;
01907
01908
01909 if (d->m_numOperations == 0)
01910 emit sigEndOperation();
01911 else if (d->m_numOperations < 0)
01912
01913 d->m_numOperations = 0;
01914 }
01915
01916
01917 bool KoDocument::isStoredExtern() const
01918 {
01919 return !storeInternal() && hasExternURL();
01920 }
01921
01922 void KoDocument::setModified( bool mod )
01923 {
01924 if ( isAutosaving() )
01925 return;
01926
01927
01928
01929
01930 if ( mod && !d->modifiedAfterAutosave ) {
01931
01932 setAutoSave( d->m_autoSaveDelay );
01933 }
01934 d->modifiedAfterAutosave = mod;
01935
01936 if ( mod == isModified() )
01937 return;
01938
01939 KParts::ReadWritePart::setModified( mod );
01940
01941 if ( mod ) {
01942 m_bEmpty = FALSE;
01943 } else {
01944
01945 QPtrListIterator<KoDocumentChild> it = children();
01946 for (; it.current(); ++it )
01947 {
01948 KoDocument *doc = it.current()->document();
01949 if ( doc && !it.current()->isStoredExtern() && !it.current()->isDeleted() && doc->isModified() )
01950 doc->setModified( false );
01951 }
01952 }
01953
01954
01955 setTitleModified();
01956 emit modified( mod );
01957 }
01958
01959 void KoDocument::setDoNotSaveExtDoc( bool on )
01960 {
01961 d->m_doNotSaveExtDoc = on;
01962 }
01963
01964 int KoDocument::queryCloseDia()
01965 {
01966
01967
01968 QString name;
01969 if ( documentInfo() )
01970 {
01971 name = documentInfo()->title();
01972 }
01973 if ( name.isEmpty() )
01974 name = url().fileName();
01975
01976 if ( name.isEmpty() )
01977 name = i18n( "Untitled" );
01978
01979 int res = KMessageBox::warningYesNoCancel( 0L,
01980 i18n( "<p>The document <b>'%1'</b> has been modified.</p><p>Do you want to save it?</p>" ).arg(name));
01981
01982 switch(res)
01983 {
01984 case KMessageBox::Yes :
01985 setDoNotSaveExtDoc();
01986 save();
01987 setModified( false );
01988 break;
01989 case KMessageBox::No :
01990 removeAutoSaveFiles();
01991 setModified( false );
01992 break;
01993 default :
01994 return res;
01995 }
01996 return res;
01997 }
01998
01999 int KoDocument::queryCloseExternalChildren()
02000 {
02001
02002 setDoNotSaveExtDoc(false);
02003 QPtrListIterator<KoDocumentChild> it( children() );
02004 for (; it.current(); ++it )
02005 {
02006 if ( !it.current()->isDeleted() )
02007 {
02008 KoDocument *doc = it.current()->document();
02009 if ( doc )
02010 {
02011 bool foo = doc->isStoredExtern();
02012 kdDebug(36001) << "========== isStoredExtern() returned "
02013 << foo << " ==========" << endl;
02014
02015 if ( foo )
02016 {
02017 {
02018 kdDebug(30003)<<k_funcinfo<<" found modified child: "<<doc->url().url()<<" extern="<<doc->isStoredExtern()<<endl;
02019 if ( doc->queryCloseDia() == KMessageBox::Cancel )
02020 return KMessageBox::Cancel;
02021 }
02022 }
02023 if ( doc->queryCloseExternalChildren() == KMessageBox::Cancel )
02024 return KMessageBox::Cancel;
02025 }
02026 }
02027 }
02028 return KMessageBox::Ok;
02029 }
02030
02031 void KoDocument::setTitleModified( const QString caption, bool mod )
02032 {
02033
02034 KoDocument *doc = dynamic_cast<KoDocument *>( parent() );
02035 if ( doc )
02036 {
02037 doc->setTitleModified( caption, mod );
02038 return;
02039 }
02040
02041 QPtrListIterator<KoMainWindow> it( d->m_shells );
02042 for (; it.current(); ++it )
02043 {
02044 it.current()->updateCaption();
02045 it.current()->updateReloadFileAction(this);
02046 it.current()->updateVersionsFileAction(this);
02047 }
02048 }
02049
02050 void KoDocument::setTitleModified()
02051 {
02052
02053 KoDocument *doc = dynamic_cast<KoDocument *>( parent() );
02054 QString caption;
02055 if ( (url().isEmpty() || isStoredExtern()) && d->m_current )
02056 {
02057
02058 if ( documentInfo() )
02059 {
02060 KoDocumentInfoPage * page = documentInfo()->page( QString::fromLatin1("about") );
02061 if (page)
02062 caption = static_cast<KoDocumentInfoAbout *>(page)->title();
02063 }
02064 if ( caption.isEmpty() )
02065 caption = url().prettyURL( 0, KURL::StripFileProtocol );
02066
02067
02068 if ( doc )
02069 {
02070 doc->setTitleModified( caption, isModified() );
02071 return;
02072 }
02073 else
02074 {
02075
02076 setTitleModified( caption, isModified() );
02077 return;
02078 }
02079 }
02080 if ( doc )
02081 {
02082
02083 doc->setTitleModified();
02084 }
02085 }
02086
02087 bool KoDocument::loadChildren( KoStore* )
02088 {
02089 return true;
02090 }
02091
02092 bool KoDocument::completeLoading( KoStore* )
02093 {
02094 return true;
02095 }
02096
02097 bool KoDocument::completeSaving( KoStore* )
02098 {
02099 return true;
02100 }
02101
02102 QDomDocument KoDocument::createDomDocument( const QString& tagName, const QString& version ) const
02103 {
02104 return createDomDocument( instance()->instanceName(), tagName, version );
02105 }
02106
02107
02108 QDomDocument KoDocument::createDomDocument( const QString& appName, const QString& tagName, const QString& version )
02109 {
02110 QDomImplementation impl;
02111 QString url = QString("http://www.koffice.org/DTD/%1-%1.dtd").arg(appName).arg(version);
02112 QDomDocumentType dtype = impl.createDocumentType( tagName,
02113 QString("-//KDE//DTD %1 %1//EN").arg(appName).arg(version),
02114 url );
02115
02116 QString namespaceURN = QString("http://www.koffice.org/DTD/%1").arg(appName);
02117 QDomDocument doc = impl.createDocument( namespaceURN, tagName, dtype );
02118 doc.insertBefore( doc.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"UTF-8\"" ), doc.documentElement() );
02119 return doc;
02120 }
02121
02122 KoXmlWriter* KoDocument::createOasisXmlWriter( QIODevice* dev, const char* rootElementName )
02123 {
02124 KoXmlWriter* writer = new KoXmlWriter( dev );
02125 writer->startDocument( rootElementName );
02126 writer->startElement( rootElementName );
02127 writer->addAttribute( "xmlns:office", KoXmlNS::office );
02128 writer->addAttribute( "xmlns:meta", KoXmlNS::meta );
02129
02130 if ( qstrcmp( rootElementName, "office:document-meta" ) != 0 ) {
02131 writer->addAttribute( "xmlns:config", KoXmlNS::config );
02132 writer->addAttribute( "xmlns:text", KoXmlNS::text );
02133 writer->addAttribute( "xmlns:table", KoXmlNS::table );
02134 writer->addAttribute( "xmlns:draw", KoXmlNS::draw );
02135 writer->addAttribute( "xmlns:presentation", KoXmlNS::presentation );
02136 writer->addAttribute( "xmlns:dr3d", KoXmlNS::dr3d );
02137 writer->addAttribute( "xmlns:chart", KoXmlNS::chart );
02138 writer->addAttribute( "xmlns:form", KoXmlNS::form );
02139 writer->addAttribute( "xmlns:script", KoXmlNS::script );
02140 writer->addAttribute( "xmlns:style", KoXmlNS::style );
02141 writer->addAttribute( "xmlns:number", KoXmlNS::number );
02142 writer->addAttribute( "xmlns:math", KoXmlNS::math );
02143 writer->addAttribute( "xmlns:svg", KoXmlNS::svg );
02144 writer->addAttribute( "xmlns:fo", KoXmlNS::fo );
02145 writer->addAttribute( "xmlns:koffice", KoXmlNS::koffice );
02146 }
02147
02148
02149 writer->addAttribute( "xmlns:dc", KoXmlNS::dc );
02150 writer->addAttribute( "xmlns:xlink", KoXmlNS::xlink );
02151 return writer;
02152 }
02153
02154 QDomDocument KoDocument::saveXML()
02155 {
02156 kdError(30003) << "KoDocument::saveXML not implemented" << endl;
02157 d->lastErrorMessage = i18n( "Internal error: saveXML not implemented" );
02158 return QDomDocument();
02159 }
02160
02161 KService::Ptr KoDocument::nativeService()
02162 {
02163 if ( !m_nativeService )
02164 m_nativeService = readNativeService( instance() );
02165
02166 return m_nativeService;
02167 }
02168
02169 QCString KoDocument::nativeFormatMimeType() const
02170 {
02171 KService::Ptr service = const_cast<KoDocument *>(this)->nativeService();
02172 if ( !service )
02173 return QCString();
02174 return service->property( "X-KDE-NativeMimeType" ).toString().latin1();
02175 }
02176
02177 QCString KoDocument::nativeOasisMimeType() const
02178 {
02179 KService::Ptr service = const_cast<KoDocument *>(this)->nativeService();
02180 if ( !service )
02181 return QCString();
02182 return service->property( "X-KDE-NativeOasisMimeType" ).toString().latin1();
02183 }
02184
02185
02186
02187 KService::Ptr KoDocument::readNativeService( KInstance *instance )
02188 {
02189 QString instname = instance ? instance->instanceName() : kapp->instanceName();
02190
02191
02192 QString servicepartname = instname + "part.desktop";
02193 KService::Ptr service = KService::serviceByDesktopPath( servicepartname );
02194 if ( service )
02195 kdDebug(30003) << servicepartname << " found." << endl;
02196 if ( !service )
02197 {
02198
02199
02200
02201
02202
02203 service = KService::serviceByDesktopPath( QString::fromLatin1("Office/%1.desktop").arg(instname) );
02204 }
02205 if ( !service )
02206 service = KService::serviceByDesktopName( instname );
02207
02208 return service;
02209 }
02210
02211 QCString KoDocument::readNativeFormatMimeType( KInstance *instance )
02212 {
02213 KService::Ptr service = readNativeService( instance );
02214 if ( !service )
02215 return QCString();
02216
02217 if ( service->property( "X-KDE-NativeMimeType" ).toString().isEmpty() )
02218 {
02219
02220 if ( KServiceType::serviceType( "KOfficePart" ) == 0L )
02221 kdError(30003) << "The serviceType KOfficePart is missing. Check that you have a kofficepart.desktop file in the share/servicetypes directory." << endl;
02222 else {
02223 QString instname = instance ? instance->instanceName() : kapp->instanceName();
02224 if ( instname != "koshell" )
02225 kdWarning(30003) << service->desktopEntryPath() << ": no X-KDE-NativeMimeType entry!" << endl;
02226 }
02227 }
02228
02229 return service->property( "X-KDE-NativeMimeType" ).toString().latin1();
02230 }
02231
02232 QStringList KoDocument::readExtraNativeMimeTypes( KInstance *instance )
02233 {
02234 KService::Ptr service = readNativeService( instance );
02235 if ( !service )
02236 return QStringList();
02237 return service->property( "X-KDE-ExtraNativeMimeTypes" ).toStringList();
02238 }
02239
02240 void KoDocument::setupXmlReader( QXmlSimpleReader& reader, bool namespaceProcessing )
02241 {
02242 if ( namespaceProcessing )
02243 {
02244 reader.setFeature( "http://xml.org/sax/features/namespaces", TRUE );
02245 reader.setFeature( "http://xml.org/sax/features/namespace-prefixes", FALSE );
02246 }
02247 else
02248 {
02249 reader.setFeature( "http://xml.org/sax/features/namespaces", FALSE );
02250 reader.setFeature( "http://xml.org/sax/features/namespace-prefixes", TRUE );
02251 }
02252 reader.setFeature( "http://trolltech.com/xml/features/report-whitespace-only-CharData", TRUE );
02253 }
02254
02255
02256 bool KoDocument::isNativeFormat( const QCString& mimetype ) const
02257 {
02258 if ( mimetype == nativeFormatMimeType() )
02259 return true;
02260 return extraNativeMimeTypes().contains( mimetype );
02261 }
02262
02263 QStringList KoDocument::extraNativeMimeTypes() const
02264 {
02265 QStringList lst;
02266
02267
02268
02269 KService::Ptr service = const_cast<KoDocument *>(this)->nativeService();
02270 if ( !service )
02271 return lst;
02272 return service->property( "X-KDE-ExtraNativeMimeTypes" ).toStringList();
02273 }
02274
02275 int KoDocument::supportedSpecialFormats() const
02276 {
02277
02278
02279
02280 return SaveAsDirectoryStore;
02281 }
02282
02283 void KoDocument::addShell( KoMainWindow *shell )
02284 {
02285 if ( d->m_shells.findRef( shell ) == -1 )
02286 {
02287
02288 d->m_shells.append( shell );
02289 }
02290 }
02291
02292 void KoDocument::removeShell( KoMainWindow *shell )
02293 {
02294
02295 d->m_shells.removeRef( shell );
02296 }
02297
02298 const QPtrList<KoMainWindow>& KoDocument::shells() const
02299 {
02300 return d->m_shells;
02301 }
02302
02303 int KoDocument::shellCount() const
02304 {
02305 return d->m_shells.count();
02306 }
02307
02308 DCOPObject * KoDocument::dcopObject()
02309 {
02310 if ( !d->m_dcopObject )
02311 d->m_dcopObject = new KoDocumentIface( this );
02312 return d->m_dcopObject;
02313 }
02314
02315 QCString KoDocument::dcopObjectId() const
02316 {
02317 return const_cast<KoDocument *>(this)->dcopObject()->objId();
02318 }
02319
02320 void KoDocument::setErrorMessage( const QString& errMsg )
02321 {
02322 d->lastErrorMessage = errMsg;
02323 }
02324
02325 QString KoDocument::errorMessage() const
02326 {
02327 return d->lastErrorMessage;
02328 }
02329
02330 void KoDocument::showSavingErrorDialog()
02331 {
02332 if ( d->lastErrorMessage.isEmpty() )
02333 {
02334 KMessageBox::error( 0L, i18n( "Could not save\n%1" ).arg( m_file ) );
02335 }
02336 else if ( d->lastErrorMessage != "USER_CANCELED" )
02337 {
02338 KMessageBox::error( 0L, i18n( "Could not save %1\nReason: %2" ).arg( m_file, d->lastErrorMessage ) );
02339 }
02340 }
02341
02342 void KoDocument::showLoadingErrorDialog()
02343 {
02344 if ( d->lastErrorMessage.isEmpty() )
02345 {
02346 KMessageBox::error( 0L, i18n( "Could not open\n%1" ).arg( url().prettyURL( 0, KURL::StripFileProtocol ) ) );
02347 }
02348 else if ( d->lastErrorMessage != "USER_CANCELED" )
02349 {
02350 KMessageBox::error( 0L, i18n( "Could not open %1\nReason: %2" ).arg( url().prettyURL( 0, KURL::StripFileProtocol ), d->lastErrorMessage ) );
02351 }
02352 }
02353
02354 bool KoDocument::isAutosaving() const
02355 {
02356 return d->m_autosaving;
02357 }
02358
02359 bool KoDocument::isLoading() const
02360 {
02361 return d->m_bLoading;
02362 }
02363
02364 void KoDocument::removeAutoSaveFiles()
02365 {
02366
02367 QString asf = autoSaveFile( m_file );
02368 if ( QFile::exists( asf ) )
02369 QFile::remove( asf );
02370 asf = autoSaveFile( QString::null );
02371 if ( QFile::exists( asf ) )
02372 QFile::remove( asf );
02373 }
02374
02375 void KoDocument::setBackupFile( bool _b )
02376 {
02377 d->m_backupFile = _b;
02378 }
02379
02380 bool KoDocument::backupFile()const
02381 {
02382 return d->m_backupFile;
02383 }
02384
02385
02386 void KoDocument::setBackupPath( const QString & _path)
02387 {
02388 d->m_backupPath = _path;
02389 }
02390
02391 QString KoDocument::backupPath()const
02392 {
02393 return d->m_backupPath;
02394 }
02395
02396 void KoDocument::setCurrent( bool on )
02397 {
02398
02399 KoDocument *doc = dynamic_cast<KoDocument *>( parent() );
02400 if ( doc )
02401 {
02402 if ( !isStoredExtern() )
02403 {
02404
02405 doc->setCurrent( true );
02406 return;
02407 }
02408
02409 d->m_current = on;
02410 if ( !on )
02411 {
02412 doc->setCurrent( true );
02413 return;
02414 }
02415 doc->forceCurrent( false );
02416 }
02417 else
02418 d->m_current = on;
02419
02420 setTitleModified();
02421 }
02422
02423 void KoDocument::forceCurrent( bool on )
02424 {
02425
02426 d->m_current = on;
02427 KoDocument *doc = dynamic_cast<KoDocument *>( parent() );
02428 if ( doc )
02429 {
02430 doc->forceCurrent( false );
02431 }
02432 }
02433
02434 bool KoDocument::isCurrent() const
02435 {
02436 return d->m_current;
02437 }
02438
02439 bool KoDocument::storeInternal() const
02440 {
02441 return d->m_storeInternal;
02442 }
02443
02444 void KoDocument::setStoreInternal( bool i )
02445 {
02446 d->m_storeInternal = i;
02447
02448 }
02449
02450 bool KoDocument::hasExternURL() const
02451 {
02452 return !url().protocol().isEmpty() && url().protocol() != STORE_PROTOCOL && url().protocol() != INTERNAL_PROTOCOL;
02453 }
02454
02455 void KoDocument::slotStarted( KIO::Job* job )
02456 {
02457 if ( job )
02458 {
02459 job->setWindow( d->m_shells.current() );
02460 }
02461 }
02462
02463 static const struct {
02464 const char* localName;
02465 const char* documentType;
02466 } TN2DTArray[] = {
02467 { "text", I18N_NOOP( "a word processing" ) },
02468 { "spreadsheet", I18N_NOOP( "a spreadsheet" ) },
02469 { "presentation", I18N_NOOP( "a presentation" ) },
02470 { "chart", I18N_NOOP( "a chart" ) },
02471 { "drawing", I18N_NOOP( "a drawing" ) }
02472 };
02473 static const unsigned int numTN2DT = sizeof( TN2DTArray ) / sizeof( *TN2DTArray );
02474
02475 QString KoDocument::tagNameToDocumentType( const QString& localName )
02476 {
02477 for ( unsigned int i = 0 ; i < numTN2DT ; ++i )
02478 if ( localName == TN2DTArray[i].localName )
02479 return i18n( TN2DTArray[i].documentType );
02480 return localName;
02481 }
02482
02483 QValueList<KoTextDocument *> KoDocument::allTextDocuments() const
02484 {
02485 return QValueList<KoTextDocument *>();
02486 }
02487
02488 KoPageLayout KoDocument::pageLayout(int ) const
02489 {
02490 return m_pageLayout;
02491 }
02492
02493 KoUnit::Unit KoDocument::unit() const
02494 {
02495 return d->m_unit;
02496 }
02497
02498 void KoDocument::setUnit( KoUnit::Unit unit )
02499 {
02500 if ( d->m_unit != unit )
02501 {
02502 d->m_unit = unit;
02503 emit unitChanged( unit );
02504 }
02505 }
02506
02507 QString KoDocument::unitName() const
02508 {
02509 return KoUnit::unitName( unit() );
02510 }
02511
02512 void KoDocument::showStartUpWidget( KoMainWindow* parent, bool alwaysShow )
02513 {
02514 if(!alwaysShow) {
02515 KConfigGroup cfgGrp( instance()->config(), "TemplateChooserDialog" );
02516 QString fullTemplateName = cfgGrp.readPathEntry( "AlwaysUseTemplate" );
02517
02518 if( !fullTemplateName.isEmpty() ) {
02519 openTemplate( fullTemplateName );
02520 shells().getFirst()->setRootDocument( this );
02521 return;
02522 }
02523 }
02524
02525 if(d->m_startUpWidget){
02526 d->m_startUpWidget->show();
02527 } else {
02528 d->m_startUpWidget = createOpenPane( parent->centralWidget(), instance(), templateType() );
02529 }
02530
02531 parent->setDocToOpen( this );
02532 parent->factory()->container("mainToolBar", parent)->hide();
02533 }
02534
02535 void KoDocument::openExistingFile( const QString& file )
02536 {
02537 KURL url( file );
02538 bool ok = openURL( url );
02539 setModified( false );
02540
02541 if( ok )
02542 QTimer::singleShot( 0, this, SLOT( deleteOpenPane() ) );
02543 }
02544
02545 void KoDocument::openTemplate( const QString& file )
02546 {
02547 bool ok = loadNativeFormat( file );
02548 setModified( false );
02549
02550 if ( ok ) {
02551 deleteOpenPane();
02552 resetURL();
02553 setEmpty();
02554 } else {
02555 showLoadingErrorDialog();
02556 initEmpty();
02557 }
02558 }
02559
02560 void KoDocument::initEmpty()
02561 {
02562 setEmpty();
02563 setModified(false);
02564 }
02565
02566 void KoDocument::startCustomDocument() {
02567 deleteOpenPane();
02568 }
02569
02570 KoOpenPane* KoDocument::createOpenPane( QWidget* parent, KInstance* instance,
02571 const QString& templateType )
02572 {
02573 KoOpenPane* openPane = new KoOpenPane( parent, instance, templateType );
02574 QWidget *customDoc = createCustomDocumentWidget(openPane);
02575 if(customDoc) {
02576 openPane->setCustomDocumentWidget( customDoc );
02577 connect( customDoc, SIGNAL( documentSelected() ), this, SLOT( startCustomDocument() ) );
02578 }
02579 openPane->show();
02580
02581 connect( openPane, SIGNAL( openExistingFile( const QString& ) ),
02582 this, SLOT( openExistingFile( const QString& ) ) );
02583 connect( openPane, SIGNAL( openTemplate( const QString& ) ),
02584 this, SLOT( openTemplate( const QString& ) ) );
02585
02586 return openPane;
02587 }
02588
02589 void KoDocument::setTemplateType( const QString& _templateType )
02590 {
02591 d->m_templateType = _templateType;
02592 }
02593
02594 QString KoDocument::templateType() const
02595 {
02596 return d->m_templateType;
02597 }
02598
02599 void KoDocument::deleteOpenPane()
02600 {
02601 if( d->m_startUpWidget ) {
02602 d->m_startUpWidget->hide();
02603 QTimer::singleShot(1000, this, SLOT(deleteOpenPaneDelayed()));
02604
02605 shells().getFirst()->factory()->container("mainToolBar", shells().getFirst())->show();
02606 shells().getFirst()->setRootDocument( this );
02607 } else {
02608 emit closeEmbedInitDialog();
02609 }
02610 }
02611
02612 void KoDocument::deleteOpenPaneDelayed()
02613 {
02614 delete d->m_startUpWidget;
02615 d->m_startUpWidget = 0;
02616 }
02617
02618 QWidget* KoDocument::createCustomDocumentWidget(QWidget *) {
02619 return 0;
02620 }
02621
02622 bool KoDocument::showEmbedInitDialog(QWidget* parent)
02623 {
02624 KDialogBase dlg(parent, "EmbedInitDialog", true, i18n("Embedding Object"), 0, KDialogBase::NoDefault);
02625 KoOpenPane* pane = createOpenPane(&dlg, instance(), templateType());
02626 pane->layout()->setMargin(0);
02627 dlg.setMainWidget(pane);
02628 dlg.setInitialSize(dlg.configDialogSize("EmbedInitDialog"));
02629 connect(this, SIGNAL(closeEmbedInitDialog()), &dlg, SLOT(slotOk()));
02630
02631 bool ok = dlg.exec() == QDialog::Accepted;
02632
02633 dlg.saveDialogSize("EmbedInitDialog");
02634
02635 return ok;
02636 }
02637
02638 #include "KoDocument_p.moc"
02639 #include "KoDocument.moc"