Esempio n. 1
0
    /**
    * Find sub-project represented by @param item in the projects DOM document.
    * This will return the root-node (i.e. documentElement) if sub-project
    * is not found.
    * @param item - the ProjectFolderItem representing the sub-project
    * @param name - the value of the name attribute of the element to look for
    * @return a QDomElement representing @param item in the DOM document or the document element
    */
    QDomElement findElementInDocument( ProjectBaseItem *item, const QString &name )
    {
        QStack<QString> domPath;
        domPath.push( name );
        #if KDEV_PLUGIN_VERSION >= 10
        if (!item->parent()) {
            //this must be the project root
            return projectDomDocument.documentElement();
        }
        ProjectBaseItem *wItem = item;
        while ((wItem = wItem->parent())){
            if (wItem->folder())
                domPath.push( wItem->folder()->folderName() );
        };
        //remove last element, because it’s the root element, we don’t want to find that
        domPath.pop();
        #else
        // for older kdevplatform versions
        //TODO: can this be done without dynamic_cast?
        ProjectFolderItem *wItem = dynamic_cast<ProjectFolderItem*>( item->parent() );
        if (!wItem) {
             //this must be the project root
             return projectDomDocument.documentElement();
         }
        while (!wItem->isProjectRoot()){
            domPath.push( wItem->folderName() );
            //TODO: can this be done without dynamic_cast?
            wItem = dynamic_cast<ProjectFolderItem*>( wItem->parent() );
         };
        #endif

        QDomElement child = projectDomDocument.documentElement();
        while ( !domPath.isEmpty() ){
            QString nextFolder = domPath.pop();
            QDomElement d = child.firstChildElement("item");
            while (!d.isNull() && d.attribute("name") != nextFolder){
                if (d.attribute("name") == ""){
                    kWarning() << "no, this shouldn't happen";
                    return QDomElement();
                }
                d = d.nextSiblingElement("item");
            }
            child = d;
        }
        return child;
    }