Source Code (Use browser search to find items of interest.)

Class Index

kdelibs'KApplicationTree (./kdelibs/kfile/kopenwith.h:159)

class KApplicationTree : public KListView
{
    Q_OBJECT
public:
    KApplicationTree( QWidget *parent );

    bool isDesktopFile( const QString& filename );

    /**
     * Parse a single .desktop/.kdelnk file
     */
    void parseDesktopFile( QFileInfo *fi, KAppTreeListItem *item, QString relPath );
    /**
     * Parse a directory for .desktop/.kdelnk files
     */
    void parseDesktopDir( QString relPath, KAppTreeListItem *item = 0 );

    KAppTreeListItem *it;

protected:
    void resizeEvent( QResizeEvent *_ev );

public slots:
    void slotItemHighlighted(QListViewItem* i);
    void slotSelectionChanged(QListViewItem* i);

signals:
    void selected( const QString& _name, const QString& _exec );
    void highlighted( const QString& _name, const  QString& _exec );
};

/* ------------------------------------------------------------------------- */

kdelibs'KApplicationTree::KApplicationTree() (./kdelibs/kfile/kopenwith.cpp:120)

KApplicationTree::KApplicationTree( QWidget *parent )
    : KListView( parent )
{
    addColumn( i18n("Known Applications") );
    setRootIsDecorated( true );

    parseDesktopDir( QString::null );

    connect( this, SIGNAL( currentChanged(QListViewItem*) ), SLOT( slotItemHighlighted(QListViewItem*) ) );
    connect( this, SIGNAL( selectionChanged(QListViewItem*) ), SLOT( slotSelectionChanged(QListViewItem*) ) );
}


// ----------------------------------------------------------------------


kdelibs'KApplicationTree::isDesktopFile() (./kdelibs/kfile/kopenwith.cpp:135)

bool KApplicationTree::isDesktopFile( const QString& filename )
{
    QFile file(filename);
    if (!file.open(IO_Raw | IO_ReadOnly))
        return false;

    QByteArray buffer(100);
    file.readBlock(buffer.data(), 100);
    file.close();

    KMimeMagicResult *result = KMimeMagic::self()->findBufferType( buffer );
    if (!result)
        return false;

    if (result->mimeType() != QString::fromLatin1("application/x-desktop"))
        return false;

    return true;
}


// ----------------------------------------------------------------------


kdelibs'KApplicationTree::parseDesktopFile() (./kdelibs/kfile/kopenwith.cpp:158)

void KApplicationTree::parseDesktopFile( QFileInfo *fi, KAppTreeListItem *item, QString relPath )
{
    QPixmap pixmap;
    QString text_name, pixmap_name, mini_pixmap_name, big_pixmap_name, command_name, comment;

    QString file = fi->absFilePath();

    if( fi->isDir() ) {
        // don't create empty directory items
        QDir dir( file );
	if ( dir.entryList().count() == 2 ) // . and ..
	    return;
	
        text_name = fi->fileName();
        file += QString::fromLatin1("/.directory");
    }
    else {
        int pos = fi->fileName().find( QString::fromLatin1(".desktop") );
        if( pos >= 0 )
            text_name = fi->fileName().left( pos );
        else {
            pos = fi->fileName().find(QString::fromLatin1(".kdelnk"));
            if (pos >= 0)
                text_name = fi->fileName().left(pos);
            else
                text_name = fi->fileName();
        }
    }

    QFile config( file );

    if( config.exists() ) {
        KSimpleConfig kconfig( file, true );
        kconfig.setDesktopGroup();
        command_name      = kconfig.readEntry(QString::fromLatin1("Exec"));
        mini_pixmap_name  = kconfig.readEntry(QString::fromLatin1("MiniIcon"));
        big_pixmap_name   = kconfig.readEntry(QString::fromLatin1("Icon"));
        comment           = kconfig.readEntry(QString::fromLatin1("Comment"));
        text_name         = kconfig.readEntry(QString::fromLatin1("Name"), text_name);

        if( !mini_pixmap_name.isEmpty() )
	  pixmap = SmallIcon(mini_pixmap_name);
        if( pixmap.isNull() && !big_pixmap_name.isEmpty() )
	  pixmap = SmallIcon(big_pixmap_name);
        if( pixmap.isNull() )
            pixmap = SmallIcon(QString::fromLatin1("default"));
    }
    else {
        command_name = text_name;
	pixmap = SmallIcon(QString::fromLatin1("default"));
    }

    KAppTreeListItem * newItem;
    if(item)
      newItem = new KAppTreeListItem( item, text_name, pixmap, false, fi->isDir(),
                                      relPath + fi->fileName(), command_name );
    else
      newItem = new KAppTreeListItem( this, text_name, pixmap, false, fi->isDir(),
                                      relPath + fi->fileName(), command_name );

    if ( fi->isDir() )
      newItem->setExpandable( true );
}


