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