Update ooo320-m1
[ooovba.git] / svx / source / cui / hldocntp.cxx
blob3bd3bb6c8b79cbd14349875d3996c6480c216444
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: hldocntp.cxx,v $
10 * $Revision: 1.40 $
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
36 #endif
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
76 String aStrURL;
77 String aStrExt;
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;
85 if ( bIsValidURL )
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
90 bool wasAbs;
91 INetURLObject base(rBase);
92 base.setFinalSlash();
93 aURLObject = base.smartRel2Abs(
94 rPath, wasAbs, true, INetURLObject::ENCODE_ALL,
95 RTL_TEXTENCODING_UTF8, true);
97 bIsValidURL = aURLObject.GetProtocol() != INET_PROT_NOT_VALID;
98 if ( bIsValidURL )
100 String aBase( aURLObject.getName( INetURLObject::LAST_SEGMENT, sal_False ) );
101 if ( ( aBase.Len() == 0 ) || ( aBase.GetChar( 0 ) == '.' ) )
102 bIsValidURL = sal_False;
104 if ( bIsValidURL )
106 USHORT nPos = maLbDocTypes.GetSelectEntryPos();
107 if ( nPos != LISTBOX_ENTRY_NOTFOUND )
108 aURLObject.SetExtension( ((DocumentTypeData*)maLbDocTypes.GetEntryData( nPos ))->aStrExt );
112 return bIsValidURL;
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);
136 InitStdControls();
137 FreeResource();
139 SetExchangeSupport ();
141 maCbbPath.SetPosSizePixel ( LogicToPixel( Point( COL_2 , 25 ), MAP_APPFONT ),
142 LogicToPixel( Size ( 176 - COL_DIFF, 60), MAP_APPFONT ) );
143 maCbbPath.Show();
144 maCbbPath.SetBaseURL(SvtPathOptions().GetWorkPath());
145 // maCbbPath.SetHelpId( HID_HYPERDLG_DOC_PATH );
147 // set defaults
148 maRbtEditNow.Check();
150 maBtCreate.SetClickHdl ( LINK ( this, SvxHyperlinkNewDocTp, ClickNewHdl_Impl ) );
152 FillDocumentList ();
155 SvxHyperlinkNewDocTp::~SvxHyperlinkNewDocTp ()
157 for ( USHORT n=0; n<maLbDocTypes.GetEntryCount(); n++ )
159 DocumentTypeData* pTypeData = (DocumentTypeData*)
160 maLbDocTypes.GetEntryData ( n );
161 delete pTypeData;
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 )
185 // Open file
186 Config aCfg( rFile );
187 aCfg.SetGroup( INTERNETSHORTCUT_ID_TAG );
189 // read URL
190 rURL = aCfg.ReadKey( ByteString( RTL_CONSTASCII_STRINGPARAM( INTERNETSHORTCUT_URL_TAG) ), RTL_TEXTENCODING_ASCII_US );
191 SvtPathOptions aPathOpt;
192 rURL = aPathOpt.SubstituteVariable( rURL );
194 // read target
195 if ( pShowAsFolder )
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 ) );
201 // read image-ID
202 String aStrIconId( aCfg.ReadKey( ByteString( RTL_CONSTASCII_STRINGPARAM( INTERNETSHORTCUT_ICONID_TAG ) ), RTL_TEXTENCODING_ASCII_US ) );
203 rIconId = aStrIconId.ToInt32();
205 // read title
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 ()
216 EnterWait();
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" )))
243 continue;
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 );
253 if ( pFilter )
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 );
268 LeaveWait();
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();
283 INetURLObject aURL;
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 /*************************************************************************
305 |* Set initial focus
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 );
324 if ( !bRet )
326 WarningBox aWarning( this, WB_OK, SVX_RESSTR(RID_SVXSTR_HYPDLG_NOVALIDFILENAME) );
327 aWarning.Execute();
329 return bRet;
332 /*************************************************************************
334 |* Any action to do after apply-button is pressed
336 \************************************************************************/
338 void SvxHyperlinkNewDocTp::DoApply ()
340 EnterWait();
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
351 INetURLObject aURL;
352 if ( ImplGetURLObject( aStrNewName, maCbbPath.GetBaseURL(), aURL ) )
355 ///////////////////////////////////////////////////////
356 // create Document
358 aStrNewName = aURL.GetURLPath( INetURLObject::NO_DECODE );
359 SfxViewFrame *pViewFrame = NULL;
362 bool bCreate = true;
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);
371 if( pIStm )
372 delete pIStm;
374 if( bOk )
376 WarningBox aWarning( this, WB_YES_NO, SVX_RESSTR(RID_SVXSTR_HYPERDLG_QUERYOVERWRITE) );
377 bCreate = aWarning.Execute() == BUTTON_YES;
381 if( bCreate )
383 // current document
384 SfxViewFrame* pCurrentDocFrame = SfxViewFrame::Current();
386 if ( aStrNewName != aEmptyStr )
388 // get private-url
389 USHORT nPos = maLbDocTypes.GetSelectEntryPos();
390 if( nPos == LISTBOX_ENTRY_NOTFOUND )
391 nPos=0;
392 String aStrDocName ( ( ( DocumentTypeData* )
393 maLbDocTypes.GetEntryData( nPos ) )->aStrURL );
395 // create items
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);
409 // open url
410 const SfxPoolItem* pReturn = GetDispatcher()->Execute( SID_OPENDOC,
411 SFX_CALLMODE_SYNCHRON,
412 &aName, &aFlags,
413 &aFrame, &aReferer, 0L );
415 // save new doc
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();
420 if (pViewFrame)
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,
427 &aNewName, 0L );
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();
450 LeaveWait();
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 );
465 String aStrURL;
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
474 if( bZeroPath )
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 );
488 String aStrName;
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 )
500 aStrTmp += aStrName;
502 INetURLObject aNewURL( aStrTmp );
504 if( aStrName.Len() > 0 && aNewURL.getExtension().getLength() > 0 &&
505 maLbDocTypes.GetSelectEntryPos() != LISTBOX_ENTRY_NOTFOUND )
507 // get private-url
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 );
516 else
518 aStrTmp = aNewURL.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS );
521 maCbbPath.SetText ( aStrTmp );
523 return( 0L );