LibreOffice Module sc (master) 1
sharedocdlg.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <o3tl/safeint.hxx>
21#include <osl/security.hxx>
22#include <osl/diagnose.h>
23#include <sfx2/dialoghelper.hxx>
27#include <o3tl/string_view.hxx>
28
29#include <docsh.hxx>
30
31#include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
32#include <com/sun/star/document/XDocumentProperties.hpp>
33
34#include <scresid.hxx>
35#include <sharedocdlg.hxx>
36#include <strings.hrc>
37#include <viewdata.hxx>
38
39using namespace ::com::sun::star;
40
41IMPL_LINK(ScShareDocumentDlg, SizeAllocated, const Size&, rSize, void)
42{
43 OUString sWidestAccessString = getWidestDateTime(ScGlobal::getLocaleData(), false);
44 const int nAccessWidth = m_xLbUsers->get_pixel_size(sWidestAccessString).Width() * 2;
45 std::vector<int> aWidths
46 {
47 o3tl::narrowing<int>(rSize.Width() - nAccessWidth)
48 };
49 m_xLbUsers->set_column_fixed_widths(aWidths);
50}
51
52
54 : GenericDialogController(pParent, "modules/scalc/ui/sharedocumentdlg.ui",
55 "ShareDocumentDialog")
56 , m_aStrNoUserData(ScResId(STR_NO_USER_DATA_AVAILABLE))
57 , m_aStrUnknownUser(ScResId(STR_UNKNOWN_USER_CONFLICT))
58 , m_aStrExclusiveAccess(ScResId(STR_EXCLUSIVE_ACCESS))
59 , mpDocShell(nullptr)
60 , m_xCbShare(m_xBuilder->weld_check_button("share"))
61 , m_xFtWarning(m_xBuilder->weld_label("warning"))
62 , m_xLbUsers(m_xBuilder->weld_tree_view("users"))
63{
64
65 OSL_ENSURE( pViewData, "ScShareDocumentDlg CTOR: mpViewData is null!" );
66 mpDocShell = ( pViewData ? pViewData->GetDocShell() : nullptr );
67 OSL_ENSURE( mpDocShell, "ScShareDocumentDlg CTOR: mpDocShell is null!" );
68
69 std::vector<int> aWidths
70 {
71 o3tl::narrowing<int>(m_xLbUsers->get_approximate_digit_width() * 25)
72 };
73 m_xLbUsers->set_column_fixed_widths(aWidths);
74
75 m_xLbUsers->set_size_request(-1, m_xLbUsers->get_height_rows(9));
76 m_xLbUsers->connect_size_allocate(LINK(this, ScShareDocumentDlg, SizeAllocated));
77
78 bool bIsDocShared = mpDocShell && mpDocShell->IsDocShared();
79 m_xCbShare->set_active(bIsDocShared);
80 m_xCbShare->connect_toggled( LINK( this, ScShareDocumentDlg, ToggleHandle ) );
81 m_xFtWarning->set_sensitive(bIsDocShared);
82
83 m_xLbUsers->set_selection_mode(SelectionMode::NONE);
84
85 UpdateView();
86}
87
89{
90}
91
93{
94 m_xFtWarning->set_sensitive(m_xCbShare->get_active());
95}
96
98{
99 return m_xCbShare->get_active();
100}
101
103{
104 if ( !mpDocShell )
105 {
106 return;
107 }
108
109 if ( mpDocShell->IsDocShared() )
110 {
111 try
112 {
114 std::vector<LockFileEntry> aUsersData = aControlFile.GetUsersData();
115 sal_Int32 nLength = aUsersData.size();
116
117 if ( nLength > 0 )
118 {
119 sal_Int32 nUnknownUser = 1;
120
121 for ( sal_Int32 i = 0; i < nLength; ++i )
122 {
123 if ( !aUsersData[i][LockFileComponent::EDITTIME].isEmpty() )
124 {
125 OUString aUser;
126 if ( !aUsersData[i][LockFileComponent::OOOUSERNAME].isEmpty() )
127 {
128 aUser = aUsersData[i][LockFileComponent::OOOUSERNAME];
129 }
130 else if ( !aUsersData[i][LockFileComponent::SYSUSERNAME].isEmpty() )
131 {
132 aUser = aUsersData[i][LockFileComponent::SYSUSERNAME];
133 }
134 else
135 {
136 aUser = m_aStrUnknownUser + " " + OUString::number( nUnknownUser++ );
137 }
138
139 // parse the edit time string of the format "DD.MM.YYYY hh:mm"
140 OUString aDateTimeStr = aUsersData[i][LockFileComponent::EDITTIME];
141 sal_Int32 nIndex = 0;
142 std::u16string_view aDateStr = o3tl::getToken(aDateTimeStr, 0, ' ', nIndex );
143 std::u16string_view aTimeStr = o3tl::getToken(aDateTimeStr, 0, ' ', nIndex );
144 nIndex = 0;
145 sal_uInt16 nDay = sal::static_int_cast< sal_uInt16 >( o3tl::toInt32(o3tl::getToken(aDateStr, 0, '.', nIndex )) );
146 sal_uInt16 nMonth = sal::static_int_cast< sal_uInt16 >( o3tl::toInt32(o3tl::getToken(aDateStr, 0, '.', nIndex )) );
147 sal_uInt16 nYear = sal::static_int_cast< sal_uInt16 >( o3tl::toInt32(o3tl::getToken(aDateStr, 0, '.', nIndex )) );
148 nIndex = 0;
149 sal_uInt16 nHours = sal::static_int_cast< sal_uInt16 >( o3tl::toInt32(o3tl::getToken(aTimeStr, 0, ':', nIndex )) );
150 sal_uInt16 nMinutes = sal::static_int_cast< sal_uInt16 >( o3tl::toInt32(o3tl::getToken(aTimeStr, 0, ':', nIndex )) );
151 Date aDate( nDay, nMonth, nYear );
152 tools::Time aTime( nHours, nMinutes );
153 DateTime aDateTime( aDate, aTime );
154
155 OUString aString = formatDateTime(aDateTime, ScGlobal::getLocaleData(), false);
156
157 m_xLbUsers->append_text(aUser);
158 m_xLbUsers->set_text(m_xLbUsers->n_children() - 1, aString, 1);
159 }
160 }
161 }
162 else
163 {
164 m_xLbUsers->append_text(m_aStrNoUserData);
165 }
166 }
167 catch ( uno::Exception& )
168 {
169 TOOLS_WARN_EXCEPTION( "sc", "ScShareDocumentDlg::UpdateView()" );
170 m_xLbUsers->clear();
171 m_xLbUsers->append_text(m_aStrNoUserData);
172 }
173 }
174 else
175 {
176 // get OOO user name
177 SvtUserOptions aUserOpt;
178 OUString aUser = aUserOpt.GetFirstName();
179 if ( !aUser.isEmpty() )
180 {
181 aUser += " ";
182 }
183 aUser += aUserOpt.GetLastName();
184 if ( aUser.isEmpty() )
185 {
186 // get sys user name
187 OUString aUserName;
188 ::osl::Security aSecurity;
189 aSecurity.getUserName( aUserName );
190 aUser = aUserName;
191 }
192 if ( aUser.isEmpty() )
193 {
194 // unknown user name
195 aUser = m_aStrUnknownUser;
196 }
197 aUser += " " + m_aStrExclusiveAccess;
198
199 uno::Reference<document::XDocumentPropertiesSupplier> xDPS(mpDocShell->GetModel(), uno::UNO_QUERY_THROW);
200 uno::Reference<document::XDocumentProperties> xDocProps = xDPS->getDocumentProperties();
201
202 util::DateTime uDT(xDocProps->getModificationDate());
203 DateTime aDateTime(uDT);
204
205 OUString aString = formatDateTime(aDateTime, ScGlobal::getLocaleData(), false) + " " +
206 ScGlobal::getLocaleData().getTime( aDateTime, false );
207
208 m_xLbUsers->append_text(aUser);
209 m_xLbUsers->set_text(m_xLbUsers->n_children() - 1, aString, 1);
210 }
211}
212
213/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString getTime(const tools::Time &rTime, bool bSec=true, bool b100Sec=false) const
static SC_DLLPUBLIC const LocaleDataWrapper & getLocaleData()
Definition: global.cxx:1053
OUString m_aStrExclusiveAccess
Definition: sharedocdlg.hxx:33
OUString m_aStrUnknownUser
Definition: sharedocdlg.hxx:32
ScShareDocumentDlg(weld::Window *pParent, const ScViewData *pViewData)
Definition: sharedocdlg.cxx:53
bool IsShareDocumentChecked() const
Definition: sharedocdlg.cxx:97
OUString m_aStrNoUserData
Definition: sharedocdlg.hxx:31
std::unique_ptr< weld::Label > m_xFtWarning
Definition: sharedocdlg.hxx:38
virtual ~ScShareDocumentDlg() override
Definition: sharedocdlg.cxx:88
ScDocShell * mpDocShell
Definition: sharedocdlg.hxx:35
std::unique_ptr< weld::CheckButton > m_xCbShare
Definition: sharedocdlg.hxx:37
std::unique_ptr< weld::TreeView > m_xLbUsers
Definition: sharedocdlg.hxx:39
ScDocShell * GetDocShell() const
Definition: viewdata.hxx:354
OUString GetSharedFileURL() const
bool IsDocShared() const
css::uno::Reference< css::frame::XModel3 > GetModel() const
OUString GetFirstName() const
OUString GetLastName() const
std::vector< LockFileEntry > GetUsersData()
#define TOOLS_WARN_EXCEPTION(area, stream)
OUString SFX2_DLLPUBLIC formatDateTime(const DateTime &rDateTime, const LocaleDataWrapper &rWrapper, bool bWithSec)
OUString SFX2_DLLPUBLIC getWidestDateTime(const LocaleDataWrapper &rWrapper, bool bWithSec)
sal_Int32 nIndex
int i
sal_Int32 toInt32(std::u16string_view str, sal_Int16 radix=10)
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
OUString ScResId(TranslateId aId)
Definition: scdll.cxx:90
IMPL_LINK_NOARG(ScShareDocumentDlg, ToggleHandle, weld::Toggleable &, void)
Definition: sharedocdlg.cxx:92
IMPL_LINK(ScShareDocumentDlg, SizeAllocated, const Size &, rSize, void)
Definition: sharedocdlg.cxx:41
sal_Int32 nLength