Exemple #1
0
/**
 * @brief Accesses items data
 * @param index Index of item
 * @param role Role
 * @return data
 */
QVariant ConcertModel::data(const QModelIndex &index, int role) const
{
    if (index.row() < 0 || index.row() > m_concerts.count())
        return QVariant();

    if (role == Qt::UserRole)
        return index.row();

    Concert *concert = m_concerts[index.row()];
    if (index.column() == 0 && role == Qt::DisplayRole) {
        return Helper::appendArticle(concert->name());
    } else if (index.column() == 0 && role == Qt::ToolTipRole) {
        if (concert->files().size() == 0)
            return QVariant();
        return concert->files().at(0);
    } else if (index.column() == 1 && role == Qt::DisplayRole) {
        return concert->folderName();
    } else if (role == Qt::UserRole+1) {
        return concert->controller()->infoLoaded();
    } else if (role == Qt::UserRole+2) {
        return concert->hasChanged();
    } else if (role == Qt::UserRole+3) {
        return concert->syncNeeded();
    /*
    } else if (role == Qt::ForegroundRole) {
        if (concert->hasChanged())
            return QColor(255, 0, 0);
    */
    } else if (role == Qt::FontRole) {
        if (concert->hasChanged()) {
            QFont font;
            font.setItalic(true);
            return font;
        }
    } else if (role == Qt::DecorationRole) {
        if (!concert->controller()->infoLoaded())
            return m_newIcon;
        else if (concert->syncNeeded())
            return m_syncIcon;
    }
    return QVariant();
}
/**
 * @brief Starts the scanning process
 */
void ConcertFileSearcher::reload(bool force)
{
    m_aborted = false;

    if (force)
        Manager::instance()->database()->clearConcerts();

    Manager::instance()->concertModel()->clear();
    emit searchStarted(tr("Searching for Concerts..."), m_progressMessageId);

    QList<Concert*> concerts;
    QList<Concert*> dbConcerts;
    QList<QStringList> contents;
    foreach (SettingsDir dir, m_directories) {
        if (m_aborted)
            return;

        QList<Concert*> concertsFromDb = Manager::instance()->database()->concerts(dir.path);
        if (dir.autoReload || force || concertsFromDb.count() == 0) {
            Manager::instance()->database()->clearConcerts(dir.path);
            scanDir(dir.path, dir.path, contents, dir.separateFolders, true);
        } else {
            dbConcerts.append(concertsFromDb);
        }
    }
    emit currentDir("");

    emit searchStarted(tr("Loading Concerts..."), m_progressMessageId);
    int concertCounter=0;
    int concertSum=contents.size()+dbConcerts.size();

    // Setup concerts
    Manager::instance()->database()->transaction();
    foreach (const QStringList &files, contents) {
        if (m_aborted)
            return;

        bool inSeparateFolder = false;
        QString path;
        // get directory
        if (!files.isEmpty()) {
            int index = -1;
            for (int i=0, n=m_directories.count() ; i<n ; ++i) {
                if (files.at(0).startsWith(m_directories[i].path)) {
                    if (index == -1)
                        index = i;
                    else if (m_directories[index].path.length() < m_directories[i].path.length())
                        index = i;
                }
            }
            if (index != -1) {
                inSeparateFolder = m_directories[index].separateFolders;
                path = m_directories[index].path;
            }
        }
        Concert *concert = new Concert(files, this);
        concert->setInSeparateFolder(inSeparateFolder);
        concert->controller()->loadData(Manager::instance()->mediaCenterInterface());
        emit currentDir(concert->name());
        Manager::instance()->database()->add(concert, path);
        concerts.append(concert);
        emit progress(++concertCounter, concertSum, m_progressMessageId);
    }
    Manager::instance()->database()->commit();

    // Setup concerts loaded from database
    foreach (Concert *concert, dbConcerts) {
        if (m_aborted)
            return;

        concert->controller()->loadData(Manager::instance()->mediaCenterInterface(), false, false);
        emit currentDir(concert->name());
        concerts.append(concert);
        emit progress(++concertCounter, concertSum, m_progressMessageId);
    }

    foreach (Concert *concert, concerts)
        Manager::instance()->concertModel()->addConcert(concert);

    qDebug() << "Searching for concerts done";
    if (!m_aborted)
        emit concertsLoaded(m_progressMessageId);
}