Beispiel #1
0
void Tests::createAnEmptyFile()
{
   qDebug() << "===== createAnEmptyFile() =====";

   Protos::Common::Entry remoteEntry;
   remoteEntry.set_path("/remoteShare1/");
   remoteEntry.set_name("remoteFile.txt");
   remoteEntry.set_size(1 * 1024 * 1024); // 1Mo.

   try
   {
      QList<QSharedPointer<IChunk>> chunks = this->fileManager->newFile(remoteEntry);
      for (int i = 0; i < chunks.size(); i++)
         QVERIFY(chunks[i]->getHash().isNull());
   }
   catch(NoWriteableDirectoryException&)
   {
      QFAIL("NoWriteableDirectoryException");
   }
   catch(InsufficientStorageSpaceException&)
   {
      QFAIL("InsufficientStorageSpaceException");
   }
   catch(UnableToCreateNewFileException&)
   {
      QFAIL("UnableToCreateNewFileException");
   }
}
Beispiel #2
0
void Tests::getHashesFromAFileEntry2()
{
   qDebug() << "===== getHashesFromAFileEntry2() =====";

   {
      QFile file1("sharedDirs/big2.bin");
      file1.open(QIODevice::WriteOnly);

      QFile file2("sharedDirs/big3.bin");
      file2.open(QIODevice::WriteOnly);

      file1.resize(128 * 1024 * 1024); // 128Mo
      file2.resize(128 * 1024 * 1024); // 128Mo
   }
   QTest::qWait(2000); // Begin the computing of the big2.bin hashes.

   Protos::Common::Entries sharedDirs = this->fileManager->getEntries();
   const string sharedDirId = sharedDirs.entry(1).shared_dir().id().hash();

   Protos::Common::Entry entry;
   entry.set_path("/");
   entry.set_name("big3.bin");
   entry.mutable_shared_dir()->mutable_id()->set_hash(sharedDirId);
   QSharedPointer<IGetHashesResult> result = this->fileManager->getHashes(entry);

   HashesReceiver hashesReceiver;
   connect(result.data(), SIGNAL(nextHash(Common::Hash)), &hashesReceiver, SLOT(nextHash(Common::Hash)));
   Protos::Core::GetHashesResult res = result->start(); // Should stop the computing of 'big2.bin' and switch to 'big3.bin'.
   QCOMPARE(res.status(), Protos::Core::GetHashesResult_Status_OK);

   QTest::qWait(4000);
}
Beispiel #3
0
bool File::matchesEntry(const Protos::Common::Entry& entry) const
{
   QMutexLocker locker(&this->mutex);

   return
      this->getRoot()->getId() == entry.shared_dir().id().hash() &&
      this->getPath() == Common::ProtoHelper::getStr(entry, &Protos::Common::Entry::path) &&
      this->getSize() == static_cast<qint64>(entry.size()) &&
      Global::removeUnfinishedSuffix(this->getName()) == Global::removeUnfinishedSuffix(Common::ProtoHelper::getStr(entry, &Protos::Common::Entry::name));
}
Beispiel #4
0
bool SearchModel::SearchTree::isSameAs(const Protos::Common::Entry& otherEntry) const
{
   if (otherEntry.chunk_size() != this->getItem().chunk_size())
      return false;

   for (int i = 0; i < otherEntry.chunk_size(); i++)
      if (Common::Hash(otherEntry.chunk(i).hash()) != Common::Hash(this->getItem().chunk(i).hash()))
         return false;

   return true;
}
Beispiel #5
0
bool entryLessThan(const Protos::Common::Entry& e1, const Protos::Common::Entry& e2)
{
   std::string s1;
   std::string s2;

   if (e1.has_shared_dir() && e2.has_shared_dir())
   {
      s1 = e1.shared_dir().shared_name();
      s2 = e2.shared_dir().shared_name();
      std::transform(s1.begin(), s1.end(), s1.begin(), tolower);
      std::transform(s2.begin(), s2.end(), s2.begin(), tolower);
   }

   if (s1 == s2)
   {
      std::string p1 = e1.path();
      std::string p2 = e2.path();
      std::transform(p1.begin(), p1.end(), p1.begin(), tolower);
      std::transform(p2.begin(), p2.end(), p2.begin(), tolower);

      if (p1 == p2)
      {
         std::string n1 = e1.name();
         std::string n2 = e2.name();
         std::transform(n1.begin(), n1.end(), n1.begin(), tolower);
         std::transform(n2.begin(), n2.end(), n2.begin(), tolower);
         return n1 < n2;
      }
      else
         return p1 < p2;
   }
   else
      return s1 < s2;
}
Beispiel #6
0
/**
  * To know if a given entry is already in queue. It depends of (shared dir id, name, path).
  */
