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 necessary 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 : @threadsafe no
50 : *//*-*************************************************************************************************************/
51 0 : FilterDialog::FilterDialog( Window* pParentWindow ,
52 : ResMgr* pResMgr )
53 : : ModalDialog (pParentWindow, ResId( DLG_FILTER_SELECT, *pResMgr ) )
54 : , m_ftURL (this, ResId( FT_URL, *pResMgr))
55 : , m_lbFilters (this, ResId( LB_FILTERS, *pResMgr))
56 : , m_btnOK (this, ResId( BTN_OK, *pResMgr))
57 : , m_btnCancel (this, ResId( BTN_CANCEL, *pResMgr))
58 : , m_btnHelp (this, ResId( BTN_HELP, *pResMgr))
59 0 : , m_pFilterNames(NULL)
60 : {
61 0 : FreeResource();
62 0 : }
63 :
64 : /*-************************************************************************************************************
65 : @short set file name on dialog control
66 : @descr We convert given URL (it must be an URL!) into valid file name and show it on our dialog.
67 : @param "sURL", URL for showing
68 : @threadsafe no
69 : *//*-*************************************************************************************************************/
70 0 : void FilterDialog::SetURL( const OUString& sURL )
71 : {
72 : // convert it and use given pure string as fallback if conversion failed
73 0 : m_ftURL.SetText( impl_buildUIFileName(sURL) );
74 0 : }
75 :
76 : /*-************************************************************************************************************
77 : @short change list of filter names
78 : @descr We save given pointer internal and use it to fill our listbox with given names.
79 : Saved list pointer is used on method "AskForFilter()" too, to find user selected item
80 : and return pointer into these list as result of operation.
81 : So it's possible to call dialog again and again for different or same filter list
82 : and ask user for his decision by best performance!
83 :
84 : @attention Don't free memory of given list after this call till object will die ... or
85 : you call "ChangeFilters( NULL )"! Then we forget it too.
86 :
87 : @seealso method AskForFilter()
88 :
89 : @param "pFilterNames", pointer to list of filter names, which should be used for later operations.
90 : @onerror We clear list box and forget our currently set filter information completely!
91 : @threadsafe no
92 : *//*-*************************************************************************************************************/
93 0 : void FilterDialog::ChangeFilters( const FilterNameList* pFilterNames )
94 : {
95 0 : m_pFilterNames = pFilterNames;
96 0 : m_lbFilters.Clear();
97 0 : if( m_pFilterNames != NULL )
98 : {
99 0 : for( FilterNameListPtr pItem = m_pFilterNames->begin();
100 0 : pItem != m_pFilterNames->end() ;
101 : ++pItem )
102 : {
103 0 : m_lbFilters.InsertEntry( pItem->sUI );
104 : }
105 : }
106 0 : }
107 :
108 : /*-************************************************************************************************************
109 : @short ask user for his decision
110 : @descr We show the dialog and if user finish it with "OK" - we try to find selected item in internal saved
111 : name list (which you must set in "ChangeFilters()"!). If we return sal_True as result, you can use out
112 : parameter "pSelectedItem" as pointer into your FilterNameList to get selected item really ...
113 : but if we return sal_False ... user has cancel the dialog ... you should not do that. pSelectedItem isnt
114 : set to any valid value then. We don't change them ...
115 :
116 : @seealso method ChangeFilters()
117 :
118 : @param "pSelectedItem", returns result of selection as pointer into set list of filter names
119 : (valid for function return sal_True only!)
120 : @return true => pSelectedItem parameter points into name list and represent use decision
121 : false => use has cancelled dialog (pSelectedItem isnt valid then!)
122 :
123 : @onerror We return false ... but don't change pSelectedItem!
124 : @threadsafe no
125 : *//*-*************************************************************************************************************/
126 0 : bool FilterDialog::AskForFilter( FilterNameListPtr& pSelectedItem )
127 : {
128 0 : bool bSelected = false;
129 :
130 0 : if( m_pFilterNames != NULL )
131 : {
132 0 : if( ModalDialog::Execute() == RET_OK )
133 : {
134 0 : OUString sEntry = m_lbFilters.GetSelectEntry();
135 0 : if( !sEntry.isEmpty() )
136 : {
137 0 : int nPos = m_lbFilters.GetSelectEntryPos();
138 0 : if( nPos < (int)(m_pFilterNames->size()) )
139 : {
140 0 : pSelectedItem = m_pFilterNames->begin();
141 0 : pSelectedItem += nPos;
142 0 : bSelected = ( pSelectedItem != m_pFilterNames->end() );
143 : }
144 0 : }
145 : }
146 : }
147 :
148 0 : return bSelected;
149 : }
150 :
151 : /*-************************************************************************************************************
152 : @short helper class to calculate length of given string
153 : @descr Instances of it can be used as callback for INetURLObject::getAbbreviated() method to build
154 : short URLs to show it on GUI. We use in ctor set OutputDevice to call special VCL method ...
155 :
156 : @seealso method OutputDevice::GetTextWidth()
157 : @seealso method InetURLObject::getAbbreviated()
158 : @threadsafe no
159 : *//*-*************************************************************************************************************/
160 0 : class StringCalculator : public ::cppu::WeakImplHelper1< ::com::sun::star::util::XStringWidth >
161 : {
162 : public:
163 0 : StringCalculator( const OutputDevice* pDevice )
164 0 : : m_pDevice( pDevice )
165 : {
166 0 : }
167 :
168 0 : sal_Int32 SAL_CALL queryStringWidth( const OUString& sString ) throw( ::com::sun::star::uno::RuntimeException, std::exception ) SAL_OVERRIDE
169 : {
170 0 : return (sal_Int32)(m_pDevice->GetTextWidth(sString));
171 : }
172 :
173 : private:
174 : const OutputDevice* m_pDevice;
175 : };
176 :
177 : /*-************************************************************************************************************
178 : @short try to build short name of given URL to show it n GUI
179 : @descr We detect type of given URL automaticly and build this short name depend on this type ...
180 : If we couldnt make it right we return full given string without any changes ...
181 :
182 : @seealso class LocalFileHelper
183 : @seealso method InetURLObject::getAbbreviated()
184 :
185 : @param "sName", file name
186 : @return A short file name ...
187 :
188 : @onerror We return given name without any changes.
189 : @threadsafe no
190 : *//*-*************************************************************************************************************/
191 0 : OUString FilterDialog::impl_buildUIFileName( const OUString& sName )
192 : {
193 0 : OUString sShortName( sName );
194 :
195 0 : if( ::utl::LocalFileHelper::ConvertURLToSystemPath( sName, sShortName ) )
196 : {
197 : // it's a system file ... build short name by using osl functionality
198 : }
199 : else
200 : {
201 : // otherwise its really a url ... build short name by using INetURLObject
202 0 : ::com::sun::star::uno::Reference< ::com::sun::star::util::XStringWidth > xStringCalculator( new StringCalculator(&m_ftURL) );
203 0 : if( xStringCalculator.is() )
204 : {
205 0 : INetURLObject aBuilder ( sName );
206 0 : Size aSize = m_ftURL.GetOutputSize();
207 0 : sShortName = aBuilder.getAbbreviated( xStringCalculator, aSize.Width(), INetURLObject::DECODE_UNAMBIGUOUS );
208 0 : }
209 : }
210 :
211 0 : return sShortName;
212 : }
213 :
214 : } // namespace uui
215 :
216 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|