Debian Codegraph @

src/af/xap/xp/xad_Document.cpp

raw
1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2
3 /* AbiSource Application Framework
4 * Copyright (C) 1998,1999 AbiSource, Inc.
5 * Copyright (C) 2004 Tomas Frydrych <[email protected]>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301 USA.
21 */
22
23 #include <stdlib.h>
24 #include <cstring>
25 #include "ut_assert.h"
26 #include "ut_debugmsg.h"
27 #include "ut_string.h"
28 #include "ut_hash.h"
29 #include "ut_vector.h"
30 #include "ut_uuid.h"
31 #include "ut_misc.h"
32 #include "ut_path.h"
33 #include "xap_DialogFactory.h"
34
35 #include "xad_Document.h"
36 #include "xav_View.h"
37 #include "xap_App.h"
38 #include "xap_Strings.h"
39 #include "xap_Dlg_History.h"
40 #include "xap_Dlg_MessageBox.h"
41 #include "xap_Frame.h"
42 #include "xap_Strings.h"
43 #include "xap_Dialog_Id.h"
44
45 #ifdef ENABLE_RESOURCE_MANAGER
46 #include "xap_ResourceManager.h"
47 #endif
48
49 AD_Document::AD_Document() :
50 #ifdef ENABLE_RESOURCE_MANAGER
51 m_pResourceManager(new XAP_ResourceManager),
52 #else
53 m_pResourceManager(0),
54 #endif
55 m_iRefCount(1),
56 m_szFilename(NULL),
57 m_szEncodingName(""), // Should this have a default? UTF-8, perhaps?
58 m_bPieceTableChanging(false),
59 m_lastSavedTime(0),
60 m_lastOpenedTime(time(NULL)),
61 m_iEditTime(0),
62 m_iVersion(0),
63 m_bHistoryWasSaved(false),
64 m_bMarkRevisions(false),
65 m_bShowRevisions(true),
66 m_iRevisionID(1),
67 m_iShowRevisionID(0), // show all
68 m_bAutoRevisioning(false),
69 m_bForcedDirty(false),
70 m_pUUID(NULL),
71 m_pOrigUUID(NULL),
72 m_pMyUUID(NULL),
73 m_bDoNotAdjustHistory(false),
74 m_bAfterFirstSave(false)
75 { // TODO: clear the ignore list
76
77
78 // create UUID for this doc
79 UT_return_if_fail(XAP_App::getApp() && XAP_App::getApp()->getUUIDGenerator());
80
81 m_pUUID = XAP_App::getApp()->getUUIDGenerator()->createUUID();
82 UT_return_if_fail(m_pUUID);
83 UT_return_if_fail(m_pUUID->isValid());
84 //
85 // Make a copy with the same value so we know when we're importing
86 // a remote CR
87 //
88 m_pMyUUID = XAP_App::getApp()->getUUIDGenerator()->createUUID();
89 UT_return_if_fail(m_pMyUUID);
90 UT_return_if_fail(m_pMyUUID->isValid());
91
92 m_pOrigUUID = XAP_App::getApp()->getUUIDGenerator()->createUUID();
93 UT_return_if_fail(m_pOrigUUID);
94 UT_return_if_fail(m_pOrigUUID->isValid());
95 UT_UTF8String s;
96 m_pUUID->toString(s);
97 m_pOrigUUID->setUUID(s);
98 m_pMyUUID->setUUID(s);
99 UT_UTF8String OrigS;
100 m_pOrigUUID->toString(OrigS);
101 m_pOrigUUID->toString(m_sOrigUUIDString);
102 m_pMyUUID->toString(m_sMyUUIDString);
103 UT_DEBUGMSG(("!!!!!!!!!!----------------- Created string %s \n",s.utf8_str()));
104 UT_DEBUGMSG(("!!!!!!!!!!----------------- Orig string %s \n",OrigS.utf8_str()));
105 }
106
107 AD_Document::~AD_Document()
108 {
109 UT_ASSERT(m_iRefCount == 0);
110
111 // NOTE: let subclass clean up m_szFilename, so it matches the alloc mechanism
112
113 // & finally...
114 #ifdef ENABLE_RESOURCE_MANAGER
115 DELETEP(m_pResourceManager);
116 #endif
117
118 UT_VECTOR_PURGEALL(AD_VersionData*, m_vHistory);
119 UT_VECTOR_PURGEALL(AD_Revision*, m_vRevisions);
120
121 if (m_szFilename)
122 g_free(const_cast<void *>(static_cast<const void *>(m_szFilename)));
123
124 DELETEP(m_pUUID);
125 DELETEP(m_pOrigUUID);
126 DELETEP(m_pMyUUID);
127 }
128
129 const std::string & AD_Document::getPrintFilename(void) const
130 {
131 return m_sPrintFilename;
132 }
133
134 void AD_Document::setPrintFilename(const std::string & sFilename)
135 {
136 m_sPrintFilename = sFilename;
137 }
138
139 bool AD_Document::isOrigUUID(void) const
140 {
141 UT_UTF8String sDoc;
142 UT_UTF8String sOrig;
143 if((m_pMyUUID== NULL) || (m_pOrigUUID == NULL))
144 return false;
145 m_pMyUUID->toString(sDoc);
146 m_pOrigUUID->toString(sOrig);
147 bool b = (strcmp(sDoc.utf8_str(),sOrig.utf8_str()) == 0);
148 return b;
149 }
150
151 bool AD_Document::isPieceTableChanging(void) const
152 {
153 return m_bPieceTableChanging;
154 }
155
156 /*!
157 Creates a new UUID; this uuid has the 'MAC' portion set to the
158 same value as the main doc uuid -- this guarantees that all id's
159 generated this way will be document unique even though withou
160 using MAC in the uuid we cannot absolutely guarantee universal
161 uniqueness.
162 */
163 UT_UUID * AD_Document::getNewUUID()const
164 {
165 // when new uuid is requested, we will generate it reusing the MAC
166 // part of the doc uuid. This will ensure that all uuid's in the
167 // present document are unique even though we no longer use MAC
168 // addrress in them
169 UT_return_val_if_fail(XAP_App::getApp() && XAP_App::getApp()->getUUIDGenerator(), NULL);
170 UT_return_val_if_fail(m_pUUID, NULL);
171 UT_UUID * pUUID = XAP_App::getApp()->getUUIDGenerator()->createUUID(*m_pUUID);
172
173 UT_return_val_if_fail(pUUID, NULL);
174 pUUID->resetTime();
175 UT_ASSERT(pUUID->isValid());
176 return pUUID;
177 }
178
179 /*!
180 see notes on getNewUUID()
181 */
182 UT_uint32 AD_Document::getNewUUID32() const
183 {
184 #if 0
185 UT_return_val_if_fail(XAP_App::getApp() && XAP_App::getApp()->getUUIDGenerator(),0);
186 return XAP_App::getApp()->getUUIDGenerator()->getNewUUID32();
187 #else
188 UT_UUID *pUUID = getNewUUID();
189 UT_return_val_if_fail(pUUID, 0);
190 UT_uint32 iRet = pUUID->hash32();
191 delete pUUID;
192 return iRet;
193 #endif
194 }
195
196 /*!
197 see notes on getNewUUID()
198 */
199 UT_uint64 AD_Document::getNewUUID64() const
200 {
201 #if 0
202 UT_return_val_if_fail(XAP_App::getApp() && XAP_App::getApp()->getUUIDGenerator(),0);
203 return XAP_App::getApp()->getUUIDGenerator()->getNewUUID64();
204 #else
205 UT_UUID *pUUID = getNewUUID();
206 UT_return_val_if_fail(pUUID, 0);
207 UT_uint32 iRet = pUUID->hash32();
208 delete pUUID;
209 return iRet;
210 #endif
211 }
212
213
214 void AD_Document::ref(void)
215 {
216 UT_ASSERT(m_iRefCount > 0);
217
218 m_iRefCount++;
219 }
220
221
222 void AD_Document::unref(void)
223 {
224 UT_ASSERT(m_iRefCount > 0);
225
226 if (--m_iRefCount == 0)
227 {
228 delete this;
229 }
230 }
231
232 const char * AD_Document::getFilename(void) const
233 {
234 return m_szFilename;
235 }
236
237 // Document-wide Encoding name used for some file formats (Text, RTF, HTML)
238
239 void AD_Document::setEncodingName(const char *szEncodingName)
240 {
241 if (szEncodingName == NULL)
242 szEncodingName = "";
243
244 m_szEncodingName = szEncodingName;
245 }
246
247 const char * AD_Document::getEncodingName(void) const
248 {
249 return m_szEncodingName.size() ? m_szEncodingName.c_str() : 0;
250 }
251
252 void AD_Document::purgeHistory()
253 {
254 UT_VECTOR_PURGEALL(AD_VersionData*, m_vHistory);
255 m_bHistoryWasSaved = false;
256 }
257
258
259 /*!
260 Add given version data to the document history.
261 */
262 void AD_Document::addRecordToHistory(const AD_VersionData &vd)
263 {
264 AD_VersionData * v = new AD_VersionData(vd);
265 UT_return_if_fail(v);
266 m_vHistory.addItem((void*)v);
267 }
268
269 /*!
270 Get the version number for n-th record in version history
271 */
272 UT_uint32 AD_Document::getHistoryNthId(UT_sint32 i)const
273 {
274 if(!m_vHistory.getItemCount())
275 return 0;
276
277 AD_VersionData * v = (AD_VersionData*)m_vHistory.getNthItem(i);
278
279 if(!v)
280 return 0;
281
282 return v->getId();
283 }
284
285 UT_uint32 AD_Document::getHistoryNthTopXID(UT_sint32 i)const
286 {
287 if(!m_vHistory.getItemCount())
288 return 0;
289
290 AD_VersionData * v = (AD_VersionData*)m_vHistory.getNthItem(i);
291
292 if(!v)
293 return 0;
294
295 return v->getTopXID();
296 }
297
298 /*!
299 Get time stamp for n-th record in version history
300 NB: the time stamp represents the last save time
301 */
302 time_t AD_Document::getHistoryNthTime(UT_sint32 i)const
303 {
304 if(!m_vHistory.getItemCount())
305 return 0;
306
307 AD_VersionData * v = (AD_VersionData*)m_vHistory.getNthItem(i);
308
309 if(!v)
310 return 0;
311
312 return v->getTime();
313 }
314
315 time_t AD_Document::getHistoryNthTimeStarted(UT_sint32 i)const
316 {
317 if(!m_vHistory.getItemCount())
318 return 0;
319
320 AD_VersionData * v = (AD_VersionData*)m_vHistory.getNthItem(i);
321
322 if(!v)
323 return 0;
324
325 return v->getStartTime();
326 }
327
328 bool AD_Document::getHistoryNthAutoRevisioned(UT_sint32 i)const
329 {
330 if(!m_vHistory.getItemCount())
331 return 0;
332
333 AD_VersionData * v = (AD_VersionData*)m_vHistory.getNthItem(i);
334
335 if(!v)
336 return false;
337
338 return v->isAutoRevisioned();
339 }
340
341
342 /*!
343 Get get cumulative edit time for n-th record in version history
344 */
345 time_t AD_Document::getHistoryNthEditTime(UT_sint32 i)const
346 {
347 if(!m_vHistory.getItemCount() || !m_pUUID)
348 return 0;
349
350 AD_VersionData * v = (AD_VersionData*)m_vHistory.getNthItem(i);
351
352 if(!v)
353 return 0;
354
355 time_t t0 = v->getStartTime();
356 time_t t1 = v->getTime();
357
358 UT_ASSERT( t1 >= t0 );
359 return t1-t0;
360 }
361
362 /*!
363 Get the UID for n-th record in version history
364 */
365 const UT_UUID & AD_Document::getHistoryNthUID(UT_sint32 i) const
366 {
367 if(!m_vHistory.getItemCount())
368 return UT_UUID::getNull();
369
370 AD_VersionData * v = (AD_VersionData*)m_vHistory.getNthItem(i);
371
372 if(!v)
373 return UT_UUID::getNull();
374
375 return v->getUID();
376 }
377
378
379 /*!
380 Returns true if both documents are based on the same root document
381 */
382 bool AD_Document::areDocumentsRelated(const AD_Document & d) const
383 {
384 if((!m_pUUID && d.getDocUUID()) || (m_pUUID && !d.getDocUUID()))
385 return false;
386
387 return (*m_pUUID == *(d.getDocUUID()));
388 }
389
390 /*!
391 Returns true if both documents are based on the same root document
392 and all version records have identical UID's
393 on return, the last identical version id is found in iVersion
394 */
395 bool AD_Document::areDocumentHistoriesEqual(const AD_Document & d, UT_uint32 &iVersion) const
396 {
397 iVersion = 0;
398
399 if((!m_pUUID && d.getDocUUID()) || (m_pUUID && !d.getDocUUID()))
400 return false;
401
402 if(!(*m_pUUID == *(d.getDocUUID())))
403 return false;
404
405 UT_uint32 iCount = UT_MIN(getHistoryCount(), d.getHistoryCount());
406 UT_uint32 iMaxCount = UT_MAX(getHistoryCount(), d.getHistoryCount());
407
408 for(UT_uint32 i = 0; i < iCount; ++i)
409 {
410 AD_VersionData * v1 = (AD_VersionData*)m_vHistory.getNthItem(i);
411 AD_VersionData * v2 = (AD_VersionData*)d.m_vHistory.getNthItem(i);
412
413 if(!(*v1 == *v2))
414 return false;
415
416 iVersion = v1->getId();
417 }
418
419 if(iMaxCount != iCount)
420 return false;
421
422 return true;
423 }
424
425 /*!
426 Verifies to what extent we are able to restore given version of
427 the document
428
429 \return: return value indicates whether full/partial/no restore is possible
430
431 \param UT_uint32 & iVersion: on entry contains the version number
432 user wants to revert to; if return
433 value indicates partial restore
434 iVersion indicates which nearest
435 (greater) version can be restored
436 fully, or 0 if no such version exists
437 */
438 AD_HISTORY_STATE AD_Document::verifyHistoryState(UT_uint32 &iVersion) const
439 {
440 if(!m_vHistory.getItemCount())
441 return ADHIST_NO_RESTORE;
442
443 AD_HISTORY_STATE eRet = ADHIST_FULL_RESTORE; // be optimistic
444
445 const AD_VersionData * v = NULL;
446 UT_sint32 i;
447 bool bFullRestore = false;
448 bool bFound = false;
449
450 // find the lowest autorevisioned record greater than iVersion and
451 // evaluate the state of history above iVersion
452 for(i = 0; i < getHistoryCount(); ++i)
453 {
454 v = (const AD_VersionData*)m_vHistory.getNthItem(i);
455
456 if(!v)
457 {
458 UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN);
459 continue;
460 }
461
462 if(v->getId() < iVersion + 1)
463 continue;
464
465 if(!v->isAutoRevisioned())
466 continue;
467
468 // if we got so far, we have an autorevisioned record greater
469 // than iVersion
470
471 if(!bFound)
472 {
473 bFound = true;
474
475 if(v->getId() == iVersion + 1)
476 bFullRestore = true;
477
478 continue;
479 }
480
481 bFullRestore &= v->isAutoRevisioned();
482 }
483
484 if(!bFound)
485 {
486 // there are no autorevisioned records above our version
487 return ADHIST_NO_RESTORE;
488 }
489
490 if(!bFullRestore)
491 {
492 eRet = ADHIST_PARTIAL_RESTORE;
493
494 // we want to find out from which version would full restore
495 // be possible
496 UT_uint32 iMinVersion = 0; // assume nothing
497 for(i = getHistoryCount(); i > 0; --i)
498 {
499 v = (const AD_VersionData*)m_vHistory.getNthItem(i-1);
500
501 if(!v)
502 {
503 UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN);
504 continue;
505 }
506
507 if(v->getId() <= iVersion) // too far down the table
508 break;
509
510 if(!v->isAutoRevisioned()) // break in the history
511 break;
512
513 iMinVersion = v->getId();
514 }
515
516 // iMinVersion now contains the lowest version with a full
517 // record above it, which we pass back to the caller
518 iVersion = iMinVersion;
519 }
520
521 return eRet;
522 }
523
524 const AD_VersionData * AD_Document::findHistoryRecord(UT_uint32 iVersion) const
525 {
526 for(UT_sint32 i = 0; i < getHistoryCount(); ++i)
527 {
528 const AD_VersionData * v = (const AD_VersionData*)m_vHistory.getNthItem(i);
529
530 if(v->getId() == iVersion)
531 return v;
532 }
533
534 return NULL;
535 }
536
537
538 /*!
539 Set UID for the present document
540 */
541 void AD_Document::setDocUUID(const char * s)
542 {
543 if(!m_pUUID)
544 {
545 UT_return_if_fail(0);
546 }
547
548 if(!m_pUUID->setUUID(s))
549 {
550 // string we were passed did not contain valid uuid
551 // if our original id was valid, we will keep it, otherwise
552 // make a new one
553 if(!m_pUUID->isValid())
554 m_pUUID->makeUUID();
555 }
556 }
557
558 /*!
559 Set Orig UID for the present document
560 */
561 void AD_Document::setOrigUUID(const char * s)
562 {
563 if(!m_pOrigUUID)
564 {
565 UT_return_if_fail(0);
566 }
567
568 if(!m_pOrigUUID->setUUID(s))
569 {
570 // string we were passed did not contain valid uuid
571 // if our original id was valid, we will keep it, otherwise
572 // make a new one
573 if(!m_pOrigUUID->isValid())
574 m_pOrigUUID->makeUUID();
575 }
576 m_pOrigUUID->toString(m_sOrigUUIDString);
577 }
578
579
580 /*!
581 Set Orig UID for the present document
582 */
583 void AD_Document::setMyUUID(const char * s)
584 {
585 if(!m_pMyUUID)
586 {
587 UT_return_if_fail(0);
588 }
589
590 if(!m_pMyUUID->setUUID(s))
591 {
592 // string we were passed did not contain valid uuid
593 // if our original id was valid, we will keep it, otherwise
594 // make a new one
595 if(!m_pMyUUID->isValid())
596 m_pMyUUID->makeUUID();
597 }
598 m_pMyUUID->toString(m_sMyUUIDString);
599 }
600
601 /*!
602 Get the UID of this document represented as a string (this
603 function is primarily for exporters)
604 */
605 const char * AD_Document::getDocUUIDString() const
606 {
607 UT_return_val_if_fail(m_pUUID, NULL);
608 static UT_UTF8String s;
609 m_pUUID->toString(s);
610 return s.utf8_str();
611 }
612
613
614 /*!
615 Get the Original UID of this document represented as a string (this
616 function is primarily for exporters)
617 */
618 const char * AD_Document::getOrigDocUUIDString() const
619 {
620 UT_return_val_if_fail(m_pOrigUUID, NULL);
621 return m_sOrigUUIDString.utf8_str();
622 }
623
624
625 /*!
626 Get the UID of the users of this document represented as a string (this
627 function is primarily for exporters)
628
629 NOTE: don't make this a static variable, as this value might change over
630 the life of the document
631 */
632 UT_UTF8String AD_Document::getMyUUIDString() const
633 {
634 UT_return_val_if_fail(m_pMyUUID, "");
635 return m_sMyUUIDString;
636 }
637
638 ///////////////////////////////////////////////////////////////////
639 ///////////////////////////////////////////////////////////////////
640
641 /*!
642 find revision with id iId and return its index in revision table
643 \param iId -- id of revision we are interested in
644 \return -- if found index into revision vector if not found < 0
645 */
646 UT_sint32 AD_Document::getRevisionIndxFromId(UT_uint32 iId) const
647 {
648 for(UT_sint32 i = 0; i < m_vRevisions.getItemCount(); i++)
649 {
650 if(m_vRevisions.getNthItem(i)->getId() == iId)
651 return i;
652 }
653
654 return -1;
655 }
656
657 /**
658 * Should we pay attention to change tracking?
659 */
660 bool AD_Document::usingChangeTracking() const
661 {
662 bool ret = false;
663
664 ret |= isMarkRevisions();
665 ret |= ( getHighestRevisionId() > 1 );
666
667 return ret;
668 }
669
670
671
672 UT_uint32 AD_Document::getHighestRevisionId() const
673 {
674 UT_uint32 iId = 0;
675
676 for(UT_sint32 i = 0; i < m_vRevisions.getItemCount(); i++)
677 {
678 iId = UT_MAX(iId, m_vRevisions.getNthItem(i)->getId());
679 }
680
681 return iId;
682 }
683
684 const AD_Revision * AD_Document::getHighestRevision() const
685 {
686 UT_uint32 iId = 0;
687 const AD_Revision * r = NULL;
688
689 for(UT_sint32 i = 0; i < m_vRevisions.getItemCount(); i++)
690 {
691 const AD_Revision * t = m_vRevisions.getNthItem(i);
692 UT_uint32 t_id = t->getId();
693
694 if(t_id > iId)
695 {
696 iId = t_id;
697 r = t;
698 }
699 }
700
701 return r;
702 }
703
704 bool AD_Document::addRevision(UT_uint32 iId,
705 const UT_UCS4Char * pDesc, UT_uint32 iLen,
706 time_t tStart, UT_uint32 iVer,bool bGenCR)
707 {
708 for(UT_sint32 i = 0; i < m_vRevisions.getItemCount(); i++)
709 {
710 const AD_Revision * r = m_vRevisions.getNthItem(i);
711 if(r->getId() == iId)
712 return false;
713 }
714
715 UT_UCS4Char * pD = NULL;
716
717 if(pDesc)
718 {
719 pD = new UT_UCS4Char [iLen + 1];
720 UT_UCS4_strncpy(pD,pDesc,iLen);
721 pD[iLen] = 0;
722 }
723
724 AD_Revision * pRev = new AD_Revision(iId, pD, tStart, iVer);
725 addRevision(pRev,bGenCR);
726 m_iRevisionID = iId;
727 return true;
728 }
729
730 bool AD_Document::addRevision(AD_Revision * pRev, bool bGenCR)
731 {
732 m_vRevisions.addItem(pRev);
733 if(bGenCR)
734 {
735 const gchar * szAtts[11]={"docprop","revision",
736 "revision",NULL,
737 "revision-desc",NULL,
738 "revision-time",NULL,
739 "revision-ver",NULL,NULL};
740 UT_UTF8String sID,sTime,sVer;
741 UT_UTF8String_sprintf(sID,"%d",pRev->getId());
742 UT_UTF8String_sprintf(sTime,"%d",pRev->getStartTime());
743 UT_UTF8String_sprintf(sVer,"%d",pRev->getVersion());
744 UT_UTF8String sDesc(pRev->getDescription());
745 szAtts[3]= sID.utf8_str();
746 szAtts[5] = sDesc.utf8_str();
747 szAtts[7] = sTime.utf8_str();
748 szAtts[9] = sVer.utf8_str();
749 createAndSendDocPropCR(szAtts,NULL);
750 }
751 forceDirty();
752 return true;
753 }
754
755 void AD_Document::_purgeRevisionTable()
756 {
757 UT_VECTOR_PURGEALL(AD_Revision*, m_vRevisions);
758 m_vRevisions.clear();
759 }
760
761 void AD_Document::setMarkRevisions(bool bMark)
762 {
763 if(m_bMarkRevisions != bMark)
764 {
765 m_bMarkRevisions = bMark;
766 forceDirty();
767 }
768 }
769
770 void AD_Document::toggleMarkRevisions()
771 {
772 setMarkRevisions(!m_bMarkRevisions);
773 }
774
775 void AD_Document::setShowRevisions(bool bShow)
776 {
777 if(m_bShowRevisions != bShow)
778 {
779 m_bShowRevisions = bShow;
780 forceDirty();
781 }
782 }
783
784 void AD_Document::toggleShowRevisions()
785 {
786 setShowRevisions(!m_bShowRevisions);
787 }
788
789 void AD_Document::setShowRevisionId(UT_uint32 iId)
790 {
791 if(iId != m_iShowRevisionID)
792 {
793 m_iShowRevisionID = iId;
794 forceDirty();
795 }
796 }
797
798 void AD_Document::setRevisionId(UT_uint32 iId)
799 {
800 if(iId != m_iRevisionID)
801 {
802 m_iRevisionID = iId;
803 // not in this case; this value is not persistent between sessions
804 //forceDirty();
805 }
806 }
807
808 void AD_Document::setAutoRevisioning(bool b)
809 {
810 if(b != m_bAutoRevisioning)
811 {
812 // First of all, we will increase the document version number;
813 // this will allow us to match autorevision id to a document
814 // version number. However, do not increase the version number,
815 // etc., if == 0 (we have a new, unsaved document)
816 time_t t = time(NULL);
817
818 if(m_bAfterFirstSave)
819 {
820 m_iVersion++;
821
822 AD_VersionData v(m_iVersion, t, b, getTopXID());
823 addRecordToHistory(v);
824 }
825
826 m_bAutoRevisioning = b;
827
828 if(b)
829 {
830 // now create new revision
831 // if we did not adjust version because we have just been
832 // loaded and not saved, we do not want to adjust the
833 // revision number either
834 if(m_bAfterFirstSave)
835 {
836 const XAP_StringSet * pSS = XAP_App::getApp()->getStringSet();
837 UT_return_if_fail(pSS);
838 UT_UCS4String ucs4(pSS->getValue(XAP_STRING_ID_MSG_AutoRevision));
839
840 UT_uint32 iId = getRevisionId() + 1;
841
842 setRevisionId(iId);
843 addRevision(iId, ucs4.ucs4_str(),ucs4.length(),t, m_iVersion);
844 }
845 else if(getHighestRevisionId() != getRevisionId())
846 {
847 // we have not saved yet, but the revision we are
848 // going to be using is not in the revision table, add it
849 const XAP_StringSet * pSS = XAP_App::getApp()->getStringSet();
850 UT_return_if_fail(pSS);
851 UT_UCS4String ucs4(pSS->getValue(XAP_STRING_ID_MSG_AutoRevision));
852
853 UT_uint32 iId = getRevisionId();
854 addRevision(iId, ucs4.ucs4_str(),ucs4.length(),t, m_iVersion);
855 }
856
857
858 // collapse all revisions ...
859 setShowRevisionId(PD_MAX_REVISION);
860 setShowRevisions(false);
861 }
862 else
863 {
864 // we have to wipe out all of the revision information
865 // from the document; this is because non-revisioned items
866 // are treated as revision level 0 and so any
867 // revisions in the document would overshadow any
868 // subsequent changes -- see bug 7183
869
870 // step out of revision mode ...
871 _setMarkRevisions(false);
872 m_bAutoRevisioning = false;
873
874 if(acceptAllRevisions())
875 {
876 // we succeeded in restoring the document, so now clear the
877 // history record
878 _purgeRevisionTable();
879
880 m_bDoNotAdjustHistory = true;
881 save();
882 m_bDoNotAdjustHistory = false;
883 }
884
885 // go back to revisioning mode so that the
886 // setMarkRevisions() below triggers rebuild ...
887 _setMarkRevisions(true);
888
889 }
890
891 setMarkRevisions(b);
892 }
893 }
894
895 /*!
896 Find revision id that corresponds to given document version
897 \return: id > 0 on success or 0 on failure
898 */
899 UT_uint32 AD_Document::findAutoRevisionId(UT_uint32 iVersion) const
900 {
901 for(UT_sint32 i = 0; i < m_vRevisions.getItemCount(); i++)
902 {
903 const AD_Revision *pRev= m_vRevisions.getNthItem(i);
904 UT_return_val_if_fail(pRev, 0);
905
906 if(pRev->getVersion() == iVersion)
907 return pRev->getId();
908 }
909
910 UT_DEBUGMSG(("AD_Document::findAutoRevisionId: autorevision for version %d not found\n",
911 iVersion));
912
913 return 0;
914 }
915
916 /*!
917 Finds the nearest autorevision for the given document version
918 \param UT_uint32 iVersion: the document version
919 \param bool bLesser: indicates whether nearest lesser or nearest
920 greater autorevision is required
921 \return: id > 0 on success or 0 on failure
922 */
923 UT_uint32 AD_Document::findNearestAutoRevisionId(UT_uint32 iVersion, bool bLesser) const
924 {
925 UT_uint32 iId = 0;
926
927 for(UT_sint32 i = 0; i < m_vRevisions.getItemCount(); i++)
928 {
929 const AD_Revision *pRev= m_vRevisions.getNthItem(i);
930 UT_return_val_if_fail(pRev, 0);
931
932 if(bLesser)
933 {
934 if(pRev->getVersion() < iVersion)
935 iId = pRev->getId();
936 else
937 break;
938 }
939 else
940 {
941 if(pRev->getVersion() > iVersion)
942 return pRev->getId();
943 }
944 }
945
946 #ifdef DEBUG
947 if(iId == 0)
948 {
949 UT_DEBUGMSG(("AD_Document::findNearestAutoRevisionId: not found [ver. %d, bLesser=%d]\n",
950 iVersion, bLesser));
951 }
952 #endif
953 return iId;
954 }
955
956
957 /*!
958 Update document history and version information; should only be
959 called inside save() and saveAs()
960 */
961 void AD_Document::_adjustHistoryOnSave()
962 {
963 if(m_bDoNotAdjustHistory)
964 return;
965
966 // record this as the last time the document was saved + adjust
967 // the cumulative edit time
968 m_iVersion++;
969
970 if(!m_bHistoryWasSaved || m_bAutoRevisioning)
971 {
972 // if this is the first save, we will record the time the doc
973 // was opened as the start time, otherwise, we will use the
974 // current time
975 time_t t = !m_bHistoryWasSaved ? m_lastOpenedTime : time(NULL);
976
977 AD_VersionData v(m_iVersion,t,m_bAutoRevisioning,getTopXID());
978 m_lastSavedTime = v.getTime(); // store the time of this save
979 addRecordToHistory(v);
980
981 m_bHistoryWasSaved = true;
982 }
983 else
984 {
985 UT_return_if_fail(m_vHistory.getItemCount() > 0);
986
987 // change the edit time of the last entry and create a new UID
988 // for the record
989 AD_VersionData * v = (AD_VersionData*)m_vHistory.getNthItem(m_vHistory.getItemCount()-1);
990
991 UT_return_if_fail(v);
992 v->setId(m_iVersion);
993 v->newUID();
994 m_lastSavedTime = v->getTime();
995 }
996
997 if(m_bAutoRevisioning)
998 {
999 // create new revision
1000 const XAP_StringSet * pSS = XAP_App::getApp()->getStringSet();
1001 UT_return_if_fail(pSS);
1002 UT_UCS4String ucs4(pSS->getValue(XAP_STRING_ID_MSG_AutoRevision));
1003
1004 UT_uint32 iId = getRevisionId()+1;
1005 setRevisionId(iId);
1006 addRevision(iId, ucs4.ucs4_str(),ucs4.length(),time(NULL), m_iVersion);
1007 }
1008 }
1009
1010 bool AD_Document::_restoreVersion(XAP_Frame * pFrame, UT_uint32 iVersion)
1011 {
1012 UT_return_val_if_fail(pFrame, false);
1013
1014 if(isDirty())
1015 {
1016 if(pFrame->showMessageBox(XAP_STRING_ID_MSG_HistoryConfirmSave,
1017 XAP_Dialog_MessageBox::b_YN,
1018 XAP_Dialog_MessageBox::a_YES, getFilename())
1019 == XAP_Dialog_MessageBox::a_NO)
1020 {
1021 return false;
1022 }
1023
1024 save();
1025 }
1026
1027 // save the document under a different name ...
1028 // create unique new name
1029 UT_uint32 i = 0;
1030
1031 const char * pPath = g_strdup(getFilename());
1032 UT_return_val_if_fail(pPath, false);
1033
1034 char * pDot = (char *)strrchr(pPath,'.');
1035 if(pDot)
1036 {
1037 *pDot = 0;
1038 pDot++;
1039 }
1040
1041 UT_String s1, s2;
1042
1043 do
1044 {
1045 i++;
1046
1047 UT_String_sprintf(s2, "_version_%d-%d", iVersion, i);
1048
1049 s1 = pPath;
1050 s1 += s2;
1051
1052 if(pDot && *pDot)
1053 {
1054 s1 += ".";
1055 s1 += pDot;
1056 }
1057
1058 }
1059 while(UT_isRegularFile(s1.c_str()));
1060
1061 FREEP(pPath);
1062
1063 m_bDoNotAdjustHistory = true;
1064 saveAs(s1.c_str(), getLastSavedAsType());
1065 m_bDoNotAdjustHistory = false;
1066
1067 // step out of revision mode ...
1068 _setMarkRevisions(false);
1069 m_bAutoRevisioning = false;
1070
1071 UT_uint32 iRevisionId = findAutoRevisionId(iVersion);
1072
1073 // the revision id is the id of a revision that has been used to
1074 // modify version iVersion of the document. To restore iVersion,
1075 // we therefore have to reject all revisions >= iRevisionId
1076 UT_return_val_if_fail( iRevisionId > 0, false );
1077 iRevisionId--;
1078
1079 if(rejectAllHigherRevisions(iRevisionId))
1080 {
1081 // we succeeded in restoring the document, so now clear the
1082 // history record
1083 UT_sint32 iCount = getHistoryCount();
1084 const AD_VersionData * pVLast = NULL;
1085 time_t iEditTime = 0;
1086
1087 for(UT_sint32 j = 0; j < iCount; ++j)
1088 {
1089 AD_VersionData * v = (AD_VersionData *)m_vHistory.getNthItem(j);
1090 if(!v)
1091 {
1092 UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN);
1093 continue;
1094 }
1095
1096 if(v->getId() == iVersion)
1097 {
1098 pVLast = v;
1099 continue;
1100 }
1101
1102 if(v->getId() > iVersion)
1103 {
1104 // remember the lenth of the session
1105 iEditTime += (v->getTime() - v->getStartTime());
1106
1107 delete v;
1108 m_vHistory.deleteNthItem(j);
1109 iCount--;
1110 j--;
1111 }
1112 }
1113
1114 UT_return_val_if_fail(pVLast,false);
1115
1116 // set the document version correctly
1117 setDocVersion(iVersion);
1118 setLastSavedTime(pVLast->getTime());
1119 setLastOpenedTime(time(NULL));
1120
1121 UT_ASSERT(m_iEditTime >= iEditTime);
1122 m_iEditTime -= iEditTime;
1123
1124 // now save me as I am
1125 m_bDoNotAdjustHistory = true;
1126 save();
1127 _clearUndo();
1128 m_bDoNotAdjustHistory = false;
1129 }
1130
1131 return true;
1132 }
1133
1134 bool AD_Document::showHistory(AV_View * pView)
1135 {
1136 XAP_Frame * pFrame = static_cast<XAP_Frame *> ( pView->getParentData());
1137 UT_return_val_if_fail(pFrame, false);
1138
1139 pFrame->raise();
1140
1141 XAP_DialogFactory * pDialogFactory
1142 = static_cast<XAP_DialogFactory *>(pFrame->getDialogFactory());
1143
1144 XAP_Dialog_History * pDialog
1145 = static_cast<XAP_Dialog_History *>(pDialogFactory->requestDialog(XAP_DIALOG_ID_HISTORY));
1146
1147 UT_return_val_if_fail(pDialog,false);
1148
1149 pDialog->setDocument(this);
1150 pDialog->runModal(pFrame);
1151
1152 bool bShow = (pDialog->getAnswer() == XAP_Dialog_History::a_OK);
1153 bool bRet = false;
1154
1155 if (bShow)
1156 {
1157 UT_uint32 iVersion = pDialog->getSelectionId();
1158 UT_uint32 iOrigVersion = iVersion;
1159
1160 const XAP_StringSet * pSS = XAP_App::getApp()->getStringSet();
1161
1162 if(iVersion)
1163 {
1164 switch(verifyHistoryState(iVersion))
1165 {
1166 case ADHIST_PARTIAL_RESTORE:
1167 {
1168 // display warning message allowing user to
1169 // cancel, etc.
1170 UT_return_val_if_fail(pSS,false);
1171 UT_String s1, s2;
1172 const char * msg1, * msg2, * msg3, * msg4;
1173
1174 if(iVersion)
1175 {
1176 // full restore possible
1177 msg1 = pSS->getValue(XAP_STRING_ID_MSG_HistoryPartRestore1);
1178 msg2 = pSS->getValue(XAP_STRING_ID_MSG_HistoryPartRestore2);
1179 msg4 = pSS->getValue(XAP_STRING_ID_MSG_HistoryPartRestore4);
1180 UT_return_val_if_fail(msg1 && msg2 && msg4, false);
1181
1182 s1 = msg1;
1183 s1 += " ";
1184 s1 += msg2;
1185 s1 += " ";
1186 s1 += msg4;
1187
1188 UT_String_sprintf(s2,s1.c_str(),iOrigVersion,iVersion,iOrigVersion);
1189
1190 switch(pFrame->showMessageBox(s2.c_str(),
1191 XAP_Dialog_MessageBox::b_YNC,
1192 XAP_Dialog_MessageBox::a_YES))
1193 {
1194 case XAP_Dialog_MessageBox::a_NO:
1195 bRet = _restoreVersion(pFrame, iOrigVersion);
1196 break;
1197
1198 case XAP_Dialog_MessageBox::a_YES:
1199 bRet = _restoreVersion(pFrame, iVersion);
1200 break;
1201
1202 case XAP_Dialog_MessageBox::a_CANCEL:
1203 break;
1204
1205 default:
1206 UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN);
1207 }
1208
1209 }
1210 else
1211 {
1212 // full restore not possible
1213 msg1 = pSS->getValue(XAP_STRING_ID_MSG_HistoryPartRestore1);
1214 msg3 = pSS->getValue(XAP_STRING_ID_MSG_HistoryPartRestore3);
1215 msg4 = pSS->getValue(XAP_STRING_ID_MSG_HistoryPartRestore4);
1216 UT_return_val_if_fail(msg1 && msg3 && msg4,false);
1217
1218 s1 = msg1;
1219 s1 += " ";
1220 s1 += msg3;
1221 s1 += " ";
1222 s1 += msg4;
1223
1224 UT_String_sprintf(s2, s1.c_str(), iOrigVersion);
1225
1226 switch(pFrame->showMessageBox(s2.c_str(),
1227 XAP_Dialog_MessageBox::b_OC,
1228 XAP_Dialog_MessageBox::a_OK))
1229 {
1230 case XAP_Dialog_MessageBox::a_OK:
1231 bRet = _restoreVersion(pFrame, iOrigVersion);
1232 break;
1233
1234 case XAP_Dialog_MessageBox::a_CANCEL:
1235 break;
1236
1237 default:
1238 UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN);
1239 }
1240
1241 }
1242 }
1243 break;
1244
1245 case ADHIST_FULL_RESTORE:
1246 bRet = _restoreVersion(pFrame, iVersion);
1247 break;
1248
1249 case ADHIST_NO_RESTORE:
1250 // issue a warning message and quit
1251 {
1252
1253 UT_return_val_if_fail(pSS,false);
1254 UT_String s2;
1255 const char * msg1;
1256
1257 msg1 = pSS->getValue(XAP_STRING_ID_MSG_HistoryNoRestore);
1258 UT_return_val_if_fail(msg1,false);
1259
1260 UT_String_sprintf(s2, msg1, iOrigVersion);
1261
1262 pFrame->showMessageBox(s2.c_str(),
1263 XAP_Dialog_MessageBox::b_O,
1264 XAP_Dialog_MessageBox::a_OK);
1265 }
1266 break;
1267
1268 default:
1269 UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN);
1270 }
1271 }
1272 }
1273
1274 pDialogFactory->releaseDialog(pDialog);
1275
1276 return bRet;
1277 }
1278
1279 UT_Error AD_Document::saveAs(const char * szFilename, int ieft, const char * props)
1280 {
1281 UT_Error e = _saveAs(szFilename, ieft, props);
1282
1283 m_bAfterFirstSave |= (UT_OK == e);
1284 return e;
1285 }
1286
1287
1288 UT_Error AD_Document::saveAs(const char * szFilename, int ieft, bool cpy, const char * props)
1289 {
1290 UT_Error e = _saveAs(szFilename, ieft, cpy, props);
1291
1292 m_bAfterFirstSave |= (UT_OK == e);
1293 return e;
1294 }
1295
1296
1297 UT_Error AD_Document::save(void)
1298 {
1299 UT_Error e = _save();
1300
1301 m_bAfterFirstSave |= (UT_OK == e);
1302 return e;
1303 }
1304
1305 bool AD_Document::purgeAllRevisions(AV_View * pView)
1306 {
1307 UT_return_val_if_fail( pView, false );
1308
1309 XAP_Frame * pFrame = static_cast<XAP_Frame *> ( pView->getParentData());
1310 UT_return_val_if_fail( pFrame, false );
1311
1312 if(pFrame->showMessageBox(XAP_STRING_ID_MSG_NoUndo,
1313 XAP_Dialog_MessageBox::b_YN,
1314 XAP_Dialog_MessageBox::a_YES, getFilename())
1315 == XAP_Dialog_MessageBox::a_NO)
1316 {
1317 return false;
1318 }
1319
1320 setMarkRevisions(false);
1321 bool bRet = acceptAllRevisions();
1322 purgeRevisionTable(true);
1323 _clearUndo();
1324 return bRet;
1325 }
1326
1327
1328 ///////////////////////////////////////////////////
1329 // AD_VersionData
1330 //
1331 // constructor for new entries
1332 AD_VersionData::AD_VersionData(UT_uint32 v, time_t start, bool autorev, UT_uint32 xid)
1333 :m_iId(v),m_pUUID(NULL),m_tStart(start),m_bAutoRevision(autorev),m_iTopXID(xid)
1334 {
1335 // we do not create uuid's based on the main doc uuid as
1336 // AD_Document::getNewUUID() does; this is because we need to be
1337 // able to distinguish between versions of documents created at
1338 // different place at the same time.
1339 UT_UUIDGenerator * pGen = XAP_App::getApp()->getUUIDGenerator();
1340 UT_return_if_fail(pGen);
1341
1342 m_pUUID = pGen->createUUID();
1343 UT_return_if_fail(m_pUUID);
1344 m_tStart = m_pUUID->getTime();
1345 }
1346
1347
1348 // constructors for importers
1349 AD_VersionData::AD_VersionData(UT_uint32 v, UT_UTF8String &uuid, time_t start, bool autorev, UT_uint32 iTopXID):
1350 m_iId(v),m_pUUID(NULL),m_tStart(start),m_bAutoRevision(autorev),m_iTopXID(iTopXID)
1351 {
1352 UT_UUIDGenerator * pGen = XAP_App::getApp()->getUUIDGenerator();
1353 UT_return_if_fail(pGen);
1354
1355 m_pUUID = pGen->createUUID(uuid);
1356 UT_ASSERT_HARMLESS(m_pUUID);
1357 }
1358
1359 AD_VersionData::AD_VersionData(UT_uint32 v, const char *uuid, time_t start, bool autorev, UT_uint32 iTopXID):
1360 m_iId(v),m_pUUID(NULL),m_tStart(start),m_bAutoRevision(autorev),m_iTopXID(iTopXID)
1361 {
1362 UT_UUIDGenerator * pGen = XAP_App::getApp()->getUUIDGenerator();
1363 UT_return_if_fail(pGen);
1364
1365 m_pUUID = pGen->createUUID(uuid);
1366 UT_ASSERT_HARMLESS(m_pUUID);
1367 }
1368
1369 // copy constructor
1370 AD_VersionData::AD_VersionData(const AD_VersionData & v):
1371 m_iId(v.m_iId), m_pUUID(NULL), m_bAutoRevision(v.m_bAutoRevision), m_iTopXID(v.m_iTopXID)
1372 {
1373 UT_return_if_fail(v.m_pUUID);
1374 UT_UUIDGenerator * pGen = XAP_App::getApp()->getUUIDGenerator();
1375 UT_return_if_fail(pGen);
1376
1377 m_pUUID = pGen->createUUID(*(v.m_pUUID));
1378 UT_ASSERT(m_pUUID);
1379
1380 m_tStart = v.m_tStart;
1381 }
1382
1383 AD_VersionData & AD_VersionData::operator = (const AD_VersionData &v)
1384 {
1385 m_iId = v.m_iId;
1386 *m_pUUID = *(v.m_pUUID);
1387 m_tStart = v.m_tStart;
1388 m_iTopXID = v.m_iTopXID;
1389 m_bAutoRevision = v.m_bAutoRevision;
1390 return *this;
1391 }
1392
1393 bool AD_VersionData::operator == (const AD_VersionData &v)
1394 {
1395 return (m_iId == v.m_iId && m_tStart == v.m_tStart
1396 && *m_pUUID == *(v.m_pUUID) && m_bAutoRevision == v.m_bAutoRevision && m_iTopXID == v.m_iTopXID);
1397 }
1398
1399 AD_VersionData::~AD_VersionData()
1400 {
1401 DELETEP(m_pUUID);
1402 }
1403
1404 time_t AD_VersionData::getTime()const
1405 {
1406 if(!m_pUUID)
1407 return 0;
1408
1409 return m_pUUID->getTime();
1410 }
1411
1412 bool AD_VersionData::newUID()
1413 {
1414 if(!m_pUUID)
1415 return false;
1416
1417 return m_pUUID->makeUUID();
1418 }