Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / ui / miscdlgs / sharedocdlg.cxx
blob34ecbd7140b03c8a6f40c988deb8ced594751e2b
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>
31 #include "sharedocdlg.hxx"
32 #include "scresid.hxx"
33 #include "docsh.hxx"
34 #include "viewdata.hxx"
37 using namespace ::com::sun::star;
39 class ScShareTable : public SvSimpleTable
41 private:
42 OUString m_sWidestAccessString;
43 public:
44 ScShareTable(SvSimpleTableContainer& rParent)
45 : SvSimpleTable(rParent)
47 m_sWidestAccessString = getWidestTime(*ScGlobal::pLocaleData);
49 virtual void Resize()
51 SvSimpleTable::Resize();
52 if (isInitialLayout(this))
53 setColWidths();
55 void setColWidths()
57 HeaderBar &rBar = GetTheHeaderBar();
58 if (rBar.GetItemCount() < 2)
59 return;
61 long nAccessedWidth = 12 +
62 std::max(rBar.GetTextWidth(rBar.GetItemText(2)),
63 GetTextWidth(m_sWidestAccessString));
64 long nWebSiteWidth = std::max(
65 12 + rBar.GetTextWidth(rBar.GetItemText(1)),
66 GetSizePixel().Width() - nAccessedWidth);
67 long aStaticTabs[]= { 2, 0, 0 };
68 aStaticTabs[2] = nWebSiteWidth;
69 SvSimpleTable::SetTabs(aStaticTabs, MAP_PIXEL);
73 //=============================================================================
74 // class ScShareDocumentDlg
75 //=============================================================================
77 ScShareDocumentDlg::ScShareDocumentDlg( Window* pParent, ScViewData* pViewData )
78 : ModalDialog(pParent, "ShareDocumentDialog", "modules/scalc/ui/sharedocumentdlg.ui")
79 , mpViewData(pViewData)
80 , mpDocShell(NULL)
82 OSL_ENSURE( mpViewData, "ScShareDocumentDlg CTOR: mpViewData is null!" );
83 mpDocShell = ( mpViewData ? mpViewData->GetDocShell() : NULL );
84 OSL_ENSURE( mpDocShell, "ScShareDocumentDlg CTOR: mpDocShell is null!" );
86 get(m_pCbShare, "share");
87 get(m_pFtWarning, "warning");
89 SvSimpleTableContainer *pCtrl = get<SvSimpleTableContainer>("users");
90 pCtrl->set_height_request(pCtrl->GetTextHeight()*9);
91 m_pLbUsers = new ScShareTable(*pCtrl);
93 m_aStrNoUserData = get<FixedText>("nouserdata")->GetText();
94 m_aStrUnknownUser = get<FixedText>("unknownuser")->GetText();
95 m_aStrExclusiveAccess = get<FixedText>("exclusive")->GetText();
97 bool bIsDocShared = ( mpDocShell ? mpDocShell->IsDocShared() : false );
98 m_pCbShare->Check( bIsDocShared );
99 m_pCbShare->SetToggleHdl( LINK( this, ScShareDocumentDlg, ToggleHandle ) );
100 m_pFtWarning->Enable( bIsDocShared );
102 long nTabs[] = { 2, 0, 0 };
103 m_pLbUsers->SetTabs( nTabs );
105 OUString aHeader(get<FixedText>("name")->GetText());
106 aHeader += "\t";
107 aHeader += get<FixedText>("accessed")->GetText();
108 m_pLbUsers->InsertHeaderEntry( aHeader, HEADERBAR_APPEND, HIB_LEFT | HIB_LEFTIMAGE | HIB_VCENTER );
109 m_pLbUsers->SetSelectionMode( NO_SELECTION );
111 UpdateView();
114 ScShareDocumentDlg::~ScShareDocumentDlg()
116 delete m_pLbUsers;
119 IMPL_LINK_NOARG(ScShareDocumentDlg, ToggleHandle)
121 m_pFtWarning->Enable( m_pCbShare->IsChecked() );
123 return 0;
126 bool ScShareDocumentDlg::IsShareDocumentChecked() const
128 return m_pCbShare->IsChecked();
131 void ScShareDocumentDlg::UpdateView()
133 if ( !mpDocShell )
135 return;
138 if ( mpDocShell->IsDocShared() )
142 ::svt::ShareControlFile aControlFile( mpDocShell->GetSharedFileURL() );
143 uno::Sequence< uno::Sequence< OUString > > aUsersData = aControlFile.GetUsersData();
144 const uno::Sequence< OUString >* pUsersData = aUsersData.getConstArray();
145 sal_Int32 nLength = aUsersData.getLength();
147 if ( nLength > 0 )
149 sal_Int32 nUnknownUser = 1;
151 for ( sal_Int32 i = 0; i < nLength; ++i )
153 if ( pUsersData[i].getLength() > SHARED_EDITTIME_ID )
155 OUString aUser;
156 if ( !pUsersData[i][SHARED_OOOUSERNAME_ID].isEmpty() )
158 aUser = pUsersData[i][SHARED_OOOUSERNAME_ID];
160 else if ( !pUsersData[i][SHARED_SYSUSERNAME_ID].isEmpty() )
162 aUser = pUsersData[i][SHARED_SYSUSERNAME_ID];
164 else
166 aUser = OUString(m_aStrUnknownUser) + " " + OUString::number( nUnknownUser++ );
169 // parse the edit time string of the format "DD.MM.YYYY hh:mm"
170 OUString aDateTimeStr = pUsersData[i][SHARED_EDITTIME_ID];
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 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, NULL );
193 else
195 m_pLbUsers->InsertEntry( m_aStrNoUserData, NULL );
198 catch ( uno::Exception& )
200 OSL_FAIL( "ScShareDocumentDlg::UpdateView(): caught exception\n" );
201 m_pLbUsers->Clear();
202 m_pLbUsers->InsertEntry( m_aStrNoUserData, NULL );
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 Date d(uDT.Day, uDT.Month, uDT.Year);
237 Time t(uDT.Hours, uDT.Minutes, uDT.Seconds, uDT.NanoSeconds);
238 DateTime aDateTime(d,t);
240 aString += formatTime(aDateTime, *ScGlobal::pLocaleData);
241 aString += " ";
242 aString += ScGlobal::pLocaleData->getTime( aDateTime, false );
244 m_pLbUsers->InsertEntry( aString, NULL );
248 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */