Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / sc / source / ui / miscdlgs / sharedocdlg.cxx
blob213b93a4cdf44cd80f31777171992cf99c0fb897
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
25 #include <docsh.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
38 private:
39 OUString m_sWidestAccessString;
40 public:
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))
50 setColWidths();
52 void setColWidths()
54 HeaderBar &rBar = GetTheHeaderBar();
55 if (rBar.GetItemCount() < 2)
56 return;
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)
75 , mpDocShell(nullptr)
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());
101 aHeader += "\t";
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 );
106 UpdateView();
109 ScShareDocumentDlg::~ScShareDocumentDlg()
111 disposeOnce();
114 void ScShareDocumentDlg::dispose()
116 m_pLbUsers.disposeAndClear();
117 m_pCbShare.clear();
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()
134 if ( !mpDocShell )
136 return;
139 if ( mpDocShell->IsDocShared() )
143 ::svt::ShareControlFile aControlFile( mpDocShell->GetSharedFileURL() );
144 std::vector<LockFileEntry> aUsersData = aControlFile.GetUsersData();
145 sal_Int32 nLength = aUsersData.size();
147 if ( nLength > 0 )
149 sal_Int32 nUnknownUser = 1;
151 for ( sal_Int32 i = 0; i < nLength; ++i )
153 if ( !aUsersData[i][LockFileComponent::EDITTIME].isEmpty() )
155 OUString aUser;
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];
164 else
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 );
174 nIndex = 0;
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() );
178 nIndex = 0;
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 );
186 aString += "\t";
187 aString += formatTime(aDateTime, *ScGlobal::pLocaleData);
189 m_pLbUsers->InsertEntry( aString );
193 else
195 m_pLbUsers->InsertEntry( m_aStrNoUserData );
198 catch ( uno::Exception& )
200 OSL_FAIL( "ScShareDocumentDlg::UpdateView(): caught exception\n" );
201 m_pLbUsers->Clear();
202 m_pLbUsers->InsertEntry( m_aStrNoUserData );
205 else
207 // get OOO user name
208 SvtUserOptions aUserOpt;
209 OUString aUser = aUserOpt.GetFirstName();
210 if ( !aUser.isEmpty() )
212 aUser += " ";
214 aUser += aUserOpt.GetLastName();
215 if ( aUser.isEmpty() )
217 // get sys user name
218 OUString aUserName;
219 ::osl::Security aSecurity;
220 aSecurity.getUserName( aUserName );
221 aUser = aUserName;
223 if ( aUser.isEmpty() )
225 // unknown user name
226 aUser = m_aStrUnknownUser;
228 aUser += " ";
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);
239 aString += " ";
240 aString += ScGlobal::pLocaleData->getTime( aDateTime, false );
242 m_pLbUsers->InsertEntry( aString );
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */