Beispiel #1
0
bool PlaylistSaver::saveM3U(const KURL &file, int opt)
{
//	kdDebug(66666) << k_funcinfo << "file='" << file.path() << "', opt=" << opt << endl;

    bool isExt=(opt==EXTM3U); // easier ;)

    QString local(napp->tempSaveName(file.path()));
    QFile saver(local);
    saver.open(IO_ReadWrite | IO_Truncate);
    QTextStream t(&saver);

    reset();
    PlaylistItem i;
    QStringList props;

    // this is more code but otoh faster than checking for isExt inside the loop
    if(isExt)
    {
        t << "#EXTM3U" << '\n';

        while ((i=writeItem()))
        {
            int length = static_cast<int>(((i.property("length")).toInt())/1000);
            if(length==0) length=-1; // special value in an EXTM3U file, means "unknown"
            KURL u(i.property("url"));
            QString title;

            // if a playlistitem is without a tag or ONLY title is set
            if((i.property("author").isEmpty() && i.property("title").isEmpty())
                    || (i.property("author").isEmpty() && !i.property("title").isEmpty()) )
                title = u.filename().left(u.filename().length()-4);
            else
                title = i.property("author") + " - " + i.property("title");

//			kdDebug(66666) << "#EXTINF:"<< QString::number(length) << "," << title << endl;
            t << "#EXTINF:"<< QString::number(length) << "," << title << '\n';

            if (u.isLocalFile())
                t << u.path() << '\n';
            else
                t << u.url() << '\n';
        }
    }
    else
    {
        while ((i=writeItem()))
        {
            KURL u(i.property("url"));
            if (u.isLocalFile())
                t << u.path() << '\n';
            else
                t << u.url() << '\n';
        }
    }

    saver.close();
    KIO::NetAccess::upload(local, file, 0L);
    saver.remove();
    return true;
}
Beispiel #2
0
bool PlaylistSaver::saveXML(const KURL &file, int )
{
    QString localFile;
    if (file.isLocalFile())
        localFile = QFile::encodeName(file.path());
    else
        localFile = napp->tempSaveName(file.path());

    // QDom is a pain :)
    QDomDocument doc("playlist");
    doc.setContent(QString("<!DOCTYPE XMLPlaylist><playlist version=\"1.0\" client=\"noatun\"/>"));

    QDomElement docElem=doc.documentElement();

    reset();
    PlaylistItem i;
    QStringList props;
    while ((i=writeItem()))
    {
        // write all properties
        props=i.properties();
        QDomElement elem=doc.createElement("item");
        for (QStringList::Iterator pi(props.begin()); pi!=props.end(); ++pi)
        {
            QString val=i.property(*pi);
            elem.setAttribute(*pi, val);

            if ((*pi)=="url")
            {
                KURL u(val);
                if (u.isLocalFile())
                {
                    elem.setAttribute("local", u.path());
                }
            }
        }

        docElem.appendChild(elem);
        props.clear();
    }
    Noatun::KSaver saver(localFile);
    if (!saver.open())
        return false;
    saver.textStream().setEncoding(QTextStream::UnicodeUTF8);
    saver.textStream() << doc.toString();
    saver.close();

    return true;
}