1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <osl/security.hxx>
21 #include <sfx2/dialoghelper.hxx>
22 #include <svl/sharecontrolfile.hxx>
23 #include <unotools/useroptions.hxx>
27 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
28 #include <com/sun/star/document/XDocumentProperties.hpp>
30 #include "sharedocdlg.hxx"
31 #include "scresid.hxx"
32 #include "viewdata.hxx"
34 using namespace ::com::sun::star
;
36 class ScShareTable
: public SvSimpleTable
39 OUString m_sWidestAccessString
;
41 explicit ScShareTable(SvSimpleTableContainer
& rParent
)
42 : SvSimpleTable(rParent
)
44 m_sWidestAccessString
= getWidestTime(*ScGlobal::pLocaleData
);
46 virtual void Resize() override
48 SvSimpleTable::Resize();
49 if (isInitialLayout(this))
54 HeaderBar
&rBar
= GetTheHeaderBar();
55 if (rBar
.GetItemCount() < 2)
58 long nAccessedWidth
= 12 +
59 std::max(rBar
.GetTextWidth(rBar
.GetItemText(2)),
60 GetTextWidth(m_sWidestAccessString
));
61 long nWebSiteWidth
= std::max(
62 12 + rBar
.GetTextWidth(rBar
.GetItemText(1)),
63 GetSizePixel().Width() - nAccessedWidth
);
64 long aStaticTabs
[]= { 2, 0, 0 };
65 aStaticTabs
[2] = nWebSiteWidth
;
66 SvSimpleTable::SetTabs(aStaticTabs
, MAP_PIXEL
);
70 // class ScShareDocumentDlg
72 ScShareDocumentDlg::ScShareDocumentDlg( vcl::Window
* pParent
, ScViewData
* pViewData
)
73 : ModalDialog(pParent
, "ShareDocumentDialog", "modules/scalc/ui/sharedocumentdlg.ui")
74 , mpViewData(pViewData
)
77 OSL_ENSURE( mpViewData
, "ScShareDocumentDlg CTOR: mpViewData is null!" );
78 mpDocShell
= ( mpViewData
? mpViewData
->GetDocShell() : nullptr );
79 OSL_ENSURE( mpDocShell
, "ScShareDocumentDlg CTOR: mpDocShell is null!" );
81 get(m_pCbShare
, "share");
82 get(m_pFtWarning
, "warning");
84 SvSimpleTableContainer
*pCtrl
= get
<SvSimpleTableContainer
>("users");
85 pCtrl
->set_height_request(pCtrl
->GetTextHeight()*9);
86 m_pLbUsers
= VclPtr
<ScShareTable
>::Create(*pCtrl
);
88 m_aStrNoUserData
= get
<FixedText
>("nouserdata")->GetText();
89 m_aStrUnknownUser
= get
<FixedText
>("unknownuser")->GetText();
90 m_aStrExclusiveAccess
= get
<FixedText
>("exclusive")->GetText();
92 bool bIsDocShared
= mpDocShell
&& mpDocShell
->IsDocShared();
93 m_pCbShare
->Check( bIsDocShared
);
94 m_pCbShare
->SetToggleHdl( LINK( this, ScShareDocumentDlg
, ToggleHandle
) );
95 m_pFtWarning
->Enable( bIsDocShared
);
97 long nTabs
[] = { 2, 0, 0 };
98 m_pLbUsers
->SetTabs( nTabs
);
100 OUString
aHeader(get
<FixedText
>("name")->GetText());
102 aHeader
+= get
<FixedText
>("accessed")->GetText();
103 m_pLbUsers
->InsertHeaderEntry( aHeader
, HEADERBAR_APPEND
, HeaderBarItemBits::LEFT
| HeaderBarItemBits::LEFTIMAGE
| HeaderBarItemBits::VCENTER
);
104 m_pLbUsers
->SetSelectionMode( NO_SELECTION
);
109 ScShareDocumentDlg::~ScShareDocumentDlg()
114 void ScShareDocumentDlg::dispose()
116 m_pLbUsers
.disposeAndClear();
118 m_pFtWarning
.clear();
119 ModalDialog::dispose();
122 IMPL_LINK_NOARG_TYPED(ScShareDocumentDlg
, ToggleHandle
, CheckBox
&, void)
124 m_pFtWarning
->Enable( m_pCbShare
->IsChecked() );
127 bool ScShareDocumentDlg::IsShareDocumentChecked() const
129 return m_pCbShare
->IsChecked();
132 void ScShareDocumentDlg::UpdateView()
139 if ( mpDocShell
->IsDocShared() )
143 ::svt::ShareControlFile
aControlFile( mpDocShell
->GetSharedFileURL() );
144 std::vector
<LockFileEntry
> aUsersData
= aControlFile
.GetUsersData();
145 sal_Int32 nLength
= aUsersData
.size();
149 sal_Int32 nUnknownUser
= 1;
151 for ( sal_Int32 i
= 0; i
< nLength
; ++i
)
153 if ( !aUsersData
[i
][LockFileComponent::EDITTIME
].isEmpty() )
156 if ( !aUsersData
[i
][LockFileComponent::OOOUSERNAME
].isEmpty() )
158 aUser
= aUsersData
[i
][LockFileComponent::OOOUSERNAME
];
160 else if ( !aUsersData
[i
][LockFileComponent::SYSUSERNAME
].isEmpty() )
162 aUser
= aUsersData
[i
][LockFileComponent::SYSUSERNAME
];
166 aUser
= m_aStrUnknownUser
+ " " + OUString::number( nUnknownUser
++ );
169 // parse the edit time string of the format "DD.MM.YYYY hh:mm"
170 OUString aDateTimeStr
= aUsersData
[i
][LockFileComponent::EDITTIME
];
171 sal_Int32 nIndex
= 0;
172 OUString aDateStr
= aDateTimeStr
.getToken( 0, ' ', nIndex
);
173 OUString aTimeStr
= aDateTimeStr
.getToken( 0, ' ', nIndex
);
175 sal_uInt16 nDay
= sal::static_int_cast
< sal_uInt16
>( aDateStr
.getToken( 0, '.', nIndex
).toInt32() );
176 sal_uInt16 nMonth
= sal::static_int_cast
< sal_uInt16
>( aDateStr
.getToken( 0, '.', nIndex
).toInt32() );
177 sal_uInt16 nYear
= sal::static_int_cast
< sal_uInt16
>( aDateStr
.getToken( 0, '.', nIndex
).toInt32() );
179 sal_uInt16 nHours
= sal::static_int_cast
< sal_uInt16
>( aTimeStr
.getToken( 0, ':', nIndex
).toInt32() );
180 sal_uInt16 nMinutes
= sal::static_int_cast
< sal_uInt16
>( aTimeStr
.getToken( 0, ':', nIndex
).toInt32() );
181 Date
aDate( nDay
, nMonth
, nYear
);
182 tools::Time
aTime( nHours
, nMinutes
);
183 DateTime
aDateTime( aDate
, aTime
);
185 OUString
aString( aUser
);
187 aString
+= formatTime(aDateTime
, *ScGlobal::pLocaleData
);
189 m_pLbUsers
->InsertEntry( aString
);
195 m_pLbUsers
->InsertEntry( m_aStrNoUserData
);
198 catch ( uno::Exception
& )
200 OSL_FAIL( "ScShareDocumentDlg::UpdateView(): caught exception\n" );
202 m_pLbUsers
->InsertEntry( m_aStrNoUserData
);
208 SvtUserOptions aUserOpt
;
209 OUString aUser
= aUserOpt
.GetFirstName();
210 if ( !aUser
.isEmpty() )
214 aUser
+= aUserOpt
.GetLastName();
215 if ( aUser
.isEmpty() )
219 ::osl::Security aSecurity
;
220 aSecurity
.getUserName( aUserName
);
223 if ( aUser
.isEmpty() )
226 aUser
= m_aStrUnknownUser
;
229 aUser
+= m_aStrExclusiveAccess
;
230 OUString aString
= aUser
+ "\t";
232 uno::Reference
<document::XDocumentPropertiesSupplier
> xDPS(mpDocShell
->GetModel(), uno::UNO_QUERY_THROW
);
233 uno::Reference
<document::XDocumentProperties
> xDocProps
= xDPS
->getDocumentProperties();
235 util::DateTime
uDT(xDocProps
->getModificationDate());
236 DateTime
aDateTime(uDT
);
238 aString
+= formatTime(aDateTime
, *ScGlobal::pLocaleData
);
240 aString
+= ScGlobal::pLocaleData
->getTime( aDateTime
, false );
242 m_pLbUsers
->InsertEntry( aString
);
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */