1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_framework.hxx"
30 #include <xml/acceleratorconfigurationwriter.hxx>
31 
32 //_______________________________________________
33 // own includes
34 #include <acceleratorconst.h>
35 #include <threadhelp/readguard.hxx>
36 
37 //_______________________________________________
38 // interface includes
39 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
40 #include <com/sun/star/xml/sax/XAttributeList.hpp>
41 #include <com/sun/star/awt/KeyModifier.hpp>
42 
43 //_______________________________________________
44 // other includes
45 #include <vcl/svapp.hxx>
46 #include <rtl/ustrbuf.hxx>
47 
48 #include <comphelper/attributelist.hxx>
49 
50 
51 //_______________________________________________
52 // namespace
53 
54 namespace framework{
55 
56 
57 //-----------------------------------------------
58 AcceleratorConfigurationWriter::AcceleratorConfigurationWriter(const AcceleratorCache&                                       rContainer,
59                                                                const css::uno::Reference< css::xml::sax::XDocumentHandler >& xConfig   )
60     : ThreadHelpBase(&Application::GetSolarMutex())
61     , m_xConfig     (xConfig                      )
62     , m_rContainer  (rContainer                   )
63 {
64 }
65 
66 //-----------------------------------------------
67 AcceleratorConfigurationWriter::~AcceleratorConfigurationWriter()
68 {
69 }
70 
71 //-----------------------------------------------
72 void AcceleratorConfigurationWriter::flush()
73 {
74     // SAFE -> ----------------------------------
75     ReadGuard aReadLock(m_aLock);
76 
77     css::uno::Reference< css::xml::sax::XDocumentHandler >         xCFG        = m_xConfig;
78     css::uno::Reference< css::xml::sax::XExtendedDocumentHandler > xExtendedCFG(m_xConfig, css::uno::UNO_QUERY_THROW);
79 
80     aReadLock.unlock();
81     // <- SAFE ----------------------------------
82 
83     // prepare attribute list
84     ::comphelper::AttributeList* pAttribs = new ::comphelper::AttributeList;
85     css::uno::Reference< css::xml::sax::XAttributeList > xAttribs(static_cast< css::xml::sax::XAttributeList* >(pAttribs), css::uno::UNO_QUERY);
86 
87 	pAttribs->AddAttribute(AL_XMLNS_ACCEL, ATTRIBUTE_TYPE_CDATA, NS_XMLNS_ACCEL);
88 	pAttribs->AddAttribute(AL_XMLNS_XLINK, ATTRIBUTE_TYPE_CDATA, NS_XMLNS_XLINK);
89 
90     // generate xml
91 	xCFG->startDocument();
92 
93     xExtendedCFG->unknown(DOCTYPE_ACCELERATORS);
94     xCFG->ignorableWhitespace(::rtl::OUString());
95 
96 	xCFG->startElement(AL_ELEMENT_ACCELERATORLIST, xAttribs);
97 	xCFG->ignorableWhitespace(::rtl::OUString());
98 
99     // TODO think about threadsafe using of cache
100     AcceleratorCache::TKeyList                 lKeys = m_rContainer.getAllKeys();
101     AcceleratorCache::TKeyList::const_iterator pKey  ;
102     for (  pKey  = lKeys.begin();
103            pKey != lKeys.end()  ;
104          ++pKey                 )
105     {
106         const css::awt::KeyEvent& rKey     = *pKey;
107         const ::rtl::OUString&    rCommand = m_rContainer.getCommandByKey(rKey);
108         impl_ts_writeKeyCommandPair(rKey, rCommand, xCFG);
109     }
110 
111     /* TODO write key-command list
112     std::vector< SfxAcceleratorConfigItem>::const_iterator p;
113 	for ( p = m_aWriteAcceleratorList.begin(); p != m_aWriteAcceleratorList.end(); p++ )
114 		WriteAcceleratorItem( *p );
115     */
116 
117 	xCFG->ignorableWhitespace(::rtl::OUString());
118 	xCFG->endElement(AL_ELEMENT_ACCELERATORLIST);
119 	xCFG->ignorableWhitespace(::rtl::OUString());
120 	xCFG->endDocument();
121 }
122 
123 //-----------------------------------------------
124 void AcceleratorConfigurationWriter::impl_ts_writeKeyCommandPair(const css::awt::KeyEvent&                                     aKey    ,
125                                                                  const ::rtl::OUString&                                        sCommand,
126                                                                  const css::uno::Reference< css::xml::sax::XDocumentHandler >& xConfig )
127 {
128 	::comphelper::AttributeList* pAttribs = new ::comphelper::AttributeList;
129 	css::uno::Reference< css::xml::sax::XAttributeList > xAttribs (static_cast< css::xml::sax::XAttributeList* >(pAttribs) , css::uno::UNO_QUERY_THROW);
130 
131     ::rtl::OUString sKey = m_rKeyMapping->mapCodeToIdentifier(aKey.KeyCode);
132     // TODO check if key is empty!
133 
134 	pAttribs->AddAttribute(AL_ATTRIBUTE_KEYCODE, ATTRIBUTE_TYPE_CDATA, sKey    );
135 	pAttribs->AddAttribute(AL_ATTRIBUTE_URL    , ATTRIBUTE_TYPE_CDATA, sCommand);
136 
137     if ((aKey.Modifiers & css::awt::KeyModifier::SHIFT) == css::awt::KeyModifier::SHIFT)
138         pAttribs->AddAttribute(AL_ATTRIBUTE_MOD_SHIFT, ATTRIBUTE_TYPE_CDATA, ::rtl::OUString::createFromAscii("true"));
139 
140     if ((aKey.Modifiers & css::awt::KeyModifier::MOD1) == css::awt::KeyModifier::MOD1)
141         pAttribs->AddAttribute(AL_ATTRIBUTE_MOD_MOD1, ATTRIBUTE_TYPE_CDATA, ::rtl::OUString::createFromAscii("true"));
142 
143     if ((aKey.Modifiers & css::awt::KeyModifier::MOD2) == css::awt::KeyModifier::MOD2)
144         pAttribs->AddAttribute(AL_ATTRIBUTE_MOD_MOD2, ATTRIBUTE_TYPE_CDATA, ::rtl::OUString::createFromAscii("true"));
145 
146     if ((aKey.Modifiers & css::awt::KeyModifier::MOD3) == css::awt::KeyModifier::MOD3)
147         pAttribs->AddAttribute(AL_ATTRIBUTE_MOD_MOD3, ATTRIBUTE_TYPE_CDATA, ::rtl::OUString::createFromAscii("true"));
148 
149 	xConfig->ignorableWhitespace(::rtl::OUString());
150 	xConfig->startElement(AL_ELEMENT_ITEM, xAttribs);
151 	xConfig->ignorableWhitespace(::rtl::OUString());
152 	xConfig->endElement(AL_ELEMENT_ITEM);
153 	xConfig->ignorableWhitespace(::rtl::OUString());
154 }
155 
156 } // namespace framework
157