LibreOffice Module cui (master)  1
webconninfo.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
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>
28 
29 using namespace ::com::sun::star;
30 
31 
32 namespace svx
33 {
34 
35 // class WebConnectionInfoDialog -----------------------------------------
36 
38  : GenericDialogController(pParent, "cui/ui/storedwebconnectiondialog.ui", "StoredWebConnectionDialog")
39  , m_nPos( -1 )
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"))
44 {
45  std::vector<int> aWidths
46  {
47  o3tl::narrowing<int>(m_xPasswordsLB->get_approximate_digit_width() * 50)
48  };
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));
52 
53  m_xPasswordsLB->connect_column_clicked(LINK(this, WebConnectionInfoDialog, HeaderBarClickedHdl));
54 
56 
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 ) );
61 
62  m_xRemoveBtn->set_sensitive( false );
63  m_xChangeBtn->set_sensitive( false );
64 
65  m_xPasswordsLB->make_sorted();
66 }
67 
69 {
70 }
71 
72 IMPL_LINK(WebConnectionInfoDialog, HeaderBarClickedHdl, int, nColumn, void)
73 {
74  if (nColumn == 0) // only the first column is sorted
75  {
76  m_xPasswordsLB->set_sort_order(!m_xPasswordsLB->get_sort_order());
77  }
78 }
79 
81 {
82  try
83  {
84  uno::Reference< task::XPasswordContainer2 > xMasterPasswd(
85  task::PasswordContainer::create(comphelper::getProcessComponentContext()));
86 
87  if ( xMasterPasswd->isPersistentStoringAllowed() )
88  {
89  uno::Reference< task::XInteractionHandler > xInteractionHandler =
90  task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), nullptr);
91 
92  const uno::Sequence< task::UrlRecord > aURLEntries = xMasterPasswd->getAllPersistent( xInteractionHandler );
93  sal_Int32 nCount = 0;
94  for ( task::UrlRecord const & urlEntry : aURLEntries )
95  {
96  for ( auto const & user : urlEntry.UserList )
97  {
98  m_xPasswordsLB->append(OUString::number(nCount), urlEntry.Url);
99  m_xPasswordsLB->set_text(nCount, user.UserName, 1);
100  ++nCount;
101  }
102  }
103 
104  // remember pos of first url container entry.
105  m_nPos = nCount;
106 
107  const uno::Sequence< OUString > aUrls
108  = xMasterPasswd->getUrls( true /* OnlyPersistent */ );
109 
110  for ( OUString const & url : aUrls )
111  {
112  m_xPasswordsLB->append(OUString::number(nCount), url);
113  m_xPasswordsLB->set_text(nCount, "*");
114  ++nCount;
115  }
116  }
117  }
118  catch( uno::Exception& )
119  {}
120 }
121 
122 
124 {
125  try
126  {
127  int nEntry = m_xPasswordsLB->get_selected_index();
128  if (nEntry != -1)
129  {
130  OUString aURL = m_xPasswordsLB->get_text(nEntry, 0);
131  OUString aUserName = m_xPasswordsLB->get_text(nEntry, 1);
132 
133  uno::Reference< task::XPasswordContainer2 > xPasswdContainer(
134  task::PasswordContainer::create(comphelper::getProcessComponentContext()));
135 
136  int nPos = m_xPasswordsLB->get_id(nEntry).toInt32();
137  if ( nPos < m_nPos )
138  {
139  xPasswdContainer->removePersistent( aURL, aUserName );
140  }
141  else
142  {
143  xPasswdContainer->removeUrl( aURL );
144  }
145 
146  m_xPasswordsLB->remove(nEntry);
147  }
148  }
149  catch( uno::Exception& )
150  {}
151 }
152 
153 IMPL_LINK_NOARG(WebConnectionInfoDialog, RemoveAllPasswordsHdl, weld::Button&, void)
154 {
155  try
156  {
157  uno::Reference< task::XPasswordContainer2 > xPasswdContainer(
158  task::PasswordContainer::create(comphelper::getProcessComponentContext()));
159 
160  // should the master password be requested before?
161  xPasswdContainer->removeAllPersistent();
162 
163  const uno::Sequence< OUString > aUrls
164  = xPasswdContainer->getUrls( true /* OnlyPersistent */ );
165  for ( OUString const & url : aUrls )
166  xPasswdContainer->removeUrl( url );
167 
168  m_xPasswordsLB->clear();
169  }
170  catch( uno::Exception& )
171  {}
172 }
173 
175 {
176  try
177  {
178  int nEntry = m_xPasswordsLB->get_selected_index();
179  if (nEntry != -1)
180  {
181  OUString aURL = m_xPasswordsLB->get_text(nEntry, 0);
182  OUString aUserName = m_xPasswordsLB->get_text(nEntry, 1);
183 
185  = new ::comphelper::SimplePasswordRequest;
186 
187  uno::Reference< task::XInteractionHandler > xInteractionHandler =
188  task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), m_xDialog->GetXWindow());
189  xInteractionHandler->handle( pPasswordRequest );
190 
191  if ( pPasswordRequest->isPassword() )
192  {
193  OUString aNewPass = pPasswordRequest->getPassword();
194  uno::Sequence<OUString> aPasswd { aNewPass };
195 
196  uno::Reference< task::XPasswordContainer2 > xPasswdContainer(
197  task::PasswordContainer::create(comphelper::getProcessComponentContext()));
198  xPasswdContainer->addPersistent(
199  aURL, aUserName, aPasswd, xInteractionHandler );
200  }
201  }
202  }
203  catch( uno::Exception& )
204  {}
205 }
206 
207 
209 {
210  int nEntry = m_xPasswordsLB->get_selected_index();
211  if (nEntry == -1)
212  {
213  m_xRemoveBtn->set_sensitive(false);
214  m_xChangeBtn->set_sensitive(false);
215  }
216  else
217  {
218  m_xRemoveBtn->set_sensitive(true);
219 
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);
224  }
225 }
226 
227 }
228 
229 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::shared_ptr< weld::Dialog > m_xDialog
URL aURL
size_t m_nPos
std::unique_ptr< weld::Button > m_xRemoveAllBtn
Definition: webconninfo.hxx:30
std::unique_ptr< weld::Button > m_xRemoveBtn
Definition: webconninfo.hxx:29
std::unique_ptr< weld::Button > m_xChangeBtn
Definition: webconninfo.hxx:31
std::unique_ptr< weld::TreeView > m_xPasswordsLB
Definition: webconninfo.hxx:32
virtual ~WebConnectionInfoDialog() override
Definition: webconninfo.cxx:68
int nCount
IMPL_LINK(ClassificationDialog, SelectClassificationHdl, weld::ComboBox &, rBox, void)
WebConnectionInfoDialog(weld::Window *pParent)
Definition: webconninfo.cxx:37
Reference< XComponentContext > getProcessComponentContext()
IMPL_LINK_NOARG(AccessibilityCheckEntry, GotoButtonClicked, weld::Button &, void)
sal_uInt16 nPos