From c7b0c7b88c1bebb038bd70f4995fc35771fa83c0 Mon Sep 17 00:00:00 2001 From: Mavridis Philippe Date: Sun, 7 Jul 2024 22:22:18 +0300 Subject: [PATCH] Implement TDEFileChooserPortal::SaveFiles Allows selecting a directory to save multiple files while checking for existing files with similar filenames. Signed-off-by: Mavridis Philippe --- src/file_chooser_portal.cpp | 80 ++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/src/file_chooser_portal.cpp b/src/file_chooser_portal.cpp index 0a7b340..ee8b0f6 100644 --- a/src/file_chooser_portal.cpp +++ b/src/file_chooser_portal.cpp @@ -24,6 +24,7 @@ #include // TDE +#include #include #include #include @@ -126,7 +127,84 @@ bool TDEFileChooserPortal::SaveFiles(const TQT_DBusObjectPath& handle, TQMap &results, TQT_DBusError& error) { - return false; + // Get list of files to save + if (!OPTION_VALID("files", "aay")) + { + kdWarning() << "TDEFileChooserPortal::SaveFiles: " + << "Invalid or misssing files option" << endl; + return false; + } + + TQValueList filelist = options["files"].value.toTQValueList(); + TQValueList::iterator fi; + TQStringList files; + for (fi = filelist.begin(); fi != filelist.end(); ++fi) + { + files << bytelist_to_string((*fi).toList().toByteList()); + } + + // Parse options + FileDialogOpts opts; + opts.caption = title; + + if (OPTION_VALID("accept_label", "s")) + opts.okButtonText = options["accept_label"].value.toString(); + + if (OPTION_VALID("modal", "b")) + opts.modal = options["modal"].value.toBool(); + + if (OPTION_VALID("current_folder", "ay")) + opts.startDir = bytelist_to_string(options["current_folder"].value.toList().toByteList()); + + // We can't just use execFileDialog because we need to do some special processing + KFileDialog *dialog = new KFileDialog(opts.startDir, TQString::null, + nullptr, "xdg-tde-file-chooser", + opts.modal); + + dialog->setMode(KFile::LocalOnly | KFile::Directory); + + if (!opts.caption.isNull()) + dialog->setPlainCaption(opts.caption); + + if (!opts.okButtonText.isNull()) + dialog->okButton()->setText(opts.okButtonText); + + if (opts.windowId > 0) KWin::setMainWindow(dialog, opts.windowId); + + if (dialog->exec() == TQDialog::Accepted) + { + TQDir d(dialog->selectedURL().path()); + KURL::List urllist; + for (TQStringList::Iterator it = files.begin(); it != files.end(); ++it) + { + TQString filename = (*it); + TQFileInfo fi(d, filename); + KURL url(fi.absFilePath()); + if (fi.exists()) + { + TDEIO::RenameDlg rename(dialog, opts.caption, TQString::null, + fi.absFilePath(), TDEIO::M_OVERWRITE); + int result = rename.exec(); + switch (result) + { + case TDEIO::R_RENAME: + url = rename.newDestURL(); + break; + case TDEIO::R_CANCEL: + url = KURL(); + break; + } + } + urllist << url.url(); + } + + response = 0; + TQT_DBusDataList urls = kurl_list_to_datalist(urllist); + TQT_DBusVariant var = TQT_DBusData::fromList(urls).getAsVariantData().toVariant(); + results.insert("uris", var); + } + else response = 1; + return true; } void TDEFileChooserPortal::handleMethodReply(const TQT_DBusMessage &reply)