KDEUI
kshortcutschemeshelper.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "kshortcutschemeshelper_p.h"
00020
00021 #include <QFile>
00022 #include <QTextStream>
00023 #include <QDomDocument>
00024
00025 #include <kconfiggroup.h>
00026 #include <kaction.h>
00027 #include <kstandarddirs.h>
00028 #include <kactioncollection.h>
00029 #include <kxmlguiclient.h>
00030 #include <kdebug.h>
00031
00032 bool KShortcutSchemesHelper::exportActionCollection(KActionCollection *collection,
00033 const QString &schemeName, const QString dir)
00034 {
00035 const KXMLGUIClient *client = collection->parentGUIClient();
00036 if (!client)
00037 return false;
00038
00039 QString schemeFileName;
00040 if (!dir.isEmpty())
00041 schemeFileName = dir + client->componentData().componentName() + schemeName + "shortcuts.rc";
00042 else
00043 schemeFileName = shortcutSchemeFileName(client, schemeName);
00044
00045 QFile schemeFile(schemeFileName);
00046 if (!schemeFile.open(QFile::WriteOnly | QFile::Truncate))
00047 {
00048 kDebug() << "COULD NOT WRITE" << schemeFileName;
00049 return false;
00050 }
00051
00052 QDomDocument doc;
00053 QDomElement docElem = doc.createElement("kpartgui");
00054 docElem.setAttribute("version", "1");
00055 docElem.setAttribute("name", client->componentData().componentName());
00056 doc.appendChild(docElem);
00057 QDomElement elem = doc.createElement("ActionProperties");
00058 docElem.appendChild(elem);
00059
00060
00061 foreach (QAction *action, collection->actions()) {
00062 KAction *kaction = qobject_cast<KAction*>(action);
00063 if (!kaction)
00064 continue;
00065
00066 QString actionName = kaction->objectName();
00067 QString shortcut = kaction->shortcut(KAction::ActiveShortcut).toString();
00068 if (!shortcut.isEmpty())
00069 {
00070 QDomElement act_elem = doc.createElement("Action");
00071 act_elem.setAttribute( "name", actionName );
00072 act_elem.setAttribute( "shortcut", shortcut );
00073 elem.appendChild(act_elem);
00074 }
00075 }
00076
00077 QTextStream out(&schemeFile);
00078 out << doc.toString(2);
00079 return true;
00080 }
00081
00082 QString KShortcutSchemesHelper::currentShortcutSchemeName()
00083 {
00084 return KGlobal::config()->group( "Shortcut Schemes" ).readEntry("Current Scheme", "Default");
00085 }
00086
00087 QString KShortcutSchemesHelper::shortcutSchemeFileName(const KXMLGUIClient *client, const QString &schemeName)
00088 {
00089 return KStandardDirs::locateLocal("data",
00090 client->componentData().componentName() + '/' +
00091 client->componentData().componentName() + schemeName + "shortcuts.rc" );
00092 }
00093
00094 QString KShortcutSchemesHelper::applicationShortcutSchemeFileName(const QString &schemeName)
00095 {
00096 return KGlobal::dirs()->locateLocal("appdata",
00097 KGlobal::mainComponent().componentName() + schemeName + "shortcuts.rc");
00098 }