xref: /AOO41X/main/cui/source/options/webconninfo.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
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_cui.hxx"
30 
31 // include ---------------------------------------------------------------
32 #include <dialmgr.hxx>
33 #include <cuires.hrc>
34 #include <com/sun/star/task/UrlRecord.hpp>
35 #include <com/sun/star/task/XPasswordContainer.hpp>
36 #include <com/sun/star/task/XMasterPasswordHandling.hpp>
37 #include "com/sun/star/task/XUrlContainer.hpp"
38 #include <comphelper/processfactory.hxx>
39 #include <comphelper/docpasswordrequest.hxx>
40 #include "webconninfo.hxx"
41 #include "webconninfo.hrc"
42 
43 using namespace ::com::sun::star;
44 
45 //........................................................................
46 namespace svx
47 {
48 //........................................................................
49 
50 // class PasswordTable ---------------------------------------------------
51 
52 PasswordTable::PasswordTable( Window* pParent, const ResId& rResId ) :
53     SvxSimpleTable( pParent, rResId )
54 {
55     SetStyle( GetStyle() | WB_NOINITIALSELECTION );
56 }
57 
58 void PasswordTable::InsertHeaderItem( sal_uInt16 nColumn, const String& rText, HeaderBarItemBits nBits )
59 {
60     GetTheHeaderBar()->InsertItem( nColumn, rText, 0, nBits );
61 }
62 
63 void PasswordTable::ResetTabs()
64 {
65     SetTabs();
66 }
67 
68 void PasswordTable::Resort( bool bForced )
69 {
70     sal_uInt16 nColumn = GetSelectedCol();
71     if ( 0 == nColumn || bForced ) // only the first column is sorted
72     {
73         HeaderBarItemBits nBits = GetTheHeaderBar()->GetItemBits(1);
74         sal_Bool bUp = ( ( nBits & HIB_UPARROW ) == HIB_UPARROW );
75         SvSortMode eMode = SortAscending;
76 
77         if ( bUp )
78         {
79             nBits &= ~HIB_UPARROW;
80             nBits |= HIB_DOWNARROW;
81             eMode = SortDescending;
82         }
83         else
84         {
85             nBits &= ~HIB_DOWNARROW;
86             nBits |= HIB_UPARROW;
87         }
88         GetTheHeaderBar()->SetItemBits( 1, nBits );
89         SvTreeList* pListModel = GetModel();
90         pListModel->SetSortMode( eMode );
91         pListModel->Resort();
92     }
93 }
94 
95 // class WebConnectionInfoDialog -----------------------------------------
96 
97 // -----------------------------------------------------------------------
98 WebConnectionInfoDialog::WebConnectionInfoDialog( Window* pParent ) :
99      ModalDialog( pParent, CUI_RES( RID_SVXDLG_WEBCONNECTION_INFO ) )
100     ,m_aNeverShownFI    ( this, CUI_RES( FI_NEVERSHOWN ) )
101     ,m_aPasswordsLB     ( this, CUI_RES( LB_PASSWORDS ) )
102     ,m_aRemoveBtn       ( this, CUI_RES( PB_REMOVE ) )
103     ,m_aRemoveAllBtn    ( this, CUI_RES( PB_REMOVEALL ) )
104     ,m_aChangeBtn       ( this, CUI_RES( PB_CHANGE ) )
105     ,m_aButtonsFL       ( this, CUI_RES( FL_BUTTONS ) )
106     ,m_aCloseBtn        ( this, CUI_RES( PB_CLOSE ) )
107     ,m_aHelpBtn         ( this, CUI_RES( PB_HELP ) )
108     ,m_nPos             ( -1 )
109 {
110     static long aStaticTabs[]= { 3, 0, 150, 250 };
111     m_aPasswordsLB.SetTabs( aStaticTabs );
112     m_aPasswordsLB.InsertHeaderItem( 1, CUI_RESSTR( STR_WEBSITE ),
113         HIB_LEFT | HIB_VCENTER | HIB_FIXEDPOS | HIB_CLICKABLE | HIB_UPARROW );
114     m_aPasswordsLB.InsertHeaderItem( 2, CUI_RESSTR( STR_USERNAME ),
115         HIB_LEFT | HIB_VCENTER | HIB_FIXEDPOS );
116     m_aPasswordsLB.ResetTabs();
117 
118     FreeResource();
119 
120     m_aPasswordsLB.SetHeaderBarClickHdl( LINK( this, WebConnectionInfoDialog, HeaderBarClickedHdl ) );
121     m_aRemoveBtn.SetClickHdl( LINK( this, WebConnectionInfoDialog, RemovePasswordHdl ) );
122     m_aRemoveAllBtn.SetClickHdl( LINK( this, WebConnectionInfoDialog, RemoveAllPasswordsHdl ) );
123     m_aChangeBtn.SetClickHdl( LINK( this, WebConnectionInfoDialog, ChangePasswordHdl ) );
124 
125     // one button too small for its text?
126     sal_Int32 i = 0;
127     long nBtnTextWidth = 0;
128     Window* pButtons[] = { &m_aRemoveBtn, &m_aRemoveAllBtn, &m_aChangeBtn };
129     Window** pButton = pButtons;
130     const sal_Int32 nBCount = sizeof( pButtons ) / sizeof( pButtons[ 0 ] );
131     for ( ; i < nBCount; ++i, ++pButton )
132     {
133         long nTemp = (*pButton)->GetCtrlTextWidth( (*pButton)->GetText() );
134         if ( nTemp > nBtnTextWidth )
135             nBtnTextWidth = nTemp;
136     }
137     nBtnTextWidth = nBtnTextWidth * 115 / 100; // a little offset
138     long nButtonWidth = m_aRemoveBtn.GetSizePixel().Width();
139     if ( nBtnTextWidth > nButtonWidth )
140     {
141         // so make the buttons broader and its control in front of it smaller
142         long nDelta = nBtnTextWidth - nButtonWidth;
143         pButton = pButtons;
144         for ( i = 0; i < nBCount; ++i, ++pButton )
145         {
146             Point aNewPos = (*pButton)->GetPosPixel();
147             if ( &m_aRemoveAllBtn == (*pButton) )
148                 aNewPos.X() += nDelta;
149             else if ( &m_aChangeBtn == (*pButton) )
150                 aNewPos.X() -= nDelta;
151             Size aNewSize = (*pButton)->GetSizePixel();
152             aNewSize.Width() += nDelta;
153             (*pButton)->SetPosSizePixel( aNewPos, aNewSize );
154         }
155     }
156 
157     FillPasswordList();
158 
159     m_aRemoveBtn.SetClickHdl( LINK( this, WebConnectionInfoDialog, RemovePasswordHdl ) );
160     m_aRemoveAllBtn.SetClickHdl( LINK( this, WebConnectionInfoDialog, RemoveAllPasswordsHdl ) );
161     m_aChangeBtn.SetClickHdl( LINK( this, WebConnectionInfoDialog, ChangePasswordHdl ) );
162     m_aPasswordsLB.SetSelectHdl( LINK( this, WebConnectionInfoDialog, EntrySelectedHdl ) );
163 
164     m_aRemoveBtn.Enable( sal_False );
165     m_aChangeBtn.Enable( sal_False );
166 
167     HeaderBarClickedHdl( NULL );
168 }
169 
170 // -----------------------------------------------------------------------
171 WebConnectionInfoDialog::~WebConnectionInfoDialog()
172 {
173 }
174 
175 // -----------------------------------------------------------------------
176 IMPL_LINK( WebConnectionInfoDialog, HeaderBarClickedHdl, SvxSimpleTable*, pTable )
177 {
178     m_aPasswordsLB.Resort( NULL == pTable );
179     return 0;
180 }
181 
182 // -----------------------------------------------------------------------
183 void WebConnectionInfoDialog::FillPasswordList()
184 {
185     try
186     {
187         uno::Reference< task::XMasterPasswordHandling > xMasterPasswd(
188             comphelper::getProcessServiceFactory()->createInstance(
189                 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.task.PasswordContainer" ) ) ),
190             uno::UNO_QUERY );
191 
192         if ( xMasterPasswd.is() && xMasterPasswd->isPersistentStoringAllowed() )
193         {
194             uno::Reference< task::XPasswordContainer > xPasswdContainer( xMasterPasswd, uno::UNO_QUERY_THROW );
195             uno::Reference< task::XInteractionHandler > xInteractionHandler(
196                 comphelper::getProcessServiceFactory()->createInstance(
197                     rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.task.InteractionHandler" ) ) ),
198                 uno::UNO_QUERY_THROW );
199 
200             uno::Sequence< task::UrlRecord > aURLEntries = xPasswdContainer->getAllPersistent( xInteractionHandler );
201             sal_Int32 nCount = 0;
202             for ( sal_Int32 nURLInd = 0; nURLInd < aURLEntries.getLength(); nURLInd++ )
203             {
204                 for ( sal_Int32 nUserInd = 0; nUserInd < aURLEntries[nURLInd].UserList.getLength(); nUserInd++ )
205                 {
206                     ::rtl::OUString aUIEntry( aURLEntries[nURLInd].Url );
207                     aUIEntry += ::rtl::OUString::valueOf( (sal_Unicode)'\t' );
208                     aUIEntry += aURLEntries[nURLInd].UserList[nUserInd].UserName;
209                     SvLBoxEntry* pEntry = m_aPasswordsLB.InsertEntry( aUIEntry );
210                     pEntry->SetUserData( (void*)(nCount++) );
211                 }
212             }
213 
214             // remember pos of first url container entry.
215             m_nPos = nCount;
216 
217             uno::Reference< task::XUrlContainer > xUrlContainer(
218                 xPasswdContainer, uno::UNO_QUERY_THROW );
219 
220             uno::Sequence< rtl::OUString > aUrls
221                 = xUrlContainer->getUrls( sal_True /* OnlyPersistent */ );
222 
223             for ( sal_Int32 nURLIdx = 0; nURLIdx < aUrls.getLength(); nURLIdx++ )
224             {
225                 ::rtl::OUString aUIEntry( aUrls[ nURLIdx ] );
226                 aUIEntry += ::rtl::OUString::valueOf( (sal_Unicode)'\t' );
227                 aUIEntry += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "*" ) );
228                 SvLBoxEntry* pEntry = m_aPasswordsLB.InsertEntry( aUIEntry );
229                 pEntry->SetUserData( (void*)(nCount++) );
230             }
231         }
232     }
233     catch( uno::Exception& )
234     {}
235 }
236 
237 // -----------------------------------------------------------------------
238 IMPL_LINK( WebConnectionInfoDialog, RemovePasswordHdl, PushButton*, EMPTYARG )
239 {
240     try
241     {
242         SvLBoxEntry* pEntry = m_aPasswordsLB.GetCurEntry();
243         if ( pEntry )
244         {
245             ::rtl::OUString aURL = m_aPasswordsLB.GetEntryText( pEntry, 0 );
246             ::rtl::OUString aUserName = m_aPasswordsLB.GetEntryText( pEntry, 1 );
247 
248             uno::Reference< task::XPasswordContainer > xPasswdContainer(
249                 comphelper::getProcessServiceFactory()->createInstance(
250                     rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
251                         "com.sun.star.task.PasswordContainer" ) ) ),
252                 uno::UNO_QUERY_THROW );
253 
254             sal_Int32 nPos = (sal_Int32)(sal_IntPtr)pEntry->GetUserData();
255             if ( nPos < m_nPos )
256             {
257                 xPasswdContainer->removePersistent( aURL, aUserName );
258             }
259             else
260             {
261                 uno::Reference< task::XUrlContainer > xUrlContainer(
262                     xPasswdContainer, uno::UNO_QUERY_THROW );
263                 xUrlContainer->removeUrl( aURL );
264             }
265             m_aPasswordsLB.RemoveEntry( pEntry );
266         }
267     }
268     catch( uno::Exception& )
269     {}
270 
271     return 0;
272 }
273 
274 // -----------------------------------------------------------------------
275 IMPL_LINK( WebConnectionInfoDialog, RemoveAllPasswordsHdl, PushButton*, EMPTYARG )
276 {
277     try
278     {
279         uno::Reference< task::XPasswordContainer > xPasswdContainer(
280             comphelper::getProcessServiceFactory()->createInstance(
281                 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
282                     "com.sun.star.task.PasswordContainer" ) ) ),
283             uno::UNO_QUERY_THROW );
284 
285         // should the master password be requested before?
286         xPasswdContainer->removeAllPersistent();
287 
288         uno::Reference< task::XUrlContainer > xUrlContainer(
289             xPasswdContainer, uno::UNO_QUERY_THROW );
290         uno::Sequence< rtl::OUString > aUrls
291             = xUrlContainer->getUrls( sal_True /* OnlyPersistent */ );
292         for ( sal_Int32 nURLIdx = 0; nURLIdx < aUrls.getLength(); nURLIdx++ )
293             xUrlContainer->removeUrl( aUrls[ nURLIdx ] );
294 
295         m_aPasswordsLB.Clear();
296     }
297     catch( uno::Exception& )
298     {}
299 
300     return 0;
301 }
302 
303 // -----------------------------------------------------------------------
304 IMPL_LINK( WebConnectionInfoDialog, ChangePasswordHdl, PushButton*, EMPTYARG )
305 {
306     try
307     {
308         SvLBoxEntry* pEntry = m_aPasswordsLB.GetCurEntry();
309         if ( pEntry )
310         {
311             ::rtl::OUString aURL = m_aPasswordsLB.GetEntryText( pEntry, 0 );
312             ::rtl::OUString aUserName = m_aPasswordsLB.GetEntryText( pEntry, 1 );
313 
314             ::comphelper::SimplePasswordRequest* pPasswordRequest
315                   = new ::comphelper::SimplePasswordRequest( task::PasswordRequestMode_PASSWORD_CREATE );
316             uno::Reference< task::XInteractionRequest > rRequest( pPasswordRequest );
317 
318             uno::Reference< task::XInteractionHandler > xInteractionHandler(
319                 comphelper::getProcessServiceFactory()->createInstance(
320                     rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
321                         "com.sun.star.task.InteractionHandler" ) ) ),
322                 uno::UNO_QUERY_THROW );
323             xInteractionHandler->handle( rRequest );
324 
325             if ( pPasswordRequest->isPassword() )
326             {
327                 String aNewPass = pPasswordRequest->getPassword();
328                 uno::Sequence< ::rtl::OUString > aPasswd( 1 );
329                 aPasswd[0] = aNewPass;
330 
331                 uno::Reference< task::XPasswordContainer > xPasswdContainer(
332                     comphelper::getProcessServiceFactory()->createInstance(
333                         rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
334                             "com.sun.star.task.PasswordContainer" ) ) ),
335                     uno::UNO_QUERY_THROW );
336                 xPasswdContainer->addPersistent(
337                     aURL, aUserName, aPasswd, xInteractionHandler );
338             }
339         }
340     }
341     catch( uno::Exception& )
342     {}
343 
344     return 0;
345 }
346 
347 // -----------------------------------------------------------------------
348 IMPL_LINK( WebConnectionInfoDialog, EntrySelectedHdl, void*, EMPTYARG )
349 {
350     SvLBoxEntry* pEntry = m_aPasswordsLB.GetCurEntry();
351 	if ( !pEntry )
352     {
353         m_aRemoveBtn.Enable( sal_False );
354         m_aChangeBtn.Enable( sal_False );
355     }
356     else
357 	{
358         m_aRemoveBtn.Enable( sal_True );
359 
360         // url container entries (-> use system credentials) have
361         // no password
362         sal_Int32 nPos = (sal_Int32)(sal_IntPtr)pEntry->GetUserData();
363         m_aChangeBtn.Enable( nPos < m_nPos );
364 	}
365 
366 	return 0;
367 }
368 
369 //........................................................................
370 }   // namespace svx
371 //........................................................................
372 
373