1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include "webconninfo.hxx"
#include <com/sun/star/task/InteractionHandler.hpp>
#include <com/sun/star/task/PasswordContainer.hpp>
#include <com/sun/star/task/UrlRecord.hpp>
#include <com/sun/star/task/XPasswordContainer2.hpp>
#include <comphelper/processfactory.hxx>
#include <comphelper/docpasswordrequest.hxx>

using namespace ::com::sun::star;


namespace svx
{

// class WebConnectionInfoDialog -----------------------------------------

WebConnectionInfoDialog::WebConnectionInfoDialog(weld::Window* pParent)
    : GenericDialogController(pParent, "cui/ui/storedwebconnectiondialog.ui", "StoredWebConnectionDialog")
    , m_nPos( -1 )
    , m_xRemoveBtn(m_xBuilder->weld_button("remove"))
    , m_xRemoveAllBtn(m_xBuilder->weld_button("removeall"))
    , m_xChangeBtn(m_xBuilder->weld_button("change"))
    , m_xPasswordsLB(m_xBuilder->weld_tree_view("logins"))
{
    std::vector<int> aWidths;
    aWidths.push_back(m_xPasswordsLB->get_approximate_digit_width() * 50);
    m_xPasswordsLB->set_column_fixed_widths(aWidths);
    m_xPasswordsLB->set_size_request(m_xPasswordsLB->get_approximate_digit_width() * 70,
                                     m_xPasswordsLB->get_height_rows(8));

    m_xPasswordsLB->connect_column_clicked(LINK(this, WebConnectionInfoDialog, HeaderBarClickedHdl));

    FillPasswordList();

    m_xRemoveBtn->connect_clicked( LINK( this, WebConnectionInfoDialog, RemovePasswordHdl ) );
    m_xRemoveAllBtn->connect_clicked( LINK( this, WebConnectionInfoDialog, RemoveAllPasswordsHdl ) );
    m_xChangeBtn->connect_clicked( LINK( this, WebConnectionInfoDialog, ChangePasswordHdl ) );
    m_xPasswordsLB->connect_changed( LINK( this, WebConnectionInfoDialog, EntrySelectedHdl ) );

    m_xRemoveBtn->set_sensitive( false );
    m_xChangeBtn->set_sensitive( false );

    m_xPasswordsLB->make_sorted();
}

WebConnectionInfoDialog::~WebConnectionInfoDialog()
{
}

IMPL_LINK(WebConnectionInfoDialog, HeaderBarClickedHdl, int, nColumn, void)<--- syntax error
{
    if (nColumn == 0) // only the first column is sorted
    {
        m_xPasswordsLB->set_sort_order(!m_xPasswordsLB->get_sort_order());
    }
}

void WebConnectionInfoDialog::FillPasswordList()
{
    try
    {
        uno::Reference< task::XPasswordContainer2 > xMasterPasswd(
            task::PasswordContainer::create(comphelper::getProcessComponentContext()));

        if ( xMasterPasswd->isPersistentStoringAllowed() )
        {
            uno::Reference< task::XInteractionHandler > xInteractionHandler =
                task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), nullptr);

            const uno::Sequence< task::UrlRecord > aURLEntries = xMasterPasswd->getAllPersistent( xInteractionHandler );
            sal_Int32 nCount = 0;
            for ( task::UrlRecord const & urlEntry : aURLEntries )
            {
                for ( auto const & user : urlEntry.UserList )
                {
                    m_xPasswordsLB->append(OUString::number(nCount), urlEntry.Url);
                    m_xPasswordsLB->set_text(nCount, user.UserName, 1);
                    ++nCount;
                }
            }

            // remember pos of first url container entry.
            m_nPos = nCount;

            const uno::Sequence< OUString > aUrls
                = xMasterPasswd->getUrls( true /* OnlyPersistent */ );

            for ( OUString const & url : aUrls )
            {
                m_xPasswordsLB->append(OUString::number(nCount), url);
                m_xPasswordsLB->set_text(nCount, "*");
                ++nCount;
            }
        }
    }
    catch( uno::Exception& )
    {}
}


IMPL_LINK_NOARG(WebConnectionInfoDialog, RemovePasswordHdl, weld::Button&, void)
{
    try
    {
        int nEntry = m_xPasswordsLB->get_selected_index();
        if (nEntry != -1)
        {
            OUString aURL = m_xPasswordsLB->get_text(nEntry, 0);
            OUString aUserName = m_xPasswordsLB->get_text(nEntry, 1);

            uno::Reference< task::XPasswordContainer2 > xPasswdContainer(
                task::PasswordContainer::create(comphelper::getProcessComponentContext()));

            int nPos = m_xPasswordsLB->get_id(nEntry).toInt32();
            if ( nPos < m_nPos )
            {
                xPasswdContainer->removePersistent( aURL, aUserName );
            }
            else
            {
                xPasswdContainer->removeUrl( aURL );
            }

            m_xPasswordsLB->remove(nEntry);
        }
    }
    catch( uno::Exception& )
    {}
}

IMPL_LINK_NOARG(WebConnectionInfoDialog, RemoveAllPasswordsHdl, weld::Button&, void)
{
    try
    {
        uno::Reference< task::XPasswordContainer2 > xPasswdContainer(
            task::PasswordContainer::create(comphelper::getProcessComponentContext()));

        // should the master password be requested before?
        xPasswdContainer->removeAllPersistent();

        const uno::Sequence< OUString > aUrls
            = xPasswdContainer->getUrls( true /* OnlyPersistent */ );
        for ( OUString const & url : aUrls )
            xPasswdContainer->removeUrl( url );

        m_xPasswordsLB->clear();
    }
    catch( uno::Exception& )
    {}
}

IMPL_LINK_NOARG(WebConnectionInfoDialog, ChangePasswordHdl, weld::Button&, void)
{
    try
    {
        int nEntry = m_xPasswordsLB->get_selected_index();
        if (nEntry != -1)
        {
            OUString aURL = m_xPasswordsLB->get_text(nEntry, 0);
            OUString aUserName = m_xPasswordsLB->get_text(nEntry, 1);

            ::comphelper::SimplePasswordRequest* pPasswordRequest
                  = new ::comphelper::SimplePasswordRequest;
            uno::Reference< task::XInteractionRequest > rRequest( pPasswordRequest );

            uno::Reference< task::XInteractionHandler > xInteractionHandler =
                task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), m_xDialog->GetXWindow());
            xInteractionHandler->handle( rRequest );

            if ( pPasswordRequest->isPassword() )
            {
                OUString aNewPass = pPasswordRequest->getPassword();
                uno::Sequence<OUString> aPasswd { aNewPass };

                uno::Reference< task::XPasswordContainer2 > xPasswdContainer(
                    task::PasswordContainer::create(comphelper::getProcessComponentContext()));
                xPasswdContainer->addPersistent(
                    aURL, aUserName, aPasswd, xInteractionHandler );
            }
        }
    }
    catch( uno::Exception& )
    {}
}


IMPL_LINK_NOARG(WebConnectionInfoDialog, EntrySelectedHdl, weld::TreeView&, void)
{
    int nEntry = m_xPasswordsLB->get_selected_index();
    if (nEntry == -1)
    {
        m_xRemoveBtn->set_sensitive(false);
        m_xChangeBtn->set_sensitive(false);
    }
    else
    {
        m_xRemoveBtn->set_sensitive(true);

        // url container entries (-> use system credentials) have
        // no password
        int nPos = m_xPasswordsLB->get_id(nEntry).toInt32();
        m_xChangeBtn->set_sensitive(nPos < m_nPos);
    }
}

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */