Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / apps / konqueror / settings / konq / kcustommenueditor.cpp
blob609f01df560267417ba6e642106c40baf800793d
1 /* This file is part of the KDE libraries
2 Copyright (C) 2002 Waldo Bastian (bastian@kde.org)
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; version 2
7 of the License.
9 This library 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 GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 // Own
21 #include "kcustommenueditor.h"
23 // Qt
24 #include <QtCore/QDir>
25 #include <QtCore/QRegExp>
26 #include <QtGui/QImage>
27 #include <QtGui/QPushButton>
29 // KDE
30 #include <k3listview.h>
31 #include <kconfigbase.h>
32 #include <kconfiggroup.h>
33 #include <kdialogbuttonbox.h>
34 #include <kglobal.h>
35 #include <khbox.h>
36 #include <kiconloader.h>
37 #include <klocale.h>
38 #include <kopenwithdialog.h>
39 #include <kservice.h>
40 #include <kstandarddirs.h>
43 class KCustomMenuEditor::Item : public Q3ListViewItem
45 public:
46 Item(Q3ListView *parent, KService::Ptr service)
47 : Q3ListViewItem(parent),
48 s(service)
50 init();
53 Item(Q3ListViewItem *parent, KService::Ptr service)
54 : Q3ListViewItem(parent),
55 s(service)
57 init();
60 void init()
62 QString serviceName = s->name();
64 // item names may contain ampersands. To avoid them being converted
65 // to accelators, replace them with two ampersands.
66 serviceName.replace("&", "&&");
68 QPixmap normal = KIconLoader::global()->loadIcon(s->icon(), KIconLoader::Small,
69 0, KIconLoader::DefaultState, QStringList(), 0L, true);
71 // make sure they are not larger than 16x16
72 if (normal.width() > 16 || normal.height() > 16) {
73 QImage tmp = normal.toImage();
74 tmp = tmp.scaled(16, 16, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
75 normal = QPixmap::fromImage(tmp);
77 setText(0, serviceName);
78 setPixmap(0, normal);
81 KService::Ptr s;
84 class KCustomMenuEditor::KCustomMenuEditorPrivate
86 public:
87 QPushButton * pbRemove;
88 QPushButton * pbMoveUp;
89 QPushButton * pbMoveDown;
92 KCustomMenuEditor::KCustomMenuEditor(QWidget *parent)
93 : KDialog(parent),
94 m_listView(0),d(new KCustomMenuEditorPrivate)
96 setCaption( i18n("Menu Editor") );
97 setButtons( Ok | Cancel );
98 setDefaultButton(Ok);
99 showButtonSeparator(true);
100 KHBox *page = new KHBox(this);
101 setMainWidget(page);
102 m_listView = new K3ListView(page);
103 m_listView->addColumn(i18n("Menu"));
104 m_listView->setFullWidth(true);
105 m_listView->setSorting(-1);
106 KDialogButtonBox *buttonBox = new KDialogButtonBox(page, Qt::Vertical);
107 buttonBox->addButton(i18n("New..."),QDialogButtonBox::ActionRole, this, SLOT(slotNewItem()));
108 d->pbRemove=buttonBox->addButton(i18n("Remove"),QDialogButtonBox::DestructiveRole, this, SLOT(slotRemoveItem()));
109 d->pbMoveUp=buttonBox->addButton(i18n("Move Up"),QDialogButtonBox::ActionRole, this, SLOT(slotMoveUp()));
110 d->pbMoveDown=buttonBox->addButton(i18n("Move Down"),QDialogButtonBox::ActionRole, this, SLOT(slotMoveDown()));
111 buttonBox->layout();
112 connect( m_listView, SIGNAL( selectionChanged () ), this, SLOT( refreshButton() ) );
113 refreshButton();
116 KCustomMenuEditor::~KCustomMenuEditor()
118 delete d;
121 void KCustomMenuEditor::refreshButton()
123 Q3ListViewItem *item = m_listView->currentItem();
124 d->pbRemove->setEnabled( item );
125 d->pbMoveUp->setEnabled( item && item->itemAbove() );
126 d->pbMoveDown->setEnabled( item && item->itemBelow() );
129 void
130 KCustomMenuEditor::load(KConfig *cfg)
132 KConfigGroup cg(cfg, QString());
133 int count = cg.readEntry("NrOfItems", 0);
134 Q3ListViewItem *last = 0;
135 for(int i = 0; i < count; i++)
137 QString entry = cg.readPathEntry(QString("Item%1").arg(i+1), QString());
138 if (entry.isEmpty())
139 continue;
141 // Try KSycoca first.
142 KService::Ptr menuItem = KService::serviceByDesktopPath( entry );
143 if (!menuItem)
144 menuItem = KService::serviceByDesktopName( entry );
145 if (!menuItem)
146 menuItem = new KService( entry );
148 if (!menuItem->isValid())
149 continue;
151 Q3ListViewItem *item = new Item(m_listView, menuItem);
152 item->moveItem(last);
153 last = item;
157 void
158 KCustomMenuEditor::save(KConfig *cfg)
160 // First clear the whole config file.
161 QStringList groups = cfg->groupList();
162 for(QStringList::ConstIterator it = groups.begin();
163 it != groups.end(); ++it)
165 cfg->deleteGroup(*it);
168 KConfigGroup cg(cfg, QString());
169 Item * item = (Item *) m_listView->firstChild();
170 int i = 0;
171 while(item)
173 i++;
174 QString path = item->s->entryPath();
175 if (QDir::isRelativePath(path) || QDir::isRelativePath(KGlobal::dirs()->relativeLocation("xdgdata-apps", path)))
176 path = item->s->desktopEntryName();
177 cg.writePathEntry(QString("Item%1").arg(i), path);
178 item = (Item *) item->nextSibling();
180 cg.writeEntry("NrOfItems", i);
183 void
184 KCustomMenuEditor::slotNewItem()
186 Q3ListViewItem *item = m_listView->currentItem();
188 KOpenWithDialog dlg(this);
189 dlg.setSaveNewApplications(true);
191 if (dlg.exec())
193 KService::Ptr s = dlg.service();
194 if (s && s->isValid())
196 Item *newItem = new Item(m_listView, s);
197 newItem->moveItem(item);
199 refreshButton();
203 void
204 KCustomMenuEditor::slotRemoveItem()
206 Q3ListViewItem *item = m_listView->currentItem();
207 if (!item)
208 return;
210 delete item;
211 refreshButton();
214 void
215 KCustomMenuEditor::slotMoveUp()
217 Q3ListViewItem *item = m_listView->currentItem();
218 if (!item)
219 return;
221 Q3ListViewItem *searchItem = m_listView->firstChild();
222 while(searchItem)
224 Q3ListViewItem *next = searchItem->nextSibling();
225 if (next == item)
227 searchItem->moveItem(item);
228 break;
230 searchItem = next;
232 refreshButton();
235 void
236 KCustomMenuEditor::slotMoveDown()
238 Q3ListViewItem *item = m_listView->currentItem();
239 if (!item)
240 return;
242 Q3ListViewItem *after = item->nextSibling();
243 if (!after)
244 return;
246 item->moveItem( after );
247 refreshButton();
250 #include "kcustommenueditor.moc"