xref: /AOO41X/main/sc/source/ui/miscdlgs/sharedocdlg.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sc.hxx"
30 
31 //-----------------------------------------------------------------------------
32 
33 #include <osl/security.hxx>
34 #include <svl/sharecontrolfile.hxx>
35 #include <unotools/useroptions.hxx>
36 
37 #include <docsh.hxx>
38 
39 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
40 #include <com/sun/star/document/XDocumentProperties.hpp>
41 
42 
43 #include "sharedocdlg.hxx"
44 #include "sharedocdlg.hrc"
45 #include "scresid.hxx"
46 #include "docsh.hxx"
47 #include "viewdata.hxx"
48 
49 
50 using namespace ::com::sun::star;
51 
52 
53 //=============================================================================
54 // class ScShareDocumentDlg
55 //=============================================================================
56 
57 ScShareDocumentDlg::ScShareDocumentDlg( Window* pParent, ScViewData* pViewData )
58     :ModalDialog( pParent, ScResId( RID_SCDLG_SHAREDOCUMENT ) )
59     ,maCbShare            ( this, ScResId( CB_SHARE ) )
60     ,maFtWarning          ( this, ScResId( FT_WARNING ) )
61     ,maFlUsers            ( this, ScResId( FL_USERS ) )
62     ,maFtUsers            ( this, ScResId( FT_USERS ) )
63     ,maLbUsers            ( this, ScResId( LB_USERS ) )
64     ,maFlEnd              ( this, ScResId( FL_END ) )
65     ,maBtnHelp            ( this, ScResId( BTN_HELP ) )
66     ,maBtnOK              ( this, ScResId( BTN_OK ) )
67     ,maBtnCancel          ( this, ScResId( BTN_CANCEL ) )
68     ,maStrTitleName       ( ScResId( STR_TITLE_NAME ) )
69     ,maStrTitleAccessed   ( ScResId( STR_TITLE_ACCESSED ) )
70     ,maStrNoUserData      ( ScResId( STR_NO_USER_DATA ) )
71     ,maStrUnkownUser      ( ScResId( STR_UNKNOWN_USER ) )
72     ,maStrExclusiveAccess ( ScResId( STR_EXCLUSIVE_ACCESS ) )
73     ,mpViewData           ( pViewData )
74     ,mpDocShell           ( NULL )
75 {
76     DBG_ASSERT( mpViewData, "ScShareDocumentDlg CTOR: mpViewData is null!" );
77     mpDocShell = ( mpViewData ? mpViewData->GetDocShell() : NULL );
78     DBG_ASSERT( mpDocShell, "ScShareDocumentDlg CTOR: mpDocShell is null!" );
79 
80     FreeResource();
81 
82     bool bIsDocShared = ( mpDocShell ? mpDocShell->IsDocShared() : false );
83     maCbShare.Check( bIsDocShared );
84     maCbShare.SetToggleHdl( LINK( this, ScShareDocumentDlg, ToggleHandle ) );
85     maFtWarning.Enable( bIsDocShared );
86 
87     long nTabs[] = { 2, 10, 128 };
88     maLbUsers.SetTabs( nTabs );
89 
90     String aHeader( maStrTitleName );
91     aHeader += '\t';
92     aHeader += maStrTitleAccessed;
93     maLbUsers.InsertHeaderEntry( aHeader, HEADERBAR_APPEND, HIB_LEFT | HIB_LEFTIMAGE | HIB_VCENTER );
94     maLbUsers.SetSelectionMode( NO_SELECTION );
95 
96     UpdateView();
97 }
98 
99 ScShareDocumentDlg::~ScShareDocumentDlg()
100 {
101 }
102 
103 IMPL_LINK( ScShareDocumentDlg, ToggleHandle, void*, EMPTYARG )
104 {
105     maFtWarning.Enable( maCbShare.IsChecked() );
106 
107     return 0;
108 }
109 
110 bool ScShareDocumentDlg::IsShareDocumentChecked() const
111 {
112     return maCbShare.IsChecked();
113 }
114 
115 void ScShareDocumentDlg::UpdateView()
116 {
117     if ( !mpDocShell )
118     {
119         return;
120     }
121 
122     if ( mpDocShell->IsDocShared() )
123     {
124         try
125         {
126             ::svt::ShareControlFile aControlFile( mpDocShell->GetSharedFileURL() );
127             uno::Sequence< uno::Sequence< ::rtl::OUString > > aUsersData = aControlFile.GetUsersData();
128             const uno::Sequence< ::rtl::OUString >* pUsersData = aUsersData.getConstArray();
129             sal_Int32 nLength = aUsersData.getLength();
130 
131             if ( nLength > 0 )
132             {
133                 sal_Int32 nUnknownUser = 1;
134 
135                 for ( sal_Int32 i = 0; i < nLength; ++i )
136                 {
137                     if ( pUsersData[i].getLength() > SHARED_EDITTIME_ID )
138                     {
139                         String aUser;
140                         if ( pUsersData[i][SHARED_OOOUSERNAME_ID].getLength() )
141                         {
142                             aUser = pUsersData[i][SHARED_OOOUSERNAME_ID];
143                         }
144                         else if ( pUsersData[i][SHARED_SYSUSERNAME_ID].getLength() )
145                         {
146                             aUser = pUsersData[i][SHARED_SYSUSERNAME_ID];
147                         }
148                         else
149                         {
150                             aUser = maStrUnkownUser;
151                             aUser += ' ';
152                             aUser += String::CreateFromInt32( nUnknownUser++ );
153                         }
154 
155                         // parse the edit time string of the format "DD.MM.YYYY hh:mm"
156                         ::rtl::OUString aDateTimeStr = pUsersData[i][SHARED_EDITTIME_ID];
157                         sal_Int32 nIndex = 0;
158                         ::rtl::OUString aDateStr = aDateTimeStr.getToken( 0, ' ', nIndex );
159                         ::rtl::OUString aTimeStr = aDateTimeStr.getToken( 0, ' ', nIndex );
160                         nIndex = 0;
161                         sal_uInt16 nDay = sal::static_int_cast< sal_uInt16 >( aDateStr.getToken( 0, '.', nIndex ).toInt32() );
162                         sal_uInt16 nMonth = sal::static_int_cast< sal_uInt16 >( aDateStr.getToken( 0, '.', nIndex ).toInt32() );
163                         sal_uInt16 nYear = sal::static_int_cast< sal_uInt16 >( aDateStr.getToken( 0, '.', nIndex ).toInt32() );
164                         nIndex = 0;
165                         sal_uInt16 nHours = sal::static_int_cast< sal_uInt16 >( aTimeStr.getToken( 0, ':', nIndex ).toInt32() );
166                         sal_uInt16 nMinutes = sal::static_int_cast< sal_uInt16 >( aTimeStr.getToken( 0, ':', nIndex ).toInt32() );
167                         Date aDate( nDay, nMonth, nYear );
168                         Time aTime( nHours, nMinutes );
169                         DateTime aDateTime( aDate, aTime );
170 
171                         String aString( aUser );
172                         aString += '\t';
173                         aString += ScGlobal::pLocaleData->getDate( aDateTime );
174                         aString += ' ';
175                         aString += ScGlobal::pLocaleData->getTime( aDateTime, sal_False );
176 
177                         maLbUsers.InsertEntry( aString, NULL );
178                     }
179                 }
180             }
181             else
182             {
183                 maLbUsers.InsertEntry( maStrNoUserData, NULL );
184             }
185         }
186         catch ( uno::Exception& )
187         {
188             DBG_ERROR( "ScShareDocumentDlg::UpdateView(): caught exception\n" );
189             maLbUsers.Clear();
190             maLbUsers.InsertEntry( maStrNoUserData, NULL );
191         }
192     }
193     else
194     {
195         // get OOO user name
196         SvtUserOptions aUserOpt;
197         String aUser = aUserOpt.GetFirstName();
198         if ( aUser.Len() > 0 )
199         {
200             aUser += ' ';
201         }
202         aUser += String(aUserOpt.GetLastName());
203         if ( aUser.Len() == 0 )
204         {
205             // get sys user name
206             ::rtl::OUString aUserName;
207             ::osl::Security aSecurity;
208             aSecurity.getUserName( aUserName );
209             aUser = aUserName;
210         }
211         if ( aUser.Len() == 0 )
212         {
213             // unknown user name
214             aUser = maStrUnkownUser;
215         }
216         aUser += ' ';
217         aUser += maStrExclusiveAccess;
218         String aString( aUser );
219         aString += '\t';
220 
221         uno::Reference<document::XDocumentPropertiesSupplier> xDPS(mpDocShell->GetModel(), uno::UNO_QUERY_THROW);
222         uno::Reference<document::XDocumentProperties> xDocProps = xDPS->getDocumentProperties();
223 
224         util::DateTime uDT(xDocProps->getModificationDate());
225         Date d(uDT.Day, uDT.Month, uDT.Year);
226         Time t(uDT.Hours, uDT.Minutes, uDT.Seconds, uDT.HundredthSeconds);
227         DateTime aDateTime(d,t);
228 
229         aString += ScGlobal::pLocaleData->getDate( aDateTime );
230         aString += ' ';
231         aString += ScGlobal::pLocaleData->getTime( aDateTime, sal_False );
232 
233         maLbUsers.InsertEntry( aString, NULL );
234     }
235 }
236