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 "webconninfo.hrc"
22 : #include <dialmgr.hxx>
23 : #include <cuires.hrc>
24 : #include <sal/macros.h>
25 : #include <com/sun/star/task/InteractionHandler.hpp>
26 : #include <com/sun/star/task/PasswordContainer.hpp>
27 : #include <com/sun/star/task/UrlRecord.hpp>
28 : #include <com/sun/star/task/XPasswordContainer2.hpp>
29 : #include <comphelper/processfactory.hxx>
30 : #include <comphelper/docpasswordrequest.hxx>
31 : #include "svtools/treelistentry.hxx"
32 :
33 : using namespace ::com::sun::star;
34 :
35 : //........................................................................
36 : namespace svx
37 : {
38 : //........................................................................
39 :
40 : // class PasswordTable ---------------------------------------------------
41 :
42 0 : PasswordTable::PasswordTable(SvxSimpleTableContainer& rParent, WinBits nBits)
43 0 : : SvxSimpleTable(rParent, nBits | WB_NOINITIALSELECTION)
44 : {
45 0 : }
46 :
47 0 : void PasswordTable::InsertHeaderItem( sal_uInt16 nColumn, const String& rText, HeaderBarItemBits nBits )
48 : {
49 0 : GetTheHeaderBar().InsertItem( nColumn, rText, 0, nBits );
50 0 : }
51 :
52 0 : void PasswordTable::ResetTabs()
53 : {
54 0 : SetTabs();
55 0 : }
56 :
57 0 : void PasswordTable::Resort( bool bForced )
58 : {
59 0 : sal_uInt16 nColumn = GetSelectedCol();
60 0 : if ( 0 == nColumn || bForced ) // only the first column is sorted
61 : {
62 0 : HeaderBarItemBits nBits = GetTheHeaderBar().GetItemBits(1);
63 0 : sal_Bool bUp = ( ( nBits & HIB_UPARROW ) == HIB_UPARROW );
64 0 : SvSortMode eMode = SortAscending;
65 :
66 0 : if ( bUp )
67 : {
68 0 : nBits &= ~HIB_UPARROW;
69 0 : nBits |= HIB_DOWNARROW;
70 0 : eMode = SortDescending;
71 : }
72 : else
73 : {
74 0 : nBits &= ~HIB_DOWNARROW;
75 0 : nBits |= HIB_UPARROW;
76 : }
77 0 : GetTheHeaderBar().SetItemBits( 1, nBits );
78 0 : SvTreeList* pListModel = GetModel();
79 0 : pListModel->SetSortMode( eMode );
80 0 : pListModel->Resort();
81 : }
82 0 : }
83 :
84 : // class WebConnectionInfoDialog -----------------------------------------
85 :
86 : // -----------------------------------------------------------------------
87 0 : WebConnectionInfoDialog::WebConnectionInfoDialog( Window* pParent ) :
88 0 : ModalDialog( pParent, CUI_RES( RID_SVXDLG_WEBCONNECTION_INFO ) )
89 0 : ,m_aNeverShownFI ( this, CUI_RES( FI_NEVERSHOWN ) )
90 0 : ,m_aPasswordsLBContainer(this, CUI_RES( LB_PASSWORDS))
91 : ,m_aPasswordsLB(m_aPasswordsLBContainer)
92 0 : ,m_aRemoveBtn ( this, CUI_RES( PB_REMOVE ) )
93 0 : ,m_aRemoveAllBtn ( this, CUI_RES( PB_REMOVEALL ) )
94 0 : ,m_aChangeBtn ( this, CUI_RES( PB_CHANGE ) )
95 0 : ,m_aButtonsFL ( this, CUI_RES( FL_BUTTONS ) )
96 0 : ,m_aCloseBtn ( this, CUI_RES( PB_CLOSE ) )
97 0 : ,m_aHelpBtn ( this, CUI_RES( PB_HELP ) )
98 0 : ,m_nPos ( -1 )
99 : {
100 : static long aStaticTabs[]= { 3, 0, 150, 250 };
101 0 : m_aPasswordsLB.SetTabs( aStaticTabs );
102 0 : m_aPasswordsLB.InsertHeaderItem( 1, CUI_RESSTR( STR_WEBSITE ),
103 0 : HIB_LEFT | HIB_VCENTER | HIB_FIXEDPOS | HIB_CLICKABLE | HIB_UPARROW );
104 0 : m_aPasswordsLB.InsertHeaderItem( 2, CUI_RESSTR( STR_USERNAME ),
105 0 : HIB_LEFT | HIB_VCENTER | HIB_FIXEDPOS );
106 0 : m_aPasswordsLB.ResetTabs();
107 :
108 0 : FreeResource();
109 :
110 0 : m_aPasswordsLB.SetHeaderBarClickHdl( LINK( this, WebConnectionInfoDialog, HeaderBarClickedHdl ) );
111 0 : m_aRemoveBtn.SetClickHdl( LINK( this, WebConnectionInfoDialog, RemovePasswordHdl ) );
112 0 : m_aRemoveAllBtn.SetClickHdl( LINK( this, WebConnectionInfoDialog, RemoveAllPasswordsHdl ) );
113 0 : m_aChangeBtn.SetClickHdl( LINK( this, WebConnectionInfoDialog, ChangePasswordHdl ) );
114 :
115 : // one button too small for its text?
116 0 : sal_Int32 i = 0;
117 0 : long nBtnTextWidth = 0;
118 0 : Window* pButtons[] = { &m_aRemoveBtn, &m_aRemoveAllBtn, &m_aChangeBtn };
119 0 : Window** pButton = pButtons;
120 0 : const sal_Int32 nBCount = SAL_N_ELEMENTS( pButtons );
121 0 : for ( ; i < nBCount; ++i, ++pButton )
122 : {
123 0 : long nTemp = (*pButton)->GetCtrlTextWidth( (*pButton)->GetText() );
124 0 : if ( nTemp > nBtnTextWidth )
125 0 : nBtnTextWidth = nTemp;
126 : }
127 0 : nBtnTextWidth = nBtnTextWidth * 115 / 100; // a little offset
128 0 : long nButtonWidth = m_aRemoveBtn.GetSizePixel().Width();
129 0 : if ( nBtnTextWidth > nButtonWidth )
130 : {
131 : // so make the buttons broader and its control in front of it smaller
132 0 : long nDelta = nBtnTextWidth - nButtonWidth;
133 0 : pButton = pButtons;
134 0 : for ( i = 0; i < nBCount; ++i, ++pButton )
135 : {
136 0 : Point aNewPos = (*pButton)->GetPosPixel();
137 0 : if ( &m_aRemoveAllBtn == (*pButton) )
138 0 : aNewPos.X() += nDelta;
139 0 : else if ( &m_aChangeBtn == (*pButton) )
140 0 : aNewPos.X() -= nDelta;
141 0 : Size aNewSize = (*pButton)->GetSizePixel();
142 0 : aNewSize.Width() += nDelta;
143 0 : (*pButton)->SetPosSizePixel( aNewPos, aNewSize );
144 : }
145 : }
146 :
147 0 : FillPasswordList();
148 :
149 0 : m_aRemoveBtn.SetClickHdl( LINK( this, WebConnectionInfoDialog, RemovePasswordHdl ) );
150 0 : m_aRemoveAllBtn.SetClickHdl( LINK( this, WebConnectionInfoDialog, RemoveAllPasswordsHdl ) );
151 0 : m_aChangeBtn.SetClickHdl( LINK( this, WebConnectionInfoDialog, ChangePasswordHdl ) );
152 0 : m_aPasswordsLB.SetSelectHdl( LINK( this, WebConnectionInfoDialog, EntrySelectedHdl ) );
153 :
154 0 : m_aRemoveBtn.Enable( sal_False );
155 0 : m_aChangeBtn.Enable( sal_False );
156 :
157 0 : HeaderBarClickedHdl( NULL );
158 0 : }
159 :
160 : // -----------------------------------------------------------------------
161 0 : WebConnectionInfoDialog::~WebConnectionInfoDialog()
162 : {
163 0 : }
164 :
165 : // -----------------------------------------------------------------------
166 0 : IMPL_LINK( WebConnectionInfoDialog, HeaderBarClickedHdl, SvxSimpleTable*, pTable )
167 : {
168 0 : m_aPasswordsLB.Resort( NULL == pTable );
169 0 : return 0;
170 : }
171 :
172 : // -----------------------------------------------------------------------
173 0 : void WebConnectionInfoDialog::FillPasswordList()
174 : {
175 : try
176 : {
177 : uno::Reference< task::XPasswordContainer2 > xMasterPasswd(
178 0 : task::PasswordContainer::create(comphelper::getProcessComponentContext()));
179 :
180 0 : if ( xMasterPasswd->isPersistentStoringAllowed() )
181 : {
182 : uno::Reference< task::XInteractionHandler > xInteractionHandler(
183 : task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), 0),
184 0 : uno::UNO_QUERY);
185 :
186 0 : uno::Sequence< task::UrlRecord > aURLEntries = xMasterPasswd->getAllPersistent( xInteractionHandler );
187 0 : sal_Int32 nCount = 0;
188 0 : for ( sal_Int32 nURLInd = 0; nURLInd < aURLEntries.getLength(); nURLInd++ )
189 : {
190 0 : for ( sal_Int32 nUserInd = 0; nUserInd < aURLEntries[nURLInd].UserList.getLength(); nUserInd++ )
191 : {
192 0 : ::rtl::OUString aUIEntry( aURLEntries[nURLInd].Url );
193 0 : aUIEntry += ::rtl::OUString::valueOf( (sal_Unicode)'\t' );
194 0 : aUIEntry += aURLEntries[nURLInd].UserList[nUserInd].UserName;
195 0 : SvTreeListEntry* pEntry = m_aPasswordsLB.InsertEntry( aUIEntry );
196 0 : pEntry->SetUserData( (void*)(sal_IntPtr)(nCount++) );
197 0 : }
198 : }
199 :
200 : // remember pos of first url container entry.
201 0 : m_nPos = nCount;
202 :
203 : uno::Sequence< rtl::OUString > aUrls
204 0 : = xMasterPasswd->getUrls( sal_True /* OnlyPersistent */ );
205 :
206 0 : for ( sal_Int32 nURLIdx = 0; nURLIdx < aUrls.getLength(); nURLIdx++ )
207 : {
208 0 : ::rtl::OUString aUIEntry( aUrls[ nURLIdx ] );
209 0 : aUIEntry += ::rtl::OUString::valueOf( (sal_Unicode)'\t' );
210 0 : aUIEntry += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "*" ) );
211 0 : SvTreeListEntry* pEntry = m_aPasswordsLB.InsertEntry( aUIEntry );
212 0 : pEntry->SetUserData( (void*)(sal_IntPtr)(nCount++) );
213 0 : }
214 0 : }
215 : }
216 0 : catch( uno::Exception& )
217 : {}
218 0 : }
219 :
220 : // -----------------------------------------------------------------------
221 0 : IMPL_LINK_NOARG(WebConnectionInfoDialog, RemovePasswordHdl)
222 : {
223 : try
224 : {
225 0 : SvTreeListEntry* pEntry = m_aPasswordsLB.GetCurEntry();
226 0 : if ( pEntry )
227 : {
228 0 : ::rtl::OUString aURL = m_aPasswordsLB.GetEntryText( pEntry, 0 );
229 0 : ::rtl::OUString aUserName = m_aPasswordsLB.GetEntryText( pEntry, 1 );
230 :
231 : uno::Reference< task::XPasswordContainer2 > xPasswdContainer(
232 0 : task::PasswordContainer::create(comphelper::getProcessComponentContext()));
233 :
234 0 : sal_Int32 nPos = (sal_Int32)(sal_IntPtr)pEntry->GetUserData();
235 0 : if ( nPos < m_nPos )
236 : {
237 0 : xPasswdContainer->removePersistent( aURL, aUserName );
238 : }
239 : else
240 : {
241 0 : xPasswdContainer->removeUrl( aURL );
242 : }
243 0 : m_aPasswordsLB.RemoveEntry( pEntry );
244 : }
245 : }
246 0 : catch( uno::Exception& )
247 : {}
248 :
249 0 : return 0;
250 : }
251 :
252 : // -----------------------------------------------------------------------
253 0 : IMPL_LINK_NOARG(WebConnectionInfoDialog, RemoveAllPasswordsHdl)
254 : {
255 : try
256 : {
257 : uno::Reference< task::XPasswordContainer2 > xPasswdContainer(
258 0 : task::PasswordContainer::create(comphelper::getProcessComponentContext()));
259 :
260 : // should the master password be requested before?
261 0 : xPasswdContainer->removeAllPersistent();
262 :
263 : uno::Sequence< rtl::OUString > aUrls
264 0 : = xPasswdContainer->getUrls( sal_True /* OnlyPersistent */ );
265 0 : for ( sal_Int32 nURLIdx = 0; nURLIdx < aUrls.getLength(); nURLIdx++ )
266 0 : xPasswdContainer->removeUrl( aUrls[ nURLIdx ] );
267 :
268 0 : m_aPasswordsLB.Clear();
269 : }
270 0 : catch( uno::Exception& )
271 : {}
272 :
273 0 : return 0;
274 : }
275 :
276 : // -----------------------------------------------------------------------
277 0 : IMPL_LINK_NOARG(WebConnectionInfoDialog, ChangePasswordHdl)
278 : {
279 : try
280 : {
281 0 : SvTreeListEntry* pEntry = m_aPasswordsLB.GetCurEntry();
282 0 : if ( pEntry )
283 : {
284 0 : ::rtl::OUString aURL = m_aPasswordsLB.GetEntryText( pEntry, 0 );
285 0 : ::rtl::OUString aUserName = m_aPasswordsLB.GetEntryText( pEntry, 1 );
286 :
287 : ::comphelper::SimplePasswordRequest* pPasswordRequest
288 0 : = new ::comphelper::SimplePasswordRequest( task::PasswordRequestMode_PASSWORD_CREATE );
289 0 : uno::Reference< task::XInteractionRequest > rRequest( pPasswordRequest );
290 :
291 : uno::Reference< task::XInteractionHandler > xInteractionHandler(
292 : task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), 0),
293 0 : uno::UNO_QUERY );
294 0 : xInteractionHandler->handle( rRequest );
295 :
296 0 : if ( pPasswordRequest->isPassword() )
297 : {
298 0 : String aNewPass = pPasswordRequest->getPassword();
299 0 : uno::Sequence< ::rtl::OUString > aPasswd( 1 );
300 0 : aPasswd[0] = aNewPass;
301 :
302 : uno::Reference< task::XPasswordContainer2 > xPasswdContainer(
303 0 : task::PasswordContainer::create(comphelper::getProcessComponentContext()));
304 0 : xPasswdContainer->addPersistent(
305 0 : aURL, aUserName, aPasswd, xInteractionHandler );
306 0 : }
307 : }
308 : }
309 0 : catch( uno::Exception& )
310 : {}
311 :
312 0 : return 0;
313 : }
314 :
315 : // -----------------------------------------------------------------------
316 0 : IMPL_LINK_NOARG(WebConnectionInfoDialog, EntrySelectedHdl)
317 : {
318 0 : SvTreeListEntry* pEntry = m_aPasswordsLB.GetCurEntry();
319 0 : if ( !pEntry )
320 : {
321 0 : m_aRemoveBtn.Enable( sal_False );
322 0 : m_aChangeBtn.Enable( sal_False );
323 : }
324 : else
325 : {
326 0 : m_aRemoveBtn.Enable( sal_True );
327 :
328 : // url container entries (-> use system credentials) have
329 : // no password
330 0 : sal_Int32 nPos = (sal_Int32)(sal_IntPtr)pEntry->GetUserData();
331 0 : m_aChangeBtn.Enable( nPos < m_nPos );
332 : }
333 :
334 0 : return 0;
335 : }
336 :
337 : //........................................................................
338 : } // namespace svx
339 : //........................................................................
340 :
341 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|