summaryrefslogtreecommitdiff
path: root/cui/source/dialogs/hlinettp.cxx
blob: 18df910f6054804dc8bd686c320683727c7a1598 (plain)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/* -*- 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 <unotools/useroptions.hxx>
#include <svl/adrparse.hxx>

#include <hlinettp.hxx>
#include <hlmarkwn_def.hxx>

sal_Char const sAnonymous[]    = "anonymous";
sal_Char const sFTPScheme[]    = INET_FTP_SCHEME;

/*************************************************************************
|*
|* Constructor / Destructor
|*
|************************************************************************/
SvxHyperlinkInternetTp::SvxHyperlinkInternetTp(weld::Container* pParent,
                                               SvxHpLinkDlg* pDlg,
                                               const SfxItemSet* pItemSet)
    : SvxHyperlinkTabPageBase(pParent, pDlg, "cui/ui/hyperlinkinternetpage.ui", "HyperlinkInternetPage",
                              pItemSet)
    , m_bMarkWndOpen(false)
    , m_xRbtLinktypInternet(xBuilder->weld_radio_button("linktyp_internet"))
    , m_xRbtLinktypFTP(xBuilder->weld_radio_button("linktyp_ftp"))
    , m_xCbbTarget(new SvxHyperURLBox(xBuilder->weld_combo_box("target")))
    , m_xFtLogin(xBuilder->weld_label("login_label"))
    , m_xEdLogin(xBuilder->weld_entry("login"))
    , m_xFtPassword(xBuilder->weld_label("password_label"))
    , m_xEdPassword(xBuilder->weld_entry("password"))
    , m_xCbAnonymous(xBuilder->weld_check_button("anonymous"))
{
    m_xCbbTarget->SetSmartProtocol(INetProtocol::Http);

    InitStdControls();

    m_xCbbTarget->show();

    SetExchangeSupport ();

    // set defaults
    m_xRbtLinktypInternet->set_active(true);

    // set handlers
    Link<weld::Button&, void> aLink( LINK ( this, SvxHyperlinkInternetTp, Click_SmartProtocol_Impl ) );
    m_xRbtLinktypInternet->connect_clicked( aLink );
    m_xRbtLinktypFTP->connect_clicked( aLink );
    m_xCbAnonymous->connect_clicked( LINK ( this, SvxHyperlinkInternetTp, ClickAnonymousHdl_Impl ) );
    m_xEdLogin->connect_changed( LINK ( this, SvxHyperlinkInternetTp, ModifiedLoginHdl_Impl ) );
    m_xCbbTarget->connect_focus_out( LINK ( this, SvxHyperlinkInternetTp, LostFocusTargetHdl_Impl ) );
    m_xCbbTarget->connect_changed( LINK ( this, SvxHyperlinkInternetTp, ModifiedTargetHdl_Impl ) );
    maTimer.SetInvokeHandler ( LINK ( this, SvxHyperlinkInternetTp, TimeoutHdl_Impl ) );
}

SvxHyperlinkInternetTp::~SvxHyperlinkInternetTp()
{
}

/*************************************************************************
|*
|* Fill the all dialog-controls except controls in groupbox "more..."
|*
|************************************************************************/
void SvxHyperlinkInternetTp::FillDlgFields(const OUString& rStrURL)
{
    INetURLObject aURL(rStrURL);
    OUString aStrScheme(GetSchemeFromURL(rStrURL));

    // set additional controls for FTP: Username / Password
    if (aStrScheme.startsWith(sFTPScheme))
    {
        if ( aURL.GetUser().toAsciiLowerCase().startsWith( sAnonymous ) )
            setAnonymousFTPUser();
        else
            setFTPUser(aURL.GetUser(), aURL.GetPass());

        //do not show password and user in url
        if(!aURL.GetUser().isEmpty() || !aURL.GetPass().isEmpty() )
            aURL.SetUserAndPass("", "");
    }

    // set URL-field
    // Show the scheme, #72740
    if ( aURL.GetProtocol() != INetProtocol::NotValid )
        m_xCbbTarget->set_entry_text( aURL.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous ) );
    else
        m_xCbbTarget->set_entry_text(rStrURL);

    SetScheme(aStrScheme);
}

void SvxHyperlinkInternetTp::setAnonymousFTPUser()
{
    m_xEdLogin->set_text(sAnonymous);
    SvAddressParser aAddress(SvtUserOptions().GetEmail());
    m_xEdPassword->set_text(aAddress.Count() ? aAddress.GetEmailAddress(0) : OUString());

    m_xFtLogin->set_sensitive(false);
    m_xFtPassword->set_sensitive(false);
    m_xEdLogin->set_sensitive(false);
    m_xEdPassword->set_sensitive(false);
    m_xCbAnonymous->set_active(true);
}

