1/*
2 * Copyright (C) 2000 Matthias Elter <elter@kde.org>
3 * Copyright (C) 2001-2002 Raffaele Sandrini <sandrini@kde.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#include "kmenuedit.h"
21
22#include <QSplitter>
23
24#include <KAction>
25#include <KActionCollection>
26#include <KActionMenu>
27#include <KConfig>
28#include <KDebug>
29#include <KGlobal>
30#include <KIcon>
31#include <KLocale>
32#include <KMessageBox>
33#include <KService>
34#include <KStandardAction>
35#include <KStandardShortcut>
36#include <KXMLGUIFactory>
37#include <sonnet/configdialog.h>
38
39#include "treeview.h"
40#include "basictab.h"
41#include "preferencesdlg.h"
42#include "kmenueditadaptor.h"
43
44#include "kmenuedit.moc"
45
46KMenuEdit::KMenuEdit ()
47 : KXmlGuiWindow (0)
48 , m_tree(0)
49 , m_basicTab(0)
50 , m_splitter(0)
51 , m_actionDelete(0)
52{
53 // dbus
54 ( void )new KmenueditAdaptor(this);
55 QDBusConnection::sessionBus().registerObject("/KMenuEdit", this);
56
57 KConfigGroup group( KGlobal::config(), "General" );
58 m_showHidden = group.readEntry("ShowHidden", false);
59
60 // setup GUI
61 setupActions();
62 slotChangeView();
63}
64
65KMenuEdit::~KMenuEdit()
66{
67 KConfigGroup group(KGlobal::config(), "General");
68 group.writeEntry("SplitterSizes", m_splitter->sizes());
69
70 group.sync();
71}
72
73void KMenuEdit::setupActions()
74{
75 KAction *action = 0;
76
77 action = actionCollection()->addAction(NEW_SUBMENU_ACTION_NAME);
78 action->setIcon(KIcon("menu_new"));
79 action->setText(i18n("&New Submenu..."));
80 action->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_N));
81 action = actionCollection()->addAction(NEW_ITEM_ACTION_NAME);
82 action->setIcon(KIcon("document-new")) ;
83 action->setText(i18n("New &Item..."));
84 action->setShortcuts(KStandardShortcut::openNew());
85 action = actionCollection()->addAction(NEW_SEPARATOR_ACTION_NAME);
86 action->setIcon(KIcon("menu_new_sep"));
87 action->setText(i18n("New S&eparator"));
88 action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_I));
89
90 // "sort selection" menu
91 KActionMenu* sortMenu = new KActionMenu(KIcon("view-sort-ascending"), i18n("&Sort"), this);
92 sortMenu->setDelayed(false);
93 actionCollection()->addAction(SORT_ACTION_NAME, sortMenu);
94 action = actionCollection()->addAction(SORT_BY_NAME_ACTION_NAME);
95 action->setText(i18n("&Sort selection by Name"));
96 sortMenu->addAction(action);
97 action = actionCollection()->addAction(SORT_BY_DESCRIPTION_ACTION_NAME);
98 action->setText(i18n("&Sort selection by Description"));
99 sortMenu->addAction(action);
100 sortMenu->addSeparator();
101 action = actionCollection()->addAction(SORT_ALL_BY_NAME_ACTION_NAME);
102 action->setText(i18n("&Sort all by Name"));
103 sortMenu->addAction(action);
104 action = actionCollection()->addAction(SORT_ALL_BY_DESCRIPTION_ACTION_NAME);
105 action->setText(i18n("&Sort all by Description"));
106 sortMenu->addAction(action);
107
108 // move up/down
109 action = actionCollection()->addAction(MOVE_UP_ACTION_NAME);
110 action->setIcon(KIcon("go-up"));
111 action->setText(i18n("Move &Up"));
112 action = actionCollection()->addAction(MOVE_DOWN_ACTION_NAME);
113 action->setIcon(KIcon("go-down"));
114 action->setText(i18n("Move &Down"));
115
116 actionCollection()->addAction(KStandardAction::Save, this, SLOT(slotSave()));
117 actionCollection()->addAction(KStandardAction::Quit, this, SLOT(close()));
118 actionCollection()->addAction(KStandardAction::Cut);
119 actionCollection()->addAction(KStandardAction::Copy);
120 actionCollection()->addAction(KStandardAction::Paste);
121
122 action = new KAction( i18n("Restore to System Menu"), this );
123 actionCollection()->addAction( "restore_system_menu", action );
124 connect( action, SIGNAL(triggered(bool)), SLOT(slotRestoreMenu()) );
125
126 KStandardAction::preferences( this, SLOT(slotConfigure()), actionCollection() );
127}
128
129void KMenuEdit::slotConfigure()
130{
131 PreferencesDialog dialog( this );
132 if ( dialog.exec() )
133 {
134 KConfigGroup group( KGlobal::config(), "General" );
135 bool newShowHiddenValue = group.readEntry("ShowHidden", false);
136 if ( newShowHiddenValue != m_showHidden )
137 {
138 m_showHidden = newShowHiddenValue;
139 m_tree->updateTreeView(m_showHidden);
140 m_basicTab->updateHiddenEntry( m_showHidden );
141 }
142 }
143}
144
145void KMenuEdit::setupView()
146{
147 m_splitter = new QSplitter;
148 m_splitter->setOrientation(Qt::Horizontal);
149 m_tree = new TreeView(actionCollection());
150 m_splitter->addWidget(m_tree);
151 m_basicTab = new BasicTab;
152 m_splitter->addWidget(m_basicTab);
153
154 connect(m_tree, SIGNAL(entrySelected(MenuFolderInfo*)),
155 m_basicTab, SLOT(setFolderInfo(MenuFolderInfo*)));
156 connect(m_tree, SIGNAL(entrySelected(MenuEntryInfo*)),
157 m_basicTab, SLOT(setEntryInfo(MenuEntryInfo*)));
158 connect(m_tree, SIGNAL(disableAction()),
159 m_basicTab, SLOT(slotDisableAction()) );
160
161 connect(m_basicTab, SIGNAL(changed(MenuFolderInfo*)),
162 m_tree, SLOT(currentDataChanged(MenuFolderInfo*)));
163
164 connect(m_basicTab, SIGNAL(changed(MenuEntryInfo*)),
165 m_tree, SLOT(currentDataChanged(MenuEntryInfo*)));
166
167 connect(m_basicTab, SIGNAL(findServiceShortcut(KShortcut,KService::Ptr&)),
168 m_tree, SLOT(findServiceShortcut(KShortcut,KService::Ptr&)));
169
170 // restore splitter sizes
171 KSharedConfig::Ptr config = KGlobal::config();
172 KConfigGroup group(config, "General");
173 QList<int> sizes = group.readEntry("SplitterSizes",QList<int>());
174
175 if (sizes.isEmpty()) {
176 sizes << 1 << 3;
177 }
178 m_splitter->setSizes(sizes);
179 m_tree->setFocus();
180
181 setCentralWidget(m_splitter);
182}
183
184void KMenuEdit::selectMenu(const QString &menu)
185{
186 m_tree->selectMenu(menu);
187}
188
189void KMenuEdit::selectMenuEntry(const QString &menuEntry)
190{
191 m_tree->selectMenuEntry(menuEntry);
192}
193
194void KMenuEdit::slotChangeView()
195{
196 guiFactory()->removeClient( this );
197
198 delete m_actionDelete;
199
200 m_actionDelete = actionCollection()->addAction(DELETE_ACTION_NAME);
201 m_actionDelete->setIcon(KIcon("edit-delete"));
202 m_actionDelete->setText(i18n("&Delete"));
203 m_actionDelete->setShortcut(QKeySequence(Qt::Key_Delete));
204
205 if (m_splitter == 0) {
206 setupView();
207 }
208 setupGUI(KXmlGuiWindow::ToolBar|Keys|Save|Create, "kmenueditui.rc");
209
210 m_tree->setViewMode(m_showHidden);
211 m_basicTab->updateHiddenEntry( m_showHidden );
212}
213
214void KMenuEdit::slotSave()
215{
216 m_tree->save();
217}
218
219bool KMenuEdit::queryClose()
220{
221 if (!m_tree->dirty()) return true;
222
223
224 int result;
225 result = KMessageBox::warningYesNoCancel(this,
226 i18n("You have made changes to the menu.\n"
227 "Do you want to save the changes or discard them?"),
228 i18n("Save Menu Changes?"),
229 KStandardGuiItem::save(), KStandardGuiItem::discard() );
230
231 switch(result)
232 {
233 case KMessageBox::Yes:
234 return m_tree->save();
235
236 case KMessageBox::No:
237 return true;
238
239 default:
240 break;
241 }
242 return false;
243}
244
245void KMenuEdit::slotRestoreMenu()
246{
247 m_tree->restoreMenuSystem();
248}
249
250void KMenuEdit::restoreSystemMenu()
251{
252 m_tree->restoreMenuSystem();
253}
254