1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
22 #include <com/sun/star/util/XStringWidth.hpp>
23 #include <cppuhelper/implbase.hxx>
24 #include <tools/urlobj.hxx>
26 #include <osl/file.hxx>
30 /*-************************************************************************************************************
31 @short initialize filter dialog with start values
32 @descr We set some necessary information on these instance for later working and create internal structures.
33 After construction user should call "SetFilters()" and "SetURL()" to fill listbox with selectable filter
34 names and set file name of file, which should be used for selected filter.
36 @seealso method SetFilters()
37 @seealso method SetURL()
39 @param "pParentWindow" , parent window for dialog
41 */ /*-*************************************************************************************************************/
42 FilterDialog::FilterDialog(weld::Window
* pParentWindow
)
43 : GenericDialogController(pParentWindow
, "uui/ui/filterselect.ui", "FilterSelectDialog")
44 , m_pFilterNames(nullptr)
45 , m_xFtURL(m_xBuilder
->weld_label("url"))
46 , m_xLbFilters(m_xBuilder
->weld_tree_view("filters"))
48 m_xLbFilters
->set_size_request(m_xLbFilters
->get_approximate_digit_width() * 42,
49 m_xLbFilters
->get_height_rows(15));
52 FilterDialog::~FilterDialog() {}
54 /*-************************************************************************************************************
55 @short set file name on dialog control
56 @descr We convert given URL (it must be a URL!) into valid file name and show it on our dialog.
57 @param "sURL", URL for showing
59 */ /*-*************************************************************************************************************/
60 void FilterDialog::SetURL(const OUString
& sURL
)
62 // convert it and use given pure string as fallback if conversion failed
63 m_xFtURL
->set_label(impl_buildUIFileName(sURL
));
66 /*-************************************************************************************************************
67 @short change list of filter names
68 @descr We save given pointer internal and use it to fill our listbox with given names.
69 Saved list pointer is used on method "AskForFilter()" too, to find user selected item
70 and return pointer into these list as result of operation.
71 So it's possible to call dialog again and again for different or same filter list
72 and ask user for his decision by best performance!
74 @attention Don't free memory of given list after this call till object will die ... or
75 you call "ChangeFilters( NULL )"! Then we forget it too.
77 @seealso method AskForFilter()
79 @param "pFilterNames", pointer to list of filter names, which should be used for later operations.
80 @onerror We clear list box and forget our currently set filter information completely!
82 */ /*-*************************************************************************************************************/
83 void FilterDialog::ChangeFilters(const FilterNameList
* pFilterNames
)
85 m_pFilterNames
= pFilterNames
;
86 m_xLbFilters
->clear();
87 if (m_pFilterNames
!= nullptr)
89 for (const auto& rItem
: *m_pFilterNames
)
91 m_xLbFilters
->append_text(rItem
.sUI
);
96 /*-************************************************************************************************************
97 @short ask user for his decision
98 @descr We show the dialog and if user finish it with "OK" - we try to find selected item in internal saved
99 name list (which you must set in "ChangeFilters()"!). If we return sal_True as result, you can use out
100 parameter "pSelectedItem" as pointer into your FilterNameList to get selected item really ...
101 but if we return sal_False ... user has cancel the dialog ... you should not do that. pSelectedItem is not
102 set to any valid value then. We don't change them ...
104 @seealso method ChangeFilters()
106 @param "pSelectedItem", returns result of selection as pointer into set list of filter names
107 (valid for function return sal_True only!)
108 @return true => pSelectedItem parameter points into name list and represent use decision
109 false => use has cancelled dialog (pSelectedItem is not valid then!)
111 @onerror We return false ... but don't change pSelectedItem!
113 */ /*-*************************************************************************************************************/
114 bool FilterDialog::AskForFilter(FilterNameListPtr
& pSelectedItem
)
116 bool bSelected
= false;
118 if (m_pFilterNames
!= nullptr)
120 if (m_xDialog
->run() == RET_OK
)
122 OUString sEntry
= m_xLbFilters
->get_selected_text();
123 if (!sEntry
.isEmpty())
125 int nPos
= m_xLbFilters
->get_selected_index();
126 if (nPos
< static_cast<int>(m_pFilterNames
->size()))
128 pSelectedItem
= m_pFilterNames
->begin();
129 pSelectedItem
+= nPos
;
130 bSelected
= (pSelectedItem
!= m_pFilterNames
->end());
141 /*-************************************************************************************************************
142 @short helper class to calculate length of given string
143 @descr Instances of it can be used as callback for INetURLObject::getAbbreviated() method to build
144 short URLs to show it on GUI. We use in ctor set OutputDevice to call special VCL method ...
146 @seealso method OutputDevice::GetTextWidth()
147 @seealso method InetURLObject::getAbbreviated()
149 */ /*-*************************************************************************************************************/
150 class StringCalculator
: public ::cppu::WeakImplHelper
<css::util::XStringWidth
>
153 explicit StringCalculator(weld::Widget
* pDevice
)
158 sal_Int32 SAL_CALL
queryStringWidth(const OUString
& sString
) override
160 return static_cast<sal_Int32
>(m_pDevice
->get_pixel_size(sString
).Width());
164 weld::Widget
* m_pDevice
;
168 /*-************************************************************************************************************
169 @short try to build short name of given URL to show it n GUI
170 @descr We detect type of given URL automatically and build this short name depend on this type ...
171 If we couldn't make it right we return full given string without any changes ...
173 @seealso method InetURLObject::getAbbreviated()
175 @param "sName", file name
176 @return A short file name ...
178 @onerror We return given name without any changes.
180 */ /*-*************************************************************************************************************/
181 OUString
FilterDialog::impl_buildUIFileName(const OUString
& sName
)
183 OUString
sShortName(sName
);
185 if (osl::FileBase::getSystemPathFromFileURL(sName
, sShortName
) == osl::FileBase::E_None
)
188 // it's a system file... build short name by using osl functionality
192 // otherwise it's really a URL... build short name by using INetURLObject
193 css::uno::Reference
<css::util::XStringWidth
> xStringCalculator(
194 new StringCalculator(m_xFtURL
.get()));
195 if (xStringCalculator
.is())
197 INetURLObject
aBuilder(sName
);
198 Size aSize
= m_xLbFilters
->get_preferred_size();
199 sShortName
= aBuilder
.getAbbreviated(xStringCalculator
, aSize
.Width(),
200 INetURLObject::DecodeMechanism::Unambiguous
);
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */