Line data Source code
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 <dialmgr.hxx>
22 : #include <cuires.hrc>
23 : #include <sal/macros.h>
24 : #include <com/sun/star/task/InteractionHandler.hpp>
25 : #include <com/sun/star/task/PasswordContainer.hpp>
26 : #include <com/sun/star/task/UrlRecord.hpp>
27 : #include <com/sun/star/task/XPasswordContainer2.hpp>
28 : #include <comphelper/processfactory.hxx>
29 : #include <comphelper/docpasswordrequest.hxx>
30 : #include "svtools/treelistentry.hxx"
31 : #include <vcl/layout.hxx>
32 :
33 : using namespace ::com::sun::star;
34 :
35 :
36 : namespace svx
37 : {
38 :
39 :
40 : // class PasswordTable ---------------------------------------------------
41 :
42 0 : PasswordTable::PasswordTable(SvSimpleTableContainer& rParent, WinBits nBits)
43 0 : : SvSimpleTable(rParent, nBits | WB_NOINITIALSELECTION)
44 : {
45 0 : }
46 :
47 0 : void PasswordTable::InsertHeaderItem(sal_uInt16 nColumn, const OUString& rText, HeaderBarItemBits nBits)
48 : {
49 0 : GetTheHeaderBar().InsertItem( nColumn, rText, 0, nBits );
50 0 : }
51 :
52 0 : void PasswordTable::Resort( bool bForced )
53 : {
54 0 : sal_uInt16 nColumn = GetSelectedCol();
55 0 : if ( 0 == nColumn || bForced ) // only the first column is sorted
56 : {
57 0 : HeaderBarItemBits nBits = GetTheHeaderBar().GetItemBits(1);
58 0 : bool bUp = ( ( nBits & HIB_UPARROW ) == HIB_UPARROW );
59 0 : SvSortMode eMode = SortAscending;
60 :
61 0 : if ( bUp )
62 : {
63 0 : nBits &= ~HIB_UPARROW;
64 0 : nBits |= HIB_DOWNARROW;
65 0 : eMode = SortDescending;
66 : }
67 : else
68 : {
69 0 : nBits &= ~HIB_DOWNARROW;
70 0 : nBits |= HIB_UPARROW;
71 : }
72 0 : GetTheHeaderBar().SetItemBits( 1, nBits );
73 0 : SvTreeList* pListModel = GetModel();
74 0 : pListModel->SetSortMode( eMode );
75 0 : pListModel->Resort();
76 : }
77 0 : }
78 :
79 0 : void PasswordTable::Resize()
80 : {
81 0 : SvSimpleTable::Resize();
82 0 : if (isInitialLayout(this))
83 0 : setColWidths();
84 0 : }
85 :
86 0 : void PasswordTable::setColWidths()
87 : {
88 0 : HeaderBar &rBar = GetTheHeaderBar();
89 0 : if (rBar.GetItemCount() < 2)
90 0 : return;
91 0 : long nUserNameWidth = 12 +
92 0 : std::max(rBar.GetTextWidth(rBar.GetItemText(2)),
93 0 : GetTextWidth(OUString("XXXXXXXXXXXX")));
94 : long nWebSiteWidth = std::max(
95 0 : 12 + rBar.GetTextWidth(rBar.GetItemText(1)),
96 0 : GetSizePixel().Width() - nUserNameWidth);
97 0 : long aStaticTabs[]= { 2, 0, 0 };
98 0 : aStaticTabs[2] = nWebSiteWidth;
99 0 : SvSimpleTable::SetTabs(aStaticTabs, MAP_PIXEL);
100 : }
101 :
102 : // class WebConnectionInfoDialog -----------------------------------------
103 :
104 :
105 0 : WebConnectionInfoDialog::WebConnectionInfoDialog(vcl::Window* pParent)
106 : : ModalDialog(pParent, "StoredWebConnectionDialog", "cui/ui/storedwebconnectiondialog.ui")
107 0 : , m_nPos( -1 )
108 : {
109 0 : get(m_pRemoveBtn, "remove");
110 0 : get(m_pRemoveAllBtn, "removeall");
111 0 : get(m_pChangeBtn, "change");
112 :
113 0 : SvSimpleTableContainer *pPasswordsLBContainer = get<SvSimpleTableContainer>("logins");
114 0 : m_pPasswordsLB = new PasswordTable(*pPasswordsLBContainer, 0);
115 :
116 0 : long aStaticTabs[]= { 2, 0, 0 };
117 0 : m_pPasswordsLB->SetTabs( aStaticTabs );
118 0 : m_pPasswordsLB->InsertHeaderItem( 1, get<FixedText>("website")->GetText(),
119 0 : HIB_LEFT | HIB_VCENTER | HIB_FIXEDPOS | HIB_CLICKABLE | HIB_UPARROW );
120 0 : m_pPasswordsLB->InsertHeaderItem( 2, get<FixedText>("username")->GetText(),
121 0 : HIB_LEFT | HIB_VCENTER | HIB_FIXEDPOS );
122 0 : pPasswordsLBContainer->set_height_request(m_pPasswordsLB->GetTextHeight()*8);
123 :
124 0 : m_pPasswordsLB->SetHeaderBarClickHdl( LINK( this, WebConnectionInfoDialog, HeaderBarClickedHdl ) );
125 0 : m_pRemoveBtn->SetClickHdl( LINK( this, WebConnectionInfoDialog, RemovePasswordHdl ) );
126 0 : m_pRemoveAllBtn->SetClickHdl( LINK( this, WebConnectionInfoDialog, RemoveAllPasswordsHdl ) );
127 0 : m_pChangeBtn->SetClickHdl( LINK( this, WebConnectionInfoDialog, ChangePasswordHdl ) );
128 :
129 :
130 0 : FillPasswordList();
131 :
132 0 : m_pRemoveBtn->SetClickHdl( LINK( this, WebConnectionInfoDialog, RemovePasswordHdl ) );
133 0 : m_pRemoveAllBtn->SetClickHdl( LINK( this, WebConnectionInfoDialog, RemoveAllPasswordsHdl ) );
134 0 : m_pChangeBtn->SetClickHdl( LINK( this, WebConnectionInfoDialog, ChangePasswordHdl ) );
135 0 : m_pPasswordsLB->SetSelectHdl( LINK( this, WebConnectionInfoDialog, EntrySelectedHdl ) );
136 :
137 0 : m_pRemoveBtn->Enable( false );
138 0 : m_pChangeBtn->Enable( false );
139 :
140 0 : HeaderBarClickedHdl( NULL );
141 0 : }
142 :
143 0 : WebConnectionInfoDialog::~WebConnectionInfoDialog()
144 : {
145 0 : delete m_pPasswordsLB;
146 0 : }
147 :
148 :
149 0 : IMPL_LINK( WebConnectionInfoDialog, HeaderBarClickedHdl, SvSimpleTable*, pTable )
150 : {
151 0 : m_pPasswordsLB->Resort( NULL == pTable );
152 0 : return 0;
153 : }
154 :
155 :
156 0 : void WebConnectionInfoDialog::FillPasswordList()
157 : {
158 : try
159 : {
160 : uno::Reference< task::XPasswordContainer2 > xMasterPasswd(
161 0 : task::PasswordContainer::create(comphelper::getProcessComponentContext()));
162 :
163 0 : if ( xMasterPasswd->isPersistentStoringAllowed() )
164 : {
165 : uno::Reference< task::XInteractionHandler > xInteractionHandler(
166 : task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), 0),
167 0 : uno::UNO_QUERY);
168 :
169 0 : uno::Sequence< task::UrlRecord > aURLEntries = xMasterPasswd->getAllPersistent( xInteractionHandler );
170 0 : sal_Int32 nCount = 0;
171 0 : for ( sal_Int32 nURLInd = 0; nURLInd < aURLEntries.getLength(); nURLInd++ )
172 : {
173 0 : for ( sal_Int32 nUserInd = 0; nUserInd < aURLEntries[nURLInd].UserList.getLength(); nUserInd++ )
174 : {
175 0 : OUString aUIEntry( aURLEntries[nURLInd].Url );
176 0 : aUIEntry += OUString( (sal_Unicode)'\t' );
177 0 : aUIEntry += aURLEntries[nURLInd].UserList[nUserInd].UserName;
178 0 : SvTreeListEntry* pEntry = m_pPasswordsLB->InsertEntry( aUIEntry );
179 0 : pEntry->SetUserData( reinterpret_cast<void*>(nCount++) );
180 0 : }
181 : }
182 :
183 : // remember pos of first url container entry.
184 0 : m_nPos = nCount;
185 :
186 : uno::Sequence< OUString > aUrls
187 0 : = xMasterPasswd->getUrls( sal_True /* OnlyPersistent */ );
188 :
189 0 : for ( sal_Int32 nURLIdx = 0; nURLIdx < aUrls.getLength(); nURLIdx++ )
190 : {
191 0 : OUString aUIEntry( aUrls[ nURLIdx ] );
192 0 : aUIEntry += OUString( (sal_Unicode)'\t' );
193 0 : aUIEntry += OUString( "*" );
194 0 : SvTreeListEntry* pEntry = m_pPasswordsLB->InsertEntry( aUIEntry );
195 0 : pEntry->SetUserData( reinterpret_cast<void*>(nCount++) );
196 0 : }
197 0 : }
198 : }
199 0 : catch( uno::Exception& )
200 : {}
201 0 : }
202 :
203 :
204 0 : IMPL_LINK_NOARG(WebConnectionInfoDialog, RemovePasswordHdl)
205 : {
206 : try
207 : {
208 0 : SvTreeListEntry* pEntry = m_pPasswordsLB->GetCurEntry();
209 0 : if ( pEntry )
210 : {
211 0 : OUString aURL = m_pPasswordsLB->GetEntryText( pEntry, 0 );
212 0 : OUString aUserName = m_pPasswordsLB->GetEntryText( pEntry, 1 );
213 :
214 : uno::Reference< task::XPasswordContainer2 > xPasswdContainer(
215 0 : task::PasswordContainer::create(comphelper::getProcessComponentContext()));
216 :
217 0 : sal_Int32 nPos = (sal_Int32)reinterpret_cast<sal_IntPtr>(pEntry->GetUserData());
218 0 : if ( nPos < m_nPos )
219 : {
220 0 : xPasswdContainer->removePersistent( aURL, aUserName );
221 : }
222 : else
223 : {
224 0 : xPasswdContainer->removeUrl( aURL );
225 : }
226 0 : m_pPasswordsLB->RemoveEntry( pEntry );
227 : }
228 : }
229 0 : catch( uno::Exception& )
230 : {}
231 :
232 0 : return 0;
233 : }
234 :
235 :
236 0 : IMPL_LINK_NOARG(WebConnectionInfoDialog, RemoveAllPasswordsHdl)
237 : {
238 : try
239 : {
240 : uno::Reference< task::XPasswordContainer2 > xPasswdContainer(
241 0 : task::PasswordContainer::create(comphelper::getProcessComponentContext()));
242 :
243 : // should the master password be requested before?
244 0 : xPasswdContainer->removeAllPersistent();
245 :
246 : uno::Sequence< OUString > aUrls
247 0 : = xPasswdContainer->getUrls( sal_True /* OnlyPersistent */ );
248 0 : for ( sal_Int32 nURLIdx = 0; nURLIdx < aUrls.getLength(); nURLIdx++ )
249 0 : xPasswdContainer->removeUrl( aUrls[ nURLIdx ] );
250 :
251 0 : m_pPasswordsLB->Clear();
252 : }
253 0 : catch( uno::Exception& )
254 : {}
255 :
256 0 : return 0;
257 : }
258 :
259 :
260 0 : IMPL_LINK_NOARG(WebConnectionInfoDialog, ChangePasswordHdl)
261 : {
262 : try
263 : {
264 0 : SvTreeListEntry* pEntry = m_pPasswordsLB->GetCurEntry();
265 0 : if ( pEntry )
266 : {
267 0 : OUString aURL = m_pPasswordsLB->GetEntryText( pEntry, 0 );
268 0 : OUString aUserName = m_pPasswordsLB->GetEntryText( pEntry, 1 );
269 :
270 : ::comphelper::SimplePasswordRequest* pPasswordRequest
271 0 : = new ::comphelper::SimplePasswordRequest( task::PasswordRequestMode_PASSWORD_CREATE );
272 0 : uno::Reference< task::XInteractionRequest > rRequest( pPasswordRequest );
273 :
274 : uno::Reference< task::XInteractionHandler > xInteractionHandler(
275 : task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), 0),
276 0 : uno::UNO_QUERY );
277 0 : xInteractionHandler->handle( rRequest );
278 :
279 0 : if ( pPasswordRequest->isPassword() )
280 : {
281 0 : OUString aNewPass = pPasswordRequest->getPassword();
282 0 : uno::Sequence< OUString > aPasswd( 1 );
283 0 : aPasswd[0] = aNewPass;
284 :
285 : uno::Reference< task::XPasswordContainer2 > xPasswdContainer(
286 0 : task::PasswordContainer::create(comphelper::getProcessComponentContext()));
287 0 : xPasswdContainer->addPersistent(
288 0 : aURL, aUserName, aPasswd, xInteractionHandler );
289 0 : }
290 : }
291 : }
292 0 : catch( uno::Exception& )
293 : {}
294 :
295 0 : return 0;
296 : }
297 :
298 :
299 0 : IMPL_LINK_NOARG(WebConnectionInfoDialog, EntrySelectedHdl)
300 : {
301 0 : SvTreeListEntry* pEntry = m_pPasswordsLB->GetCurEntry();
302 0 : if ( !pEntry )
303 : {
304 0 : m_pRemoveBtn->Enable( false );
305 0 : m_pChangeBtn->Enable( false );
306 : }
307 : else
308 : {
309 0 : m_pRemoveBtn->Enable( true );
310 :
311 : // url container entries (-> use system credentials) have
312 : // no password
313 0 : sal_Int32 nPos = (sal_Int32)reinterpret_cast<sal_IntPtr>(pEntry->GetUserData());
314 0 : m_pChangeBtn->Enable( nPos < m_nPos );
315 : }
316 :
317 0 : return 0;
318 : }
319 :
320 :
321 0 : } // namespace svx
322 :
323 :
324 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|