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