~ubuntu-branches/ubuntu/vivid/krusader/vivid-proposed

« back to all changes in this revision

Viewing changes to krusader/Dialogs/krkeydialog.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-05-05 22:26:37 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100505222637-ydv3cwjwy365on2r
Tags: 1:2.1.0~beta1-1ubuntu1
* Merge from Debian Unstable.  Remaining changes:
  - Retain Kubuntu doc path

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// C++ Implementation: krkeydialog
3
 
//
4
 
// Description: 
5
 
//
6
 
//
7
 
// Author: Jonas Bähr <http://jonas-baehr.de/>, (C) 2006
8
 
//
9
 
// Copyright: See COPYING file that comes with this distribution
10
 
//
11
 
//
 
1
/*****************************************************************************
 
2
 * Copyright (C) 2006 Jonas Bähr <jonas.baehr@web.de>                        *
 
3
 *                                                                           *
 
4
 * This program is free software; you can redistribute it and/or modify      *
 
5
 * it under the terms of the GNU General Public License as published by      *
 
6
 * the Free Software Foundation; either version 2 of the License, or         *
 
7
 * (at your option) any later version.                                       *
 
8
 *                                                                           *
 
9
 * This package is distributed in the hope that it will be useful,           *
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
 
12
 * GNU General Public License for more details.                              *
 
13
 *                                                                           *
 
14
 * You should have received a copy of the GNU General Public License         *
 
15
 * along with this package; if not, write to the Free Software               *
 
16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA *
 
17
 *****************************************************************************/
 
18
 
12
19
#include "krkeydialog.h"
13
20
 
14
 
#include <qlayout.h>
15
 
#include <qtextstream.h>
 
21
#include <QtGui/QLayout>
 
22
#include <QtCore/QTextStream>
16
23
#include <klocale.h>
17
24
#include <kpushbutton.h>
18
25
#include <kmessagebox.h>
29
36
static const char* FILE_FILTER = I18N_NOOP("*.keymap|Krusader keymaps\n*|all files");
30
37
 
31
38
 
32
 
KrKeyDialog::KrKeyDialog( QWidget * parent ) : KShortcutsDialog( KShortcutsEditor::AllActions, KShortcutsEditor::LetterShortcutsDisallowed /* allow letter shortcuts */, parent ) {
33
 
   addCollection( krApp->actionCollection() );
34
 
 
35
 
   // HACK This fetches the layout of the buttonbox from KDialog, although it is not accessable with KDialog's API
36
 
   // None the less it's quite save to use since this implementation hasn't changed since KDE-3.3 (I haven't looked at earlier
37
 
   // versions since we don't support them) and now all work is done in KDE-4.
38
 
   QWidget* buttonBox = static_cast<QWidget*>( button(KDialog::Ok)->parent() );
39
 
   QBoxLayout* buttonBoxLayout = static_cast<QBoxLayout*>( buttonBox->layout() );
40
 
 
41
 
   KPushButton* importButton = new KPushButton( i18n("Import shortcuts"), buttonBox );
42
 
   importButton->setWhatsThis( i18n( "Load a keybinding profile, e.g., total_commander.keymap" ) );
43
 
   buttonBoxLayout->insertWidget( 1, importButton ); // the defaults-button should stay on position 0
44
 
   connect( importButton, SIGNAL( clicked() ), SLOT( slotImportShortcuts() ) );
45
 
 
46
 
   KPushButton* exportButton = new KPushButton( i18n("Export shortcuts"), buttonBox );
47
 
   exportButton->setWhatsThis( i18n( "Save current keybindings in a keymap file." ) );
48
 
   buttonBoxLayout->insertWidget( 2, exportButton );
49
 
   connect( exportButton, SIGNAL( clicked() ), SLOT( slotExportShortcuts() ) );
50
 
 
51
 
   // Also quite HACK 'isch but unfortunately KKeyDialog don't giveus access to this widget
52
 
   _chooser = static_cast<KShortcutsEditor*>( mainWidget() );
53
 
 
54
 
   configure( true /* SaveSettings */ ); // this runs the dialog
55
 
}
56
 
 
57
 
