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 <o3tl/safeint.hxx>
21 #include "webconninfo.hxx"
22 #include <com/sun/star/task/InteractionHandler.hpp>
23 #include <com/sun/star/task/PasswordContainer.hpp>
24 #include <com/sun/star/task/UrlRecord.hpp>
25 #include <com/sun/star/task/XPasswordContainer2.hpp>
26 #include <comphelper/processfactory.hxx>
27 #include <comphelper/docpasswordrequest.hxx>
29 using namespace ::com::sun::star
;
35 // class WebConnectionInfoDialog -----------------------------------------
37 WebConnectionInfoDialog::WebConnectionInfoDialog(weld::Window
* pParent
)
38 : GenericDialogController(pParent
, "cui/ui/storedwebconnectiondialog.ui", "StoredWebConnectionDialog")
40 , m_xRemoveBtn(m_xBuilder
->weld_button("remove"))
41 , m_xRemoveAllBtn(m_xBuilder
->weld_button("removeall"))
42 , m_xChangeBtn(m_xBuilder
->weld_button("change"))
43 , m_xPasswordsLB(m_xBuilder
->weld_tree_view("logins"))
45 std::vector
<int> aWidths
47 o3tl::narrowing
<int>(m_xPasswordsLB
->get_approximate_digit_width() * 50)
49 m_xPasswordsLB
->set_column_fixed_widths(aWidths
);
50 m_xPasswordsLB
->set_size_request(m_xPasswordsLB
->get_approximate_digit_width() * 70,
51 m_xPasswordsLB
->get_height_rows(8));
53 m_xPasswordsLB
->connect_column_clicked(LINK(this, WebConnectionInfoDialog
, HeaderBarClickedHdl
));
57 m_xRemoveBtn
->connect_clicked( LINK( this, WebConnectionInfoDialog
, RemovePasswordHdl
) );
58 m_xRemoveAllBtn
->connect_clicked( LINK( this, WebConnectionInfoDialog
, RemoveAllPasswordsHdl
) );
59 m_xChangeBtn
->connect_clicked( LINK( this, WebConnectionInfoDialog
, ChangePasswordHdl
) );
60 m_xPasswordsLB
->connect_changed( LINK( this, WebConnectionInfoDialog
, EntrySelectedHdl
) );
62 m_xRemoveBtn
->set_sensitive( false );
63 m_xChangeBtn
->set_sensitive( false );
65 m_xPasswordsLB
->make_sorted();
68 WebConnectionInfoDialog::~WebConnectionInfoDialog()
72 IMPL_LINK(WebConnectionInfoDialog
, HeaderBarClickedHdl
, int, nColumn
, void)
74 if (nColumn
== 0) // only the first column is sorted
76 m_xPasswordsLB
->set_sort_order(!m_xPasswordsLB
->get_sort_order());
80 void WebConnectionInfoDialog::FillPasswordList()
84 uno::Reference
< task::XPasswordContainer2
> xMasterPasswd(
85 task::PasswordContainer::create(comphelper::getProcessComponentContext()));
87 if ( xMasterPasswd
->isPersistentStoringAllowed() )
89 uno::Reference
< task::XInteractionHandler
> xInteractionHandler
=
90 task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), nullptr);
92 const uno::Sequence
< task::UrlRecord
> aURLEntries
= xMasterPasswd
->getAllPersistent( xInteractionHandler
);
94 for ( task::UrlRecord
const & urlEntry
: aURLEntries
)
96 for ( auto const & user
: urlEntry
.UserList
)
98 m_xPasswordsLB
->append(OUString::number(nCount
), urlEntry
.Url
);
99 m_xPasswordsLB
->set_text(nCount
, user
.UserName
, 1);
104 // remember pos of first url container entry.
107 const uno::Sequence
< OUString
> aUrls
108 = xMasterPasswd
->getUrls( true /* OnlyPersistent */ );
110 for ( OUString
const & url
: aUrls
)
112 m_xPasswordsLB
->append(OUString::number(nCount
), url
);
113 m_xPasswordsLB
->set_text(nCount
, "*");
118 catch( uno::Exception
& )
123 IMPL_LINK_NOARG(WebConnectionInfoDialog
, RemovePasswordHdl
, weld::Button
&, void)
127 int nEntry
= m_xPasswordsLB
->get_selected_index();
130 OUString aURL
= m_xPasswordsLB
->get_text(nEntry
, 0);
131 OUString aUserName
= m_xPasswordsLB
->get_text(nEntry
, 1);
133 uno::Reference
< task::XPasswordContainer2
> xPasswdContainer(
134 task::PasswordContainer::create(comphelper::getProcessComponentContext()));
136 int nPos
= m_xPasswordsLB
->get_id(nEntry
).toInt32();
139 xPasswdContainer
->removePersistent( aURL
, aUserName
);
143 xPasswdContainer
->removeUrl( aURL
);
146 m_xPasswordsLB
->remove(nEntry
);
149 catch( uno::Exception
& )
153 IMPL_LINK_NOARG(WebConnectionInfoDialog
, RemoveAllPasswordsHdl
, weld::Button
&, void)
157 uno::Reference
< task::XPasswordContainer2
> xPasswdContainer(
158 task::PasswordContainer::create(comphelper::getProcessComponentContext()));
160 // should the master password be requested before?
161 xPasswdContainer
->removeAllPersistent();
163 const uno::Sequence
< OUString
> aUrls
164 = xPasswdContainer
->getUrls( true /* OnlyPersistent */ );
165 for ( OUString
const & url
: aUrls
)
166 xPasswdContainer
->removeUrl( url
);
168 m_xPasswordsLB
->clear();
170 catch( uno::Exception
& )
174 IMPL_LINK_NOARG(WebConnectionInfoDialog
, ChangePasswordHdl
, weld::Button
&, void)
178 int nEntry
= m_xPasswordsLB
->get_selected_index();
181 OUString aURL
= m_xPasswordsLB
->get_text(nEntry
, 0);
182 OUString aUserName
= m_xPasswordsLB
->get_text(nEntry
, 1);
184 rtl::Reference
<::comphelper::SimplePasswordRequest
> pPasswordRequest
185 = new ::comphelper::SimplePasswordRequest
;
187 uno::Reference
< task::XInteractionHandler
> xInteractionHandler
=
188 task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), m_xDialog
->GetXWindow());
189 xInteractionHandler
->handle( pPasswordRequest
);
191 if ( pPasswordRequest
->isPassword() )
193 OUString aNewPass
= pPasswordRequest
->getPassword();
194 uno::Sequence
<OUString
> aPasswd
{ aNewPass
};
196 uno::Reference
< task::XPasswordContainer2
> xPasswdContainer(
197 task::PasswordContainer::create(comphelper::getProcessComponentContext()));
198 xPasswdContainer
->addPersistent(
199 aURL
, aUserName
, aPasswd
, xInteractionHandler
);
203 catch( uno::Exception
& )
208 IMPL_LINK_NOARG(WebConnectionInfoDialog
, EntrySelectedHdl
, weld::TreeView
&, void)
210 int nEntry
= m_xPasswordsLB
->get_selected_index();
213 m_xRemoveBtn
->set_sensitive(false);
214 m_xChangeBtn
->set_sensitive(false);
218 m_xRemoveBtn
->set_sensitive(true);
220 // url container entries (-> use system credentials) have
222 int nPos
= m_xPasswordsLB
->get_id(nEntry
).toInt32();
223 m_xChangeBtn
->set_sensitive(nPos
< m_nPos
);
229 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */