LibreOffice Module cui (master)  1
hldocntp.cxx
Go to the documentation of this file.
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 <hldocntp.hxx>
21 #include <osl/file.hxx>
22 #include <sfx2/viewfrm.hxx>
23 #include <sfx2/docfilt.hxx>
24 #include <svl/stritem.hxx>
25 #include <com/sun/star/awt/XTopWindow.hpp>
26 #include <com/sun/star/uno/Reference.h>
27 #include <com/sun/star/uno/Sequence.h>
28 #include <com/sun/star/beans/PropertyValue.hpp>
29 #include <com/sun/star/uno/Exception.hpp>
30 #include <vcl/svapp.hxx>
31 #include <vcl/weld.hxx>
32 #include <tools/urlobj.hxx>
33 #include <unotools/pathoptions.hxx>
36 #include <unotools/ucbhelper.hxx>
37 
39 #include <com/sun/star/ui/dialogs/FolderPicker.hpp>
40 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
41 
42 #include <cuihyperdlg.hxx>
43 #include <dialmgr.hxx>
44 #include <strings.hrc>
45 
46 using namespace ::com::sun::star::lang;
47 using namespace ::com::sun::star::ui::dialogs;
48 using namespace ::com::sun::star::uno;
49 
50 using namespace ::com::sun::star;
51 
52 /*************************************************************************
53 |*
54 |* Data-struct for documenttypes in listbox
55 |*
56 |************************************************************************/
57 
58 namespace {
59 
60 struct DocumentTypeData
61 {
62  OUString aStrURL;
63  OUString aStrExt;
64  DocumentTypeData (const OUString& aURL, const OUString& aExt) : aStrURL(aURL), aStrExt(aExt)
65  {}
66 };
67 
68 }
69 
70 bool SvxHyperlinkNewDocTp::ImplGetURLObject( const OUString& rPath, const OUString& rBase, INetURLObject& aURLObject ) const
71 {
72  bool bIsValidURL = !rPath.isEmpty();
73  if ( bIsValidURL )
74  {
75  aURLObject.SetURL( rPath );
76  if ( aURLObject.GetProtocol() == INetProtocol::NotValid ) // test if the source is already a valid url
77  { // if not we have to create a url from a physical file name
78  bool wasAbs;
79  INetURLObject base(rBase);
80  base.setFinalSlash();
81  aURLObject = base.smartRel2Abs(
82  rPath, wasAbs, true, INetURLObject::EncodeMechanism::All,
83  RTL_TEXTENCODING_UTF8, true);
84  }
85  bIsValidURL = aURLObject.GetProtocol() != INetProtocol::NotValid;
86  if ( bIsValidURL )
87  {
88  OUString aBase( aURLObject.getName( INetURLObject::LAST_SEGMENT, false ) );
89  if ( aBase.isEmpty() || ( aBase[0] == '.' ) )
90  bIsValidURL = false;
91  }
92  if ( bIsValidURL )
93  {
94  sal_Int32 nPos = m_xLbDocTypes->get_selected_index();
95  if (nPos != -1)
96  aURLObject.SetExtension(reinterpret_cast<DocumentTypeData*>(m_xLbDocTypes->get_id(nPos).toInt64())->aStrExt);
97  }
98 
99  }
100  return bIsValidURL;
101 }
102 
103 /*************************************************************************
104 |*
105 |* Constructor / Destructor
106 |*
107 |************************************************************************/
108 
110  : SvxHyperlinkTabPageBase(pParent, pDlg, "cui/ui/hyperlinknewdocpage.ui", "HyperlinkNewDocPage", pItemSet)
111  , m_xRbtEditNow(xBuilder->weld_radio_button("editnow"))
112  , m_xRbtEditLater(xBuilder->weld_radio_button("editlater"))
113  , m_xCbbPath(new SvxHyperURLBox(xBuilder->weld_combo_box("path")))
114  , m_xBtCreate(xBuilder->weld_button("create"))
115  , m_xLbDocTypes(xBuilder->weld_tree_view("types"))
116 {
117  m_xCbbPath->SetSmartProtocol(INetProtocol::File);
118  m_xLbDocTypes->set_size_request(-1, m_xLbDocTypes->get_height_rows(5));
119 
120  InitStdControls();
121 
123 
124  m_xCbbPath->show();
125  m_xCbbPath->SetBaseURL(SvtPathOptions().GetWorkPath());
126 
127  // set defaults
128  m_xRbtEditNow->set_active(true);
129 
130  m_xBtCreate->connect_clicked(LINK(this, SvxHyperlinkNewDocTp, ClickNewHdl_Impl));
131 
132  FillDocumentList ();
133 }
134 
136 {
137  if (m_xLbDocTypes)
138  {
139  for (sal_Int32 n = 0, nEntryCount = m_xLbDocTypes->n_children(); n < nEntryCount; ++n)
140  delete reinterpret_cast<DocumentTypeData*>(m_xLbDocTypes->get_id(n).toInt64());
141  m_xLbDocTypes = nullptr;
142  }
143 }
144 
145 /*************************************************************************
146 |*
147 |* Fill the all dialog-controls except controls in groupbox "more..."
148 |*
149 |************************************************************************/
150 
151 
152 void SvxHyperlinkNewDocTp::FillDlgFields(const OUString& /*rStrURL*/)
153 {
154 }
155 
157 {
158  weld::WaitObject aWaitObj(mpDialog->getDialog());
159 
160  std::vector<SvtDynMenuEntry> aDynamicMenuEntries( SvtDynamicMenuOptions().GetMenu( EDynamicMenuType::NewMenu ) );
161 
162  for ( const SvtDynMenuEntry & rDynamicMenuEntry : aDynamicMenuEntries )
163  {
164  OUString aDocumentUrl = rDynamicMenuEntry.sURL;
165  OUString aTitle = rDynamicMenuEntry.sTitle;
166 
167  //#i96822# business cards, labels and database should not be inserted here
168  if( aDocumentUrl == "private:factory/swriter?slot=21051" ||
169  aDocumentUrl == "private:factory/swriter?slot=21052" ||
170  aDocumentUrl == "private:factory/sdatabase?Interactive" )
171  continue;
172 
173  // Insert into listbox
174  if ( !aDocumentUrl.isEmpty() )
175  {
176  if ( aDocumentUrl == "private:factory/simpress?slot=6686" ) // SJ: #106216# do not start
177  aDocumentUrl = "private:factory/simpress"; // the AutoPilot for impress
178 
179  // insert private-url and default-extension as user-data
180  std::shared_ptr<const SfxFilter> pFilter = SfxFilter::GetDefaultFilterFromFactory( aDocumentUrl );
181  if ( pFilter )
182  {
183  // insert doc-name and image
184  OUString aTitleName = aTitle.replaceFirst( "~", "" );
185 
186  OUString aStrDefExt(pFilter->GetDefaultExtension());
187  DocumentTypeData *pTypeData = new DocumentTypeData(aDocumentUrl, aStrDefExt.copy(2));
188  OUString sId(OUString::number(reinterpret_cast<sal_Int64>(pTypeData)));
189  m_xLbDocTypes->append(sId, aTitleName);
190  }
191  }
192  }
193  m_xLbDocTypes->select(0);
194 }
195 
196 /*************************************************************************
197 |*
198 |* retrieve and prepare data from dialog-fields
199 |*
200 |************************************************************************/
201 
202 void SvxHyperlinkNewDocTp::GetCurentItemData ( OUString& rStrURL, OUString& aStrName,
203  OUString& aStrIntName, OUString& aStrFrame,
204  SvxLinkInsertMode& eMode )
205 {
206  // get data from dialog-controls
207  rStrURL = m_xCbbPath->get_active_text();
209  if ( ImplGetURLObject( rStrURL, m_xCbbPath->GetBaseURL(), aURL ) )
210  {
212  }
213 
214  GetDataFromCommonFields( aStrName, aStrIntName, aStrFrame, eMode );
215 }
216 
217 /*************************************************************************
218 |*
219 |* static method to create Tabpage
220 |*
221 |************************************************************************/
222 
223 std::unique_ptr<IconChoicePage> SvxHyperlinkNewDocTp::Create(weld::Container* pWindow, SvxHpLinkDlg* pDlg, const SfxItemSet* pItemSet)
224 {
225  return std::make_unique<SvxHyperlinkNewDocTp>(pWindow, pDlg, pItemSet);
226 }
227 
228 /*************************************************************************
229 |*
230 |* Set initial focus
231 |*
232 |************************************************************************/
234 {
235  m_xCbbPath->grab_focus();
236 }
237 
238 /*************************************************************************
239 |*
240 |* Ask page whether an insert is possible
241 |*
242 \************************************************************************/
244 {
245  INetURLObject aINetURLObject;
246  bool bRet = ImplGetURLObject(m_xCbbPath->get_active_text(), m_xCbbPath->GetBaseURL(), aINetURLObject);
247  if ( !bRet )
248  {
249  std::unique_ptr<weld::MessageDialog> xWarn(Application::CreateMessageDialog(mpDialog->getDialog(),
250  VclMessageType::Warning, VclButtonsType::Ok,
251  CuiResId(RID_SVXSTR_HYPDLG_NOVALIDFILENAME)));
252  xWarn->run();
253  }
254  return bRet;
255 }
256 
257 namespace
258 {
259  struct ExecuteInfo
260  {
261  bool bRbtEditLater;
262  bool bRbtEditNow;
264  OUString aStrDocName;
265  // current document
266  css::uno::Reference<css::frame::XFrame> xFrame;
267  SfxDispatcher* pDispatcher;
268  };
269 }
270 
271 IMPL_STATIC_LINK(SvxHyperlinkNewDocTp, DispatchDocument, void*, p, void)
272 {
273  std::unique_ptr<ExecuteInfo> xExecuteInfo(static_cast<ExecuteInfo*>(p));
274  if (!xExecuteInfo->xFrame.is())
275  return;
276  try
277  {
278  //if it throws dispatcher is invalid
279  css::uno::Reference<css::awt::XTopWindow>(xExecuteInfo->xFrame->getContainerWindow(), css::uno::UNO_QUERY_THROW);
280 
281  SfxViewFrame *pViewFrame = nullptr;
282 
283  // create items
284  SfxStringItem aName( SID_FILE_NAME, xExecuteInfo->aStrDocName );
285  SfxStringItem aReferer( SID_REFERER, "private:user" );
286  SfxStringItem aFrame( SID_TARGETNAME, "_blank");
287 
288  OUString aStrFlags('S');
289  if (xExecuteInfo->bRbtEditLater)
290  {
291  aStrFlags += "H";
292  }
293  SfxStringItem aFlags (SID_OPTIONS, aStrFlags);
294 
295  // open url
296  const SfxPoolItem* pReturn = xExecuteInfo->pDispatcher->ExecuteList(
297  SID_OPENDOC, SfxCallMode::SYNCHRON,
298  { &aName, &aFlags, &aFrame, &aReferer });
299 
300  // save new doc
301  const SfxViewFrameItem *pItem = dynamic_cast<const SfxViewFrameItem*>( pReturn ); // SJ: pReturn is NULL if the Hyperlink
302  if ( pItem ) // creation is cancelled #106216#
303  {
304  pViewFrame = pItem->GetFrame();
305  if (pViewFrame)
306  {
307  SfxStringItem aNewName( SID_FILE_NAME, xExecuteInfo->aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ) );
308  SfxUnoFrameItem aDocFrame( SID_FILLFRAME, pViewFrame->GetFrame().GetFrameInterface() );
309  fprintf(stderr, "is there a frame int %p\n", pViewFrame->GetFrame().GetFrameInterface().get() );
310  pViewFrame->GetDispatcher()->ExecuteList(
311  SID_SAVEASDOC, SfxCallMode::SYNCHRON,
312  { &aNewName }, { &aDocFrame });
313  }
314  }
315 
316  if (xExecuteInfo->bRbtEditNow)
317  {
318  css::uno::Reference<css::awt::XTopWindow> xWindow(xExecuteInfo->xFrame->getContainerWindow(), css::uno::UNO_QUERY);
319  if (xWindow.is()) //will be false if the frame was exited while the document was loading (e.g. we waited for warning dialogs)
320  xWindow->toFront();
321  }
322 
323  if (pViewFrame && xExecuteInfo->bRbtEditLater)
324  {
325  SfxObjectShell* pObjShell = pViewFrame->GetObjectShell();
326  pObjShell->DoClose();
327  }
328  }
329  catch (...)
330  {
331  }
332 }
333 
334 /*************************************************************************
335 |*
336 |* Any action to do after apply-button is pressed
337 |*
338 \************************************************************************/
340 {
341  weld::WaitObject aWait(mpDialog->getDialog());
342 
343  // get data from dialog-controls
344  OUString aStrNewName = m_xCbbPath->get_active_text();
345 
346  if ( aStrNewName.isEmpty() )
347  aStrNewName = maStrInitURL;
348 
349  // create a real URL-String
351  if ( !ImplGetURLObject( aStrNewName, m_xCbbPath->GetBaseURL(), aURL ) )
352  return;
353 
354  // create Document
355  aStrNewName = aURL.GetURLPath( INetURLObject::DecodeMechanism::NONE );
356  bool bCreate = true;
357  try
358  {
359  // check if file exists, warn before we overwrite it
360  std::unique_ptr<SvStream> pIStm = ::utl::UcbStreamHelper::CreateStream( aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), StreamMode::READ );
361 
362  bool bOk = pIStm && ( pIStm->GetError() == ERRCODE_NONE);
363 
364  pIStm.reset();
365 
366  if( bOk )
367  {
368  std::unique_ptr<weld::MessageDialog> xWarn(Application::CreateMessageDialog(mpDialog->getDialog(),
369  VclMessageType::Warning, VclButtonsType::YesNo,
370  CuiResId(RID_SVXSTR_HYPERDLG_QUERYOVERWRITE)));
371  bCreate = xWarn->run() == RET_YES;
372  }
373  }
374  catch (const uno::Exception&)
375  {
376  }
377 
378  if (!bCreate || aStrNewName.isEmpty())
379  return;
380 
381  ExecuteInfo* pExecuteInfo = new ExecuteInfo;
382 
383  pExecuteInfo->bRbtEditLater = m_xRbtEditLater->get_active();
384  pExecuteInfo->bRbtEditNow = m_xRbtEditNow->get_active();
385  // get private-url
386  sal_Int32 nPos = m_xLbDocTypes->get_selected_index();
387  if (nPos == -1)
388  nPos = 0;
389  pExecuteInfo->aURL = aURL;
390  pExecuteInfo->aStrDocName = reinterpret_cast<DocumentTypeData*>(m_xLbDocTypes->get_id(nPos).toInt64())->aStrURL;
391 
392  // current document
393  pExecuteInfo->xFrame = GetDispatcher()->GetFrame()->GetFrame().GetFrameInterface();
394  pExecuteInfo->pDispatcher = GetDispatcher();
395 
396  Application::PostUserEvent(LINK(nullptr, SvxHyperlinkNewDocTp, DispatchDocument), pExecuteInfo);
397 }
398 
399 /*************************************************************************
400 |*
401 |* Click on imagebutton : new
402 |*
403 |************************************************************************/
405 {
406  DisableClose( true );
407  uno::Reference < XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
408  uno::Reference < XFolderPicker2 > xFolderPicker = FolderPicker::create(xContext);
409 
410  OUString aStrURL;
411  OUString aTempStrURL( m_xCbbPath->get_active_text() );
412  osl::FileBase::getFileURLFromSystemPath( aTempStrURL, aStrURL );
413 
414  OUString aStrPath = aStrURL;
415  bool bZeroPath = aStrPath.isEmpty();
416  bool bHandleFileName = bZeroPath; // when path has length of 0, then the rest should always be handled
417  // as file name, otherwise we do not yet know
418 
419  if( bZeroPath )
420  aStrPath = SvtPathOptions().GetWorkPath();
421  else if( !::utl::UCBContentHelper::IsFolder( aStrURL ) )
422  bHandleFileName = true;
423 
424  xFolderPicker->setDisplayDirectory( aStrPath );
425  sal_Int16 nResult = xFolderPicker->execute();
426  DisableClose( false );
427  if( ExecutableDialogResults::OK != nResult )
428  return;
429 
430  char const sSlash[] = "/";
431 
432  INetURLObject aURL( aStrURL, INetProtocol::File );
433  OUString aStrName;
434  if( bHandleFileName )
435  aStrName = bZeroPath? aTempStrURL : aURL.getName();
436 
437  m_xCbbPath->SetBaseURL( xFolderPicker->getDirectory() );
438  OUString aStrTmp( xFolderPicker->getDirectory() );
439 
440  if( aStrTmp[ aStrTmp.getLength() - 1 ] != sSlash[0] )
441  aStrTmp += sSlash;
442 
443  // append old file name
444  if( bHandleFileName )
445  aStrTmp += aStrName;
446 
447  INetURLObject aNewURL( aStrTmp );
448 
449  if (!aStrName.isEmpty() && !aNewURL.getExtension().isEmpty() &&
450  m_xLbDocTypes->get_selected_index() != -1)
451  {
452  // get private-url
453  const sal_Int32 nPos = m_xLbDocTypes->get_selected_index();
454  aNewURL.setExtension(reinterpret_cast<DocumentTypeData*>(m_xLbDocTypes->get_id(nPos).toInt64())->aStrExt);
455  }
456 
457  if( aNewURL.GetProtocol() == INetProtocol::File )
458  {
459  osl::FileBase::getSystemPathFromFileURL(aNewURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), aStrTmp);
460  }
461  else
462  {
464  }
465 
466  m_xCbbPath->set_entry_text( aStrTmp );
467 }
468 
469 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
URL aURL
void FillDlgFields(const OUString &rStrURL) override
Definition: hldocntp.cxx:152
bool ImplGetURLObject(const OUString &rPath, const OUString &rBase, INetURLObject &aURLObject) const
Definition: hldocntp.cxx:70
virtual void SetInitFocus() override
Definition: hldocntp.cxx:233
OUString GetURLPath(DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
IMPL_STATIC_LINK(SvxHyperlinkNewDocTp, DispatchDocument, void *, p, void)
Definition: hldocntp.cxx:271
Reference< XFrame > xFrame
sal_Int64 n
static ImplSVEvent * PostUserEvent(const Link< void *, void > &rLink, void *pCaller=nullptr, bool bReferenceLink=false)
std::unique_ptr< weld::RadioButton > m_xRbtEditNow
Definition: hldocntp.hxx:31
std::unique_ptr< weld::RadioButton > m_xRbtEditLater
Definition: hldocntp.hxx:32
RET_YES
bool setFinalSlash()
static std::unique_ptr< SvStream > CreateStream(const OUString &rFileName, StreamMode eOpenMode, css::uno::Reference< css::awt::XWindow > xParentWin=nullptr)
virtual bool AskApply() override
Definition: hldocntp.cxx:243
bool setExtension(OUString const &rTheExtension, sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8)
SfxFrame & GetFrame() const
void SetExtension(OUString const &rTheExtension)
void GetDataFromCommonFields(OUString &aStrName, OUString &aStrIntName, OUString &aStrFrame, SvxLinkInsertMode &eMode)
Definition: hltpbase.cxx:430
static std::shared_ptr< const SfxFilter > GetDefaultFilterFromFactory(const OUString &rServiceName)
static std::unique_ptr< IconChoicePage > Create(weld::Container *pWindow, SvxHpLinkDlg *pDlg, const SfxItemSet *pItemSet)
Definition: hldocntp.cxx:223
SfxDispatcher * GetDispatcher() const
Definition: hltpbase.cxx:314
void const * base
OUString getName(sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true, DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
SvxLinkInsertMode
SvxHpLinkDlg * mpDialog
Definition: hltpbase.hxx:63
SvxHyperlinkNewDocTp(weld::Container *pParent, SvxHpLinkDlg *pDlg, const SfxItemSet *pItemSet)
Definition: hldocntp.cxx:109
IMPL_LINK_NOARG(SvxHyperlinkNewDocTp, ClickNewHdl_Impl, weld::Button &, void)
Definition: hldocntp.cxx:404
std::unique_ptr< SvxHyperURLBox > m_xCbbPath
Definition: hldocntp.hxx:33
OUString CuiResId(const char *pKey)
Definition: cuiresmgr.cxx:23
const css::uno::Reference< css::frame::XFrame > & GetFrameInterface() const
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
std::unique_ptr< weld::TreeView > m_xLbDocTypes
Definition: hldocntp.hxx:35
virtual void DoApply() override
Definition: hldocntp.cxx:339
void SetExchangeSupport()
Definition: iconcdlg.hxx:74
const OUString & GetWorkPath() const
SfxViewFrame * GetFrame() const
#define ERRCODE_NONE
OUString aName
INetProtocol GetProtocol() const
SfxViewFrame * GetFrame() const
void * p
Reference< XComponentContext > getProcessComponentContext()
ComboBox-Control for URL's with History and Autocompletion.
Definition: hltpbase.hxx:35
OUString sId
std::unique_ptr< weld::Button > m_xBtCreate
Definition: hldocntp.hxx:34
bool SetURL(OUString const &rTheAbsURIRef, EncodeMechanism eMechanism=EncodeMechanism::WasEncoded, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8)
UNOTOOLS_DLLPUBLIC bool IsFolder(OUString const &url)
void GetCurentItemData(OUString &rStrURL, OUString &aStrName, OUString &aStrIntName, OUString &aStrFrame, SvxLinkInsertMode &eMode) override
Definition: hldocntp.cxx:202
INetURLObject smartRel2Abs(OUString const &rTheRelURIRef, bool &rWasAbsolute, bool bIgnoreFragment=false, EncodeMechanism eMechanism=EncodeMechanism::WasEncoded, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8, bool bRelativeNonURIs=false, FSysStyle eStyle=FSysStyle::Detect) const
virtual ~SvxHyperlinkNewDocTp() override
Definition: hldocntp.cxx:135
Tabpage : Basisclass.
Definition: hltpbase.hxx:46
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, bool bMobile=false)
sal_uInt16 nPos
OUString getExtension(sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true, DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const