bump product version to 4.1.6.2
[LibreOffice.git] / uui / source / fltdlg.cxx
blob8da56bebb2dda61231b8b43370ea09abffbcc73b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "fltdlg.hxx"
22 #include "ids.hrc"
24 #include "fltdlg.hrc"
26 #include <com/sun/star/util/XStringWidth.hpp>
27 #include <cppuhelper/implbase1.hxx>
28 #include <unotools/localfilehelper.hxx>
29 #include <tools/urlobj.hxx>
31 #include <vcl/button.hxx>
32 #include <osl/mutex.hxx>
33 #include <vcl/svapp.hxx>
35 namespace uui
38 /*-************************************************************************************************************//**
39 @short initialize filter dialog with start values
40 @descr We set some neccessary information on these instance for later working and create internal structures.
41 After construction user should call "SetFilters()" and "SetURL()" to fill listbox with selectable filter
42 names and set file name of file, which should be used for selected filter.
44 @seealso method SetFilters()
45 @seealso method SetURL()
47 @param "pParentWindow" , parent window for dialog
48 @param "pResMgr" , resource manager
49 @return -
51 @onerror -
52 @threadsafe no
53 *//*-*************************************************************************************************************/
54 FilterDialog::FilterDialog( Window* pParentWindow ,
55 ResMgr* pResMgr )
56 : ModalDialog (pParentWindow, ResId( DLG_FILTER_SELECT, *pResMgr ) )
57 , m_ftURL (this, ResId( FT_URL, *pResMgr))
58 , m_lbFilters (this, ResId( LB_FILTERS, *pResMgr))
59 , m_btnOK (this, ResId( BTN_OK, *pResMgr))
60 , m_btnCancel (this, ResId( BTN_CANCEL, *pResMgr))
61 , m_btnHelp (this, ResId( BTN_HELP, *pResMgr))
62 , m_pFilterNames(NULL)
64 FreeResource();
67 /*-************************************************************************************************************//**
68 @short set file name on dialog control
69 @descr We convert given URL (it must be an URL!) into valid file name and show it on our dialog.
71 @seealso -
73 @param "sURL", URL for showing
74 @return -
76 @onerror -
77 @threadsafe no
78 *//*-*************************************************************************************************************/
79 void FilterDialog::SetURL( const String& sURL )
81 // convert it and use given pure string as fallback if conversion failed
82 m_ftURL.SetText( impl_buildUIFileName(sURL) );
85 /*-************************************************************************************************************//**
86 @short change list of filter names
87 @descr We save given pointer internal and use it to fill our listbox with given names.
88 Saved list pointer is used on method "AskForFilter()" too, to find user selected item
89 and return pointer into these list as result of operation.
90 So it's possible to call dialog again and again for different or same filter list
91 and ask user for his decision by best performance!
93 @attention Don't free memory of given list after this call till object will die ... or
94 you call "ChangeFilters( NULL )"! Then we forget it too.
96 @seealso method AskForFilter()
98 @param "pFilterNames", pointer to list of filter names, which should be used for later operations.
99 @return -
101 @onerror We clear list box and forget our currently set filter information completely!
102 @threadsafe no
103 *//*-*************************************************************************************************************/
104 void FilterDialog::ChangeFilters( const FilterNameList* pFilterNames )
106 m_pFilterNames = pFilterNames;
107 m_lbFilters.Clear();
108 if( m_pFilterNames != NULL )
110 for( FilterNameListPtr pItem = m_pFilterNames->begin();
111 pItem != m_pFilterNames->end() ;
112 ++pItem )
114 m_lbFilters.InsertEntry( pItem->sUI );
119 /*-************************************************************************************************************//**
120 @short ask user for his decision
121 @descr We show the dialog and if user finish it with "OK" - we try to find selected item in internal saved
122 name list (which you must set in "ChangeFilters()"!). If we return sal_True as result, you can use out
123 parameter "pSelectedItem" as pointer into your FilterNameList to get selected item realy ...
124 but if we return sal_False ... user has cancel the dialog ... you shouldnt do that. pSelectedItem isnt
125 set to any valid value then. We don't change them ...
127 @seealso method ChangeFilters()
129 @param "pSelectedItem", returns result of selection as pointer into set list of filter names
130 (valid for function return sal_True only!)
131 @return true => pSelectedItem parameter points into name list and represent use decision
132 false => use has cancelled dialog (pSelectedItem isnt valid then!)
134 @onerror We return false ... but don't change pSelectedItem!
135 @threadsafe no
136 *//*-*************************************************************************************************************/
137 bool FilterDialog::AskForFilter( FilterNameListPtr& pSelectedItem )
139 bool bSelected = sal_False;
141 if( m_pFilterNames != NULL )
143 if( ModalDialog::Execute() == RET_OK )
145 String sEntry = m_lbFilters.GetSelectEntry();
146 if( sEntry.Len() > 0 )
148 int nPos = m_lbFilters.GetSelectEntryPos();
149 if( nPos < (int)(m_pFilterNames->size()) )
151 pSelectedItem = m_pFilterNames->begin();
152 pSelectedItem += nPos;
153 bSelected = ( pSelectedItem != m_pFilterNames->end() );
159 return bSelected;
162 /*-************************************************************************************************************//**
163 @short helper class to calculate length of given string
164 @descr Instances of it can be used as callback for INetURLObject::getAbbreviated() method to build
165 short URLs to show it on GUI. We use in ctor set OutputDevice to call special VCL method ...
167 @seealso method OutputDevice::GetTextWidth()
168 @seealso method InetURLObject::getAbbreviated()
170 @param -
171 @return -
173 @onerror -
174 @threadsafe no
175 *//*-*************************************************************************************************************/
176 class StringCalculator : public ::cppu::WeakImplHelper1< ::com::sun::star::util::XStringWidth >
178 public:
179 StringCalculator( const OutputDevice* pDevice )
180 : m_pDevice( pDevice )
184 sal_Int32 SAL_CALL queryStringWidth( const OUString& sString ) throw( ::com::sun::star::uno::RuntimeException )
186 return (sal_Int32)(m_pDevice->GetTextWidth(String(sString)));
189 private:
190 const OutputDevice* m_pDevice;
193 /*-************************************************************************************************************//**
194 @short try to build short name of given URL to show it n GUI
195 @descr We detect type of given URL automaticly and build this short name depend on this type ...
196 If we couldnt make it right we return full given string without any changes ...
198 @seealso class LocalFileHelper
199 @seealso method InetURLObject::getAbbreviated()
201 @param "sName", file name
202 @return A short file name ...
204 @onerror We return given name without any changes.
205 @threadsafe no
206 *//*-*************************************************************************************************************/
207 String FilterDialog::impl_buildUIFileName( const String& sName )
209 OUString sShortName( sName );
211 if( ::utl::LocalFileHelper::ConvertURLToSystemPath( sName, sShortName ) == sal_True )
213 // its a system file ... build short name by using osl functionality
215 else
217 // otherwise its really a url ... build short name by using INetURLObject
218 ::com::sun::star::uno::Reference< ::com::sun::star::util::XStringWidth > xStringCalculator( new StringCalculator(&m_ftURL) );
219 if( xStringCalculator.is() == sal_True )
221 INetURLObject aBuilder ( sName );
222 Size aSize = m_ftURL.GetOutputSize();
223 sShortName = aBuilder.getAbbreviated( xStringCalculator, aSize.Width(), INetURLObject::DECODE_UNAMBIGUOUS );
227 return sShortName;
230 } // namespace uui
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */