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