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