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