1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_ucbhelper.hxx"
26 #include <cppuhelper/weak.hxx>
27 #include <cppuhelper/bootstrap.hxx>
28 #include <comphelper/processfactory.hxx>
29 #include <com/sun/star/beans/PropertyAttribute.hpp>
30 #include <com/sun/star/beans/XPropertySetInfo.hpp>
31 #include <com/sun/star/io/XActiveDataSink.hpp>
32 #include <com/sun/star/lang/XComponent.hpp>
33 #include <com/sun/star/sdbc/XResultSet.hpp>
34 #include <com/sun/star/ucb/XCommandInfo.hpp>
35 #include <com/sun/star/ucb/XContentAccess.hpp>
36 #include <com/sun/star/ucb/CommandAbortedException.hpp>
37 #include <com/sun/star/ucb/ContentInfo.hpp>
38 #include <com/sun/star/ucb/ContentInfoAttribute.hpp>
39 
40 #include "rtl/ref.hxx"
41 #include <ucbhelper/content.hxx>
42 #include <ucbhelper/contentbroker.hxx>
43 #include <ucbhelper/commandenvironment.hxx>
44 #include <svtools/svtreebx.hxx>
45 #include <vcl/wrkwin.hxx>
46 #include <vcl/svapp.hxx>
47 #include <vcl/menu.hxx>
48 #include <vcl/dialog.hxx>
49 #include <vcl/fixed.hxx>
50 #include <vcl/group.hxx>
51 #include <vcl/button.hxx>
52 #include <vcl/msgbox.hxx>
53 #include <ucbhelper/macros.hxx>
54 #include <ucbhelper/configurationkeys.hxx>
55 
56 #ifndef _UCBEXPLORER_HRC
57 #include "ucbexplorer.hrc"
58 #endif
59 
60 using namespace com::sun::star;
61 using namespace com::sun::star::beans;
62 using namespace com::sun::star::io;
63 using namespace com::sun::star::lang;
64 using namespace com::sun::star::sdbc;
65 using namespace com::sun::star::task;
66 using namespace com::sun::star::ucb;
67 using namespace com::sun::star::uno;
68 using namespace rtl;
69 
70 //=========================================================================
71 //
72 // class TestDataSink.
73 //
74 //=========================================================================
75 
76 class TestDataSink : public cppu::OWeakObject, public XActiveDataSink
77 {
78     uno::Reference< XInputStream > m_xStream;
79 
80 public:
81 //    TestDataSink() {}
82 //    virtual ~TestDataSink();
83 
84     // XInterface methods
85     virtual Any SAL_CALL queryInterface( const Type & rType )
86         throw( RuntimeException );
87     virtual void SAL_CALL acquire()
88         throw ();
89     virtual void SAL_CALL release()
90         throw ();
91 
92     // XActiveDataSink methods.
93     virtual void SAL_CALL setInputStream(
94                                 const uno::Reference< XInputStream >& aStream )
95         throw( RuntimeException );
96     virtual uno::Reference< XInputStream > SAL_CALL getInputStream()
97         throw( RuntimeException );
98 };
99 
100 //=========================================================================
101 //
102 // class StringInputDialog.
103 //
104 //=========================================================================
105 
106 class StringInputDialog : public ModalDialog
107 {
108 private:
109     FixedText    m_aNameText;
110     Edit         m_aNameEdit;
111     GroupBox     m_aNameGroup;
112     OKButton     m_aOKBtn;
113     CancelButton m_aCancelBtn;
114     HelpButton   m_aHelpBtn;
115 
116     DECL_LINK( OKHdl, Button * );
117     DECL_LINK( NameHdl, Edit * );
118 
119 public:
120     StringInputDialog( ResMgr& rResMgr,
121                        const String& rTitle,
122                        const String& rDefaultText,
123                        String* pGroupName = 0 );
GetValue() const124     String GetValue() const { return m_aNameEdit.GetText(); }
SetValue(const String & rNewName)125     void SetValue( const String& rNewName ) { m_aNameEdit.SetText( rNewName ); }
126 };
127 
128 //=========================================================================
129 //
130 // class UcbExplorerListBoxEntry.
131 //
132 //=========================================================================
133 
134 class UcbExplorerListBoxEntry : public SvLBoxEntry
135 {
136     friend class UcbExplorerTreeListBox;
137 
138     enum EntryType { FOLDER, DOCUMENT, LINK };
139 
140     ::ucbhelper::Content m_aContent;
141     EntryType            m_eType;
142 
143 public:
144     UcbExplorerListBoxEntry();
145     virtual ~UcbExplorerListBoxEntry();
146 
147     BOOL createNewContent( const ContentInfo& rInfo,
148                            ::ucbhelper::Content& rNewContent );
149 };
150 
151 //=========================================================================
152 //
153 // class UcbExplorerTreeListBox.
154 //
155 //=========================================================================
156 
157 class UcbExplorerTreeListBox : public SvTreeListBox
158 {
159     Bitmap  m_aFolderClosed;
160     Bitmap  m_aFolderOpened;
161     Bitmap  m_aDocument;
162     Bitmap  m_aLink;
163 
164 private:
165     virtual SvLBoxEntry* CreateEntry() const;
166     virtual void         RequestingChilds( SvLBoxEntry* pParent );
167 
168 public:
169     UcbExplorerTreeListBox( ResMgr & rResMgr, Window* pParent, WinBits nWinStyle = 0 );
170     virtual ~UcbExplorerTreeListBox();
171 
172     virtual void Command( const CommandEvent& rCEvt );
173 
174     UcbExplorerListBoxEntry*
175     InsertEntry( ::ucbhelper::Content& rContent, SvLBoxEntry* pParent );
176     UcbExplorerListBoxEntry*
177     InsertEntry( const String& rURL, SvLBoxEntry* pParent = 0 );
178 };
179 
180 //=========================================================================
181 //
182 // class UcbExplorerWindow.
183 //
184 //=========================================================================
185 
186 class UcbExplorerWindow : public WorkWindow
187 {
188     friend class MyApp;
189 
190     UcbExplorerTreeListBox m_aTree;
191 
192 public:
193     UcbExplorerWindow( ResMgr & rResMgr, Window *pParent, WinBits nWinStyle );
194     virtual ~UcbExplorerWindow();
195 
196     virtual void Resize();
197 };
198 
199 //=========================================================================
200 //
201 // class MyApp.
202 //
203 //=========================================================================
204 
205 class MyApp : public Application
206 {
207 public:
208     virtual void Main();
209 };
210 
211 //=========================================================================
212 //
213 // TestDataSink implementation.
214 //
215 //=========================================================================
216 
217 // virtual
queryInterface(const Type & rType)218 Any SAL_CALL TestDataSink::queryInterface( const Type & rType )
219     throw( RuntimeException )
220 {
221     Any aRet = cppu::queryInterface(
222                         rType,
223                             static_cast< XActiveDataSink * >( this ) );
224     return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
225 }
226 
227 //=========================================================================
228 // virtual
acquire()229 void SAL_CALL TestDataSink::acquire()
230     throw()
231 {
232     OWeakObject::acquire();
233 }
234 
235 //=========================================================================
236 // virtual
release()237 void SAL_CALL TestDataSink::release()
238     throw()
239 {
240     OWeakObject::release();
241 }
242 
243 //=========================================================================
244 // virtual
setInputStream(const uno::Reference<XInputStream> & aStream)245 void SAL_CALL TestDataSink::setInputStream(
246                                 const uno::Reference< XInputStream >& aStream )
247     throw( RuntimeException )
248 {
249     m_xStream = aStream;
250 }
251 
252 //=========================================================================
253 // virtual
getInputStream()254 uno::Reference< XInputStream > SAL_CALL TestDataSink::getInputStream()
255     throw( RuntimeException )
256 {
257     return m_xStream;
258 }
259 
260 //=========================================================================
261 //
262 // StringInputDialog implementation.
263 //
264 //=========================================================================
265 
StringInputDialog(ResMgr & rResMgr,const String & rTitle,const String & rDefaultText,String * pGroupName)266 StringInputDialog::StringInputDialog( ResMgr& rResMgr,
267                                       const String& rTitle,
268                                       const String& rDefaultText,
269                                       String* pGroupName )
270   : ModalDialog( 0, ResId( DLG_STRINGINPUT, rResMgr ) ),
271   m_aNameText ( this, ResId( FT_STRINGINPUT_DLG_NAME, rResMgr ) ),
272   m_aNameEdit ( this, ResId( ED_STRINGINPUT_DLG_NAME, rResMgr ) ),
273   m_aNameGroup( this, ResId( GB_STRINGINPUT_DLG_NAME, rResMgr ) ),
274   m_aOKBtn    ( this, ResId( BT_STRINGINPUT_DLG_OK, rResMgr ) ),
275   m_aCancelBtn( this, ResId( BT_STRINGINPUT_DLG_CANCEL, rResMgr ) ),
276   m_aHelpBtn  ( this, ResId( BT_STRINGINPUT_DLG_HELP, rResMgr ) )
277 {
278     FreeResource();
279     SetText( rTitle );
280     m_aNameEdit.SetText( rDefaultText );
281     m_aOKBtn.SetClickHdl( LINK( this, StringInputDialog, OKHdl ) );
282     m_aNameEdit.SetModifyHdl( LINK( this, StringInputDialog, NameHdl ) );
283 
284     if ( pGroupName )
285         m_aNameGroup.SetText( *pGroupName );
286 };
287 
288 //=========================================================================
IMPL_LINK(StringInputDialog,OKHdl,Button *,EMPTYARG)289 IMPL_LINK( StringInputDialog, OKHdl, Button *, EMPTYARG )
290 {
291     // trim the strings
292     m_aNameEdit.SetText(
293         m_aNameEdit.GetText().EraseLeadingChars().EraseTrailingChars() );
294     EndDialog( RET_OK );
295     return 1;
296 }
297 
298 //=========================================================================
IMPL_LINK(StringInputDialog,NameHdl,Edit *,EMPTYARG)299 IMPL_LINK( StringInputDialog, NameHdl, Edit *, EMPTYARG )
300 {
301 /*
302     // trim the strings
303     String aName = m_aNameEdit.GetText();
304     aName.EraseLeadingChars().EraseTrailingChars();
305     if ( aName.Len() )
306     {
307         if ( !m_aOKBtn.IsEnabled() )
308             m_aOKBtn.Enable( TRUE );
309     }
310     else
311     {
312         if ( m_aOKBtn.IsEnabled() )
313             m_aOKBtn.Enable( FALSE );
314     }
315 */
316     return 0;
317 }
318 
319 //=========================================================================
320 //
321 // UcbExplorerListBoxEntry implementation.
322 //
323 //=========================================================================
324 
UcbExplorerListBoxEntry()325 UcbExplorerListBoxEntry::UcbExplorerListBoxEntry()
326 {
327 }
328 
329 //=========================================================================
330 // virtual
~UcbExplorerListBoxEntry()331 UcbExplorerListBoxEntry::~UcbExplorerListBoxEntry()
332 {
333 }
334 
335 //=========================================================================
createNewContent(const ContentInfo & rInfo,::ucbhelper::Content & rNewContent)336 BOOL UcbExplorerListBoxEntry::createNewContent( const ContentInfo& rInfo,
337                                                 ::ucbhelper::Content& rNewContent )
338 {
339     sal_Int32 nCount = rInfo.Properties.getLength();
340     Sequence< Any > aPropValues( nCount );
341     Sequence< OUString > aPropNames( nCount );
342 
343     if ( nCount > 0 )
344     {
345         // Collect property values.
346 
347         Any* pValues = aPropValues.getArray();
348         const Property* pProps = rInfo.Properties.getConstArray();
349         for ( sal_Int32 n = 0; n < nCount; ++n )
350         {
351             const OUString& rName = pProps[ n ].Name;
352 
353             std::auto_ptr< ResMgr > xManager(
354                 ResMgr::CreateResMgr( CREATEVERSIONRESMGR_NAME( ucbexplorer ) ) );
355             StringInputDialog* pDlg = new StringInputDialog( *xManager.get(), rName, rName );
356             USHORT nRet = pDlg->Execute();
357             if ( nRet == RET_OK )
358             {
359                 OUString aValue( pDlg->GetValue() );
360 
361                 // Convert value according to supplied type info...
362 
363                 const Type& rType = pProps[ n ].Type;
364 
365                 if ( rType == getCppuType(
366                                     static_cast< const OUString * >( 0 ) ) )
367                 {
368                     // string
369                     pValues[ n ] <<= aValue;
370                 }
371                 else if ( rType == getCppuType(
372                                     static_cast< const sal_Int32 * >( 0 ) ) )
373                 {
374                     // long
375                     pValues[ n ] <<= aValue.toInt32();
376                 }
377                 else if ( rType == getCppuType(
378                                     static_cast< const sal_Int16 * >( 0 ) ) )
379                 {
380                     // short
381                     pValues[ n ] <<= sal_Int16( aValue.toInt32() ) ;
382                 }
383                 else if ( rType == getCppuBooleanType() )
384                 {
385                     // boolean
386                     pValues[ n ] <<= sal_Bool( aValue.toChar() ) ;
387                 }
388                 else if ( rType == getCppuCharType() )
389                 {
390                     // char
391                     pValues[ n ] <<= aValue.toChar();
392                 }
393                 else if ( rType == getCppuType(
394                                     static_cast< const sal_Int8 * >( 0 ) ) )
395                 {
396                     // byte
397                     pValues[ n ] <<= sal_Int8( aValue.toChar() ) ;
398                 }
399                 else if ( rType == getCppuType(
400                                     static_cast< const sal_Int64 * >( 0 ) ) )
401                 {
402                     // hyper
403                     pValues[ n ] <<= aValue.toInt64();
404                 }
405                 else if ( rType == getCppuType(
406                                     static_cast< const float * >( 0 ) ) )
407                 {
408                     // float
409                     pValues[ n ] <<= aValue.toFloat();
410                 }
411                 else if ( rType == getCppuType(
412                                     static_cast< const double * >( 0 ) ) )
413                 {
414                     // double
415                     pValues[ n ] <<= aValue.toDouble();
416                 }
417                 else
418                 {
419                     // See com/sun/star/ucb/ContentInfo.idl
420                     DBG_ERROR( "UcbExplorerListBoxEntry::createNewContent - "
421                                "Invlid type for bootstrap property!" );
422                 }
423             }
424 
425             delete pDlg;
426 
427             if ( nRet != RET_OK )
428                 return FALSE;
429 
430             aPropNames[ n ] = rName;
431         }
432     }
433 
434     uno::Reference< XInputStream > xData;
435 
436     if ( rInfo.Attributes & ContentInfoAttribute::INSERT_WITH_INPUTSTREAM )
437     {
438         // Let the user specify the URL of a content containing the
439         // data to supply to the new content.
440 
441         std::auto_ptr< ResMgr > xManager(
442             ResMgr::CreateResMgr( CREATEVERSIONRESMGR_NAME( ucbexplorer ) ) );
443         StringInputDialog* pDlg = new StringInputDialog(
444                                             *xManager.get(),
445                                             OUString::createFromAscii(
446                                                 "Document Data Source URL" ),
447                                             OUString() );
448         USHORT nRet = pDlg->Execute();
449         if ( nRet == RET_OK )
450         {
451             // Create a content object for the given URL.
452 
453             OUString aSourceURL( pDlg->GetValue() );
454             if ( aSourceURL.getLength() == 0 )
455             {
456                 DBG_ERROR( "UcbExplorerListBoxEntry::createNewContent - "
457                                "No document data URL!" );
458                 return FALSE;
459             }
460 
461             try
462             {
463                 uno::Reference< XCommandEnvironment > xEnv;
464 
465                 ::ucbhelper::ContentBroker* pBroker = ::ucbhelper::ContentBroker::get();
466                 if ( pBroker )
467                 {
468                     uno::Reference< XInteractionHandler > xInteractionHandler(
469                         pBroker->getServiceManager()->createInstance(
470                                 OUString::createFromAscii(
471                                     "com.sun.star.task.InteractionHandler" ) ),
472                         UNO_QUERY );
473 
474                     uno::Reference< XProgressHandler > xProgressHandler
475                                     /* = new ProgressHandler( *pBroker ) */ ;
476 
477                     xEnv = new ::ucbhelper::CommandEnvironment( xInteractionHandler,
478                                                                 xProgressHandler );
479                 }
480 
481                 ::ucbhelper::Content aSourceContent( aSourceURL, xEnv );
482 
483                 // Get source data.
484                 rtl::Reference< TestDataSink > xSourceData = new TestDataSink;
485                 aSourceContent.openStream( xSourceData.get() );
486                 xData = xSourceData->getInputStream();
487             }
488             catch ( ContentCreationException const & )
489             {
490                 DBG_ERROR( "UcbExplorerListBoxEntry::createNewContent - "
491                                "No content for document data!" );
492                 return FALSE;
493             }
494             catch ( CommandAbortedException const & )
495             {
496                 return FALSE;
497             }
498             catch ( RuntimeException const & )
499             {
500                 throw;
501             }
502             catch ( Exception const & )
503             {
504                 return FALSE;
505             }
506         }
507 
508         delete pDlg;
509 
510         if ( nRet != RET_OK )
511             return FALSE;
512     }
513 
514     // Create new content.
515 
516         ULONG n = Application::ReleaseSolarMutex();
517     BOOL bRet = sal_False;
518 
519     try
520     {
521         bRet = m_aContent.insertNewContent(
522                     rInfo.Type, aPropNames, aPropValues, xData, rNewContent );
523     }
524     catch ( CommandAbortedException const & )
525     {
526     }
527     catch ( RuntimeException const & )
528     {
529         throw;
530     }
531     catch ( Exception const & )
532     {
533     }
534 
535     Application::AcquireSolarMutex( n );
536     return bRet;
537 }
538 
539 //=========================================================================
540 //
541 // UcbExplorerTreeListBox implementation.
542 //
543 //=========================================================================
544 
UcbExplorerTreeListBox(ResMgr & rResMgr,Window * pParent,WinBits nWinStyle)545 UcbExplorerTreeListBox::UcbExplorerTreeListBox( ResMgr & rResMgr,
546                                                 Window* pParent,
547                                                 WinBits nWinStyle )
548 : SvTreeListBox( pParent, nWinStyle ),
549   m_aFolderClosed( ResId( BMP_FOLDER_CLOSED, rResMgr ) ),
550   m_aFolderOpened( ResId( BMP_FOLDER_OPENED, rResMgr ) ),
551   m_aDocument( ResId( BMP_DOCUMENT, rResMgr ) ),
552   m_aLink( ResId( BMP_LINK, rResMgr ) )
553 {
554 }
555 
556 //=========================================================================
557 // virtual
~UcbExplorerTreeListBox()558 UcbExplorerTreeListBox::~UcbExplorerTreeListBox()
559 {
560 }
561 
562 //=========================================================================
563 // virtual
CreateEntry() const564 SvLBoxEntry* UcbExplorerTreeListBox::CreateEntry() const
565 {
566     return new UcbExplorerListBoxEntry();
567 }
568 
569 //=========================================================================
570 // virtual
RequestingChilds(SvLBoxEntry * pParent)571 void UcbExplorerTreeListBox::RequestingChilds( SvLBoxEntry* pParent )
572 {
573     UcbExplorerListBoxEntry* pEntry
574         = static_cast< UcbExplorerListBoxEntry * >( pParent );
575     if ( !pEntry->HasChilds() )
576     {
577         switch ( pEntry->m_eType )
578         {
579             case UcbExplorerListBoxEntry::FOLDER:
580             {
581                     ULONG n = Application::ReleaseSolarMutex();
582 
583                 try
584                 {
585                     Sequence< OUString > aPropertyNames( 0 );
586 //                    OUString* pNames = aPropertyNames.getArray();
587 //                    pNames[ 0 ] = OUString::createFromAscii( "Title" );
588 
589                     uno::Reference< XResultSet > xResultSet
590                         = pEntry->m_aContent.createCursor(
591                                     aPropertyNames,
592                                     ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS );
593                     uno::Reference< XContentAccess > xContentAccess(
594                                                     xResultSet, UNO_QUERY );
595 
596                     if ( xResultSet.is() && xContentAccess.is() )
597                     {
598                         while ( xResultSet->next() )
599                         {
600                             Application::AcquireSolarMutex( n );
601 
602                             InsertEntry(
603                                 xContentAccess->queryContentIdentifierString(),
604                                 pParent );
605 
606                             n = Application::ReleaseSolarMutex();
607                         }
608                     }
609                 }
610                 catch ( CommandAbortedException const & )
611                 {
612                 }
613                 catch ( RuntimeException const & )
614                 {
615                     throw;
616                 }
617                 catch ( Exception const & )
618                 {
619                 }
620 
621                 Application::AcquireSolarMutex( n );
622                 break;
623             }
624 
625             case UcbExplorerListBoxEntry::DOCUMENT:
626                 break;
627 
628             case UcbExplorerListBoxEntry::LINK:
629                 break;
630 
631             default:
632                 break;
633         }
634     }
635 }
636 
637 //=========================================================================
638 // virtual
Command(const CommandEvent & rCEvt)639 void UcbExplorerTreeListBox::Command( const CommandEvent& rCEvt )
640 {
641     if ( rCEvt.GetCommand() == COMMAND_CONTEXTMENU )
642     {
643         const Point& rPos = rCEvt.GetMousePosPixel();
644         UcbExplorerListBoxEntry* pEntry
645             = static_cast< UcbExplorerListBoxEntry * >(
646                 GetEntry( rPos, TRUE ) );
647         if ( pEntry )
648         {
649            std::auto_ptr< ResMgr > xManager(
650                 ResMgr::CreateResMgr( CREATEVERSIONRESMGR_NAME( ucbexplorer ) ) );
651            PopupMenu* pMenu = new PopupMenu( ResId( MENU_POPUP, *xManager.get() ) );
652             PopupMenu* pNewMenu = 0;
653 
654 //            pMenu->SetSelectHdl( LINK( this,
655 //                                       SfxCommonTemplateDialog_Impl,
656 //                                       MenuSelectHdl ) );
657 
658             //////////////////////////////////////////////////////////////
659             // Configure "New"
660             //////////////////////////////////////////////////////////////
661 
662             Sequence< ContentInfo > aInfo
663                 = pEntry->m_aContent.queryCreatableContentsInfo();
664             const ContentInfo* pInfo = aInfo.getConstArray();
665             sal_Int32 nCount = aInfo.getLength();
666             BOOL bCanCreate =  ( nCount > 0 );
667 
668             pNewMenu = new PopupMenu;
669             pMenu->SetPopupMenu( MENU_NEW, pNewMenu );
670 
671             for ( sal_Int32 n = 0; n < nCount; ++n )
672             {
673                 const ContentInfo& rInfo = pInfo[ n ];
674                 pNewMenu->InsertItem( 20000 + n + 1, rInfo.Type );
675             }
676 
677             pMenu->EnableItem( MENU_NEW, bCanCreate );
678 
679             //////////////////////////////////////////////////////////////
680             // Configure "Rename"
681             //////////////////////////////////////////////////////////////
682 
683             sal_Bool bEnable = sal_False;
684 
685             try
686             {
687                 Property aProp =
688                     pEntry->m_aContent.getProperties()->getPropertyByName(
689                                         OUString::createFromAscii( "Title" ) );
690                 bEnable = !( aProp.Attributes & PropertyAttribute::READONLY );
691             }
692             catch( UnknownPropertyException const & )
693             {
694                 // getPropertyByName
695                 bEnable = sal_False;
696             }
697             catch ( CommandAbortedException const & )
698             {
699             }
700             catch ( RuntimeException const & )
701             {
702                 throw;
703             }
704             catch ( Exception const & )
705             {
706             }
707 
708             pMenu->EnableItem( MENU_RENAME, bEnable );
709 
710             //////////////////////////////////////////////////////////////
711             // Configure "Delete"
712             //////////////////////////////////////////////////////////////
713 
714             try
715             {
716                 pMenu->EnableItem( MENU_DELETE,
717                                    pEntry->m_aContent
718                                         .getCommands()->hasCommandByName(
719                                         OUString::createFromAscii(
720                                             "delete" ) ) );
721             }
722             catch ( CommandAbortedException const & )
723             {
724             }
725             catch ( RuntimeException const & )
726             {
727                 throw;
728             }
729             catch ( Exception const & )
730             {
731             }
732 
733             //////////////////////////////////////////////////////////////
734             // Execute menu.
735             //////////////////////////////////////////////////////////////
736 
737             USHORT nSelected = pMenu->Execute( this, rPos );
738             switch ( nSelected )
739             {
740 //                case MENU_NEW:
741 //                    break;
742 
743                 case MENU_RENAME:
744                 {
745                     OUString aNewTitle;
746 
747                     try
748                     {
749                         pEntry->m_aContent.getPropertyValue(
750                                     OUString::createFromAscii( "Title" ) )
751                                     >>= aNewTitle;
752                     }
753                     catch ( CommandAbortedException const & )
754                     {
755                     }
756                     catch ( RuntimeException const & )
757                     {
758                         throw;
759                     }
760                     catch ( Exception const & )
761                     {
762                     }
763 
764                     std::auto_ptr< ResMgr > xManager(
765                         ResMgr::CreateResMgr( CREATEVERSIONRESMGR_NAME( ucbexplorer ) ) );
766                     StringInputDialog* pDlg
767                             = new StringInputDialog(
768                                     *xManager.get(),
769                                     OUString::createFromAscii( "Title" ),
770                                     aNewTitle );
771 
772                     USHORT nRet = pDlg->Execute();
773 
774                     Any aValue;
775 
776                     if ( nRet == RET_OK )
777                     {
778                         aNewTitle = pDlg->GetValue();
779                         aValue <<= aNewTitle;
780                     }
781 
782                     delete pDlg;
783 
784                     if ( nRet != RET_OK )
785                         break;
786 
787                     sal_Bool bOK = sal_False;
788 
789                     ULONG n = Application::ReleaseSolarMutex();
790 
791                     try
792                     {
793                         pEntry->m_aContent.setPropertyValue(
794                                     OUString::createFromAscii( "Title" ),
795                                     aValue );
796                         bOK = sal_True;
797                     }
798                     catch ( CommandAbortedException const & )
799                     {
800                     }
801                     catch ( RuntimeException const & )
802                     {
803                         throw;
804                     }
805                     catch ( Exception const & )
806                     {
807                     }
808 
809                     Application::AcquireSolarMutex( n );
810 
811                     if ( bOK )
812                     {
813                         OUString aText( aNewTitle );
814                         OUString aTargetURL;
815                         try
816                         {
817                             pEntry->m_aContent.getPropertyValue(
818                                 OUString::createFromAscii( "TargetURL" ) )
819                                     >>= aTargetURL;
820                         }
821                         catch ( CommandAbortedException const & )
822                         {
823                         }
824                         catch ( RuntimeException const & )
825                         {
826                             throw;
827                         }
828                         catch ( Exception const & )
829                         {
830                             // The property is optional!
831                         }
832 
833                         if ( aTargetURL.getLength() > 0 )
834                         {
835                             // Insert link.
836                             aText += OUString::createFromAscii( " --> " );
837                             aText += aTargetURL;
838                         }
839 
840                         SetEntryText( pEntry, aText );
841                     }
842                     break;
843                 }
844 
845                 case MENU_DELETE:
846                 {
847                     ULONG n = Application::ReleaseSolarMutex();
848                     sal_Bool bOK = sal_True;
849 
850                     try
851                     {
852                         pEntry->m_aContent.executeCommand(
853                                     OUString::createFromAscii( "delete" ),
854                                     makeAny( sal_True ) );
855                     }
856                     catch ( CommandAbortedException const & )
857                     {
858                         bOK = sal_False;
859                     }
860                     catch ( RuntimeException const & )
861                     {
862                         throw;
863                     }
864                     catch ( Exception const & )
865                     {
866                         bOK = sal_False;
867                     }
868 
869                     Application::AcquireSolarMutex( n );
870 
871                     if ( bOK )
872                         RemoveSelection();
873 
874                     break;
875                 }
876 
877                 default:
878                 {
879                     if ( ( nSelected > 20000 ) &&
880                          ( ( nSelected - 20000 ) <= aInfo.getLength() ) )
881                     {
882                         // New-menu entry selected.
883 
884                         ::ucbhelper::Content aNewContent;
885                         if ( pEntry->createNewContent(
886                                     aInfo.getConstArray()[ nSelected - 20001 ],
887                                     aNewContent ) )
888                         {
889                             if ( !IsExpanded( pEntry ) )
890                                 Expand( pEntry );
891                             else
892                                 InsertEntry( aNewContent, pEntry );
893                         }
894                     }
895                     break;
896                 }
897             }
898 
899             delete pNewMenu;
900             delete pMenu;
901             return;
902         }
903     }
904 
905     SvTreeListBox::Command( rCEvt );
906 }
907 
908 //=========================================================================
InsertEntry(::ucbhelper::Content & rContent,SvLBoxEntry * pParent)909 UcbExplorerListBoxEntry* UcbExplorerTreeListBox::InsertEntry(
910                                                 ::ucbhelper::Content& rContent,
911                                                 SvLBoxEntry* pParent )
912 {
913     try
914     {
915         OUString aTitle;
916         rContent.getPropertyValue(
917                         OUString::createFromAscii( "Title" ) ) >>= aTitle;
918         if ( !aTitle.getLength() )
919             aTitle = OUString::createFromAscii( "/" );
920 
921             UcbExplorerListBoxEntry* pEntry = 0;
922 
923         if ( rContent.isFolder() )
924         {
925             // Insert folder.
926             pEntry = static_cast< UcbExplorerListBoxEntry * >(
927                             SvTreeListBox::InsertEntry( aTitle,
928                                                     m_aFolderOpened,
929                                                     m_aFolderClosed,
930                                                     pParent,
931                                                     TRUE ) );
932             pEntry->m_eType = UcbExplorerListBoxEntry::FOLDER;
933         }
934         else
935         {
936             OUString aTargetURL;
937             try
938             {
939                 rContent.getPropertyValue(
940                     OUString::createFromAscii( "TargetURL" ) ) >>= aTargetURL;
941             }
942             catch ( CommandAbortedException const & )
943             {
944             }
945             catch ( RuntimeException const & )
946             {
947                 throw;
948             }
949             catch ( Exception const & )
950             {
951                 // The property is optional!
952             }
953 
954             if ( aTargetURL.getLength() > 0 )
955             {
956                 // Insert link.
957                 aTitle += OUString::createFromAscii( " --> " );
958                 aTitle += aTargetURL;
959                 pEntry = static_cast< UcbExplorerListBoxEntry * >(
960                                 SvTreeListBox::InsertEntry( aTitle,
961                                                         m_aLink,
962                                                         m_aLink,
963                                                         pParent,
964                                                         TRUE ) );
965                 pEntry->m_eType = UcbExplorerListBoxEntry::LINK;
966             }
967             else
968             {
969                 // Insert Document
970                 pEntry = static_cast< UcbExplorerListBoxEntry * >(
971                                 SvTreeListBox::InsertEntry( aTitle,
972                                                         m_aDocument,
973                                                         m_aDocument,
974                                                         pParent,
975                                                         TRUE ) );
976                 pEntry->m_eType = UcbExplorerListBoxEntry::DOCUMENT;
977             }
978         }
979 
980         pEntry->m_aContent = rContent;
981         return pEntry;
982     }
983     catch ( CommandAbortedException const & )
984     {
985     }
986     catch ( Exception const & )
987     {
988     }
989 
990     return 0;
991 }
992 
993 //=========================================================================
InsertEntry(const String & rURL,SvLBoxEntry * pParent)994 UcbExplorerListBoxEntry* UcbExplorerTreeListBox::InsertEntry(
995                                                 const String& rURL,
996                                                     SvLBoxEntry* pParent )
997 {
998     try
999     {
1000         uno::Reference< XCommandEnvironment > xEnv;
1001 
1002         ::ucbhelper::ContentBroker* pBroker = ::ucbhelper::ContentBroker::get();
1003         if ( pBroker )
1004         {
1005             uno::Reference< XInteractionHandler > xInteractionHandler(
1006                 pBroker->getServiceManager()->createInstance(
1007                         OUString::createFromAscii(
1008                             "com.sun.star.task.InteractionHandler" ) ),
1009                 UNO_QUERY );
1010 
1011             uno::Reference< XProgressHandler > xProgressHandler
1012                                 /* = new ProgressHandler( *pBroker ) */ ;
1013 
1014             xEnv = new ::ucbhelper::CommandEnvironment( xInteractionHandler,
1015                                                         xProgressHandler );
1016         }
1017 
1018         ::ucbhelper::Content aContent( rURL, xEnv );
1019         return InsertEntry( aContent, pParent );
1020     }
1021     catch ( ContentCreationException const & )
1022     {
1023     }
1024 
1025     return 0;
1026 }
1027 
1028 //=========================================================================
1029 //
1030 // UcbExplorerWindow implementation.
1031 //
1032 //=========================================================================
1033 
UcbExplorerWindow(ResMgr & rResMgr,Window * pParent,WinBits nWinStyle)1034 UcbExplorerWindow::UcbExplorerWindow( ResMgr & rResMgr, Window *pParent, WinBits nWinStyle )
1035 : WorkWindow( pParent, nWinStyle ),
1036   m_aTree( rResMgr, this, WB_HSCROLL )
1037 {
1038     Font aTreeFont( m_aTree.GetFont() );
1039     aTreeFont.SetName( String( RTL_CONSTASCII_USTRINGPARAM("Courier") ) );
1040     aTreeFont.SetFamily( FAMILY_MODERN );
1041     aTreeFont.SetPitch( PITCH_FIXED );
1042     aTreeFont.SetSize( Size( 0, 12 ) );
1043 
1044     m_aTree.SetFont( aTreeFont );
1045     m_aTree.SetIndent( 20 );
1046     m_aTree.SetPosPixel( Point( 0, 0 ) );
1047 
1048     m_aTree.Show();
1049 }
1050 
1051 //-------------------------------------------------------------------------
1052 // virtual
~UcbExplorerWindow()1053 UcbExplorerWindow::~UcbExplorerWindow()
1054 {
1055 }
1056 
1057 //-------------------------------------------------------------------------
1058 // virtual
Resize()1059 void UcbExplorerWindow::Resize()
1060 {
1061     m_aTree.SetSizePixel( GetOutputSizePixel() );
1062 }
1063 
1064 //=========================================================================
1065 //
1066 // MyApp implementation.
1067 //
1068 //=========================================================================
1069 
1070 // virtual
Main()1071 void MyApp::Main()
1072 {
1073     //////////////////////////////////////////////////////////////////////
1074     // Initialize local Service Manager and basic services.
1075     //////////////////////////////////////////////////////////////////////
1076 
1077     uno::Reference< XMultiServiceFactory > xFac;
1078     try
1079     {
1080         uno::Reference< XComponentContext > xCtx(
1081             cppu::defaultBootstrap_InitialComponentContext() );
1082         if ( !xCtx.is() )
1083         {
1084             DBG_ERROR( "Error creating initial component context!" );
1085             return;
1086         }
1087 
1088         xFac = uno::Reference< XMultiServiceFactory >(
1089             xCtx->getServiceManager(), UNO_QUERY );
1090 
1091         if ( !xFac.is() )
1092         {
1093             DBG_ERROR( "No service manager!" );
1094             return;
1095         }
1096     }
1097     catch ( com::sun::star::uno::Exception const & )
1098     {
1099         DBG_ERROR( "Exception during creation of initial component context!" );
1100         return;
1101     }
1102 
1103     comphelper::setProcessServiceFactory( xFac );
1104 
1105     uno::Reference< XComponent > xComponent( xFac, UNO_QUERY );
1106 
1107     //////////////////////////////////////////////////////////////////////
1108     // Create UCB.
1109     //////////////////////////////////////////////////////////////////////
1110 
1111 #if 1
1112     // Init UCB (Read configuration from registry)
1113     Sequence< Any > aArgs( 2 );
1114     aArgs[ 0 ] <<= OUString::createFromAscii( UCB_CONFIGURATION_KEY1_LOCAL );
1115     aArgs[ 1 ] <<= OUString::createFromAscii( UCB_CONFIGURATION_KEY2_OFFICE );
1116     sal_Bool bSuccess = ::ucbhelper::ContentBroker::initialize( xFac, aArgs );
1117 #else
1118     // Init UCB (Use provided configuration data)
1119     ::ucbhelper::ContentProviderDataList aProviders;
1120     aProviders.push_back(
1121         ::ucbhelper::ContentProviderData(
1122             OUString::createFromAscii( "com.sun.star.ucb.FileContentProvider" ),
1123             OUString::createFromAscii( "file" ),
1124             OUString() ) );
1125     sal_Bool bSuccess = ::ucbhelper::ContentBroker::initialize( xFac, aProviders );
1126 #endif
1127 
1128     if ( !bSuccess )
1129     {
1130         DBG_ERROR( "Error creating UCB!" );
1131         return;
1132     }
1133 
1134     //////////////////////////////////////////////////////////////////////
1135     // Create/init/show app window.
1136     //////////////////////////////////////////////////////////////////////
1137 
1138     std::auto_ptr< ResMgr > xManager(
1139         ResMgr::CreateResMgr( CREATEVERSIONRESMGR_NAME( ucbexplorer ) ) );
1140 
1141     UcbExplorerWindow aAppWin( *xManager.get(), 0, WB_APP | WB_STDWORK );
1142 
1143     MenuBar aMBMain( ResId( MENU_MAIN, *xManager.get() ) );
1144 
1145     // Check for command line params
1146 #if 0
1147     for ( int i = 0; i < GetCommandLineParamCount(); ++i )
1148     {
1149         String aPara = GetCommandLineParam( i );
1150     }
1151 #endif
1152 
1153      String aRootURL = GetCommandLineParam( 0 );
1154      if ( aRootURL.Len() == 0 )
1155         aRootURL = UniString::CreateFromAscii(
1156                         RTL_CONSTASCII_STRINGPARAM( "vnd.sun.star.hier:/" ) );
1157 
1158      String aTitle( ResId( TEXT_TITLEBAR, *xManager.get() ) );
1159     aTitle.AppendAscii( RTL_CONSTASCII_STRINGPARAM( " - " ) );
1160     aTitle += aRootURL;
1161 
1162     aAppWin.SetText( aTitle );
1163 
1164     aAppWin.SetPosSizePixel( 0, 0, 500, 750 );
1165 
1166     aAppWin.Show();
1167 
1168     aAppWin.m_aTree.InsertEntry( aRootURL );
1169 
1170     //////////////////////////////////////////////////////////////////////
1171     // Execute app.
1172     //////////////////////////////////////////////////////////////////////
1173 
1174     Execute();
1175 
1176     //////////////////////////////////////////////////////////////////////
1177     // Cleanup.
1178     //////////////////////////////////////////////////////////////////////
1179 
1180     // m_aTree holds UCB contents!
1181     aAppWin.m_aTree.Clear();
1182 
1183     ::ucbhelper::ContentBroker::deinitialize();
1184 
1185     if ( xComponent.is() )
1186         xComponent->dispose();
1187 }
1188 
1189 //=========================================================================
1190 //
1191 // The Application.
1192 //
1193 //=========================================================================
1194 
1195 MyApp aMyApp;
1196 
1197