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 "webconninfo.hxx"
21 #include <sal/macros.h>
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>
29 
30 using namespace ::com::sun::star;
31 
32 
33 namespace svx
34 {
35 
36 // class WebConnectionInfoDialog -----------------------------------------
37 
39  : GenericDialogController(pParent, "cui/ui/storedwebconnectiondialog.ui", "StoredWebConnectionDialog")
40  , m_nPos( -1 )
41  , m_xRemoveBtn(m_xBuilder->weld_button("remove"))
42  , m_xRemoveAllBtn(m_xBuilder->weld_button("removeall"))
43  , m_xChangeBtn(m_xBuilder->weld_button("change"))
44  , m_xPasswordsLB(m_xBuilder->weld_tree_view("logins"))
45 {
46  std::vector<int> aWidths;
47  aWidths.push_back(m_xPasswordsLB->get_approximate_digit_width() * 50);
48  m_xPasswordsLB->set_column_fixed_widths(aWidths);
49  m_xPasswordsLB->set_size_request(m_xPasswordsLB->get_approximate_digit_width() * 70,
50  m_xPasswordsLB->get_height_rows(8));
51 
52  m_xPasswordsLB->connect_column_clicked(LINK(this, WebConnectionInfoDialog, HeaderBarClickedHdl));
53 
55 
56  m_xRemoveBtn->connect_clicked( LINK( this, WebConnectionInfoDialog, RemovePasswordHdl ) );
57  m_xRemoveAllBtn->connect_clicked( LINK( this, WebConnectionInfoDialog, RemoveAllPasswordsHdl ) );
58  m_xChangeBtn->connect_clicked( LINK( this, WebConnectionInfoDialog, ChangePasswordHdl ) );
59  m_xPasswordsLB->connect_changed( LINK( this, WebConnectionInfoDialog, EntrySelectedHdl ) );
60 
61  m_xRemoveBtn->set_sensitive( false );
62  m_xChangeBtn->set_sensitive( false );
63 
64  m_xPasswordsLB->make_sorted();
65 }
66 
68 {
69 }
70 
71 IMPL_LINK(WebConnectionInfoDialog, HeaderBarClickedHdl, int, nColumn, void)
72 {
73  if (nColumn == 0) // only the first column is sorted
74  {
75  m_xPasswordsLB->set_sort_order(!m_xPasswordsLB->get_sort_order());
76  }
77 }
78 
80 {
81  try
82  {
83  uno::Reference< task::XPasswordContainer2 > xMasterPasswd(
84  task::PasswordContainer::create(comphelper::getProcessComponentContext()));
85 
86  if ( xMasterPasswd->isPersistentStoringAllowed() )
87  {
88  uno::Reference< task::XInteractionHandler > xInteractionHandler =
89  task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), nullptr);
90 
91  uno::Sequence< task::UrlRecord > aURLEntries = xMasterPasswd->getAllPersistent( xInteractionHandler );
92  sal_Int32 nCount = 0;
93  for ( sal_Int32 nURLInd = 0; nURLInd < aURLEntries.getLength(); nURLInd++ )
94  {
95  for ( sal_Int32 nUserInd = 0; nUserInd < aURLEntries[nURLInd].UserList.getLength(); nUserInd++ )
96  {
97  m_xPasswordsLB->append(OUString::number(nCount), aURLEntries[nURLInd].Url);
98  m_xPasswordsLB->set_text(nCount, aURLEntries[nURLInd].UserList[nUserInd].UserName, 1);
99  ++nCount;
100  }
101  }
102 
103  // remember pos of first url container entry.
104  m_nPos = nCount;
105 
106  uno::Sequence< OUString > aUrls
107  = xMasterPasswd->getUrls( true /* OnlyPersistent */ );
108 
109  for ( sal_Int32 nURLIdx = 0; nURLIdx < aUrls.getLength(); nURLIdx++ )
110  {
111  m_xPasswordsLB->append(OUString::number(nCount), aUrls[nURLIdx]);
112  m_xPasswordsLB->set_text(nCount, "*");
113  ++nCount;
114  }
115  }
116  }
117  catch( uno::Exception& )
118  {}
119 }
120 
121 
123 {
124  try
125  {
126  int nEntry = m_xPasswordsLB->get_selected_index();
127  if (nEntry != -1)
128  {
129  OUString aURL = m_xPasswordsLB->get_text(nEntry, 0);
130  OUString aUserName = m_xPasswordsLB->get_text(nEntry, 1);
131 
132  uno::Reference< task::XPasswordContainer2 > xPasswdContainer(
133  task::PasswordContainer::create(comphelper::getProcessComponentContext()));
134 
135  int nPos = m_xPasswordsLB->get_id(nEntry).toInt32();
136  if ( nPos < m_nPos )
137  {
138  xPasswdContainer->removePersistent( aURL, aUserName );
139  }
140  else
141  {
142  xPasswdContainer->removeUrl( aURL );
143  }
144 
145  m_xPasswordsLB->remove(nEntry);
146  }
147  }
148  catch( uno::Exception& )
149  {}
150 }
151 
152 IMPL_LINK_NOARG(WebConnectionInfoDialog, RemoveAllPasswordsHdl, weld::Button&, void)
153 {
154  try
155  {
156  uno::Reference< task::XPasswordContainer2 > xPasswdContainer(
157  task::PasswordContainer::create(comphelper::getProcessComponentContext()));
158 
159  // should the master password be requested before?
160  xPasswdContainer->removeAllPersistent();
161 
162  uno::Sequence< OUString > aUrls
163  = xPasswdContainer->getUrls( true /* OnlyPersistent */ );
164  for ( sal_Int32 nURLIdx = 0; nURLIdx < aUrls.getLength(); nURLIdx++ )
165  xPasswdContainer->removeUrl( aUrls[ nURLIdx ] );
166 
167  m_xPasswordsLB->clear();
168  }
169  catch( uno::Exception& )
170  {}
171 }
172 
174 {
175  try
176  {
177  int nEntry = m_xPasswordsLB->get_selected_index();
178  if (nEntry != -1)
179  {
180  OUString aURL = m_xPasswordsLB->get_text(nEntry, 0);
181  OUString aUserName = m_xPasswordsLB->get_text(nEntry, 1);
182 
183  ::comphelper::SimplePasswordRequest* pPasswordRequest
184  = new ::comphelper::SimplePasswordRequest;
185  uno::Reference< task::XInteractionRequest > rRequest( pPasswordRequest );
186 
187  uno::Reference< task::XInteractionHandler > xInteractionHandler =
188  task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), m_xDialog->GetXWindow());
189  xInteractionHandler->handle( rRequest );
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::unique_ptr< weld::Button > m_xRemoveAllBtn
Definition: webconninfo.hxx:31
IMPL_LINK_NOARG(ClassificationDialog, OnAsyncExpandHdl, void *, void)
std::unique_ptr< weld::Button > m_xRemoveBtn
Definition: webconninfo.hxx:30
std::unique_ptr< weld::Button > m_xChangeBtn
Definition: webconninfo.hxx:32
std::unique_ptr< weld::TreeView > m_xPasswordsLB
Definition: webconninfo.hxx:33
virtual ~WebConnectionInfoDialog() override
Definition: webconninfo.cxx:67
IMPL_LINK(ClassificationDialog, SelectClassificationHdl, weld::ComboBox &, rBox, void)
SQLCHAR SQLSMALLINT SQLCHAR * UserName
WebConnectionInfoDialog(weld::Window *pParent)
Definition: webconninfo.cxx:38
Reference< XComponentContext > getProcessComponentContext()
sal_Int32 nPos