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     bool selected = ( item!= 0 );
00116     int pos = m_listView->itemPos( item );
00117     d->pbRemove->setEnabled( selected );
00118     d->pbMoveUp->setEnabled( selected && pos>0);
00119     d->pbMoveDown->setEnabled( selected && pos < ( m_listView->childCount() ));
00120 }
00121 
00122 void
00123 KCustomMenuEditor::load(KConfigBase *cfg)
00124 {
00125    cfg->setGroup(QString::null);
00126    int count = cfg->readNumEntry("NrOfItems");
00127    QListViewItem *last = 0;
00128    for(int i = 0; i < count; i++)
00129    {
00130       QString entry = cfg->readPathEntry(QString("Item%1").arg(i+1));
00131       if (entry.isEmpty())
00132          continue;
00133 
00134       // Try KSycoca first.
00135       KService::Ptr menuItem = KService::serviceByDesktopPath( entry );
00136       if (!menuItem)
00137          menuItem = KService::serviceByDesktopName( entry );
00138       if (!menuItem)
00139          menuItem = new KService( entry );
00140 
00141       if (!menuItem->isValid())
00142          continue;
00143 
00144       QListViewItem *item = new Item(m_listView, menuItem);
00145       item->moveItem(last);
00146       last = item;
00147    }
00148 }
00149 
00150 void
00151 KCustomMenuEditor::save(KConfigBase *cfg)
00152 {
00153    // First clear the whole config file.
00154    QStringList groups = cfg->groupList();
00155    for(QStringList::ConstIterator it = groups.begin();
00156       it != groups.end(); ++it)
00157    {
00158       cfg->deleteGroup(*it);
00159    }
00160 
00161    cfg->setGroup(QString::null);
00162    Item * item = (Item *) m_listView->firstChild();
00163    int i = 0;
00164    while(item)
00165    {
00166       i++;
00167       QString path = item->s->desktopEntryPath();
00168       if (!path.startsWith("/") || !KGlobal::dirs()->relativeLocation("xdgdata-apps", path).startsWith("/"))
00169          path = item->s->desktopEntryName();
00170       cfg->writePathEntry(QString("Item%1").arg(i), path);
00171       item = (Item *) item->nextSibling();
00172    }
00173    cfg->writeEntry("NrOfItems", i);
00174 }
00175 
00176 void
00177 KCustomMenuEditor::slotNewItem()
00178 {
00179    QListViewItem *item = m_listView->currentItem();
00180 
00181    KOpenWithDlg dlg(this);
00182    dlg.setSaveNewApplications(true);
00183 
00184    if (dlg.exec())
00185    {
00186       KService::Ptr s = dlg.service();
00187       if (s && s->isValid())
00188       {
00189          Item *newItem = new Item(m_listView, s);
00190          newItem->moveItem(item);
00191       }
00192       refreshButton();
00193    }
00194 }
00195 
00196 void
00197 KCustomMenuEditor::slotRemoveItem()
00198 {
00199    QListViewItem *item = m_listView->currentItem();
00200    if (!item)
00201       return;
00202 
00203    delete item;
00204    refreshButton();
00205 }
00206 
00207 void
00208 KCustomMenuEditor::slotMoveUp()
00209 {
00210    QListViewItem *item = m_listView->currentItem();
00211    if (!item)
00212       return;
00213 
00214    QListViewItem *searchItem = m_listView->firstChild();
00215    while(searchItem)
00216    {
00217       QListViewItem *next = searchItem->nextSibling();
00218       if (next == item)
00219       {
00220          searchItem->moveItem(item);
00221          break;
00222       }
00223       searchItem = next;
00224    }
00225    refreshButton();
00226 }
00227 
00228 void
00229 KCustomMenuEditor::slotMoveDown()
00230 {
00231    QListViewItem *item = m_listView->currentItem();
00232    if (!item)
00233       return;
00234 
00235    QListViewItem *after = item->nextSibling();
00236    if (!after)
00237       return;
00238 
00239    item->moveItem( after );
00240    refreshButton();
00241 }
00242 
00243 #include "kcustommenueditor.moc"
KDE Logo
This file is part of the documentation for kio Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Mar 3 19:23:39 2005 by doxygen 1.3.6 written by Dimitri van Heesch, © 1997-2003