Line data Source code
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 <sfx2/viewfrm.hxx>
22 : #include <sfx2/docfac.hxx>
23 : #include <com/sun/star/uno/Reference.h>
24 : #include <com/sun/star/uno/Sequence.h>
25 : #include <com/sun/star/beans/PropertyValue.hpp>
26 : #include <com/sun/star/uno/Exception.hpp>
27 : #include <unotools/localfilehelper.hxx>
28 : #include <vcl/image.hxx>
29 : #include <tools/urlobj.hxx>
30 : #include <unotools/pathoptions.hxx>
31 : #include <unotools/dynamicmenuoptions.hxx>
32 : #include <sfx2/filedlghelper.hxx>
33 : #include <unotools/ucbstreamhelper.hxx>
34 : #include <unotools/ucbhelper.hxx>
35 :
36 : #include "hyperdlg.hrc"
37 : #include <comphelper/processfactory.hxx>
38 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
39 : #include <com/sun/star/ui/dialogs/FolderPicker.hpp>
40 : #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
41 :
42 : using namespace ::com::sun::star::lang;
43 : using namespace ::com::sun::star::ui::dialogs;
44 : using namespace ::com::sun::star::uno;
45 :
46 : using namespace ::rtl;
47 : using namespace ::com::sun::star;
48 :
49 : /*************************************************************************
50 : |*
51 : |* Data-struct for documenttypes in listbox
52 : |*
53 : |************************************************************************/
54 :
55 0 : struct DocumentTypeData
56 : {
57 : String aStrURL;
58 : String aStrExt;
59 0 : DocumentTypeData (String aURL, String aExt) : aStrURL(aURL), aStrExt(aExt)
60 0 : {}
61 : };
62 :
63 0 : sal_Bool SvxHyperlinkNewDocTp::ImplGetURLObject( const String& rPath, const String& rBase, INetURLObject& aURLObject ) const
64 : {
65 0 : sal_Bool bIsValidURL = rPath.Len() != 0;
66 0 : if ( bIsValidURL )
67 : {
68 0 : aURLObject.SetURL( rPath );
69 0 : if ( aURLObject.GetProtocol() == INET_PROT_NOT_VALID ) // test if the source is already a valid url
70 : { // if not we have to create a url from a physical file name
71 : bool wasAbs;
72 0 : INetURLObject base(rBase);
73 0 : base.setFinalSlash();
74 : aURLObject = base.smartRel2Abs(
75 : rPath, wasAbs, true, INetURLObject::ENCODE_ALL,
76 0 : RTL_TEXTENCODING_UTF8, true);
77 : }
78 0 : bIsValidURL = aURLObject.GetProtocol() != INET_PROT_NOT_VALID;
79 0 : if ( bIsValidURL )
80 : {
81 0 : String aBase( aURLObject.getName( INetURLObject::LAST_SEGMENT, sal_False ) );
82 0 : if ( ( aBase.Len() == 0 ) || ( aBase.GetChar( 0 ) == '.' ) )
83 0 : bIsValidURL = sal_False;
84 : }
85 0 : if ( bIsValidURL )
86 : {
87 0 : sal_uInt16 nPos = maLbDocTypes.GetSelectEntryPos();
88 0 : if ( nPos != LISTBOX_ENTRY_NOTFOUND )
89 0 : aURLObject.SetExtension( ((DocumentTypeData*)maLbDocTypes.GetEntryData( nPos ))->aStrExt );
90 : }
91 :
92 : }
93 0 : return bIsValidURL;
94 : }
95 :
96 : /*************************************************************************
97 : |*
98 : |* Contructor / Destructor
99 : |*
100 : |************************************************************************/
101 :
102 0 : SvxHyperlinkNewDocTp::SvxHyperlinkNewDocTp ( Window *pParent, const SfxItemSet& rItemSet)
103 0 : : SvxHyperlinkTabPageBase ( pParent, CUI_RES( RID_SVXPAGE_HYPERLINK_NEWDOCUMENT ), rItemSet ),
104 0 : maGrpNewDoc ( this, CUI_RES (GRP_NEWDOCUMENT) ),
105 0 : maRbtEditNow ( this, CUI_RES (RB_EDITNOW) ),
106 0 : maRbtEditLater ( this, CUI_RES (RB_EDITLATER) ),
107 0 : maFtPath ( this, CUI_RES (FT_PATH_NEWDOC) ),
108 : maCbbPath ( this, INET_PROT_FILE ),
109 0 : maBtCreate ( this, CUI_RES (BTN_CREATE) ),
110 0 : maFtDocTypes ( this, CUI_RES (FT_DOCUMENT_TYPES) ),
111 0 : maLbDocTypes ( this, CUI_RES (LB_DOCUMENT_TYPES) )
112 : {
113 : // Set HC bitmaps and disable display of bitmap names.
114 0 : maBtCreate.EnableTextDisplay (sal_False);
115 :
116 0 : InitStdControls();
117 0 : FreeResource();
118 :
119 0 : SetExchangeSupport ();
120 :
121 0 : maCbbPath.SetPosSizePixel ( LogicToPixel( Point( COL_2 , 25 ), MAP_APPFONT ),
122 0 : LogicToPixel( Size ( 176 - COL_DIFF, 60), MAP_APPFONT ) );
123 0 : maCbbPath.Show();
124 0 : maCbbPath.SetBaseURL(SvtPathOptions().GetWorkPath());
125 :
126 : // set defaults
127 0 : maRbtEditNow.Check();
128 :
129 0 : maBtCreate.SetClickHdl ( LINK ( this, SvxHyperlinkNewDocTp, ClickNewHdl_Impl ) );
130 :
131 0 : maBtCreate.SetAccessibleRelationMemberOf( &maGrpNewDoc );
132 0 : maBtCreate.SetAccessibleRelationLabeledBy( &maFtPath );
133 :
134 0 : FillDocumentList ();
135 0 : }
136 :
137 0 : SvxHyperlinkNewDocTp::~SvxHyperlinkNewDocTp ()
138 : {
139 0 : for ( sal_uInt16 n=0; n<maLbDocTypes.GetEntryCount(); n++ )
140 : {
141 : DocumentTypeData* pTypeData = (DocumentTypeData*)
142 0 : maLbDocTypes.GetEntryData ( n );
143 0 : delete pTypeData;
144 : }
145 0 : }
146 :
147 : /*************************************************************************
148 : |*
149 : |* Fill the all dialog-controls except controls in groupbox "more..."
150 : |*
151 : |************************************************************************/
152 :
153 :
154 0 : void SvxHyperlinkNewDocTp::FillDlgFields ( String& /*aStrURL*/ )
155 : {
156 0 : }
157 :
158 0 : void SvxHyperlinkNewDocTp::FillDocumentList ()
159 : {
160 0 : EnterWait();
161 :
162 : uno::Sequence< uno::Sequence< beans::PropertyValue > >
163 0 : aDynamicMenuEntries( SvtDynamicMenuOptions().GetMenu( E_NEWMENU ) );
164 :
165 0 : sal_uInt32 i, nCount = aDynamicMenuEntries.getLength();
166 0 : for ( i = 0; i < nCount; i++ )
167 : {
168 0 : uno::Sequence< beans::PropertyValue >& rDynamicMenuEntry = aDynamicMenuEntries[ i ];
169 :
170 0 : rtl::OUString aDocumentUrl, aTitle, aImageId, aTargetName;
171 :
172 0 : for ( int e = 0; e < rDynamicMenuEntry.getLength(); e++ )
173 : {
174 0 : if ( rDynamicMenuEntry[ e ].Name == DYNAMICMENU_PROPERTYNAME_URL )
175 0 : rDynamicMenuEntry[ e ].Value >>= aDocumentUrl;
176 0 : else if ( rDynamicMenuEntry[e].Name == DYNAMICMENU_PROPERTYNAME_TITLE )
177 0 : rDynamicMenuEntry[e].Value >>= aTitle;
178 0 : else if ( rDynamicMenuEntry[e].Name == DYNAMICMENU_PROPERTYNAME_IMAGEIDENTIFIER )
179 0 : rDynamicMenuEntry[e].Value >>= aImageId;
180 0 : else if ( rDynamicMenuEntry[e].Name == DYNAMICMENU_PROPERTYNAME_TARGETNAME )
181 0 : rDynamicMenuEntry[e].Value >>= aTargetName;
182 : }
183 : //#i96822# business cards, labels and database should not be inserted here
184 0 : if( aDocumentUrl == "private:factory/swriter?slot=21051" ||
185 0 : aDocumentUrl == "private:factory/swriter?slot=21052" ||
186 0 : aDocumentUrl == "private:factory/sdatabase?Interactive" )
187 0 : continue;
188 :
189 : // Insert into listbox
190 0 : if ( !aDocumentUrl.isEmpty() )
191 : {
192 0 : if ( aDocumentUrl == "private:factory/simpress?slot=6686" ) // SJ: #106216# do not start
193 0 : aDocumentUrl = String( RTL_CONSTASCII_USTRINGPARAM( "private:factory/simpress" ) ); // the AutoPilot for impress
194 :
195 : // insert private-url and default-extension as user-data
196 0 : const SfxFilter* pFilter = SfxFilter::GetDefaultFilterFromFactory( aDocumentUrl );
197 0 : if ( pFilter )
198 : {
199 : // insert doc-name and image
200 0 : String aTitleName( aTitle );
201 0 : aTitleName.Erase( aTitleName.Search( (sal_Unicode)'~' ), 1 );
202 :
203 0 : sal_Int16 nPos = maLbDocTypes.InsertEntry ( aTitleName );
204 0 : String aStrDefExt( pFilter->GetDefaultExtension () );
205 0 : DocumentTypeData *pTypeData = new DocumentTypeData ( aDocumentUrl, aStrDefExt.Copy( 2, aStrDefExt.Len() ) );
206 0 : maLbDocTypes.SetEntryData ( nPos, pTypeData );
207 : }
208 : }
209 0 : }
210 0 : maLbDocTypes.SelectEntryPos ( 0 );
211 :
212 0 : LeaveWait();
213 0 : }
214 :
215 : /*************************************************************************
216 : |*
217 : |* retrieve and prepare data from dialog-fields
218 : |*
219 : |************************************************************************/
220 :
221 0 : void SvxHyperlinkNewDocTp::GetCurentItemData ( String& aStrURL, String& aStrName,
222 : String& aStrIntName, String& aStrFrame,
223 : SvxLinkInsertMode& eMode )
224 : {
225 : // get data from dialog-controls
226 0 : aStrURL = maCbbPath.GetText();
227 0 : INetURLObject aURL;
228 0 : if ( ImplGetURLObject( aStrURL, maCbbPath.GetBaseURL(), aURL ) )
229 : {
230 0 : aStrURL = aURL.GetMainURL( INetURLObject::NO_DECODE );
231 : }
232 :
233 0 : GetDataFromCommonFields( aStrName, aStrIntName, aStrFrame, eMode );
234 0 : }
235 :
236 : /*************************************************************************
237 : |*
238 : |* static method to create Tabpage
239 : |*
240 : |************************************************************************/
241 :
242 0 : IconChoicePage* SvxHyperlinkNewDocTp::Create( Window* pWindow, const SfxItemSet& rItemSet )
243 : {
244 0 : return( new SvxHyperlinkNewDocTp( pWindow, rItemSet ) );
245 : }
246 :
247 : /*************************************************************************
248 : |*
249 : |* Set initial focus
250 : |*
251 : |************************************************************************/
252 :
253 0 : void SvxHyperlinkNewDocTp::SetInitFocus()
254 : {
255 0 : maCbbPath.GrabFocus();
256 0 : }
257 :
258 : /*************************************************************************
259 : |*
260 : |* Ask page whether an insert is possible
261 : |*
262 : \************************************************************************/
263 :
264 0 : sal_Bool SvxHyperlinkNewDocTp::AskApply()
265 : {
266 0 : INetURLObject aINetURLObject;
267 0 : sal_Bool bRet = ImplGetURLObject( maCbbPath.GetText(), maCbbPath.GetBaseURL(), aINetURLObject );
268 0 : if ( !bRet )
269 : {
270 0 : WarningBox aWarning( this, WB_OK, CUI_RESSTR(RID_SVXSTR_HYPDLG_NOVALIDFILENAME) );
271 0 : aWarning.Execute();
272 : }
273 0 : return bRet;
274 : }
275 :
276 : /*************************************************************************
277 : |*
278 : |* Any action to do after apply-button is pressed
279 : |*
280 : \************************************************************************/
281 :
282 0 : void SvxHyperlinkNewDocTp::DoApply ()
283 : {
284 0 : EnterWait();
285 :
286 : // get data from dialog-controls
287 0 : String aStrNewName = maCbbPath.GetText();
288 :
289 0 : if ( aStrNewName == aEmptyStr )
290 0 : aStrNewName = maStrInitURL;
291 :
292 : ///////////////////////////////////////////////////////
293 : // create a real URL-String
294 :
295 0 : INetURLObject aURL;
296 0 : if ( ImplGetURLObject( aStrNewName, maCbbPath.GetBaseURL(), aURL ) )
297 : {
298 :
299 : ///////////////////////////////////////////////////////
300 : // create Document
301 :
302 0 : aStrNewName = aURL.GetURLPath( INetURLObject::NO_DECODE );
303 0 : SfxViewFrame *pViewFrame = NULL;
304 : try
305 : {
306 0 : bool bCreate = true;
307 :
308 : // check if file exists, warn before we overwrite it
309 : {
310 0 : com::sun::star::uno::Reference < com::sun::star::task::XInteractionHandler > xHandler;
311 0 : SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( aURL.GetMainURL( INetURLObject::NO_DECODE ), STREAM_READ, xHandler );
312 :
313 0 : sal_Bool bOk = pIStm && ( pIStm->GetError() == 0);
314 :
315 0 : if( pIStm )
316 0 : delete pIStm;
317 :
318 0 : if( bOk )
319 : {
320 0 : WarningBox aWarning( this, WB_YES_NO, CUI_RESSTR(RID_SVXSTR_HYPERDLG_QUERYOVERWRITE) );
321 0 : bCreate = aWarning.Execute() == BUTTON_YES;
322 0 : }
323 : }
324 :
325 0 : if( bCreate )
326 : {
327 : // current document
328 0 : SfxViewFrame* pCurrentDocFrame = SfxViewFrame::Current();
329 :
330 0 : if ( aStrNewName != aEmptyStr )
331 : {
332 : // get private-url
333 0 : sal_uInt16 nPos = maLbDocTypes.GetSelectEntryPos();
334 0 : if( nPos == LISTBOX_ENTRY_NOTFOUND )
335 0 : nPos=0;
336 : String aStrDocName ( ( ( DocumentTypeData* )
337 0 : maLbDocTypes.GetEntryData( nPos ) )->aStrURL );
338 :
339 : // create items
340 0 : SfxStringItem aName( SID_FILE_NAME, aStrDocName );
341 0 : SfxStringItem aReferer( SID_REFERER, rtl::OUString("private:user") );
342 0 : SfxStringItem aFrame( SID_TARGETNAME, rtl::OUString("_blank") );
343 :
344 0 : rtl::OUString aStrFlags('S');
345 0 : if ( maRbtEditLater.IsChecked() )
346 : {
347 0 : aStrFlags += rtl::OUString('H');
348 : }
349 0 : SfxStringItem aFlags (SID_OPTIONS, aStrFlags);
350 :
351 : // open url
352 : const SfxPoolItem* pReturn = GetDispatcher()->Execute( SID_OPENDOC,
353 : SFX_CALLMODE_SYNCHRON,
354 : &aName, &aFlags,
355 0 : &aFrame, &aReferer, 0L );
356 :
357 : // save new doc
358 0 : const SfxViewFrameItem *pItem = PTR_CAST( SfxViewFrameItem, pReturn ); // SJ: pReturn is NULL if the Hyperlink
359 0 : if ( pItem ) // creation is cancelled #106216#
360 : {
361 0 : pViewFrame = pItem->GetFrame();
362 0 : if (pViewFrame)
363 : {
364 0 : SfxStringItem aNewName( SID_FILE_NAME, aURL.GetMainURL( INetURLObject::NO_DECODE ) );
365 :
366 : pViewFrame->GetDispatcher()->Execute( SID_SAVEASDOC,
367 : SFX_CALLMODE_SYNCHRON,
368 0 : &aNewName, 0L );
369 :
370 : }
371 0 : }
372 : }
373 :
374 0 : if ( maRbtEditNow.IsChecked() && pCurrentDocFrame )
375 : {
376 0 : pCurrentDocFrame->ToTop();
377 : }
378 : }
379 : }
380 0 : catch (const uno::Exception&)
381 : {
382 : }
383 :
384 0 : if ( pViewFrame && maRbtEditLater.IsChecked() )
385 : {
386 0 : SfxObjectShell* pObjShell = pViewFrame->GetObjectShell();
387 0 : pObjShell->DoClose();
388 : }
389 : }
390 :
391 0 : LeaveWait();
392 0 : }
393 :
394 : /*************************************************************************
395 : |*
396 : |* Click on imagebutton : new
397 : |*
398 : |************************************************************************/
399 :
400 0 : IMPL_LINK_NOARG(SvxHyperlinkNewDocTp, ClickNewHdl_Impl)
401 : {
402 0 : uno::Reference < XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
403 0 : uno::Reference < XFolderPicker2 > xFolderPicker = FolderPicker::create(xContext);
404 :
405 0 : String aStrURL;
406 0 : String aTempStrURL( maCbbPath.GetText() );
407 0 : utl::LocalFileHelper::ConvertSystemPathToURL( aTempStrURL, maCbbPath.GetBaseURL(), aStrURL );
408 :
409 0 : String aStrPath = aStrURL;
410 0 : sal_Bool bZeroPath = ( aStrPath.Len() == 0 );
411 0 : sal_Bool bHandleFileName = bZeroPath; // when path has length of 0, then the rest should always be handled
412 : // as file name, otherwise we do not yet know
413 :
414 0 : if( bZeroPath )
415 0 : aStrPath = SvtPathOptions().GetWorkPath();
416 0 : else if( !::utl::UCBContentHelper::IsFolder( aStrURL ) )
417 0 : bHandleFileName = sal_True;
418 :
419 0 : xFolderPicker->setDisplayDirectory( aStrPath );
420 0 : DisableClose( sal_True );
421 0 : sal_Int16 nResult = xFolderPicker->execute();
422 0 : DisableClose( sal_False );
423 0 : if( ExecutableDialogResults::OK == nResult )
424 : {
425 0 : sal_Char const sSlash[] = "/";
426 :
427 0 : INetURLObject aURL( aStrURL, INET_PROT_FILE );
428 0 : String aStrName;
429 0 : if( bHandleFileName )
430 0 : aStrName = bZeroPath? aTempStrURL : String(aURL.getName());
431 :
432 0 : maCbbPath.SetBaseURL( xFolderPicker->getDirectory() );
433 0 : String aStrTmp( xFolderPicker->getDirectory() );
434 :
435 0 : if( aStrTmp.GetChar( aStrTmp.Len() - 1 ) != sSlash[0] )
436 0 : aStrTmp.AppendAscii( sSlash );
437 :
438 : // append old file name
439 0 : if( bHandleFileName )
440 0 : aStrTmp += aStrName;
441 :
442 0 : INetURLObject aNewURL( aStrTmp );
443 :
444 0 : if( aStrName.Len() > 0 && !aNewURL.getExtension().isEmpty() &&
445 0 : maLbDocTypes.GetSelectEntryPos() != LISTBOX_ENTRY_NOTFOUND )
446 : {
447 : // get private-url
448 0 : sal_uInt16 nPos = maLbDocTypes.GetSelectEntryPos();
449 0 : aNewURL.setExtension( ( ( DocumentTypeData* ) maLbDocTypes.GetEntryData( nPos ) )->aStrExt );
450 : }
451 :
452 0 : if( aNewURL.GetProtocol() == INET_PROT_FILE )
453 : {
454 0 : utl::LocalFileHelper::ConvertURLToSystemPath( aNewURL.GetMainURL( INetURLObject::NO_DECODE ), aStrTmp );
455 : }
456 : else
457 : {
458 0 : aStrTmp = aNewURL.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS );
459 : }
460 :
461 0 : maCbbPath.SetText ( aStrTmp );
462 : }
463 0 : return( 0L );
464 3 : }
465 :
466 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|