kio Library: kcustommenueditor.cpp Source File (kio Library)

| # kio Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# 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 #include <qdir.h>
    00025
    00026 #include <kbuttonbox.h>
    00027 #include <klocale.h>
    00028 #include <kglobal.h>
    00029 #include <kiconloader.h>
    00030 #include <klistview.h>
    00031 #include <kservice.h>
    00032 #include <kstandarddirs.h>
    00033 #include <kconfigbase.h>
    00034 #include <kopenwith.h>
    00035
    00036 #include "kcustommenueditor.h"
    00037
    00038 class KCustomMenuEditor::Item : public QListViewItem
    00039 {
    00040 public:
    00041    Item(QListView *parent, KService::Ptr service)
    00042      : QListViewItem(parent),
    00043        s(service)
    00044    {
    00045       init();
    00046    }
    00047
    00048    Item(QListViewItem *parent, KService::Ptr service)
    00049      : QListViewItem(parent),
    00050        s(service)
    00051    {
    00052       init();
    00053    }
    00054
    00055    void init()
    00056    {
    00057       QString serviceName = s->name();
    00058
    00059       // item names may contain ampersands. To avoid them being converted
    00060       // to accelators, replace them with two ampersands.
    00061       serviceName.replace("&", "&&");
    00062
    00063       QPixmap normal = KGlobal::instance()->iconLoader()->loadIcon(s->icon(), KIcon::Small,
    00064                               0, KIcon::DefaultState, 0L, true);
    00065
    00066       // make sure they are not larger than 16x16
    00067       if (normal.width() > 16 || normal.height() > 16) {
    00068           QImage 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     QPushButton * pbRemove;
    00083     QPushButton * pbMoveUp;
    00084     QPushButton * pbMoveDown;
    00085 };
    00086
    00087 KCustomMenuEditor::KCustomMenuEditor(QWidget *parent)
    00088   : KDialogBase(parent, "custommenueditor", true, i18n("Menu Editor"), Ok|Cancel, Ok, true),
    00089     m_listView(0)
    00090 {
    00091     d = new KCustomMenuEditorPrivate;
    00092    QHBox *page = makeHBoxMainWidget();
    00093    m_listView = new KListView(page);
    00094    m_listView->addColumn(i18n("Menu"));
    00095    m_listView->setFullWidth(true);
    00096    m_listView->setSorting(-1);
    00097    KButtonBox *buttonBox = new KButtonBox(page, Vertical);
    00098    buttonBox->addButton(i18n("New..."), this, SLOT(slotNewItem()));
    00099    d->pbRemove=buttonBox->addButton(i18n("Remove"), this, SLOT(slotRemoveItem()));
    00100    d->pbMoveUp=buttonBox->addButton(i18n("Move Up"), this, SLOT(slotMoveUp()));
    00101    d->pbMoveDown=buttonBox->addButton(i18n("Move Down"), this, SLOT(slotMoveDown()));
    00102    buttonBox->layout();
    00103    connect( m_listView, SIGNAL( selectionChanged () ), this, SLOT( refreshButton() ) );
    00104    refreshButton();
    00105 }
    00106
    00107 KCustomMenuEditor::~KCustomMenuEditor()
    00108 {
    00109     delete d;
    00110     d=0;
    00111 }
    00112
    00113 void KCustomMenuEditor::refreshButton()
    00114 {
    00115     QListViewItem *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(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 (QDir::isRelativePath(path) || QDir::isRelativePath(KGlobal::dirs()->relativeLocation("xdgdata-apps", path)))
    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"

This file is part of the documentation for kio Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:23:55 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003