merge the formfield patch from ooo-build
[ooovba.git] / svx / source / cui / webconninfo.cxx
blob33bd7f892568847c093e96ff197892c4583ac6dd
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: webconninfo.cxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
34 #ifdef SVX_DLLIMPLEMENTATION
35 #undef SVX_DLLIMPLEMENTATION
36 #endif
38 // include ---------------------------------------------------------------
39 #include <svx/dialmgr.hxx>
40 #ifndef _SVX_DIALOGS_HRC
41 #include <svx/dialogs.hrc>
42 #endif
44 #include <com/sun/star/task/UrlRecord.hpp>
45 #include <com/sun/star/task/XPasswordContainer.hpp>
46 #include <com/sun/star/task/XMasterPasswordHandling.hpp>
47 #include "com/sun/star/task/XUrlContainer.hpp"
49 #include <comphelper/processfactory.hxx>
50 #include <comphelper/docpasswordrequest.hxx>
52 #include "webconninfo.hxx"
53 #include "webconninfo.hrc"
55 using namespace ::com::sun::star;
57 //........................................................................
58 namespace svx
60 //........................................................................
62 // class PasswordTable ---------------------------------------------------
64 PasswordTable::PasswordTable( Window* pParent, const ResId& rResId ) :
65 SvxSimpleTable( pParent, rResId )
67 SetWindowBits( GetStyle() | WB_NOINITIALSELECTION );
70 void PasswordTable::InsertHeaderItem( USHORT nColumn, const String& rText, HeaderBarItemBits nBits )
72 GetTheHeaderBar()->InsertItem( nColumn, rText, 0, nBits );
75 void PasswordTable::ResetTabs()
77 SetTabs();
80 void PasswordTable::Resort( bool bForced )
82 USHORT nColumn = GetSelectedCol();
83 if ( 0 == nColumn || bForced ) // only the first column is sorted
85 HeaderBarItemBits nBits = GetTheHeaderBar()->GetItemBits(1);
86 BOOL bUp = ( ( nBits & HIB_UPARROW ) == HIB_UPARROW );
87 SvSortMode eMode = SortAscending;
89 if ( bUp )
91 nBits &= ~HIB_UPARROW;
92 nBits |= HIB_DOWNARROW;
93 eMode = SortDescending;
95 else
97 nBits &= ~HIB_DOWNARROW;
98 nBits |= HIB_UPARROW;
100 GetTheHeaderBar()->SetItemBits( 1, nBits );
101 SvTreeList* pListModel = GetModel();
102 pListModel->SetSortMode( eMode );
103 pListModel->Resort();
107 // class WebConnectionInfoDialog -----------------------------------------
109 // -----------------------------------------------------------------------
110 WebConnectionInfoDialog::WebConnectionInfoDialog( Window* pParent ) :
111 ModalDialog( pParent, SVX_RES( RID_SVXDLG_WEBCONNECTION_INFO ) )
112 ,m_aNeverShownFI ( this, SVX_RES( FI_NEVERSHOWN ) )
113 ,m_aPasswordsLB ( this, SVX_RES( LB_PASSWORDS ) )
114 ,m_aRemoveBtn ( this, SVX_RES( PB_REMOVE ) )
115 ,m_aRemoveAllBtn ( this, SVX_RES( PB_REMOVEALL ) )
116 ,m_aChangeBtn ( this, SVX_RES( PB_CHANGE ) )
117 ,m_aButtonsFL ( this, SVX_RES( FL_BUTTONS ) )
118 ,m_aCloseBtn ( this, SVX_RES( PB_CLOSE ) )
119 ,m_aHelpBtn ( this, SVX_RES( PB_HELP ) )
120 ,m_nPos ( -1 )
122 static long aStaticTabs[]= { 3, 0, 150, 250 };
123 m_aPasswordsLB.SetTabs( aStaticTabs );
124 m_aPasswordsLB.InsertHeaderItem( 1, SVX_RESSTR( STR_WEBSITE ),
125 HIB_LEFT | HIB_VCENTER | HIB_FIXEDPOS | HIB_CLICKABLE | HIB_UPARROW );
126 m_aPasswordsLB.InsertHeaderItem( 2, SVX_RESSTR( STR_USERNAME ),
127 HIB_LEFT | HIB_VCENTER | HIB_FIXEDPOS );
128 m_aPasswordsLB.ResetTabs();
130 FreeResource();
132 m_aPasswordsLB.SetHeaderBarClickHdl( LINK( this, WebConnectionInfoDialog, HeaderBarClickedHdl ) );
133 m_aRemoveBtn.SetClickHdl( LINK( this, WebConnectionInfoDialog, RemovePasswordHdl ) );
134 m_aRemoveAllBtn.SetClickHdl( LINK( this, WebConnectionInfoDialog, RemoveAllPasswordsHdl ) );
135 m_aChangeBtn.SetClickHdl( LINK( this, WebConnectionInfoDialog, ChangePasswordHdl ) );
137 // one button too small for its text?
138 sal_Int32 i = 0;
139 long nBtnTextWidth = 0;
140 Window* pButtons[] = { &m_aRemoveBtn, &m_aRemoveAllBtn, &m_aChangeBtn };
141 Window** pButton = pButtons;
142 const sal_Int32 nBCount = sizeof( pButtons ) / sizeof( pButtons[ 0 ] );
143 for ( ; i < nBCount; ++i, ++pButton )
145 long nTemp = (*pButton)->GetCtrlTextWidth( (*pButton)->GetText() );
146 if ( nTemp > nBtnTextWidth )
147 nBtnTextWidth = nTemp;
149 nBtnTextWidth = nBtnTextWidth * 115 / 100; // a little offset
150 long nButtonWidth = m_aRemoveBtn.GetSizePixel().Width();
151 if ( nBtnTextWidth > nButtonWidth )
153 // so make the buttons broader and its control in front of it smaller
154 long nDelta = nBtnTextWidth - nButtonWidth;
155 pButton = pButtons;
156 for ( i = 0; i < nBCount; ++i, ++pButton )
158 Point aNewPos = (*pButton)->GetPosPixel();
159 if ( &m_aRemoveAllBtn == (*pButton) )
160 aNewPos.X() += nDelta;
161 else if ( &m_aChangeBtn == (*pButton) )
162 aNewPos.X() -= nDelta;
163 Size aNewSize = (*pButton)->GetSizePixel();
164 aNewSize.Width() += nDelta;
165 (*pButton)->SetPosSizePixel( aNewPos, aNewSize );
169 FillPasswordList();
171 m_aRemoveBtn.SetClickHdl( LINK( this, WebConnectionInfoDialog, RemovePasswordHdl ) );
172 m_aRemoveAllBtn.SetClickHdl( LINK( this, WebConnectionInfoDialog, RemoveAllPasswordsHdl ) );
173 m_aChangeBtn.SetClickHdl( LINK( this, WebConnectionInfoDialog, ChangePasswordHdl ) );
174 m_aPasswordsLB.SetSelectHdl( LINK( this, WebConnectionInfoDialog, EntrySelectedHdl ) );
176 m_aRemoveBtn.Enable( FALSE );
177 m_aChangeBtn.Enable( FALSE );
179 HeaderBarClickedHdl( NULL );
182 // -----------------------------------------------------------------------
183 WebConnectionInfoDialog::~WebConnectionInfoDialog()
187 // -----------------------------------------------------------------------
188 IMPL_LINK( WebConnectionInfoDialog, HeaderBarClickedHdl, SvxSimpleTable*, pTable )
190 m_aPasswordsLB.Resort( NULL == pTable );
191 return 0;
194 // -----------------------------------------------------------------------
195 void WebConnectionInfoDialog::FillPasswordList()
199 uno::Reference< task::XMasterPasswordHandling > xMasterPasswd(
200 comphelper::getProcessServiceFactory()->createInstance(
201 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.task.PasswordContainer" ) ) ),
202 uno::UNO_QUERY );
204 if ( xMasterPasswd.is() && xMasterPasswd->isPersistentStoringAllowed() )
206 uno::Reference< task::XPasswordContainer > xPasswdContainer( xMasterPasswd, uno::UNO_QUERY_THROW );
207 uno::Reference< task::XInteractionHandler > xInteractionHandler(
208 comphelper::getProcessServiceFactory()->createInstance(
209 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.task.InteractionHandler" ) ) ),
210 uno::UNO_QUERY_THROW );
212 uno::Sequence< task::UrlRecord > aURLEntries = xPasswdContainer->getAllPersistent( xInteractionHandler );
213 sal_Int32 nCount = 0;
214 for ( sal_Int32 nURLInd = 0; nURLInd < aURLEntries.getLength(); nURLInd++ )
216 for ( sal_Int32 nUserInd = 0; nUserInd < aURLEntries[nURLInd].UserList.getLength(); nUserInd++ )
218 ::rtl::OUString aUIEntry( aURLEntries[nURLInd].Url );
219 aUIEntry += ::rtl::OUString::valueOf( (sal_Unicode)'\t' );
220 aUIEntry += aURLEntries[nURLInd].UserList[nUserInd].UserName;
221 SvLBoxEntry* pEntry = m_aPasswordsLB.InsertEntry( aUIEntry );
222 pEntry->SetUserData( (void*)(nCount++) );
226 // remember pos of first url container entry.
227 m_nPos = nCount;
229 uno::Reference< task::XUrlContainer > xUrlContainer(
230 xPasswdContainer, uno::UNO_QUERY_THROW );
232 uno::Sequence< rtl::OUString > aUrls
233 = xUrlContainer->getUrls( sal_True /* OnlyPersistent */ );
235 for ( sal_Int32 nURLIdx = 0; nURLIdx < aUrls.getLength(); nURLIdx++ )
237 ::rtl::OUString aUIEntry( aUrls[ nURLIdx ] );
238 aUIEntry += ::rtl::OUString::valueOf( (sal_Unicode)'\t' );
239 aUIEntry += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "*" ) );
240 SvLBoxEntry* pEntry = m_aPasswordsLB.InsertEntry( aUIEntry );
241 pEntry->SetUserData( (void*)(nCount++) );
245 catch( uno::Exception& )
249 // -----------------------------------------------------------------------
250 IMPL_LINK( WebConnectionInfoDialog, RemovePasswordHdl, PushButton*, EMPTYARG )
254 SvLBoxEntry* pEntry = m_aPasswordsLB.GetCurEntry();
255 if ( pEntry )
257 ::rtl::OUString aURL = m_aPasswordsLB.GetEntryText( pEntry, 0 );
258 ::rtl::OUString aUserName = m_aPasswordsLB.GetEntryText( pEntry, 1 );
260 uno::Reference< task::XPasswordContainer > xPasswdContainer(
261 comphelper::getProcessServiceFactory()->createInstance(
262 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
263 "com.sun.star.task.PasswordContainer" ) ) ),
264 uno::UNO_QUERY_THROW );
266 sal_Int32 nPos = (sal_Int32)(sal_IntPtr)pEntry->GetUserData();
267 if ( nPos < m_nPos )
269 xPasswdContainer->removePersistent( aURL, aUserName );
271 else
273 uno::Reference< task::XUrlContainer > xUrlContainer(
274 xPasswdContainer, uno::UNO_QUERY_THROW );
275 xUrlContainer->removeUrl( aURL );
277 m_aPasswordsLB.RemoveEntry( pEntry );
280 catch( uno::Exception& )
283 return 0;
286 // -----------------------------------------------------------------------
287 IMPL_LINK( WebConnectionInfoDialog, RemoveAllPasswordsHdl, PushButton*, EMPTYARG )
291 uno::Reference< task::XPasswordContainer > xPasswdContainer(
292 comphelper::getProcessServiceFactory()->createInstance(
293 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
294 "com.sun.star.task.PasswordContainer" ) ) ),
295 uno::UNO_QUERY_THROW );
297 // should the master password be requested before?
298 xPasswdContainer->removeAllPersistent();
300 uno::Reference< task::XUrlContainer > xUrlContainer(
301 xPasswdContainer, uno::UNO_QUERY_THROW );
302 uno::Sequence< rtl::OUString > aUrls
303 = xUrlContainer->getUrls( sal_True /* OnlyPersistent */ );
304 for ( sal_Int32 nURLIdx = 0; nURLIdx < aUrls.getLength(); nURLIdx++ )
305 xUrlContainer->removeUrl( aUrls[ nURLIdx ] );
307 m_aPasswordsLB.Clear();
309 catch( uno::Exception& )
312 return 0;
315 // -----------------------------------------------------------------------
316 IMPL_LINK( WebConnectionInfoDialog, ChangePasswordHdl, PushButton*, EMPTYARG )
320 SvLBoxEntry* pEntry = m_aPasswordsLB.GetCurEntry();
321 if ( pEntry )
323 ::rtl::OUString aURL = m_aPasswordsLB.GetEntryText( pEntry, 0 );
324 ::rtl::OUString aUserName = m_aPasswordsLB.GetEntryText( pEntry, 1 );
326 ::comphelper::DocPasswordRequest* pPasswordRequest
327 = new ::comphelper::DocPasswordRequest(
328 ::comphelper::DocPasswordRequestType_STANDARD,
329 task::PasswordRequestMode_PASSWORD_CREATE, aURL );
330 uno::Reference< task::XInteractionRequest > rRequest( pPasswordRequest );
332 uno::Reference< task::XInteractionHandler > xInteractionHandler(
333 comphelper::getProcessServiceFactory()->createInstance(
334 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
335 "com.sun.star.task.InteractionHandler" ) ) ),
336 uno::UNO_QUERY_THROW );
337 xInteractionHandler->handle( rRequest );
339 if ( pPasswordRequest->isPassword() )
341 String aNewPass = pPasswordRequest->getPassword();
342 uno::Sequence< ::rtl::OUString > aPasswd( 1 );
343 aPasswd[0] = aNewPass;
345 uno::Reference< task::XPasswordContainer > xPasswdContainer(
346 comphelper::getProcessServiceFactory()->createInstance(
347 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
348 "com.sun.star.task.PasswordContainer" ) ) ),
349 uno::UNO_QUERY_THROW );
350 xPasswdContainer->addPersistent(
351 aURL, aUserName, aPasswd, xInteractionHandler );
355 catch( uno::Exception& )
358 return 0;
361 // -----------------------------------------------------------------------
362 IMPL_LINK( WebConnectionInfoDialog, EntrySelectedHdl, void*, EMPTYARG )
364 SvLBoxEntry* pEntry = m_aPasswordsLB.GetCurEntry();
365 if ( !pEntry )
367 m_aRemoveBtn.Enable( FALSE );
368 m_aChangeBtn.Enable( FALSE );
370 else
372 m_aRemoveBtn.Enable( TRUE );
374 // url container entries (-> use system credentials) have
375 // no password
376 sal_Int32 nPos = (sal_Int32)(sal_IntPtr)pEntry->GetUserData();
377 m_aChangeBtn.Enable( nPos < m_nPos );
380 return 0;
383 //........................................................................
384 } // namespace svx
385 //........................................................................