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 .
20 #include <sal/config.h>
22 #include <accelerators/keymapping.hxx>
23 #include <xml/acceleratorconfigurationwriter.hxx>
25 #include <acceleratorconst.h>
27 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
28 #include <com/sun/star/xml/sax/XAttributeList.hpp>
29 #include <com/sun/star/awt/KeyModifier.hpp>
31 #include <vcl/svapp.hxx>
32 #include <rtl/ustrbuf.hxx>
34 #include <comphelper/attributelist.hxx>
38 AcceleratorConfigurationWriter::AcceleratorConfigurationWriter(const AcceleratorCache
& rContainer
,
39 const css::uno::Reference
< css::xml::sax::XDocumentHandler
>& xConfig
)
40 : m_xConfig (xConfig
)
41 , m_rContainer (rContainer
)
45 AcceleratorConfigurationWriter::~AcceleratorConfigurationWriter()
49 void AcceleratorConfigurationWriter::flush()
51 css::uno::Reference
< css::xml::sax::XExtendedDocumentHandler
> xExtendedCFG(m_xConfig
, css::uno::UNO_QUERY_THROW
);
53 // prepare attribute list
54 ::comphelper::AttributeList
* pAttribs
= new ::comphelper::AttributeList
;
55 css::uno::Reference
< css::xml::sax::XAttributeList
> xAttribs(static_cast< css::xml::sax::XAttributeList
* >(pAttribs
), css::uno::UNO_QUERY
);
57 pAttribs
->AddAttribute(
58 "xmlns:accel", ATTRIBUTE_TYPE_CDATA
,
59 "http://openoffice.org/2001/accel");
60 pAttribs
->AddAttribute(
61 "xmlns:xlink", ATTRIBUTE_TYPE_CDATA
, "http://www.w3.org/1999/xlink");
64 xExtendedCFG
->startDocument();
66 xExtendedCFG
->unknown(
67 "<!DOCTYPE accel:acceleratorlist PUBLIC \"-//OpenOffice.org//DTD"
68 " OfficeDocument 1.0//EN\" \"accelerator.dtd\">");
69 xExtendedCFG
->ignorableWhitespace(OUString());
71 xExtendedCFG
->startElement(AL_ELEMENT_ACCELERATORLIST
, xAttribs
);
72 xExtendedCFG
->ignorableWhitespace(OUString());
74 // TODO think about threadsafe using of cache
75 AcceleratorCache::TKeyList lKeys
= m_rContainer
.getAllKeys();
76 for (auto const& lKey
: lKeys
)
78 const OUString
& rCommand
= m_rContainer
.getCommandByKey(lKey
);
79 impl_ts_writeKeyCommandPair(lKey
, rCommand
, xExtendedCFG
);
82 /* TODO write key-command list
83 for (auto const& writeAccelerator : m_aWriteAcceleratorList)
84 WriteAcceleratorItem(writeAccelerator);
87 xExtendedCFG
->ignorableWhitespace(OUString());
88 xExtendedCFG
->endElement(AL_ELEMENT_ACCELERATORLIST
);
89 xExtendedCFG
->ignorableWhitespace(OUString());
90 xExtendedCFG
->endDocument();
93 void AcceleratorConfigurationWriter::impl_ts_writeKeyCommandPair(const css::awt::KeyEvent
& aKey
,
94 const OUString
& sCommand
,
95 const css::uno::Reference
< css::xml::sax::XDocumentHandler
>& xConfig
)
97 ::comphelper::AttributeList
* pAttribs
= new ::comphelper::AttributeList
;
98 css::uno::Reference
< css::xml::sax::XAttributeList
> xAttribs (static_cast< css::xml::sax::XAttributeList
* >(pAttribs
) , css::uno::UNO_QUERY_THROW
);
100 OUString sKey
= KeyMapping::get().mapCodeToIdentifier(aKey
.KeyCode
);
101 // TODO check if key is empty!
103 pAttribs
->AddAttribute("accel:code", ATTRIBUTE_TYPE_CDATA
, sKey
);
104 pAttribs
->AddAttribute("xlink:href", ATTRIBUTE_TYPE_CDATA
, sCommand
);
106 if ((aKey
.Modifiers
& css::awt::KeyModifier::SHIFT
) == css::awt::KeyModifier::SHIFT
)
107 pAttribs
->AddAttribute("accel:shift", ATTRIBUTE_TYPE_CDATA
, "true");
109 if ((aKey
.Modifiers
& css::awt::KeyModifier::MOD1
) == css::awt::KeyModifier::MOD1
)
110 pAttribs
->AddAttribute("accel:mod1", ATTRIBUTE_TYPE_CDATA
, "true");
112 if ((aKey
.Modifiers
& css::awt::KeyModifier::MOD2
) == css::awt::KeyModifier::MOD2
)
113 pAttribs
->AddAttribute("accel:mod2", ATTRIBUTE_TYPE_CDATA
, "true");
115 if ((aKey
.Modifiers
& css::awt::KeyModifier::MOD3
) == css::awt::KeyModifier::MOD3
)
116 pAttribs
->AddAttribute("accel:mod3", ATTRIBUTE_TYPE_CDATA
, "true");
118 xConfig
->ignorableWhitespace(OUString());
119 xConfig
->startElement(AL_ELEMENT_ITEM
, xAttribs
);
120 xConfig
->ignorableWhitespace(OUString());
121 xConfig
->endElement(AL_ELEMENT_ITEM
);
122 xConfig
->ignorableWhitespace(OUString());
125 } // namespace framework
127 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */