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 <o3tl/safeint.hxx>
21 #include <osl/security.hxx>
22 #include <osl/diagnose.h>
23 #include <sfx2/dialoghelper.hxx>
24 #include <svl/sharecontrolfile.hxx>
25 #include <unotools/useroptions.hxx>
26 #include <comphelper/diagnose_ex.hxx>
27 #include <o3tl/string_view.hxx>
31 #include <com/sun/star/document/XDocumentProperties.hpp>
33 #include <scresid.hxx>
34 #include <sharedocdlg.hxx>
35 #include <strings.hrc>
36 #include <viewdata.hxx>
38 using namespace ::com::sun::star
;
40 IMPL_LINK(ScShareDocumentDlg
, SizeAllocated
, const Size
&, rSize
, void)
42 OUString sWidestAccessString
= getWidestDateTime(ScGlobal::getLocaleData(), false);
43 const int nAccessWidth
= m_xLbUsers
->get_pixel_size(sWidestAccessString
).Width() * 2;
44 std::vector
<int> aWidths
46 o3tl::narrowing
<int>(rSize
.Width() - nAccessWidth
)
48 m_xLbUsers
->set_column_fixed_widths(aWidths
);
52 ScShareDocumentDlg::ScShareDocumentDlg(weld::Window
* pParent
, const ScViewData
* pViewData
)
53 : GenericDialogController(pParent
, u
"modules/scalc/ui/sharedocumentdlg.ui"_ustr
,
54 u
"ShareDocumentDialog"_ustr
)
55 , m_aStrNoUserData(ScResId(STR_NO_USER_DATA_AVAILABLE
))
56 , m_aStrUnknownUser(ScResId(STR_UNKNOWN_USER_CONFLICT
))
57 , m_aStrExclusiveAccess(ScResId(STR_EXCLUSIVE_ACCESS
))
59 , m_xCbShare(m_xBuilder
->weld_check_button(u
"share"_ustr
))
60 , m_xFtWarning(m_xBuilder
->weld_label(u
"warning"_ustr
))
61 , m_xLbUsers(m_xBuilder
->weld_tree_view(u
"users"_ustr
))
64 OSL_ENSURE( pViewData
, "ScShareDocumentDlg CTOR: mpViewData is null!" );
65 mpDocShell
= ( pViewData
? pViewData
->GetDocShell() : nullptr );
66 OSL_ENSURE( mpDocShell
, "ScShareDocumentDlg CTOR: mpDocShell is null!" );
68 std::vector
<int> aWidths
70 o3tl::narrowing
<int>(m_xLbUsers
->get_approximate_digit_width() * 25)
72 m_xLbUsers
->set_column_fixed_widths(aWidths
);
74 m_xLbUsers
->set_size_request(-1, m_xLbUsers
->get_height_rows(9));
75 m_xLbUsers
->connect_size_allocate(LINK(this, ScShareDocumentDlg
, SizeAllocated
));
77 bool bIsDocShared
= mpDocShell
&& mpDocShell
->IsDocShared();
78 m_xCbShare
->set_active(bIsDocShared
);
79 m_xCbShare
->connect_toggled( LINK( this, ScShareDocumentDlg
, ToggleHandle
) );
80 m_xFtWarning
->set_sensitive(bIsDocShared
);
82 m_xLbUsers
->set_selection_mode(SelectionMode::NONE
);
87 ScShareDocumentDlg::~ScShareDocumentDlg()
91 IMPL_LINK_NOARG(ScShareDocumentDlg
, ToggleHandle
, weld::Toggleable
&, void)
93 m_xFtWarning
->set_sensitive(m_xCbShare
->get_active());
96 bool ScShareDocumentDlg::IsShareDocumentChecked() const
98 return m_xCbShare
->get_active();
101 void ScShareDocumentDlg::UpdateView()
108 if ( mpDocShell
->IsDocShared() )
112 ::svt::ShareControlFile
aControlFile( mpDocShell
->GetSharedFileURL() );
113 std::vector
<LockFileEntry
> aUsersData
= aControlFile
.GetUsersData();
114 sal_Int32 nLength
= aUsersData
.size();
118 sal_Int32 nUnknownUser
= 1;
120 for ( sal_Int32 i
= 0; i
< nLength
; ++i
)
122 if ( !aUsersData
[i
][LockFileComponent::EDITTIME
].isEmpty() )
125 if ( !aUsersData
[i
][LockFileComponent::OOOUSERNAME
].isEmpty() )
127 aUser
= aUsersData
[i
][LockFileComponent::OOOUSERNAME
];
129 else if ( !aUsersData
[i
][LockFileComponent::SYSUSERNAME
].isEmpty() )
131 aUser
= aUsersData
[i
][LockFileComponent::SYSUSERNAME
];
135 aUser
= m_aStrUnknownUser
+ " " + OUString::number( nUnknownUser
++ );
138 // parse the edit time string of the format "DD.MM.YYYY hh:mm"
139 OUString aDateTimeStr
= aUsersData
[i
][LockFileComponent::EDITTIME
];
140 sal_Int32 nIndex
= 0;
141 std::u16string_view aDateStr
= o3tl::getToken(aDateTimeStr
, 0, ' ', nIndex
);
142 std::u16string_view aTimeStr
= o3tl::getToken(aDateTimeStr
, 0, ' ', nIndex
);
144 sal_uInt16 nDay
= sal::static_int_cast
< sal_uInt16
>( o3tl::toInt32(o3tl::getToken(aDateStr
, 0, '.', nIndex
)) );
145 sal_uInt16 nMonth
= sal::static_int_cast
< sal_uInt16
>( o3tl::toInt32(o3tl::getToken(aDateStr
, 0, '.', nIndex
)) );
146 sal_uInt16 nYear
= sal::static_int_cast
< sal_uInt16
>( o3tl::toInt32(o3tl::getToken(aDateStr
, 0, '.', nIndex
)) );
148 sal_uInt16 nHours
= sal::static_int_cast
< sal_uInt16
>( o3tl::toInt32(o3tl::getToken(aTimeStr
, 0, ':', nIndex
)) );
149 sal_uInt16 nMinutes
= sal::static_int_cast
< sal_uInt16
>( o3tl::toInt32(o3tl::getToken(aTimeStr
, 0, ':', nIndex
)) );
150 Date
aDate( nDay
, nMonth
, nYear
);
151 tools::Time
aTime( nHours
, nMinutes
);
152 DateTime
aDateTime( aDate
, aTime
);
154 OUString aString
= formatDateTime(aDateTime
, ScGlobal::getLocaleData(), false);
156 m_xLbUsers
->append_text(aUser
);
157 m_xLbUsers
->set_text(m_xLbUsers
->n_children() - 1, aString
, 1);
163 m_xLbUsers
->append_text(m_aStrNoUserData
);
166 catch ( uno::Exception
& )
168 TOOLS_WARN_EXCEPTION( "sc", "ScShareDocumentDlg::UpdateView()" );
170 m_xLbUsers
->append_text(m_aStrNoUserData
);
176 SvtUserOptions aUserOpt
;
177 OUString aUser
= aUserOpt
.GetFirstName();
178 if ( !aUser
.isEmpty() )
182 aUser
+= aUserOpt
.GetLastName();
183 if ( aUser
.isEmpty() )
187 ::osl::Security aSecurity
;
188 aSecurity
.getUserName( aUserName
);
191 if ( aUser
.isEmpty() )
194 aUser
= m_aStrUnknownUser
;
196 aUser
+= " " + m_aStrExclusiveAccess
;
198 uno::Reference
<document::XDocumentProperties
> xDocProps
= mpDocShell
->GetModel()->getDocumentProperties();
200 util::DateTime
uDT(xDocProps
->getModificationDate());
201 DateTime
aDateTime(uDT
);
203 OUString aString
= formatDateTime(aDateTime
, ScGlobal::getLocaleData(), false) + " " +
204 ScGlobal::getLocaleData().getTime( aDateTime
, false );
206 m_xLbUsers
->append_text(aUser
);
207 m_xLbUsers
->set_text(m_xLbUsers
->n_children() - 1, aString
, 1);
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */