LCOV - code coverage report
Current view: top level - cui/source/options - webconninfo.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 0 157 0.0 %
Date: 2015-06-13 12:38:46 Functions: 0 22 0.0 %
Legend: Lines: hit not hit

          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 & HeaderBarItemBits::UPARROW ) == HeaderBarItemBits::UPARROW );
      59           0 :         SvSortMode eMode = SortAscending;
      60             : 
      61           0 :         if ( bUp )
      62             :         {
      63           0 :             nBits &= ~HeaderBarItemBits::UPARROW;
      64           0 :             nBits |= HeaderBarItemBits::DOWNARROW;
      65           0 :             eMode = SortDescending;
      66             :         }
      67             :         else
      68             :         {
      69           0 :             nBits &= ~HeaderBarItemBits::DOWNARROW;
      70           0 :             nBits |= HeaderBarItemBits::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 = VclPtr<PasswordTable>::Create(*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 :         HeaderBarItemBits::LEFT | HeaderBarItemBits::VCENTER | HeaderBarItemBits::FIXEDPOS | HeaderBarItemBits::CLICKABLE | HeaderBarItemBits::UPARROW );
     120           0 :     m_pPasswordsLB->InsertHeaderItem( 2, get<FixedText>("username")->GetText(),
     121           0 :         HeaderBarItemBits::LEFT | HeaderBarItemBits::VCENTER | HeaderBarItemBits::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 :     disposeOnce();
     146           0 : }
     147             : 
     148           0 : void WebConnectionInfoDialog::dispose()
     149             : {
     150           0 :     m_pPasswordsLB.disposeAndClear();
     151           0 :     m_pRemoveBtn.clear();
     152           0 :     m_pRemoveAllBtn.clear();
     153           0 :     m_pChangeBtn.clear();
     154           0 :     ModalDialog::dispose();
     155           0 : }
     156             : 
     157           0 : IMPL_LINK( WebConnectionInfoDialog, HeaderBarClickedHdl, SvSimpleTable*, pTable )
     158             : {
     159           0 :     m_pPasswordsLB->Resort( NULL == pTable );
     160           0 :     return 0;
     161             : }
     162             : 
     163             : 
     164           0 : void WebConnectionInfoDialog::FillPasswordList()
     165             : {
     166             :     try
     167             :     {
     168             :         uno::Reference< task::XPasswordContainer2 > xMasterPasswd(
     169           0 :             task::PasswordContainer::create(comphelper::getProcessComponentContext()));
     170             : 
     171           0 :         if ( xMasterPasswd->isPersistentStoringAllowed() )
     172             :         {
     173             :             uno::Reference< task::XInteractionHandler > xInteractionHandler(
     174             :                 task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), 0),
     175           0 :                 uno::UNO_QUERY);
     176             : 
     177           0 :             uno::Sequence< task::UrlRecord > aURLEntries = xMasterPasswd->getAllPersistent( xInteractionHandler );
     178           0 :             sal_Int32 nCount = 0;
     179           0 :             for ( sal_Int32 nURLInd = 0; nURLInd < aURLEntries.getLength(); nURLInd++ )
     180             :             {
     181           0 :                 for ( sal_Int32 nUserInd = 0; nUserInd < aURLEntries[nURLInd].UserList.getLength(); nUserInd++ )
     182             :                 {
     183           0 :                     OUString aUIEntry( aURLEntries[nURLInd].Url );
     184           0 :                     aUIEntry += OUString( (sal_Unicode)'\t' );
     185           0 :                     aUIEntry += aURLEntries[nURLInd].UserList[nUserInd].UserName;
     186           0 :                     SvTreeListEntry* pEntry = m_pPasswordsLB->InsertEntry( aUIEntry );
     187           0 :                     pEntry->SetUserData( reinterpret_cast<void*>(nCount++) );
     188           0 :                 }
     189             :             }
     190             : 
     191             :             // remember pos of first url container entry.
     192           0 :             m_nPos = nCount;
     193             : 
     194             :             uno::Sequence< OUString > aUrls
     195           0 :                 = xMasterPasswd->getUrls( sal_True /* OnlyPersistent */ );
     196             : 
     197           0 :             for ( sal_Int32 nURLIdx = 0; nURLIdx < aUrls.getLength(); nURLIdx++ )
     198             :             {
     199           0 :                 OUString aUIEntry( aUrls[ nURLIdx ] );
     200           0 :                 aUIEntry += OUString( (sal_Unicode)'\t' );
     201           0 :                 aUIEntry += OUString( "*" );
     202           0 :                 SvTreeListEntry* pEntry = m_pPasswordsLB->InsertEntry( aUIEntry );
     203           0 :                 pEntry->SetUserData( reinterpret_cast<void*>(nCount++) );
     204           0 :             }
     205           0 :         }
     206             :     }
     207           0 :     catch( uno::Exception& )
     208             :     {}
     209           0 : }
     210             : 
     211             : 
     212           0 : IMPL_LINK_NOARG(WebConnectionInfoDialog, RemovePasswordHdl)
     213             : {
     214             :     try
     215             :     {
     216           0 :         SvTreeListEntry* pEntry = m_pPasswordsLB->GetCurEntry();
     217           0 :         if ( pEntry )
     218             :         {
     219           0 :             OUString aURL = SvTabListBox::GetEntryText( pEntry, 0 );
     220           0 :             OUString aUserName = SvTabListBox::GetEntryText( pEntry, 1 );
     221             : 
     222             :             uno::Reference< task::XPasswordContainer2 > xPasswdContainer(
     223           0 :                 task::PasswordContainer::create(comphelper::getProcessComponentContext()));
     224             : 
     225           0 :             sal_Int32 nPos = (sal_Int32)reinterpret_cast<sal_IntPtr>(pEntry->GetUserData());
     226           0 :             if ( nPos < m_nPos )
     227             :             {
     228           0 :                 xPasswdContainer->removePersistent( aURL, aUserName );
     229             :             }
     230             :             else
     231             :             {
     232           0 :                 xPasswdContainer->removeUrl( aURL );
     233             :             }
     234           0 :             m_pPasswordsLB->RemoveEntry( pEntry );
     235             :         }
     236             :     }
     237           0 :     catch( uno::Exception& )
     238             :     {}
     239             : 
     240           0 :     return 0;
     241             : }
     242             : 
     243             : 
     244           0 : IMPL_LINK_NOARG(WebConnectionInfoDialog, RemoveAllPasswordsHdl)
     245             : {
     246             :     try
     247             :     {
     248             :         uno::Reference< task::XPasswordContainer2 > xPasswdContainer(
     249           0 :             task::PasswordContainer::create(comphelper::getProcessComponentContext()));
     250             : 
     251             :         // should the master password be requested before?
     252           0 :         xPasswdContainer->removeAllPersistent();
     253             : 
     254             :         uno::Sequence< OUString > aUrls
     255           0 :             = xPasswdContainer->getUrls( sal_True /* OnlyPersistent */ );
     256           0 :         for ( sal_Int32 nURLIdx = 0; nURLIdx < aUrls.getLength(); nURLIdx++ )
     257           0 :             xPasswdContainer->removeUrl( aUrls[ nURLIdx ] );
     258             : 
     259           0 :         m_pPasswordsLB->Clear();
     260             :     }
     261           0 :     catch( uno::Exception& )
     262             :     {}
     263             : 
     264           0 :     return 0;
     265             : }
     266             : 
     267             : 
     268           0 : IMPL_LINK_NOARG(WebConnectionInfoDialog, ChangePasswordHdl)
     269             : {
     270             :     try
     271             :     {
     272           0 :         SvTreeListEntry* pEntry = m_pPasswordsLB->GetCurEntry();
     273           0 :         if ( pEntry )
     274             :         {
     275           0 :             OUString aURL = SvTabListBox::GetEntryText( pEntry, 0 );
     276           0 :             OUString aUserName = SvTabListBox::GetEntryText( pEntry, 1 );
     277             : 
     278             :             ::comphelper::SimplePasswordRequest* pPasswordRequest
     279           0 :                   = new ::comphelper::SimplePasswordRequest( task::PasswordRequestMode_PASSWORD_CREATE );
     280           0 :             uno::Reference< task::XInteractionRequest > rRequest( pPasswordRequest );
     281             : 
     282             :             uno::Reference< task::XInteractionHandler > xInteractionHandler(
     283             :                 task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), 0),
     284           0 :                 uno::UNO_QUERY );
     285           0 :             xInteractionHandler->handle( rRequest );
     286             : 
     287           0 :             if ( pPasswordRequest->isPassword() )
     288             :             {
     289           0 :                 OUString aNewPass = pPasswordRequest->getPassword();
     290           0 :                 uno::Sequence< OUString > aPasswd( 1 );
     291           0 :                 aPasswd[0] = aNewPass;
     292             : 
     293             :                 uno::Reference< task::XPasswordContainer2 > xPasswdContainer(
     294           0 :                     task::PasswordContainer::create(comphelper::getProcessComponentContext()));
     295           0 :                 xPasswdContainer->addPersistent(
     296           0 :                     aURL, aUserName, aPasswd, xInteractionHandler );
     297           0 :             }
     298             :         }
     299             :     }
     300           0 :     catch( uno::Exception& )
     301             :     {}
     302             : 
     303           0 :     return 0;
     304             : }
     305             : 
     306             : 
     307           0 : IMPL_LINK_NOARG(WebConnectionInfoDialog, EntrySelectedHdl)
     308             : {
     309           0 :     SvTreeListEntry* pEntry = m_pPasswordsLB->GetCurEntry();
     310           0 :     if ( !pEntry )
     311             :     {
     312           0 :         m_pRemoveBtn->Enable( false );
     313           0 :         m_pChangeBtn->Enable( false );
     314             :     }
     315             :     else
     316             :     {
     317           0 :         m_pRemoveBtn->Enable( true );
     318             : 
     319             :         // url container entries (-> use system credentials) have
     320             :         // no password
     321           0 :         sal_Int32 nPos = (sal_Int32)reinterpret_cast<sal_IntPtr>(pEntry->GetUserData());
     322           0 :         m_pChangeBtn->Enable( nPos < m_nPos );
     323             :     }
     324             : 
     325           0 :     return 0;
     326             : }
     327             : 
     328             : 
     329           0 : }
     330             : 
     331             : 
     332             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11