KoDocument.h 24.6 KB
Newer Older
Casper Boemann's avatar
Casper Boemann committed
1 2 3
/* This file is part of the KDE project
   Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
   Copyright (C) 2000-2005 David Faure <faure@kde.org>
4
   Copyright (C) 2007 Thorsten Zachmann <zachmann@kde.org>
Halla Rempt's avatar
Halla Rempt committed
5
   Copyright (C) 2010 Boudewijn Rempt <boud@kogmbh.com>
6

Casper Boemann's avatar
Casper Boemann committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
   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.
*/

Thomas Zander's avatar
Thomas Zander committed
23 24
#ifndef KODOCUMENT_H
#define KODOCUMENT_H
Casper Boemann's avatar
Casper Boemann committed
25

26 27
#include <QDateTime>
#include <QTransform>
Halla Rempt's avatar
Halla Rempt committed
28
#include <QList>
Thomas Zander's avatar
Thomas Zander committed
29

Casper Boemann's avatar
Casper Boemann committed
30 31
#include <kcomponentdata.h>

32 33
#include <KoUnit.h>
#include <KoPageLayout.h>
Thomas Zander's avatar
Thomas Zander committed
34 35 36
#include "komain_export.h"
#include "KoGridData.h"
#include "KoGuidesData.h"
Casper Boemann's avatar
Casper Boemann committed
37
#include <KoXmlReader.h>
38
#include <KoOdfDocument.h>
39 40
#include <kundo2stack.h>

41 42
#include <klocale.h>

43
class KisSketchPart;
Alexander Potashev's avatar
Alexander Potashev committed
44
class KUndo2Command;
45
class KoPart;
Casper Boemann's avatar
Casper Boemann committed
46 47
class KoStore;
class KoDocumentInfo;
48 49
class KoDocumentRdf;
class KoDocumentRdfBase;
50
class KoProgressUpdater;
51
class KoProgressProxy;
52
class KoDocumentInfoDlg;
Casper Boemann's avatar
Casper Boemann committed
53 54 55 56 57 58 59 60 61 62 63 64 65

class KoVersionInfo
{
public:
    QDateTime date;
    QString saved_by;
    QString comment;
    QString title;

    QByteArray data; //the content of the compressed version
};

/**
Halla Rempt's avatar
Halla Rempt committed
66
 *  The %Calligra document class
Casper Boemann's avatar
Casper Boemann committed
67
 *
Halla Rempt's avatar
Halla Rempt committed
68
 *  This class provides some functionality each %Calligra document should have.
Casper Boemann's avatar
Casper Boemann committed
69
 *
Halla Rempt's avatar
Halla Rempt committed
70
 *  @short The %Calligra document class
Casper Boemann's avatar
Casper Boemann committed
71
 */