void SvxHyperlinkInternetTp::setFTPUser(const OUString& rUser, const OUString& rPassword)
{
    m_xEdLogin->set_text(rUser);
    m_xEdPassword->set_text(rPassword);

    m_xFtLogin->set_sensitive(true);
    m_xFtPassword->set_sensitive(true);
    m_xEdLogin->set_sensitive(true);
    m_xEdPassword->set_sensitive(true);
    m_xCbAnonymous->set_active(false);
}

/*************************************************************************
|*
|* retrieve and prepare data from dialog-fields
|*
|************************************************************************/

void SvxHyperlinkInternetTp::GetCurentItemData ( OUString& rStrURL, OUString& aStrName,
                                                 OUString& aStrIntName, OUString& aStrFrame,
                                                 SvxLinkInsertMode& eMode )
{
    rStrURL = CreateAbsoluteURL();
    GetDataFromCommonFields( aStrName, aStrIntName, aStrFrame, eMode );
}

OUString SvxHyperlinkInternetTp::CreateAbsoluteURL() const
{
    // erase leading and trailing whitespaces
    OUString aStrURL(m_xCbbTarget->get_active_text().trim());

    INetURLObject aURL(aStrURL);

    if( aURL.GetProtocol() == INetProtocol::NotValid )
    {
        aURL.SetSmartProtocol( GetSmartProtocolFromButtons() );
        aURL.SetSmartURL(aStrURL);
    }

    // username and password for ftp-url
    if( aURL.GetProtocol() == INetProtocol::Ftp && !m_xEdLogin->get_text().isEmpty() )
        aURL.SetUserAndPass ( m_xEdLogin->get_text(), m_xEdPassword->get_text() );

    if ( aURL.GetProtocol() != INetProtocol::NotValid )
        return aURL.GetMainURL( INetURLObject::DecodeMechanism::ToIUri );
    else //#105788# always create a URL even if it is not valid
        return aStrURL;
}

/*************************************************************************
|*
|* static method to create Tabpage
|*
|************************************************************************/

std::unique_ptr<IconChoicePage> SvxHyperlinkInternetTp::Create(weld::Container* pWindow, SvxHpLinkDlg* pDlg, const SfxItemSet* pItemSet)
{
    return std::make_unique<SvxHyperlinkInternetTp>(pWindow, pDlg, pItemSet);
}

/*************************************************************************
|*
|* Set initial focus
|*
|************************************************************************/
void SvxHyperlinkInternetTp::SetInitFocus()
{
    m_xCbbTarget->grab_focus();
}

/*************************************************************************
|*
|* Contents of editfield "Target" modified
|*
|************************************************************************/
IMPL_LINK_NOARG(SvxHyperlinkInternetTp, ModifiedTargetHdl_Impl, weld::ComboBox&, void)
{
    OUString aScheme = GetSchemeFromURL( m_xCbbTarget->get_active_text() );
    if( !aScheme.isEmpty() )
        SetScheme( aScheme );

    // start timer
    maTimer.SetTimeout( 2500 );
    maTimer.Start();
}

/*************************************************************************
|*
|* If target-field was modify, to browse the new doc after timeout
|*
|************************************************************************/
IMPL_LINK_NOARG(SvxHyperlinkInternetTp, TimeoutHdl_Impl, Timer *, void)
{
    RefreshMarkWindow();
}

/*************************************************************************
|*
|* Contents of editfield "Login" modified
|*
|************************************************************************/
IMPL_LINK_NOARG(SvxHyperlinkInternetTp, ModifiedLoginHdl_Impl, weld::Entry&, void)
{
    OUString aStrLogin ( m_xEdLogin->get_text() );
    if ( aStrLogin.equalsIgnoreAsciiCase( sAnonymous ) )
    {
        m_xCbAnonymous->set_active(true);
        ClickAnonymousHdl_Impl(*m_xCbAnonymous);
    }
}

