Bump version to 4.3-4
[LibreOffice.git] / cui / source / options / webconninfo.cxx
blob427145a9e65986ae5f06599c5cf09bd1274dbe63
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 <dialmgr.hxx>
22 #include <cuires.hrc>
23 #include <sal/macros.h>
24 #include <com/sun/star/task/InteractionHandler.hpp>
25 #include <com/sun/star/task/PasswordContainer.hpp>
26 #include <com/sun/star/task/UrlRecord.hpp>
27 #include <com/sun/star/task/XPasswordContainer2.hpp>
28 #include <comphelper/processfactory.hxx>
29 #include <comphelper/docpasswordrequest.hxx>
30 #include "svtools/treelistentry.hxx"
31 #include <vcl/layout.hxx>
33 using namespace ::com::sun::star;
36 namespace svx
40 // class PasswordTable ---------------------------------------------------
42 PasswordTable::PasswordTable(SvSimpleTableContainer& rParent, WinBits nBits)
43 : SvSimpleTable(rParent, nBits | WB_NOINITIALSELECTION)
47 void PasswordTable::InsertHeaderItem(sal_uInt16 nColumn, const OUString& rText, HeaderBarItemBits nBits)
49 GetTheHeaderBar().InsertItem( nColumn, rText, 0, nBits );
52 void PasswordTable::Resort( bool bForced )
54 sal_uInt16 nColumn = GetSelectedCol();
55 if ( 0 == nColumn || bForced ) // only the first column is sorted
57 HeaderBarItemBits nBits = GetTheHeaderBar().GetItemBits(1);
58 bool bUp = ( ( nBits & HIB_UPARROW ) == HIB_UPARROW );
59 SvSortMode eMode = SortAscending;
61 if ( bUp )
63 nBits &= ~HIB_UPARROW;
64 nBits |= HIB_DOWNARROW;
65 eMode = SortDescending;
67 else
69 nBits &= ~HIB_DOWNARROW;
70 nBits |= HIB_UPARROW;
72 GetTheHeaderBar().SetItemBits( 1, nBits );
73 SvTreeList* pListModel = GetModel();
74 pListModel->SetSortMode( eMode );
75 pListModel->Resort();
79 void PasswordTable::Resize()
81 SvSimpleTable::Resize();
82 if (isInitialLayout(this))
83 setColWidths();
86 void PasswordTable::setColWidths()
88 HeaderBar &rBar = GetTheHeaderBar();
89 if (rBar.GetItemCount() < 2)
90 return;
91 long nUserNameWidth = 12 +
92 std::max(rBar.GetTextWidth(rBar.GetItemText(2)),
93 GetTextWidth(OUString("XXXXXXXXXXXX")));
94 long nWebSiteWidth = std::max(
95 12 + rBar.GetTextWidth(rBar.GetItemText(1)),
96 GetSizePixel().Width() - nUserNameWidth);
97 long aStaticTabs[]= { 2, 0, 0 };
98 aStaticTabs[2] = nWebSiteWidth;
99 SvSimpleTable::SetTabs(aStaticTabs, MAP_PIXEL);
102 // class WebConnectionInfoDialog -----------------------------------------
105 WebConnectionInfoDialog::WebConnectionInfoDialog(Window* pParent)
106 : ModalDialog(pParent, "StoredWebConnectionDialog", "cui/ui/storedwebconnectiondialog.ui")
107 , m_nPos( -1 )
109 get(m_pRemoveBtn, "remove");
110 get(m_pRemoveAllBtn, "removeall");
111 get(m_pChangeBtn, "change");
113 SvSimpleTableContainer *pPasswordsLBContainer = get<SvSimpleTableContainer>("logins");
114 m_pPasswordsLB = new PasswordTable(*pPasswordsLBContainer, 0);
116 long aStaticTabs[]= { 2, 0, 0 };
117 m_pPasswordsLB->SetTabs( aStaticTabs );
118 m_pPasswordsLB->InsertHeaderItem( 1, get<FixedText>("website")->GetText(),
119 HIB_LEFT | HIB_VCENTER | HIB_FIXEDPOS | HIB_CLICKABLE | HIB_UPARROW );
120 m_pPasswordsLB->InsertHeaderItem( 2, get<FixedText>("username")->GetText(),
121 HIB_LEFT | HIB_VCENTER | HIB_FIXEDPOS );
122 pPasswordsLBContainer->set_height_request(m_pPasswordsLB->GetTextHeight()*8);
124 m_pPasswordsLB->SetHeaderBarClickHdl( LINK( this, WebConnectionInfoDialog, HeaderBarClickedHdl ) );
125 m_pRemoveBtn->SetClickHdl( LINK( this, WebConnectionInfoDialog, RemovePasswordHdl ) );
126 m_pRemoveAllBtn->SetClickHdl( LINK( this, WebConnectionInfoDialog, RemoveAllPasswordsHdl ) );
127 m_pChangeBtn->SetClickHdl( LINK( this, WebConnectionInfoDialog, ChangePasswordHdl ) );
130 FillPasswordList();
132 m_pRemoveBtn->SetClickHdl( LINK( this, WebConnectionInfoDialog, RemovePasswordHdl ) );
133 m_pRemoveAllBtn->SetClickHdl( LINK( this, WebConnectionInfoDialog, RemoveAllPasswordsHdl ) );
134 m_pChangeBtn->SetClickHdl( LINK( this, WebConnectionInfoDialog, ChangePasswordHdl ) );
135 m_pPasswordsLB->SetSelectHdl( LINK( this, WebConnectionInfoDialog, EntrySelectedHdl ) );
137 m_pRemoveBtn->Enable( false );
138 m_pChangeBtn->Enable( false );
140 HeaderBarClickedHdl( NULL );
143 WebConnectionInfoDialog::~WebConnectionInfoDialog()
145 delete m_pPasswordsLB;
149 IMPL_LINK( WebConnectionInfoDialog, HeaderBarClickedHdl, SvSimpleTable*, pTable )
151 m_pPasswordsLB->Resort( NULL == pTable );
152 return 0;
156 void WebConnectionInfoDialog::FillPasswordList()
160 uno::Reference< task::XPasswordContainer2 > xMasterPasswd(
161 task::PasswordContainer::create(comphelper::getProcessComponentContext()));
163 if ( xMasterPasswd->isPersistentStoringAllowed() )
165 uno::Reference< task::XInteractionHandler > xInteractionHandler(
166 task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), 0),
167 uno::UNO_QUERY);
169 uno::Sequence< task::UrlRecord > aURLEntries = xMasterPasswd->getAllPersistent( xInteractionHandler );
170 sal_Int32 nCount = 0;
171 for ( sal_Int32 nURLInd = 0; nURLInd < aURLEntries.getLength(); nURLInd++ )
173 for ( sal_Int32 nUserInd = 0; nUserInd < aURLEntries[nURLInd].UserList.getLength(); nUserInd++ )
175 OUString aUIEntry( aURLEntries[nURLInd].Url );
176 aUIEntry += OUString( (sal_Unicode)'\t' );
177 aUIEntry += aURLEntries[nURLInd].UserList[nUserInd].UserName;
178 SvTreeListEntry* pEntry = m_pPasswordsLB->InsertEntry( aUIEntry );
179 pEntry->SetUserData( (void*)(sal_IntPtr)(nCount++) );
183 // remember pos of first url container entry.
184 m_nPos = nCount;
186 uno::Sequence< OUString > aUrls
187 = xMasterPasswd->getUrls( sal_True /* OnlyPersistent */ );
189 for ( sal_Int32 nURLIdx = 0; nURLIdx < aUrls.getLength(); nURLIdx++ )
191 OUString aUIEntry( aUrls[ nURLIdx ] );
192 aUIEntry += OUString( (sal_Unicode)'\t' );
193 aUIEntry += OUString( "*" );
194 SvTreeListEntry* pEntry = m_pPasswordsLB->InsertEntry( aUIEntry );
195 pEntry->SetUserData( (void*)(sal_IntPtr)(nCount++) );
199 catch( uno::Exception& )
204 IMPL_LINK_NOARG(WebConnectionInfoDialog, RemovePasswordHdl)
208 SvTreeListEntry* pEntry = m_pPasswordsLB->GetCurEntry();
209 if ( pEntry )
211 OUString aURL = m_pPasswordsLB->GetEntryText( pEntry, 0 );
212 OUString aUserName = m_pPasswordsLB->GetEntryText( pEntry, 1 );
214 uno::Reference< task::XPasswordContainer2 > xPasswdContainer(
215 task::PasswordContainer::create(comphelper::getProcessComponentContext()));
217 sal_Int32 nPos = (sal_Int32)(sal_IntPtr)pEntry->GetUserData();
218 if ( nPos < m_nPos )
220 xPasswdContainer->removePersistent( aURL, aUserName );
222 else
224 xPasswdContainer->removeUrl( aURL );
226 m_pPasswordsLB->RemoveEntry( pEntry );
229 catch( uno::Exception& )
232 return 0;
236 IMPL_LINK_NOARG(WebConnectionInfoDialog, RemoveAllPasswordsHdl)
240 uno::Reference< task::XPasswordContainer2 > xPasswdContainer(
241 task::PasswordContainer::create(comphelper::getProcessComponentContext()));
243 // should the master password be requested before?
244 xPasswdContainer->removeAllPersistent();
246 uno::Sequence< OUString > aUrls
247 = xPasswdContainer->getUrls( sal_True /* OnlyPersistent */ );
248 for ( sal_Int32 nURLIdx = 0; nURLIdx < aUrls.getLength(); nURLIdx++ )
249 xPasswdContainer->removeUrl( aUrls[ nURLIdx ] );
251 m_pPasswordsLB->Clear();
253 catch( uno::Exception& )
256 return 0;
260 IMPL_LINK_NOARG(WebConnectionInfoDialog, ChangePasswordHdl)
264 SvTreeListEntry* pEntry = m_pPasswordsLB->GetCurEntry();
265 if ( pEntry )
267 OUString aURL = m_pPasswordsLB->GetEntryText( pEntry, 0 );
268 OUString aUserName = m_pPasswordsLB->GetEntryText( pEntry, 1 );
270 ::comphelper::SimplePasswordRequest* pPasswordRequest
271 = new ::comphelper::SimplePasswordRequest( task::PasswordRequestMode_PASSWORD_CREATE );
272 uno::Reference< task::XInteractionRequest > rRequest( pPasswordRequest );
274 uno::Reference< task::XInteractionHandler > xInteractionHandler(
275 task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), 0),
276 uno::UNO_QUERY );
277 xInteractionHandler->handle( rRequest );
279 if ( pPasswordRequest->isPassword() )
281 OUString aNewPass = pPasswordRequest->getPassword();
282 uno::Sequence< OUString > aPasswd( 1 );
283 aPasswd[0] = aNewPass;
285 uno::Reference< task::XPasswordContainer2 > xPasswdContainer(
286 task::PasswordContainer::create(comphelper::getProcessComponentContext()));
287 xPasswdContainer->addPersistent(
288 aURL, aUserName, aPasswd, xInteractionHandler );
292 catch( uno::Exception& )
295 return 0;
299 IMPL_LINK_NOARG(WebConnectionInfoDialog, EntrySelectedHdl)
301 SvTreeListEntry* pEntry = m_pPasswordsLB->GetCurEntry();
302 if ( !pEntry )
304 m_pRemoveBtn->Enable( false );
305 m_pChangeBtn->Enable( false );
307 else
309 m_pRemoveBtn->Enable( true );
311 // url container entries (-> use system credentials) have
312 // no password
313 sal_Int32 nPos = (sal_Int32)(sal_IntPtr)pEntry->GetUserData();
314 m_pChangeBtn->Enable( nPos < m_nPos );
317 return 0;
321 } // namespace svx
324 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */