QTreeWidgetItem *CollectionTreeWidget::addAlbum(QString artist, QString album, unsigned int albumId) {
    // Find id in database if we don't have it
    if (albumId == 0) {
         QSqlTableModel *model = service->collectionModel();

         // SQLite used two single quotes to escape a single quote! :)
         QString filter = "artist = '" + QString(artist).replace("'","''") + "' AND "
                          "album = '" + QString(album).replace("'","''") + "'";
         model->setFilter(filter);
         model->select();

         while (model->canFetchMore()) model->fetchMore();
         int total = model->rowCount();
         if (total > 0) {
            albumId = model->record(0).value(model->fieldIndex("id_album")).toInt();
         }
         else {
             qDebug("ERROR: no album found! -- " + model->filter().toUtf8());
             return NULL;
         }
    }

    // Looks for the artist
    QTreeWidgetItem *newAlbumNode; // The node with the album, whether it exists or not
    QTreeWidgetItem *artistItem;
    artistItem = addArtist(artist);

    // Look for album
    for (int i = 0; i < artistItem->childCount(); i++) {
        if (artistItem->child(i)->text(0) == album) {
            newAlbumNode = artistItem->child(i);
            return newAlbumNode;
        }
    }

    // Create our new album node and add it if it was not found
    newAlbumNode = new CollectionTreeWidgetItem(LevelAlbum, albumId, (QTreeWidget*)0);
    newAlbumNode->setText(0, album);
    newAlbumNode->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator);

    // Set icon
    newAlbumNode->setIcon(0, IconFactory::fromTheme("media-cdrom"));

    artistItem->addChild(newAlbumNode);
    artistItem->sortChildren(0, Qt::AscendingOrder);

    return newAlbumNode;
}
QTreeWidgetItem *CollectionTreeWidget::addArtist(QString artist, unsigned int id) {
    if (id == 0) {
        QSqlTableModel *model = service->artistModel();

        // SQLite uses two single quotes to escape a single quote! :)
        QString filter = "name = '" + QString(artist).replace("'","''") + "'";
        model->setFilter(filter);
        model->select();

        while (model->canFetchMore()) model->fetchMore();
        int total = model->rowCount();
        if (total > 0) {
            id = model->record(0).value(model->fieldIndex("id")).toInt();
        }
        else {
            qDebug("ERROR: no artist found! -- " + model->filter().toUtf8());
            return NULL;
        }
    }

    QList<QTreeWidgetItem*> artistList = findItems(artist, Qt::MatchExactly, 0);
    if (artistList.isEmpty()) {
        CollectionTreeWidgetItem *item = new CollectionTreeWidgetItem(LevelArtist, id, (QTreeWidget*)0);
        item->setText(0, artist);
        item->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator);

        // Set font to bold
        QFont font = item->font(0);
        font.setBold(true);
        item->setFont(0, font);

        // Set icon
        item->setIcon(0, IconFactory::fromTheme("folder"));

        // Insert item
        insertTopLevelItem(0, item);
        sortItems(0, Qt::AscendingOrder);

        return item;
    }
    else {
        return artistList.first();
    }
}
Exemplo n.º 3
-1
CollectionTreeWidgetItem *CollectionTreeWidget::addMusic(Music *music, unsigned int id) {
    // Find id in database if we don't have it
    if (id == 0) {
         QSqlTableModel *model = service->collectionModel();

         // SQLite used two single quotes to escape a single quote! :)
         QString filter = "artist = '" + music->getArtist().replace("'","''") + "' AND "
                          "album = '" + music->getAlbum().replace("'","''") + "' AND "
                          "music = '" + music->getTitle().replace("'","''") + "'";
         model->setFilter(filter);
         model->select();

         while (model->canFetchMore()) model->fetchMore();
         int total = model->rowCount();
         if (total > 0) {
            id = model->record(0).value(model->fieldIndex("id_music")).toInt();
         }
         else {
             qDebug("ERROR: no songs found! -- " + model->filter().toUtf8());
             return NULL;
         }
    }

    // Looks for the album
    QTreeWidgetItem *albumItem = addAlbum(music->getArtist(), music->getAlbum());

    // Create our new music node and add it if it was not found
    removeMusic(id);

    CollectionTreeWidgetItem *newMusicNode = new CollectionTreeWidgetItem(LevelMusic, id, (QTreeWidget*)0);
    newMusicNode->setText(0, music->getTitle());
    newMusicNode->setIcon(0, IconFactory::fromTheme("sound"));
    albumItem->addChild(newMusicNode);
    musicList.append(newMusicNode);

    return newMusicNode;
}