// ----------------------------------------------------------------------


kdelibs'KApplicationTree::parseDesktopDir() (./kdelibs/kfile/kopenwith.cpp:225)

void KApplicationTree::parseDesktopDir( QString relPath, KAppTreeListItem *item)
{
    // for one relative path, like "Applications", there can be several real
    // dirs (ex : a global one and a local one). Parse them both.
    QStringList list = KGlobal::dirs()->findDirs("apps", relPath);
    for (QStringList::ConstIterator dirsit = list.begin(); dirsit != list.end(); dirsit++) {
        //debug(QString("(*dirsit): '%1'").arg((*dirsit)));
        QDir d( (*dirsit) );
        if( d.exists() ) {
            debug("dirs exists");
            d.setSorting( SORT_SPEC );
            QList <QString> item_list;

            const QFileInfoList *list = d.entryInfoList();
            QFileInfoListIterator it( *list );
            QFileInfo *fi;

            while( ( fi = it.current() ) ) {
                if( fi->fileName() == QString::fromLatin1(".") || fi->fileName() == QString::fromLatin1("..") ) {
                    ++it;
                    continue;
                }
                // check if item already in list (e.g. parsed from ~/.kde)
                QListViewItem * pChild = item ? item->firstChild() : this->firstChild();
                bool found = false;
                while( pChild && !found ) {
                    found = ( ((KAppTreeListItem *)pChild)->path == relPath + fi->fileName() );
                    pChild = pChild->nextSibling();
                }
                if ( found ) {
                    kdDebug() << "skipping " << *dirsit << " | " << fi->fileName() << endl;
                    ++it;
                    continue;
                }

                if( fi->isDir() ) {
                    parseDesktopFile( fi, item, relPath );
                }
                else {
                    if( !isDesktopFile( fi->absFilePath() ) ) {
                        ++it;
                        continue;
                    }
                    parseDesktopFile( fi, item, relPath );
                }
                ++it;
            }
        }
    }
}


// ----------------------------------------------------------------------


kdelibs'KApplicationTree::slotItemHighlighted() (./kdelibs/kfile/kopenwith.cpp:279)

void KApplicationTree::slotItemHighlighted(QListViewItem* i)
{
    // i may be 0 (see documentation)
    if(!i)
        return;

    KAppTreeListItem *item = (KAppTreeListItem *) i;

    if( (!item->directory ) && (!item->exec.isEmpty()) )
        emit highlighted( item->text(0), item->exec );
}


// ----------------------------------------------------------------------


kdelibs'KApplicationTree::slotSelectionChanged() (./kdelibs/kfile/kopenwith.cpp:294)

void KApplicationTree::slotSelectionChanged(QListViewItem* i)
{
    // i may be 0 (see documentation)
    if(!i)
        return;

    KAppTreeListItem *item = (KAppTreeListItem *) i;

    if( ( !item->directory ) && (!item->exec.isEmpty() ) )
        emit selected( item->text(0), item->exec );
}

// ----------------------------------------------------------------------


kdelibs'KApplicationTree::resizeEvent() (./kdelibs/kfile/kopenwith.cpp:308)

void KApplicationTree::resizeEvent( QResizeEvent * e)
{
    setColumnWidth(0, width()-QApplication::style().scrollBarExtent().width());
    KListView::resizeEvent(e);
}


/***************************************************************
 *
 * KOpenWithDlg
 *
 ***************************************************************/