1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <cuihyperdlg.hxx>
21 #include <osl/file.hxx>
22 #include <sfx2/filedlghelper.hxx>
23 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
25 #include <hldoctp.hxx>
26 #include <hlmarkwn_def.hxx>
28 char const sHash
[] = "#";
30 /*************************************************************************
32 |* Constructor / Destructor
34 |************************************************************************/
36 SvxHyperlinkDocTp::SvxHyperlinkDocTp(weld::Container
* pParent
, SvxHpLinkDlg
* pDlg
, const SfxItemSet
* pItemSet
)
37 : SvxHyperlinkTabPageBase(pParent
, pDlg
, u
"cui/ui/hyperlinkdocpage.ui"_ustr
, u
"HyperlinkDocPage"_ustr
, pItemSet
)
38 , m_xCbbPath(new SvxHyperURLBox(xBuilder
->weld_combo_box(u
"path"_ustr
)))
39 , m_xBtFileopen(xBuilder
->weld_button(u
"fileopen"_ustr
))
40 , m_xEdTarget(xBuilder
->weld_entry(u
"target"_ustr
))
41 , m_xFtFullURL(xBuilder
->weld_label(u
"url"_ustr
))
42 , m_xBtBrowse(xBuilder
->weld_button(u
"browse"_ustr
))
43 , m_bMarkWndOpen(false)
45 m_xCbbPath
->SetSmartProtocol(INetProtocol::File
);
50 m_xCbbPath
->SetBaseURL(INET_FILE_SCHEME
);
55 m_xBtFileopen
->connect_clicked( LINK ( this, SvxHyperlinkDocTp
, ClickFileopenHdl_Impl
) );
56 m_xBtBrowse
->connect_clicked( LINK ( this, SvxHyperlinkDocTp
, ClickTargetHdl_Impl
) );
57 m_xCbbPath
->connect_changed( LINK ( this, SvxHyperlinkDocTp
, ModifiedPathHdl_Impl
) );
58 m_xEdTarget
->connect_changed( LINK ( this, SvxHyperlinkDocTp
, ModifiedTargetHdl_Impl
) );
60 m_xCbbPath
->connect_focus_out( LINK ( this, SvxHyperlinkDocTp
, LostFocusPathHdl_Impl
) );
62 maTimer
.SetInvokeHandler ( LINK ( this, SvxHyperlinkDocTp
, TimeoutHdl_Impl
) );
65 SvxHyperlinkDocTp::~SvxHyperlinkDocTp()
69 /*************************************************************************
71 |* Fill all dialog-controls except controls in groupbox "more..."
73 |************************************************************************/
74 void SvxHyperlinkDocTp::FillDlgFields(const OUString
& rStrURL
)
76 sal_Int32 nPos
= rStrURL
.indexOf(sHash
);
78 m_xCbbPath
->set_entry_text( rStrURL
.copy( 0, ( nPos
== -1 ? rStrURL
.getLength() : nPos
) ) );
80 // set target in document at editfield
82 if ( nPos
!= -1 && nPos
< rStrURL
.getLength()-1 )
83 aStrMark
= rStrURL
.copy( nPos
+1 );
84 m_xEdTarget
->set_text( aStrMark
);
86 ModifiedPathHdl_Impl(*m_xCbbPath
->getWidget());
89 /*************************************************************************
91 |* retrieve current url-string
93 |************************************************************************/
94 OUString
SvxHyperlinkDocTp::GetCurrentURL () const
96 // get data from dialog-controls
98 OUString
aStrPath( m_xCbbPath
->get_active_text() );
99 OUString
aStrMark( m_xEdTarget
->get_text() );
101 if ( !aStrPath
.isEmpty() )
103 INetURLObject
aURL( aStrPath
);
104 if ( aURL
.GetProtocol() != INetProtocol::NotValid
) // maybe the path is already a valid
105 aStrURL
= aStrPath
; // hyperlink, then we can use this path directly
108 osl::FileBase::getFileURLFromSystemPath( aStrPath
, aStrURL
);
109 aStrURL
= INetURLObject::decode(aStrURL
, INetURLObject::DecodeMechanism::ToIUri
, RTL_TEXTENCODING_UTF8
);
112 //#105788# always create a URL even if it is not valid
113 if( aStrURL
.isEmpty() )
117 if( !aStrMark
.isEmpty() )
119 aStrURL
+= sHash
+ aStrMark
;
125 /*************************************************************************
127 |* retrieve and prepare data from dialog-fields
129 |************************************************************************/
130 void SvxHyperlinkDocTp::GetCurrentItemData ( OUString
& rStrURL
, OUString
& aStrName
,
131 OUString
& aStrIntName
, OUString
& aStrFrame
,
132 SvxLinkInsertMode
& eMode
)
134 // get data from standard-fields
135 rStrURL
= GetCurrentURL();
137 if( rStrURL
.equalsIgnoreAsciiCase( INET_FILE_SCHEME
) )
140 GetDataFromCommonFields( aStrName
, aStrIntName
, aStrFrame
, eMode
);
143 /*************************************************************************
145 |* static method to create Tabpage
147 |************************************************************************/
148 std::unique_ptr
<IconChoicePage
> SvxHyperlinkDocTp::Create(weld::Container
* pWindow
, SvxHpLinkDlg
* pDlg
, const SfxItemSet
* pItemSet
)
150 return std::make_unique
<SvxHyperlinkDocTp
>(pWindow
, pDlg
, pItemSet
);
153 /*************************************************************************
157 |************************************************************************/
158 void SvxHyperlinkDocTp::SetInitFocus()
160 m_xCbbPath
->grab_focus();
163 /*************************************************************************
165 |* Click on imagebutton : fileopen
167 |************************************************************************/
168 IMPL_LINK_NOARG(SvxHyperlinkDocTp
, ClickFileopenHdl_Impl
, weld::Button
&, void)
170 DisableClose( true );
171 // Open Fileopen-Dialog
172 sfx2::FileDialogHelper
aDlg(
173 css::ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE
, FileDialogFlags::NONE
,
174 mpDialog
->getDialog() );
175 OUString
aOldURL( GetCurrentURL() );
176 if( aOldURL
.startsWithIgnoreAsciiCase( INET_FILE_SCHEME
) )
179 osl::FileBase::getSystemPathFromFileURL(aOldURL
, aPath
);
180 aDlg
.SetDisplayFolder( aPath
);
183 ErrCode nError
= aDlg
.Execute();
184 DisableClose( false );
186 if ( ERRCODE_NONE
!= nError
)
189 OUString
aURL( aDlg
.GetPath() );
192 osl::FileBase::getSystemPathFromFileURL(aURL
, aPath
);
194 m_xCbbPath
->SetBaseURL( aURL
);
195 m_xCbbPath
->set_entry_text(aPath
);
197 if ( aOldURL
!= GetCurrentURL() )
198 ModifiedPathHdl_Impl(*m_xCbbPath
->getWidget());
201 /*************************************************************************
203 |* Click on imagebutton : target
205 |************************************************************************/
206 IMPL_LINK_NOARG(SvxHyperlinkDocTp
, ClickTargetHdl_Impl
, weld::Button
&, void)
213 if ( GetPathType ( maStrURL
) == EPathType::ExistsFile
||
214 maStrURL
.isEmpty() ||
215 maStrURL
.equalsIgnoreAsciiCase( INET_FILE_SCHEME
) ||
216 maStrURL
.startsWith( sHash
) )
218 mxMarkWnd
->SetError( LERR_NOERROR
);
220 weld::WaitObject
aWait(mpDialog
->getDialog());
222 if ( maStrURL
.equalsIgnoreAsciiCase( INET_FILE_SCHEME
) )
223 mxMarkWnd
->RefreshTree ( u
""_ustr
);
225 mxMarkWnd
->RefreshTree ( maStrURL
);
228 mxMarkWnd
->SetError( LERR_DOCNOTOPEN
);
231 /*************************************************************************
233 |* Contents of combobox "Path" modified
235 |************************************************************************/
236 IMPL_LINK_NOARG(SvxHyperlinkDocTp
, ModifiedPathHdl_Impl
, weld::ComboBox
&, void)
238 maStrURL
= GetCurrentURL();
240 maTimer
.SetTimeout( 2500 );
243 m_xFtFullURL
->set_label( maStrURL
);
246 /*************************************************************************
248 |* If path-field was modify, to browse the new doc after timeout
250 |************************************************************************/
251 IMPL_LINK_NOARG(SvxHyperlinkDocTp
, TimeoutHdl_Impl
, Timer
*, void)
253 if ( IsMarkWndVisible() && ( GetPathType( maStrURL
)== EPathType::ExistsFile
||
254 maStrURL
.isEmpty() ||
255 maStrURL
.equalsIgnoreAsciiCase( INET_FILE_SCHEME
) ) )
257 weld::WaitObject
aWait(mpDialog
->getDialog());
261 if ( maStrURL
.equalsIgnoreAsciiCase( INET_FILE_SCHEME
) )
262 mxMarkWnd
->RefreshTree ( u
""_ustr
);
264 mxMarkWnd
->RefreshTree ( maStrURL
);
269 /*************************************************************************
271 |* Contents of editfield "Target" modified
273 |************************************************************************/
274 IMPL_LINK_NOARG(SvxHyperlinkDocTp
, ModifiedTargetHdl_Impl
, weld::Entry
&, void)
276 maStrURL
= GetCurrentURL();
278 if (IsMarkWndVisible())
279 mxMarkWnd
->SelectEntry(m_xEdTarget
->get_text());
281 m_xFtFullURL
->set_label( maStrURL
);
284 /*************************************************************************
286 |* editfield "Target" lost focus
288 |************************************************************************/
289 IMPL_LINK_NOARG(SvxHyperlinkDocTp
, LostFocusPathHdl_Impl
, weld::Widget
&, void)
291 maStrURL
= GetCurrentURL();
293 m_xFtFullURL
->set_label( maStrURL
);
296 /*************************************************************************
298 |* Get String from Bookmark-Wnd
300 |************************************************************************/
301 void SvxHyperlinkDocTp::SetMarkStr ( const OUString
& aStrMark
)
303 m_xEdTarget
->set_text(aStrMark
);
305 ModifiedTargetHdl_Impl ( *m_xEdTarget
);
308 /*************************************************************************
310 |* retrieve kind of pathstr
312 |************************************************************************/
313 SvxHyperlinkDocTp::EPathType
SvxHyperlinkDocTp::GetPathType ( std::u16string_view rStrPath
)
315 INetURLObject
aURL( rStrPath
, INetProtocol::File
);
317 if( aURL
.HasError() )
318 return EPathType::Invalid
;
320 return EPathType::ExistsFile
;
323 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */