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