KrKeyDialog::~KrKeyDialog() {
58
 
}
59
 
 
60
 
void KrKeyDialog::slotImportShortcuts() {
61
 
   // find $KDEDIR/share/apps/krusader
62
 
   QString basedir = KGlobal::dirs()->findResourceDir("appdata", "total_commander.keymap");
63
 
   // let the user select a file to load
64
 
   QString filename = KFileDialog::getOpenFileName(basedir, i18n(FILE_FILTER), 0, i18n("Select a keymap file"));
65
 
   if ( filename.isEmpty() )
66
 
      return;
67
 
 
68
 
   KConfig conf( filename, KConfig::NoGlobals /*no KDEGlobal*/ );
69
 
   if ( ! conf.hasGroup("Shortcuts") ) {
70
 
      int answer = KMessageBox::warningContinueCancel( this,    //parent
71
 
                i18n("This file does not seem to be a valid keymap.\n"
72
 
                        "It may be a keymap using a legacy format. The import can't be undone!"),       //text
73
 
                i18n("Try to import legacy format?"),   //caption
74
 
                KGuiItem( i18n("Import anyway") ),      //Label for the continue-button
75
 
                KStandardGuiItem::cancel(),
76
 
                "Confirm Import Legacy Shortcuts"       //dontAskAgainName (for the config-file)
77
 
        );
78
 
      if ( answer == KMessageBox::Continue )
79
 
         importLegacyShortcuts( filename );
80
 
      else
81
 
         return;
82
 
   }
83
 
   else
84
 
      _chooser->save();
85
 
}
86
 
 
87
 
void KrKeyDialog::importLegacyShortcuts( const QString& file ) {
88
 
/*
89
 
 * This is basicaly Shie's code. It's copied from Kronfigurator's loog&feel page and adapted to the dialog
90
 
 */
91
 
        // check if there's an info file with the keymap
92
 
        QFile info(file+".info");
93
 
        if (info.open(QIODevice::ReadOnly)) {
94
 
                QTextStream stream(&info);
95
 
                QStringList infoText = stream.readAll().split("\n");
96
 
                if (KMessageBox::questionYesNoList(krApp, i18n("The following information was attached to the keymap. Do you really want to import this keymap?"), infoText)!=KMessageBox::Yes)
97
 
                        return;
98
 
        }
99
 
 
100
 
        // ok, import away
101
 
        QFile f(file);
102
 
        if (!f.open(QIODevice::ReadOnly)) {
103
 
                krOut << "Error opening " << file << endl;
104
 
                return;
105
 
        }
106
 
        char *actionName;
107
 
        QDataStream stream(&f);
108
 
        int key;
109
 
        QAction *action;
110
 
        while (!stream.atEnd()) {
111
 
                stream >> actionName >> key;
112
 
                action = krApp->actionCollection()->action(actionName);
113
 
                if (action) {
114
 
                        action->setShortcut(key);
115
 
//                      krOut << "set shortcut for " << actionName <<endl;
116
 
                } else {
117
 
                   krOut << "unknown action " << actionName << endl;
118
 
                }
119
 
        }
120
 
        f.close();
121
 
 
122
 
        KMessageBox::information( this, // parent
123
 
                i18n("Please restart this dialog in order to see the changes"), // text
124
 
                i18n("Legacy import completed") // caption
125
 
                );
126
 
}
127
 
 
128
 
