void UploadToMegaDialog::on_bOK_clicked() { MegaNode *node = megaApi->getNodeByPath(ui->eFolderPath->text().toUtf8().constData()); if (node && node->isFolder()) { selectedHandle = node->getHandle(); delete node; accept(); return; } delete node; if (!ui->eFolderPath->text().compare(QString::fromUtf8("/MEGAsync Uploads"))) { ui->bChange->setEnabled(false); ui->bOK->setEnabled(false); MegaNode *rootNode = megaApi->getRootNode(); if (!rootNode) { return; } megaApi->createFolder(tr("MEGAsync Uploads").toUtf8().constData(), rootNode, delegateListener); delete rootNode; return; } MegaApi::log(MegaApi::LOG_LEVEL_ERROR, QString::fromUtf8("Folder not found: %1").arg(ui->eFolderPath->text()).toUtf8().constData()); ui->eFolderPath->setText(QString::fromUtf8("/MEGAsync Uploads")); return; }
void NodeSelector::onRequestFinish(MegaApi *, MegaRequest *request, MegaError *e) { ui->bNewFolder->setEnabled(true); ui->bOk->setEnabled(true); if (e->getErrorCode() != MegaError::API_OK) { QMessageBox::critical(this, QString::fromUtf8("MEGAsync"), tr("Error") + QString::fromUtf8(": ") + QCoreApplication::translate("MegaError", e->getErrorString())); ui->tMegaFolders->setEnabled(true); return; } if(request->getType() == MegaRequest::TYPE_CREATE_FOLDER) { if (e->getErrorCode() == MegaError::API_OK) { MegaNode *node = megaApi->getNodeByHandle(request->getNodeHandle()); if (node) { QModelIndex row = model->insertNode(node, selectedItem); setSelectedFolderHandle(node->getHandle()); ui->tMegaFolders->selectionModel()->select(row, QItemSelectionModel::ClearAndSelect); ui->tMegaFolders->selectionModel()->setCurrentIndex(row, QItemSelectionModel::ClearAndSelect); } } else { QMessageBox::critical(this, QString::fromUtf8("MEGAsync"), tr("Error") + QString::fromUtf8(": ") + QCoreApplication::translate("MegaError", e->getErrorString())); } } else if (request->getType() == MegaRequest::TYPE_REMOVE || request->getType() == MegaRequest::TYPE_MOVE) { if (e->getErrorCode() == MegaError::API_OK) { MegaNode *parent = model->getNode(selectedItem.parent()); if (parent) { model->removeNode(selectedItem); setSelectedFolderHandle(parent->getHandle()); } } } ui->tMegaFolders->setEnabled(true); }
void LinkProcessor::importLinks(MegaNode *node) { if (!node) { return; } MegaNodeList *children = megaApi->getChildren(node); importParentFolder = node->getHandle(); for (int i = 0; i < linkList.size(); i++) { if (!linkNode[i]) { MegaApi::log(MegaApi::LOG_LEVEL_ERROR, "Trying to import a NULL node"); } if (linkNode[i] && linkSelected[i] && !linkError[i]) { bool dupplicate = false; long long dupplicateHandle; const char* name = linkNode[i]->getName(); long long size = linkNode[i]->getSize(); for (int j = 0; j < children->size(); j++) { MegaNode *child = children->get(j); if (!strcmp(name, child->getName()) && (size == child->getSize())) { dupplicate = true; dupplicateHandle = child->getHandle(); } } if (!dupplicate) { remainingNodes++; megaApi->copyNode(linkNode[i], node, delegateListener); } else { emit onDupplicateLink(linkList[i], QString::fromUtf8(name), dupplicateHandle); } } } delete children; }
void UploadToMegaDialog::onRequestFinish(MegaApi *, MegaRequest *request, MegaError *e) { ui->bChange->setEnabled(true); ui->bOK->setEnabled(true); MegaNode *node = megaApi->getNodeByHandle(request->getNodeHandle()); if (e->getErrorCode() != MegaError::API_OK || !node) { MegaApi::log(MegaApi::LOG_LEVEL_ERROR, QString::fromAscii("Request error: %1") .arg(QCoreApplication::translate("MegaError", e->getErrorString())).toUtf8().constData()); this->reject(); delete node; return; } selectedHandle = node->getHandle(); delete node; accept(); }
void NodeSelector::onSelectionChanged(QItemSelection, QItemSelection) { if (ui->tMegaFolders->selectionModel()->selectedIndexes().size()) { selectedItem = ui->tMegaFolders->selectionModel()->selectedIndexes().at(0); MegaNode *node = model->getNode(selectedItem); if (node) { selectedFolder = node->getHandle(); } else { selectedFolder = mega::INVALID_HANDLE; } } else { selectedItem = QModelIndex(); selectedFolder = mega::INVALID_HANDLE; } }
void UploadToMegaDialog::on_bChange_clicked() { QPointer<NodeSelector> nodeSelector = new NodeSelector(megaApi, NodeSelector::UPLOAD_SELECT, this); MegaNode *defaultNode = megaApi->getNodeByPath(ui->eFolderPath->text().toUtf8().constData()); if (defaultNode) { nodeSelector->setSelectedFolderHandle(defaultNode->getHandle()); delete defaultNode; } int result = nodeSelector->exec(); if (!nodeSelector || result != QDialog::Accepted) { delete nodeSelector; return; } MegaHandle selectedMegaFolderHandle = nodeSelector->getSelectedFolderHandle(); MegaNode *node = megaApi->getNodeByHandle(selectedMegaFolderHandle); if (!node) { delete nodeSelector; return; } const char *nPath = megaApi->getNodePath(node); if (!nPath) { delete nodeSelector; delete node; return; } ui->eFolderPath->setText(QString::fromUtf8(nPath)); delete nodeSelector; delete [] nPath; delete node; }
void NodeSelector::on_bNewFolder_clicked() { QPointer<QInputDialog> id = new QInputDialog(this); id->setWindowTitle(tr("New folder")); id->setLabelText(tr("Enter the new folder name:")); int result = id->exec(); if (!id || !result) { delete id; return; } QString text = id->textValue(); text = text.trimmed(); if (!text.isEmpty()) { MegaNode *parent = megaApi->getNodeByHandle(selectedFolder); if (!parent) { parent = megaApi->getRootNode(); if (!parent) { delete id; return; } selectedFolder = parent->getHandle(); selectedItem = QModelIndex(); } MegaNode *node = megaApi->getNodeByPath(text.toUtf8().constData(), parent); if (!node || node->isFile()) { ui->bNewFolder->setEnabled(false); ui->bOk->setEnabled(false); ui->tMegaFolders->setEnabled(false); megaApi->createFolder(text.toUtf8().constData(), parent, delegateListener); } else { for (int i = 0; i < model->rowCount(selectedItem); i++) { QModelIndex row = model->index(i, 0, selectedItem); MegaNode *node = model->getNode(row); if (node && text.compare(QString::fromUtf8(node->getName())) == 0) { setSelectedFolderHandle(node->getHandle()); ui->tMegaFolders->selectionModel()->select(row, QItemSelectionModel::ClearAndSelect); ui->tMegaFolders->selectionModel()->setCurrentIndex(row, QItemSelectionModel::ClearAndSelect); break; } } } delete parent; delete node; } else { QMessageBox::critical(this, QString::fromUtf8("MEGAsync"), tr("Please enter a valid folder name")); } delete id; }
void NodeSelector::setSelectedFolderHandle(long long selectedHandle) { MegaNode *node = megaApi->getNodeByHandle(selectedHandle); if (!node) { return; } QList<MegaNode *> list; while (node) { list.append(node); node = megaApi->getParentNode(node); } if (!list.size()) { return; } int index = list.size() - 1; QModelIndex modelIndex; QModelIndex parentModelIndex; node = list.at(index); for (int i = 0; i < model->rowCount(); i++) { QModelIndex tmp = model->index(i, 0); MegaNode *n = model->getNode(tmp); if (n && n->getHandle() == node->getHandle()) { node = NULL; parentModelIndex = modelIndex; modelIndex = tmp; index--; ui->tMegaFolders->expand(parentModelIndex); break; } } if (node) { for (int k = 0; k < list.size(); k++) { delete list.at(k); } ui->tMegaFolders->collapseAll(); return; } while (index >= 0) { node = list.at(index); for (int j = 0; j < model->rowCount(modelIndex); j++) { QModelIndex tmp = model->index(j, 0, modelIndex); MegaNode *n = model->getNode(tmp); if (n && n->getHandle() == node->getHandle()) { node = NULL; parentModelIndex = modelIndex; modelIndex = tmp; index--; ui->tMegaFolders->expand(parentModelIndex); break; } } if (node) { for (int k = 0; k < list.size(); k++) { delete list.at(k); } ui->tMegaFolders->collapseAll(); return; } } for (int k = 0; k < list.size(); k++) { delete list.at(k); } ui->tMegaFolders->selectionModel()->setCurrentIndex(modelIndex, QItemSelectionModel::ClearAndSelect); ui->tMegaFolders->selectionModel()->select(modelIndex, QItemSelectionModel::ClearAndSelect); }
/** * @brief TEST_F SdkTestShares * * Initialize a test scenario by: * * - Creating/uploading some folders/files to share * - Creating a new contact to share to * * Performs different operations related to sharing: * * - Share a folder with an existing contact * - Check the correctness of the outgoing share * - Check the reception and correctness of the incoming share * - Modify the access level * - Revoke the access to the share * - Share a folder with a non registered email * - Check the correctness of the pending outgoing share * - Create a public link * - Import a public link * - Get a node from public link * - Remove a public link */ TEST_F(SdkTest, SdkTestShares) { MegaShareList *sl; MegaShare *s; MegaNodeList *nl; MegaNode *n; MegaNode *n1; getMegaApiAux(); // login + fetchnodes // Initialize a test scenario : create some folders/files to share // Create some nodes to share // |--Shared-folder // |--subfolder // |--file.txt MegaNode *rootnode = megaApi->getRootNode(); char foldername1[64] = "Shared-folder"; MegaHandle hfolder1; responseReceived = false; megaApi->createFolder(foldername1, rootnode); waitForResponse(&responseReceived); ASSERT_EQ(MegaError::API_OK, lastError) << "Cannot create a folder (error: " << lastError << ")"; hfolder1 = h; // 'h' is set in 'onRequestFinish()' n1 = megaApi->getNodeByHandle(hfolder1); char foldername2[64] = "subfolder"; MegaHandle hfolder2; responseReceived = false; megaApi->createFolder(foldername2, megaApi->getNodeByHandle(hfolder1)); waitForResponse(&responseReceived); ASSERT_EQ(MegaError::API_OK, lastError) << "Cannot create a folder (error: " << lastError << ")"; hfolder2 = h; MegaHandle hfile1; createFile(PUBLICFILE.data(), false); // not a large file since don't need to test transfers here uploadFinished = false; megaApi->startUpload(PUBLICFILE.data(), megaApi->getNodeByHandle(hfolder1)); waitForResponse(&uploadFinished); ASSERT_EQ(MegaError::API_OK, lastError) << "Cannot upload file (error: " << lastError << ")"; hfile1 = h; // Initialize a test scenario: create a new contact to share to string message = "Hi contact. Let's share some stuff"; contactRequestUpdated = false; contactRequestUpdatedAux = false; ASSERT_NO_FATAL_FAILURE( inviteContact(emailaux, message, MegaContactRequest::INVITE_ACTION_ADD) ); waitForResponse(&contactRequestUpdatedAux); // at the target side (auxiliar account) waitForResponse(&contactRequestUpdated); // at the source side (main account) MegaContactRequestList *crlaux = megaApiAux->getIncomingContactRequests(); ASSERT_EQ(1, crlaux->size()) << "Too many incoming contact requests in auxiliar account"; MegaContactRequest *craux = crlaux->get(0); contactRequestUpdated = false; contactRequestUpdatedAux = false; ASSERT_NO_FATAL_FAILURE( replyContact(craux, MegaContactRequest::REPLY_ACTION_ACCEPT) ); waitForResponse(&contactRequestUpdatedAux); // at the target side (auxiliar account) waitForResponse(&contactRequestUpdated); // at the source side (main account) delete crlaux; // --- Create a new outgoing share --- nodeUpdated = false; nodeUpdatedAux = false; ASSERT_NO_FATAL_FAILURE( shareFolder(n1, emailaux.data(), MegaShare::ACCESS_READ) ); waitForResponse(&nodeUpdated); waitForResponse(&nodeUpdatedAux); // --- Check the outgoing share --- sl = megaApi->getOutShares(); ASSERT_EQ(1, sl->size()) << "Outgoing share failed"; s = sl->get(0); ASSERT_EQ(MegaShare::ACCESS_READ, s->getAccess()) << "Wrong access level of outgoing share"; ASSERT_EQ(hfolder1, s->getNodeHandle()) << "Wrong node handle of outgoing share"; ASSERT_STREQ(emailaux.data(), s->getUser()) << "Wrong email address of outgoing share"; ASSERT_TRUE(megaApi->isShared(n1)) << "Wrong sharing information at outgoing share"; ASSERT_TRUE(megaApi->isOutShare(n1)) << "Wrong sharing information at outgoing share"; delete sl; // --- Check the incoming share --- nl = megaApiAux->getInShares(megaApiAux->getContact(email.data())); ASSERT_EQ(1, nl->size()) << "Incoming share not received in auxiliar account"; n = nl->get(0); ASSERT_EQ(hfolder1, n->getHandle()) << "Wrong node handle of incoming share"; ASSERT_STREQ(foldername1, n->getName()) << "Wrong folder name of incoming share"; ASSERT_EQ(MegaError::API_OK, megaApiAux->checkAccess(n, MegaShare::ACCESS_READ).getErrorCode()) << "Wrong access level of incoming share"; ASSERT_TRUE(megaApiAux->isInShare(n)) << "Wrong sharing information at incoming share"; ASSERT_TRUE(megaApiAux->isShared(n)) << "Wrong sharing information at incoming share"; delete nl; // --- Modify the access level of an outgoing share --- nodeUpdated = false; nodeUpdatedAux = false; ASSERT_NO_FATAL_FAILURE( shareFolder(megaApi->getNodeByHandle(hfolder1), emailaux.data(), MegaShare::ACCESS_READWRITE) ); waitForResponse(&nodeUpdated); waitForResponse(&nodeUpdatedAux); nl = megaApiAux->getInShares(megaApiAux->getContact(email.data())); ASSERT_EQ(1, nl->size()) << "Incoming share not received in auxiliar account"; n = nl->get(0); ASSERT_EQ(MegaError::API_OK, megaApiAux->checkAccess(n, MegaShare::ACCESS_READWRITE).getErrorCode()) << "Wrong access level of incoming share"; delete nl; // --- Revoke access to an outgoing share --- nodeUpdated = false; nodeUpdatedAux = false; ASSERT_NO_FATAL_FAILURE( shareFolder(n1, emailaux.data(), MegaShare::ACCESS_UNKNOWN) ); waitForResponse(&nodeUpdated); waitForResponse(&nodeUpdatedAux); sl = megaApi->getOutShares(); ASSERT_EQ(0, sl->size()) << "Outgoing share revocation failed"; delete sl; nl = megaApiAux->getInShares(megaApiAux->getContact(email.data())); ASSERT_EQ(0, nl->size()) << "Incoming share revocation failed"; delete nl; // --- Get pending outgoing shares --- char emailfake[64]; srand (time(NULL)); sprintf(emailfake, "*****@*****.**", rand()%1000000); // carefull, antispam rejects too many tries without response for the same address n = megaApi->getNodeByHandle(hfolder2); nodeUpdated = false; ASSERT_NO_FATAL_FAILURE( shareFolder(n, emailfake, MegaShare::ACCESS_FULL) ); waitForResponse(&nodeUpdated); sl = megaApi->getPendingOutShares(n); delete n; ASSERT_EQ(1, sl->size()) << "Pending outgoing share failed"; s = sl->get(0); n = megaApi->getNodeByHandle(s->getNodeHandle()); // ASSERT_STREQ(emailfake, s->getUser()) << "Wrong email address of outgoing share"; User is not created yet ASSERT_FALSE(megaApi->isShared(n)) << "Node is already shared, must be pending"; ASSERT_FALSE(megaApi->isOutShare(n)) << "Node is already shared, must be pending"; delete sl; delete n; // --- Create a public link --- MegaNode *nfile1 = megaApi->getNodeByHandle(hfile1); ASSERT_NO_FATAL_FAILURE( createPublicLink(nfile1) ); // The created link is stored in this->link at onRequestFinish() // --- Import a public link --- ASSERT_NO_FATAL_FAILURE( importPublicLink(link, rootnode) ); MegaNode *nimported = megaApi->getNodeByHandle(h); ASSERT_STREQ(nfile1->getName(), nimported->getName()) << "Imported file with wrong name"; ASSERT_EQ(rootnode->getHandle(), nimported->getParentHandle()) << "Imported file in wrong path"; // --- Get node from public link --- ASSERT_NO_FATAL_FAILURE( getPublicNode(link) ); ASSERT_TRUE(publicNode->isPublic()) << "Cannot get a node from public link"; // --- Remove a public link --- ASSERT_NO_FATAL_FAILURE( removePublicLink(nfile1) ); delete nfile1; nfile1 = megaApi->getNodeByHandle(h); ASSERT_FALSE(nfile1->isPublic()) << "Public link removal failed (still public)"; delete nimported; }
void MegaUploader::upload(QFileInfo info, MegaNode *parent) { QApplication::processEvents(); MegaNodeList *children = megaApi->getChildren(parent); QByteArray utf8name = info.fileName().toUtf8(); QString currentPath = QDir::toNativeSeparators(info.absoluteFilePath()); MegaNode *dupplicate = NULL; for (int i = 0; i < children->size(); i++) { MegaNode *child = children->get(i); if (!strcmp(utf8name.constData(), child->getName()) && ((info.isDir() && (child->getType() == MegaNode::TYPE_FOLDER)) || (info.isFile() && (child->getType() == MegaNode::TYPE_FILE) && (info.size() == child->getSize())))) { dupplicate = child->copy(); break; } } delete children; if (dupplicate) { if (dupplicate->getType() == MegaNode::TYPE_FILE) { emit dupplicateUpload(info.absoluteFilePath(), info.fileName(), dupplicate->getHandle()); } if (dupplicate->getType() == MegaNode::TYPE_FOLDER) { QDir dir(info.absoluteFilePath()); QFileInfoList entries = dir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot); for (int i = 0; i < entries.size(); i++) { upload(entries[i], dupplicate); } } delete dupplicate; return; } string localPath = megaApi->getLocalPath(parent); if (localPath.size() && megaApi->isSyncable(info.fileName().toUtf8().constData())) { #ifdef WIN32 QString destPath = QDir::toNativeSeparators(QString::fromWCharArray((const wchar_t *)localPath.data()) + QDir::separator() + info.fileName()); if (destPath.startsWith(QString::fromAscii("\\\\?\\"))) { destPath = destPath.mid(4); } #else QString destPath = QDir::toNativeSeparators(QString::fromUtf8(localPath.data()) + QDir::separator() + info.fileName()); #endif megaApi->moveToLocalDebris(destPath.toUtf8().constData()); QtConcurrent::run(Utilities::copyRecursively, currentPath, destPath); } else if (info.isFile()) { megaApi->startUpload(currentPath.toUtf8().constData(), parent); } else if (info.isDir()) { folders.enqueue(info); megaApi->createFolder(info.fileName().toUtf8().constData(), parent, delegateListener); } }