kio Library API Documentation

kcustommenueditor.cpp

00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2002 Waldo Bastian (bastian@kde.org) 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; version 2 00007 of the License. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00017 Boston, MA 02111-1307, USA. 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 // item names may contain ampersands. To avoid them being converted 00059 // to accelators, replace them with two ampersands. 00060 serviceName.replace("&", "&&"); 00061 00062 QPixmap normal = KGlobal::instance()->iconLoader()->loadIcon(s->icon(), KIcon::Small, 00063 0, KIcon::DefaultState, 0L, true); 00064 00065 // make sure they are not larger than 16x16 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 00121 void 00122 KCustomMenuEditor::load(KConfigBase *cfg) 00123 { 00124 cfg->setGroup(QString::null); 00125 int count = cfg->readNumEntry("NrOfItems"); 00126 QListViewItem *last = 0; 00127 for(int i = 0; i < count; i++) 00128 { 00129 QString entry = cfg->readPathEntry(QString("Item%1").arg(i+1)); 00130 if (entry.isEmpty()) 00131 continue; 00132 00133 // Try KSycoca first. 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 QListViewItem *item = new Item(m_listView, menuItem); 00144 item->moveItem(last); 00145 last = item; 00146 } 00147 } 00148 00149 void 00150 KCustomMenuEditor::save(KConfigBase *cfg) 00151 { 00152 // First clear the whole config file. 00153 QStringList groups = cfg->groupList(); 00154 for(QStringList::ConstIterator it = groups.begin(); 00155 it != groups.end(); ++it) 00156 { 00157 cfg->deleteGroup(*it); 00158 } 00159 00160 cfg->setGroup(QString::null); 00161 Item * item = (Item *) m_listView->firstChild(); 00162 int i = 0; 00163 while(item) 00164 { 00165 i++; 00166 QString path = item->s->desktopEntryPath(); 00167 if (!path.startsWith("/") || !KGlobal::dirs()->relativeLocation("xdgdata-apps", path).startsWith("/")) 00168 path = item->s->desktopEntryName(); 00169 cfg->writePathEntry(QString("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 QListViewItem *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 QListViewItem *item = m_listView->currentItem(); 00199 if (!item) 00200 return; 00201 00202 delete item; 00203 refreshButton(); 00204 } 00205 00206 void 00207 KCustomMenuEditor::slotMoveUp() 00208 { 00209 QListViewItem *item = m_listView->currentItem(); 00210 if (!item) 00211 return; 00212 00213 QListViewItem *searchItem = m_listView->firstChild(); 00214 while(searchItem) 00215 { 00216 QListViewItem *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 QListViewItem *item = m_listView->currentItem(); 00231 if (!item) 00232 return; 00233 00234 QListViewItem *after = item->nextSibling(); 00235 if (!after) 00236 return; 00237 00238 item->moveItem( after ); 00239 refreshButton(); 00240 } 00241 00242 #include "kcustommenueditor.moc"
KDE Logo
This file is part of the documentation for kio Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Oct 10 18:55:26 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003