bool DownloadQueue::isEntryAlreadyQueued(const Protos::Common::Entry& localEntry)
{
   QMap<std::string, Download*>::const_iterator i = this->downloadsIndexedByName.constFind(localEntry.name());

   while (i != this->downloadsIndexedByName.constEnd() && i.key() == localEntry.name())
   {
      if (
         i.value()->getLocalEntry().path() == localEntry.path() &&
         (!localEntry.has_shared_dir() || i.value()->getLocalEntry().shared_dir().id().hash() == localEntry.shared_dir().id().hash())
      )
         return true;

       ++i;
   }

   return false;
}
Beispiel #7
0
void Tests::getHashesFromAFileEntry1()
{
   qDebug() << "===== getHashesFromAFileEntry1() =====";

   // Find the id of the first shared directory.
   Protos::Common::Entries sharedDirs = this->fileManager->getEntries();
   const string sharedDirId = sharedDirs.entry(1).shared_dir().id().hash();

   Protos::Common::Entry entry;
   entry.set_path("/share1/");
   entry.set_name("r.txt");
   entry.mutable_shared_dir()->mutable_id()->set_hash(sharedDirId);
   QSharedPointer<IGetHashesResult> result = this->fileManager->getHashes(entry);

   HashesReceiver hashesReceiver;
   connect(result.data(), SIGNAL(nextHash(Common::Hash)), &hashesReceiver, SLOT(nextHash(Common::Hash)));

   Protos::Core::GetHashesResult res = result->start();

   QCOMPARE(res.status(), Protos::Core::GetHashesResult_Status_OK);
   QVERIFY(hashesReceiver.waitToReceive(QList<Common::Hash>() << Common::Hash::fromStr("97d464813598e2e4299b5fe7db29aefffdf2641d"), 500));
}
Beispiel #8
0
/**
  * Try to select an entry from a remote peer in the browse tab.
  * The entry to browse is set in 'this->remoteEntryToBrowse'.
  */
void BrowseWidget::tryToReachEntryToBrowse()
{
    if (!this->tryingToReachEntryToBrowse)
        return;

    // First we search for the shared directory of the entry.
    for (int r = 0; r < this->browseModel.rowCount(); r++)
    {
        QModelIndex currentIndex = this->browseModel.index(r, 0);
        Protos::Common::Entry root = this->browseModel.getEntry(currentIndex);
        if (root.has_shared_dir() && this->remoteEntryToBrowse.has_shared_dir() && root.shared_dir().id().hash() == this->remoteEntryToBrowse.shared_dir().id().hash())
        {
            // Then we try to match each folder name. If a folder cannot be reached then we ask to expand the last folder.
            // After the folder entries are loaded, 'tryToReachEntryToBrowse()' will be recalled via the signal 'BrowseModel::loadingResultFinished()'.
            const QStringList& path = Common::ProtoHelper::getStr(this->remoteEntryToBrowse, &Protos::Common::Entry::path).append(Common::ProtoHelper::getStr(this->remoteEntryToBrowse, &Protos::Common::Entry::name)).split('/', QString::SkipEmptyParts);
            for (QStringListIterator i(path); i.hasNext();)
            {
                QModelIndex childIndex = this->browseModel.searchChild(i.next(), currentIndex);
                if (!childIndex.isValid())
                {
                    this->ui->treeView->expand(currentIndex);
                    return;
                }
                currentIndex = childIndex;

                // We reach the last entry name (file or directory), we just have to show and select it.
                if (!i.hasNext())
                {
                    this->ui->treeView->scrollTo(currentIndex);
                    this->ui->treeView->selectionModel()->select(currentIndex, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
                }
            }
        }
    }

    this->tryingToReachEntryToBrowse = false;
}
Beispiel #9
0
/**
  * Will append the shared directory name to the relative path.
  */
QString SearchModel::SearchTree::entryPath(const Protos::Common::Entry& entry)
{
   const QString path = Common::ProtoHelper::getStr(entry, &Protos::Common::Entry::path);

   QString completePath;
   if (path.isEmpty())
      completePath.append("/");
   else
   {
      const QString sharedName = Common::ProtoHelper::getStr(entry.shared_dir(), &Protos::Common::SharedDir::shared_name);
      completePath.append("/").append(sharedName).append(path);
   }

   return completePath;
}