72
class KOMAIN_EXPORT KoDocument : public QObject, public KoOdfDocument
Casper Boemann's avatar
Casper Boemann committed
73 74
{
    Q_OBJECT
75
    Q_PROPERTY(bool backupFile READ backupFile WRITE setBackupFile)
76
    Q_PROPERTY(int pageCount READ pageCount)
Casper Boemann's avatar
Casper Boemann committed
77 78 79 80

public:

    /**
81
     * Constructor.
Casper Boemann's avatar
Casper Boemann committed
82
     *
83
     * @param parent The KoPart that owns the document. XXX: should be removed!
84 85 86
     * @param undoStack accepts the stack for the document. You can create any type of stack if you need.
     *        The stack objects will become owned by the document. This is used by Krita's KisDoc2. The default value for this
     *        parameter is a usual Qt's stack.
Casper Boemann's avatar
Casper Boemann committed
87
     */
88 89
    explicit KoDocument(KoPart *parent,
                        KUndo2Stack *undoStack = new KUndo2Stack());
Casper Boemann's avatar
Casper Boemann committed
90 91 92 93 94 95 96 97 98

    /**
     *  Destructor.
     *
     * The destructor does not delete any attached KoView objects and it does not
     * delete the attached widget as returned by widget().
     */
    virtual ~KoDocument();

99
    /// XXX: Temporary!
100
    KoPart *documentPart() const;
101

Casper Boemann's avatar
Casper Boemann committed
102
    /**
103
     * Reimplemented from KoParts::ReadWritePart for internal reasons
Casper Boemann's avatar
Casper Boemann committed
104 105
     * (for the autosave functionality)
     */
106
    virtual bool openUrl(const KUrl & url);
Casper Boemann's avatar
Casper Boemann committed
107 108 109 110

    /**
     * Opens the document given by @p url, without storing the URL
     * in the KoDocument.
111
     * Call this instead of openUrl() to implement KoMainWindow's
Casper Boemann's avatar
Casper Boemann committed
112 113
     * File --> Import feature.
     *
114 115
     * @note This will call openUrl(). To differentiate this from an ordinary
     *       Open operation (in any reimplementation of openUrl() or openFile())
Casper Boemann's avatar
Casper Boemann committed
116 117
     *       call isImporting().
     */
118
    bool importDocument(const KUrl &url);
Casper Boemann's avatar
Casper Boemann committed
119 120 121 122

    /**
     * Saves the document as @p url without changing the state of the
     * KoDocument (URL, modified flag etc.). Call this instead of
123
     * KoParts::ReadWritePart::saveAs() to implement KoMainWindow's
Casper Boemann's avatar
Casper Boemann committed
124 125 126 127 128 129
     * File --> Export feature.
     *
     * @note This will call KoDocument::saveAs(). To differentiate this
     *       from an ordinary Save operation (in any reimplementation of
     *       saveFile()) call isExporting().
     */
130
    bool exportDocument(const KUrl &url);
Casper Boemann's avatar
Casper Boemann committed
131 132 133 134 135 136 137 138

    /**
     * @brief Sets whether the document can be edited or is read only.
     *
     * This recursively applied to all child documents and
     * KoView::updateReadWrite is called for every attached
     * view.
     */
139
    virtual void setReadWrite(bool readwrite = true);
Casper Boemann's avatar
Casper Boemann committed
140 141 142 143 144

    /**
     * To be preferred when a document exists. It is fast when calling
     * it multiple times since it caches the result that readNativeFormatMimeType()
     * delivers.
145
     * This comes from the X-KDE-NativeMimeType key in the .desktop file.
Casper Boemann's avatar
Casper Boemann committed
146
     */
147
    virtual QByteArray nativeFormatMimeType() const = 0;
Casper Boemann's avatar
Casper Boemann committed
148 149 150

    /**
     * Returns the OASIS OpenDocument mimetype of the document, if supported
151 152 153 154
     * This comes from the X-KDE-NativeOasisMimeType key in the
     * desktop file
     *
     * @return the oasis mimetype or, if it hasn't one, the nativeformatmimetype.
Casper Boemann's avatar
Casper Boemann committed
155
     */
156
    virtual QByteArray nativeOasisMimeType() const = 0;
157

Casper Boemann's avatar
Casper Boemann committed
158
    /// Checks whether a given mimetype can be handled natively.
159
    bool isNativeFormat(const QByteArray& mimetype) const;
Casper Boemann's avatar
Casper Boemann committed
160 161 162

    /// Returns a list of the mimetypes considered "native", i.e. which can
    /// be saved by KoDocument without a filter, in *addition* to the main one
163
    virtual QStringList extraNativeMimeTypes() const = 0;
Casper Boemann's avatar
Casper Boemann committed
164 165

    /// Enum values used by specialOutputFlag - note that it's a bitfield for supportedSpecialFormats
Halla Rempt's avatar
Halla Rempt committed
166
    enum { /*SaveAsCalligra1dot1 = 1,*/ // old and removed
167 168 169 170 171
        SaveAsDirectoryStore = 2,
        SaveAsFlatXML = 4,
        SaveEncrypted = 8
                        // bitfield! next value is 16
    };
Casper Boemann's avatar
Casper Boemann committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

    /**
     * Return the set of SupportedSpecialFormats that the application wants to
     * offer in the "Save" file dialog.
     */
    virtual int supportedSpecialFormats() const;

    /**
     * Returns the actual mimetype of the document
     */
    QByteArray mimeType() const;

    /**
     * @brief Sets the mime type for the document.
     *
     * When choosing "save as" this is also the mime type
     * selected by default.
     */
190
    void setMimeType(const QByteArray & mimeType);
Casper Boemann's avatar
Casper Boemann committed
191 192 193 194 195 196 197 198 199 200

    /**
     * @brief Set the format in which the document should be saved.
     *
     * This is called on loading, and in "save as", so you shouldn't
     * have to call it.
     *
     * @param mimeType the mime type (format) to use.
     * @param specialOutputFlag is for "save as older version" etc.
     */
201
    void setOutputMimeType(const QByteArray & mimeType, int specialOutputFlag = 0);
Casper Boemann's avatar
Casper Boemann committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    QByteArray outputMimeType() const;
    int specialOutputFlag() const;

    /**
     * Returns true if this document was the result of opening a foreign
     * file format and if the user hasn't yet saved the document (in any
     * format).
     *
     * Used by KoMainWindow to warn the user when s/he lazily presses
     * CTRL+S to save in the same foreign format, putting all his/her
     * formatting at risk (normally an export confirmation only comes up
     * with Save As).
     *
     * @param exporting specifies whether this is the setting for a
     * File --> Export or File --> Save/Save As operation.
     */
218 219
    bool confirmNonNativeSave(const bool exporting) const;
    void setConfirmNonNativeSave(const bool exporting, const bool on);
Casper Boemann's avatar
Casper Boemann committed
220

221 222 223 224 225 226 227 228 229 230 231

    /**
     * @return true if saving/exporting should inhibit the option dialog
     */
    bool saveInBatchMode() const;

    /**
     * @param batchMode if true, do not show the option dialog when saving or exporting.
     */
    void setSaveInBatchMode(const bool batchMode);

Casper Boemann's avatar
Casper Boemann committed
232 233 234
    /**
     * Sets the error message to be shown to the user (use i18n()!)
     * when loading or saving fails.
235
     * If you asked the user about something and they chose "Cancel",
Casper Boemann's avatar
Casper Boemann committed
236 237
     * set the message to the magic string "USER_CANCELED", to skip the error dialog.
     */
238
    void setErrorMessage(const QString& errMsg);
Casper Boemann's avatar
Casper Boemann committed
239 240 241 242 243 244 245

    /**
     * Return the last error message. Usually KoDocument takes care of
     * showing it; this method is mostly provided for non-interactive use.
     */
    QString errorMessage() const;

246 247 248 249 250 251 252 253 254 255

    /**
     * Show the last error message in a message box.
     * The dialog box will mention a loading problem.
     * openUrl/openFile takes care of doing it, but not loadNativeFormat itself,
     * so this is often called after loadNativeFormat returned false.
     */
    void showLoadingErrorDialog();


Casper Boemann's avatar
Casper Boemann committed
256 257 258 259
    /**
     * @brief Generates a preview picture of the document
     * @note The preview is used in the File Dialog and also to create the Thumbnail
     */
260
    virtual QPixmap generatePreview(const QSize& size);
Casper Boemann's avatar
Casper Boemann committed
261 262

    /**
Halla Rempt's avatar
Halla Rempt committed
263
     *  Paints the data itself.
Halla Rempt's avatar
Halla Rempt committed
264
     *  It's this method that %Calligra Parts have to implement.
Casper Boemann's avatar
Casper Boemann committed
265 266 267 268
     *
     *  @param painter     The painter object onto which will be drawn.
     *  @param rect        The rect that should be used in the painter object.
     */
269
    virtual void paintContent(QPainter &painter, const QRect &rect) = 0;
Casper Boemann's avatar
Casper Boemann committed
270 271 272 273 274 275

    /**
     *  Tells the document that its title has been modified, either because
     *  the modified status changes (this is done by setModified() ) or
     *  because the URL or the document-info's title changed.
     */
276
    void setTitleModified();
Casper Boemann's avatar
Casper Boemann committed
277 278 279 280

    /**
     *  @return true if the document is empty.
     */
Halla Rempt's avatar
Halla Rempt committed
281
    virtual bool isEmpty() const;
Casper Boemann's avatar
Casper Boemann committed
282 283 284 285 286 287 288 289 290

    /**
     *  @brief Sets the document to empty.
     *
     *  Used after loading a template
     *  (which is not empty, but not the user's input).
     *
     *  @see isEmpty()
     */
Halla Rempt's avatar
Halla Rempt committed
291
    virtual void setEmpty();
Casper Boemann's avatar
Casper Boemann committed
292 293 294 295 296 297 298 299 300

    /**
     *  @brief Loads a document from a store.
     *
     *  You should never have to reimplement.
     *
     *  @param store The store to load from
     *  @param url An internal url, like tar:/1/2
     */
Jos van den Oever's avatar
Jos van den Oever committed
301
    virtual bool loadFromStore(KoStore *store, const QString& url);
Casper Boemann's avatar
Casper Boemann committed
302 303 304 305 306

    /**
     *  @brief Loads an OASIS document from a store.
     *  This is used for both the main document and embedded objects.
     */
Jos van den Oever's avatar
Jos van den Oever committed
307
    virtual bool loadOasisFromStore(KoStore *store);
Casper Boemann's avatar
Casper Boemann committed
308 309

    /**
310
     *  @brief Saves a sub-document to a store.
Casper Boemann's avatar
Casper Boemann committed
311
     *
312
     *  You should not have to reimplement this.
Casper Boemann's avatar
Casper Boemann committed
313
     */
Jos van den Oever's avatar
Jos van den Oever committed
314
    virtual bool saveToStore(KoStore *store, const QString& path);
Casper Boemann's avatar
Casper Boemann committed
315 316

    /**
Halla Rempt's avatar
Halla Rempt committed
317
     *  Reimplement this method to load the contents of your Calligra document,
Casper Boemann's avatar
Casper Boemann committed
318 319
     *  from the XML document. This is for the pre-Oasis file format (maindoc.xml).
     */
Jos van den Oever's avatar
Jos van den Oever committed
320
    virtual bool loadXML(const KoXmlDocument & doc, KoStore *store) = 0;
Casper Boemann's avatar
Casper Boemann committed
321 322 323


    /**
Halla Rempt's avatar
Halla Rempt committed
324
     *  Reimplement this to save the contents of the %Calligra document into
Casper Boemann's avatar
Casper Boemann committed
325 326 327 328 329 330 331 332 333 334
     *  a QDomDocument. The framework takes care of saving it to the store.
     */
    virtual QDomDocument saveXML();

    /**
     *  Return a correctly created QDomDocument for this KoDocument,
     *  including processing instruction, complete DOCTYPE tag (with systemId and publicId), and root element.
     *  @param tagName the name of the tag for the root element
     *  @param version the DTD version (usually the application's version).
     */
Halla Rempt's avatar
Halla Rempt committed
335
    QDomDocument createDomDocument(const QString& tagName, const QString& version) const;
Casper Boemann's avatar
Casper Boemann committed
336 337

    /**
Halla Rempt's avatar
Halla Rempt committed
338
     *  Return a correctly created QDomDocument for an old (1.3-style) %Calligra document,
Casper Boemann's avatar
Casper Boemann committed
339 340
     *  including processing instruction, complete DOCTYPE tag (with systemId and publicId), and root element.
     *  This static method can be used e.g. by filters.
341 342
     *  @param appName the app's instance name, e.g. words, kspread, kpresenter etc.
     *  @param tagName the name of the tag for the root element, e.g. DOC for words/kpresenter.
Casper Boemann's avatar
Casper Boemann committed
343 344
     *  @param version the DTD version (usually the application's version).
     */
345
    static QDomDocument createDomDocument(const QString& appName, const QString& tagName, const QString& version);
Casper Boemann's avatar
Casper Boemann committed
346 347 348 349 350 351 352

    /**
     *  The first thing to do in loadOasis is get hold of the office:body tag, then its child.
     *  If the child isn't the expected one, the error message can indicate what it is instead.
     *  This method returns a translated name for the type of document,
     *  e.g. i18n("Word Processing") for office:text.
     */
353
    static QString tagNameToDocumentType(const QString& localName);
Casper Boemann's avatar
Casper Boemann committed
354 355 356 357 358 359 360

    /**
     *  Loads a document in the native format from a given URL.
     *  Reimplement if your native format isn't XML.
     *
     *  @param file the file to load - usually KReadOnlyPart::m_file or the result of a filter
     */
361
    virtual bool loadNativeFormat(const QString & file);
Casper Boemann's avatar
Casper Boemann committed
362 363 364 365 366 367

    /**
     *  Saves the document in native format, to a given file
     *  You should never have to reimplement.
     *  Made public for writing templates.
     */
368
    virtual bool saveNativeFormat(const QString & file);
Casper Boemann's avatar
Casper Boemann committed
369

370 371 372 373 374 375 376 377 378 379
    /**
     * Saves the document in native ODF format to the given store.
     */
    bool saveNativeFormatODF(KoStore *store, const QByteArray &mimeType);

    /**
     * Saves the document in the native format to the given store.
     */
    bool saveNativeFormatCalligra(KoStore *store);

Casper Boemann's avatar
Casper Boemann committed
380 381 382 383
    /**
     * Activate/deactivate/configure the autosave feature.
     * @param delay in seconds, 0 to disable
     */
384
    void setAutoSave(int delay);
Casper Boemann's avatar
Casper Boemann committed
385 386 387 388 389 390 391

    /**
     * Checks whether the document is currently in the process of autosaving
     */
    bool isAutosaving() const;

    /**
392
     * Set whether the next openUrl call should check for an auto-saved file
Casper Boemann's avatar
Casper Boemann committed
393
     * and offer to open it. This is usually true, but can be turned off
394 395
     * (e.g. for the preview module). This only checks for names auto-saved
     * files, unnamed auto-saved files are only checked on KoApplication startup.
Casper Boemann's avatar
Casper Boemann committed
396
     */
397
    void setCheckAutoSaveFile(bool b);
Casper Boemann's avatar
Casper Boemann committed
398 399

    /**
400
     * Set whether the next openUrl call should show error message boxes in case
Casper Boemann's avatar
Casper Boemann committed
401 402 403
     * of errors. This is usually the case, but e.g. not when generating thumbnail
     * previews.
     */
404
    void setAutoErrorHandlingEnabled(bool b);
Casper Boemann's avatar
Casper Boemann committed
405 406 407 408 409 410 411 412 413 414

    /**
     * Checks whether error message boxes should be shown.
     */
    bool isAutoErrorHandlingEnabled() const;

    /**
     * Retrieve the default value for autosave in seconds.
     * Called by the applications to use the correct default in their config
     */
Thomas Zander's avatar
Thomas Zander committed
415
    static int defaultAutoSave();
Casper Boemann's avatar
Casper Boemann committed
416 417 418 419 420 421 422

    /**
     * @return the information concerning this document.
     * @see KoDocumentInfo
     */
    KoDocumentInfo *documentInfo() const;

423 424 425 426 427 428
    /**
     * @return the Rdf metadata for this document.
     * This method should only be used by code that links to
     * the RDF system and needs full access to the KoDocumentRdf object.
     * @see KoDocumentRdf
     */
Halla Rempt's avatar
Halla Rempt committed
429
    KoDocumentRdfBase *documentRdf() const;
Halla Rempt's avatar
Halla Rempt committed
430 431

    /**
Halla Rempt's avatar
Fix dox  
Halla Rempt committed
432 433 434
     * Replace the current rdf document with the given rdf document. The existing RDF document
     * will be deleted, and if RDF support is compiled out, KoDocument does not take ownership.
     * Otherwise, KoDocument will own the rdf document.
Halla Rempt's avatar
Halla Rempt committed
435
     */
436
    void setDocumentRdf(KoDocumentRdfBase *rdfDocument);
437

438 439 440 441 442
    /**
     * @return the object to report progress to.
     * One can add more KoUpdaters to it to make the progress reporting more
     * accurate. If no active progress reporter is present, 0 is returned.
     **/
Jos van den Oever's avatar
Jos van den Oever committed
443
    KoProgressUpdater *progressUpdater() const;
444

445 446 447 448 449
    /**
     * Set a custom progress proxy to use to report loading
     * progress to.
     */
    void setProgressProxy(KoProgressProxy *progressProxy);
450
    KoProgressProxy* progressProxy() const;
451

Casper Boemann's avatar
Casper Boemann committed
452 453 454 455 456 457 458 459 460 461 462 463 464
    /**
     * Return true if url() is a real filename, false if url() is
     * an internal url in the store, like "tar:/..."
     */
    virtual bool isStoredExtern() const;

    /**
     * @return the page layout associated with this document (margins, pageSize, etc).
     * Override this if you want to provide different sized pages.
     *
     * @see KoPageLayout
     */
    virtual KoPageLayout pageLayout(int pageNumber = 0) const;
465
    virtual void setPageLayout(const KoPageLayout &pageLayout);
Casper Boemann's avatar
Casper Boemann committed
466 467 468 469 470 471

    /**
     * Performs a cleanup of unneeded backup files
     */
    void removeAutoSaveFiles();

472
    void setBackupFile(bool _b);
Casper Boemann's avatar
Casper Boemann committed
473 474 475 476 477 478 479 480 481

    bool backupFile()const;

    /**
     * Returns true if this document or any of its internal child documents are modified.
     */
    bool isModified() const;

    /**
482
     * Returns true during loading (openUrl can be asynchronous)
Casper Boemann's avatar
Casper Boemann committed
483 484 485 486 487 488 489 490
     */
    bool isLoading() const;

    int queryCloseDia();

    /**
     * Sets the backup path of the document
     */
491
    void setBackupPath(const QString & _path);
Casper Boemann's avatar
Casper Boemann committed
492 493 494 495 496 497

    /**
     * @return path to the backup document
     */
    QString backupPath()const;

498 499 500 501 502 503 504 505 506 507
    /**
     * @return caption of the document
     *
     * Caption is of the form "[title] - [url]",
     * built out of the document info (title) and pretty-printed
     * document URL.
     * If the title is not present, only the URL it returned.
     */
    QString caption() const;

Casper Boemann's avatar
Casper Boemann committed
508 509
    /**
     * Sets the document URL to empty URL
Halla Rempt's avatar
Halla Rempt committed
510
     * KParts doesn't allow this, but %Calligra apps have e.g. templates
Casper Boemann's avatar
Casper Boemann committed
511 512 513
     * After using loadNativeFormat on a template, one wants
     * to set the url to KUrl()
     */
514
    void resetURL();
Casper Boemann's avatar
Casper Boemann committed
515 516 517 518

    /**
     * Set when you want an external embedded document to be stored internally
     */
519
    void setStoreInternal(bool i);
Casper Boemann's avatar
Casper Boemann committed
520 521 522 523 524 525 526 527 528 529 530

    /**
     * @return true when external embedded documents are stored internally
     */
    bool storeInternal() const;

    bool hasExternURL() const;

    /**
     * @internal (public for KoMainWindow)
     */
531
    void setMimeTypeAfterLoading(const QString& mimeType);
Casper Boemann's avatar
Casper Boemann committed
532 533 534 535

    /**
     * @return returns the number of pages in the document.
     */
536
    virtual int pageCount() const;
Casper Boemann's avatar
Casper Boemann committed
537 538 539 540 541 542 543 544 545

    /**
     * Returns the unit used to display all measures/distances.
     */
    KoUnit unit() const;

    /**
     * Sets the unit used to display all measures/distances.
     */
546
    void setUnit(const KoUnit &unit);
Casper Boemann's avatar
Casper Boemann committed
547

548 549
    /**
     * Save the unit to the settings writer
Halla Rempt's avatar
Halla Rempt committed
550
     *
551 552
     * @param settingsWriter
     */
Jos van den Oever's avatar
Jos van den Oever committed
553
    void saveUnitOdf(KoXmlWriter *settingsWriter) const;
554

Casper Boemann's avatar
Casper Boemann committed
555 556
    QList<KoVersionInfo> &versionList();

557
    bool loadNativeFormatFromStore(QByteArray &data);
Casper Boemann's avatar
Casper Boemann committed
558 559

    /**
Halla Rempt's avatar
Halla Rempt committed
560 561 562
     * Adds a new version and then saves the whole document.
     * @param comment the comment for the version
     * @return true on success, otherwise false
Casper Boemann's avatar
Casper Boemann committed
563
    */
564
    bool addVersion(const QString& comment);
Casper Boemann's avatar
Casper Boemann committed
565

566 567
    /// return the grid data for this document.
    KoGridData &gridData();
Casper Boemann's avatar
Casper Boemann committed
568

569 570
    /// returns the guides data for this document.
    KoGuidesData &guidesData();
Casper Boemann's avatar
Casper Boemann committed
571

Sven Langkamp's avatar
 
Sven Langkamp committed
572 573
    void clearUndoHistory();

574 575 576 577 578 579

    /**
     *  Sets the modified flag on the document. This means that it has
     *  to be saved or not before deleting it.
     */
    virtual void setModified(bool _mod);
580

Casper Boemann's avatar
Casper Boemann committed
581 582 583 584 585
    /**
     * Initialize an empty document using default values
     */
    virtual void initEmpty();

586 587 588
    /**
     * Returns the global undo stack
     */
Alexander Potashev's avatar
Alexander Potashev committed
589
    KUndo2Stack *undoStack();
590 591 592 593 594 595 596 597 598 599 600 601 602 603


    /**
     * Set the output stream to report profile information to.
     */
    void setProfileStream(QTextStream *profilestream);

    /**
     * Set the output stream to report profile information to.
     */
    void setProfileReferenceTime(const QTime& referenceTime);

public slots:

Casper Boemann's avatar
Casper Boemann committed
604 605 606 607
    /**
     * Adds a command to the undo stack and executes it by calling the redo() function.
     * @param command command to add to the undo stack
     */
Alexander Potashev's avatar
Alexander Potashev committed
608
    virtual void addCommand(KUndo2Command *command);
Casper Boemann's avatar
Casper Boemann committed
609 610 611 612 613

    /**
     * Begins recording of a macro command. At the end endMacro needs to be called.
     * @param text command description
     */
614
    virtual void beginMacro(const KUndo2MagicString &text);
Casper Boemann's avatar
Casper Boemann committed
615 616 617 618 619 620 621 622 623 624 625 626 627

    /**
     * Ends the recording of a macro command.
     */
    virtual void endMacro();

signals:

    /**
     * This signal is emitted when the unit is changed by setUnit().
     * It is common to connect views to it, in order to change the displayed units
     * (e.g. in the rulers)
     */
628
    void unitChanged(const KoUnit &unit);
Casper Boemann's avatar
Casper Boemann committed
629 630 631 632 633 634

    /**
     * Progress info while loading or saving. The value is in percents (i.e. a number between 0 and 100)
     * Your KoDocument-derived class should emit the signal now and then during load/save.
     * KoMainWindow will take care of displaying a progress bar automatically.
     */
635
    void sigProgress(int value);
Casper Boemann's avatar
Casper Boemann committed
636 637 638 639 640

    /**
     * Emitted e.g. at the beginning of a save operation
     * This is emitted by KoDocument and used by KoView to display a statusbar message
     */
641
    void statusBarMessage(const QString& text);
Casper Boemann's avatar
Casper Boemann committed
642 643 644 645 646

    /**
     * Emitted e.g. at the end of a save operation
     * This is emitted by KoDocument and used by KoView to clear the statusbar message
     */
647
    void clearStatusBarMessage();
Casper Boemann's avatar
Casper Boemann committed
648 649 650 651

    /**
    * Emitted when the document is modified
    */
652
    void modified(bool);
Casper Boemann's avatar
Casper Boemann committed
653

654
    void titleModified(const QString &caption, bool isModified);
Casper Boemann's avatar
Casper Boemann committed
655 656

protected:
657 658

    friend class KoPart;
659
    friend class KisSketchPart;
Casper Boemann's avatar
Casper Boemann committed
660 661

    /**
662 663
     * Generate a name for the document.
     */
Casper Boemann's avatar
Casper Boemann committed
664 665
    QString newObjectName();

666
    QString autoSaveFile(const QString & path) const;
667
    void setDisregardAutosaveFailure(bool disregardFailure);
668

Casper Boemann's avatar
Casper Boemann committed
669 670 671 672 673 674
    /**
     *  Loads a document from KReadOnlyPart::m_file (KParts takes care of downloading
     *  remote documents).
     *  Applies a filter if necessary, and calls loadNativeFormat in any case
     *  You should not have to reimplement, except for very special cases.
     *
Halla Rempt's avatar
Halla Rempt committed
675 676
     * NOTE: this method also creates a new KoView instance!
     *
677
     * This method is called from the KReadOnlyPart::openUrl method.
Casper Boemann's avatar
Casper Boemann committed
678 679 680
     */
    virtual bool openFile();

681 682 683 684 685
    /**
     * This method is called by @a openFile() to allow applications to setup there
     * own KoProgressUpdater-subTasks which are then taken into account for the
     * displayed progressbar during loading.
     */
686
    virtual void setupOpenFileSubProgress();
687

Casper Boemann's avatar
Casper Boemann committed
688 689 690 691 692 693 694 695 696 697 698 699 700
    /**
     *  Saves a document to KReadOnlyPart::m_file (KParts takes care of uploading
     *  remote documents)
     *  Applies a filter if necessary, and calls saveNativeFormat in any case
     *  You should not have to reimplement, except for very special cases.
     */
    virtual bool saveFile();

    /**
     *  Overload this function if you have to load additional files
     *  from a store. This function is called after loadXML()
     *  and after loadChildren() have been called.
     */
Jos van den Oever's avatar
Jos van den Oever committed
701
    virtual bool completeLoading(KoStore *store);
Casper Boemann's avatar
Casper Boemann committed
702 703 704 705 706 707 708 709 710 711 712

    /**
     *  If you want to write additional files to a store,
     *  then you must do it here.
     *  In the implementation, you should prepend the document
     *  url (using url().url()) before the filename, so that everything is kept relative
     *  to this document. For instance it will produce urls such as
     *  tar:/1/pictures/picture0.png, if the doc url is tar:/1
     *  But do this ONLY if the document is not stored extern (see isStoredExtern() ).
     *  If it is, then the pictures should be saved to tar:/pictures.
     */
Jos van den Oever's avatar
Jos van den Oever committed
713
    virtual bool completeSaving(KoStore *store);
Casper Boemann's avatar
Casper Boemann committed
714 715 716


    /** @internal */
717
    virtual void setModified();
Casper Boemann's avatar
Casper Boemann committed
718 719

    /**
720
     *  Returns whether or not the current openUrl() or openFile() call is
Casper Boemann's avatar
Casper Boemann committed
721 722 723 724 725 726 727 728 729 730 731 732 733 734
     *  actually an import operation (like File --> Import).
     *  This is for informational purposes only.
     */
    bool isImporting() const;

    /**
     *  Returns whether or not the current saveFile() call is actually an export
     *  operation (like File --> Export).
     *  If this function returns true during saveFile() and you are changing
     *  some sort of state, you _must_ restore it before the end of saveFile();
     *  otherwise, File --> Export will not work properly.
     */
    bool isExporting() const;

735
public:
736

737
    QString localFilePath() const;
738 739 740 741 742 743 744 745
    void setLocalFilePath( const QString &localFilePath );

    virtual KoDocumentInfoDlg* createDocumentInfoDialog(QWidget *parent, KoDocumentInfo *docInfo) const;

    bool isReadWrite() const;

    KUrl url() const;
    void setUrl(const KUrl &url);
746

747
    virtual bool closeUrl(bool promptToSave = true);
748

749 750 751 752 753 754 755 756 757 758 759
    virtual bool saveAs( const KUrl &url );

public slots:

    virtual bool save();
    bool waitSaveComplete();

signals:

    void completed();
    void canceled(const QString &);
760

Casper Boemann's avatar
Casper Boemann committed
761
private slots:
762

Casper Boemann's avatar
Casper Boemann committed
763
    void slotAutoSave();
764

765 766 767
    /// Called by the undo stack when undo or redo is called
    void slotUndoStackIndexChanged(int idx);

768 769
protected:
    bool oldLoadAndParse(KoStore *store, const QString& filename, KoXmlDocument& doc);
Casper Boemann's avatar
Casper Boemann committed
770
private:
Halla Rempt's avatar
Halla Rempt committed
771 772
    bool saveToStream(QIODevice *dev);

773
    QString checkImageMimeTypes(const QString &mimeType, const KUrl& url) const;
774

775
    bool loadNativeFormatFromStore(const QString& file);
Jos van den Oever's avatar
Jos van den Oever committed
776
    bool loadNativeFormatFromStoreInternal(KoStore *store);
Casper Boemann's avatar
Casper Boemann committed
777

Jos van den Oever's avatar
Jos van den Oever committed
778 779
    bool savePreview(KoStore *store);
    bool saveOasisPreview(KoStore *store, KoXmlWriter *manifestWriter);
Halla Rempt's avatar
Halla Rempt committed
780

781 782
    QString prettyPathOrUrl() const;

783 784 785 786 787 788
    bool queryClose();
    bool saveToUrl();
    bool openUrlInternal(const KUrl &url);

    void abortLoad();

Casper Boemann's avatar
Casper Boemann committed
789
    class Private;
Jos van den Oever's avatar
Jos van den Oever committed
790
    Private *const d;
791 792 793 794 795

    Q_PRIVATE_SLOT(d, void _k_slotJobFinished( KJob * job ))
    Q_PRIVATE_SLOT(d, void _k_slotStatJobFinished(KJob*))
    Q_PRIVATE_SLOT(d, void _k_slotGotMimeType(KIO::Job *job, const QString &mime))
    Q_PRIVATE_SLOT(d, void _k_slotUploadFinished( KJob * job ))
Casper Boemann's avatar
Casper Boemann committed
796 797
};

798 799
Q_DECLARE_METATYPE(KoDocument*)

Casper Boemann's avatar
Casper Boemann committed
800
#endif