void SvxHyperlinkInternetTp::SetScheme(const OUString& rScheme)
{
    //if rScheme is empty or unknown the default behaviour is like it where HTTP
    bool bFTP = rScheme.startsWith(sFTPScheme);
    bool bInternet = !bFTP;

    //update protocol button selection:
    m_xRbtLinktypFTP->set_active(bFTP);
    m_xRbtLinktypInternet->set_active(bInternet);

    //update target:
    RemoveImproperProtocol(rScheme);
    m_xCbbTarget->SetSmartProtocol( GetSmartProtocolFromButtons() );

    //show/hide  special fields for FTP:
    m_xFtLogin->set_visible( bFTP );
    m_xFtPassword->set_visible( bFTP );
    m_xEdLogin->set_visible( bFTP );
    m_xEdPassword->set_visible( bFTP );
    m_xCbAnonymous->set_visible( bFTP );

    //update 'link target in document'-window and opening-button
    if (rScheme.startsWith(INET_HTTP_SCHEME) || rScheme.isEmpty())
    {
        if ( m_bMarkWndOpen )
            ShowMarkWnd ();
    }
    else
    {
        //disable for https and ftp
        if ( m_bMarkWndOpen )
            HideMarkWnd ();
    }
}

/*************************************************************************
|*
|* Remove protocol if it does not fit to the current button selection
|*
|************************************************************************/

void SvxHyperlinkInternetTp::RemoveImproperProtocol(const OUString& aProperScheme)
{
    OUString aStrURL ( m_xCbbTarget->get_active_text() );
    if ( !aStrURL.isEmpty() )
    {
        OUString aStrScheme(GetSchemeFromURL(aStrURL));
        if ( !aStrScheme.isEmpty() && aStrScheme != aProperScheme )
        {
            aStrURL = aStrURL.copy( aStrScheme.getLength() );
            m_xCbbTarget->set_entry_text( aStrURL );
        }
    }
}

OUString SvxHyperlinkInternetTp::GetSchemeFromButtons() const
{
    if( m_xRbtLinktypFTP->get_active() )
        return INET_FTP_SCHEME;
    return INET_HTTP_SCHEME;
}

INetProtocol SvxHyperlinkInternetTp::GetSmartProtocolFromButtons() const
{
    if( m_xRbtLinktypFTP->get_active() )
    {
        return INetProtocol::Ftp;
    }
    return INetProtocol::Http;
}

/*************************************************************************
|*
|* Click on Radiobutton : Internet or FTP
|*
|************************************************************************/
IMPL_LINK_NOARG(SvxHyperlinkInternetTp, Click_SmartProtocol_Impl, weld::Button&, void)
{
    OUString aScheme = GetSchemeFromButtons();
    SetScheme(aScheme);
}

/*************************************************************************
|*
|* Click on Checkbox : Anonymous user
|*
|************************************************************************/
IMPL_LINK_NOARG(SvxHyperlinkInternetTp, ClickAnonymousHdl_Impl, weld::Button&, void)
{
    // disable login-editfields if checked
    if ( m_xCbAnonymous->get_active() )
    {
        if ( m_xEdLogin->get_text().toAsciiLowerCase().startsWith( sAnonymous ) )
        {
            maStrOldUser.clear();
            maStrOldPassword.clear();
        }
        else
        {
            maStrOldUser = m_xEdLogin->get_text();
            maStrOldPassword = m_xEdPassword->get_text();
        }

        setAnonymousFTPUser();
    }
    else
        setFTPUser(maStrOldUser, maStrOldPassword);
}

/*************************************************************************
|*
|* Combobox Target lost the focus
|*
|************************************************************************/
IMPL_LINK_NOARG(SvxHyperlinkInternetTp, LostFocusTargetHdl_Impl, weld::Widget&, void)
{
    RefreshMarkWindow();
}

void SvxHyperlinkInternetTp::RefreshMarkWindow()
{
    if (m_xRbtLinktypInternet->get_active() && IsMarkWndVisible())
    {
        weld::WaitObject aWait(mpDialog->getDialog());
        OUString aStrURL( CreateAbsoluteURL() );
        if ( !aStrURL.isEmpty() )
            mxMarkWnd->RefreshTree ( aStrURL );
        else
            mxMarkWnd->SetError( LERR_DOCNOTOPEN );
    }
}

/*************************************************************************
|*
|* Get String from Bookmark-Wnd
|*
|************************************************************************/
void SvxHyperlinkInternetTp::SetMarkStr ( const OUString& aStrMark )
{
    OUString aStrURL(m_xCbbTarget->get_active_text());

    const sal_Unicode sUHash = '#';
    sal_Int32 nPos = aStrURL.lastIndexOf( sUHash );

    if( nPos != -1 )
        aStrURL = aStrURL.copy(0, nPos);

    aStrURL += OUStringChar(sUHash) + aStrMark;

    m_xCbbTarget->set_entry_text(aStrURL);
}

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