LibreOffice Module dbaccess (master) 1
opendoccontrols.cxx
Go to the documentation of this file.
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 <opendoccontrols.hxx>
21
22#include <com/sun/star/uno/Sequence.hxx>
23#include <com/sun/star/beans/PropertyValue.hpp>
24#include <com/sun/star/lang/XMultiServiceFactory.hpp>
25#include <com/sun/star/container/XNameAccess.hpp>
26#include <com/sun/star/ui/theModuleUIConfigurationManagerSupplier.hpp>
27#include <com/sun/star/ui/XUIConfigurationManager.hpp>
28#include <com/sun/star/graphic/XGraphic.hpp>
29#include <com/sun/star/ui/XImageManager.hpp>
30
35#include <tools/urlobj.hxx>
36#include <osl/diagnose.h>
37
38namespace dbaui
39{
40
41 namespace
42 {
43 using ::com::sun::star::uno::Reference;
44 using ::com::sun::star::uno::Exception;
45 using ::com::sun::star::uno::Sequence;
46 using ::com::sun::star::uno::XComponentContext;
47 using ::com::sun::star::container::XNameAccess;
48 using ::com::sun::star::beans::PropertyValue;
49 using ::com::sun::star::ui::theModuleUIConfigurationManagerSupplier;
50 using ::com::sun::star::ui::XModuleUIConfigurationManagerSupplier;
51 using ::com::sun::star::ui::XUIConfigurationManager;
52 using ::com::sun::star::ui::XImageManager;
53 using ::com::sun::star::graphic::XGraphic;
54
55 Reference< XGraphic> GetCommandIcon( const char* _pCommandURL, const OUString& _rModuleName )
56 {
57 if ( !_pCommandURL || !*_pCommandURL )
58 return nullptr;
59
60 OUString sCommandURL = OUString::createFromAscii( _pCommandURL );
61 try
62 {
63 do
64 {
65 // Retrieve popup menu labels
66 Reference< css::uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
67 if ( !xContext.is() )
68 break;
69
70 Reference< XModuleUIConfigurationManagerSupplier > xSupplier(
71 theModuleUIConfigurationManagerSupplier::get(xContext) );
72
73 Reference< XUIConfigurationManager > xManager( xSupplier->getUIConfigurationManager( _rModuleName ) );
74 Reference< XImageManager > xImageManager;
75 if ( xManager.is() )
76 xImageManager.set(xManager->getImageManager(), css::uno::UNO_QUERY);
77 if ( !xImageManager.is() )
78 break;
79
80 Sequence< OUString > aCommandList( &sCommandURL, 1 );
81 Sequence<Reference< XGraphic> > xIconList( xImageManager->getImages( 0, aCommandList ) );
82 if ( !xIconList.hasElements() )
83 break;
84
85 return xIconList[0];
86 }
87 while ( false );
88 }
89 catch ( Exception& ) {}
90
91 return nullptr;
92 }
93 }
94
95 // OpenButton
96
97 OpenDocumentButton::OpenDocumentButton(std::unique_ptr<weld::Button> xControl, const char* _pAsciiModuleName)
98 : m_xControl(std::move(xControl))
99 {
100 impl_init( _pAsciiModuleName );
101 }
102
103 void OpenDocumentButton::impl_init( const char* _pAsciiModuleName )
104 {
105 OSL_ENSURE( _pAsciiModuleName, "OpenDocumentButton::impl_init: invalid module name!" );
106 m_sModule = OUString::createFromAscii( _pAsciiModuleName );
107
108 // our label should equal the UI text of the "Open" command
111 m_xControl->set_label(" " + sLabel.replaceAll("~", ""));
112
113 // Place icon left of text and both centered in the button.
114 m_xControl->set_image(GetCommandIcon(".uno:Open", m_sModule));
115 }
116
117 // OpenDocumentListBox
118
119 OpenDocumentListBox::OpenDocumentListBox(std::unique_ptr<weld::ComboBox> xControl, const char* _pAsciiModuleName )
120 : m_xControl(std::move(xControl))
121 {
122 // we need to limit the max auto width feature of the filter box
123 int nWidth = m_xControl->get_approximate_digit_width() * 50;
124 m_xControl->set_size_request(nWidth, -1);
125
126 impl_init( _pAsciiModuleName );
127 }
128
129 void OpenDocumentListBox::impl_init( const char* _pAsciiModuleName )
130 {
131 OSL_ENSURE( _pAsciiModuleName, "OpenDocumentListBox::impl_init: invalid module name!" );
132
133 std::vector< SvtHistoryOptions::HistoryItem > aHistory = SvtHistoryOptions::GetList( EHistoryType::PickList );
134 Reference< XNameAccess > xFilterFactory;
135 xFilterFactory.set(::comphelper::getProcessServiceFactory()->createInstance(
136 "com.sun.star.document.FilterFactory" ), css::uno::UNO_QUERY);
137
138 for ( const SvtHistoryOptions::HistoryItem& rHistoryItem : aHistory )
139 {
140 try
141 {
142 // Get the current history item's properties.
143 OUString sURL = rHistoryItem.sURL;
144 OUString sFilter = rHistoryItem.sFilter;
145 OUString sTitle = rHistoryItem.sTitle;
146 OUString sPassword = rHistoryItem.sPassword;
147
148 // If the entry is a Base file then insert it into the
149 // history list and the list box.
151 xFilterFactory->getByName( sFilter ) >>= aProps;
152
153 ::comphelper::SequenceAsHashMap aFilterProperties( aProps );
154 OUString sDocumentService = aFilterProperties.getUnpackedValueOrDefault(
155 "DocumentService", OUString() );
156 if ( sDocumentService.equalsAscii( _pAsciiModuleName ) )
157 {
158 // yes, it's a Base document
160 aURL.SetSmartURL( sURL );
161 // The password is set only when it is not empty.
162 if ( !sPassword.isEmpty() )
163 aURL.SetPass( sPassword );
164
165 if ( sTitle.isEmpty() )
167
168 OUString sDecodedURL = aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE );
169
170 m_xControl->append_text(sTitle);
171 m_aURLs.emplace_back(sDecodedURL, sFilter);
172 }
173 }
174 catch( Exception& ) {}
175 }
176 }
177
179 {
180 OUString sURL;
181 sal_Int32 nSelected = m_xControl->get_active();
182 if (nSelected != -1)
183 sURL = impl_getDocumentAtIndex( nSelected ).first;
184 return sURL;
185 }
186
188 {
189 return m_aURLs[_nListIndex];
190 }
191
192} // namespace dbaui
193
194/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
PropertiesInfo aProperties
HRESULT createInstance(REFIID iid, Ifc **ppIfc)
constexpr OUStringLiteral sDocumentService
TValueType getUnpackedValueOrDefault(const OUString &sKey, const TValueType &aDefault) const
std::unique_ptr< weld::Button > m_xControl
OpenDocumentButton(std::unique_ptr< weld::Button > xControl, const char *_pAsciiModuleName)
void impl_init(const char *_pAsciiModuleName)
std::pair< OUString, OUString > StringPair
const StringPair & impl_getDocumentAtIndex(sal_uInt16 _nListIndex) const
std::vector< StringPair > m_aURLs
void impl_init(const char *_pAsciiModuleName)
OUString GetSelectedDocumentURL() const
std::unique_ptr< weld::ComboBox > m_xControl
OpenDocumentListBox(std::unique_ptr< weld::ComboBox > xControl, const char *_pAsciiModuleName)
URL aURL
Definition: intercept.cxx:87
std::vector< HistoryItem > GetList(EHistoryType eHistory)
@ Exception
Sequence< beans::PropertyValue > GetCommandProperties(const OUString &rsCommandName, const OUString &rsModuleName)
OUString GetLabelForCommand(const css::uno::Sequence< css::beans::PropertyValue > &rProperties)
Reference< XControl > m_xControl