00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
#include <qhbox.h>
00021
#include <qregexp.h>
00022
#include <qimage.h>
00023
#include <qpushbutton.h>
00024
00025
#include <kbuttonbox.h>
00026
#include <klocale.h>
00027
#include <kglobal.h>
00028
#include <kiconloader.h>
00029
#include <klistview.h>
00030
#include <kservice.h>
00031
#include <kstandarddirs.h>
00032
#include <kconfigbase.h>
00033
#include <kopenwith.h>
00034
00035
#include "kcustommenueditor.h"
00036
00037
class KCustomMenuEditor::Item :
public QListViewItem
00038 {
00039
public:
00040 Item(
QListView *parent, KService::Ptr service)
00041 :
QListViewItem(parent),
00042 s(service)
00043 {
00044 init();
00045 }
00046
00047 Item(
QListViewItem *parent, KService::Ptr service)
00048 :
QListViewItem(parent),
00049 s(service)
00050 {
00051 init();
00052 }
00053
00054
void init()
00055 {
00056
QString serviceName = s->name();
00057
00058
00059
00060 serviceName.replace(
"&",
"&&");
00061
00062
QPixmap normal =
KGlobal::instance()->
iconLoader()->
loadIcon(s->icon(), KIcon::Small,
00063 0,
KIcon::DefaultState, 0L,
true);
00064
00065
00066
if (normal.width() > 16 || normal.height() > 16) {
00067
QImage tmp = normal.convertToImage();
00068 tmp = tmp.smoothScale(16, 16);
00069 normal.convertFromImage(tmp);
00070 }
00071 setText(0, serviceName);
00072 setPixmap(0, normal);
00073 }
00074
00075 KService::Ptr s;
00076 };
00077
00078
class KCustomMenuEditor::KCustomMenuEditorPrivate
00079 {
00080
public:
00081
QPushButton * pbRemove;
00082
QPushButton * pbMoveUp;
00083
QPushButton * pbMoveDown;
00084 };
00085
00086 KCustomMenuEditor::KCustomMenuEditor(
QWidget *parent)
00087 :
KDialogBase(parent,
"custommenueditor", true, i18n(
"Menu Editor"), Ok|Cancel, Ok, true),
00088 m_listView(0)
00089 {
00090 d =
new KCustomMenuEditorPrivate;
00091
QHBox *page = makeHBoxMainWidget();
00092 m_listView =
new KListView(page);
00093 m_listView->addColumn(i18n(
"Menu"));
00094 m_listView->setFullWidth(
true);
00095 m_listView->setSorting(-1);
00096
KButtonBox *buttonBox =
new KButtonBox(page, Vertical);
00097 buttonBox->
addButton(i18n(
"New..."),
this, SLOT(slotNewItem()));
00098 d->pbRemove=buttonBox->
addButton(i18n(
"Remove"),
this, SLOT(slotRemoveItem()));
00099 d->pbMoveUp=buttonBox->
addButton(i18n(
"Move Up"),
this, SLOT(slotMoveUp()));
00100 d->pbMoveDown=buttonBox->
addButton(i18n(
"Move Down"),
this, SLOT(slotMoveDown()));
00101 buttonBox->
layout();
00102 connect( m_listView, SIGNAL( selectionChanged () ),
this, SLOT( refreshButton() ) );
00103 refreshButton();
00104 }
00105
00106 KCustomMenuEditor::~KCustomMenuEditor()
00107 {
00108
delete d;
00109 d=0;
00110 }
00111
00112
void KCustomMenuEditor::refreshButton()
00113 {
00114
QListViewItem *item = m_listView->currentItem();
00115 d->pbRemove->setEnabled( item );
00116 d->pbMoveUp->setEnabled( item && item->itemAbove() );
00117 d->pbMoveDown->setEnabled( item && item->itemBelow() );
00118 }
00119
00120
void
00121 KCustomMenuEditor::load(
KConfigBase *cfg)
00122 {
00123 cfg->
setGroup(QString::null);
00124
int count = cfg->
readNumEntry(
"NrOfItems");
00125
QListViewItem *last = 0;
00126
for(
int i = 0; i < count; i++)
00127 {
00128
QString entry = cfg->
readPathEntry(
QString(
"Item%1").arg(i+1));
00129
if (entry.isEmpty())
00130
continue;
00131
00132
00133 KService::Ptr menuItem =
KService::serviceByDesktopPath( entry );
00134
if (!menuItem)
00135 menuItem =
KService::serviceByDesktopName( entry );
00136
if (!menuItem)
00137 menuItem =
new KService( entry );
00138
00139
if (!menuItem->isValid())
00140
continue;
00141
00142
QListViewItem *item =
new Item(m_listView, menuItem);
00143 item->moveItem(last);
00144 last = item;
00145 }
00146 }
00147
00148
void
00149 KCustomMenuEditor::save(
KConfigBase *cfg)
00150 {
00151
00152
QStringList groups = cfg->
groupList();
00153
for(QStringList::ConstIterator it = groups.begin();
00154 it != groups.end(); ++it)
00155 {
00156 cfg->
deleteGroup(*it);
00157 }
00158
00159 cfg->
setGroup(QString::null);
00160 Item * item = (Item *) m_listView->firstChild();
00161
int i = 0;
00162
while(item)
00163 {
00164 i++;
00165
QString path = item->s->desktopEntryPath();
00166
if (!path.startsWith(
"/") || !
KGlobal::dirs()->
relativeLocation(
"xdgdata-apps", path).startsWith(
"/"))
00167 path = item->s->desktopEntryName();
00168 cfg->
writePathEntry(
QString(
"Item%1").arg(i), path);
00169 item = (Item *) item->nextSibling();
00170 }
00171 cfg->
writeEntry(
"NrOfItems", i);
00172 }
00173
00174
void
00175 KCustomMenuEditor::slotNewItem()
00176 {
00177
QListViewItem *item = m_listView->currentItem();
00178
00179
KOpenWithDlg dlg(
this);
00180 dlg.setSaveNewApplications(
true);
00181
00182
if (dlg.exec())
00183 {
00184 KService::Ptr s = dlg.service();
00185
if (s && s->isValid())
00186 {
00187 Item *newItem =
new Item(m_listView, s);
00188 newItem->moveItem(item);
00189 }
00190 refreshButton();
00191 }
00192 }
00193
00194
void
00195 KCustomMenuEditor::slotRemoveItem()
00196 {
00197
QListViewItem *item = m_listView->currentItem();
00198
if (!item)
00199
return;
00200
00201
delete item;
00202 refreshButton();
00203 }
00204
00205
void
00206 KCustomMenuEditor::slotMoveUp()
00207 {
00208
QListViewItem *item = m_listView->currentItem();
00209
if (!item)
00210
return;
00211
00212
QListViewItem *searchItem = m_listView->firstChild();
00213
while(searchItem)
00214 {
00215
QListViewItem *
next = searchItem->nextSibling();
00216
if (
next == item)
00217 {
00218 searchItem->moveItem(item);
00219
break;
00220 }
00221 searchItem =
next;
00222 }
00223 refreshButton();
00224 }
00225
00226
void
00227 KCustomMenuEditor::slotMoveDown()
00228 {
00229
QListViewItem *item = m_listView->currentItem();
00230
if (!item)
00231
return;
00232
00233
QListViewItem *after = item->nextSibling();
00234
if (!after)
00235
return;
00236
00237 item->moveItem( after );
00238 refreshButton();
00239 }
00240
00241
#include "kcustommenueditor.moc"