1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: versdlg.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sfx2.hxx"
33 #include <unotools/localedatawrapper.hxx>
34 #ifndef _UNOTOOLS_PROCESSFACTORY_HXX
35 #include <comphelper/processfactory.hxx>
37 #include <svtools/eitem.hxx>
38 #include <svtools/intitem.hxx>
39 #include <svtools/stritem.hxx>
40 #include <svtools/itemset.hxx>
41 #include <svtools/useroptions.hxx>
42 #include <vcl/msgbox.hxx>
43 #include <vcl/svapp.hxx>
44 #include <tools/datetime.hxx>
46 #define _SVSTDARR_STRINGSDTOR
47 #include <svtools/svstdarr.hxx>
49 #include "versdlg.hrc"
50 #include "versdlg.hxx"
51 #include <sfx2/viewfrm.hxx>
52 #include "sfxresid.hxx"
53 #include <sfx2/docfile.hxx>
54 #include <sfx2/objsh.hxx>
55 #include <sfx2/sfxsids.hrc>
56 #include <sfx2/dispatch.hxx>
57 #include <sfx2/request.hxx>
59 #include <sfx2/sfxuno.hxx>
61 using namespace com::sun::star
;
63 // **************************************************************************
69 DateTime aCreationDate
;
72 SfxVersionInfo( const SfxVersionInfo
& rInfo
)
75 SfxVersionInfo
& operator=( const SfxVersionInfo
&rInfo
)
78 aComment
= rInfo
.aComment
;
79 aAuthor
= rInfo
.aAuthor
;
80 aCreationDate
= rInfo
.aCreationDate
;
84 DECLARE_LIST( _SfxVersionTable
, SfxVersionInfo
* )
85 class SfxVersionTableDtor
: public _SfxVersionTable
88 SfxVersionTableDtor( const sal_uInt16 nInitSz
=0, const sal_uInt16 nReSz
=1 )
89 : _SfxVersionTable( nInitSz
, nReSz
)
92 SfxVersionTableDtor( const SfxVersionTableDtor
&rCpy
) :
93 _SfxVersionTable( rCpy
)
96 SfxVersionTableDtor( const uno::Sequence
< util::RevisionTag
>& rInfo
);
98 ~SfxVersionTableDtor()
101 SfxVersionTableDtor
& operator=( const SfxVersionTableDtor
&rCpy
);
103 SvStream
& Read( SvStream
& );
104 SvStream
& Write( SvStream
& ) const;
105 SvStringsDtor
* GetVersions() const;
108 SfxVersionTableDtor::SfxVersionTableDtor( const uno::Sequence
< util::RevisionTag
>& rInfo
)
110 for ( sal_Int32 n
=0; n
<(sal_Int32
)rInfo
.getLength(); n
++ )
112 SfxVersionInfo
* pInfo
= new SfxVersionInfo
;
113 pInfo
->aName
= rInfo
[n
].Identifier
;
114 pInfo
->aComment
= rInfo
[n
].Comment
;
115 pInfo
->aAuthor
= rInfo
[n
].Author
;
117 Date
aDate ( rInfo
[n
].TimeStamp
.Day
, rInfo
[n
].TimeStamp
.Month
, rInfo
[n
].TimeStamp
.Year
);
118 Time
aTime ( rInfo
[n
].TimeStamp
.Hours
, rInfo
[n
].TimeStamp
.Minutes
, rInfo
[n
].TimeStamp
.Seconds
, rInfo
[n
].TimeStamp
.HundredthSeconds
);
120 pInfo
->aCreationDate
= DateTime( aDate
, aTime
);
121 Insert( pInfo
, Count() );
125 void SfxVersionTableDtor::DelDtor()
127 SfxVersionInfo
* pTmp
= First();
136 SfxVersionTableDtor
& SfxVersionTableDtor::operator=( const SfxVersionTableDtor
& rTbl
)
139 SfxVersionInfo
* pTmp
= ((SfxVersionTableDtor
&)rTbl
).First();
142 SfxVersionInfo
*pNew
= new SfxVersionInfo( *pTmp
);
143 Insert( pNew
, LIST_APPEND
);
144 pTmp
= ((SfxVersionTableDtor
&)rTbl
).Next();
149 //----------------------------------------------------------------
150 //----------------------------------------------------------------
151 //----------------------------------------------------------------
152 SfxVersionInfo::SfxVersionInfo()
156 static String
ConvertDateTime_Impl(const DateTime
& rTime
, const LocaleDataWrapper
& rWrapper
)
158 const String
pDelim ( DEFINE_CONST_UNICODE( ", "));
159 String
aStr(rWrapper
.getDate(rTime
));
161 aStr
+= rWrapper
.getTime(rTime
, TRUE
, FALSE
);
165 SvStringsDtor
* SfxVersionTableDtor::GetVersions() const
167 SvStringsDtor
*pList
= new SvStringsDtor
;
168 SfxVersionInfo
* pInfo
= ((SfxVersionTableDtor
*) this)->First();
169 LocaleDataWrapper
aLocaleWrapper( ::comphelper::getProcessServiceFactory(), Application::GetSettings().GetLocale() );
172 String
*pString
= new String( pInfo
->aComment
);
173 (*pString
) += DEFINE_CONST_UNICODE( "; " );
174 (*pString
) += ConvertDateTime_Impl( pInfo
->aCreationDate
, aLocaleWrapper
);
175 pList
->Insert( pString
, pList
->Count() );
176 pInfo
= ((SfxVersionTableDtor
*) this)->Next();
182 // Achtung im Code wird dieses Array direkt (0, 1, ...) indiziert
183 static long nTabs_Impl
[] =
189 void SfxVersionsTabListBox_Impl::KeyInput( const KeyEvent
& rKeyEvent
)
191 const KeyCode
& rCode
= rKeyEvent
.GetKeyCode();
192 switch ( rCode
.GetCode() )
197 Window::GetParent()->KeyInput( rKeyEvent
);
200 SvTabListBox::KeyInput( rKeyEvent
);
205 SfxVersionsTabListBox_Impl::SfxVersionsTabListBox_Impl( Window
* pParent
, const ResId
& rResId
)
206 : SvTabListBox( pParent
, rResId
)
210 SfxVersionDialog::SfxVersionDialog ( SfxViewFrame
* pVwFrame
, BOOL bIsSaveVersionOnClose
)
211 : SfxModalDialog( NULL
, SfxResId( DLG_VERSIONS
) )
212 , aNewGroup( this, SfxResId( GB_NEWVERSIONS
) )
213 , aSaveButton( this, SfxResId( PB_SAVE
) )
214 , aSaveCheckBox( this, SfxResId( CB_SAVEONCLOSE
) )
215 , aExistingGroup( this, SfxResId( GB_OLDVERSIONS
) )
216 , aDateTimeText( this, SfxResId( FT_DATETIME
) )
217 , aSavedByText( this, SfxResId( FT_SAVEDBY
) )
218 , aCommentText( this, SfxResId( FT_COMMENTS
) )
219 , aVersionBox( this, SfxResId( TLB_VERSIONS
) )
220 , aCloseButton( this, SfxResId( PB_CLOSE
) )
221 , aOpenButton( this, SfxResId( PB_OPEN
) )
222 , aViewButton( this, SfxResId( PB_VIEW
) )
223 , aDeleteButton( this, SfxResId( PB_DELETE
) )
224 , aCompareButton( this, SfxResId( PB_COMPARE
) )
225 , aHelpButton( this, SfxResId( PB_HELP
) )
226 , pViewFrame( pVwFrame
)
228 , mpLocaleWrapper( NULL
)
229 , mbIsSaveVersionOnClose( bIsSaveVersionOnClose
)
233 Link aClickLink
= LINK( this, SfxVersionDialog
, ButtonHdl_Impl
);
234 aViewButton
.SetClickHdl ( aClickLink
);
235 aSaveButton
.SetClickHdl ( aClickLink
);
236 aDeleteButton
.SetClickHdl ( aClickLink
);
237 aCompareButton
.SetClickHdl ( aClickLink
);
238 aOpenButton
.SetClickHdl ( aClickLink
);
239 aSaveCheckBox
.SetClickHdl ( aClickLink
);
241 aVersionBox
.SetSelectHdl( LINK( this, SfxVersionDialog
, SelectHdl_Impl
) );
242 aVersionBox
.SetDoubleClickHdl( LINK( this, SfxVersionDialog
, DClickHdl_Impl
) );
244 aVersionBox
.GrabFocus();
245 aVersionBox
.SetWindowBits( WB_HSCROLL
| WB_CLIPCHILDREN
);
246 aVersionBox
.SetSelectionMode( SINGLE_SELECTION
);
247 aVersionBox
.SetTabs( &nTabs_Impl
[0], MAP_APPFONT
);
248 aVersionBox
.Resize(); // OS: Hack fuer richtige Selektion
251 // set dialog title (filename or docinfo title)
252 String sText
= GetText();
253 ( sText
+= ' ' ) += pViewFrame
->GetObjectShell()->GetTitle();
259 String
ConvertWhiteSpaces_Impl( const String
& rText
)
261 // converted linebreaks and tabs to blanks; it's necessary for the display
263 const sal_Unicode
* pChars
= rText
.GetBuffer();
274 sConverted
+= *pChars
;
283 void SfxVersionDialog::Init_Impl()
285 SfxObjectShell
*pObjShell
= pViewFrame
->GetObjectShell();
286 SfxMedium
* pMedium
= pObjShell
->GetMedium();
287 uno::Sequence
< util::RevisionTag
> aVersions
= pMedium
->GetVersionList( true );
289 mpTable
= new SfxVersionTableDtor( aVersions
);
291 for ( USHORT n
= 0; n
< mpTable
->Count(); ++n
)
293 SfxVersionInfo
*pInfo
= mpTable
->GetObject(n
);
294 String aEntry
= ConvertDateTime_Impl( pInfo
->aCreationDate
, *mpLocaleWrapper
);
296 aEntry
+= pInfo
->aAuthor
;
298 aEntry
+= ConvertWhiteSpaces_Impl( pInfo
->aComment
);
299 SvLBoxEntry
*pEntry
= aVersionBox
.InsertEntry( aEntry
);
300 pEntry
->SetUserData( pInfo
);
304 aSaveCheckBox
.Check( mbIsSaveVersionOnClose
);
306 BOOL bEnable
= !pObjShell
->IsReadOnly();
307 aSaveButton
.Enable( bEnable
);
308 aSaveCheckBox
.Enable( bEnable
);
310 aOpenButton
.Disable();
311 aViewButton
.Disable();
312 aDeleteButton
.Disable();
313 aCompareButton
.Disable();
315 SelectHdl_Impl( &aVersionBox
);
318 SfxVersionDialog::~SfxVersionDialog ()
321 delete mpLocaleWrapper
;
324 void SfxVersionDialog::Open_Impl()
326 SfxObjectShell
*pObjShell
= pViewFrame
->GetObjectShell();
328 SvLBoxEntry
*pEntry
= aVersionBox
.FirstSelected();
329 ULONG nPos
= aVersionBox
.GetModel()->GetRelPos( pEntry
);
330 SfxInt16Item
aItem( SID_VERSION
, (short)nPos
+1 );
331 SfxStringItem
aTarget( SID_TARGETNAME
, DEFINE_CONST_UNICODE("_blank") );
332 SfxStringItem
aReferer( SID_REFERER
, DEFINE_CONST_UNICODE("private:user") );
333 SfxStringItem
aFile( SID_FILE_NAME
, pObjShell
->GetMedium()->GetName() );
335 ::rtl::OUString aPassString
;
336 if ( GetPasswd_Impl( pObjShell
->GetMedium()->GetItemSet(), aPassString
) )
338 // there is a password, it should be used during the opening
339 SfxStringItem
aPassItem( SID_PASSWORD
, aPassString
);
340 pViewFrame
->GetDispatcher()->Execute(
341 SID_OPENDOC
, SFX_CALLMODE_ASYNCHRON
, &aFile
, &aItem
, &aTarget
, &aReferer
, &aPassItem
, 0L );
344 pViewFrame
->GetDispatcher()->Execute(
345 SID_OPENDOC
, SFX_CALLMODE_ASYNCHRON
, &aFile
, &aItem
, &aTarget
, &aReferer
, 0L );
350 void SfxVersionDialog::RecalcDateColumn()
352 // recalculate the datetime column width
354 mpLocaleWrapper
= new LocaleDataWrapper(
355 ::comphelper::getProcessServiceFactory(), Application::GetSettings().GetLocale() );
356 String sDateTime
= ConvertDateTime_Impl( aNow
, *mpLocaleWrapper
);
357 long nWidth
= aVersionBox
.GetTextWidth( sDateTime
);
358 nWidth
+= 15; // a little offset
359 long nTab
= aVersionBox
.GetTab(1);
363 long nDelta
= nWidth
- nTab
;
364 aVersionBox
.SetTab( 1, nTab
+ nDelta
, MAP_PIXEL
);
365 nTab
= aVersionBox
.GetTab(2);
366 aVersionBox
.SetTab( 2, nTab
+ nDelta
, MAP_PIXEL
);
368 // resize and move header
369 Size aSize
= aDateTimeText
.GetSizePixel();
370 aSize
.Width() += nDelta
;
371 aDateTimeText
.SetSizePixel( aSize
);
372 Point aPos
= aSavedByText
.GetPosPixel();
374 aSavedByText
.SetPosPixel( aPos
);
375 aPos
= aCommentText
.GetPosPixel();
377 aCommentText
.SetPosPixel( aPos
);
381 IMPL_LINK( SfxVersionDialog
, DClickHdl_Impl
, Control
*, EMPTYARG
)
387 IMPL_LINK( SfxVersionDialog
, SelectHdl_Impl
, Control
*, EMPTYARG
)
389 bool bEnable
= ( aVersionBox
.FirstSelected() != NULL
);
390 SfxObjectShell
* pObjShell
= pViewFrame
->GetObjectShell();
391 aDeleteButton
.Enable( bEnable
!= false && !pObjShell
->IsReadOnly() );
392 aOpenButton
.Enable( bEnable
!= false );
393 aViewButton
.Enable( bEnable
!= false );
395 const SfxPoolItem
*pDummy
=NULL
;
396 SfxItemState eState
= pViewFrame
->GetDispatcher()->QueryState( SID_DOCUMENT_MERGE
, pDummy
);
397 eState
= pViewFrame
->GetDispatcher()->QueryState( SID_DOCUMENT_COMPARE
, pDummy
);
398 aCompareButton
.Enable( bEnable
!= false && eState
>= SFX_ITEM_AVAILABLE
);
403 IMPL_LINK( SfxVersionDialog
, ButtonHdl_Impl
, Button
*, pButton
)
405 SfxObjectShell
*pObjShell
= pViewFrame
->GetObjectShell();
406 SvLBoxEntry
*pEntry
= aVersionBox
.FirstSelected();
408 if ( pButton
== &aSaveCheckBox
)
410 mbIsSaveVersionOnClose
= aSaveCheckBox
.IsChecked();
412 else if ( pButton
== &aSaveButton
)
414 SfxVersionInfo aInfo
;
415 aInfo
.aAuthor
= SvtUserOptions().GetFullName();
416 SfxViewVersionDialog_Impl
* pDlg
= new SfxViewVersionDialog_Impl( this, aInfo
, TRUE
);
417 short nRet
= pDlg
->Execute();
418 if ( nRet
== RET_OK
)
420 SfxStringItem
aComment( SID_DOCINFO_COMMENTS
, aInfo
.aComment
);
421 pObjShell
->SetModified( TRUE
);
422 const SfxPoolItem
* aItems
[2];
423 aItems
[0] = &aComment
;
425 pViewFrame
->GetBindings().ExecuteSynchron( SID_SAVEDOC
, aItems
, 0 );
426 aVersionBox
.SetUpdateMode( FALSE
);
429 aVersionBox
.SetUpdateMode( TRUE
);
434 if ( pButton
== &aDeleteButton
&& pEntry
)
436 pObjShell
->GetMedium()->RemoveVersion_Impl( ((SfxVersionInfo
*) pEntry
->GetUserData())->aName
);
437 pObjShell
->SetModified( TRUE
);
438 aVersionBox
.SetUpdateMode( FALSE
);
441 aVersionBox
.SetUpdateMode( TRUE
);
443 else if ( pButton
== &aOpenButton
&& pEntry
)
447 else if ( pButton
== &aViewButton
&& pEntry
)
449 SfxVersionInfo
* pInfo
= (SfxVersionInfo
*) pEntry
->GetUserData();
450 SfxViewVersionDialog_Impl
* pDlg
= new SfxViewVersionDialog_Impl( this, *pInfo
, FALSE
);
454 else if ( pEntry
&& pButton
== &aCompareButton
)
456 SfxAllItemSet
aSet( pObjShell
->GetPool() );
457 ULONG nPos
= aVersionBox
.GetModel()->GetRelPos( pEntry
);
458 aSet
.Put( SfxInt16Item( SID_VERSION
, (short)nPos
+1 ) );
459 aSet
.Put( SfxStringItem( SID_FILE_NAME
, pObjShell
->GetMedium()->GetName() ) );
461 SfxItemSet
* pSet
= pObjShell
->GetMedium()->GetItemSet();
462 SFX_ITEMSET_ARG( pSet
, pFilterItem
, SfxStringItem
, SID_FILTER_NAME
, FALSE
);
463 SFX_ITEMSET_ARG( pSet
, pFilterOptItem
, SfxStringItem
, SID_FILE_FILTEROPTIONS
, FALSE
);
465 aSet
.Put( *pFilterItem
);
466 if ( pFilterOptItem
)
467 aSet
.Put( *pFilterOptItem
);
469 pViewFrame
->GetDispatcher()->Execute( SID_DOCUMENT_COMPARE
, SFX_CALLMODE_ASYNCHRON
, aSet
);
476 SfxViewVersionDialog_Impl::SfxViewVersionDialog_Impl ( Window
*pParent
, SfxVersionInfo
& rInfo
, BOOL bEdit
)
477 : SfxModalDialog( pParent
, SfxResId( DLG_COMMENTS
) )
478 , aDateTimeText( this, SfxResId( FT_DATETIME
) )
479 , aSavedByText( this, SfxResId( FT_SAVEDBY
) )
480 , aEdit( this, SfxResId( ME_VERSIONS
) )
481 , aOKButton( this, SfxResId( PB_OK
) )
482 , aCancelButton( this, SfxResId( PB_CANCEL
) )
483 , aCloseButton( this, SfxResId( PB_CLOSE
) )
484 , aHelpButton( this, SfxResId( PB_HELP
) )
489 LocaleDataWrapper
aLocaleWrapper( ::comphelper::getProcessServiceFactory(), Application::GetSettings().GetLocale() );
490 aDateTimeText
.SetText( aDateTimeText
.GetText().Append(ConvertDateTime_Impl( pInfo
->aCreationDate
, aLocaleWrapper
)) );
491 aSavedByText
.SetText( aSavedByText
.GetText().Append(pInfo
->aAuthor
) );
492 aEdit
.SetText( rInfo
.aComment
);
494 aCloseButton
.SetClickHdl ( LINK( this, SfxViewVersionDialog_Impl
, ButtonHdl
) );
495 aOKButton
.SetClickHdl ( LINK( this, SfxViewVersionDialog_Impl
, ButtonHdl
) );
501 aCancelButton
.Hide();
502 aEdit
.SetReadOnly( TRUE
);
508 IMPL_LINK( SfxViewVersionDialog_Impl
, ButtonHdl
, Button
*, pButton
)
510 if ( pButton
== &aCloseButton
)
512 EndDialog( RET_CANCEL
);
514 else if ( pButton
== &aOKButton
)
516 pInfo
->aComment
= aEdit
.GetText();