示例#1
0
文件: Tests.cpp 项目: PowerKiKi/D-LAN
void Tests::compareExpectedResult(const Protos::Common::FindResult& result, const FindResult& expectedResult)
{
   for (int i = 0; i < result.entry_size(); i++)
   {
      QVERIFY(expectedResult.contains(result.entry(i).level()));
      QVERIFY(expectedResult[result.entry(i).level()].contains(Common::ProtoHelper::getStr(result.entry(i).entry(), &Protos::Common::Entry::name)));
   }
}
示例#2
0
文件: Tests.cpp 项目: PowerKiKi/D-LAN
void Tests::printSearch(const QString& terms, const Protos::Common::FindResult& result)
{
   qDebug() << "Search : " << terms;
   for (int i = 0; i < result.entry_size(); i++)
      qDebug() << "[" << result.entry(i).level() << "] " << Common::ProtoHelper::getStr(result.entry(i).entry(), &Protos::Common::Entry::name);
}
示例#3
0
/**
  * This method is called several times, one by received entries. The entries are inserted into the model.
  * The given entries are sorted by their level, we will keep the sort when inserting the entry but with some modifications :
  * - The directories are put first.
  * - All entries with the same level are sorted first by their path (prefixed with the shared directory name) and then by their name.
  * - All file entries with the same chunks (identical data) are grouped. They can be owned by different peer.
  */
void SearchModel::result(const Protos::Common::FindResult& findResult)
{
   if (findResult.entry_size() == 0)
      return;

   QList<const Protos::Common::FindResult_EntryLevel*> sortedEntries;
   for (int i = 0; i < findResult.entry_size(); i++)
      sortedEntries << &findResult.entry(i);
   qSort(sortedEntries.begin(), sortedEntries.end(), &findEntryLessThan);

   int currentIndex = 0;

   bool maxLevelChange = false;

   for (QListIterator<const Protos::Common::FindResult_EntryLevel*> i(sortedEntries); i.hasNext();)
   {
      const Protos::Common::FindResult_EntryLevel* entry = i.next();
      if (this->setMaxLevel(entry->level()))
         maxLevelChange = true;

      // Search if a similar entry already exists. If so then insert the new tree as child.
      if (entry->entry().type() == Protos::Common::Entry_Type_FILE && entry->entry().chunk_size() > 0)
      {
         Common::Hash firstChunk = entry->entry().chunk(0).hash();
         SearchTree* similarTree = 0;
         if ((similarTree = this->indexedFile.value(firstChunk)) && similarTree->isSameAs(entry->entry()))
         {
            if (similarTree->getNbChildren() == 0)
            {
               this->beginInsertRows(this->createIndex(0, 0, similarTree), 0, 0);
               similarTree->insertChild(similarTree);
               this->endInsertRows();
            }

            // Search the better name (tree with the lowest level) to display it on the top.
            for (int i = 0; i <= similarTree->getNbChildren(); i++)
            {
               if (i == similarTree->getNbChildren() || static_cast<SearchTree*>(similarTree->getChild(i))->getLevel() > static_cast<int>(entry->level()))
               {
                  this->beginInsertRows(this->createIndex(0, 0, similarTree), i, i);
                  Common::Hash peerID = findResult.peer_id().hash();
                  SearchTree* newTree = similarTree->insertChild(i, *entry, peerID, this->peerListModel.getNick(peerID, tr("<unknown>")));
                  this->endInsertRows();

                  if (static_cast<int>(entry->level()) < similarTree->getLevel())
                  {
                     const int row = similarTree->getOwnPosition();
                     similarTree->copyFrom(newTree);
                     emit dataChanged(this->createIndex(row, 0, similarTree), this->createIndex(row, 3, similarTree));
                  }

                  break;
               }
            }

            continue;
         }
      }

      currentIndex = this->insertTree(*entry, findResult.peer_id().hash(), currentIndex);
   }

   if (maxLevelChange && this->rowCount() > 0)
      emit dataChanged(this->createIndex(0, 2), this->createIndex(this->rowCount() - 1, 2));
}