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 <cuihyperdlg.hxx>
21 #include <osl/file.hxx>
22 #include <sfx2/filedlghelper.hxx>
23 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
24 
25 #include <hldoctp.hxx>
26 #include <hlmarkwn_def.hxx>
27 
28 sal_Char const sHash[]          = "#";
29 sal_Char const sFileScheme[]    = INET_FILE_SCHEME;
30 
31 /*************************************************************************
32 |*
33 |* Constructor / Destructor
34 |*
35 |************************************************************************/
36 
SvxHyperlinkDocTp(weld::Container * pParent,SvxHpLinkDlg * pDlg,const SfxItemSet * pItemSet)37 SvxHyperlinkDocTp::SvxHyperlinkDocTp(weld::Container* pParent, SvxHpLinkDlg* pDlg, const SfxItemSet* pItemSet)
38     : SvxHyperlinkTabPageBase(pParent, pDlg, "cui/ui/hyperlinkdocpage.ui", "HyperlinkDocPage", pItemSet)
39     , m_xCbbPath(new SvxHyperURLBox(xBuilder->weld_combo_box("path")))
40     , m_xBtFileopen(xBuilder->weld_button("fileopen"))
41     , m_xEdTarget(xBuilder->weld_entry("target"))
42     , m_xFtFullURL(xBuilder->weld_label("url"))
43     , m_xBtBrowse(xBuilder->weld_button("browse"))
44     , m_bMarkWndOpen(false)
45 {
46     m_xCbbPath->SetSmartProtocol(INetProtocol::File);
47 
48     InitStdControls();
49 
50     m_xCbbPath->show();
51     m_xCbbPath->SetBaseURL(INET_FILE_SCHEME);
52 
53     SetExchangeSupport();
54 
55     // set handlers
56     m_xBtFileopen->connect_clicked( LINK ( this, SvxHyperlinkDocTp, ClickFileopenHdl_Impl ) );
57     m_xBtBrowse->connect_clicked( LINK ( this, SvxHyperlinkDocTp, ClickTargetHdl_Impl ) );
58     m_xCbbPath->connect_changed( LINK ( this, SvxHyperlinkDocTp, ModifiedPathHdl_Impl ) );
59     m_xEdTarget->connect_changed( LINK ( this, SvxHyperlinkDocTp, ModifiedTargetHdl_Impl ) );
60 
61     m_xCbbPath->connect_focus_out( LINK ( this, SvxHyperlinkDocTp, LostFocusPathHdl_Impl ) );
62 
63     maTimer.SetInvokeHandler ( LINK ( this, SvxHyperlinkDocTp, TimeoutHdl_Impl ) );
64 }
65 
~SvxHyperlinkDocTp()66 SvxHyperlinkDocTp::~SvxHyperlinkDocTp()
67 {
68 }
69 
70 /*************************************************************************
71 |*
72 |* Fill all dialog-controls except controls in groupbox "more..."
73 |*
74 |************************************************************************/
FillDlgFields(const OUString & rStrURL)75 void SvxHyperlinkDocTp::FillDlgFields(const OUString& rStrURL)
76 {
77     sal_Int32 nPos = rStrURL.indexOf(sHash);
78     // path
79     m_xCbbPath->set_entry_text( rStrURL.copy( 0, ( nPos == -1 ? rStrURL.getLength() : nPos ) ) );
80 
81     // set target in document at editfield
82     OUString aStrMark;
83     if ( nPos != -1 && nPos < rStrURL.getLength()-1 )
84         aStrMark = rStrURL.copy( nPos+1 );
85     m_xEdTarget->set_text( aStrMark );
86 
87     ModifiedPathHdl_Impl(*m_xCbbPath->getWidget());
88 }
89 
90 /*************************************************************************
91 |*
92 |* retrieve current url-string
93 |*
94 |************************************************************************/
GetCurrentURL() const95 OUString SvxHyperlinkDocTp::GetCurrentURL () const
96 {
97     // get data from dialog-controls
98     OUString aStrURL;
99     OUString aStrPath( m_xCbbPath->get_active_text() );
100     OUString aStrMark( m_xEdTarget->get_text() );
101 
102     if ( !aStrPath.isEmpty() )
103     {
104         INetURLObject aURL( aStrPath );
105         if ( aURL.GetProtocol() != INetProtocol::NotValid )    // maybe the path is already a valid
106             aStrURL = aStrPath;                             // hyperlink, then we can use this path directly
107         else
108         {
109             osl::FileBase::getFileURLFromSystemPath( aStrPath, aStrURL );
110             aStrURL = INetURLObject::decode(aStrURL, INetURLObject::DecodeMechanism::ToIUri, RTL_TEXTENCODING_UTF8);
111         }
112 
113         //#105788# always create a URL even if it is not valid
114         if( aStrURL.isEmpty() )
115             aStrURL = aStrPath;
116     }
117 
118     if( !aStrMark.isEmpty() )
119     {
120         aStrURL += sHash + aStrMark;
121     }
122 
123     return aStrURL;
124 }
125 
126 /*************************************************************************
127 |*
128 |* retrieve and prepare data from dialog-fields
129 |*
130 |************************************************************************/
GetCurentItemData(OUString & rStrURL,OUString & aStrName,OUString & aStrIntName,OUString & aStrFrame,SvxLinkInsertMode & eMode)131 void SvxHyperlinkDocTp::GetCurentItemData ( OUString& rStrURL, OUString& aStrName,
132                                             OUString& aStrIntName, OUString& aStrFrame,
133                                             SvxLinkInsertMode& eMode )
134 {
135     // get data from standard-fields
136     rStrURL = GetCurrentURL();
137 
138     if( rStrURL.equalsIgnoreAsciiCase( sFileScheme ) )
139          rStrURL.clear();
140 
141     GetDataFromCommonFields( aStrName, aStrIntName, aStrFrame, eMode );
142 }
143 
144 /*************************************************************************
145 |*
146 |* static method to create Tabpage
147 |*
148 |************************************************************************/
Create(weld::Container * pWindow,SvxHpLinkDlg * pDlg,const SfxItemSet * pItemSet)149 std::unique_ptr<IconChoicePage> SvxHyperlinkDocTp::Create(weld::Container* pWindow, SvxHpLinkDlg* pDlg, const SfxItemSet* pItemSet)
150 {
151     return std::make_unique<SvxHyperlinkDocTp>(pWindow, pDlg, pItemSet);
152 }
153 
154 /*************************************************************************
155 |*
156 |* Set initial focus
157 |*
158 |************************************************************************/
SetInitFocus()159 void SvxHyperlinkDocTp::SetInitFocus()
160 {
161     m_xCbbPath->grab_focus();
162 }
163 
164 /*************************************************************************
165 |*
166 |* Click on imagebutton : fileopen
167 |*
168 |************************************************************************/
IMPL_LINK_NOARG(SvxHyperlinkDocTp,ClickFileopenHdl_Impl,weld::Button &,void)169 IMPL_LINK_NOARG(SvxHyperlinkDocTp, ClickFileopenHdl_Impl, weld::Button&, void)
170 {
171     DisableClose( true );
172     // Open Fileopen-Dialog
173     sfx2::FileDialogHelper aDlg(
174         css::ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE, FileDialogFlags::NONE,
175         mpDialog->getDialog() );
176     OUString aOldURL( GetCurrentURL() );
177     if( aOldURL.startsWithIgnoreAsciiCase( sFileScheme ) )
178     {
179         OUString aPath;
180         osl::FileBase::getSystemPathFromFileURL(aOldURL, aPath);
181         aDlg.SetDisplayFolder( aPath );
182     }
183 
184     ErrCode nError = aDlg.Execute();
185     DisableClose( false );
186 
187     if ( ERRCODE_NONE == nError )
188     {
189         OUString aURL( aDlg.GetPath() );
190         OUString aPath;
191 
192         osl::FileBase::getSystemPathFromFileURL(aURL, aPath);
193 
194         m_xCbbPath->SetBaseURL( aURL );
195         m_xCbbPath->set_entry_text(aPath);
196 
197         if ( aOldURL != GetCurrentURL() )
198             ModifiedPathHdl_Impl(*m_xCbbPath->getWidget());
199     }
200 }
201 
202 /*************************************************************************
203 |*
204 |* Click on imagebutton : target
205 |*
206 |************************************************************************/
IMPL_LINK_NOARG(SvxHyperlinkDocTp,ClickTargetHdl_Impl,weld::Button &,void)207 IMPL_LINK_NOARG(SvxHyperlinkDocTp, ClickTargetHdl_Impl, weld::Button&, void)
208 {
209     ShowMarkWnd();
210 
211     if ( GetPathType ( maStrURL ) == EPathType::ExistsFile  ||
212          maStrURL.isEmpty() ||
213          maStrURL.equalsIgnoreAsciiCase( sFileScheme ) ||
214          maStrURL.startsWith( sHash ) )
215     {
216         mxMarkWnd->SetError( LERR_NOERROR );
217 
218         weld::WaitObject aWait(mpDialog->getDialog());
219 
220         if ( maStrURL.equalsIgnoreAsciiCase( sFileScheme ) )
221             mxMarkWnd->RefreshTree ( "" );
222         else
223             mxMarkWnd->RefreshTree ( maStrURL );
224     }
225     else
226         mxMarkWnd->SetError( LERR_DOCNOTOPEN );
227 }
228 
229 /*************************************************************************
230 |*
231 |* Contents of combobox "Path" modified
232 |*
233 |************************************************************************/
IMPL_LINK_NOARG(SvxHyperlinkDocTp,ModifiedPathHdl_Impl,weld::ComboBox &,void)234 IMPL_LINK_NOARG(SvxHyperlinkDocTp, ModifiedPathHdl_Impl, weld::ComboBox&, void)
235 {
236     maStrURL = GetCurrentURL();
237 
238     maTimer.SetTimeout( 2500 );
239     maTimer.Start();
240 
241     m_xFtFullURL->set_label( maStrURL );
242 }
243 
244 /*************************************************************************
245 |*
246 |* If path-field was modify, to browse the new doc after timeout
247 |*
248 |************************************************************************/
IMPL_LINK_NOARG(SvxHyperlinkDocTp,TimeoutHdl_Impl,Timer *,void)249 IMPL_LINK_NOARG(SvxHyperlinkDocTp, TimeoutHdl_Impl, Timer *, void)
250 {
251     if ( IsMarkWndVisible() && ( GetPathType( maStrURL )== EPathType::ExistsFile ||
252                                   maStrURL.isEmpty() ||
253                                   maStrURL.equalsIgnoreAsciiCase( sFileScheme ) ) )
254     {
255         weld::WaitObject aWait(mpDialog->getDialog());
256 
257         if ( maStrURL.equalsIgnoreAsciiCase( sFileScheme ) )
258             mxMarkWnd->RefreshTree ( "" );
259         else
260             mxMarkWnd->RefreshTree ( maStrURL );
261     }
262 }
263 
264 /*************************************************************************
265 |*
266 |* Contents of editfield "Target" modified
267 |*
268 |************************************************************************/
IMPL_LINK_NOARG(SvxHyperlinkDocTp,ModifiedTargetHdl_Impl,weld::Entry &,void)269 IMPL_LINK_NOARG(SvxHyperlinkDocTp, ModifiedTargetHdl_Impl, weld::Entry&, void)
270 {
271     maStrURL = GetCurrentURL();
272 
273     if (IsMarkWndVisible())
274         mxMarkWnd->SelectEntry(m_xEdTarget->get_text());
275 
276     m_xFtFullURL->set_label( maStrURL );
277 }
278 
279 /*************************************************************************
280 |*
281 |* editfield "Target" lost focus
282 |*
283 |************************************************************************/
IMPL_LINK_NOARG(SvxHyperlinkDocTp,LostFocusPathHdl_Impl,weld::Widget &,void)284 IMPL_LINK_NOARG(SvxHyperlinkDocTp, LostFocusPathHdl_Impl, weld::Widget&, void)
285 {
286     maStrURL = GetCurrentURL();
287 
288     m_xFtFullURL->set_label( maStrURL );
289 }
290 
291 /*************************************************************************
292 |*
293 |* Get String from Bookmark-Wnd
294 |*
295 |************************************************************************/
SetMarkStr(const OUString & aStrMark)296 void SvxHyperlinkDocTp::SetMarkStr ( const OUString& aStrMark )
297 {
298     m_xEdTarget->set_text(aStrMark);
299 
300     ModifiedTargetHdl_Impl ( *m_xEdTarget );
301 }
302 
303 /*************************************************************************
304 |*
305 |* retrieve kind of pathstr
306 |*
307 |************************************************************************/
GetPathType(const OUString & rStrPath)308 SvxHyperlinkDocTp::EPathType SvxHyperlinkDocTp::GetPathType ( const OUString& rStrPath )
309 {
310     INetURLObject aURL( rStrPath, INetProtocol::File );
311 
312     if( aURL.HasError() )
313         return EPathType::Invalid;
314     else
315         return EPathType::ExistsFile;
316 }
317 
318 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
319