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