Example #1
0
UBAppleWidget::UBAppleWidget(const QUrl& pWidgetUrl, QWidget *parent)
    : UBAbstractWidget(pWidgetUrl, parent)
{
    QString path = pWidgetUrl.toLocalFile();

    if (!path.endsWith(".wdgt") && !path.endsWith(".wdgt/"))
    {
        int lastSlashIndex = path.lastIndexOf("/");
        if (lastSlashIndex > 0)
        {
            path = path.mid(0, lastSlashIndex + 1);
        }
    }

    QFile plistFile(path + "/Info.plist");
    plistFile.open(QFile::ReadOnly);

    QByteArray plistBin = plistFile.readAll();
    QString plist = QString::fromUtf8(plistBin);

    int mainHtmlIndex = plist.indexOf("MainHTML");
    int mainHtmlIndexStart = plist.indexOf("<string>", mainHtmlIndex);
    int mainHtmlIndexEnd = plist.indexOf("</string>", mainHtmlIndexStart);

    if (mainHtmlIndex > -1 && mainHtmlIndexStart > -1 && mainHtmlIndexEnd > -1)
    {
        mMainHtmlFileName = plist.mid(mainHtmlIndexStart + 8, mainHtmlIndexEnd - mainHtmlIndexStart - 8);
    }

    mMainHtmlUrl = pWidgetUrl;
    mMainHtmlUrl.setPath(pWidgetUrl.path() + "/" + mMainHtmlFileName);

    QWebView::load(mMainHtmlUrl);

    QPixmap defaultPixmap(pWidgetUrl.toLocalFile() + "/Default.png");

    setFixedSize(defaultPixmap.size());

    mNominalSize = defaultPixmap.size();
}
KNMusicPlaylistModel *KNMusicPlaylistiTunesXMLParser::read(
        const QString &filePath)
{
    //Get the plist file.
    QFile plistFile(filePath);
    //Open the playlist file first.
    if(!plistFile.open(QIODevice::ReadOnly))
    {
        return nullptr;
    }
    //Generate a dom document to parse the file.
    QDomDocument plistDocument;
    //Use QDomDocument to parse the file.
    if(!plistDocument.setContent(&plistFile, true))
    {
        //If there's any error, return nullptr.
        return nullptr;
    }
    //Close the playlist file.
    plistFile.close();
    //Get the 'plist' element from the document.
    QDomElement plistRoot=plistDocument.documentElement();
    //Check the 'plist' element.
    if(plistRoot.nodeName()!="plist" ||
                !plistRoot.hasAttribute("version") ||
                plistRoot.attribute("version")!="1.0")
    {
        return nullptr;
    }
    //Get the dict elements from the 'plist'.
    QDomNodeList root=plistRoot.elementsByTagName("dict");
    //Check the list data.
    if(root.isEmpty())
    {
        return nullptr;
    }
    //Pick out the 'dict' element.
    QDomNodeList dictElement=root.at(0).toElement().childNodes();
    //Check 'dict' childe nodes size.
    if(dictElement.isEmpty())
    {
        return nullptr;
    }
    //Initial the track info hash.
    QHash<QString, QString> musicLocateHash;
    QStringList playlistIndexList;
    //Prepare the playlist.
    KNMusicPlaylistModel *model=new KNMusicPlaylistModel(thread());
    //Get the first node.
    QDomElement keyNode=dictElement.at(0).toElement(),
                valueNode=keyNode.nextSiblingElement();
    //Check the current key node.
    while(!keyNode.isNull() && !keyNode.isNull())
    {
        //Check the name of the node.
        //For 'tracks':
        if(keyNode.text()=="Tracks")
        {
            //Check whether the child nodes is empty.
            if(!valueNode.childNodes().isEmpty())
            {
                //Get all the tracks information.
                QDomElement trackKeyNode=
                                valueNode.childNodes().at(0).toElement(),
                            trackValueNode=
                                trackKeyNode.nextSiblingElement();
                //Pick out all the tracks.
                while(!trackKeyNode.isNull() && !trackValueNode.isNull())
                {
                    //Parse the track dict information.
                    QDomElement songKeyNode=
                                  trackValueNode.childNodes().at(0).toElement(),
                                songValueNode=
                                  songKeyNode.nextSiblingElement();
                    //If all the information is valid.
                    while(!songKeyNode.isNull() && !songValueNode.isNull())
                    {
                        //Get the location of the song.
                        if(songKeyNode.text()=="Location")
                        {
                            //Get the url from the file.
                            //Use QUrl to translate it to a readable string.
                            QString rawUrlText=
                                    QUrl::fromPercentEncoding(
                                        songValueNode.text().toUtf8());
                            //Remove the protocol name.
                            if(rawUrlText.length()>17 &&
                                    rawUrlText.left(17)=="file://localhost/")
                            {
                                rawUrlText.remove(0, 17);
                            }
                            else
                            {
                                rawUrlText=QUrl(rawUrlText).path();
                            }
                            //Set the hash.
                            musicLocateHash.insert(
                                        trackKeyNode.text(),
                                        QFileInfo(rawUrlText).absoluteFilePath()
                                        );
                            break;
                        }
                        //Switch to the next song elements.
                        songKeyNode=songValueNode.nextSiblingElement();
                        songValueNode=songKeyNode.nextSiblingElement();
                    }
                    //Switch to the track elements.
                    trackKeyNode=trackValueNode.nextSiblingElement();
                    trackValueNode=trackKeyNode.nextSiblingElement();
                }
            }
        }
        //For 'Playlists'.
        else if(keyNode.text()=="Playlists")
        {
            //Check the node size.
            if(valueNode.childNodes().size()!=0)
            {
                //Get the dict of the playlist.
                QDomElement playlistDict=
                        valueNode.childNodes().at(0).toElement();
                //Check the size of playlist dict.
                if(playlistDict.childNodes().size()!=0)
                {
                    //Get the playlist keys.
                    QDomElement playlistKey=
                            playlistDict.childNodes().at(0).toElement(),
                                playlistValue=
                            playlistKey.nextSiblingElement();
                    //Read the playlist information.
                    while(!playlistKey.isNull() && !playlistValue.isNull())
                    {
                        //For 'Name' element, it stored the name of the
                        //playlist.
                        if(playlistKey.text()=="Name")
                        {
                            //Set the title to model.
                            model->setTitle(playlistValue.text());
                        }
                        //For 'Playlist Items' element, it stored the file path
                        //information of the songs.
                        else if(playlistKey.text()=="Playlist Items")
                        {
                            //Check the array is empty or not.
                            QDomNodeList itemsArray=playlistValue.childNodes();
                            if(!itemsArray.isEmpty())
                            {
                                //Get all the information from the items array.
                                for(int i=0; i<itemsArray.size(); i++)
                                {
                                    QDomElement currentTrack=
                                            itemsArray.at(i).toElement();
                                    if(currentTrack.childNodes().size()==2)
                                    {
                                        QDomElement trackElement=
                                                        currentTrack.
                                                        childNodes().
                                                        at(0).toElement(),
                                                    trackIndex=
                                                        trackElement.
                                                        nextSiblingElement();
                                        //Check if the track element is
                                        //'Track ID' we will save track info.
                                        if(trackElement.text()=="Track ID")
                                        {
                                            //Insert the track.
                                            playlistIndexList.append(
                                                        trackIndex.text());
                                        }
                                    }
                                }
                            }
                        }
                        //Switch to the next playlist key.
                        playlistKey=playlistValue.nextSiblingElement();
                        playlistValue=playlistKey.nextSiblingElement();
                    }
                }
            }
        }
        //Switch to the next elements.
        keyNode=valueNode.nextSiblingElement();
        valueNode=keyNode.nextSiblingElement();
    }
    //Prepare the file list.
    QStringList fileList;
    //Translate the index list to file list.
    for(auto i=playlistIndexList.constBegin();
        i!=playlistIndexList.constEnd();
        ++i)
    {
        //Get the file path according to the hash table.
        QString filePath=musicLocateHash.value((*i), QString());
        //If the file is not empty,
        if(!filePath.isEmpty())
        {
            //Add to file list.
            fileList.append(filePath);
        }
    }
    //Add the file list to model.
    model->appendFiles(fileList);
    //Set the changed flag.
    model->setChanged(true);
    //Give back the model.
    return model;
}
Example #3
0
void QNapiConfig::setShowDockIcon(bool show)
{
    QString infoPlistPath = QFileInfo(QApplication::applicationDirPath() + "/../Info.plist").canonicalFilePath();
    
    QFile plistFile(infoPlistPath);
    
    QDomDocument doc;
    if(!doc.setContent(&plistFile) || !doc.hasChildNodes())
        return;
    
    QDomNodeList nodes = doc.childNodes();
    
    QDomNode node;
    int i;
    for(i = 0; i < nodes.size(); ++i)
    {
        node = nodes.at(i);
        if(node.nodeName() == "plist")
            break;
    }
    
    if((i == nodes.size()) || !node.hasChildNodes())
        return;
    
    nodes = node.childNodes();
    
    for(i = 0; i < nodes.size(); ++i)
    {
        node = nodes.at(i);
        if(node.nodeName() == "dict")
            break;
    }
    
    if((i == nodes.size()) || !node.hasChildNodes())
        return;
    
    nodes = node.childNodes();
    
    for(i = 0; i < nodes.size(); ++i)
    {
        node = nodes.at(i);
        
        QString subText;
        
        if(node.hasChildNodes())
        {
            subText = node.childNodes().at(0).toText().data();
        }
        
        if(subText == "LSUIElement")
            break;
    }
    
    if(i >= nodes.size())
        return;
    
    node = node.nextSibling();
    
    node.toElement().setTagName(show ? "false" : "true");

    QString modifiedContent = doc.toString(4);

    plistFile.close();
    
    if(!plistFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
        return;

    QTextStream plistStream(&plistFile);
    
    plistStream << modifiedContent;
    
    plistFile.close();
    
}
Example #4
0
bool QNapiConfig::showDockIcon()
{
    const bool show_default = true;
    
    QString infoPlistPath = QFileInfo(QApplication::applicationDirPath() + "/../Info.plist").canonicalFilePath();

    QFile plistFile(infoPlistPath);
    
    QDomDocument doc;
    if(!doc.setContent(&plistFile))
        return show_default;

    if(!doc.hasChildNodes())
        return show_default;

    QDomNodeList nodes = doc.childNodes();
    
    QDomNode node;
    int i;
    for(i = 0; i < nodes.size(); ++i)
    {
        node = nodes.at(i);
        if(node.nodeName() == "plist")
            break;
    }
    
    if(i == nodes.size())
        return show_default;

    if(!node.hasChildNodes())
        return show_default;

    nodes = node.childNodes();
    
    for(i = 0; i < nodes.size(); ++i)
    {
        node = nodes.at(i);
        if(node.nodeName() == "dict")
            break;
    }

    if(i == nodes.size())
        return show_default;
    
    if(!node.hasChildNodes())
        return show_default;
    
    nodes = node.childNodes();

    for(i = 0; i < nodes.size(); ++i)
    {
        node = nodes.at(i);

        QString subText;

        if(node.hasChildNodes())
        {
            subText = node.childNodes().at(0).toText().data();
        }

        if(subText == "LSUIElement")
            break;
    }

    if(i < nodes.size())
    {
        node = node.nextSibling();
        return (node.nodeName() != "true");
    }

    return show_default;
    
}