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