xref: /aoo42x/main/uui/source/fltdlg.cxx (revision 86e1cf34)
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 //_________________________________________________________________________________________________________________
25 //	my own includes
26 //_________________________________________________________________________________________________________________
27 
28 #include "fltdlg.hxx"
29 
30 #ifndef UUI_IDS_HRC
31 #include "ids.hrc"
32 #endif
33 
34 #ifndef UUI_FLTDLG_HRC
35 #include "fltdlg.hrc"
36 #endif
37 
38 //_________________________________________________________________________________________________________________
39 //	includes of other projects
40 //_________________________________________________________________________________________________________________
41 #include <com/sun/star/util/XStringWidth.hpp>
42 #include <cppuhelper/implbase1.hxx>
43 #include <unotools/localfilehelper.hxx>
44 #include <tools/list.hxx>
45 #include <tools/urlobj.hxx>
46 
47 #ifndef _BUTTON_HXX //autogen
48 #include <vcl/button.hxx>
49 #endif
50 #include <vos/mutex.hxx>
51 #include <vcl/svapp.hxx>
52 
53 namespace uui
54 {
55 
56 /*-************************************************************************************************************//**
57     @short      initialize filter dialog with start values
58     @descr      We set some necessary informations on these instance for later working and create internal structures.
59                 After construction user should call "SetFilters()" and "SetURL()" to fill listbox with selectable filter
60                 names and set file name of file, which should be used for selected filter.
61 
62     @seealso    method SetFilters()
63     @seealso    method SetURL()
64 
65     @param      "pParentWindow"  , parent window for dialog
66     @param      "pResMgr"        , ressource manager
67     @return     -
68 
69     @onerror    -
70     @threadsafe no
71 *//*-*************************************************************************************************************/
72 FilterDialog::FilterDialog( Window* pParentWindow ,
73                             ResMgr* pResMgr       )
74     :   ModalDialog  ( pParentWindow, ResId( DLG_FILTER_SELECT, *pResMgr ) )
75     ,   m_ftURL      ( this, ResId( FT_URL, *pResMgr            )                   )
76     ,   m_lbFilters  ( this, ResId( LB_FILTERS, *pResMgr        )                   )
77     ,   m_btnOK      ( this, ResId( BTN_OK, *pResMgr            )                   )
78     ,   m_btnCancel  ( this, ResId( BTN_CANCEL, *pResMgr        )                   )
79     ,   m_btnHelp    ( this, ResId( BTN_HELP, *pResMgr          )                   )
80 {
81     FreeResource();
82 }
83 
84 /*-************************************************************************************************************//**
85     @short      set file name on dialog control
86     @descr      We convert given URL (it must be an URL!) into valid file name and show it on our dialog.
87 
88     @seealso    -
89 
90     @param      "sURL", URL for showing
91     @return     -
92 
93     @onerror    -
94     @threadsafe no
95 *//*-*************************************************************************************************************/
96 void FilterDialog::SetURL( const String& sURL )
97 {
98     // convert it and use given pure string as fallback if conversion failed
99     m_ftURL.SetText( impl_buildUIFileName(sURL) );
100 }
101 
102 /*-************************************************************************************************************//**
103     @short      change list of filter names
104     @descr      We save given pointer internal and use it to fill our listbox with given names.
105                 Saved list pointer is used on method "AskForFilter()" too, to find user selected item
106                 and return pointer into these list as result of operation.
107                 So it's possible to call dialog again and again for different or same filter list
108                 and ask user for his decision by best performance!
109 
110     @attention  Don't free memory of given list after this call till object will die ... or
111                 you call "ChangeFilters( NULL )"! Then we forget it too.
112 
113     @seealso    method AskForFilter()
114 
115     @param      "pFilterNames", pointer to list of filter names, which should be used for later operations.
116     @return     -
117 
118     @onerror    We clear list box and forget our currently set filter informations completly!
119     @threadsafe no
120 *//*-*************************************************************************************************************/
121 void FilterDialog::ChangeFilters( const FilterNameList* pFilterNames )
122 {
123     m_pFilterNames = pFilterNames;
124     m_lbFilters.Clear();
125     if( m_pFilterNames != NULL )
126     {
127         for( FilterNameListPtr pItem  = m_pFilterNames->begin();
128                                pItem != m_pFilterNames->end()  ;
129                                ++pItem                         )
130         {
131             m_lbFilters.InsertEntry( pItem->sUI );
132         }
133     }
134 }
135 
136 /*-************************************************************************************************************//**
137     @short      ask user for his decision
138     @descr      We show the dialog and if user finish it with "OK" - we try to find selected item in internal saved
139                 name list (which you must set in "ChangeFilters()"!). If we return sal_True as result, you can use out
140                 parameter "pSelectedItem" as pointer into your FilterNameList to get selected item really ...
141                 but if we return sal_False ... user hsa cancel the dialog ... you shouldn't do that. pSelectedItem isn't
142                 set to any valid value then. We don't change them ...
143 
144     @seealso    method ChangeFilters()
145 
146     @param      "pSelectedItem", returns result of selection as pointer into set list of filter names
147                                  (valid for function return sal_True only!)
148     @return     true  => pSelectedItem parameter points into name list and represent use decision
149                 false => use has cancelled dialog (pSelectedItem isn't valid then!)
150 
151     @onerror    We return false ... but don't change pSelectedItem!
152     @threadsafe no
153 *//*-*************************************************************************************************************/
154 bool FilterDialog::AskForFilter( FilterNameListPtr& pSelectedItem )
155 {
156     bool bSelected = sal_False;
157 
158     if( m_pFilterNames != NULL )
159     {
160         if( ModalDialog::Execute() == RET_OK )
161         {
162             String sEntry = m_lbFilters.GetSelectEntry();
163             if( sEntry.Len() > 0 )
164             {
165                 int nPos = m_lbFilters.GetSelectEntryPos();
166                 if( nPos < (int)(m_pFilterNames->size()) )
167                 {
168                     pSelectedItem  = m_pFilterNames->begin();
169                     pSelectedItem += nPos;
170                     bSelected      = ( pSelectedItem != m_pFilterNames->end() );
171                 }
172             }
173         }
174     }
175 
176     return bSelected;
177 }
178 
179 /*-************************************************************************************************************//**
180     @short      helper class to calculate length of given string
181     @descr      Instances of it can be used as callback for INetURLObject::getAbbreviated() method to build
182                 short URLs to show it on GUI. We use in ctor set OutputDevice to call special VCL method ...
183 
184     @seealso    method OutputDevice::GetTextWidth()
185     @seealso    method InetURLObject::getAbbreviated()
186 
187     @param      -
188     @return     -
189 
190     @onerror    -
191     @threadsafe no
192 *//*-*************************************************************************************************************/
193 class StringCalculator : public ::cppu::WeakImplHelper1< ::com::sun::star::util::XStringWidth >
194 {
195 	public:
196         StringCalculator( const OutputDevice* pDevice )
197             : m_pDevice( pDevice )
198         {
199         }
200 
201         sal_Int32 SAL_CALL queryStringWidth( const ::rtl::OUString& sString ) throw( ::com::sun::star::uno::RuntimeException )
202 		{
203             return (sal_Int32)(m_pDevice->GetTextWidth(String(sString)));
204 		}
205 
206     private:
207         const OutputDevice* m_pDevice;
208 };
209 
210 /*-************************************************************************************************************//**
211     @short      try to build short name of given URL to show it n GUI
212     @descr      We detect type of given URL automatically and build this short name depend on this type ...
213                 If we couldnt make it right we return full given string without any changes ...
214 
215     @seealso    class LocalFileHelper
216     @seealso    method InetURLObject::getAbbreviated()
217 
218     @param      "sName", file name
219     @return     A short file name ...
220 
221     @onerror    We return given name without any changes.
222     @threadsafe no
223 *//*-*************************************************************************************************************/
224 String FilterDialog::impl_buildUIFileName( const String& sName )
225 {
226     String sShortName( sName );
227 
228     if( ::utl::LocalFileHelper::ConvertURLToSystemPath( sName, sShortName ) == sal_True )
229     {
230         // its a system file ... build short name by using osl functionality
231     }
232     else
233     {
234         // otherwise its really a url ... build short name by using INetURLObject
235         ::com::sun::star::uno::Reference< ::com::sun::star::util::XStringWidth > xStringCalculator( new StringCalculator(&m_ftURL) );
236         if( xStringCalculator.is() == sal_True )
237         {
238             INetURLObject aBuilder   ( sName );
239             Size          aSize      = m_ftURL.GetOutputSize();
240                           sShortName = aBuilder.getAbbreviated( xStringCalculator, aSize.Width(), INetURLObject::DECODE_UNAMBIGUOUS );
241         }
242     }
243 
244     return sShortName;
245 }
246 
247 }   // namespace uui
248