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