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
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.
21 #include "kcustommenueditor.h"
24 #include <QtCore/QDir>
25 #include <QtCore/QRegExp>
26 #include <QtGui/QImage>
27 #include <QtGui/QPushButton>
30 #include <k3listview.h>
31 #include <kconfigbase.h>
32 #include <kconfiggroup.h>
33 #include <kdialogbuttonbox.h>
36 #include <kiconloader.h>
38 #include <kopenwithdialog.h>
40 #include <kstandarddirs.h>
43 class KCustomMenuEditor::Item
: public Q3ListViewItem
46 Item(Q3ListView
*parent
, KService::Ptr service
)
47 : Q3ListViewItem(parent
),
53 Item(Q3ListViewItem
*parent
, KService::Ptr service
)
54 : Q3ListViewItem(parent
),
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
);
84 class KCustomMenuEditor::KCustomMenuEditorPrivate
87 QPushButton
* pbRemove
;
88 QPushButton
* pbMoveUp
;
89 QPushButton
* pbMoveDown
;
92 KCustomMenuEditor::KCustomMenuEditor(QWidget
*parent
)
94 m_listView(0),d(new KCustomMenuEditorPrivate
)
96 setCaption( i18n("Menu Editor") );
97 setButtons( Ok
| Cancel
);
99 showButtonSeparator(true);
100 KHBox
*page
= new KHBox(this);
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()));
112 connect( m_listView
, SIGNAL( selectionChanged () ), this, SLOT( refreshButton() ) );
116 KCustomMenuEditor::~KCustomMenuEditor()
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() );
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());
141 // Try KSycoca first.
142 KService::Ptr menuItem
= KService::serviceByDesktopPath( entry
);
144 menuItem
= KService::serviceByDesktopName( entry
);
146 menuItem
= new KService( entry
);
148 if (!menuItem
->isValid())
151 Q3ListViewItem
*item
= new Item(m_listView
, menuItem
);
152 item
->moveItem(last
);
158 KCustomMenuEditor::save(KConfig
*cfg
)
160 // First clear the whole config file.
161 const 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();
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
);
184 KCustomMenuEditor::slotNewItem()
186 Q3ListViewItem
*item
= m_listView
->currentItem();
188 KOpenWithDialog
dlg(this);
189 dlg
.setSaveNewApplications(true);
193 KService::Ptr s
= dlg
.service();
194 if (s
&& s
->isValid())
196 Item
*newItem
= new Item(m_listView
, s
);
197 newItem
->moveItem(item
);
204 KCustomMenuEditor::slotRemoveItem()
206 Q3ListViewItem
*item
= m_listView
->currentItem();
215 KCustomMenuEditor::slotMoveUp()
217 Q3ListViewItem
*item
= m_listView
->currentItem();
221 Q3ListViewItem
*searchItem
= m_listView
->firstChild();
224 Q3ListViewItem
*next
= searchItem
->nextSibling();
227 searchItem
->moveItem(item
);
236 KCustomMenuEditor::slotMoveDown()
238 Q3ListViewItem
*item
= m_listView
->currentItem();
242 Q3ListViewItem
*after
= item
->nextSibling();
246 item
->moveItem( after
);
250 #include "kcustommenueditor.moc"