KDEUI
kshortcutschemeseditor.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 "kshortcutsdialog_p.h" 00020 00021 #include <QDir> 00022 #include <QLabel> 00023 #include <QMenu> 00024 #include <QFile> 00025 #include <QTextStream> 00026 #include <QtXml/QDomDocument> 00027 #include <QFileDialog> 00028 00029 #include <kcombobox.h> 00030 #include <kpushbutton.h> 00031 #include <kstandarddirs.h> 00032 #include <kactioncollection.h> 00033 #include <kmessagebox.h> 00034 #include <kxmlguiclient.h> 00035 #include <kinputdialog.h> 00036 00037 #include "kshortcutsdialog.h" 00038 #include "kshortcutschemeshelper_p.h" 00039 00040 KShortcutSchemesEditor::KShortcutSchemesEditor(KShortcutsDialog *parent) 00041 :QGroupBox(i18n("Shortcut Schemes"), parent), m_dialog(parent) 00042 { 00043 KConfigGroup group( KGlobal::config(), "Shortcut Schemes" ); 00044 00045 const QStringList schemeFiles = KGlobal::dirs()->findAllResources("appdata", "*shortcuts.rc"); 00046 QStringList schemes; 00047 schemes << "Default"; 00048 foreach (QString schemeFile, schemeFiles) 00049 { 00050 schemes << schemeFile.remove(QRegExp("^.*/"+KGlobal::mainComponent().componentName())). 00051 remove("shortcuts.rc"); 00052 } 00053 00054 QString currentScheme = group.readEntry("Current Scheme", "Default"); 00055 00056 QHBoxLayout *l = new QHBoxLayout(this); 00057 l->setMargin(0); 00058 00059 QLabel *schemesLabel = new QLabel(i18n("Current scheme:"), this); 00060 l->addWidget(schemesLabel); 00061 00062 m_schemesList = new KComboBox(this); 00063 m_schemesList->setEditable(false); 00064 m_schemesList->addItems(schemes); 00065 m_schemesList->setCurrentIndex(m_schemesList->findText(currentScheme)); 00066 schemesLabel->setBuddy(m_schemesList); 00067 l->addWidget(m_schemesList); 00068 00069 m_newScheme = new KPushButton(i18n("New...")); 00070 l->addWidget(m_newScheme); 00071 00072 m_deleteScheme = new KPushButton(i18n("Delete")); 00073 l->addWidget(m_deleteScheme); 00074 00075 KPushButton *moreActions = new KPushButton(i18n("More Actions")); 00076 l->addWidget(moreActions); 00077 00078 QMenu *moreActionsMenu = new QMenu(this); 00079 moreActionsMenu->addAction(i18n("Save as Scheme Defaults"), 00080 this, SLOT(saveAsDefaultsForScheme())); 00081 moreActionsMenu->addAction(i18n("Export Scheme..."), 00082 this, SLOT(exportShortcutsScheme())); 00083 00084 moreActions->setMenu(moreActionsMenu); 00085 00086 l->addStretch(1); 00087 00088 connect(m_schemesList, SIGNAL(activated(const QString&)), 00089 this, SIGNAL(shortcutsSchemeChanged(const QString&))); 00090 connect(m_newScheme, SIGNAL(clicked()), this, SLOT(newScheme())); 00091 connect(m_deleteScheme, SIGNAL(clicked()), this, SLOT(deleteScheme())); 00092 updateDeleteButton(); 00093 } 00094 00095 void KShortcutSchemesEditor::newScheme() 00096 { 00097 bool ok; 00098 const QString newName = KInputDialog::getText(i18n("Name for New Scheme"), 00099 i18n("Name for new scheme:"), i18n("New Scheme"), &ok,this); 00100 if (!ok ) 00101 return; 00102 00103 if (m_schemesList->findText(newName) != -1) 00104 { 00105 KMessageBox::sorry(this, i18n("A scheme with this name already exists.")); 00106 return; 00107 } 00108 00109 const QString newSchemeFileName = KShortcutSchemesHelper::applicationShortcutSchemeFileName(newName); 00110 00111 QFile schemeFile(newSchemeFileName); 00112 if (!schemeFile.open(QFile::WriteOnly | QFile::Truncate)) 00113 return; 00114 00115 QDomDocument doc; 00116 QDomElement docElem = doc.createElement("kpartgui"); 00117 doc.appendChild(docElem); 00118 QDomElement elem = doc.createElement("ActionProperties"); 00119 docElem.appendChild(elem); 00120 00121 QTextStream out(&schemeFile); 00122 out << doc.toString(4); 00123 00124 m_schemesList->addItem(newName); 00125 m_schemesList->setCurrentIndex(m_schemesList->findText(newName)); 00126 updateDeleteButton(); 00127 emit shortcutsSchemeChanged(newName); 00128 } 00129 00130 void KShortcutSchemesEditor::deleteScheme() 00131 { 00132 if (KMessageBox::questionYesNo(this, 00133 i18n("Do you really want to delete the scheme %1?\n\ 00134 Note that this will not remove any system wide shortcut schemes.", currentScheme())) == KMessageBox::No) 00135 return; 00136 00137 //delete the scheme for the app itself 00138 QFile::remove(KShortcutSchemesHelper::applicationShortcutSchemeFileName(currentScheme())); 00139 00140 //delete all scheme files we can find for xmlguiclients in the user directories 00141 foreach (KActionCollection *collection, m_dialog->actionCollections()) 00142 { 00143 const KXMLGUIClient *client = collection->parentGUIClient(); 00144 if (!client) 00145 continue; 00146 QFile::remove(KShortcutSchemesHelper::shortcutSchemeFileName(client, currentScheme())); 00147 } 00148 00149 m_schemesList->removeItem(m_schemesList->findText(currentScheme())); 00150 updateDeleteButton(); 00151 emit shortcutsSchemeChanged(currentScheme()); 00152 } 00153 00154 QString KShortcutSchemesEditor::currentScheme() 00155 { 00156 return m_schemesList->currentText(); 00157 } 00158 00159 void KShortcutSchemesEditor::exportShortcutsScheme() 00160 { 00161 //ask user about dir 00162 QString exportTo = QFileDialog::getExistingDirectory(this, i18n("Export to Location"), //krazy:exclude=qclasses it is not possible to use KDirSelectDialog here because kfile links against kdeui; the dialog gets replaced anyway with the KDE one at runtime 00163 QDir::currentPath()); 00164 if (exportTo.isEmpty()) 00165 return; 00166 00167 QDir schemeRoot(exportTo); 00168 00169 if (!schemeRoot.exists(exportTo)) 00170 { 00171 KMessageBox::error(this, i18n("Could not export shortcuts scheme because the location is invalid.")); 00172 return; 00173 } 00174 00175 foreach (KActionCollection *collection, m_dialog->actionCollections()) 00176 { 00177 const KXMLGUIClient *client = collection->parentGUIClient(); 00178 if (!client) continue; 00179 QString fileDir = QLatin1String("shortcuts/share/apps/") 00180 + client->componentData().componentName() + '/'; 00181 schemeRoot.mkpath(fileDir); 00182 KShortcutSchemesHelper::exportActionCollection(collection, 00183 currentScheme(), exportTo + '/' + fileDir); 00184 } 00185 } 00186 00187 void KShortcutSchemesEditor::saveAsDefaultsForScheme() 00188 { 00189 foreach (KActionCollection *collection, m_dialog->actionCollections()) 00190 KShortcutSchemesHelper::exportActionCollection(collection, currentScheme()); 00191 } 00192 00193 00194 void KShortcutSchemesEditor::updateDeleteButton() 00195 { 00196 m_deleteScheme->setEnabled(m_schemesList->count()>=1); 00197 }
KDE 4.6 API Reference