LibreOffice Module sfx2 (master) 1
classificationcontroller.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
12#include <com/sun/star/lang/XServiceInfo.hpp>
13#include <com/sun/star/document/XDocumentProperties.hpp>
14
15#include <com/sun/star/uno/XComponentContext.hpp>
16#include <com/sun/star/frame/XFrame.hpp>
17
21#include <sfx2/objsh.hxx>
22#include <sfx2/strings.hrc>
23#include <sfx2/sfxresid.hxx>
24#include <vcl/event.hxx>
25#include <vcl/toolbox.hxx>
26#include <vcl/svapp.hxx>
27#include <vcl/vclptr.hxx>
28#include <vcl/weld.hxx>
29
34
35using namespace com::sun::star;
36
37namespace sfx2
38{
39
40namespace {
41
42class ClassificationCategoriesController;
43
44}
45
47
48namespace {
49
51class ClassificationPropertyListener : public ClassificationPropertyListenerBase
52{
53 ClassificationCategoriesController& m_rController;
54
55public:
56 ClassificationPropertyListener(const rtl::Reference<comphelper::ConfigurationListener>& xListener, ClassificationCategoriesController& rController);
57 void setProperty(const uno::Any& rProperty) override;
58};
59
60}
61
62using ClassificationCategoriesControllerBase = cppu::ImplInheritanceHelper<svt::ToolboxController, lang::XServiceInfo>;
63
64namespace {
65
66class ClassificationControl;
67
69class ClassificationCategoriesController : public ClassificationCategoriesControllerBase
70{
73 ClassificationPropertyListener m_aPropertyListener;
74
75 DECL_LINK(SelectHdl, weld::ComboBox&, void);
76
77public:
78 explicit ClassificationCategoriesController(const uno::Reference<uno::XComponentContext>& rContext);
79
80 // XServiceInfo
81 OUString SAL_CALL getImplementationName() override;
82 sal_Bool SAL_CALL supportsService(const OUString& rServiceName) override;
83 uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
84
85 // XComponent
86 void SAL_CALL dispose() override;
87
88 // XToolbarController
89 uno::Reference<awt::XWindow> SAL_CALL createItemWindow(const uno::Reference<awt::XWindow>& rParent) override;
90
91 // XStatusListener
92 void SAL_CALL statusChanged(const frame::FeatureStateEvent& rEvent) override;
93
94 void removeEntries();
95};
96
98class SAL_WARN_UNUSED ClassificationControl final : public InterimItemWindow
99{
100 std::unique_ptr<weld::Label> m_xLabel;
101 std::unique_ptr<weld::ComboBox> m_xCategory;
102
103 DECL_LINK(KeyInputHdl, const KeyEvent&, bool);
104
105 void SetOptimalSize();
106 void DataChanged(const DataChangedEvent& rEvent) override;
107
108public:
109 explicit ClassificationControl(vcl::Window* pParent);
110 ~ClassificationControl() override;
111 void dispose() override;
112 weld::ComboBox& getCategory()
113 {
114 return *m_xCategory;
115 }
116 void set_sensitive(bool bSensitive)
117 {
118 Enable(bSensitive);
119 m_xContainer->set_sensitive(bSensitive);
120 }
121 static sfx::ClassificationCreationOrigin getExistingClassificationOrigin();
122 void toggleInteractivityOnOrigin();
123 void setCategoryStateFromPolicy(const SfxClassificationHelper & rHelper);
124};
125
126OUString const & getCategoryType()
127{
129}
130
131} // end anonymous namespace
132
133ClassificationPropertyListener::ClassificationPropertyListener(const rtl::Reference<comphelper::ConfigurationListener>& xListener, ClassificationCategoriesController& rController)
134 : ClassificationPropertyListenerBase(xListener, "WritePath")
135 , m_rController(rController)
136{
137}
138
139void ClassificationPropertyListener::setProperty(const uno::Any& /*rProperty*/)
140{
141 // So that its gets re-filled with entries from the new policy.
142 m_rController.removeEntries();
143}
144
145ClassificationCategoriesController::ClassificationCategoriesController(const uno::Reference<uno::XComponentContext>& rContext)
146 : ClassificationCategoriesControllerBase(rContext, uno::Reference<frame::XFrame>(), OUString(".uno:ClassificationApply"))
147 , m_pClassification(nullptr)
148 , m_xListener(new comphelper::ConfigurationListener("/org.openoffice.Office.Paths/Paths/Classification"))
150{
151
152}
153
154OUString ClassificationCategoriesController::getImplementationName()
155{
156 return "com.sun.star.comp.sfx2.ClassificationCategoriesController";
157}
158
159sal_Bool ClassificationCategoriesController::supportsService(const OUString& rServiceName)
160{
161 return cppu::supportsService(this, rServiceName);
162}
163
164uno::Sequence<OUString> ClassificationCategoriesController::getSupportedServiceNames()
165{
166 return { "com.sun.star.frame.ToolbarController" };
167}
168
169void ClassificationCategoriesController::dispose()
170{
171 SolarMutexGuard aSolarMutexGuard;
172
174 m_pClassification.disposeAndClear();
175 m_aPropertyListener.dispose();
176 m_xListener->dispose();
177}
178
179uno::Reference<awt::XWindow> ClassificationCategoriesController::createItemWindow(const uno::Reference<awt::XWindow>& rParent)
180{
182 auto pToolbar = dynamic_cast<ToolBox*>(pParent.get());
183 if (pToolbar)
184 {
186 m_pClassification->getCategory().connect_changed(LINK(this, ClassificationCategoriesController, SelectHdl));
187 m_pClassification->Show();
188 }
189
191}
192
193IMPL_LINK(ClassificationCategoriesController, SelectHdl, weld::ComboBox&, rCategory, void)
194{
195 m_pClassification->toggleInteractivityOnOrigin();
196
197 if (ClassificationControl::getExistingClassificationOrigin() == sfx::ClassificationCreationOrigin::MANUAL)
198 {
199 SfxObjectShell* pObjectShell = SfxObjectShell::Current();
200 if (!pObjectShell)
201 return;
203 m_pClassification->setCategoryStateFromPolicy(aHelper);
204 }
205 else
206 {
207 OUString aEntry = rCategory.get_active_text();
208
209 const OUString& aType = getCategoryType();
210 uno::Sequence<beans::PropertyValue> aPropertyValues(comphelper::InitPropertySequence({
211 {"Name", uno::Any(aEntry)},
212 {"Type", uno::Any(aType)},
213 }));
214 comphelper::dispatchCommand(".uno:ClassificationApply", aPropertyValues);
215 }
216}
217
218void ClassificationCategoriesController::statusChanged(const frame::FeatureStateEvent& /*rEvent*/)
219{
221 return;
222
223 SfxObjectShell* pObjectShell = SfxObjectShell::Current();
224 if (!pObjectShell)
225 return;
226
228
229 //toggle if the pop-up is enabled/disabled
230 m_pClassification->toggleInteractivityOnOrigin();
231
232 // check if classification was set via the advanced dialog
233 if (ClassificationControl::getExistingClassificationOrigin() != sfx::ClassificationCreationOrigin::MANUAL)
234 {
235 weld::ComboBox& rCategories = m_pClassification->getCategory();
236 if (rCategories.get_count() == 0)
237 {
238 std::vector<OUString> aNames = aHelper.GetBACNames();
239 for (const OUString& rName : aNames)
240 rCategories.append_text(rName);
241 }
242 }
243
244 // Restore state based on the doc. model.
245 m_pClassification->setCategoryStateFromPolicy(aHelper);
246
247}
248
249void ClassificationCategoriesController::removeEntries()
250{
251 m_pClassification->getCategory().clear();
252}
253
254ClassificationControl::ClassificationControl(vcl::Window* pParent)
255 : InterimItemWindow(pParent, "sfx/ui/classificationbox.ui", "ClassificationBox")
256 , m_xLabel(m_xBuilder->weld_label("label"))
257 , m_xCategory(m_xBuilder->weld_combo_box("combobox"))
258{
259 InitControlBase(m_xCategory.get());
260
261 m_xCategory->connect_key_press(LINK(this, ClassificationControl, KeyInputHdl));
262
263 // WB_NOLABEL means here that the control won't be replaced with a label
264 // when it wouldn't fit the available space.
265 SetStyle(GetStyle() | WB_DIALOGCONTROL | WB_NOLABEL);
266
267 OUString aText;
269 {
271 aText = SfxResId(STR_CLASSIFIED_INTELLECTUAL_PROPERTY);
272 break;
274 aText = SfxResId(STR_CLASSIFIED_NATIONAL_SECURITY);
275 break;
277 aText = SfxResId(STR_CLASSIFIED_EXPORT_CONTROL);
278 break;
279 }
280
281 m_xLabel->set_label(aText);
282
283 // Same as SvxColorDockingWindow.
284 const Size aLogicalAttrSize(150, 0);
285 Size aSize(LogicToPixel(aLogicalAttrSize, MapMode(MapUnit::MapAppFont)));
286 m_xCategory->set_size_request(aSize.Width() - m_xLabel->get_preferred_size().Width(), -1);
287
288 SetOptimalSize();
289}
290
291IMPL_LINK(ClassificationControl, KeyInputHdl, const KeyEvent&, rKEvt, bool)
292{
293 return ChildKeyInput(rKEvt);
294}
295
296ClassificationControl::~ClassificationControl()
297{
298 disposeOnce();
299}
300
301void ClassificationControl::dispose()
302{
303 m_xLabel.reset();
304 m_xCategory.reset();
306}
307
308void ClassificationControl::SetOptimalSize()
309{
310 SetSizePixel(get_preferred_size());
311}
312
313void ClassificationControl::DataChanged(const DataChangedEvent& rEvent)
314{
315 if ((rEvent.GetType() == DataChangedEventType::SETTINGS) && (rEvent.GetFlags() & AllSettingsFlags::STYLE))
316 SetOptimalSize();
317
318 toggleInteractivityOnOrigin();
319
321}
322
323sfx::ClassificationCreationOrigin ClassificationControl::getExistingClassificationOrigin()
324{
325 SfxObjectShell* pObjectShell = SfxObjectShell::Current();
326 if (!pObjectShell)
328
329 uno::Reference<document::XDocumentProperties> xDocumentProperties = pObjectShell->getDocProperties();
330 uno::Reference<beans::XPropertyContainer> xPropertyContainer = xDocumentProperties->getUserDefinedProperties();
331
333 return sfx::getCreationOriginProperty(xPropertyContainer, aKeyCreator);
334}
335
336void ClassificationControl::toggleInteractivityOnOrigin()
337{
338 if (getExistingClassificationOrigin() == sfx::ClassificationCreationOrigin::MANUAL)
339 {
340 set_sensitive(false);
341 }
342 else
343 {
344 set_sensitive(true);
345 }
346}
347
348void ClassificationControl::setCategoryStateFromPolicy(const SfxClassificationHelper & rHelper)
349{
350 const OUString& rCategoryName = rHelper.GetBACName(SfxClassificationHelper::getPolicyType());
351 if (!rCategoryName.isEmpty())
352 {
353 getCategory().set_active_text(rCategoryName);
354 }
355}
356
357} // namespace sfx2
358
359extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface* com_sun_star_sfx2_ClassificationCategoriesController_get_implementation(uno::XComponentContext* pContext, const uno::Sequence<uno::Any>&)
360{
361 return cppu::acquire(new sfx2::ClassificationCategoriesController(pContext));
362}
363
364/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
PropertyValueVector_t aPropertyValues
DataChangedEventType GetType() const
AllSettingsFlags GetFlags() const
virtual void dispose() override
Shared code to handle Business Authorization Identification and Labeling Scheme (BAILS) properties.
static SfxClassificationPolicyType getPolicyType()
const OUString & GetBACName(SfxClassificationPolicyType eType) const
Get the currently selected category for eType.
static const OUString & policyTypeToString(SfxClassificationPolicyType eType)
Returns the string representation of a SfxClassificationPolicyType element.
css::uno::Reference< css::document::XDocumentProperties > getDocProperties() const
Definition: objmisc.cxx:161
static SAL_WARN_UNUSED_RESULT SfxObjectShell * Current()
Definition: objxtor.cxx:481
static css::uno::Reference< css::awt::XWindow > GetInterface(vcl::Window *pWindow)
static vcl::Window * GetWindow(const css::uno::Reference< css::awt::XWindow > &rxWindow)
reference_type * get() const
static VclPtr< reference_type > Create(Arg &&... arg)
std::unique_ptr< weld::ComboBox > m_xCategory
rtl::Reference< comphelper::ConfigurationListener > m_xListener
SAL_DLLPUBLIC_EXPORT uno::XInterface * com_sun_star_sfx2_ClassificationCategoriesController_get_implementation(uno::XComponentContext *pContext, const uno::Sequence< uno::Any > &)
VclPtr< ClassificationControl > m_pClassification
ClassificationCategoriesController & m_rController
ClassificationPropertyListener m_aPropertyListener
std::unique_ptr< weld::Label > m_xLabel
virtual void SAL_CALL dispose() override
virtual void DataChanged(const DataChangedEvent &rDCEvt)
void append_text(const OUString &rStr)
virtual int get_count() const=0
DECL_LINK(CheckNameHdl, SvxNameDialog &, bool)
Any aHelper
bool dispatchCommand(const OUString &rCommand, const uno::Reference< css::frame::XFrame > &rFrame, const css::uno::Sequence< css::beans::PropertyValue > &rArguments, const uno::Reference< css::frame::XDispatchResultListener > &rListener)
css::uno::Sequence< css::beans::PropertyValue > InitPropertySequence(::std::initializer_list< ::std::pair< OUString, css::uno::Any > > vInit)
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
Reference
comphelper::ConfigurationListenerProperty< OUString > ClassificationPropertyListenerBase
cppu::ImplInheritanceHelper< svt::ToolboxController, lang::XServiceInfo > ClassificationCategoriesControllerBase
IMPL_LINK(ClassificationControl, KeyInputHdl, const KeyEvent &, rKEvt, bool)
sfx::ClassificationCreationOrigin getCreationOriginProperty(uno::Reference< beans::XPropertyContainer > const &rxPropertyContainer, sfx::ClassificationKeyCreator const &rKeyCreator)
ClassificationCreationOrigin
Specifies the origin: either defined by the BAF policy or manual via. the advanced classification dia...
void dispose()
Reference< XNameAccess > m_xContainer
OUString SfxResId(TranslateId aId)
Definition: sfxresid.cxx:22
unsigned char sal_Bool
#define SAL_WARN_UNUSED