Use correct object
[LibreOffice.git] / cui / source / options / webconninfo.cxx
blobbb55e8dcfa49973ca58f1e7f9d97c10fbe4a32f8
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 <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;
32 namespace svx
35 // class WebConnectionInfoDialog -----------------------------------------
37 WebConnectionInfoDialog::WebConnectionInfoDialog(weld::Window* pParent)
38 : GenericDialogController(pParent, u"cui/ui/storedwebconnectiondialog.ui"_ustr, u"StoredWebConnectionDialog"_ustr)
39 , m_nPos( -1 )
40 , m_xRemoveBtn(m_xBuilder->weld_button(u"remove"_ustr))
41 , m_xRemoveAllBtn(m_xBuilder->weld_button(u"removeall"_ustr))
42 , m_xChangeBtn(m_xBuilder->weld_button(u"change"_ustr))
43 , m_xPasswordsLB(m_xBuilder->weld_tree_view(u"logins"_ustr))
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));
55 FillPasswordList();
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_selection_changed(
61 LINK(this, WebConnectionInfoDialog, EntrySelectedHdl));
63 m_xRemoveBtn->set_sensitive( false );
64 m_xChangeBtn->set_sensitive( false );
66 m_xPasswordsLB->make_sorted();
69 WebConnectionInfoDialog::~WebConnectionInfoDialog()
73 IMPL_LINK(WebConnectionInfoDialog, HeaderBarClickedHdl, int, nColumn, void)
75 if (nColumn == 0) // only the first column is sorted
77 m_xPasswordsLB->set_sort_order(!m_xPasswordsLB->get_sort_order());
81 void WebConnectionInfoDialog::FillPasswordList()
83 try
85 uno::Reference< task::XPasswordContainer2 > xMasterPasswd(
86 task::PasswordContainer::create(comphelper::getProcessComponentContext()));
88 if ( xMasterPasswd->isPersistentStoringAllowed() )
90 uno::Reference< task::XInteractionHandler > xInteractionHandler =
91 task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), nullptr);
93 const uno::Sequence< task::UrlRecord > aURLEntries = xMasterPasswd->getAllPersistent( xInteractionHandler );
94 sal_Int32 nCount = 0;
95 for ( task::UrlRecord const & urlEntry : aURLEntries )
97 for ( auto const & user : urlEntry.UserList )
99 m_xPasswordsLB->append(OUString::number(nCount), urlEntry.Url);
100 m_xPasswordsLB->set_text(nCount, user.UserName, 1);
101 ++nCount;
105 // remember pos of first url container entry.
106 m_nPos = nCount;
108 const uno::Sequence< OUString > aUrls
109 = xMasterPasswd->getUrls( true /* OnlyPersistent */ );
111 for ( OUString const & url : aUrls )
113 m_xPasswordsLB->append(OUString::number(nCount), url);
114 m_xPasswordsLB->set_text(nCount, u"*"_ustr);
115 ++nCount;
119 catch( uno::Exception& )
124 IMPL_LINK_NOARG(WebConnectionInfoDialog, RemovePasswordHdl, weld::Button&, void)
128 int nEntry = m_xPasswordsLB->get_selected_index();
129 if (nEntry != -1)
131 OUString aURL = m_xPasswordsLB->get_text(nEntry, 0);
132 OUString aUserName = m_xPasswordsLB->get_text(nEntry, 1);
134 uno::Reference< task::XPasswordContainer2 > xPasswdContainer(
135 task::PasswordContainer::create(comphelper::getProcessComponentContext()));
137 int nPos = m_xPasswordsLB->get_id(nEntry).toInt32();
138 if ( nPos < m_nPos )
140 xPasswdContainer->removePersistent( aURL, aUserName );
142 else
144 xPasswdContainer->removeUrl( aURL );
147 m_xPasswordsLB->remove(nEntry);
150 catch( uno::Exception& )
154 IMPL_LINK_NOARG(WebConnectionInfoDialog, RemoveAllPasswordsHdl, weld::Button&, void)
158 uno::Reference< task::XPasswordContainer2 > xPasswdContainer(
159 task::PasswordContainer::create(comphelper::getProcessComponentContext()));
161 // should the master password be requested before?
162 xPasswdContainer->removeAllPersistent();
164 const uno::Sequence< OUString > aUrls
165 = xPasswdContainer->getUrls( true /* OnlyPersistent */ );
166 for ( OUString const & url : aUrls )
167 xPasswdContainer->removeUrl( url );
169 m_xPasswordsLB->clear();
171 catch( uno::Exception& )
175 IMPL_LINK_NOARG(WebConnectionInfoDialog, ChangePasswordHdl, weld::Button&, void)
179 int nEntry = m_xPasswordsLB->get_selected_index();
180 if (nEntry != -1)
182 OUString aURL = m_xPasswordsLB->get_text(nEntry, 0);
183 OUString aUserName = m_xPasswordsLB->get_text(nEntry, 1);
185 rtl::Reference<::comphelper::SimplePasswordRequest> pPasswordRequest
186 = new ::comphelper::SimplePasswordRequest;
188 uno::Reference< task::XInteractionHandler > xInteractionHandler =
189 task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), m_xDialog->GetXWindow());
190 xInteractionHandler->handle( pPasswordRequest );
192 if ( pPasswordRequest->isPassword() )
194 uno::Sequence<OUString> aPasswd { pPasswordRequest->getPassword() };
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();
211 if (nEntry == -1)
213 m_xRemoveBtn->set_sensitive(false);
214 m_xChangeBtn->set_sensitive(false);
216 else
218 m_xRemoveBtn->set_sensitive(true);
220 // url container entries (-> use system credentials) have
221 // no password
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: */