void KrKeyDialog::slotExportShortcuts() {
129
 
   QString filename = KFileDialog::getSaveFileName( QString(), i18n(FILE_FILTER), 0, i18n("Select a keymap file") );
130
 
   if ( filename.isEmpty() )
131
 
      return;
132
 
   QFile f( filename );
133
 
   if ( f.exists() &&
134
 
                KMessageBox::warningContinueCancel( this, 
135
 
                i18n("<qt>File <b>%1</b> already exists. Do you really want to overwrite it?</qt>", filename),
136
 
                i18n("Warning"), KGuiItem( i18n("Overwrite") ) )
137
 
        != KMessageBox::Continue)
138
 
        return;
139
 
   if ( f.open( QIODevice::WriteOnly ) )
140
 
      // This is the only way to detect if the file is writable since we don't get feetback from KConfig's sync
141
 
      // Additionaly this prevents merging if the file already contains some shortcuts
142
 
      f.close();
143
 
   else {
144
 
      KMessageBox::error( this, i18n("<qt>Can't open <b>%1</b> for writing!</qt>", filename) );
145
 
      return;
146
 
   }
147
 
 
148
 
   KConfig conf( filename, KConfig::NoGlobals );
149
 
   KConfigGroup cg = conf.group( "Shortcuts" );
150
 
 
151
 
   // unfortunately we can't use this function since it only writes the actions which are different from default.
152
 
   //krApp->actionCollection()->writeShortcutSettings( "Shortcuts", &conf );
153
 
   krApp->actionCollection()->writeSettings( &cg, true /* write all actions */ );
154
 
   // That does KActionShortcutList::writeSettings for us
155
 
   //conf.sync(); // write back all changes
 
39
KrKeyDialog::KrKeyDialog(QWidget * parent) : KShortcutsDialog(KShortcutsEditor::AllActions, KShortcutsEditor::LetterShortcutsDisallowed /* allow letter shortcuts */, parent)
 
40
{
 
41
    addCollection(krApp->actionCollection());
 
42
 
 
43
    // HACK This fetches the layout of the buttonbox from KDialog, although it is not accessable with KDialog's API
 
44
    // None the less it's quite save to use since this implementation hasn't changed since KDE-3.3 (I haven't looked at earlier
 
45
    // versions since we don't support them) and now all work is done in KDE-4.
 
46
    QWidget* buttonBox = static_cast<QWidget*>(button(KDialog::Ok)->parent());
 
47
    QBoxLayout* buttonBoxLayout = static_cast<QBoxLayout*>(buttonBox->layout());
 
48
 
 
49
    KPushButton* importButton = new KPushButton(i18n("Import shortcuts"), buttonBox);
 
50
    importButton->setWhatsThis(i18n("Load a keybinding profile, e.g., total_commander.keymap"));
 
51
    buttonBoxLayout->insertWidget(1, importButton);   // the defaults-button should stay on position 0
 
52
    connect(importButton, SIGNAL(clicked()), SLOT(slotImportShortcuts()));
 
53
 
 
54
    KPushButton* exportButton = new KPushButton(i18n("Export shortcuts"), buttonBox);
 
55
    exportButton->setWhatsThis(i18n("Save current keybindings in a keymap file."));
 
56
    buttonBoxLayout->insertWidget(2, exportButton);
 
57
    connect(exportButton, SIGNAL(clicked()), SLOT(slotExportShortcuts()));
 
58
 
 
59
    // Also quite HACK 'isch but unfortunately KKeyDialog don't giveus access to this widget
 
60
    _chooser = static_cast<KShortcutsEditor*>(mainWidget());
 
61
 
 
62
    configure(true /* SaveSettings */);   // this runs the dialog
 
63
}
 
64
 
 
65
KrKeyDialog::~KrKeyDialog()
 
66
{
 
67
}
 
68
 
 
69
void KrKeyDialog::slotImportShortcuts()
 
70
{
 
71
    // find $KDEDIR/share/apps/krusader
 
72
    QString basedir = KGlobal::dirs()->findResourceDir("appdata", "total_commander.keymap");
 
73
    // let the user select a file to load
 
74
    QString filename = KFileDialog::getOpenFileName(basedir, i18n(FILE_FILTER), 0, i18n("Select a keymap file"));
 
75
    if (filename.isEmpty())
 
76
        return;
 
77
 
 
78
    KConfig conf(filename, KConfig::NoGlobals /*no KDEGlobal*/);
 
79
    if (! conf.hasGroup("Shortcuts")) {
 
80
        int answer = KMessageBox::warningContinueCancel(this,  //parent
 
81
                     i18n("This file does not seem to be a valid keymap.\n"
 
82
                          "It may be a keymap using a legacy format. The import can't be undone!"), //text
 
83
                     i18n("Try to import legacy format?"),  //caption
 
84
                     KGuiItem(i18n("Import anyway")),   //Label for the continue-button
 
85
                     KStandardGuiItem::cancel(),
 
86
                     "Confirm Import Legacy Shortcuts" //dontAskAgainName (for the config-file)
 
87
                                                       );
 
88
        if (answer == KMessageBox::Continue)
 
89
            importLegacyShortcuts(filename);
 
90
        else
 
91
            return;
 
92
    } else
 
93
        _chooser->save();
 
94
}
 
95
 
 
96
void KrKeyDialog::importLegacyShortcuts(const QString& file)
 
97
{
 
98
    /*
 
99
     * This is basicaly Shie's code. It's copied from Kronfigurator's loog&feel page and adapted to the dialog
 
100
     */
 
101
    // check if there's an info file with the keymap
 
102
    QFile info(file + ".info");
 
103
    if (info.open(QIODevice::ReadOnly)) {
 
104
        QTextStream stream(&info);
 
105
        QStringList infoText = stream.readAll().split('\n');
 
106
        if (KMessageBox::questionYesNoList(krApp, i18n("The following information was attached to the keymap. Do you really want to import this keymap?"), infoText) != KMessageBox::Yes)
 
107
            return;
 
108
    }
 
109
 
 
110
    // ok, import away
 
111
    QFile f(file);
 
112
    if (!f.open(QIODevice::ReadOnly)) {
 
113
        krOut << "Error opening " << file << endl;
 
114
        return;
 
115
    }
 
116
    char *actionName;
 
117
    QDataStream stream(&f);
 
118
    int key;
 
119
    QAction *action;
 
120
    while (!stream.atEnd()) {
 
121
        stream >> actionName >> key;
 
122
        action = krApp->actionCollection()->action(actionName);
 
123
        if (action) {
 
124
            action->setShortcut(key);
 
125
//   krOut << "set shortcut for " << actionName <<endl;
 
126
        } else {
 
127
            krOut << "unknown action " << actionName << endl;
 
128
        }
 
129
    }
 
130
    f.close();
 
131
 
 
132
    KMessageBox::information(this,  // parent
 
133
                             i18n("Please restart this dialog in order to see the changes"), // text
 
134
                             i18n("Legacy import completed") // caption
 
135
                            );
 
136
}
 
137
 
 
138
void KrKeyDialog::slotExportShortcuts()
 
139
{
 
140
    QString filename = KFileDialog::getSaveFileName(QString(), i18n(FILE_FILTER), 0, i18n("Select a keymap file"));
 
141
    if (filename.isEmpty())
 
142
        return;
 
143
    QFile f(filename);
 
144
    if (f.exists() &&
 
145
            KMessageBox::warningContinueCancel(this,
 
146
                                               i18n("<qt>File <b>%1</b> already exists. Do you really want to overwrite it?</qt>", filename),
 
147
                                               i18n("Warning"), KGuiItem(i18n("Overwrite")))
 
148
            != KMessageBox::Continue)
 
149
        return;
 
150
    if (f.open(QIODevice::WriteOnly))
 
151
        // This is the only way to detect if the file is writable since
 
152
        // we don't get feetback from KConfig's sync
 
153
        // additionally this prevents merging if the file already contains some
 
154
        // shortcuts
 
155
        f.close();
 
156
    else {
 
157
        KMessageBox::error(this, i18n("<qt>Can't open <b>%1</b> for writing!</qt>", filename));
 
158
        return;
 
159
    }
 
160
 
 
161
    KConfig conf(filename, KConfig::NoGlobals);
 
162
    KConfigGroup cg = conf.group("Shortcuts");
 
163
 
 
164
    // unfortunately we can't use this function since it only writes the actions which are different from default.
 
165
    //krApp->actionCollection()->writeShortcutSettings( "Shortcuts", &conf );
 
166
    krApp->actionCollection()->writeSettings(&cg, true /* write all actions */);
 
167
    // That does KActionShortcutList::writeSettings for us
 
168
    //conf.sync(); // write back all changes
156
169
}
157
170
 
158
171
#include "krkeydialog.moc"