KDEUI
kshortcutschemeshelper.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2008 Alexander Dymo <adymo@kdevelop.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 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 // now, iterate through our actions 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 }
KDE 4.6 API Reference