Ejemplo n.º 1
0
void PanelBrowserMenu::slotMimeCheck()
{
    // get the first map entry
    QMap<int, bool>::Iterator it = _mimemap.begin();

    // no mime types left to check -> stop timer
    if(it == _mimemap.end()) {
        _mimecheckTimer->stop();
        return;
    }

    int id = it.key();
    QString file = _filemap[id];

    _mimemap.erase(it);

    KUrl url;
    url.setPath( path() + '/' + file );

//    KMimeType::Ptr mt = KMimeType::findByURL(url, 0, true, false);
//    QString icon(mt->icon(url, true));
    QString icon = KMimeType::iconNameForUrl( url );
//    kDebug() << url.url() << ": " << icon;

    file = fontMetrics().elidedText( file, Qt::ElideMiddle, fontMetrics().maxWidth() * 20  );
    file.replace("&", "&&");

    if(!_icons->contains(icon)) {
        QPixmap pm = SmallIcon(icon);
        if( pm.height() > 16 )
        {
            QPixmap cropped( 16, 16 );
	    QPainter p(&cropped);
            p.drawPixmap(0, 0, pm, 0, 0, 16, 16);
            pm = cropped;
        }
        _icons->insert(icon, pm);
        changeItem(id, pm, file);
    }
    else
        changeItem(id, CICON(icon), file);
}
Ejemplo n.º 2
0
void PanelBrowserMenu::slotMimeCheck()
{
    // get the first map entry
    QMap<int, bool>::Iterator it = _mimemap.begin();

    // no mime types left to check -> stop timer
    if(it == _mimemap.end()) {
        _mimecheckTimer->stop();
        return;
    }

    int id = it.key();
    QString file = _filemap[id];

    _mimemap.remove(it);

    KURL url;
    url.setPath( path() + '/' + file );

//    KMimeType::Ptr mt = KMimeType::findByURL(url, 0, true, false);
//    QString icon(mt->icon(url, true));
    QString icon = KMimeType::iconForURL( url );
//    kdDebug() << url.url() << ": " << icon << endl;

    file = KStringHandler::cEmSqueeze( file, fontMetrics(), 20 );
    file.replace("&", "&&");

    if(!_icons->contains(icon)) {
        QPixmap pm = SmallIcon(icon);
        if( pm.height() > 16 )
        {
            QPixmap cropped( 16, 16 );
            copyBlt( &cropped, 0, 0, &pm, 0, 0, 16, 16 );
            pm = cropped;
        }
        _icons->insert(icon, pm);
        changeItem(id, pm, file);
    }
    else
        changeItem(id, CICON(icon), file);
}
Ejemplo n.º 3
0
void PanelBrowserMenu::initialize()
{
    _lastpress = QPoint(-1, -1);

    // don't change menu if already visible
    if (isVisible())
        return;

    if (_dirty) {
        // directory content changed while menu was visible
        slotClear();
        setInitialized(false);
        _dirty = false;
    }

    if (initialized()) return;
    setInitialized(true);

    // start watching if not already done
    if (!_dirWatch.contains(path()))
        _dirWatch.addDir( path() );

    // setup icon map
    initIconMap();

    // clear maps
    _filemap.clear();
    _mimemap.clear();

    QDir::Filters filter = QDir::Dirs | QDir::Files;
    if (KickerSettings::showHiddenFiles())
    {
        filter |= QDir::Hidden;
    }

    QDir dir(path(), QString(), QDir::DirsFirst | QDir::Name | QDir::IgnoreCase, filter);

    // does the directory exist?
    if (!dir.exists()) {
        insertItem(i18n("Failed to Read Folder"));
	return;
    }

    // get entry list
    QFileInfoList list = dir.entryInfoList();

    KUrl url;
    url.setPath(path());
    if (!KAuthorized::authorizeUrlAction("list", KUrl(), url))
    {
        insertItem(i18n("Not Authorized to Read Folder"));
        return;
    }

    // insert file manager and terminal entries
    // only the first part menu got them
    if(_startid == 0 && !_filesOnly) {
       // FIXME: no more menu titles so NOW WHAT?!
       // insertTitle(path());
       insertItem(CICON("kfm"), i18n("Open in File Manager"), this, SLOT(slotOpenFileManager()));
        if (KAuthorized::authorizeKAction("shell_access"))
            insertItem(CICON("terminal"), i18n("Open in Terminal"), this, SLOT(slotOpenTerminal()));
    }


    bool first_entry = true;
    bool dirfile_separator = false;
    unsigned int item_count = 0;
    int run_id = _startid;

    // get list iterator
    QFileInfoList::iterator it = list.begin();

    // jump to startid
    it += _startid;

    // iterate over entry list
    for (; it != list.end(); ++it)
    {
        // bump id
        run_id++;

        QFileInfo fi = *it;
        // handle directories
        if (fi.isDir())
        {
            QString name = fi.fileName();

            // ignore . and .. entries
            if (name == "." || name == "..") continue;

            QPixmap icon;
            QString path = fi.absoluteFilePath();

            // parse .directory if it does exist
            if (QFile::exists(path + "/.directory")) {

                KDesktopFile c(path + "/.directory");
                const KConfigGroup cg = c.desktopGroup();
                QString iconPath = cg.readEntry("Icon");

                if ( iconPath.startsWith("./") )
                    iconPath = path + '/' + iconPath.mid(2);

                icon = KIconLoader::global()->loadIcon(iconPath,
                                                    KIconLoader::Small, KIconLoader::SizeSmall,
                                                    KIconLoader::DefaultState, QStringList(), 0, true);
                if(icon.isNull())
                    icon = CICON("folder");
                name = cg.readEntry("Name", name);
            }

            // use cached folder icon for directories without special icon
            if (icon.isNull())
                icon = CICON("folder");

            // insert separator if we are the first menu entry
            if(first_entry) {
                if (_startid == 0 && !_filesOnly)
                    addSeparator();
                first_entry = false;
            }

            // append menu entry
            PanelBrowserMenu *submenu = new PanelBrowserMenu(path, this);
            submenu->_filesOnly = _filesOnly;
            append(icon, name, submenu);

            // bump item count
            item_count++;

            dirfile_separator = true;
        }
        // handle files
        else if(fi.isFile())
        {
            QString name = fi.fileName();
            QString title = KIO::decodeFileName(name);

            QPixmap icon;
            QString path = fi.absoluteFilePath();

            bool mimecheck = false;

            // .desktop files
            if(KDesktopFile::isDesktopFile(path))
            {
                KDesktopFile c( path );
                const KConfigGroup cg = c.desktopGroup();
                title = cg.readEntry("Name", title);

                QString s = cg.readEntry("Icon");
                if(!_icons->contains(s)) {
                    icon  = KIconLoader::global()->loadIcon(s, KIconLoader::Small, KIconLoader::SizeSmall,
                                                         KIconLoader::DefaultState, QStringList(), 0, true);

                    if(icon.isNull()) {
                        QString type = cg.readEntry("Type", "Application");
                        if (type == "Directory")
                            icon = CICON("folder");
                        else if (type == "Mimetype")
                            icon = CICON("txt");
                        else if (type == "FSDevice")
                            icon = CICON("chardevice");
                        else
                            icon = CICON("exec");
                    }
                    else
                        _icons->insert(s, icon);
                }
                else
                    icon = CICON(s);
            }
            else {
                // set unknown icon
                icon = CICON("unknown");

                // mark for delayed mimetime check
                mimecheck = true;
            }

            // insert separator if we are the first menu entry
            if(first_entry) {
                if(_startid == 0 && !_filesOnly)
                    addSeparator();
                first_entry = false;
            }

            // insert separator if we we first file after at least one directory
            if (dirfile_separator) {
                addSeparator();
                dirfile_separator = false;
            }

            // append file entry
            append(icon, title, name, mimecheck);

            // bump item count
            item_count++;
        }

        if (item_count == KickerSettings::maxEntries2())
        {
            // Only insert a "More" item if there are actually more items.
            ++it;
            if( it != list.end() ) {
                addSeparator();
                append(CICON("kdisknav"), i18n("More"), new PanelBrowserMenu(path(), this, run_id));
            }
            break;
        }
    }

#if 0
    // WABA: tear off handles don't work together with dynamically updated
    // menus. We can't update the menu while torn off, and we don't know
    // when it is torn off.
    if(KGlobalSettings::insertTearOffHandle() && item_count > 0)
        insertTearOffHandle();
#endif

    adjustSize();

    QString dirname = path();

    int maxlen = contentsRect().width() - 40;
    if(item_count == 0)
        maxlen = fontMetrics().width(dirname);

    if (fontMetrics().width(dirname) > maxlen) {
        while ((!dirname.isEmpty()) && (fontMetrics().width(dirname) > (maxlen - fontMetrics().width("..."))))
            dirname = dirname.remove(0, 1);
        dirname.prepend("...");
    }
    setWindowTitle(dirname);

    // setup and start delayed mimetype check timer
    if(_mimemap.count() > 0) {

        if(!_mimecheckTimer)
            _mimecheckTimer = new QTimer(this);

        connect(_mimecheckTimer, SIGNAL(timeout()), SLOT(slotMimeCheck()));
        _mimecheckTimer->start(0);
    }
}