xref: /trunk/main/dbaccess/source/ui/dlg/CollectionView.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_dbaccess.hxx"
30 #ifndef DBAUI_COLLECTIONVIEW_HXX
31 #include "CollectionView.hxx"
32 #endif
33 #ifndef DBAUI_COLLECTIONVIEW_HRC
34 #include "CollectionView.hrc"
35 #endif
36 #ifndef _TOOLS_DEBUG_HXX
37 #include <tools/debug.hxx>
38 #endif
39 #ifndef TOOLS_DIAGNOSE_EX_H
40 #include <tools/diagnose_ex.h>
41 #endif
42 #ifndef _DBAUI_MODULE_DBU_HXX_
43 #include "moduledbu.hxx"
44 #endif
45 #ifndef _DBU_DLG_HRC_
46 #include "dbu_dlg.hrc"
47 #endif
48 #ifndef _COMPHELPER_INTERACTION_HXX_
49 #include <comphelper/interaction.hxx>
50 #endif
51 #ifndef _CPPUHELPER_EXC_HLP_HXX_
52 #include <cppuhelper/exc_hlp.hxx>
53 #endif
54 #ifndef _TOOLKIT_HELPER_VCLUNOHELPER_HXX_
55 #include <toolkit/helper/vclunohelper.hxx>
56 #endif
57 #ifndef SVT_QUERYFOLDER_HXX
58 #include <svtools/QueryFolderName.hxx>
59 #endif
60 #ifndef _COM_SUN_STAR_LANG_XMULTISERVICEFACTORY_HPP_
61 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
62 #endif
63 #ifndef _COM_SUN_STAR_LANG_XINITIALIZATION_HPP_
64 #include <com/sun/star/lang/XInitialization.hpp>
65 #endif
66 #ifndef _COM_SUN_STAR_CONTAINER_XCHILD_HPP_
67 #include <com/sun/star/container/XChild.hpp>
68 #endif
69 #ifndef _COM_SUN_STAR_CONTAINER_XNAMECONTAINER_HPP_
70 #include <com/sun/star/container/XNameContainer.hpp>
71 #endif
72 #ifndef _COM_SUN_STAR_BEANS_PROPERTYVALUE_HPP_
73 #include <com/sun/star/beans/PropertyValue.hpp>
74 #endif
75 #ifndef _SV_MSGBOX_HXX
76 #include <vcl/msgbox.hxx>
77 #endif
78 #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC
79 #include "dbustrings.hrc"
80 #endif
81 #ifndef DBAUI_TOOLS_HXX
82 #include "UITools.hxx"
83 #endif
84 #ifndef _COM_SUN_STAR_CONTAINER_XHIERARCHICALNAMECONTAINER_HPP_
85 #include <com/sun/star/container/XHierarchicalNameContainer.hpp>
86 #endif
87 #ifndef _COM_SUN_STAR_UCB_INTERACTIVEAUGMENTEDIOEXCEPTION_HPP_
88 #include <com/sun/star/ucb/InteractiveAugmentedIOException.hpp>
89 #endif
90 #ifndef _COM_SUN_STAR_UCB_IOERRORCODE_HPP_
91 #include <com/sun/star/ucb/IOErrorCode.hpp>
92 #endif
93 #ifndef _COM_SUN_STAR_TASK_XINTERACTIONHANDLER_HPP_
94 #include <com/sun/star/task/XInteractionHandler.hpp>
95 #endif
96 #ifndef _COM_SUN_STAR_TASK_INTERACTIONCLASSIFICATION_HPP_
97 #include <com/sun/star/task/InteractionClassification.hpp>
98 #endif
99 #ifndef _COM_SUN_STAR_SDBC_SQLEXCEPTION_HPP_
100 #include <com/sun/star/sdbc/SQLException.hpp>
101 #endif
102 #ifndef _COM_SUN_STAR_AWT_XWINDOW_HPP_
103 #include <com/sun/star/awt/XWindow.hpp>
104 #endif
105 #ifndef INCLUDED_SVTOOLS_VIEWOPTIONS_HXX
106 #include <unotools/viewoptions.hxx>
107 #endif
108 #ifndef _OSL_THREAD_H_
109 #include <osl/thread.h>
110 #endif
111 #ifndef _DBHELPER_DBEXCEPTION_HXX_
112 #include <connectivity/dbexception.hxx>
113 #endif
114 
115 #define FILEDIALOG_DEF_IMAGEBORDER  10
116 //.........................................................................
117 namespace dbaui
118 {
119 //.........................................................................
120 
121 using namespace ::com::sun::star::uno;
122 using namespace ::com::sun::star::ucb;
123 using namespace ::com::sun::star::lang;
124 using namespace ::com::sun::star::beans;
125 using namespace ::com::sun::star::container;
126 using namespace ::com::sun::star::ucb;
127 using namespace ::com::sun::star::task;
128 using namespace ::com::sun::star::sdbc;
129 using namespace comphelper;
130 // -----------------------------------------------------------------------------
131 DBG_NAME(OCollectionView)
132 OCollectionView::OCollectionView( Window * pParent
133                                  ,const Reference< XContent>& _xContent
134                                  ,const ::rtl::OUString& _sDefaultName
135                                  ,const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _xORB)
136     : ModalDialog( pParent, ModuleRes(DLG_COLLECTION_VIEW))
137     , m_aFTCurrentPath( this, ModuleRes( FT_EXPLORERFILE_CURRENTPATH ) )
138     , m_aNewFolder(     this, ModuleRes( BTN_EXPLORERFILE_NEWFOLDER ) )
139     , m_aUp(            this, ModuleRes( BTN_EXPLORERFILE_UP ) )
140     , m_aView(          this, ModuleRes( CTRL_VIEW ) ,FILEVIEW_SHOW_TITLE )
141     , m_aFTName(        this, ModuleRes( FT_EXPLORERFILE_FILENAME ) )
142     , m_aName(          this, ModuleRes( ED_EXPLORERFILE_FILENAME ) )
143     , m_aFL(            this, ModuleRes( FL_1 ) )
144     , m_aPB_OK(         this, ModuleRes( BTN_EXPLORERFILE_SAVE ) )
145     , m_aPB_CANCEL(     this, ModuleRes( PB_CANCEL ) )
146     , m_aPB_HELP(       this, ModuleRes( PB_HELP ) )
147     , m_sPath(          ModuleRes( STR_PATHNAME ) )
148     , m_xContent(_xContent)
149     , m_xORB(_xORB)
150     , m_bCreateForm(sal_True)
151 {
152     DBG_CTOR(OCollectionView,NULL);
153     FreeResource();
154 
155     OSL_ENSURE(m_xContent.is(),"No valid content!");
156     m_aView.Initialize(m_xContent,String());
157     m_aFTCurrentPath.SetStyle( m_aFTCurrentPath.GetStyle() | WB_PATHELLIPSIS );
158     initCurrentPath();
159 
160     m_aName.SetText(_sDefaultName);
161     m_aName.GrabFocus();
162 
163     m_aNewFolder.SetStyle( m_aNewFolder.GetStyle() | WB_NOPOINTERFOCUS );
164     m_aUp.SetModeImage(ModuleRes(IMG_NAVIGATION_BTN_UP_SC));
165     m_aUp.SetModeImage(ModuleRes(IMG_NAVIGATION_BTN_UP_SCH),BMP_COLOR_HIGHCONTRAST);
166     m_aNewFolder.SetModeImage(ModuleRes(IMG_NAVIGATION_CREATEFOLDER_SC));
167     m_aNewFolder.SetModeImage(ModuleRes(IMG_NAVIGATION_CREATEFOLDER_SCH),BMP_COLOR_HIGHCONTRAST);
168 
169     m_aView.SetDoubleClickHdl( LINK( this, OCollectionView, Dbl_Click_FileView ) );
170     m_aView.EnableAutoResize();
171     m_aUp.SetClickHdl( LINK( this, OCollectionView, Up_Click ) );
172     m_aNewFolder.SetClickHdl( LINK( this, OCollectionView, NewFolder_Click ) );
173     m_aPB_OK.SetClickHdl( LINK( this, OCollectionView, Save_Click ) );
174 }
175 // -----------------------------------------------------------------------------
176 OCollectionView::~OCollectionView( )
177 {
178     DBG_DTOR(OCollectionView,NULL);
179 }
180 // -----------------------------------------------------------------------------
181 Reference< XContent> OCollectionView::getSelectedFolder() const
182 {
183     return m_xContent;
184 }
185 // -----------------------------------------------------------------------------
186 IMPL_LINK( OCollectionView, Save_Click, PushButton*, EMPTYARG )
187 {
188     ::rtl::OUString sName = m_aName.GetText();
189     if ( !sName.getLength() )
190         return 0;
191     try
192     {
193         ::rtl::OUString sSubFolder = m_aView.GetCurrentURL();
194         sal_Int32 nIndex = sName.lastIndexOf('/') + 1;
195         if ( nIndex )
196         {
197             if ( nIndex == 1 ) // special handling for root
198             {
199                 Reference<XChild> xChild(m_xContent,UNO_QUERY);
200                 Reference<XNameAccess> xNameAccess(xChild,UNO_QUERY);
201                 while( xNameAccess.is() )
202                 {
203                     xNameAccess.set(xChild->getParent(),UNO_QUERY);
204                     if ( xNameAccess.is() )
205                     {
206                         m_xContent.set(xNameAccess,UNO_QUERY);
207                         xChild.set(m_xContent,UNO_QUERY);
208                     }
209                 }
210                 m_aView.Initialize(m_xContent,String());
211                 initCurrentPath();
212             }
213             sSubFolder = sName.copy(0,nIndex-1);
214             sName = sName.copy(nIndex);
215             Reference<XHierarchicalNameContainer> xHier(m_xContent,UNO_QUERY);
216             OSL_ENSURE(xHier.is(),"XHierarchicalNameContainer not supported!");
217             if ( sSubFolder.getLength() && xHier.is() )
218             {
219                 if ( xHier->hasByHierarchicalName(sSubFolder) )
220                 {
221                     m_xContent.set(xHier->getByHierarchicalName(sSubFolder),UNO_QUERY);
222                 }
223                 else // sub folder doesn't exist
224                 {
225                     Sequence< Any > aValues(2);
226                     PropertyValue aValue;
227                     aValue.Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ResourceName"));
228                     aValue.Value <<= sSubFolder;
229                     aValues[0] <<= aValue;
230 
231                     aValue.Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ResourceType"));
232                     aValue.Value <<= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("folder"));
233                     aValues[1] <<= aValue;
234 
235                     InteractionClassification eClass = InteractionClassification_ERROR;
236                     ::com::sun::star::ucb::IOErrorCode eError = IOErrorCode_NOT_EXISTING_PATH;
237                     ::rtl::OUString sTemp;
238                     InteractiveAugmentedIOException aException(sTemp,Reference<XInterface>(),eClass,eError,aValues);
239 
240 
241                     Reference<XInitialization> xIni(m_xORB->createInstance(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.task.InteractionHandler"))),UNO_QUERY);
242                     if ( xIni.is() )
243                     {
244                         aValue.Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Parent"));
245                         aValue.Value <<= VCLUnoHelper::GetInterface( this );
246                         Sequence< Any > aArgs(1);
247                         aArgs[0] <<= makeAny(aValue);
248                         xIni->initialize(aArgs);
249                         OInteractionRequest* pRequest = new OInteractionRequest(makeAny(aException));
250                         Reference< XInteractionRequest > xRequest(pRequest);
251 
252                         OInteractionApprove* pApprove = new OInteractionApprove;
253                         pRequest->addContinuation(pApprove);
254 
255                         Reference< XInteractionHandler > xHandler(xIni,UNO_QUERY);
256                         xHandler->handle(xRequest);
257                     }
258                     return 0;
259                 }
260             }
261         }
262         Reference<XNameContainer> xNameContainer(m_xContent,UNO_QUERY);
263         if ( xNameContainer.is() )
264         {
265             Reference< XContent> xContent;
266             if ( xNameContainer->hasByName(sName) )
267             {
268                 QueryBox aBox( this, WB_YES_NO, ModuleRes( STR_ALREADYEXISTOVERWRITE ) );
269                 if ( aBox.Execute() != RET_YES )
270                     return 0;
271                 // xNameContainer->removeByName(sName);
272             }
273             m_aName.SetText(sName);
274             EndDialog( sal_True );
275         }
276     }
277     catch( const Exception& )
278     {
279         DBG_UNHANDLED_EXCEPTION();
280     }
281     return 0;
282 }
283 // -----------------------------------------------------------------------------
284 IMPL_LINK( OCollectionView, NewFolder_Click, PushButton*, EMPTYARG )
285 {
286     try
287     {
288         Reference<XHierarchicalNameContainer> xNameContainer(m_xContent,UNO_QUERY);
289         if ( dbaui::insertHierachyElement(this,m_xORB,xNameContainer,String(),m_bCreateForm) )
290             m_aView.Initialize(m_xContent,String());
291     }
292     catch( const SQLException& )
293     {
294         showError( ::dbtools::SQLExceptionInfo( ::cppu::getCaughtException() ), this, m_xORB );
295     }
296     catch( const Exception& )
297     {
298         DBG_UNHANDLED_EXCEPTION();
299     }
300     return 0;
301 }
302 // -----------------------------------------------------------------------------
303 IMPL_LINK( OCollectionView, Up_Click, PushButton*, EMPTYARG )
304 {
305     try
306     {
307         Reference<XChild> xChild(m_xContent,UNO_QUERY);
308         if ( xChild.is() )
309         {
310             Reference<XNameAccess> xNameAccess(xChild->getParent(),UNO_QUERY);
311             if ( xNameAccess.is() )
312             {
313                 m_xContent.set(xNameAccess,UNO_QUERY);
314                 m_aView.Initialize(m_xContent,String());
315                 initCurrentPath();
316             }
317             else
318                 m_aUp.Disable();
319         }
320     }
321     catch( const Exception& )
322     {
323         DBG_UNHANDLED_EXCEPTION();
324     }
325     return 0;
326 }
327 // -----------------------------------------------------------------------------
328 IMPL_LINK( OCollectionView, Dbl_Click_FileView, SvtFileView*, EMPTYARG )
329 {
330     try
331     {
332         Reference<XNameAccess> xNameAccess(m_xContent,UNO_QUERY);
333         if ( xNameAccess.is() )
334         {
335             ::rtl::OUString sSubFolder = m_aView.GetCurrentURL();
336             sal_Int32 nIndex = sSubFolder.lastIndexOf('/') + 1;
337             sSubFolder = sSubFolder.getToken(0,'/',nIndex);
338             if ( sSubFolder.getLength() )
339             {
340                 Reference< XContent> xContent;
341                 if ( xNameAccess->hasByName(sSubFolder) )
342                     xContent.set(xNameAccess->getByName(sSubFolder),UNO_QUERY);
343                 if ( xContent.is() )
344                 {
345                     m_xContent = xContent;
346                     m_aView.Initialize(m_xContent,String());
347                     initCurrentPath();
348                 }
349             }
350         }
351     }
352     catch( const Exception& )
353     {
354         DBG_UNHANDLED_EXCEPTION();
355     }
356     return 0;
357 }
358 // -----------------------------------------------------------------------------
359 void OCollectionView::initCurrentPath()
360 {
361     sal_Bool bEnable = sal_False;
362     try
363     {
364         if ( m_xContent.is() )
365         {
366             const ::rtl::OUString sCID = m_xContent->getIdentifier()->getContentIdentifier();
367             const static ::rtl::OUString s_sFormsCID(RTL_CONSTASCII_USTRINGPARAM("private:forms"));
368             const static ::rtl::OUString s_sReportsCID(RTL_CONSTASCII_USTRINGPARAM("private:reports"));
369             m_bCreateForm = s_sFormsCID.compareTo(sCID) == 0;
370             ::rtl::OUString sPath(RTL_CONSTASCII_USTRINGPARAM("/"));
371             if ( m_bCreateForm && sCID.getLength() != s_sFormsCID.getLength())
372                 sPath = sCID.copy(s_sFormsCID.getLength());
373             else if ( !m_bCreateForm && sCID.getLength() != s_sReportsCID.getLength() )
374                 sPath = sCID.copy(s_sReportsCID.getLength());
375 
376             m_aFTCurrentPath.SetText(sPath);
377             Reference<XChild> xChild(m_xContent,UNO_QUERY);
378             bEnable = xChild.is() && Reference<XNameAccess>(xChild->getParent(),UNO_QUERY).is();
379         }
380     }
381     catch( const Exception& )
382     {
383         DBG_UNHANDLED_EXCEPTION();
384     }
385     m_aUp.Enable(bEnable);
386 }
387 // -----------------------------------------------------------------------------
388 ::rtl::OUString OCollectionView::getName() const
389 {
390     return m_aName.GetText();
391 }
392 // -----------------------------------------------------------------------------
393 //.........................................................................
394 }   // namespace dbaui
395 //.........................................................................
396 
397 
398