Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "fltdlg.hxx"
21 :
22 : #include "ids.hrc"
23 :
24 : #include "fltdlg.hrc"
25 :
26 : #include <com/sun/star/util/XStringWidth.hpp>
27 : #include <cppuhelper/implbase1.hxx>
28 : #include <unotools/localfilehelper.hxx>
29 : #include <tools/urlobj.hxx>
30 :
31 : #include <vcl/button.hxx>
32 : #include <osl/mutex.hxx>
33 : #include <vcl/svapp.hxx>
34 :
35 : namespace uui
36 : {
37 :
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.
43 :
44 : @seealso method SetFilters()
45 : @seealso method SetURL()
46 :
47 : @param "pParentWindow" , parent window for dialog
48 : @param "pResMgr" , resource manager
49 : @return -
50 :
51 : @onerror -
52 : @threadsafe no
53 : *//*-*************************************************************************************************************/
54 0 : 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 0 : , m_pFilterNames(NULL)
63 : {
64 0 : FreeResource();
65 0 : }
66 :
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.
70 :
71 : @seealso -
72 :
73 : @param "sURL", URL for showing
74 : @return -
75 :
76 : @onerror -
77 : @threadsafe no
78 : *//*-*************************************************************************************************************/
79 0 : void FilterDialog::SetURL( const String& sURL )
80 : {
81 : // convert it and use given pure string as fallback if conversion failed
82 0 : m_ftURL.SetText( impl_buildUIFileName(sURL) );
83 0 : }
84 :
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!
92 :
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.
95 :
96 : @seealso method AskForFilter()
97 :
98 : @param "pFilterNames", pointer to list of filter names, which should be used for later operations.
99 : @return -
100 :
101 : @onerror We clear list box and forget our currently set filter information completely!
102 : @threadsafe no
103 : *//*-*************************************************************************************************************/
104 0 : void FilterDialog::ChangeFilters( const FilterNameList* pFilterNames )
105 : {
106 0 : m_pFilterNames = pFilterNames;
107 0 : m_lbFilters.Clear();
108 0 : if( m_pFilterNames != NULL )
109 : {
110 0 : for( FilterNameListPtr pItem = m_pFilterNames->begin();
111 0 : pItem != m_pFilterNames->end() ;
112 : ++pItem )
113 : {
114 0 : m_lbFilters.InsertEntry( pItem->sUI );
115 : }
116 : }
117 0 : }
118 :
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 ...
126 :
127 : @seealso method ChangeFilters()
128 :
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!)
133 :
134 : @onerror We return false ... but don't change pSelectedItem!
135 : @threadsafe no
136 : *//*-*************************************************************************************************************/
137 0 : bool FilterDialog::AskForFilter( FilterNameListPtr& pSelectedItem )
138 : {
139 0 : bool bSelected = sal_False;
140 :
141 0 : if( m_pFilterNames != NULL )
142 : {
143 0 : if( ModalDialog::Execute() == RET_OK )
144 : {
145 0 : String sEntry = m_lbFilters.GetSelectEntry();
146 0 : if( sEntry.Len() > 0 )
147 : {
148 0 : int nPos = m_lbFilters.GetSelectEntryPos();
149 0 : if( nPos < (int)(m_pFilterNames->size()) )
150 : {
151 0 : pSelectedItem = m_pFilterNames->begin();
152 0 : pSelectedItem += nPos;
153 0 : bSelected = ( pSelectedItem != m_pFilterNames->end() );
154 : }
155 0 : }
156 : }
157 : }
158 :
159 0 : return bSelected;
160 : }
161 :
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 ...
166 :
167 : @seealso method OutputDevice::GetTextWidth()
168 : @seealso method InetURLObject::getAbbreviated()
169 :
170 : @param -
171 : @return -
172 :
173 : @onerror -
174 : @threadsafe no
175 : *//*-*************************************************************************************************************/
176 0 : class StringCalculator : public ::cppu::WeakImplHelper1< ::com::sun::star::util::XStringWidth >
177 : {
178 : public:
179 0 : StringCalculator( const OutputDevice* pDevice )
180 0 : : m_pDevice( pDevice )
181 : {
182 0 : }
183 :
184 0 : sal_Int32 SAL_CALL queryStringWidth( const OUString& sString ) throw( ::com::sun::star::uno::RuntimeException )
185 : {
186 0 : return (sal_Int32)(m_pDevice->GetTextWidth(String(sString)));
187 : }
188 :
189 : private:
190 : const OutputDevice* m_pDevice;
191 : };
192 :
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 ...
197 :
198 : @seealso class LocalFileHelper
199 : @seealso method InetURLObject::getAbbreviated()
200 :
201 : @param "sName", file name
202 : @return A short file name ...
203 :
204 : @onerror We return given name without any changes.
205 : @threadsafe no
206 : *//*-*************************************************************************************************************/
207 0 : String FilterDialog::impl_buildUIFileName( const String& sName )
208 : {
209 0 : OUString sShortName( sName );
210 :
211 0 : if( ::utl::LocalFileHelper::ConvertURLToSystemPath( sName, sShortName ) == sal_True )
212 : {
213 : // its a system file ... build short name by using osl functionality
214 : }
215 : else
216 : {
217 : // otherwise its really a url ... build short name by using INetURLObject
218 0 : ::com::sun::star::uno::Reference< ::com::sun::star::util::XStringWidth > xStringCalculator( new StringCalculator(&m_ftURL) );
219 0 : if( xStringCalculator.is() == sal_True )
220 : {
221 0 : INetURLObject aBuilder ( sName );
222 0 : Size aSize = m_ftURL.GetOutputSize();
223 0 : sShortName = aBuilder.getAbbreviated( xStringCalculator, aSize.Width(), INetURLObject::DECODE_UNAMBIGUOUS );
224 0 : }
225 : }
226 :
227 0 : return sShortName;
228 : }
229 :
230 36 : } // namespace uui
231 :
232 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|