Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / cui / source / options / webconninfo.cxx
blob212e9901e269a082054cd4d6b4bac0ae902fd31e
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 "webconninfo.hxx"
21 #include "webconninfo.hrc"
22 #include <dialmgr.hxx>
23 #include <cuires.hrc>
24 #include <sal/macros.h>
25 #include <com/sun/star/task/InteractionHandler.hpp>
26 #include <com/sun/star/task/PasswordContainer.hpp>
27 #include <com/sun/star/task/UrlRecord.hpp>
28 #include <com/sun/star/task/XPasswordContainer2.hpp>
29 #include <comphelper/processfactory.hxx>
30 #include <comphelper/docpasswordrequest.hxx>
31 #include "svtools/treelistentry.hxx"
33 using namespace ::com::sun::star;
35 //........................................................................
36 namespace svx
38 //........................................................................
40 // class PasswordTable ---------------------------------------------------
42 PasswordTable::PasswordTable(SvxSimpleTableContainer& rParent, WinBits nBits)
43 : SvxSimpleTable(rParent, nBits | WB_NOINITIALSELECTION)
47 void PasswordTable::InsertHeaderItem( sal_uInt16 nColumn, const String& rText, HeaderBarItemBits nBits )
49 GetTheHeaderBar().InsertItem( nColumn, rText, 0, nBits );
52 void PasswordTable::ResetTabs()
54 SetTabs();
57 void PasswordTable::Resort( bool bForced )
59 sal_uInt16 nColumn = GetSelectedCol();
60 if ( 0 == nColumn || bForced ) // only the first column is sorted
62 HeaderBarItemBits nBits = GetTheHeaderBar().GetItemBits(1);
63 sal_Bool bUp = ( ( nBits & HIB_UPARROW ) == HIB_UPARROW );
64 SvSortMode eMode = SortAscending;
66 if ( bUp )
68 nBits &= ~HIB_UPARROW;
69 nBits |= HIB_DOWNARROW;
70 eMode = SortDescending;
72 else
74 nBits &= ~HIB_DOWNARROW;
75 nBits |= HIB_UPARROW;
77 GetTheHeaderBar().SetItemBits( 1, nBits );
78 SvTreeList* pListModel = GetModel();
79 pListModel->SetSortMode( eMode );
80 pListModel->Resort();
84 // class WebConnectionInfoDialog -----------------------------------------
86 // -----------------------------------------------------------------------
87 WebConnectionInfoDialog::WebConnectionInfoDialog( Window* pParent ) :
88 ModalDialog( pParent, CUI_RES( RID_SVXDLG_WEBCONNECTION_INFO ) )
89 ,m_aNeverShownFI ( this, CUI_RES( FI_NEVERSHOWN ) )
90 ,m_aPasswordsLBContainer(this, CUI_RES( LB_PASSWORDS))
91 ,m_aPasswordsLB(m_aPasswordsLBContainer)
92 ,m_aRemoveBtn ( this, CUI_RES( PB_REMOVE ) )
93 ,m_aRemoveAllBtn ( this, CUI_RES( PB_REMOVEALL ) )
94 ,m_aChangeBtn ( this, CUI_RES( PB_CHANGE ) )
95 ,m_aButtonsFL ( this, CUI_RES( FL_BUTTONS ) )
96 ,m_aCloseBtn ( this, CUI_RES( PB_CLOSE ) )
97 ,m_aHelpBtn ( this, CUI_RES( PB_HELP ) )
98 ,m_nPos ( -1 )
100 static long aStaticTabs[]= { 3, 0, 150, 250 };
101 m_aPasswordsLB.SetTabs( aStaticTabs );
102 m_aPasswordsLB.InsertHeaderItem( 1, CUI_RESSTR( STR_WEBSITE ),
103 HIB_LEFT | HIB_VCENTER | HIB_FIXEDPOS | HIB_CLICKABLE | HIB_UPARROW );
104 m_aPasswordsLB.InsertHeaderItem( 2, CUI_RESSTR( STR_USERNAME ),
105 HIB_LEFT | HIB_VCENTER | HIB_FIXEDPOS );
106 m_aPasswordsLB.ResetTabs();
108 FreeResource();
110 m_aPasswordsLB.SetHeaderBarClickHdl( LINK( this, WebConnectionInfoDialog, HeaderBarClickedHdl ) );
111 m_aRemoveBtn.SetClickHdl( LINK( this, WebConnectionInfoDialog, RemovePasswordHdl ) );
112 m_aRemoveAllBtn.SetClickHdl( LINK( this, WebConnectionInfoDialog, RemoveAllPasswordsHdl ) );
113 m_aChangeBtn.SetClickHdl( LINK( this, WebConnectionInfoDialog, ChangePasswordHdl ) );
115 // one button too small for its text?
116 sal_Int32 i = 0;
117 long nBtnTextWidth = 0;
118 Window* pButtons[] = { &m_aRemoveBtn, &m_aRemoveAllBtn, &m_aChangeBtn };
119 Window** pButton = pButtons;
120 const sal_Int32 nBCount = SAL_N_ELEMENTS( pButtons );
121 for ( ; i < nBCount; ++i, ++pButton )
123 long nTemp = (*pButton)->GetCtrlTextWidth( (*pButton)->GetText() );
124 if ( nTemp > nBtnTextWidth )
125 nBtnTextWidth = nTemp;
127 nBtnTextWidth = nBtnTextWidth * 115 / 100; // a little offset
128 long nButtonWidth = m_aRemoveBtn.GetSizePixel().Width();
129 if ( nBtnTextWidth > nButtonWidth )
131 // so make the buttons broader and its control in front of it smaller
132 long nDelta = nBtnTextWidth - nButtonWidth;
133 pButton = pButtons;
134 for ( i = 0; i < nBCount; ++i, ++pButton )
136 Point aNewPos = (*pButton)->GetPosPixel();
137 if ( &m_aRemoveAllBtn == (*pButton) )
138 aNewPos.X() += nDelta;
139 else if ( &m_aChangeBtn == (*pButton) )
140 aNewPos.X() -= nDelta;
141 Size aNewSize = (*pButton)->GetSizePixel();
142 aNewSize.Width() += nDelta;
143 (*pButton)->SetPosSizePixel( aNewPos, aNewSize );
147 FillPasswordList();
149 m_aRemoveBtn.SetClickHdl( LINK( this, WebConnectionInfoDialog, RemovePasswordHdl ) );
150 m_aRemoveAllBtn.SetClickHdl( LINK( this, WebConnectionInfoDialog, RemoveAllPasswordsHdl ) );
151 m_aChangeBtn.SetClickHdl( LINK( this, WebConnectionInfoDialog, ChangePasswordHdl ) );
152 m_aPasswordsLB.SetSelectHdl( LINK( this, WebConnectionInfoDialog, EntrySelectedHdl ) );
154 m_aRemoveBtn.Enable( sal_False );
155 m_aChangeBtn.Enable( sal_False );
157 HeaderBarClickedHdl( NULL );
160 // -----------------------------------------------------------------------
161 WebConnectionInfoDialog::~WebConnectionInfoDialog()
165 // -----------------------------------------------------------------------
166 IMPL_LINK( WebConnectionInfoDialog, HeaderBarClickedHdl, SvxSimpleTable*, pTable )
168 m_aPasswordsLB.Resort( NULL == pTable );
169 return 0;
172 // -----------------------------------------------------------------------
173 void WebConnectionInfoDialog::FillPasswordList()
177 uno::Reference< task::XPasswordContainer2 > xMasterPasswd(
178 task::PasswordContainer::create(comphelper::getProcessComponentContext()));
180 if ( xMasterPasswd->isPersistentStoringAllowed() )
182 uno::Reference< task::XInteractionHandler > xInteractionHandler(
183 task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), 0),
184 uno::UNO_QUERY);
186 uno::Sequence< task::UrlRecord > aURLEntries = xMasterPasswd->getAllPersistent( xInteractionHandler );
187 sal_Int32 nCount = 0;
188 for ( sal_Int32 nURLInd = 0; nURLInd < aURLEntries.getLength(); nURLInd++ )
190 for ( sal_Int32 nUserInd = 0; nUserInd < aURLEntries[nURLInd].UserList.getLength(); nUserInd++ )
192 ::rtl::OUString aUIEntry( aURLEntries[nURLInd].Url );
193 aUIEntry += ::rtl::OUString::valueOf( (sal_Unicode)'\t' );
194 aUIEntry += aURLEntries[nURLInd].UserList[nUserInd].UserName;
195 SvTreeListEntry* pEntry = m_aPasswordsLB.InsertEntry( aUIEntry );
196 pEntry->SetUserData( (void*)(sal_IntPtr)(nCount++) );
200 // remember pos of first url container entry.
201 m_nPos = nCount;
203 uno::Sequence< rtl::OUString > aUrls
204 = xMasterPasswd->getUrls( sal_True /* OnlyPersistent */ );
206 for ( sal_Int32 nURLIdx = 0; nURLIdx < aUrls.getLength(); nURLIdx++ )
208 ::rtl::OUString aUIEntry( aUrls[ nURLIdx ] );
209 aUIEntry += ::rtl::OUString::valueOf( (sal_Unicode)'\t' );
210 aUIEntry += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "*" ) );
211 SvTreeListEntry* pEntry = m_aPasswordsLB.InsertEntry( aUIEntry );
212 pEntry->SetUserData( (void*)(sal_IntPtr)(nCount++) );
216 catch( uno::Exception& )
220 // -----------------------------------------------------------------------
221 IMPL_LINK_NOARG(WebConnectionInfoDialog, RemovePasswordHdl)
225 SvTreeListEntry* pEntry = m_aPasswordsLB.GetCurEntry();
226 if ( pEntry )
228 ::rtl::OUString aURL = m_aPasswordsLB.GetEntryText( pEntry, 0 );
229 ::rtl::OUString aUserName = m_aPasswordsLB.GetEntryText( pEntry, 1 );
231 uno::Reference< task::XPasswordContainer2 > xPasswdContainer(
232 task::PasswordContainer::create(comphelper::getProcessComponentContext()));
234 sal_Int32 nPos = (sal_Int32)(sal_IntPtr)pEntry->GetUserData();
235 if ( nPos < m_nPos )
237 xPasswdContainer->removePersistent( aURL, aUserName );
239 else
241 xPasswdContainer->removeUrl( aURL );
243 m_aPasswordsLB.RemoveEntry( pEntry );
246 catch( uno::Exception& )
249 return 0;
252 // -----------------------------------------------------------------------
253 IMPL_LINK_NOARG(WebConnectionInfoDialog, RemoveAllPasswordsHdl)
257 uno::Reference< task::XPasswordContainer2 > xPasswdContainer(
258 task::PasswordContainer::create(comphelper::getProcessComponentContext()));
260 // should the master password be requested before?
261 xPasswdContainer->removeAllPersistent();
263 uno::Sequence< rtl::OUString > aUrls
264 = xPasswdContainer->getUrls( sal_True /* OnlyPersistent */ );
265 for ( sal_Int32 nURLIdx = 0; nURLIdx < aUrls.getLength(); nURLIdx++ )
266 xPasswdContainer->removeUrl( aUrls[ nURLIdx ] );
268 m_aPasswordsLB.Clear();
270 catch( uno::Exception& )
273 return 0;
276 // -----------------------------------------------------------------------
277 IMPL_LINK_NOARG(WebConnectionInfoDialog, ChangePasswordHdl)
281 SvTreeListEntry* pEntry = m_aPasswordsLB.GetCurEntry();
282 if ( pEntry )
284 ::rtl::OUString aURL = m_aPasswordsLB.GetEntryText( pEntry, 0 );
285 ::rtl::OUString aUserName = m_aPasswordsLB.GetEntryText( pEntry, 1 );
287 ::comphelper::SimplePasswordRequest* pPasswordRequest
288 = new ::comphelper::SimplePasswordRequest( task::PasswordRequestMode_PASSWORD_CREATE );
289 uno::Reference< task::XInteractionRequest > rRequest( pPasswordRequest );
291 uno::Reference< task::XInteractionHandler > xInteractionHandler(
292 task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), 0),
293 uno::UNO_QUERY );
294 xInteractionHandler->handle( rRequest );
296 if ( pPasswordRequest->isPassword() )
298 String aNewPass = pPasswordRequest->getPassword();
299 uno::Sequence< ::rtl::OUString > aPasswd( 1 );
300 aPasswd[0] = aNewPass;
302 uno::Reference< task::XPasswordContainer2 > xPasswdContainer(
303 task::PasswordContainer::create(comphelper::getProcessComponentContext()));
304 xPasswdContainer->addPersistent(
305 aURL, aUserName, aPasswd, xInteractionHandler );
309 catch( uno::Exception& )
312 return 0;
315 // -----------------------------------------------------------------------
316 IMPL_LINK_NOARG(WebConnectionInfoDialog, EntrySelectedHdl)
318 SvTreeListEntry* pEntry = m_aPasswordsLB.GetCurEntry();
319 if ( !pEntry )
321 m_aRemoveBtn.Enable( sal_False );
322 m_aChangeBtn.Enable( sal_False );
324 else
326 m_aRemoveBtn.Enable( sal_True );
328 // url container entries (-> use system credentials) have
329 // no password
330 sal_Int32 nPos = (sal_Int32)(sal_IntPtr)pEntry->GetUserData();
331 m_aChangeBtn.Enable( nPos < m_nPos );
334 return 0;
337 //........................................................................
338 } // namespace svx
339 //........................................................................
341 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */