1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
;
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
& HeaderBarItemBits::UPARROW
) == HeaderBarItemBits::UPARROW
);
59 SvSortMode eMode
= SortAscending
;
63 nBits
&= ~HeaderBarItemBits::UPARROW
;
64 nBits
|= HeaderBarItemBits::DOWNARROW
;
65 eMode
= SortDescending
;
69 nBits
&= ~HeaderBarItemBits::DOWNARROW
;
70 nBits
|= HeaderBarItemBits::UPARROW
;
72 GetTheHeaderBar().SetItemBits( 1, nBits
);
73 SvTreeList
* pListModel
= GetModel();
74 pListModel
->SetSortMode( eMode
);
79 void PasswordTable::Resize()
81 SvSimpleTable::Resize();
82 if (isInitialLayout(this))
86 void PasswordTable::setColWidths()
88 HeaderBar
&rBar
= GetTheHeaderBar();
89 if (rBar
.GetItemCount() < 2)
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(vcl::Window
* pParent
)
106 : ModalDialog(pParent
, "StoredWebConnectionDialog", "cui/ui/storedwebconnectiondialog.ui")
109 get(m_pRemoveBtn
, "remove");
110 get(m_pRemoveAllBtn
, "removeall");
111 get(m_pChangeBtn
, "change");
113 SvSimpleTableContainer
*pPasswordsLBContainer
= get
<SvSimpleTableContainer
>("logins");
114 m_pPasswordsLB
= VclPtr
<PasswordTable
>::Create(*pPasswordsLBContainer
, 0);
116 long aStaticTabs
[]= { 2, 0, 0 };
117 m_pPasswordsLB
->SetTabs( aStaticTabs
);
118 m_pPasswordsLB
->InsertHeaderItem( 1, get
<FixedText
>("website")->GetText(),
119 HeaderBarItemBits::LEFT
| HeaderBarItemBits::VCENTER
| HeaderBarItemBits::FIXEDPOS
| HeaderBarItemBits::CLICKABLE
| HeaderBarItemBits::UPARROW
);
120 m_pPasswordsLB
->InsertHeaderItem( 2, get
<FixedText
>("username")->GetText(),
121 HeaderBarItemBits::LEFT
| HeaderBarItemBits::VCENTER
| HeaderBarItemBits::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
) );
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()
148 void WebConnectionInfoDialog::dispose()
150 m_pPasswordsLB
.disposeAndClear();
151 m_pRemoveBtn
.clear();
152 m_pRemoveAllBtn
.clear();
153 m_pChangeBtn
.clear();
154 ModalDialog::dispose();
157 IMPL_LINK( WebConnectionInfoDialog
, HeaderBarClickedHdl
, SvSimpleTable
*, pTable
)
159 m_pPasswordsLB
->Resort( NULL
== pTable
);
164 void WebConnectionInfoDialog::FillPasswordList()
168 uno::Reference
< task::XPasswordContainer2
> xMasterPasswd(
169 task::PasswordContainer::create(comphelper::getProcessComponentContext()));
171 if ( xMasterPasswd
->isPersistentStoringAllowed() )
173 uno::Reference
< task::XInteractionHandler
> xInteractionHandler(
174 task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), 0),
177 uno::Sequence
< task::UrlRecord
> aURLEntries
= xMasterPasswd
->getAllPersistent( xInteractionHandler
);
178 sal_Int32 nCount
= 0;
179 for ( sal_Int32 nURLInd
= 0; nURLInd
< aURLEntries
.getLength(); nURLInd
++ )
181 for ( sal_Int32 nUserInd
= 0; nUserInd
< aURLEntries
[nURLInd
].UserList
.getLength(); nUserInd
++ )
183 OUString
aUIEntry( aURLEntries
[nURLInd
].Url
);
184 aUIEntry
+= OUString( (sal_Unicode
)'\t' );
185 aUIEntry
+= aURLEntries
[nURLInd
].UserList
[nUserInd
].UserName
;
186 SvTreeListEntry
* pEntry
= m_pPasswordsLB
->InsertEntry( aUIEntry
);
187 pEntry
->SetUserData( reinterpret_cast<void*>(nCount
++) );
191 // remember pos of first url container entry.
194 uno::Sequence
< OUString
> aUrls
195 = xMasterPasswd
->getUrls( sal_True
/* OnlyPersistent */ );
197 for ( sal_Int32 nURLIdx
= 0; nURLIdx
< aUrls
.getLength(); nURLIdx
++ )
199 OUString
aUIEntry( aUrls
[ nURLIdx
] );
200 aUIEntry
+= OUString( (sal_Unicode
)'\t' );
201 aUIEntry
+= OUString( "*" );
202 SvTreeListEntry
* pEntry
= m_pPasswordsLB
->InsertEntry( aUIEntry
);
203 pEntry
->SetUserData( reinterpret_cast<void*>(nCount
++) );
207 catch( uno::Exception
& )
212 IMPL_LINK_NOARG(WebConnectionInfoDialog
, RemovePasswordHdl
)
216 SvTreeListEntry
* pEntry
= m_pPasswordsLB
->GetCurEntry();
219 OUString aURL
= SvTabListBox::GetEntryText( pEntry
, 0 );
220 OUString aUserName
= SvTabListBox::GetEntryText( pEntry
, 1 );
222 uno::Reference
< task::XPasswordContainer2
> xPasswdContainer(
223 task::PasswordContainer::create(comphelper::getProcessComponentContext()));
225 sal_Int32 nPos
= (sal_Int32
)reinterpret_cast<sal_IntPtr
>(pEntry
->GetUserData());
228 xPasswdContainer
->removePersistent( aURL
, aUserName
);
232 xPasswdContainer
->removeUrl( aURL
);
234 m_pPasswordsLB
->RemoveEntry( pEntry
);
237 catch( uno::Exception
& )
244 IMPL_LINK_NOARG(WebConnectionInfoDialog
, RemoveAllPasswordsHdl
)
248 uno::Reference
< task::XPasswordContainer2
> xPasswdContainer(
249 task::PasswordContainer::create(comphelper::getProcessComponentContext()));
251 // should the master password be requested before?
252 xPasswdContainer
->removeAllPersistent();
254 uno::Sequence
< OUString
> aUrls
255 = xPasswdContainer
->getUrls( sal_True
/* OnlyPersistent */ );
256 for ( sal_Int32 nURLIdx
= 0; nURLIdx
< aUrls
.getLength(); nURLIdx
++ )
257 xPasswdContainer
->removeUrl( aUrls
[ nURLIdx
] );
259 m_pPasswordsLB
->Clear();
261 catch( uno::Exception
& )
268 IMPL_LINK_NOARG(WebConnectionInfoDialog
, ChangePasswordHdl
)
272 SvTreeListEntry
* pEntry
= m_pPasswordsLB
->GetCurEntry();
275 OUString aURL
= SvTabListBox::GetEntryText( pEntry
, 0 );
276 OUString aUserName
= SvTabListBox::GetEntryText( pEntry
, 1 );
278 ::comphelper::SimplePasswordRequest
* pPasswordRequest
279 = new ::comphelper::SimplePasswordRequest( task::PasswordRequestMode_PASSWORD_CREATE
);
280 uno::Reference
< task::XInteractionRequest
> rRequest( pPasswordRequest
);
282 uno::Reference
< task::XInteractionHandler
> xInteractionHandler(
283 task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), 0),
285 xInteractionHandler
->handle( rRequest
);
287 if ( pPasswordRequest
->isPassword() )
289 OUString aNewPass
= pPasswordRequest
->getPassword();
290 uno::Sequence
< OUString
> aPasswd( 1 );
291 aPasswd
[0] = aNewPass
;
293 uno::Reference
< task::XPasswordContainer2
> xPasswdContainer(
294 task::PasswordContainer::create(comphelper::getProcessComponentContext()));
295 xPasswdContainer
->addPersistent(
296 aURL
, aUserName
, aPasswd
, xInteractionHandler
);
300 catch( uno::Exception
& )
307 IMPL_LINK_NOARG(WebConnectionInfoDialog
, EntrySelectedHdl
)
309 SvTreeListEntry
* pEntry
= m_pPasswordsLB
->GetCurEntry();
312 m_pRemoveBtn
->Enable( false );
313 m_pChangeBtn
->Enable( false );
317 m_pRemoveBtn
->Enable( true );
319 // url container entries (-> use system credentials) have
321 sal_Int32 nPos
= (sal_Int32
)reinterpret_cast<sal_IntPtr
>(pEntry
->GetUserData());
322 m_pChangeBtn
->Enable( nPos
< m_nPos
);
332 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */