1*6d739b60SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*6d739b60SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*6d739b60SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*6d739b60SAndrew Rist  * distributed with this work for additional information
6*6d739b60SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*6d739b60SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*6d739b60SAndrew Rist  * "License"); you may not use this file except in compliance
9*6d739b60SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*6d739b60SAndrew Rist  *
11*6d739b60SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*6d739b60SAndrew Rist  *
13*6d739b60SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*6d739b60SAndrew Rist  * software distributed under the License is distributed on an
15*6d739b60SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*6d739b60SAndrew Rist  * KIND, either express or implied.  See the License for the
17*6d739b60SAndrew Rist  * specific language governing permissions and limitations
18*6d739b60SAndrew Rist  * under the License.
19*6d739b60SAndrew Rist  *
20*6d739b60SAndrew Rist  *************************************************************/
21*6d739b60SAndrew Rist 
22*6d739b60SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_framework.hxx"
26cdf0e10cSrcweir #include <xml/acceleratorconfigurationwriter.hxx>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir //_______________________________________________
29cdf0e10cSrcweir // own includes
30cdf0e10cSrcweir #include <acceleratorconst.h>
31cdf0e10cSrcweir #include <threadhelp/readguard.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir //_______________________________________________
34cdf0e10cSrcweir // interface includes
35cdf0e10cSrcweir #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
36cdf0e10cSrcweir #include <com/sun/star/xml/sax/XAttributeList.hpp>
37cdf0e10cSrcweir #include <com/sun/star/awt/KeyModifier.hpp>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir //_______________________________________________
40cdf0e10cSrcweir // other includes
41cdf0e10cSrcweir #include <vcl/svapp.hxx>
42cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
43cdf0e10cSrcweir 
44cdf0e10cSrcweir #include <comphelper/attributelist.hxx>
45cdf0e10cSrcweir 
46cdf0e10cSrcweir 
47cdf0e10cSrcweir //_______________________________________________
48cdf0e10cSrcweir // namespace
49cdf0e10cSrcweir 
50cdf0e10cSrcweir namespace framework{
51cdf0e10cSrcweir 
52cdf0e10cSrcweir 
53cdf0e10cSrcweir //-----------------------------------------------
54cdf0e10cSrcweir AcceleratorConfigurationWriter::AcceleratorConfigurationWriter(const AcceleratorCache&                                       rContainer,
55cdf0e10cSrcweir                                                                const css::uno::Reference< css::xml::sax::XDocumentHandler >& xConfig   )
56cdf0e10cSrcweir     : ThreadHelpBase(&Application::GetSolarMutex())
57cdf0e10cSrcweir     , m_xConfig     (xConfig                      )
58cdf0e10cSrcweir     , m_rContainer  (rContainer                   )
59cdf0e10cSrcweir {
60cdf0e10cSrcweir }
61cdf0e10cSrcweir 
62cdf0e10cSrcweir //-----------------------------------------------
63cdf0e10cSrcweir AcceleratorConfigurationWriter::~AcceleratorConfigurationWriter()
64cdf0e10cSrcweir {
65cdf0e10cSrcweir }
66cdf0e10cSrcweir 
67cdf0e10cSrcweir //-----------------------------------------------
68cdf0e10cSrcweir void AcceleratorConfigurationWriter::flush()
69cdf0e10cSrcweir {
70cdf0e10cSrcweir     // SAFE -> ----------------------------------
71cdf0e10cSrcweir     ReadGuard aReadLock(m_aLock);
72cdf0e10cSrcweir 
73cdf0e10cSrcweir     css::uno::Reference< css::xml::sax::XDocumentHandler >         xCFG        = m_xConfig;
74cdf0e10cSrcweir     css::uno::Reference< css::xml::sax::XExtendedDocumentHandler > xExtendedCFG(m_xConfig, css::uno::UNO_QUERY_THROW);
75cdf0e10cSrcweir 
76cdf0e10cSrcweir     aReadLock.unlock();
77cdf0e10cSrcweir     // <- SAFE ----------------------------------
78cdf0e10cSrcweir 
79cdf0e10cSrcweir     // prepare attribute list
80cdf0e10cSrcweir     ::comphelper::AttributeList* pAttribs = new ::comphelper::AttributeList;
81cdf0e10cSrcweir     css::uno::Reference< css::xml::sax::XAttributeList > xAttribs(static_cast< css::xml::sax::XAttributeList* >(pAttribs), css::uno::UNO_QUERY);
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 	pAttribs->AddAttribute(AL_XMLNS_ACCEL, ATTRIBUTE_TYPE_CDATA, NS_XMLNS_ACCEL);
84cdf0e10cSrcweir 	pAttribs->AddAttribute(AL_XMLNS_XLINK, ATTRIBUTE_TYPE_CDATA, NS_XMLNS_XLINK);
85cdf0e10cSrcweir 
86cdf0e10cSrcweir     // generate xml
87cdf0e10cSrcweir 	xCFG->startDocument();
88cdf0e10cSrcweir 
89cdf0e10cSrcweir     xExtendedCFG->unknown(DOCTYPE_ACCELERATORS);
90cdf0e10cSrcweir     xCFG->ignorableWhitespace(::rtl::OUString());
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 	xCFG->startElement(AL_ELEMENT_ACCELERATORLIST, xAttribs);
93cdf0e10cSrcweir 	xCFG->ignorableWhitespace(::rtl::OUString());
94cdf0e10cSrcweir 
95cdf0e10cSrcweir     // TODO think about threadsafe using of cache
96cdf0e10cSrcweir     AcceleratorCache::TKeyList                 lKeys = m_rContainer.getAllKeys();
97cdf0e10cSrcweir     AcceleratorCache::TKeyList::const_iterator pKey  ;
98cdf0e10cSrcweir     for (  pKey  = lKeys.begin();
99cdf0e10cSrcweir            pKey != lKeys.end()  ;
100cdf0e10cSrcweir          ++pKey                 )
101cdf0e10cSrcweir     {
102cdf0e10cSrcweir         const css::awt::KeyEvent& rKey     = *pKey;
103cdf0e10cSrcweir         const ::rtl::OUString&    rCommand = m_rContainer.getCommandByKey(rKey);
104cdf0e10cSrcweir         impl_ts_writeKeyCommandPair(rKey, rCommand, xCFG);
105cdf0e10cSrcweir     }
106cdf0e10cSrcweir 
107cdf0e10cSrcweir     /* TODO write key-command list
108cdf0e10cSrcweir     std::vector< SfxAcceleratorConfigItem>::const_iterator p;
109cdf0e10cSrcweir 	for ( p = m_aWriteAcceleratorList.begin(); p != m_aWriteAcceleratorList.end(); p++ )
110cdf0e10cSrcweir 		WriteAcceleratorItem( *p );
111cdf0e10cSrcweir     */
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 	xCFG->ignorableWhitespace(::rtl::OUString());
114cdf0e10cSrcweir 	xCFG->endElement(AL_ELEMENT_ACCELERATORLIST);
115cdf0e10cSrcweir 	xCFG->ignorableWhitespace(::rtl::OUString());
116cdf0e10cSrcweir 	xCFG->endDocument();
117cdf0e10cSrcweir }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir //-----------------------------------------------
120cdf0e10cSrcweir void AcceleratorConfigurationWriter::impl_ts_writeKeyCommandPair(const css::awt::KeyEvent&                                     aKey    ,
121cdf0e10cSrcweir                                                                  const ::rtl::OUString&                                        sCommand,
122cdf0e10cSrcweir                                                                  const css::uno::Reference< css::xml::sax::XDocumentHandler >& xConfig )
123cdf0e10cSrcweir {
124cdf0e10cSrcweir 	::comphelper::AttributeList* pAttribs = new ::comphelper::AttributeList;
125cdf0e10cSrcweir 	css::uno::Reference< css::xml::sax::XAttributeList > xAttribs (static_cast< css::xml::sax::XAttributeList* >(pAttribs) , css::uno::UNO_QUERY_THROW);
126cdf0e10cSrcweir 
127cdf0e10cSrcweir     ::rtl::OUString sKey = m_rKeyMapping->mapCodeToIdentifier(aKey.KeyCode);
128cdf0e10cSrcweir     // TODO check if key is empty!
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 	pAttribs->AddAttribute(AL_ATTRIBUTE_KEYCODE, ATTRIBUTE_TYPE_CDATA, sKey    );
131cdf0e10cSrcweir 	pAttribs->AddAttribute(AL_ATTRIBUTE_URL    , ATTRIBUTE_TYPE_CDATA, sCommand);
132cdf0e10cSrcweir 
133cdf0e10cSrcweir     if ((aKey.Modifiers & css::awt::KeyModifier::SHIFT) == css::awt::KeyModifier::SHIFT)
134cdf0e10cSrcweir         pAttribs->AddAttribute(AL_ATTRIBUTE_MOD_SHIFT, ATTRIBUTE_TYPE_CDATA, ::rtl::OUString::createFromAscii("true"));
135cdf0e10cSrcweir 
136cdf0e10cSrcweir     if ((aKey.Modifiers & css::awt::KeyModifier::MOD1) == css::awt::KeyModifier::MOD1)
137cdf0e10cSrcweir         pAttribs->AddAttribute(AL_ATTRIBUTE_MOD_MOD1, ATTRIBUTE_TYPE_CDATA, ::rtl::OUString::createFromAscii("true"));
138cdf0e10cSrcweir 
139cdf0e10cSrcweir     if ((aKey.Modifiers & css::awt::KeyModifier::MOD2) == css::awt::KeyModifier::MOD2)
140cdf0e10cSrcweir         pAttribs->AddAttribute(AL_ATTRIBUTE_MOD_MOD2, ATTRIBUTE_TYPE_CDATA, ::rtl::OUString::createFromAscii("true"));
141cdf0e10cSrcweir 
142cdf0e10cSrcweir     if ((aKey.Modifiers & css::awt::KeyModifier::MOD3) == css::awt::KeyModifier::MOD3)
143cdf0e10cSrcweir         pAttribs->AddAttribute(AL_ATTRIBUTE_MOD_MOD3, ATTRIBUTE_TYPE_CDATA, ::rtl::OUString::createFromAscii("true"));
144cdf0e10cSrcweir 
145cdf0e10cSrcweir 	xConfig->ignorableWhitespace(::rtl::OUString());
146cdf0e10cSrcweir 	xConfig->startElement(AL_ELEMENT_ITEM, xAttribs);
147cdf0e10cSrcweir 	xConfig->ignorableWhitespace(::rtl::OUString());
148cdf0e10cSrcweir 	xConfig->endElement(AL_ELEMENT_ITEM);
149cdf0e10cSrcweir 	xConfig->ignorableWhitespace(::rtl::OUString());
150cdf0e10cSrcweir }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir } // namespace framework
153