1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: hldocntp.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
34 #ifdef SVX_DLLIMPLEMENTATION
35 #undef SVX_DLLIMPLEMENTATION
38 #include "hldocntp.hxx"
39 #include <sfx2/viewfrm.hxx>
40 #include <sfx2/docfac.hxx>
41 #include <com/sun/star/uno/Reference.h>
42 #include <com/sun/star/uno/Sequence.h>
43 #include <com/sun/star/beans/PropertyValue.hpp>
44 #include <com/sun/star/uno/Exception.hpp>
45 #include <unotools/localfilehelper.hxx>
46 #include <tools/config.hxx>
47 #include <vcl/image.hxx>
48 #include <tools/urlobj.hxx>
49 #include <svtools/pathoptions.hxx>
50 #include <svtools/dynamicmenuoptions.hxx>
51 #include <sfx2/filedlghelper.hxx>
52 #include <unotools/ucbstreamhelper.hxx>
53 #include <unotools/ucbhelper.hxx>
55 #include "hyperdlg.hrc"
56 #include <comphelper/processfactory.hxx>
57 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
58 #include <com/sun/star/ui/dialogs/XFolderPicker.hpp>
59 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
61 using namespace ::com::sun::star::lang
;
62 using namespace ::com::sun::star::ui::dialogs
;
63 using namespace ::com::sun::star::uno
;
65 using namespace ::rtl
;
66 using namespace ::com::sun::star
;
68 /*************************************************************************
70 |* Data-struct for documenttypes in listbox
72 |************************************************************************/
74 struct DocumentTypeData
78 DocumentTypeData (String aURL
, String aExt
) : aStrURL(aURL
), aStrExt(aExt
)
82 sal_Bool
SvxHyperlinkNewDocTp::ImplGetURLObject( const String
& rPath
, const String
& rBase
, INetURLObject
& aURLObject
) const
84 sal_Bool bIsValidURL
= rPath
.Len() != 0;
87 aURLObject
.SetURL( rPath
);
88 if ( aURLObject
.GetProtocol() == INET_PROT_NOT_VALID
) // test if the source is already a valid url
89 { // if not we have to create a url from a physical file name
91 INetURLObject
base(rBase
);
93 aURLObject
= base
.smartRel2Abs(
94 rPath
, wasAbs
, true, INetURLObject::ENCODE_ALL
,
95 RTL_TEXTENCODING_UTF8
, true);
97 bIsValidURL
= aURLObject
.GetProtocol() != INET_PROT_NOT_VALID
;
100 String
aBase( aURLObject
.getName( INetURLObject::LAST_SEGMENT
, sal_False
) );
101 if ( ( aBase
.Len() == 0 ) || ( aBase
.GetChar( 0 ) == '.' ) )
102 bIsValidURL
= sal_False
;
106 USHORT nPos
= maLbDocTypes
.GetSelectEntryPos();
107 if ( nPos
!= LISTBOX_ENTRY_NOTFOUND
)
108 aURLObject
.SetExtension( ((DocumentTypeData
*)maLbDocTypes
.GetEntryData( nPos
))->aStrExt
);
115 /*************************************************************************
117 |* Contructor / Destructor
119 |************************************************************************/
121 SvxHyperlinkNewDocTp::SvxHyperlinkNewDocTp ( Window
*pParent
, const SfxItemSet
& rItemSet
)
122 : SvxHyperlinkTabPageBase ( pParent
, SVX_RES( RID_SVXPAGE_HYPERLINK_NEWDOCUMENT
), rItemSet
),
123 maGrpNewDoc ( this, SVX_RES (GRP_NEWDOCUMENT
) ),
124 maRbtEditNow ( this, SVX_RES (RB_EDITNOW
) ),
125 maRbtEditLater ( this, SVX_RES (RB_EDITLATER
) ),
126 maFtPath ( this, SVX_RES (FT_PATH_NEWDOC
) ),
127 maCbbPath ( this, INET_PROT_FILE
),
128 maBtCreate ( this, SVX_RES (BTN_CREATE
) ),
129 maFtDocTypes ( this, SVX_RES (FT_DOCUMENT_TYPES
) ),
130 maLbDocTypes ( this, SVX_RES (LB_DOCUMENT_TYPES
) )
132 // Set HC bitmaps and disable display of bitmap names.
133 maBtCreate
.SetModeImage( Image( SVX_RES( IMG_CREATE_HC
) ), BMP_COLOR_HIGHCONTRAST
);
134 maBtCreate
.EnableTextDisplay (FALSE
);
139 SetExchangeSupport ();
141 maCbbPath
.SetPosSizePixel ( LogicToPixel( Point( COL_2
, 25 ), MAP_APPFONT
),
142 LogicToPixel( Size ( 176 - COL_DIFF
, 60), MAP_APPFONT
) );
144 maCbbPath
.SetBaseURL(SvtPathOptions().GetWorkPath());
145 // maCbbPath.SetHelpId( HID_HYPERDLG_DOC_PATH );
148 maRbtEditNow
.Check();
150 maBtCreate
.SetClickHdl ( LINK ( this, SvxHyperlinkNewDocTp
, ClickNewHdl_Impl
) );
155 SvxHyperlinkNewDocTp::~SvxHyperlinkNewDocTp ()
157 for ( USHORT n
=0; n
<maLbDocTypes
.GetEntryCount(); n
++ )
159 DocumentTypeData
* pTypeData
= (DocumentTypeData
*)
160 maLbDocTypes
.GetEntryData ( n
);
165 /*************************************************************************
167 |* Fill the all dialog-controls except controls in groupbox "more..."
169 |************************************************************************/
172 void SvxHyperlinkNewDocTp::FillDlgFields ( String
& /*aStrURL*/ )
176 #define INTERNETSHORTCUT_ID_TAG "InternetShortcut"
177 #define INTERNETSHORTCUT_TITLE_TAG "Title"
178 #define INTERNETSHORTCUT_TARGET_TAG "Target"
179 #define INTERNETSHORTCUT_FOLDER_TAG "Folder"
180 #define INTERNETSHORTCUT_URL_TAG "URL"
181 #define INTERNETSHORTCUT_ICONID_TAG "IconIndex"
183 void SvxHyperlinkNewDocTp::ReadURLFile( const String
& rFile
, String
& rTitle
, String
& rURL
, sal_Int32
& rIconId
, BOOL
* pShowAsFolder
)
186 Config
aCfg( rFile
);
187 aCfg
.SetGroup( INTERNETSHORTCUT_ID_TAG
);
190 rURL
= aCfg
.ReadKey( ByteString( RTL_CONSTASCII_STRINGPARAM( INTERNETSHORTCUT_URL_TAG
) ), RTL_TEXTENCODING_ASCII_US
);
191 SvtPathOptions aPathOpt
;
192 rURL
= aPathOpt
.SubstituteVariable( rURL
);
197 String
aTemp( aCfg
.ReadKey( ByteString( RTL_CONSTASCII_STRINGPARAM( INTERNETSHORTCUT_TARGET_TAG
) ), RTL_TEXTENCODING_ASCII_US
) );
198 *pShowAsFolder
= aTemp
== String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( INTERNETSHORTCUT_FOLDER_TAG
) );
202 String
aStrIconId( aCfg
.ReadKey( ByteString( RTL_CONSTASCII_STRINGPARAM( INTERNETSHORTCUT_ICONID_TAG
) ), RTL_TEXTENCODING_ASCII_US
) );
203 rIconId
= aStrIconId
.ToInt32();
206 String aLangStr
= aPathOpt
.SubstituteVariable( DEFINE_CONST_UNICODE("$(vlang)") );
207 ByteString
aLang( aLangStr
, RTL_TEXTENCODING_UTF8
);
208 ByteString aGroup
= INTERNETSHORTCUT_ID_TAG
;
209 ( ( aGroup
+= '-' ) += aLang
) += ".W";
210 aCfg
.SetGroup( aGroup
);
211 rTitle
= String( aCfg
.ReadKey( INTERNETSHORTCUT_TITLE_TAG
), RTL_TEXTENCODING_UTF7
);
214 void SvxHyperlinkNewDocTp::FillDocumentList ()
218 uno::Sequence
< uno::Sequence
< beans::PropertyValue
> >
219 aDynamicMenuEntries( SvtDynamicMenuOptions().GetMenu( E_NEWMENU
) );
221 sal_uInt32 i
, nCount
= aDynamicMenuEntries
.getLength();
222 for ( i
= 0; i
< nCount
; i
++ )
224 uno::Sequence
< beans::PropertyValue
>& rDynamicMenuEntry
= aDynamicMenuEntries
[ i
];
226 rtl::OUString aDocumentUrl
, aTitle
, aImageId
, aTargetName
;
228 for ( int e
= 0; e
< rDynamicMenuEntry
.getLength(); e
++ )
230 if ( rDynamicMenuEntry
[ e
].Name
== DYNAMICMENU_PROPERTYNAME_URL
)
231 rDynamicMenuEntry
[ e
].Value
>>= aDocumentUrl
;
232 else if ( rDynamicMenuEntry
[e
].Name
== DYNAMICMENU_PROPERTYNAME_TITLE
)
233 rDynamicMenuEntry
[e
].Value
>>= aTitle
;
234 else if ( rDynamicMenuEntry
[e
].Name
== DYNAMICMENU_PROPERTYNAME_IMAGEIDENTIFIER
)
235 rDynamicMenuEntry
[e
].Value
>>= aImageId
;
236 else if ( rDynamicMenuEntry
[e
].Name
== DYNAMICMENU_PROPERTYNAME_TARGETNAME
)
237 rDynamicMenuEntry
[e
].Value
>>= aTargetName
;
239 //#i96822# business cards, labels and database should not be inserted here
240 if( aDocumentUrl
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "private:factory/swriter?slot=21051" ) ) ||
241 aDocumentUrl
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "private:factory/swriter?slot=21052" )) ||
242 aDocumentUrl
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "private:factory/sdatabase?Interactive" )))
245 // Insert into listbox
246 if ( aDocumentUrl
.getLength() )
248 if ( aDocumentUrl
.equalsAscii( "private:factory/simpress?slot=6686" ) ) // SJ: #106216# do not start
249 aDocumentUrl
= String( RTL_CONSTASCII_USTRINGPARAM( "private:factory/simpress" ) ); // the AutoPilot for impress
251 // insert private-url and default-extension as user-data
252 const SfxFilter
* pFilter
= SfxFilter::GetDefaultFilterFromFactory( aDocumentUrl
);
255 // insert doc-name and image
256 String
aTitleName( aTitle
);
257 aTitleName
.Erase( aTitleName
.Search( (sal_Unicode
)'~' ), 1 );
259 sal_Int16 nPos
= maLbDocTypes
.InsertEntry ( aTitleName
);
260 String
aStrDefExt( pFilter
->GetDefaultExtension () );
261 DocumentTypeData
*pTypeData
= new DocumentTypeData ( aDocumentUrl
, aStrDefExt
.Copy( 2, aStrDefExt
.Len() ) );
262 maLbDocTypes
.SetEntryData ( nPos
, pTypeData
);
266 maLbDocTypes
.SelectEntryPos ( 0 );
271 /*************************************************************************
273 |* retrieve and prepare data from dialog-fields
275 |************************************************************************/
277 void SvxHyperlinkNewDocTp::GetCurentItemData ( String
& aStrURL
, String
& aStrName
,
278 String
& aStrIntName
, String
& aStrFrame
,
279 SvxLinkInsertMode
& eMode
)
281 // get data from dialog-controls
282 aStrURL
= maCbbPath
.GetText();
284 if ( ImplGetURLObject( aStrURL
, maCbbPath
.GetBaseURL(), aURL
) )
286 aStrURL
= aURL
.GetMainURL( INetURLObject::NO_DECODE
);
289 GetDataFromCommonFields( aStrName
, aStrIntName
, aStrFrame
, eMode
);
292 /*************************************************************************
294 |* static method to create Tabpage
296 |************************************************************************/
298 IconChoicePage
* SvxHyperlinkNewDocTp::Create( Window
* pWindow
, const SfxItemSet
& rItemSet
)
300 return( new SvxHyperlinkNewDocTp( pWindow
, rItemSet
) );
303 /*************************************************************************
307 |************************************************************************/
309 void SvxHyperlinkNewDocTp::SetInitFocus()
311 maCbbPath
.GrabFocus();
314 /*************************************************************************
316 |* Ask page whether an insert is possible
318 \************************************************************************/
320 BOOL
SvxHyperlinkNewDocTp::AskApply()
322 INetURLObject aINetURLObject
;
323 sal_Bool bRet
= ImplGetURLObject( maCbbPath
.GetText(), maCbbPath
.GetBaseURL(), aINetURLObject
);
326 WarningBox
aWarning( this, WB_OK
, SVX_RESSTR(RID_SVXSTR_HYPDLG_NOVALIDFILENAME
) );
332 /*************************************************************************
334 |* Any action to do after apply-button is pressed
336 \************************************************************************/
338 void SvxHyperlinkNewDocTp::DoApply ()
342 // get data from dialog-controls
343 String aStrNewName
= maCbbPath
.GetText();
345 if ( aStrNewName
== aEmptyStr
)
346 aStrNewName
= maStrInitURL
;
348 ///////////////////////////////////////////////////////
349 // create a real URL-String
352 if ( ImplGetURLObject( aStrNewName
, maCbbPath
.GetBaseURL(), aURL
) )
355 ///////////////////////////////////////////////////////
358 aStrNewName
= aURL
.GetURLPath( INetURLObject::NO_DECODE
);
359 SfxViewFrame
*pViewFrame
= NULL
;
364 // check if file exists, warn before we overwrite it
366 com::sun::star::uno::Reference
< com::sun::star::task::XInteractionHandler
> xHandler
;
367 SvStream
* pIStm
= ::utl::UcbStreamHelper::CreateStream( aURL
.GetMainURL( INetURLObject::NO_DECODE
), STREAM_READ
, xHandler
);
369 sal_Bool bOk
= pIStm
&& ( pIStm
->GetError() == 0);
376 WarningBox
aWarning( this, WB_YES_NO
, SVX_RESSTR(RID_SVXSTR_HYPERDLG_QUERYOVERWRITE
) );
377 bCreate
= aWarning
.Execute() == BUTTON_YES
;
384 SfxViewFrame
* pCurrentDocFrame
= SfxViewFrame::Current();
386 if ( aStrNewName
!= aEmptyStr
)
389 USHORT nPos
= maLbDocTypes
.GetSelectEntryPos();
390 if( nPos
== LISTBOX_ENTRY_NOTFOUND
)
392 String
aStrDocName ( ( ( DocumentTypeData
* )
393 maLbDocTypes
.GetEntryData( nPos
) )->aStrURL
);
396 SfxStringItem
aName( SID_FILE_NAME
, aStrDocName
);
397 SfxStringItem
aReferer( SID_REFERER
, UniString::CreateFromAscii(
398 RTL_CONSTASCII_STRINGPARAM( "private:user" ) ) );
399 SfxStringItem
aFrame( SID_TARGETNAME
, UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "_blank" ) ) );
400 //SfxBoolItem aFrame( SID_OPEN_NEW_VIEW, TRUE );
402 String
aStrFlags ( sal_Unicode('S') );
403 if ( maRbtEditLater
.IsChecked() )
405 aStrFlags
+= sal_Unicode('H');
407 SfxStringItem
aFlags (SID_OPTIONS
, aStrFlags
);
410 const SfxPoolItem
* pReturn
= GetDispatcher()->Execute( SID_OPENDOC
,
411 SFX_CALLMODE_SYNCHRON
,
413 &aFrame
, &aReferer
, 0L );
416 const SfxViewFrameItem
*pItem
= PTR_CAST( SfxViewFrameItem
, pReturn
); // SJ: pReturn is NULL if the Hyperlink
417 if ( pItem
) // creation is cancelled #106216#
419 pViewFrame
= pItem
->GetFrame();
422 //SfxViewFrame *pViewFrame = pFrame->GetCurrentViewFrame();
423 SfxStringItem
aNewName( SID_FILE_NAME
, aURL
.GetMainURL( INetURLObject::NO_DECODE
) );
425 pViewFrame
->GetDispatcher()->Execute( SID_SAVEASDOC
,
426 SFX_CALLMODE_SYNCHRON
,
433 if ( maRbtEditNow
.IsChecked() && pCurrentDocFrame
)
435 pCurrentDocFrame
->ToTop();
439 catch( uno::Exception
)
443 if ( pViewFrame
&& maRbtEditLater
.IsChecked() )
445 SfxObjectShell
* pObjShell
= pViewFrame
->GetObjectShell();
446 pObjShell
->DoClose();
453 /*************************************************************************
455 |* Click on imagebutton : new
457 |************************************************************************/
459 IMPL_LINK ( SvxHyperlinkNewDocTp
, ClickNewHdl_Impl
, void *, EMPTYARG
)
461 rtl::OUString
aService( RTL_CONSTASCII_USTRINGPARAM( FOLDER_PICKER_SERVICE_NAME
) );
462 uno::Reference
< XMultiServiceFactory
> xFactory( ::comphelper::getProcessServiceFactory() );
463 uno::Reference
< XFolderPicker
> xFolderPicker( xFactory
->createInstance( aService
), UNO_QUERY
);
466 String
aTempStrURL( maCbbPath
.GetText() );
467 utl::LocalFileHelper::ConvertSystemPathToURL( aTempStrURL
, maCbbPath
.GetBaseURL(), aStrURL
);
469 String aStrPath
= aStrURL
;
470 BOOL bZeroPath
= ( aStrPath
.Len() == 0 );
471 BOOL bHandleFileName
= bZeroPath
; // when path has length of 0, then the rest should always be handled
472 // as file name, otherwise we do not yet know
475 aStrPath
= SvtPathOptions().GetWorkPath();
476 else if( !::utl::UCBContentHelper::IsFolder( aStrURL
) )
477 bHandleFileName
= TRUE
;
479 xFolderPicker
->setDisplayDirectory( aStrPath
);
480 DisableClose( sal_True
);
481 sal_Int16 nResult
= xFolderPicker
->execute();
482 DisableClose( sal_False
);
483 if( ExecutableDialogResults::OK
== nResult
)
485 sal_Char
const sSlash
[] = "/";
487 INetURLObject
aURL( aStrURL
, INET_PROT_FILE
);
489 if( bHandleFileName
)
490 aStrName
= bZeroPath
? aTempStrURL
: String(aURL
.getName());
492 maCbbPath
.SetBaseURL( xFolderPicker
->getDirectory() );
493 String
aStrTmp( xFolderPicker
->getDirectory() );
495 if( aStrTmp
.GetChar( aStrTmp
.Len() - 1 ) != sSlash
[0] )
496 aStrTmp
.AppendAscii( sSlash
);
498 // append old file name
499 if( bHandleFileName
)
502 INetURLObject
aNewURL( aStrTmp
);
504 if( aStrName
.Len() > 0 && aNewURL
.getExtension().getLength() > 0 &&
505 maLbDocTypes
.GetSelectEntryPos() != LISTBOX_ENTRY_NOTFOUND
)
508 USHORT nPos
= maLbDocTypes
.GetSelectEntryPos();
509 aNewURL
.setExtension( ( ( DocumentTypeData
* ) maLbDocTypes
.GetEntryData( nPos
) )->aStrExt
);
512 if( aNewURL
.GetProtocol() == INET_PROT_FILE
)
514 utl::LocalFileHelper::ConvertURLToSystemPath( aNewURL
.GetMainURL( INetURLObject::NO_DECODE
), aStrTmp
);
518 aStrTmp
= aNewURL
.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS
);
521 maCbbPath
.SetText ( aStrTmp
);