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