Skip to content

Commit 61452de

Browse files
committedAug 30, 2018
Socket API: add an option to replace existing files with virtual files
Issue #6726
1 parent aefeb08 commit 61452de

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed
 

‎src/gui/socketapi.cpp

+48-2
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,41 @@ void SocketApi::command_DOWNLOAD_VIRTUAL_FILE(const QString &filesArg, SocketLis
621621
}
622622
}
623623

624+
/* Go over all the files ans replace them by a virtual file */
625+
void SocketApi::command_REPLACE_VIRTUAL_FILE(const QString &filesArg, SocketListener *)
626+
{
627+
QStringList files = filesArg.split(QLatin1Char('\x1e')); // Record Separator
628+
auto suffix = QStringLiteral(APPLICATION_DOTVIRTUALFILE_SUFFIX);
629+
630+
for (const auto &file : files) {
631+
QString relativePath;
632+
auto folder = FolderMan::instance()->folderForPath(file, &relativePath);
633+
if (!folder)
634+
continue;
635+
if (file.endsWith(suffix))
636+
continue;
637+
QFileInfo fi(file);
638+
if (fi.isDir()) {
639+
folder->journalDb()->getFilesBelowPath(relativePath.toUtf8(), [&](const SyncJournalFileRecord &rec) {
640+
if (rec._type != ItemTypeFile || rec._path.endsWith(APPLICATION_DOTVIRTUALFILE_SUFFIX))
641+
return;
642+
QString file = folder->path() + '/' + QString::fromUtf8(rec._path);
643+
if (!FileSystem::rename(file, file + suffix)) {
644+
qCWarning(lcSocketApi) << "Unable to rename " << file;
645+
}
646+
});
647+
continue;
648+
}
649+
SyncJournalFileRecord record;
650+
if (!folder->journalDb()->getFileRecord(relativePath, &record) || !record.isValid())
651+
continue;
652+
if (!FileSystem::rename(file, file + suffix)) {
653+
qCWarning(lcSocketApi) << "Unable to rename " << file;
654+
}
655+
FolderMan::instance()->scheduleFolder(folder);
656+
}
657+
}
658+
624659
void SocketApi::emailPrivateLink(const QString &link)
625660
{
626661
Utility::openEmailComposer(
@@ -763,12 +798,23 @@ void SocketApi::command_GET_MENU_ITEMS(const QString &argument, OCC::SocketListe
763798
if (folder) {
764799
auto virtualFileSuffix = QStringLiteral(APPLICATION_DOTVIRTUALFILE_SUFFIX);
765800
bool hasVirtualFile = false;
801+
bool hasNormalFiles = false;
802+
bool hasDir = false;
766803
for (const auto &file : files) {
767-
if (file.endsWith(virtualFileSuffix) || (folder->useVirtualFiles() && QFileInfo(file).isDir()))
804+
if (QFileInfo(file).isDir()) {
805+
hasDir = true;
806+
} else if (file.endsWith(virtualFileSuffix)) {
768807
hasVirtualFile = true;
808+
} else if (!hasNormalFiles) {
809+
bool isOnTheServer = FileData::get(file).journalRecord().isValid();
810+
hasNormalFiles = isOnTheServer;
811+
}
769812
}
770-
if (hasVirtualFile)
813+
if (hasVirtualFile || (hasDir && folder->useVirtualFiles()))
771814
listener->sendMessage(QLatin1String("MENU_ITEM:DOWNLOAD_VIRTUAL_FILE::") + tr("Download file(s)", "", files.size()));
815+
816+
if ((hasNormalFiles || hasDir) && folder->useVirtualFiles())
817+
listener->sendMessage(QLatin1String("MENU_ITEM:REPLACE_VIRTUAL_FILE::") + tr("Replace file(s) by virtual file", "", files.size()));
772818
}
773819

774820
listener->sendMessage(QString("GET_MENU_ITEMS:END"));

‎src/gui/socketapi.h

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ private slots:
108108
Q_INVOKABLE void command_EMAIL_PRIVATE_LINK(const QString &localFile, SocketListener *listener);
109109
Q_INVOKABLE void command_OPEN_PRIVATE_LINK(const QString &localFile, SocketListener *listener);
110110
Q_INVOKABLE void command_DOWNLOAD_VIRTUAL_FILE(const QString &filesArg, SocketListener *listener);
111+
Q_INVOKABLE void command_REPLACE_VIRTUAL_FILE(const QString &filesArg, SocketListener *listener);
111112

112113
// Fetch the private link and call targetFun
113114
void fetchPrivateLinkUrlHelper(const QString &localFile, const std::function<void(const QString &url)> &targetFun);

0 commit comments

Comments
 (0)
Please sign in to comment.