@@ -2369,3 +2369,90 @@ QString OwnCloudService::fetchNextcloudAccountId(const QString &serverUrl, const
23692369}
23702370
23712371void OwnCloudService::unsetShareDialog () { shareDialog = nullptr ; }
2372+
2373+ /* *
2374+ * Fetches the file ID for a note from Nextcloud
2375+ *
2376+ * @param note
2377+ * @return the file ID as a QString, empty if not found
2378+ */
2379+ QString OwnCloudService::fetchNoteFileId (const Note ¬e) {
2380+ if (!isOwnCloudSupportEnabled ()) {
2381+ return {};
2382+ }
2383+
2384+ auto *manager = new QNetworkAccessManager (this );
2385+ QEventLoop loop;
2386+ QTimer timer;
2387+
2388+ timer.setSingleShot (true );
2389+ QObject::connect (&timer, SIGNAL (timeout ()), &loop, SLOT (quit ()));
2390+ QObject::connect (manager, SIGNAL (finished (QNetworkReply *)), &loop, SLOT (quit ()));
2391+
2392+ // 10 sec timeout for the request
2393+ timer.start (10000 );
2394+
2395+ // Get the relative path of the note file on the server
2396+ QString noteRelativePath = note.relativeNoteFilePath (QStringLiteral (" /" ));
2397+ QString notesPath = NoteFolder::currentRemotePath ();
2398+ QString filePath = notesPath + QStringLiteral (" /" ) + noteRelativePath;
2399+
2400+ QUrl url (serverUrl % webdavPath () % QStringLiteral (" /" ) % filePath);
2401+ QNetworkRequest r (url);
2402+ addAuthHeader (&r);
2403+
2404+ // Build the PROPFIND request body to get the file ID
2405+ QString body = QStringLiteral (
2406+ " <?xml version=\" 1.0\" ?>"
2407+ " <d:propfind xmlns:d=\" DAV:\" xmlns:oc=\" http://owncloud.org/ns\" >"
2408+ " <d:prop>"
2409+ " <oc:fileid />"
2410+ " </d:prop>"
2411+ " </d:propfind>" );
2412+
2413+ auto dataToSend = new QByteArray (body.toUtf8 ());
2414+ r.setHeader (QNetworkRequest::ContentLengthHeader, dataToSend->size ());
2415+ r.setHeader (QNetworkRequest::ContentTypeHeader, QStringLiteral (" application/xml" ));
2416+ auto *buffer = new QBuffer (dataToSend);
2417+
2418+ QNetworkReply *reply = manager->sendCustomRequest (r, " PROPFIND" , buffer);
2419+ ignoreSslErrorsIfAllowed (reply);
2420+ loop.exec ();
2421+
2422+ QString fileId;
2423+
2424+ // if we didn't get a timeout let us parse the response
2425+ if (timer.isActive ()) {
2426+ int statusCode = reply->attribute (QNetworkRequest::HttpStatusCodeAttribute).toInt ();
2427+
2428+ // only get the data if the status code was "success"
2429+ if (statusCode >= 200 && statusCode < 300 ) {
2430+ QString data = QString (reply->readAll ());
2431+
2432+ QDomDocument doc;
2433+ doc.setContent (data, true );
2434+
2435+ // Parse the WebDAV XML response to get the file ID
2436+ QDomNodeList responseNodes =
2437+ doc.elementsByTagNameNS (NS_DAV, QStringLiteral (" response" ));
2438+
2439+ if (responseNodes.count () > 0 ) {
2440+ QDomNode responseNode = responseNodes.at (0 );
2441+ if (responseNode.isElement ()) {
2442+ QDomElement elem = responseNode.toElement ();
2443+ QDomNodeList fileIdNodes = elem.elementsByTagNameNS (
2444+ QStringLiteral (" http://owncloud.org/ns" ), QStringLiteral (" fileid" ));
2445+
2446+ if (fileIdNodes.length () > 0 ) {
2447+ fileId = fileIdNodes.at (0 ).toElement ().text ();
2448+ }
2449+ }
2450+ }
2451+ }
2452+ }
2453+
2454+ reply->deleteLater ();
2455+ delete (manager);
2456+
2457+ return fileId;
2458+ }
0 commit comments