/src/libreoffice/uui/source/fltdlg.cxx
Line | Count | Source |
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 <com/sun/star/util/XStringWidth.hpp> |
23 | | #include <cppuhelper/implbase.hxx> |
24 | | #include <tools/urlobj.hxx> |
25 | | |
26 | | #include <osl/file.hxx> |
27 | | |
28 | | namespace uui |
29 | | { |
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. |
35 | | |
36 | | @seealso method SetFilters() |
37 | | @seealso method SetURL() |
38 | | |
39 | | @param "pParentWindow" , parent window for dialog |
40 | | @threadsafe no |
41 | | */ /*-*************************************************************************************************************/ |
42 | | FilterDialog::FilterDialog(weld::Window* pParentWindow) |
43 | 0 | : GenericDialogController(pParentWindow, u"uui/ui/filterselect.ui"_ustr, |
44 | 0 | u"FilterSelectDialog"_ustr) |
45 | 0 | , m_pFilterNames(nullptr) |
46 | 0 | , m_xFtURL(m_xBuilder->weld_label(u"url"_ustr)) |
47 | 0 | , m_xLbFilters(m_xBuilder->weld_tree_view(u"filters"_ustr)) |
48 | 0 | { |
49 | 0 | m_xLbFilters->set_size_request(m_xLbFilters->get_approximate_digit_width() * 42, |
50 | 0 | m_xLbFilters->get_height_rows(15)); |
51 | 0 | } |
52 | | |
53 | 0 | FilterDialog::~FilterDialog() {} |
54 | | |
55 | | /*-************************************************************************************************************ |
56 | | @short set file name on dialog control |
57 | | @descr We convert given URL (it must be a URL!) into valid file name and show it on our dialog. |
58 | | @param "sURL", URL for showing |
59 | | @threadsafe no |
60 | | */ /*-*************************************************************************************************************/ |
61 | | void FilterDialog::SetURL(const OUString& sURL) |
62 | 0 | { |
63 | | // convert it and use given pure string as fallback if conversion failed |
64 | 0 | m_xFtURL->set_label(impl_buildUIFileName(sURL)); |
65 | 0 | } |
66 | | |
67 | | /*-************************************************************************************************************ |
68 | | @short change list of filter names |
69 | | @descr We save given pointer internal and use it to fill our listbox with given names. |
70 | | Saved list pointer is used on method "AskForFilter()" too, to find user selected item |
71 | | and return pointer into these list as result of operation. |
72 | | So it's possible to call dialog again and again for different or same filter list |
73 | | and ask user for his decision by best performance! |
74 | | |
75 | | @attention Don't free memory of given list after this call till object will die ... or |
76 | | you call "ChangeFilters( NULL )"! Then we forget it too. |
77 | | |
78 | | @seealso method AskForFilter() |
79 | | |
80 | | @param "pFilterNames", pointer to list of filter names, which should be used for later operations. |
81 | | @onerror We clear list box and forget our currently set filter information completely! |
82 | | @threadsafe no |
83 | | */ /*-*************************************************************************************************************/ |
84 | | void FilterDialog::ChangeFilters(const FilterNameList* pFilterNames) |
85 | 0 | { |
86 | 0 | m_pFilterNames = pFilterNames; |
87 | 0 | m_xLbFilters->clear(); |
88 | 0 | if (m_pFilterNames != nullptr) |
89 | 0 | { |
90 | 0 | for (const auto& rItem : *m_pFilterNames) |
91 | 0 | { |
92 | 0 | m_xLbFilters->append_text(rItem.sUI); |
93 | 0 | } |
94 | 0 | } |
95 | 0 | } |
96 | | |
97 | | /*-************************************************************************************************************ |
98 | | @short ask user for his decision |
99 | | @descr We show the dialog and if user finish it with "OK" - we try to find selected item in internal saved |
100 | | name list (which you must set in "ChangeFilters()"!). If we return sal_True as result, you can use out |
101 | | parameter "pSelectedItem" as pointer into your FilterNameList to get selected item really ... |
102 | | but if we return sal_False ... user has cancel the dialog ... you should not do that. pSelectedItem is not |
103 | | set to any valid value then. We don't change them ... |
104 | | |
105 | | @seealso method ChangeFilters() |
106 | | |
107 | | @param "pSelectedItem", returns result of selection as pointer into set list of filter names |
108 | | (valid for function return sal_True only!) |
109 | | @return true => pSelectedItem parameter points into name list and represent use decision |
110 | | false => use has cancelled dialog (pSelectedItem is not valid then!) |
111 | | |
112 | | @onerror We return false ... but don't change pSelectedItem! |
113 | | @threadsafe no |
114 | | */ /*-*************************************************************************************************************/ |
115 | | bool FilterDialog::AskForFilter(FilterNameListPtr& pSelectedItem) |
116 | 0 | { |
117 | 0 | bool bSelected = false; |
118 | |
|
119 | 0 | if (m_pFilterNames != nullptr) |
120 | 0 | { |
121 | 0 | if (m_xDialog->run() == RET_OK) |
122 | 0 | { |
123 | 0 | OUString sEntry = m_xLbFilters->get_selected_text(); |
124 | 0 | if (!sEntry.isEmpty()) |
125 | 0 | { |
126 | 0 | int nPos = m_xLbFilters->get_selected_index(); |
127 | 0 | if (nPos < static_cast<int>(m_pFilterNames->size())) |
128 | 0 | { |
129 | 0 | pSelectedItem = m_pFilterNames->begin(); |
130 | 0 | pSelectedItem += nPos; |
131 | 0 | bSelected = (pSelectedItem != m_pFilterNames->end()); |
132 | 0 | } |
133 | 0 | } |
134 | 0 | } |
135 | 0 | } |
136 | |
|
137 | 0 | return bSelected; |
138 | 0 | } |
139 | | |
140 | | namespace |
141 | | { |
142 | | /*-************************************************************************************************************ |
143 | | @short helper class to calculate length of given string |
144 | | @descr Instances of it can be used as callback for INetURLObject::getAbbreviated() method to build |
145 | | short URLs to show it on GUI. We use in ctor set OutputDevice to call special VCL method ... |
146 | | |
147 | | @seealso method OutputDevice::GetTextWidth() |
148 | | @seealso method InetURLObject::getAbbreviated() |
149 | | @threadsafe no |
150 | | */ /*-*************************************************************************************************************/ |
151 | | class StringCalculator : public ::cppu::WeakImplHelper<css::util::XStringWidth> |
152 | | { |
153 | | public: |
154 | | explicit StringCalculator(weld::Widget* pDevice) |
155 | 0 | : m_pDevice(pDevice) |
156 | 0 | { |
157 | 0 | } |
158 | | |
159 | | sal_Int32 SAL_CALL queryStringWidth(const OUString& sString) override |
160 | 0 | { |
161 | 0 | return static_cast<sal_Int32>(m_pDevice->get_pixel_size(sString).Width()); |
162 | 0 | } |
163 | | |
164 | | private: |
165 | | weld::Widget* m_pDevice; |
166 | | }; |
167 | | } |
168 | | |
169 | | /*-************************************************************************************************************ |
170 | | @short try to build short name of given URL to show it n GUI |
171 | | @descr We detect type of given URL automatically and build this short name depend on this type ... |
172 | | If we couldn't make it right we return full given string without any changes ... |
173 | | |
174 | | @seealso method InetURLObject::getAbbreviated() |
175 | | |
176 | | @param "sName", file name |
177 | | @return A short file name ... |
178 | | |
179 | | @onerror We return given name without any changes. |
180 | | @threadsafe no |
181 | | */ /*-*************************************************************************************************************/ |
182 | | OUString FilterDialog::impl_buildUIFileName(const OUString& sName) |
183 | 0 | { |
184 | 0 | OUString sShortName(sName); |
185 | |
|
186 | 0 | if (osl::FileBase::getSystemPathFromFileURL(sName, sShortName) == osl::FileBase::E_None) |
187 | | |
188 | 0 | { |
189 | | // it's a system file... build short name by using osl functionality |
190 | 0 | } |
191 | 0 | else |
192 | 0 | { |
193 | | // otherwise it's really a URL... build short name by using INetURLObject |
194 | 0 | css::uno::Reference<css::util::XStringWidth> xStringCalculator( |
195 | 0 | new StringCalculator(m_xFtURL.get())); |
196 | 0 | if (xStringCalculator.is()) |
197 | 0 | { |
198 | 0 | INetURLObject aBuilder(sName); |
199 | 0 | Size aSize = m_xLbFilters->get_preferred_size(); |
200 | 0 | sShortName = aBuilder.getAbbreviated(xStringCalculator, aSize.Width(), |
201 | 0 | INetURLObject::DecodeMechanism::Unambiguous); |
202 | 0 | } |
203 | 0 | } |
204 | |
|
205 | 0 | return sShortName; |
206 | 0 | } |
207 | | |
208 | | } // namespace uui |
209 | | |
210 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |