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_svl.hxx"
26 #include <svl/pickerhistory.hxx>
27 #include <svl/pickerhistoryaccess.hxx>
28 #include <cppuhelper/weakref.hxx>
29 #include <vector>
30 
31 //.........................................................................
32 namespace svt
33 {
34 //.........................................................................
35 	using namespace ::com::sun::star::uno;
36 
37 	namespace
38 	{
39 		typedef ::com::sun::star::uno::WeakReference< XInterface >	InterfaceAdapter;
40 		typedef ::std::vector< InterfaceAdapter  >					InterfaceArray;
41 
42 		// ----------------------------------------------------------------
getFolderPickerHistory()43 		InterfaceArray& getFolderPickerHistory()
44 		{
45 			static InterfaceArray s_aHistory;
46 			return s_aHistory;
47 		}
48 
49 		// ----------------------------------------------------------------
getFilePickerHistory()50 		InterfaceArray& getFilePickerHistory()
51 		{
52 			static InterfaceArray s_aHistory;
53 			return s_aHistory;
54 		}
55 
56 		// ----------------------------------------------------------------
implPushBackPicker(InterfaceArray & _rHistory,const Reference<XInterface> & _rxPicker)57 		void implPushBackPicker( InterfaceArray& _rHistory, const Reference< XInterface >& _rxPicker )
58 		{
59 			if ( !_rxPicker.is() )
60 				return;
61 
62             //=============================================================
63 			// first, check which of the objects we hold in s_aHistory can be removed
64 			{
65 				InterfaceArray aCleanedHistory;
66 				for	(	InterfaceArray::const_iterator aLoop = _rHistory.begin();
67 						aLoop != _rHistory.end();
68 						++aLoop
69 					)
70 				{
71 					Reference< XInterface > xCurrent( aLoop->get() );
72 					if ( xCurrent.is() )
73 					{
74 						if ( aCleanedHistory.empty() )
75 							// make some room, assume that all interfaces (from here on) are valie
76 							aCleanedHistory.reserve( _rHistory.size() - ( aLoop - _rHistory.begin() ) );
77 						aCleanedHistory.push_back( InterfaceAdapter( xCurrent ) );
78 					}
79 				}
80 				_rHistory.swap( aCleanedHistory );
81 			}
82 
83             //=============================================================
84 			// then push_back the picker
85 			_rHistory.push_back( InterfaceAdapter( _rxPicker ) );
86 		}
87 
88 		//-----------------------------------------------------------------
implGetTopMostPicker(const InterfaceArray & _rHistory)89 		Reference< XInterface > implGetTopMostPicker( const InterfaceArray& _rHistory )
90 		{
91 			Reference< XInterface > xTopMostAlive;
92 
93             //=============================================================
94 			// search the first picker which is still alive ...
95 			for	(	InterfaceArray::const_reverse_iterator aLoop = _rHistory.rbegin();
96 					( aLoop != _rHistory.rend() ) && !xTopMostAlive.is();
97 					++aLoop
98 				)
99 			{
100 				xTopMostAlive = aLoop->get();
101 			}
102 
103 			return xTopMostAlive;
104 		}
105 	}
106 
107 	//---------------------------------------------------------------------
GetTopMostFolderPicker()108 	Reference< XInterface > GetTopMostFolderPicker( )
109 	{
110 		return implGetTopMostPicker( getFolderPickerHistory() );
111 	}
112 
113 	//---------------------------------------------------------------------
GetTopMostFilePicker()114 	Reference< XInterface > GetTopMostFilePicker( )
115 	{
116 		return implGetTopMostPicker( getFilePickerHistory() );
117 	}
118 
119 	//---------------------------------------------------------------------
addFolderPicker(const Reference<XInterface> & _rxPicker)120 	void addFolderPicker( const Reference< XInterface >& _rxPicker )
121 	{
122 		implPushBackPicker( getFolderPickerHistory(), _rxPicker );
123 	}
124 
125 	//---------------------------------------------------------------------
addFilePicker(const Reference<XInterface> & _rxPicker)126 	void addFilePicker( const Reference< XInterface >& _rxPicker )
127 	{
128 		implPushBackPicker( getFilePickerHistory(), _rxPicker );
129 	}
130 
131 //.........................................................................
132 }	// namespace svt
133 //.........................................................................
134 
135