2 * genshortcutents.cpp - Copyright 2005 Frerich Raabe <raabe@kde.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #include "kaboutdata.h"
26 #include "kcomponentdata.h"
27 #include "kcmdlineargs.h"
29 #include "kstandardshortcut.h"
31 #include <QtCore/QDate>
32 #include <QtCore/QFile>
33 #include <QtCore/QCoreApplication>
34 #include <QtCore/QTextStream>
35 #include <QtCore/QStringList>
37 static QString
keyAsMarkup( const QString
&key
)
39 if ( key
== "Alt" || key
== "Ctrl" || key
== "Enter" || key
== "Esc" ||
40 key
== "Shift" || key
== "Tab" ) {
41 return QString('&' + key
+ ';');
43 if ( key
== "Left" || key
== "Right" || key
== "Up" || key
== "Down" ) {
44 return QString("<keysym>" + key
+ " Arrow</keysym>");
46 if ( key
== "Backspace" || key
== "Menu" ) {
47 return QString("<keysym>" + key
+ "</keysym>");
49 if ( key
== "Plus" ) {
50 return QString("<keycap>+</keycap>");
52 if ( key
== "Minus" ) {
53 return QString("<keycap>-</keycap>");
55 return QString("<keycap>" + key
+ "</keycap>");
58 static QString
entityForAccel( KStandardShortcut::StandardShortcut accel
)
60 QString markup
= "<!ENTITY KeyCombo_";
62 /* We use a switch so that the compiler gives us warning messages in
63 * case KStandardShortcut::StandardShortcut gets extended (which means we have to
64 * extend this code, too).
67 case KStandardShortcut::AccelNone
:
69 case KStandardShortcut::Open
:
72 case KStandardShortcut::New
:
75 case KStandardShortcut::Close
:
78 case KStandardShortcut::Save
:
81 case KStandardShortcut::Print
:
84 case KStandardShortcut::Quit
:
87 case KStandardShortcut::Undo
:
90 case KStandardShortcut::Redo
:
93 case KStandardShortcut::Cut
:
96 case KStandardShortcut::Copy
:
99 case KStandardShortcut::Paste
:
102 case KStandardShortcut::SelectAll
:
103 markup
+= "SelectAll";
105 case KStandardShortcut::Deselect
:
106 markup
+= "Deselect";
108 case KStandardShortcut::DeleteWordBack
:
109 markup
+= "DeleteWordBack";
111 case KStandardShortcut::DeleteWordForward
:
112 markup
+= "DeleteWordForward";
114 case KStandardShortcut::Find
:
117 case KStandardShortcut::FindNext
:
118 markup
+= "FindNext";
120 case KStandardShortcut::FindPrev
:
121 markup
+= "FindPrev";
123 case KStandardShortcut::Replace
:
126 case KStandardShortcut::Home
:
129 case KStandardShortcut::Begin
:
132 case KStandardShortcut::End
:
135 case KStandardShortcut::Prior
:
138 case KStandardShortcut::Next
:
141 case KStandardShortcut::GotoLine
:
142 markup
+= "GotoLine";
144 case KStandardShortcut::AddBookmark
:
145 markup
+= "AddBookmark";
147 case KStandardShortcut::ZoomIn
:
150 case KStandardShortcut::ZoomOut
:
153 case KStandardShortcut::Up
:
156 case KStandardShortcut::Back
:
159 case KStandardShortcut::Forward
:
162 case KStandardShortcut::Reload
:
165 case KStandardShortcut::ShowMenubar
:
166 markup
+= "ShowMenubar";
168 case KStandardShortcut::Help
:
171 case KStandardShortcut::WhatsThis
:
172 markup
+= "WhatsThis";
174 case KStandardShortcut::TextCompletion
:
175 markup
+= "TextCompletion";
177 case KStandardShortcut::PrevCompletion
:
178 markup
+= "PrevCompletion";
180 case KStandardShortcut::NextCompletion
:
181 markup
+= "NextCompletion";
183 case KStandardShortcut::SubstringCompletion
:
184 markup
+= "SubstringCompletion";
186 case KStandardShortcut::RotateUp
:
187 markup
+= "RotateUp";
189 case KStandardShortcut::RotateDown
:
190 markup
+= "RotateDown";
192 case KStandardShortcut::TabNext
:
195 case KStandardShortcut::TabPrev
:
198 case KStandardShortcut::FullScreen
:
199 markup
+= "FullScreen";
201 case KStandardShortcut::BackwardWord
:
202 markup
+= "BackwardWord";
204 case KStandardShortcut::ForwardWord
:
205 markup
+= "ForwardWord";
207 case KStandardShortcut::BeginningOfLine
:
208 markup
+= "BeginningOfLine";
210 case KStandardShortcut::EndOfLine
:
211 markup
+= "EndOfLine";
213 case KStandardShortcut::PasteSelection
:
214 markup
+= "PastSelection";
216 case KStandardShortcut::StandardShortcutCount
:
222 QString internalStr
= KStandardShortcut::shortcut( accel
).toString();
223 QString firstSequence
= internalStr
.left( internalStr
.indexOf( ';' ) );
224 const QStringList keys
= firstSequence
.split( '+',QString::SkipEmptyParts
);
225 if ( keys
.empty() ) {
229 if ( keys
.count() == 1 ) {
230 if ( keys
.first().startsWith( "XF86" ) ) {
233 markup
+= keyAsMarkup( keys
.first() );
235 markup
+= "<keycombo action=\"Simul\">";
236 foreach( const QString
&key
, keys
) {
237 if ( key
.startsWith( "XF86" ) ) {
240 markup
+= keyAsMarkup( key
);
242 markup
+= "</keycombo>";
248 int main( int argc
, char **argv
)
250 KCmdLineOptions cmdLineOptions
;
251 cmdLineOptions
.add("o");
252 cmdLineOptions
.add("output <file>", ki18n("Output file"), "kde-standard-accels.entities");
254 KAboutData
aboutData( "genshortcutents", 0, ki18n("genshortcutents"), "1.0",
255 ki18n( "Generates DocBook entities for key shortcuts of standard actions" ));
257 KCmdLineArgs::init( argc
, argv
, &aboutData
);
258 KCmdLineArgs::addCmdLineOptions( cmdLineOptions
);
260 KComponentData
componentData( &aboutData
);
261 QCoreApplication
app( KCmdLineArgs::qtArgc(), KCmdLineArgs::qtArgv() );
263 KCmdLineArgs
*args
= KCmdLineArgs::parsedArgs();
265 const QString outputFileName
= args
->getOption( "output" );
266 QFile
outputFile( outputFileName
);
267 if ( !outputFile
.open( QIODevice::WriteOnly
) ) {
268 qDebug( "Failed to open %s for writing.", qPrintable( outputFileName
) );
272 QTextStream
stream( &outputFile
);
273 stream
<< "<!-- Generated by "
274 << aboutData
.programName() + ' '
275 << aboutData
.version() + " on "
276 << QDateTime::currentDateTime().toString() + ".\n";
277 stream
<< " Do not bother to fiddle with this, your modifications will be\n"
278 << " overwritten as soon as the file is regenerated. -->\n";
281 for ( unsigned int i
= 0; i
< KStandardShortcut::StandardShortcutCount
; ++i
) {
282 stream
<< entityForAccel( static_cast<KStandardShortcut::StandardShortcut
>(i
) ) << "\n";