PlaceHolders: Trigger a download of the placeholder and open it
authorOlivier Goffart <ogoffart@woboq.com>
Mon, 15 Jan 2018 15:46:52 +0000 (16:46 +0100)
committerKevin Ottens <kevin.ottens@nextcloud.com>
Tue, 15 Dec 2020 09:57:48 +0000 (10:57 +0100)
src/gui/application.cpp
src/gui/application.h
src/gui/folder.cpp
src/gui/folder.h

index a408b392bfa5338088f46b83a4f34141a3c0f21d..c0936f3bd8e8a117ea0eb38485ab890e8b3af1c1 100644 (file)
@@ -499,6 +499,9 @@ void Application::parseOptions(const QStringList &options)
             _backgroundMode = true;
         } else if (option == QLatin1String("--version") || option == QLatin1String("-v")) {
             _versionOnly = true;
+        } else if (option.endsWith(".owncloud")) {
+            // placeholder file, open it after the Folder were created (if the app is not terminated)
+            QTimer::singleShot(0, this, [this, option] { openPlaceholder(option); });
         } else {
             showHint("Unrecognized option '" + option.toStdString() + "'");
         }
@@ -674,4 +677,29 @@ void Application::slotGuiIsShowingSettings()
     emit isShowingSettingsDialog();
 }
 
+void Application::openPlaceholder(const QString &filename)
+{
+    QLatin1String placeholderExt(".owncloud");
+    if (!filename.endsWith(placeholderExt)) {
+        qWarning(lcApplication) << "Can only handle file ending in .owncloud. Unable to open" << filename;
+        return;
+    }
+    QString normalName = filename.left(filename.size() - placeholderExt.size());
+    auto folder = FolderMan::instance()->folderForPath(filename);
+    if (!folder) {
+        qWarning(lcApplication) << "Can't find sync folder for" << filename;
+        // TODO: show a QMessageBox for errors
+        return;
+    }
+    QString relativePath = QDir::cleanPath(normalName).mid(folder->cleanPath().length() + 1);
+    folder->downloadPlaceholder(relativePath);
+    auto con = QSharedPointer<QMetaObject::Connection>::create();
+    *con = QObject::connect(folder, &Folder::syncFinished, [con, normalName] {
+        QObject::disconnect(*con);
+        if (QFile::exists(normalName)) {
+            QDesktopServices::openUrl(QUrl::fromLocalFile(normalName));
+        }
+    });
+}
+
 } // namespace OCC
index 36d994a311523cf0d2e2ad8a1daaa5f2147a3831..a184cf5ef3777c15ff14794061ed9a500a24e315 100644 (file)
@@ -73,6 +73,10 @@ public slots:
     // TODO: this should not be public
     void slotownCloudWizardDone(int);
     void slotCrash();
+    /**
+     * Will download a placeholder file, and open the result.
+     */
+    void openPlaceholder(const QString &filename);
 
 protected:
     void parseOptions(const QStringList &);
index d6f84937e8f987419e7fc7b675c47974be4d31c3..8cd997340bfb2037f3b43690b85cd2e5f8861def 100644 (file)
@@ -514,6 +514,26 @@ void Folder::slotWatchedPathChanged(const QString &path)
     scheduleThisFolderSoon();
 }
 
+void Folder::downloadPlaceholder(const QString &_relativepath)
+{
+    qCInfo(lcFolder) << "Download placeholder: " << _relativepath;
+    auto relativepath = _relativepath.toUtf8();
+
+    // Set in the database that we should download the file
+    SyncJournalFileRecord record;
+    _journal.getFileRecord(relativepath, &record);
+    if (!record.isValid())
+        return;
+    record._type = ItemTypePlaceholderDownload;
+    _journal.setFileRecord(record);
+
+    // Make sure we go over that file during the discovery
+    _journal.avoidReadFromDbOnNextSync(relativepath);
+
+    // Schedule a sync (Folder man will start the sync in a few ms)
+    slotScheduleThisFolder();
+}
+
 void Folder::saveToSettings() const
 {
     // Remove first to make sure we don't get duplicates
index 6f6423b7a948c14c463a7abe9ad372ea6e3751c2..095a813d0fe5eed2d3d1ef55cc9fff620fb58ec4 100644 (file)
@@ -276,6 +276,11 @@ public slots:
        */
     void slotWatchedPathChanged(const QString &path);
 
+    /**
+     * Mark a placeholder as being ready for download, and start a sync.
+     */
+    void downloadPlaceholder(const QString &relativepath);
+
 private slots:
     void slotSyncStarted();
     void slotSyncFinished(bool);